APM:Libraries
SD.h
Go to the documentation of this file.
1 /*
2  night_ghost@ykoctpa.ru 2017
3 
4  a port of SparkFun's SD class to FatFs (c) Chan
5  because it much better and faster than sdfatlib
6 
7  also it was rewritten to:
8 * distinguish between flie at directory by stat(), not by try to open
9 * provide last error code and its text description
10 * added tracking of opened files for global sync()
11 * some new functions added
12 
13  original SparkFun readme below
14 ----------------------------------------------
15 
16  SD - a slightly more friendly wrapper for sdfatlib
17 
18  This library aims to expose a subset of SD card functionality
19  in the form of a higher level "wrapper" object.
20 
21  License: GNU General Public License V3
22  (Because sdfatlib is licensed with this.)
23 
24  (C) Copyright 2010 SparkFun Electronics
25 
26  */
27 
28 #ifndef __SD_H__
29 #define __SD_H__
30 
31 
32 #include <stdio.h>
33 #include <util.h>
34 #include <AP_HAL/AP_HAL.h>
35 
36 #if defined(BOARD_SDCARD_CS_PIN) || defined(BOARD_DATAFLASH_FATFS)
37 
38 #include "Sd2Card.h"
39 #include "SdFatFs.h"
40 
41 
42 // replace FatFs defines to standard ones
43 /*
44 #define FA_READ 0x01
45 #define FA_OPEN_EXISTING 0x00
46 
47 #if !_FS_READONLY
48 #define FA_WRITE 0x02
49 #define FA_CREATE_NEW 0x04
50 #define FA_CREATE_ALWAYS 0x08
51 #define FA_OPEN_ALWAYS 0x10
52 */
53 #define O_RDONLY FA_READ
54 #define O_WRITE FA_WRITE
55 #define O_RDWR FA_WRITE
56 #define O_CREAT FA_CREATE_NEW // создать новый, если существует то ошибка
57 #define O_TRUNC FA_CREATE_ALWAYS // переписать старый
58 #define O_APPEND 0x20
59 #define O_CLOEXEC 0x40
60 
61 #undef EEXIST
62 #undef EACCES
63 #undef EISDIR
64 #undef ENOENT
65 
66 #define EEXIST FR_EXIST
67 #define EACCES FR_DENIED
68 #define EISDIR FR_IS_DIR
69 #define ENOENT FR_NO_FILE
70 
71 // flags for ls()
73 uint8_t const LS_DATE = 1;
75 uint8_t const LS_SIZE = 2;
77 uint8_t const LS_R = 4;
78 
79 typedef void (*cb_putc)(char c);
80 
81 class File {
82  friend class SDClass;
83 public:
84  File(void);
85  File(const char* name);
86  size_t write(uint8_t);
87  size_t write(const uint8_t *buf, size_t size);
88  size_t write(const char *buf, size_t size);
89 
90  int read();
91  UINT gets(char* buf, size_t len);
92  int peek();
93  int available();
94  void flush();
95  int read(void* buf, size_t len);
96  uint8_t seek(uint32_t pos);
97  uint32_t position();
98  uint32_t size();
99  void close();
100  inline operator bool() const { return (_name == NULL)? FALSE : TRUE; } // is open?
101 
102  char* name(void);
103  char* fullname(void) {return _name;};
104  uint8_t isDirectory();
105  File openNextFile(uint8_t mode = FILE_READ);
106  void rewindDirectory(void);
107 
108  size_t print(const char* data);
109  size_t println();
110  size_t println(const char* data);
111 
112  // Print to Serial line
113  void ls(cb_putc cb, uint8_t flags, uint8_t indent = 0);
114  static void printFatDate(uint16_t fatDate, cb_putc cb);
115  static void printFatTime(uint16_t fatTime, cb_putc cb);
116  static void printTwoDigits(uint8_t v, cb_putc cb);
117  static void printNumber(int16_t n, cb_putc cb);
118  static void printStr(const char *s, cb_putc cb);
119 
120  void inline sync() { if(!is_dir) f_sync(&_d.fil); };
121 
122  inline uint32_t firstCluster(){ return is_dir ? _d.dir.obj.sclust : _d.fil.obj.sclust; }
123 
124  static void syncAll();
125  static void addOpenFile(FIL *f);
126  static void removeOpenFile(FIL *f);
127 
128 protected:
129 // should be private?
130  char *_name = NULL; //file or dir name
131 
132 #if 0
133  FIL _fil; // each struct contains sector buffer so this casue twice of memory use
134  DIR _dir;
135 #else
136  union {
137  FIL fil; // each struct contains sector buffer so this casue twice of memory use
138  DIR dir;
139  } _d;
140 
141  bool is_dir;
142 #endif
143 
144 
145 
146 // static list of all open files
147  static FIL* openFiles[16];
148  static uint8_t num_openFiles;
149 
150 };
151 
152 class SDClass {
153 
154 public:
155 
156  /* Initialize the SD peripheral */
157  static uint8_t begin(AP_HAL::OwnPtr<F4Light::SPIDevice> spi);
158  static File open(const char *filepath, uint8_t mode);
159  static File open(const char *filepath);
160  static uint8_t exists(const char *filepath);
161  static uint8_t mkdir(const char *filepath);
162  static uint8_t remove(const char *filepath);
163  static uint8_t rmdir(const char *filepath);
164 
165  static uint32_t getfree(const char *filepath, uint32_t * fssize);
166  static uint8_t stat(const char *filepath, FILINFO* fno);
167  static uint8_t format(const char *filepath);
168 
169  File openRoot(void);
170 
171  void inline sync() { File::syncAll(); };
172 
173  friend class File;
174 
175  static inline Sd2Card *getCard() { return &_card; }
176  static inline SdFatFs *getVolume() { return &_fatFs; }
177 
178  static FRESULT lastError;
179 
180  static const char * strError(FRESULT err){ return SdFatFs::strError(err); }
181 
182 private:
183  static Sd2Card _card;
184  static SdFatFs _fatFs;
185 
186 };
187 
188 extern SDClass SD;
189 
190 #endif
191 #endif
void(* cb_putc)(uint8_t c)
Definition: prototypes.h:3
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
Definition: posix.c:885
Definition: ff.h:226
ssize_t write(int fd, const void *buf, size_t count)
POSIX Write count bytes from *buf to fileno fd.
Definition: posix.c:1169
int rmdir(const char *pathname)
POSIX delete a directory.
Definition: posix.c:1671
const char * name
Definition: BusTest.cpp:11
Definition: ff.h:201
ssize_t read(int fd, void *buf, size_t count)
POSIX read count bytes from *buf to fileno fd.
Definition: posix.c:995
#define f(i)
FRESULT f_sync(FIL *fp)
Definition: ff.c:3945
int close(int fileno)
POSIX Close a file with fileno handel.
Definition: posix.c:675
FRESULT
Definition: ff.h:243
void sync(void)
POSIX Sync all pending file changes and metadata on ALL files.
Definition: posix.c:1058
Miscellaneous utility macros and procedures.
float v
Definition: Printf.cpp:15
int mkdir(const char *pathname, mode_t mode)
POSIX make a directory.
Definition: posix.c:1620
char * gets(char *p)
Definition: posix.c:429
#define NULL
Definition: hal_types.h:59
static int is_dir(const char *path)
Definition: Storage.cpp:27
unsigned int UINT
Definition: integer.h:10
int stat(const char *name, struct stat *buf)
Display struct stat, from POSIX stat(0 or fstat(), in ASCII. NOT POSIX.
Definition: posix.c:1319
Definition: ff.h:173