sdcard.c 15 KB

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