X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fdrv%2Fsd.h;h=19628691e82ab9e57c7aa7efa72571153c448836;hb=7f6d545066c243a9f38c77dff806d7f91d1893af;hp=a0ca1d42dfd8a79493879d605a7a6409bd0e771c;hpb=3715319745d93d6f552b72a5321e8dc3b595a96d;p=bertos.git diff --git a/bertos/drv/sd.h b/bertos/drv/sd.h index a0ca1d42..19628691 100644 --- a/bertos/drv/sd.h +++ b/bertos/drv/sd.h @@ -31,13 +31,11 @@ * * \brief Function library for secure digital memory. * - * Right now, the interface for these function is the one defined in diskio.h from - * the FatFS module. * * \author Francesco Sacchi * * $WIZ$ module_name = "sd" - * $WIZ$ module_depends = "kfile", "timer" + * $WIZ$ module_depends = "kfile", "timer", "kblock" * $WIZ$ module_hw = "bertos/hw/hw_sd.h" * $WIZ$ module_configuration = "bertos/cfg/cfg_sd.h" */ @@ -46,27 +44,136 @@ #ifndef DRV_SD_H #define DRV_SD_H +#include "cfg/cfg_sd.h" + #include #include #include -#include "cfg/cfg_sd.h" +#if CPU_CM3_SAM3X8 + +typedef struct SDcid +{ + uint8_t manfid; + uint8_t prod_name[8]; + uint32_t serial; + uint16_t oemid; + uint32_t year_off; + uint8_t m_rev; + uint8_t l_rev; +}SDcid; + +typedef struct SDcsd +{ + uint8_t structure; + uint8_t ccc; ///< Card command classes + uint32_t erase_size; ///< The size of an erasable sector, in write block len + uint32_t capacity; ///< Card size in byte + uint32_t max_data_rate; ///< Step rate, usec + uint32_t blk_len; ///< Block data size len in byte + uint32_t blk_num; ///< Number of block in card + uint32_t write_blk_bits; ///< Max write block length in bits + uint32_t read_blk_bits; ///< Max read block length in bits + uint8_t read_partial:1, + read_misalign:1, + write_partial:1, + write_misalign:1; +} SDcsd; + +typedef struct SDssr +{ + uint8_t bus_width; + uint8_t card_type; + uint8_t speed_class; + uint8_t au_size; + uint8_t erase_size; +} SDssr; + + +#define SD_READY_FOR_DATA BV(8) +#define SD_CARD_IS_LOCKED BV(25) + +#define SD_SEND_CID_RCA 0 +#define SD_SEND_ALL_CID BV(0) +#endif + + +#define SD_UNBUFFERED BV(0) ///< Open SD memory disabling page caching, no modification and partial write are allowed. +/** + * SD Card context structure. + */ typedef struct Sd { - KBlock b; + + KBlock b; ///< KBlock base class KFile *ch; ///< SPI communication channel - uint16_t r1; + uint16_t r1; ///< Last status data received from SD + uint16_t tranfer_len; ///< Lenght for the read/write commands, cached in order to increase speed. + + #if CPU_CM3_SAM3X8 + SDcid cid; + SDcsd csd; + SDssr ssr; + uint32_t addr; + uint32_t status; + #endif + } Sd; bool sd_initUnbuf(Sd *sd, KFile *ch); bool sd_initBuf(Sd *sd, KFile *ch); + +#if CPU_CM3_SAM3X8 + +void sd_dumpCsd(Sd *sd); +void sd_dumpCid(Sd *sd); +void sd_dumpSsr(Sd *sd); + +void sd_sendInit(void); +void sd_goIdle(void); +int sd_sendIfCond(Sd *sd); +int sd_sendAppOpCond(Sd *sd); + +int sd_getCid(Sd *sd, uint32_t addr, uint8_t flag); +int sd_getCsd(Sd *sd); +int sd_getSrc(Sd *sd); + +int sd_appStatus(Sd *sd); +int sd_getRelativeAddr(Sd *sd); + +int sd_getStatus(Sd *sd, uint32_t *buf, size_t words); + +int sd_selectCard(Sd *sd); +int sd_deSelectCard(Sd *sd); +int sd_setBusWidth(Sd *sd, size_t len); +int sd_set_BlockLen(Sd *sd, size_t len); +void sd_setHightSpeed(Sd *sd); +int sd_readSingleBlock(Sd *sd, size_t index, uint32_t *buf, size_t words); +int sd_writeSingleBlock(Sd *sd, size_t index, uint32_t *buf, size_t words); + + +INLINE int sd_setBus4bit(Sd *sd) +{ + return sd_setBusWidth(sd, 4); +} + +INLINE int sd_setBus1bit(Sd *sd) +{ + return sd_setBusWidth(sd, 1); +} + +#endif + + #if CONFIG_SD_OLD_INIT - #warning "Deprecated: this API will be removed in the next major release," - #warning "please disable CONFIG_SD_OLD_INIT and pass explicitly the SD context to sd_init()." + #if !(ARCH & ARCH_NIGHTTEST) + #warning "Deprecated: this API will be removed in the next major release," + #warning "please disable CONFIG_SD_OLD_INIT and pass explicitly the SD context to sd_init()." + #endif /** * Initializes the SD driver. @@ -77,6 +184,8 @@ bool sd_initBuf(Sd *sd, KFile *ch); * * \note This API is deprecated, disable CONFIG_SD_OLD_INIT and * use the new one instead. + * + * \see CONFIG_SD_OLD_INIT. */ #define sd_init(ch) {static struct Sd sd; sd_initUnbuf(&sd, (ch));} @@ -94,7 +203,7 @@ bool sd_initBuf(Sd *sd, KFile *ch); * * \return true if initialization succeds, false otherwise. */ - #define sd_init(sd, ch, buffered) ((buffered) ? sd_initBuf((sd), (ch)) : sd_initUnbuf((sd), (ch))) + #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_initUnbuf((sd), (ch)) : sd_initBuf((sd), (ch))) #endif @@ -110,5 +219,5 @@ INLINE Sd *SD_CAST(KBlock *b) return (Sd *)b; } - #endif /* DRV_SD_H */ +