|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #if !defined(__SD_t3_H__) && defined(__arm__) && defined(USE_TEENSY3_OPTIMIZED_CODE)
- #define __SD_t3_H__
- #define __SD_H__
-
- #include <Arduino.h>
- #include <SPI.h>
- #include "utility/ioreg.h"
-
- #define SD_CACHE_SIZE 7
-
- #define SD_SPI_SPEED SPISettings(25000000, MSBFIRST, SPI_MODE0)
-
- #define FILE_READ 0
- #define FILE_WRITE 1
- #define FILE_DIR 2
- #define FILE_DIR_ROOT16 3
- #define FILE_INVALID 4
-
- class File;
-
- class SDClass
- {
- public:
- static bool begin(uint8_t csPin = SS);
- static File open(const char *path, uint8_t mode = FILE_READ);
- static bool exists(const char *path);
- static bool mkdir(const char *path);
- static bool remove(const char *path);
- static bool rmdir(const char *path);
- private:
- static uint8_t sd_cmd0();
- static uint32_t sd_cmd8();
- static uint8_t sd_acmd41(uint32_t hcs);
- static uint32_t sd_cmd58();
- static bool sd_read(uint32_t addr, void * data);
- static void send_cmd(uint16_t cmd, uint32_t arg);
- static uint8_t recv_r1();
- static uint32_t recv_r3_or_r7();
- static void end_cmd();
- static volatile IO_REG_TYPE * csreg;
- static IO_REG_TYPE csmask;
- static uint8_t card_type;
- static File rootDir;
- static uint32_t fat1_begin_lba;
- static uint32_t fat2_begin_lba;
- static uint32_t data_begin_lba;
- static uint32_t max_cluster;
- static uint8_t sector2cluster;
- static uint8_t fat_type;
- friend class SDCache;
- friend class File;
- typedef struct {
- union {
- struct {
- char name[11];
- uint8_t attrib;
- uint8_t reserved;
- uint8_t ctime_tenth;
- uint16_t ctime;
- uint16_t cdate;
- uint16_t adate;
- uint16_t cluster_high;
- uint16_t wtime;
- uint16_t wdate;
- uint16_t cluster_low;
- uint32_t size;
- };
- struct {
- uint8_t ord;
- uint8_t lname1[10];
- uint8_t lattrib;
- uint8_t type;
- uint8_t cksum;
- uint8_t lname2[12];
- uint16_t lcluster_low;
- uint8_t lname3[4];
- };
- };
- } fatdir_t;
- typedef union {
- uint8_t u8[512];
- uint16_t u16[256];
- uint32_t u32[128];
- fatdir_t dir[16];
- } sector_t;
- };
-
-
- #define ATTR_READ_ONLY 0x01
- #define ATTR_HIDDEN 0x02
- #define ATTR_SYSTEM 0x04
- #define ATTR_VOLUME_ID 0x08
- #define ATTR_DIRECTORY 0x10
- #define ATTR_ARCHIVE 0x20
- #define ATTR_LONG_NAME 0x0F
-
- class File : public Stream
- {
- public:
- File();
- ~File();
-
- virtual size_t write(uint8_t b);
- virtual size_t write(const uint8_t *buf, size_t size);
- virtual int read();
- virtual int peek();
- virtual int available();
- virtual void flush();
- int read(void *buf, uint32_t size);
- bool seek(uint32_t pos);
- uint32_t position() {
- if (type <= FILE_WRITE) return offset;
- return 0;
- }
- uint32_t size() {
- if (type <= FILE_WRITE) return length;
- return 0;
- }
- void close();
- operator bool() {
- return (type < FILE_INVALID);
- }
- char * name() {
- return namestr;
- }
- bool isDirectory() {
- return (type == FILE_DIR) || (type == FILE_DIR_ROOT16);
- }
- File openNextFile(uint8_t mode = FILE_READ);
- void rewindDirectory() {
- rewind();
- }
- using Print::write;
- void rewind() {
- offset = 0;
- current_cluster = start_cluster;
- };
- private:
- bool find(const char *filename, File *found);
- void init(SDClass::fatdir_t *dirent);
- bool next_cluster();
- uint32_t offset;
- uint32_t length;
- uint32_t start_cluster;
- uint32_t current_cluster;
- uint32_t dirent_lba;
- uint8_t dirent_index;
- uint8_t type;
- char namestr[13];
- friend class SDClass;
- static inline uint32_t cluster_number(uint32_t n) {
- return n >> (SDClass::sector2cluster + 9);
- }
- static inline uint32_t cluster_offset(uint32_t n) {
- return n & ((1 << (SDClass::sector2cluster + 9)) - 1);
- }
- static inline uint32_t custer_to_sector(uint32_t n) {
- return (n - 2) * (1 << SDClass::sector2cluster)
- + SDClass::data_begin_lba;
- }
- static inline bool is_new_cluster(uint32_t lba) {
- return (lba & ((1 << SDClass::sector2cluster) - 1)) == 0;
- }
- };
-
- class SDCache
- {
- private:
-
-
-
-
-
-
-
- SDCache(void) { item = NULL; }
- ~SDCache(void) { release(); }
- typedef struct cache_struct {
- SDClass::sector_t data;
- uint32_t lba;
- cache_struct * next;
- uint8_t usagecount;
- uint8_t flags;
- } cache_t;
- SDClass::sector_t * read(uint32_t lba, bool is_fat=false);
- bool read(uint32_t lba, void *buffer);
- cache_t * get(uint32_t lba, bool allocate=true);
- void dirty(void);
- void flush(void);
- void release(void);
- cache_t * item;
- static cache_t *cache_list;
- static cache_t cache[SD_CACHE_SIZE];
- static void init(void);
- static void print_cache(void);
- friend class SDClass;
- friend class File;
- };
-
-
- extern SDClass SD;
-
- #endif
|