sdcard.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. #include "platform.h"
  2. #include "driver/spi.h"
  3. #include "c_types.h"
  4. #include "sdcard.h"
  5. #define CHECK_SSPIN(pin) \
  6. if (pin < 1 || pin > NUM_GPIO) return FALSE; \
  7. m_ss_pin = pin;
  8. //==============================================================================
  9. // SD card commands
  10. /** GO_IDLE_STATE - init card in spi mode if CS low */
  11. uint8_t const CMD0 = 0X00;
  12. /** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
  13. uint8_t const CMD8 = 0X08;
  14. /** SEND_CSD - read the Card Specific Data (CSD register) */
  15. uint8_t const CMD9 = 0X09;
  16. /** SEND_CID - read the card identification information (CID register) */
  17. uint8_t const CMD10 = 0X0A;
  18. /** STOP_TRANSMISSION - end multiple block read sequence */
  19. uint8_t const CMD12 = 0X0C;
  20. /** SEND_STATUS - read the card status register */
  21. uint8_t const CMD13 = 0X0D;
  22. /** READ_SINGLE_BLOCK - read a single data block from the card */
  23. uint8_t const CMD17 = 0X11;
  24. /** READ_MULTIPLE_BLOCK - read a multiple data blocks from the card */
  25. uint8_t const CMD18 = 0X12;
  26. /** WRITE_BLOCK - write a single data block to the card */
  27. uint8_t const CMD24 = 0X18;
  28. /** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
  29. uint8_t const CMD25 = 0X19;
  30. /** ERASE_WR_BLK_START - sets the address of the first block to be erased */
  31. uint8_t const CMD32 = 0X20;
  32. /** ERASE_WR_BLK_END - sets the address of the last block of the continuous
  33. range to be erased*/
  34. uint8_t const CMD33 = 0X21;
  35. /** ERASE - erase all previously selected blocks */
  36. uint8_t const CMD38 = 0X26;
  37. /** APP_CMD - escape for application specific command */
  38. uint8_t const CMD55 = 0X37;
  39. /** READ_OCR - read the OCR register of a card */
  40. uint8_t const CMD58 = 0X3A;
  41. /** CRC_ON_OFF - enable or disable CRC checking */
  42. uint8_t const CMD59 = 0X3B;
  43. /** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
  44. pre-erased before writing */
  45. uint8_t const ACMD23 = 0X17;
  46. /** SD_SEND_OP_COMD - Sends host capacity support information and
  47. activates the card's initialization process */
  48. uint8_t const ACMD41 = 0X29;
  49. //==============================================================================
  50. /** status for card in the ready state */
  51. uint8_t const R1_READY_STATE = 0X00;
  52. /** status for card in the idle state */
  53. uint8_t const R1_IDLE_STATE = 0X01;
  54. /** status bit for illegal command */
  55. uint8_t const R1_ILLEGAL_COMMAND = 0X04;
  56. /** start data token for read or write single block*/
  57. uint8_t const DATA_START_BLOCK = 0XFE;
  58. /** stop token for write multiple blocks*/
  59. uint8_t const STOP_TRAN_TOKEN = 0XFD;
  60. /** start data token for write multiple blocks*/
  61. uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
  62. /** mask for data response tokens after a write block operation */
  63. uint8_t const DATA_RES_MASK = 0X1F;
  64. /** write data accepted token */
  65. uint8_t const DATA_RES_ACCEPTED = 0X05;
  66. //------------------------------------------------------------------------------
  67. // SD card errors
  68. /** timeout error for command CMD0 (initialize card in SPI mode) */
  69. uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
  70. /** CMD8 was not accepted - not a valid SD card*/
  71. uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
  72. /** card returned an error response for CMD12 (stop multiblock read) */
  73. uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
  74. /** card returned an error response for CMD17 (read block) */
  75. uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
  76. /** card returned an error response for CMD18 (read multiple block) */
  77. uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
  78. /** card returned an error response for CMD24 (write block) */
  79. uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
  80. /** WRITE_MULTIPLE_BLOCKS command failed */
  81. uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
  82. /** card returned an error response for CMD58 (read OCR) */
  83. uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
  84. /** SET_WR_BLK_ERASE_COUNT failed */
  85. uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
  86. /** ACMD41 initialization process timeout */
  87. uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
  88. /** card returned a bad CSR version field */
  89. uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
  90. /** erase block group command failed */
  91. uint8_t const SD_CARD_ERROR_ERASE = 0XC;
  92. /** card not capable of single block erase */
  93. uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD;
  94. /** Erase sequence timed out */
  95. uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
  96. /** card returned an error token instead of read data */
  97. uint8_t const SD_CARD_ERROR_READ = 0XF;
  98. /** read CID or CSD failed */
  99. uint8_t const SD_CARD_ERROR_READ_REG = 0X10;
  100. /** timeout while waiting for start of read data */
  101. uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11;
  102. /** card did not accept STOP_TRAN_TOKEN */
  103. uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12;
  104. /** card returned an error token as a response to a write operation */
  105. uint8_t const SD_CARD_ERROR_WRITE = 0X13;
  106. /** attempt to write protected block zero */
  107. uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14; // REMOVE - not used
  108. /** card did not go ready for a multiple block write */
  109. uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15;
  110. /** card returned an error to a CMD13 status check after a write */
  111. uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16;
  112. /** timeout occurred during write programming */
  113. uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17;
  114. /** incorrect rate selected */
  115. uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18;
  116. /** init() not called */
  117. uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19;
  118. /** card returned an error for CMD59 (CRC_ON_OFF) */
  119. uint8_t const SD_CARD_ERROR_CMD59 = 0X1A;
  120. /** invalid read CRC */
  121. uint8_t const SD_CARD_ERROR_READ_CRC = 0X1B;
  122. /** SPI DMA error */
  123. uint8_t const SD_CARD_ERROR_SPI_DMA = 0X1C;
  124. //------------------------------------------------------------------------------
  125. // card types
  126. uint8_t const SD_CARD_TYPE_INVALID = 0;
  127. /** Standard capacity V1 SD card */
  128. uint8_t const SD_CARD_TYPE_SD1 = 1;
  129. /** Standard capacity V2 SD card */
  130. uint8_t const SD_CARD_TYPE_SD2 = 2;
  131. /** High Capacity SD card */
  132. uint8_t const SD_CARD_TYPE_SDHC = 3;
  133. typedef struct {
  134. uint32_t start, target;
  135. } to_t;
  136. static uint8_t m_spi_no, m_ss_pin, m_status, m_type, m_error;
  137. static void sdcard_chipselect_low( void ) {
  138. platform_gpio_write( m_ss_pin, PLATFORM_GPIO_LOW );
  139. }
  140. static void sdcard_chipselect_high( void ) {
  141. platform_gpio_write( m_ss_pin, PLATFORM_GPIO_HIGH );
  142. // send some cc to ensure that MISO returns to high
  143. platform_spi_send_recv( m_spi_no, 8, 0xff );
  144. }
  145. static void set_timeout( to_t *to, uint32_t us )
  146. {
  147. uint32_t offset;
  148. to->start = system_get_time();
  149. offset = 0xffffffff - to->start;
  150. if (offset > us) {
  151. to->target = us - offset;
  152. } else {
  153. to->target = to->start + us;
  154. }
  155. }
  156. static uint8_t timed_out( to_t *to )
  157. {
  158. uint32_t now = system_get_time();
  159. if (to->start < to->target) {
  160. if ((now >= to->start) && (now <= to->target)) {
  161. return FALSE;
  162. } else {
  163. return TRUE;
  164. }
  165. } else {
  166. if ((now >= to->start) || (now <= to->target)) {
  167. return FALSE;
  168. } else {
  169. return TRUE;
  170. }
  171. }
  172. }
  173. static int sdcard_wait_not_busy( uint32_t us )
  174. {
  175. to_t to;
  176. set_timeout( &to, us );
  177. while (platform_spi_send_recv( m_spi_no, 8, 0xff ) != 0xff) {
  178. if (timed_out( &to )) {
  179. goto fail;
  180. }
  181. }
  182. return TRUE;
  183. fail:
  184. return FALSE;
  185. }
  186. static uint8_t sdcard_command( uint8_t cmd, uint32_t arg )
  187. {
  188. sdcard_chipselect_low();
  189. // wait until card is busy
  190. sdcard_wait_not_busy( 100 * 1000 );
  191. // send command
  192. // with precalculated CRC - correct for CMD0 with arg zero or CMD8 with arg 0x1AA
  193. const uint8_t crc = cmd == CMD0 ? 0x95 : 0x87;
  194. platform_spi_transaction( m_spi_no, 16, (cmd | 0x40) << 8 | arg >> 24, 32, arg << 8 | crc, 0, 0, 0 );
  195. // skip dangling byte of data transfer
  196. if (cmd == CMD12) {
  197. platform_spi_transaction( m_spi_no, 8, 0xff, 0, 0, 0, 0, 0 );
  198. }
  199. // wait for response
  200. for (uint8_t i = 0; ((m_status = platform_spi_send_recv( m_spi_no, 8, 0xff )) & 0x80) && i != 0xFF; i++) ;
  201. return m_status;
  202. }
  203. static uint8_t sdcard_acmd( uint8_t cmd, uint32_t arg ) {
  204. sdcard_command( CMD55, 0 );
  205. return sdcard_command( cmd, arg );
  206. }
  207. static int sdcard_write_data( uint8_t token, const uint8_t *src)
  208. {
  209. uint16_t crc = 0xffff;
  210. platform_spi_transaction( m_spi_no, 8, token, 0, 0, 0, 0, 0 );
  211. platform_spi_blkwrite( m_spi_no, 512, src );
  212. platform_spi_transaction( m_spi_no, 16, crc, 0, 0, 0, 0, 0 );
  213. m_status = platform_spi_send_recv( m_spi_no, 8, 0xff );
  214. if ((m_status & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
  215. m_error = SD_CARD_ERROR_WRITE;
  216. goto fail;
  217. }
  218. return TRUE;
  219. fail:
  220. sdcard_chipselect_high();
  221. return FALSE;
  222. }
  223. static int sdcard_read_data( uint8_t *dst, size_t count )
  224. {
  225. to_t to;
  226. // wait for start block token
  227. set_timeout( &to, 100 * 1000 );
  228. while ((m_status = platform_spi_send_recv( m_spi_no, 8, 0xff)) == 0xff) {
  229. if (timed_out( &to )) {
  230. goto fail;
  231. }
  232. }
  233. if (m_status != DATA_START_BLOCK) {
  234. m_error = SD_CARD_ERROR_READ;
  235. goto fail;
  236. }
  237. // transfer data
  238. platform_spi_blkread( m_spi_no, count, (void *)dst );
  239. // discard crc
  240. platform_spi_transaction( m_spi_no, 16, 0xffff, 0, 0, 0, 0, 0 );
  241. sdcard_chipselect_high();
  242. return TRUE;
  243. fail:
  244. sdcard_chipselect_high();
  245. return FALSE;
  246. }
  247. static int sdcard_read_register( uint8_t cmd, uint8_t *buf )
  248. {
  249. if (sdcard_command( cmd, 0 )) {
  250. m_error = SD_CARD_ERROR_READ_REG;
  251. goto fail;
  252. }
  253. return sdcard_read_data( buf, 16 );
  254. fail:
  255. sdcard_chipselect_high();
  256. return FALSE;
  257. }
  258. int platform_sdcard_init( uint8_t spi_no, uint8_t ss_pin )
  259. {
  260. uint32_t arg, user_spi_clkdiv;
  261. to_t to;
  262. m_type = SD_CARD_TYPE_INVALID;
  263. m_error = 0;
  264. if (spi_no > 1) {
  265. return FALSE;
  266. }
  267. m_spi_no = spi_no;
  268. CHECK_SSPIN(ss_pin);
  269. platform_gpio_write( m_ss_pin, PLATFORM_GPIO_HIGH );
  270. platform_gpio_mode( m_ss_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT );
  271. // set SPI clock to 400 kHz for init phase
  272. user_spi_clkdiv = spi_set_clkdiv( m_spi_no, 200 );
  273. // apply initialization sequence:
  274. // keep ss and io high, apply clock for max(1ms; 74cc)
  275. // 1ms requires 400cc @ 400kHz
  276. for (int i = 0; i < 2; i++) {
  277. platform_spi_transaction( m_spi_no, 0, 0, 0, 0, 0, 200, 0 );
  278. }
  279. // command to go idle in SPI mode
  280. set_timeout( &to, 500 * 1000 );
  281. while (sdcard_command( CMD0, 0 ) != R1_IDLE_STATE) {
  282. if (timed_out( &to )) {
  283. goto fail;
  284. }
  285. }
  286. set_timeout( &to, 500 * 1000 );
  287. while (1) {
  288. if (sdcard_command( CMD8, 0x1aa) == (R1_ILLEGAL_COMMAND | R1_IDLE_STATE)) {
  289. m_type = SD_CARD_TYPE_SD1;
  290. break;
  291. }
  292. for (uint8_t i = 0; i < 4; i++) {
  293. m_status = platform_spi_send_recv( m_spi_no, 8, 0xff );
  294. }
  295. if (m_status == 0xaa) {
  296. m_type = SD_CARD_TYPE_SD2;
  297. break;
  298. }
  299. if (timed_out( &to )) {
  300. goto fail;
  301. }
  302. }
  303. // initialize card and send host supports SDHC if SD2
  304. arg = m_type == SD_CARD_TYPE_SD2 ? 0x40000000 : 0;
  305. set_timeout( &to, 500 * 1000 );
  306. while (sdcard_acmd( ACMD41, arg ) != R1_READY_STATE) {
  307. if (timed_out( &to )) {
  308. goto fail;
  309. }
  310. }
  311. // if SD2 read OCR register to check for SDHC card
  312. if (m_type == SD_CARD_TYPE_SD2) {
  313. if (sdcard_command( CMD58, 0 )) {
  314. m_error = SD_CARD_ERROR_CMD58;
  315. goto fail;
  316. }
  317. if ((platform_spi_send_recv( m_spi_no, 8, 0xff ) & 0xC0) == 0xC0) {
  318. m_type = SD_CARD_TYPE_SDHC;
  319. }
  320. // Discard rest of ocr - contains allowed voltage range.
  321. for (uint8_t i = 0; i < 3; i++) {
  322. platform_spi_send_recv( m_spi_no, 8, 0xff);
  323. }
  324. }
  325. sdcard_chipselect_high();
  326. // re-apply user's spi clock divider
  327. spi_set_clkdiv( m_spi_no, user_spi_clkdiv );
  328. return TRUE;
  329. fail:
  330. sdcard_chipselect_high();
  331. return FALSE;
  332. }
  333. int platform_sdcard_status( void )
  334. {
  335. return m_status;
  336. }
  337. int platform_sdcard_error( void )
  338. {
  339. return m_error;
  340. }
  341. int platform_sdcard_type( void )
  342. {
  343. return m_type;
  344. }
  345. int platform_sdcard_read_block( uint8_t ss_pin, uint32_t block, uint8_t *dst )
  346. {
  347. CHECK_SSPIN(ss_pin);
  348. // generate byte address for pre-SDHC types
  349. if (m_type != SD_CARD_TYPE_SDHC) {
  350. block <<= 9;
  351. }
  352. if (sdcard_command( CMD17, block )) {
  353. m_error = SD_CARD_ERROR_CMD17;
  354. goto fail;
  355. }
  356. return sdcard_read_data( dst, 512 );
  357. fail:
  358. sdcard_chipselect_high();
  359. return FALSE;
  360. }
  361. int platform_sdcard_read_blocks( uint8_t ss_pin, uint32_t block, size_t num, uint8_t *dst )
  362. {
  363. CHECK_SSPIN(ss_pin);
  364. if (num == 0) {
  365. return TRUE;
  366. }
  367. if (num == 1) {
  368. return platform_sdcard_read_block( ss_pin, block, dst );
  369. }
  370. // generate byte address for pre-SDHC types
  371. if (m_type != SD_CARD_TYPE_SDHC) {
  372. block <<= 9;
  373. }
  374. // command READ_MULTIPLE_BLOCKS
  375. if (sdcard_command( CMD18, block )) {
  376. m_error = SD_CARD_ERROR_CMD18;
  377. goto fail;
  378. }
  379. // read required blocks
  380. while (num > 0) {
  381. sdcard_chipselect_low();
  382. if (sdcard_read_data( dst, 512 )) {
  383. num--;
  384. dst = &(dst[512]);
  385. } else {
  386. break;
  387. }
  388. }
  389. // issue command STOP_TRANSMISSION
  390. if (sdcard_command( CMD12, 0 )) {
  391. m_error = SD_CARD_ERROR_CMD12;
  392. goto fail;
  393. }
  394. sdcard_chipselect_high();
  395. return TRUE;
  396. fail:
  397. sdcard_chipselect_high();
  398. return FALSE;
  399. }
  400. int platform_sdcard_read_csd( uint8_t ss_pin, uint8_t *csd )
  401. {
  402. CHECK_SSPIN(ss_pin);
  403. return sdcard_read_register( CMD9, csd );
  404. }
  405. int platform_sdcard_read_cid( uint8_t ss_pin, uint8_t *cid )
  406. {
  407. CHECK_SSPIN(ss_pin);
  408. return sdcard_read_register( CMD10, cid );
  409. }
  410. int platform_sdcard_write_block( uint8_t ss_pin, uint32_t block, const uint8_t *src )
  411. {
  412. CHECK_SSPIN(ss_pin);
  413. // generate byte address for pre-SDHC types
  414. if (m_type != SD_CARD_TYPE_SDHC) {
  415. block <<= 9;
  416. }
  417. if (sdcard_command( CMD24, block )) {
  418. m_error = SD_CARD_ERROR_CMD24;
  419. goto fail;
  420. }
  421. if (! sdcard_write_data( DATA_START_BLOCK, src )) {
  422. goto fail;
  423. }
  424. sdcard_chipselect_high();
  425. return TRUE;
  426. fail:
  427. sdcard_chipselect_high();
  428. return FALSE;
  429. }
  430. static int sdcard_write_stop( void )
  431. {
  432. sdcard_chipselect_low();
  433. if (! sdcard_wait_not_busy( 100 * 1000 )) {
  434. goto fail;
  435. }
  436. platform_spi_transaction( m_spi_no, 8, STOP_TRAN_TOKEN, 0, 0, 0, 0, 0 );
  437. if (! sdcard_wait_not_busy( 100 * 1000 )) {
  438. goto fail;
  439. }
  440. sdcard_chipselect_high();
  441. return TRUE;
  442. fail:
  443. m_error = SD_CARD_ERROR_STOP_TRAN;
  444. sdcard_chipselect_high();
  445. return FALSE;
  446. }
  447. int platform_sdcard_write_blocks( uint8_t ss_pin, uint32_t block, size_t num, const uint8_t *src )
  448. {
  449. CHECK_SSPIN(ss_pin);
  450. if (sdcard_acmd( ACMD23, num )) {
  451. m_error = SD_CARD_ERROR_ACMD23;
  452. goto fail;
  453. }
  454. // generate byte address for pre-SDHC types
  455. if (m_type != SD_CARD_TYPE_SDHC) {
  456. block <<= 9;
  457. }
  458. if (sdcard_command( CMD25, block )) {
  459. m_error = SD_CARD_ERROR_CMD25;
  460. goto fail;
  461. }
  462. sdcard_chipselect_high();
  463. for (size_t b = 0; b < num; b++, src += 512) {
  464. sdcard_chipselect_low();
  465. // wait for previous write to finish
  466. if (! sdcard_wait_not_busy( 100 * 1000 )) {
  467. goto fail_write;
  468. }
  469. if (! sdcard_write_data( WRITE_MULTIPLE_TOKEN, src )) {
  470. goto fail_write;
  471. }
  472. sdcard_chipselect_high();
  473. }
  474. return sdcard_write_stop();
  475. fail_write:
  476. m_error = SD_CARD_ERROR_WRITE_MULTIPLE;
  477. fail:
  478. sdcard_chipselect_high();
  479. return FALSE;
  480. }