SWM341_sdio.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef __SWM341_SDIO_H__
  2. #define __SWM341_SDIO_H__
  3. #define SD_CMD_GO_IDLE_STATE ((uint8_t)0)
  4. #define SD_CMD_SEND_OP_COND ((uint8_t)1)
  5. #define SD_CMD_ALL_SEND_CID ((uint8_t)2)
  6. #define SD_CMD_SET_REL_ADDR ((uint8_t)3)
  7. #define SD_CMD_SET_DSR ((uint8_t)4)
  8. #define SD_CMD_HS_SWITCH ((uint8_t)6)
  9. #define SD_CMD_SEL_DESEL_CARD ((uint8_t)7)
  10. #define SD_CMD_SEND_IF_COND ((uint8_t)8)
  11. #define SD_CMD_SEND_CSD ((uint8_t)9)
  12. #define SD_CMD_SEND_CID ((uint8_t)10)
  13. #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12)
  14. #define SD_CMD_SEND_STATUS ((uint8_t)13)
  15. #define SD_CMD_SET_BLOCKLEN ((uint8_t)16)
  16. #define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17)
  17. #define SD_CMD_READ_MULT_BLOCK ((uint8_t)18)
  18. #define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24)
  19. #define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25)
  20. #define SD_CMD_PROG_CID ((uint8_t)26)
  21. #define SD_CMD_PROG_CSD ((uint8_t)27)
  22. #define SD_CMD_APP_CMD ((uint8_t)55)
  23. /*Following commands are SD Card Specific commands.
  24. SDIO_APP_CMD should be sent before sending these commands. */
  25. #define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6)
  26. #define SD_CMD_SD_APP_STAUS ((uint8_t)13)
  27. #define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22)
  28. #define SD_CMD_SD_APP_OP_COND ((uint8_t)41)
  29. #define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42)
  30. #define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51)
  31. #define SD_CMD_SDIO_RW_DIRECT ((uint8_t)52)
  32. #define SD_CMD_SDIO_RW_EXTENDED ((uint8_t)53)
  33. #define SD_CMD53_ARG_Count 0 // 0x001 1 0x002 2 ... 0x1FF 512 0x000 512 byte
  34. #define SD_CMD53_ARG_Addr 9 // Start Address of I/O register to read or write
  35. #define SD_CMD53_ARG_AddrInc 26 // 0 Multi byte R/W to fixed address 1 Multi byte R/W to incrementing address
  36. #define SD_CMD53_ARG_CountUnit 27 // 0 Count in byte 1 Count in block
  37. #define SD_CMD53_ARG_Function 28 // The number of the function within the I/O card you wish to read or write. Function 0x00 selects the common I/O area (CIA).
  38. #define SD_CMD53_ARG_nRW 31 // 0 for read 1 for write
  39. #define SD_RESP_NO 0 //0 无响应
  40. #define SD_RESP_32b 2 //2 32位响应
  41. #define SD_RESP_128b 1 //1 128位响应
  42. #define SD_RESP_32b_busy 3 //3 32位响应,check Busy after response
  43. #define SD_BUSWIDTH_1b 0
  44. #define SD_BUSWIDTH_4b 2
  45. #define SD_RES_OK 0
  46. #define SD_RES_ERR 1
  47. #define SD_RES_TIMEOUT 2
  48. /* Card Status return by response R1 */
  49. #define SD_CS_APP_CMD (1 << 5) // The card will expect ACMD
  50. #define SD_CS_READY_FOR_DATA (1 << 8) // Corresponds to buffer empty signaling on the bus
  51. #define SD_CS_CURRENT_STATE (1 << 9) // The state of the card when receiving the command. 共 4 位
  52. #define SD_CS_CARD_ECC_FAILED (1 << 21)
  53. #define SD_CS_ILLEGAL_COMMAND (1 << 22)
  54. #define SD_CS_CARD_IS_LOCKED (1 << 25)
  55. typedef struct
  56. {
  57. __IO uint8_t CSDStruct; // CSD structure
  58. __IO uint8_t SysSpecVersion; // System specification version
  59. __IO uint8_t Reserved1; // Reserved
  60. __IO uint8_t TAAC; // Data read access-time 1
  61. __IO uint8_t NSAC; // Data read access-time 2 in CLK cycles
  62. __IO uint8_t MaxBusClkFrec; // Max. bus clock frequency
  63. __IO uint16_t CardComdClasses; //< Card command classes
  64. __IO uint8_t RdBlockLen; // Max. read data block length
  65. __IO uint8_t PartBlockRead; // Partial blocks for read allowed
  66. __IO uint8_t WrBlockMisalign; // Write block misalignment
  67. __IO uint8_t RdBlockMisalign; // Read block misalignment
  68. __IO uint8_t DSRImpl; // DSR implemented
  69. __IO uint8_t Reserved2; // Reserved
  70. __IO uint32_t DeviceSize; // Device Size
  71. __IO uint8_t MaxRdCurrentVDDMin; // Max. read current @ VDD min
  72. __IO uint8_t MaxRdCurrentVDDMax; // Max. read current @ VDD max
  73. __IO uint8_t MaxWrCurrentVDDMin; // Max. write current @ VDD min
  74. __IO uint8_t MaxWrCurrentVDDMax; // Max. write current @ VDD max
  75. __IO uint8_t DeviceSizeMul; // Device size multiplier
  76. __IO uint8_t EraseGrSize; // Erase group size
  77. __IO uint8_t EraseGrMul; // Erase group size multiplier
  78. __IO uint8_t WrProtectGrSize; // Write protect group size
  79. __IO uint8_t WrProtectGrEnable; // Write protect group enable
  80. __IO uint8_t ManDeflECC; // Manufacturer default ECC
  81. __IO uint8_t WrSpeedFact; // Write speed factor
  82. __IO uint8_t MaxWrBlockLen; // Max. write data block length
  83. __IO uint8_t WriteBlockPaPartial; // Partial blocks for write allowed
  84. __IO uint8_t Reserved3; // Reserded
  85. __IO uint8_t ContentProtectAppli; // Content protection application
  86. __IO uint8_t FileFormatGrouop; // File format group
  87. __IO uint8_t CopyFlag; // Copy flag (OTP)
  88. __IO uint8_t PermWrProtect; // Permanent write protection
  89. __IO uint8_t TempWrProtect; // Temporary write protection
  90. __IO uint8_t FileFormat; // File Format
  91. __IO uint8_t ECC; // ECC code
  92. } SD_CSD;
  93. typedef struct
  94. {
  95. __IO uint8_t ManufacturerID; // ManufacturerID
  96. __IO uint16_t OEM_AppliID; // OEM/Application ID
  97. __IO uint32_t ProdName1; // Product Name part1
  98. __IO uint8_t ProdName2; // Product Name part2
  99. __IO uint8_t ProdRev; // Product Revision
  100. __IO uint32_t ProdSN; // Product Serial Number
  101. __IO uint8_t Reserved1; // Reserved1
  102. __IO uint16_t ManufactDate; // Manufacturing Date
  103. } SD_CID;
  104. #define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000)
  105. #define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001)
  106. #define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002)
  107. #define SDIO_MULTIMEDIA_CARD ((uint32_t)0x00000003)
  108. #define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004)
  109. #define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005)
  110. #define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006)
  111. #define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007)
  112. typedef struct
  113. {
  114. SD_CSD SD_csd;
  115. SD_CID SD_cid;
  116. uint64_t CardCapacity; // Card Capacity
  117. uint32_t CardBlockSize; // Card Block Size
  118. uint16_t RCA;
  119. uint8_t CardType;
  120. } SD_CardInfo;
  121. extern SD_CardInfo SD_cardInfo;
  122. uint32_t SDIO_Init(uint32_t freq);
  123. uint32_t SDIO_BlockWrite(uint32_t block_addr, uint32_t buff[]);
  124. uint32_t SDIO_BlockRead(uint32_t block_addr, uint32_t buff[]);
  125. uint32_t SDIO_MultiBlockWrite(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);
  126. uint32_t SDIO_MultiBlockRead(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);
  127. uint32_t SDIO_DMABlockWrite(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);
  128. uint32_t SDIO_DMABlockRead(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);
  129. uint32_t _SDIO_SendCmd(uint32_t cmd, uint32_t arg, uint32_t resp_type, uint32_t *resp_data, uint32_t have_data, uint32_t data_read, uint16_t block_cnt, uint32_t use_dma);
  130. #define SDIO_SendCmd(cmd, arg, resp_type, resp_data) _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 0, 0, 0, 0)
  131. #define SDIO_SendCmdWithData(cmd, arg, resp_type, resp_data, data_read, block_cnt) _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 1, data_read, block_cnt, 0)
  132. #define SDIO_SendCmdWithDataByDMA(cmd, arg, resp_type, resp_data, data_read, block_cnt) _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 1, data_read, block_cnt, 1)
  133. void parseCID(uint32_t CID_Tab[4]);
  134. void parseCSD(uint32_t CID_Tab[4]);
  135. uint32_t calcSDCLKDiv(uint32_t freq_sel);
  136. enum SDIO_bus_width { SDIO_1bit = 0, SDIO_4bit = 1 };
  137. uint32_t SDIO_IO_Init(uint32_t freq, enum SDIO_bus_width w);
  138. uint32_t SDIO_IO_ByteWrite(uint8_t func, uint32_t addr, uint8_t data);
  139. uint32_t SDIO_IO_ByteRead(uint8_t func, uint32_t addr, uint8_t * data);
  140. uint32_t SDIO_IO_BlockWrite(uint8_t func, uint32_t addr, uint8_t addrInc, uint32_t buff[], uint16_t block_size);
  141. uint32_t SDIO_IO_BlockRead(uint8_t func, uint32_t addr, uint8_t addrInc, uint32_t buff[], uint16_t block_size);
  142. uint32_t SDIO_IO_MultiBlockWrite(uint8_t func, uint32_t addr, uint8_t addrInc, uint32_t buff[], uint16_t block_count);
  143. uint32_t SDIO_IO_MultiBlockRead(uint8_t func, uint32_t addr, uint8_t addrInc, uint32_t buff[], uint16_t block_count);
  144. #endif //__SWM341_SDIO_H__