sd.h
Go to the documentation of this file.
00001
00044 #ifndef DRV_SD_H
00045 #define DRV_SD_H
00046 
00047 #include "cfg/cfg_sd.h"
00048
00049 #include <io/kfile.h>
00050 #include <io/kblock.h>
00051
00052 #include <fs/fatfs/diskio.h>
00053
00054 typedef struct SdCID
00055 {
00056     uint8_t        manfid;
00057     uint8_t        prod_name[8];
00058     uint32_t       serial;
00059     uint16_t       oemid;
00060     uint32_t       year_off;
00061     uint8_t        m_rev;
00062     uint8_t        l_rev;
00063 }SdCID;
00064
00065 typedef struct SdCSD
00066 {
00067     uint8_t     structure;
00068     uint8_t     ccc;
00069     uint32_t    erase_size;
00070     uint32_t    capacity;
00071     uint32_t    max_data_rate;
00072     uint32_t    block_len;
00073     uint32_t    block_num;
00074     uint32_t    write_blk_bits;
00075     uint32_t    read_blk_bits;
00076     uint8_t     read_partial:1,
00077                 read_misalign:1,
00078                 write_partial:1,
00079                 write_misalign:1;
00080 } SdCSD;
00081
00082 typedef struct SdSSR
00083 {
00084     uint8_t    bus_width;
00085     uint8_t    card_type;
00086     uint8_t    speed_class;
00087     uint8_t    au_size;
00088     uint8_t    erase_size;
00089 } SdSSR;
00090
00091 #define SD_START_DELAY  10
00092 #define SD_INIT_TIMEOUT ms_to_ticks(2000)
00093 #define SD_IDLE_RETRIES 4
00094 #define SD_DEFAULT_BLOCKLEN 512
00095 
00100 #define SD_SDMMC_MODE  0
00101 #define SD_SPI_MODE    1
00102 
00104 #define SD_UNBUFFERED     BV(0) ///< Open SD memory disabling page caching, no modification and partial write are allowed.
00105 
00106 struct SdHardware* hw;
00110 typedef struct Sd
00111 {
00112     KBlock b;
00113     KFile *ch;
00114     struct SdHardware* hw;
00115     uint32_t addr;
00116     uint32_t status;
00117 } Sd;
00118
00119 bool sd_hw_initUnbuf(Sd *sd, KFile *ch);
00120 bool sd_hw_initBuf(Sd *sd, KFile *ch);
00121
00122 bool sd_spi_initUnbuf(Sd *sd, KFile *ch);
00123 bool sd_spi_initBuf(Sd *sd, KFile *ch);
00124
00125 // For old compatibility.
00126 #ifndef CONFIG_SD_MODE
00127     #define CONFIG_SD_MODE  SD_SPI_MODE
00128     #define SD_INCLUDE_SPI_SOURCE
00129 #endif
00130 
00131 #if CONFIG_SD_OLD_INIT
00132 
00133     #if !(ARCH & ARCH_NIGHTTEST)
00134         #warning "Deprecated: this API will be removed in the next major release,"
00135         #warning "please disable CONFIG_SD_OLD_INIT and pass explicitly the SD context to sd_init()."
00136     #endif
00137 
00150     #if CONFIG_SD_MODE == SD_SPI_MODE
00151         #define sd_init(ch) {static struct Sd sd; sd_spi_initUnbuf(&sd, (ch));}
00152     #else
00153         #define sd_init(ch) {static struct Sd sd; sd_hw_initUnbuf(&sd, (ch));}
00154     #endif
00155 
00156 #else
00157 
00170     #if CONFIG_SD_MODE == SD_SPI_MODE
00171         #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_spi_initUnbuf((sd), (ch)) : sd_spi_initBuf((sd), (ch)))
00172     #else
00173         #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_hw_initUnbuf((sd), (ch)) : sd_hw_initBuf((sd), (ch)))
00174     #endif
00175 
00176 #endif
00177 
00178
00179 #define KBT_SD MAKE_ID('S', 'D', 'B', 'K')
00180 
00181 bool sd_test(Sd *sd);
00182 void sd_writeTest(Sd *sd);
00183
00184 INLINE Sd *SD_CAST(KBlock *b)
00185 {
00186     ASSERT(b->priv.type == KBT_SD);
00187     return (Sd *)b;
00188 }
00189
00190 #endif /* DRV_SD_H */
00191