sdcard.h
  1  #ifndef __SDCARD_H__
  2  #define __SDCARD_H__
  3  
  4  #include <stdint.h>
  5  
  6  #ifdef __cplusplus
  7  extern "C"
  8  {
  9  #endif
 10  
 11  /**
 12   * @brief  Card Specific Data: CSD Register
 13   * 
 14   */
 15  
 16  #define SD_CS_PIN 29
 17  #define SD_SPI_DEVICE SPI_DEVICE_1
 18  #define SD_DMA_CH DMAC_CHANNEL0
 19  #define SD_SS SPI_CHIP_SELECT_1
 20  
 21      typedef struct
 22      {
 23          int mosi_pin;
 24          int miso_pin;
 25          int sclk_pin;
 26          int cs_pin;
 27          int cs_gpio_num;
 28  
 29      } sdcard_config_t;
 30  
 31      typedef struct
 32      {
 33          uint8_t CSDStruct;           /*!< CSD structure */
 34          uint8_t SysSpecVersion;      /*!< System specification version */
 35          uint8_t Reserved1;           /*!< Reserved */
 36          uint8_t TAAC;                /*!< Data read access-time 1 */
 37          uint8_t NSAC;                /*!< Data read access-time 2 in CLK cycles */
 38          uint8_t MaxBusClkFrec;       /*!< Max. bus clock frequency */
 39          uint16_t CardComdClasses;    /*!< Card command classes */
 40          uint8_t RdBlockLen;          /*!< Max. read data block length */
 41          uint8_t PartBlockRead;       /*!< Partial blocks for read allowed */
 42          uint8_t WrBlockMisalign;     /*!< Write block misalignment */
 43          uint8_t RdBlockMisalign;     /*!< Read block misalignment */
 44          uint8_t DSRImpl;             /*!< DSR implemented */
 45          uint8_t Reserved2;           /*!< Reserved */
 46          uint32_t DeviceSize;         /*!< Device Size */
 47          uint8_t MaxRdCurrentVDDMin;  /*!< Max. read current @ VDD min */
 48          uint8_t MaxRdCurrentVDDMax;  /*!< Max. read current @ VDD max */
 49          uint8_t MaxWrCurrentVDDMin;  /*!< Max. write current @ VDD min */
 50          uint8_t MaxWrCurrentVDDMax;  /*!< Max. write current @ VDD max */
 51          uint8_t DeviceSizeMul;       /*!< Device size multiplier */
 52          uint8_t EraseGrSize;         /*!< Erase group size */
 53          uint8_t EraseGrMul;          /*!< Erase group size multiplier */
 54          uint8_t WrProtectGrSize;     /*!< Write protect group size */
 55          uint8_t WrProtectGrEnable;   /*!< Write protect group enable */
 56          uint8_t ManDeflECC;          /*!< Manufacturer default ECC */
 57          uint8_t WrSpeedFact;         /*!< Write speed factor */
 58          uint8_t MaxWrBlockLen;       /*!< Max. write data block length */
 59          uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */
 60          uint8_t Reserved3;           /*!< Reserded */
 61          uint8_t ContentProtectAppli; /*!< Content protection application */
 62          uint8_t FileFormatGrouop;    /*!< File format group */
 63          uint8_t CopyFlag;            /*!< Copy flag (OTP) */
 64          uint8_t PermWrProtect;       /*!< Permanent write protection */
 65          uint8_t TempWrProtect;       /*!< Temporary write protection */
 66          uint8_t FileFormat;          /*!< File Format */
 67          uint8_t ECC;                 /*!< ECC code */
 68          uint8_t CSD_CRC;             /*!< CSD CRC */
 69          uint8_t Reserved4;           /*!< always 1*/
 70          uint8_t CSizeMlut;           /*!<  */
 71      } SD_CSD;
 72  
 73      /** 
 74    * @brief  Card Identification Data: CID Register   
 75    */
 76      typedef struct
 77      {
 78          uint8_t ManufacturerID; /*!< ManufacturerID */
 79          uint16_t OEM_AppliID;   /*!< OEM/Application ID */
 80          uint32_t ProdName1;     /*!< Product Name part1 */
 81          uint8_t ProdName2;      /*!< Product Name part2*/
 82          uint8_t ProdRev;        /*!< Product Revision */
 83          uint32_t ProdSN;        /*!< Product Serial Number */
 84          uint8_t Reserved1;      /*!< Reserved1 */
 85          uint16_t ManufactDate;  /*!< Manufacturing Date */
 86          uint8_t CID_CRC;        /*!< CID CRC */
 87          uint8_t Reserved2;      /*!< always 1 */
 88      } SD_CID;
 89  
 90      /** 
 91    * @brief SD Card information 
 92    */
 93      typedef struct
 94      {
 95          SD_CSD SD_csd;
 96          SD_CID SD_cid;
 97          uint64_t CardCapacity;  /*!< Card Capacity */
 98          uint32_t CardBlockSize; /*!< Card Block Size */
 99          uint8_t active;
100      } SD_CardInfo;
101  
102  
103      typedef void (*sd_preinit_handler_t)(sdcard_config_t *config);
104  
105      extern SD_CardInfo cardinfo;
106  
107      uint8_t sd_init(void);
108      uint8_t sd_read_sector(uint8_t *data_buff, uint32_t sector, uint32_t count);
109      uint8_t sd_write_sector(uint8_t *data_buff, uint32_t sector, uint32_t count);
110      uint8_t sd_read_sector_dma(uint8_t *data_buff, uint32_t sector, uint32_t count);
111      uint8_t sd_write_sector_dma(uint8_t *data_buff, uint32_t sector, uint32_t count);
112  
113  #ifdef __cplusplus
114  }
115  #endif
116  
117  #endif