drv_sdcard.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /***************************************************************************//**
  2. * @file drv_sdcard.c
  3. * @brief Memory card driver (SPI mode) of RT-Thread RTOS for using EFM32
  4. * USART module
  5. * COPYRIGHT (C) 2011, RT-Thread Development Team
  6. * @author onelife
  7. * @version 0.4 beta
  8. *******************************************************************************
  9. * @section License
  10. * The license and distribution terms for this file may be found in the file
  11. * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
  12. *******************************************************************************
  13. * @section Change Logs
  14. * Date Author Notes
  15. * 2011-05-13 onelife Initial creation for using EFM32 USART module
  16. * 2011-07-07 onelife Modify initialization function to return error code
  17. ******************************************************************************/
  18. /***************************************************************************//**
  19. * @addtogroup efm32_dk
  20. * @{
  21. ******************************************************************************/
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "board.h"
  24. #include "drv_usart.h"
  25. #include "drv_sdcard.h"
  26. #if defined(EFM32_USING_SPISD)
  27. #include <dfs_fs.h>
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* Private define ------------------------------------------------------------*/
  30. /* Private macro -------------------------------------------------------------*/
  31. #ifdef EFM32_SDCARD_DEBUG
  32. #define sdcard_debug(format,args...) rt_kprintf(format, ##args)
  33. #else
  34. #define sdcard_debug(format,args...)
  35. #endif
  36. /* Private constants ---------------------------------------------------------*/
  37. /* Private variables ---------------------------------------------------------*/
  38. static struct rt_device sd_device;
  39. static struct dfs_partition sdPart;
  40. static rt_device_t spi = RT_NULL;
  41. static rt_uint16_t sdType;
  42. static rt_bool_t sdAutoCs = true;
  43. static rt_timer_t sdTimer = RT_NULL;
  44. static rt_bool_t sdInTime = true;
  45. /* Private function prototypes -----------------------------------------------*/
  46. /* Private functions ---------------------------------------------------------*/
  47. /***************************************************************************//**
  48. * @brief
  49. * Memory device timeout interrupt handler
  50. *
  51. * @details
  52. *
  53. * @note
  54. *
  55. * @param[in] parameter
  56. * Parameter
  57. ******************************************************************************/
  58. static void efm_spiSd_timer(void* parameter)
  59. {
  60. sdInTime = false;
  61. }
  62. /***************************************************************************//**
  63. * @brief
  64. * Set/Clear chip select
  65. *
  66. * @details
  67. *
  68. * @note
  69. *
  70. * @param[in] enable
  71. * Chip select pin setting
  72. ******************************************************************************/
  73. static void efm_spiSd_cs(rt_uint8_t enable)
  74. {
  75. if (!sdAutoCs)
  76. {
  77. if (enable)
  78. {
  79. GPIO_PinOutClear(SD_CS_PORT, SD_CS_PIN);
  80. }
  81. else
  82. {
  83. GPIO_PinOutSet(SD_CS_PORT, SD_CS_PIN);
  84. }
  85. }
  86. }
  87. /***************************************************************************//**
  88. * @brief
  89. * Set operation speed level
  90. *
  91. * @details
  92. *
  93. * @note
  94. *
  95. * @param[in] level
  96. * Set SD speed level
  97. ******************************************************************************/
  98. static void efm_spiSd_speed(rt_uint8_t level)
  99. {
  100. RT_ASSERT(spi != RT_NULL);
  101. struct efm32_usart_device_t *usart;
  102. rt_uint32_t baudrate;
  103. usart = (struct efm32_usart_device_t *)(spi->user_data);
  104. if (level == SD_SPEED_HIGH)
  105. {
  106. baudrate = EFM32_SDCLK_HIGH;
  107. }
  108. else
  109. {
  110. baudrate = EFM32_SDCLK_LOW;
  111. }
  112. USART_BaudrateSyncSet(usart->usart_device, 0, baudrate);
  113. }
  114. /***************************************************************************//**
  115. * @brief
  116. * Read raw data from memory device
  117. *
  118. * @details
  119. *
  120. * @note
  121. *
  122. * @param[in] buffer
  123. * Poniter to the buffer
  124. *
  125. * @param[in] size
  126. * Buffer size in byte
  127. *
  128. * @return
  129. * Number of read bytes
  130. ******************************************************************************/
  131. static rt_size_t efm_spiSd_read(void *buffer, rt_size_t size)
  132. {
  133. RT_ASSERT(spi != RT_NULL);
  134. rt_uint8_t buf_read[5], ret;
  135. /* Build instruction buffer */
  136. buf_read[0] = 0x00;
  137. *(rt_uint8_t **)(&buf_read[1]) = buffer;
  138. /* Read data */
  139. efm_spiSd_cs(1);
  140. if ((ret = spi->read(spi, EFM32_NO_DATA, buf_read, size)) == 0)
  141. {
  142. sdcard_debug("SPISD: Read failed!\n");
  143. }
  144. efm_spiSd_cs(0);
  145. return ret;
  146. }
  147. /***************************************************************************//**
  148. * @brief
  149. * Send command to memory device
  150. *
  151. * @details
  152. *
  153. * @note
  154. *
  155. * @param[in] cmd
  156. * Command index
  157. *
  158. * @param[in] arg
  159. * Argument
  160. *
  161. * @param[in] trail
  162. * Pointer to the buffer to store trailing data
  163. *
  164. * @return
  165. * Command response
  166. ******************************************************************************/
  167. static rt_uint16_t efm_spiSd_cmd(
  168. rt_uint8_t cmd,
  169. rt_uint32_t arg,
  170. rt_uint8_t *trail)
  171. {
  172. RT_ASSERT(spi != RT_NULL);
  173. rt_uint8_t buf_ins[11];
  174. rt_uint8_t buf_res[32]; /* Expect (x+1+4) bytes for CRC, (x+1+19) for CSD/CID */
  175. rt_uint8_t len_trl, i, j;
  176. rt_uint16_t ret;
  177. rt_bool_t skip;
  178. ret = 0xffff;
  179. rt_memset(buf_res, 0xff, sizeof(buf_res));
  180. sdcard_debug("SPISD: Send command %d(%x)\n", cmd, arg);
  181. do
  182. {
  183. /* Build instruction buffer */
  184. buf_ins[0] = 6; /* Instruction length */
  185. buf_ins[1] = 0x40 | cmd; /* Command index */
  186. buf_ins[2] = (arg >> 24) & 0x000000ff; /* Argument: MSB first */
  187. buf_ins[3] = (arg >> 16) & 0x000000ff;
  188. buf_ins[4] = (arg >> 8) & 0x000000ff;
  189. buf_ins[5] = arg & 0x000000ff;
  190. if (cmd == CMD0)
  191. {
  192. buf_ins[6] = 0x95; /* Valid CRC for CMD0(0) */
  193. }
  194. else if (cmd == CMD8)
  195. {
  196. buf_ins[6] = 0x87; /* Valid CRC for CMD8(0x1AA) */
  197. }
  198. else if (cmd == CMD58)
  199. {
  200. buf_ins[6] = 0x01; /* Dummy CRC + Stop */
  201. }
  202. else
  203. {
  204. buf_ins[6] = 0x01; /* Dummy CRC + Stop */
  205. }
  206. *(rt_uint8_t **)(&buf_ins[7]) = buf_res; /* Pointer to RX buffer */
  207. /* Set trail length */
  208. if (cmd == CMD8)
  209. {
  210. len_trl = 4; /* R7 response */
  211. }
  212. else if (cmd == CMD9)
  213. {
  214. len_trl = SD_BLOCK_SIZE_CSD;
  215. }
  216. else if (cmd == CMD10)
  217. {
  218. len_trl = SD_BLOCK_SIZE_CID;
  219. }
  220. else if (cmd == CMD58)
  221. {
  222. len_trl = SD_BLOCK_SIZE_OCR; /* R3 response */
  223. }
  224. else
  225. {
  226. len_trl = 0;
  227. }
  228. /* Send command and get response */
  229. efm_spiSd_cs(1);
  230. if (spi->read(spi, EFM32_NO_DATA, buf_ins, sizeof(buf_res)) == 0)
  231. {
  232. sdcard_debug("SPISD: Send command failed!\n");
  233. break;
  234. }
  235. efm_spiSd_cs(0);
  236. /* Skip a stuff byte when stop reading */
  237. if (cmd == CMD12)
  238. {
  239. skip = true;
  240. }
  241. else
  242. {
  243. skip = false;
  244. }
  245. /* Find valid response: The response is sent back within command response time
  246. (NCR), 0 to 8 bytes for SDC, 1 to 8 bytes for MMC */
  247. for (i = 0; i < sizeof(buf_res); i++)
  248. {
  249. if (buf_res[i] != 0xff)
  250. {
  251. if (skip)
  252. {
  253. skip = false;
  254. sdcard_debug("SPISD: Skip %x (at %d)\n", buf_res[i], i);
  255. continue;
  256. }
  257. if (cmd == ACMD13 & 0x7f)
  258. {
  259. ret = (rt_uint16_t)buf_res[i]; /* R2 response */
  260. }
  261. else
  262. {
  263. ret = (rt_uint8_t)buf_res[i];
  264. }
  265. break;
  266. }
  267. }
  268. sdcard_debug("SPISD: Response %x (at %d)\n", ret, i);
  269. i++;
  270. /* Copy the trailing data */
  271. if ((ret != 0xffff) && len_trl && trail)
  272. {
  273. if (cmd == CMD9 || cmd == CMD10)
  274. {
  275. /* Wait for data block */
  276. for (; i < sizeof(buf_res); i++)
  277. {
  278. if (buf_res[i] == 0xfe)
  279. {
  280. break;
  281. }
  282. }
  283. /* Check if valid */
  284. if (i >= sizeof(buf_res))
  285. {
  286. sdcard_debug("SPISD: Token is not found!\n");
  287. ret = 0xffff;
  288. break;
  289. }
  290. i++;
  291. }
  292. /* Copy the data */
  293. for (j = 0; j < len_trl; j++)
  294. {
  295. trail[j] = buf_res[i + j];
  296. }
  297. }
  298. } while(0);
  299. return ret;
  300. }
  301. /***************************************************************************//**
  302. * @brief
  303. * Read a block of data from memory device. This function is used to handle
  304. * the responses of specified commands (e.g. ACMD13, CMD17 and CMD18)
  305. *
  306. * @details
  307. *
  308. * @note
  309. *
  310. * @param[in] buffer
  311. * Poniter to the buffer
  312. *
  313. * @param[in] size
  314. * Buffer size in byte
  315. *
  316. * @return
  317. * Error code
  318. ******************************************************************************/
  319. static rt_err_t efm_spiSd_readBlock(void *buffer, rt_size_t size)
  320. {
  321. RT_ASSERT(spi != RT_NULL);
  322. rt_uint8_t buf_ins[5];
  323. rt_uint8_t buf_res[8]; /* Expect 2 bytes for CRC */
  324. rt_uint8_t i, len_copy;
  325. rt_bool_t start;
  326. start = false;
  327. do
  328. {
  329. /* Build instruction buffer */
  330. buf_ins[0] = 0; /* Instruction length */
  331. *(rt_uint8_t **)(&buf_ins[1]) = buf_res; /* Pointer to RX buffer */
  332. while(1)
  333. {
  334. /* Send read command */
  335. efm_spiSd_cs(1);
  336. if (spi->read(spi, EFM32_NO_DATA, buf_ins, \
  337. sizeof(buf_res)) == 0)
  338. {
  339. sdcard_debug("SPISD: Get read command response failed!\n");
  340. break;
  341. }
  342. efm_spiSd_cs(0);
  343. /* Wait for data */
  344. for (i = 0; i < sizeof(buf_res); i++)
  345. {
  346. if (buf_res[i] != 0xff)
  347. {
  348. start = true;
  349. break;
  350. }
  351. }
  352. if (start)
  353. {
  354. break;
  355. }
  356. };
  357. /* Ckeck if valid */
  358. if (!start || (buf_res[i] != 0xfe))
  359. {
  360. sdcard_debug("SPISD: Token is invalid! (%x)\n", buf_res[i]);
  361. break;
  362. }
  363. /* Copy data to buffer and read the rest */
  364. len_copy = sizeof(buf_res) - i - 1;
  365. rt_memcpy(buffer, &buf_res[i + 1], len_copy);
  366. sdcard_debug("SPISD: Read block start at %d, copy %d bytes\n", i, \
  367. len_copy);
  368. /* Build instruction buffer */
  369. buf_ins[0] = 0; /* Instruction length */
  370. *(rt_uint8_t **)(&buf_ins[1]) = (rt_uint8_t *)buffer + len_copy; /* Pointer to RX buffer */
  371. /* Send read command */
  372. efm_spiSd_cs(1);
  373. if (spi->read(spi, EFM32_NO_DATA, buf_ins, size - len_copy) == 0)
  374. {
  375. sdcard_debug("SPISD: Read data block failed!\n");
  376. break;
  377. }
  378. *(rt_uint8_t **)(&buf_ins[1]) = buf_res; /* Pointer to RX buffer */
  379. if (spi->read(spi, EFM32_NO_DATA, buf_ins, sizeof(buf_res)) == 0)
  380. {
  381. sdcard_debug("SPISD: Read CRC failed!\n");
  382. break;
  383. }
  384. sdcard_debug("SPISD: Read CRC %x %x\n", buf_res[0], buf_res[1]);
  385. efm_spiSd_cs(0);
  386. return RT_EOK;
  387. } while(0);
  388. sdcard_debug("SPISD: Read block failed!\n");
  389. return -RT_ERROR;
  390. }
  391. /***************************************************************************//**
  392. * @brief
  393. * Write a block of data to memory device. This function is used to send data
  394. * and control tokens for block write commands (e.g. CMD24 and CMD25)
  395. *
  396. * @details
  397. *
  398. * @note
  399. *
  400. * @param[in] buffer
  401. * Poniter to the buffer
  402. *
  403. * @param[in] token
  404. * Control token
  405. *
  406. * @return
  407. * Error code
  408. ******************************************************************************/
  409. static rt_err_t efm_spiSd_writeBlock(void *buffer, rt_uint8_t token)
  410. {
  411. RT_ASSERT(spi != RT_NULL);
  412. rt_err_t ret;
  413. rt_uint8_t buf_ins[11];
  414. rt_uint8_t buf_res[8]; /* Expect a byte for data response */
  415. rt_uint8_t i;
  416. ret = RT_ERROR;
  417. do
  418. {
  419. /* Initialize timer */
  420. sdInTime = true;
  421. rt_timer_start(sdTimer);
  422. /* Wait for card ready */
  423. do
  424. {
  425. efm_spiSd_read(buf_res, sizeof(buf_res));
  426. } while (sdInTime && (buf_res[sizeof(buf_res) - 1] != 0xff));
  427. if (buf_res[sizeof(buf_res) - 1] != 0xff)
  428. {
  429. sdcard_debug("SPISD: Card is busy before writing! (%x)\n", \
  430. buf_res[sizeof(buf_res) - 1]);
  431. ret = -RT_EBUSY;
  432. break;
  433. }
  434. rt_timer_stop(sdTimer);
  435. /* Send token */
  436. buf_ins[0] = token;
  437. efm_spiSd_cs(1);
  438. if (spi->write(spi, EFM32_NO_DATA, buf_ins, 1) == 0)
  439. {
  440. sdcard_debug("SPISD: Write token failed!\n");
  441. break;
  442. }
  443. /* Send data */
  444. if (token != 0xfd)
  445. {
  446. if (spi->write(spi, EFM32_NO_DATA, buffer, SD_SECTOR_SIZE) == 0)
  447. {
  448. sdcard_debug("SPISD: Write data failed!\n");
  449. break;
  450. }
  451. /* Build instruction buffer */
  452. buf_ins[0] = 2; /* Instruction length */
  453. buf_ins[1] = 0xff; /* CRC (Dummy) */
  454. buf_ins[2] = 0xff;
  455. *(rt_uint8_t **)(&buf_ins[3]) = buf_res; /* Pointer to RX buffer */
  456. /* Send CRC and read a byte */
  457. if (spi->read(spi, EFM32_NO_DATA, buf_ins, sizeof(buf_res)) == 0)
  458. {
  459. sdcard_debug("SPISD: Write CRC failed!\n");
  460. break;
  461. }
  462. efm_spiSd_cs(0);
  463. /* Check if accepted */
  464. for (i = 0; i < sizeof(buf_res); i++)
  465. {
  466. if (buf_res[i] != 0xff)
  467. {
  468. buf_res[i] &= 0x1f;
  469. break;
  470. }
  471. }
  472. if (buf_res[i] != 0x05)
  473. {
  474. sdcard_debug("SPISD: Writing is not accepted! (%x at %d)\n", \
  475. buf_res[i], i);
  476. break;
  477. }
  478. }
  479. else
  480. {
  481. /* Initialize timer */
  482. sdInTime = true;
  483. rt_timer_start(sdTimer);
  484. /* Wait for card ready */
  485. do
  486. {
  487. efm_spiSd_read(buf_res, sizeof(buf_res));
  488. } while (sdInTime && (buf_res[sizeof(buf_res) - 1] != 0xff));
  489. if (buf_res[sizeof(buf_res) - 1] != 0xff)
  490. {
  491. sdcard_debug("SPISD: Card is busy after writing! (%x)\n", \
  492. buf_res[sizeof(buf_res) - 1] );
  493. ret = -RT_EBUSY;
  494. break;
  495. }
  496. rt_timer_stop(sdTimer);
  497. }
  498. return RT_EOK;
  499. } while(0);
  500. sdcard_debug("SPISD: Write block failed!\n");
  501. return ret;
  502. }
  503. /***************************************************************************//**
  504. * @brief
  505. * Wrapper function of send command to memory device
  506. *
  507. * @details
  508. *
  509. * @note
  510. *
  511. * @param[in] cmd
  512. * Command index
  513. *
  514. * @param[in] arg
  515. * Argument
  516. *
  517. * @param[in] trail
  518. * Pointer to the buffer to store trailing data
  519. *
  520. * @return
  521. * Command response
  522. ******************************************************************************/
  523. rt_uint16_t efm_spiSd_sendCmd(
  524. rt_uint8_t cmd,
  525. rt_uint32_t arg,
  526. rt_uint8_t *trail)
  527. {
  528. rt_uint16_t ret;
  529. /* ACMD<n> is the command sequense of CMD55-CMD<n> */
  530. if (cmd & 0x80)
  531. {
  532. cmd &= 0x7f;
  533. ret = efm_spiSd_cmd(CMD55, 0x00000000, EFM32_NO_POINTER);
  534. if (ret > 0x01)
  535. {
  536. return ret;
  537. }
  538. }
  539. return efm_spiSd_cmd(cmd, arg, trail);
  540. }
  541. /***************************************************************************//**
  542. * @brief
  543. * Initialize memory card device
  544. *
  545. * @details
  546. *
  547. * @note
  548. *
  549. * @param[in] dev
  550. * Pointer to device descriptor
  551. *
  552. * @return
  553. * Error code
  554. ******************************************************************************/
  555. static rt_err_t rt_spiSd_init(rt_device_t dev)
  556. {
  557. RT_ASSERT(spi != RT_NULL);
  558. rt_uint8_t type, cmd, tril[4];
  559. rt_uint8_t *buf_res;
  560. type = 0;
  561. buf_res = RT_NULL;
  562. do
  563. {
  564. /* Create and setup timer */
  565. if ((sdTimer = rt_timer_create(
  566. "sdTimer",
  567. efm_spiSd_timer,
  568. RT_NULL,
  569. SD_WAIT_PERIOD,
  570. RT_TIMER_FLAG_ONE_SHOT)) == RT_NULL)
  571. {
  572. sdcard_debug("SPISD: Create timer failed!\n");
  573. break;
  574. }
  575. /* Open SPI device */
  576. if (spi->open(spi, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  577. {
  578. break;
  579. }
  580. /* Switch to low speed */
  581. efm_spiSd_speed(SD_SPEED_LOW);
  582. /* 80 dummy clocks */
  583. efm_spiSd_read(RT_NULL, 80);
  584. /* Enter Idle state */
  585. if (efm_spiSd_sendCmd(CMD0, 0x00000000, EFM32_NO_POINTER) != 0x01)
  586. {
  587. break;
  588. }
  589. /* Check if SDv2 */
  590. if (efm_spiSd_sendCmd(CMD8, 0x000001AA, tril) == 0x01)
  591. {
  592. /* SDv2, Vdd: 2.7-3.6V */
  593. if (tril[2] == 0x01 && tril[3] == 0xAA)
  594. {
  595. /* Initialize timer */
  596. sdInTime = true;
  597. rt_timer_start(sdTimer);
  598. /* Wait for leaving idle state (ACMD41 with HCS bit) */
  599. while (efm_spiSd_sendCmd(ACMD41, 0x40000000, EFM32_NO_POINTER) \
  600. && sdInTime);
  601. /* Check CCS bit (bit 30) in the OCR */
  602. if (sdInTime && efm_spiSd_sendCmd(CMD58, 0x00000000, tril) \
  603. == 0x00)
  604. {
  605. type = (tril[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;
  606. }
  607. }
  608. }
  609. else
  610. {
  611. if (efm_spiSd_sendCmd(ACMD41, 0x00000000, EFM32_NO_POINTER) <= 0x01)
  612. {
  613. /* SDv1 */
  614. type = CT_SD1;
  615. cmd = ACMD41;
  616. }
  617. else
  618. {
  619. /* MMCv3 */
  620. type = CT_MMC;
  621. cmd = CMD1;
  622. }
  623. /* Initialize timer */
  624. sdInTime = true;
  625. rt_timer_start(sdTimer);
  626. /* Wait for leaving idle state */
  627. while (efm_spiSd_sendCmd(cmd, 0x00000000, EFM32_NO_POINTER) && \
  628. sdInTime);
  629. /* Set read/write block length to SD_BLOCK_SIZE */
  630. if (!sdInTime || \
  631. (efm_spiSd_sendCmd(CMD16, SD_SECTOR_SIZE, EFM32_NO_POINTER) \
  632. != 0x00))
  633. {
  634. type = 0;
  635. break;
  636. }
  637. }
  638. rt_timer_stop(sdTimer);
  639. /* Check type */
  640. sdType = type;
  641. if (sdType)
  642. {
  643. /* Initialization succeded */
  644. efm_spiSd_speed(SD_SPEED_HIGH);
  645. }
  646. else
  647. {
  648. break;
  649. }
  650. /* Allocate buffer */
  651. if ((buf_res = rt_malloc(SD_SECTOR_SIZE)) == RT_NULL)
  652. {
  653. sdcard_debug("SPISD: No memory for sector buffer\n");
  654. break;
  655. }
  656. /* Read the first sector for partition table */
  657. if (dev->read(dev, 0, buf_res, 1) != 1)
  658. {
  659. sdcard_debug("SPISD: Read first sector failed!\n");
  660. break;
  661. }
  662. /* Fetch the partition table */
  663. if (dfs_filesystem_get_partition(&sdPart, buf_res, 0) != RT_EOK)
  664. {
  665. sdPart.offset = 0;
  666. sdPart.size = 0;
  667. sdcard_debug("SPISD: No partition table\n");
  668. }
  669. /* Release buffer */
  670. rt_free(buf_res);
  671. sdcard_debug("SPISD: Init OK, card type %x\n", sdType);
  672. return RT_EOK;
  673. } while (0);
  674. /* Release buffer */
  675. if (buf_res)
  676. {
  677. rt_free(buf_res);
  678. }
  679. efm_spiSd_deinit();
  680. rt_kprintf("SPISD: Init failed!\n");
  681. return -RT_ERROR;
  682. }
  683. /***************************************************************************//**
  684. * @brief
  685. * Open memory card device
  686. *
  687. * @details
  688. *
  689. * @note
  690. *
  691. * @param[in] dev
  692. * Pointer to device descriptor
  693. *
  694. * @param[in] oflag
  695. * Device open flag
  696. *
  697. * @return
  698. * Error code
  699. ******************************************************************************/
  700. static rt_err_t rt_spiSd_open(rt_device_t dev, rt_uint16_t oflag)
  701. {
  702. sdcard_debug("SPISD: Open, flag %x\n", sd_device.flag);
  703. return RT_EOK;
  704. }
  705. /***************************************************************************//**
  706. * @brief
  707. * Close memory card device
  708. *
  709. * @details
  710. *
  711. * @note
  712. *
  713. * @param[in] dev
  714. * Pointer to device descriptor
  715. *
  716. * @return
  717. * Error code
  718. ******************************************************************************/
  719. static rt_err_t rt_spiSd_close(rt_device_t dev)
  720. {
  721. sdcard_debug("SPISD: Close, flag %x\n", sd_device.flag);
  722. return RT_EOK;
  723. }
  724. /***************************************************************************//**
  725. * @brief
  726. * Read from memory card device
  727. *
  728. * @details
  729. *
  730. * @note
  731. *
  732. * @param[in] dev
  733. * Pointer to device descriptor
  734. *
  735. * @param[in] sector
  736. * Start sector number (LBA)
  737. *
  738. * @param[in] buffer
  739. * Pointer to the buffer
  740. *
  741. * @param[in] count
  742. * Sector count (1..255)
  743. *
  744. * @return
  745. * Number of read sectors
  746. ******************************************************************************/
  747. static rt_size_t rt_spiSd_read(
  748. rt_device_t dev,
  749. rt_off_t sector,
  750. void *buffer,
  751. rt_size_t count)
  752. {
  753. rt_uint8_t buf_ins[11], buf_res[12];
  754. rt_uint8_t *ptr;
  755. rt_uint8_t cmd, i;
  756. rt_size_t cnt;
  757. ptr = (rt_uint8_t *)buffer;
  758. cnt = count;
  759. sdcard_debug("SPISD: ****** Read Data ******\n");
  760. if (!(sdType & CT_BLOCK))
  761. {
  762. /* Convert to byte address if needed */
  763. sector *= SD_SECTOR_SIZE;
  764. }
  765. do
  766. {
  767. if (cnt == 1)
  768. {
  769. /* Single block read */
  770. cmd = CMD17;
  771. sdcard_debug("SPISD: Read single block\n");
  772. }
  773. else
  774. {
  775. /* Multiple block read */
  776. cmd = CMD18;
  777. sdcard_debug("SPISD: Read multiple blocks\n");
  778. }
  779. if (efm_spiSd_sendCmd(cmd, sector, EFM32_NO_POINTER))
  780. {
  781. sdcard_debug("SPISD: Read command error!\n");
  782. break;
  783. }
  784. /* Read data */
  785. do
  786. {
  787. if (efm_spiSd_readBlock(ptr, SD_SECTOR_SIZE))
  788. {
  789. break;
  790. }
  791. ptr += SD_SECTOR_SIZE;
  792. } while(--cnt);
  793. /* Stop transmission */
  794. if (cmd == CMD18)
  795. {
  796. if (efm_spiSd_sendCmd(CMD12, 0x00000000, EFM32_NO_POINTER))
  797. {
  798. break;
  799. }
  800. }
  801. return (count);
  802. } while(0);
  803. return (0);
  804. }
  805. /***************************************************************************//**
  806. * @brief
  807. * Write to memory card device
  808. *
  809. * @details
  810. *
  811. * @note
  812. *
  813. * @param[in] dev
  814. * Pointer to device descriptor
  815. *
  816. * @param[in] sector
  817. * Start sector number (LBA)
  818. *
  819. * @param[in] buffer
  820. * Pointer to the buffer
  821. *
  822. * @param[in] count
  823. * Sector count (1..255)
  824. *
  825. * @return
  826. * Number of written sectors
  827. ******************************************************************************/
  828. static rt_size_t rt_spiSd_write (
  829. rt_device_t dev,
  830. rt_off_t sector,
  831. const void *buffer,
  832. rt_size_t count)
  833. {
  834. rt_uint8_t buf_ins[11], buf_res[12];
  835. rt_uint8_t *ptr;
  836. rt_uint8_t cmd, token, i;
  837. rt_size_t cnt;
  838. ptr = (rt_uint8_t *)buffer;
  839. cnt = count;
  840. sdcard_debug("SPISD: ****** Write Data ******\n");
  841. if (!(sdType & CT_BLOCK))
  842. {
  843. /* Convert to byte address if needed */
  844. sector *= SD_SECTOR_SIZE;
  845. }
  846. do
  847. {
  848. if (cnt == 1)
  849. {
  850. /* Single block write */
  851. cmd = CMD24;
  852. token = 0xfe;
  853. sdcard_debug("SPISD: Write single block\n");
  854. }
  855. else
  856. {
  857. /* Multiple block write */
  858. cmd = CMD25;
  859. token = 0xfc;
  860. sdcard_debug("SPISD: Write multiple blocks\n");
  861. if (sdType & CT_SDC)
  862. {
  863. if (efm_spiSd_sendCmd(ACMD23, count, EFM32_NO_POINTER))
  864. {
  865. break;
  866. }
  867. }
  868. }
  869. if (efm_spiSd_sendCmd(cmd, sector, EFM32_NO_POINTER))
  870. {
  871. sdcard_debug("SPISD: Write command error!\n");
  872. break;
  873. }
  874. /* Write data */
  875. do
  876. {
  877. if (efm_spiSd_writeBlock(ptr, token))
  878. {
  879. break;
  880. }
  881. ptr += SD_SECTOR_SIZE;
  882. } while(--cnt);
  883. /* Stop transmission token */
  884. if (efm_spiSd_writeBlock(EFM32_NO_POINTER, 0xfd))
  885. {
  886. break;
  887. }
  888. return (count);
  889. } while(0);
  890. return (0);
  891. }
  892. /***************************************************************************//**
  893. * @brief
  894. * Configure memory card device
  895. *
  896. * @details
  897. *
  898. * @note
  899. *
  900. * @param[in] dev
  901. * Pointer to device descriptor
  902. *
  903. * @param[in] ctrl
  904. * Memory card control command
  905. *
  906. * @param[in] buffer
  907. * Pointer to the buffer of in/out data
  908. *
  909. * @return
  910. * Error code
  911. ******************************************************************************/
  912. static rt_err_t rt_spiSd_control (
  913. rt_device_t dev,
  914. rt_uint8_t ctrl,
  915. void *buffer)
  916. {
  917. rt_err_t ret;
  918. rt_uint32_t c_size;
  919. rt_uint8_t n;
  920. rt_uint8_t *buf_res;
  921. ret = -RT_ERROR;
  922. buf_res = RT_NULL;
  923. switch (ctrl)
  924. {
  925. case RT_DEVICE_CTRL_SD_SYNC:
  926. /* Flush dirty buffer if present */
  927. efm_spiSd_cs(1);
  928. efm_spiSd_cs(0);
  929. ret = RT_EOK;
  930. break;
  931. case RT_DEVICE_CTRL_SD_GET_SCOUNT:
  932. {
  933. /* Allocate buffer */
  934. if ((buf_res = rt_malloc(SD_BLOCK_SIZE_CSD)) == RT_NULL)
  935. {
  936. sdcard_debug("SPISD: No memory for RX buffer\n");
  937. break;
  938. }
  939. /* Get number of sectors on the disk (32 bits) */
  940. if (efm_spiSd_sendCmd(CMD9, 0x00000000, buf_res))
  941. {
  942. sdcard_debug("SPISD: Get CSD failed!\n");
  943. break;
  944. }
  945. if ((buf_res[0] >> 6) == 0x01)
  946. {
  947. /* SDv2 */
  948. /* C_SIZE: Bit 48~69 */
  949. c_size = ((rt_uint32_t)(buf_res[7] & 0x3f) << 16) + \
  950. ((rt_uint32_t)buf_res[8] << 8) + buf_res[9] + 1;
  951. /* Result = Capacity / Sector Size */
  952. *(rt_uint32_t *)buffer = (rt_uint32_t)c_size << \
  953. (19 - SD_SECTOR_SIZE_SHIFT);
  954. }
  955. else
  956. {
  957. /* SDv1 or MMC */
  958. /* C_SIZE: Bit 62~73 */
  959. c_size = ((rt_uint32_t)(buf_res[6] & 0x03) << 10) + \
  960. ((rt_uint16_t)buf_res[7] << 2) + (buf_res[8] >> 6) + 1;
  961. /* READ_BL_LEN: Bit 80~83, C_SIZE_MULT: Bit 47~49 */
  962. n = ((buf_res[9] & 0x03) << 1) + ((buf_res[10] & 0x80) >> 7) + \
  963. 2 + (buf_res[5] & 0x0f);
  964. /* Result = Capacity / Sector Size */
  965. *(rt_uint32_t *)buffer = (rt_uint32_t)c_size << \
  966. (n - SD_SECTOR_SIZE_SHIFT);
  967. }
  968. ret = RT_EOK;
  969. break;
  970. }
  971. case RT_DEVICE_CTRL_SD_GET_SSIZE:
  972. /* Get sectors on the disk (16 bits) */
  973. *(rt_uint16_t *)buffer = SD_SECTOR_SIZE;
  974. ret = RT_EOK;
  975. break;
  976. case RT_DEVICE_CTRL_SD_GET_BSIZE:
  977. /* Get erase block size in unit of sectors (32 bits) */
  978. if (sdType & CT_SD2)
  979. {
  980. /* Allocate buffer */
  981. if ((buf_res = rt_malloc(SD_BLOCK_SIZE_SDSTAT)) == RT_NULL)
  982. {
  983. sdcard_debug("SPISD: No memory for RX buffer\n");
  984. break;
  985. }
  986. /* SDv2 */
  987. if (efm_spiSd_sendCmd(ACMD13, 0x00000000, EFM32_NO_POINTER))
  988. {
  989. sdcard_debug("SPISD: Get SD status failed!\n");
  990. break;
  991. }
  992. if (efm_spiSd_readBlock(buf_res, SD_BLOCK_SIZE_SDSTAT))
  993. {
  994. sdcard_debug("SPISD: Read SD status failed!\n");
  995. break;
  996. }
  997. /* AU_SIZE: Bit 428~431 */
  998. *(rt_uint32_t *)buffer = 16UL << ((buf_res[10] >> 4) + 9 - \
  999. SD_SECTOR_SIZE_SHIFT);
  1000. }
  1001. else
  1002. {
  1003. /* Allocate buffer */
  1004. if ((buf_res = rt_malloc(SD_BLOCK_SIZE_CSD)) == RT_NULL)
  1005. {
  1006. sdcard_debug("SPISD: No memory for RX buffer\n");
  1007. break;
  1008. }
  1009. /* SDv1 or MMC */
  1010. if (efm_spiSd_sendCmd(CMD9, 0x00000000, buf_res))
  1011. {
  1012. sdcard_debug("SPISD: Get CSD failed!\n");
  1013. break;
  1014. }
  1015. if (sdType & CT_SD1)
  1016. {
  1017. /* SECTOR_SIZE: Bit 39~45, WRITE_BL_LEN: Bit 22~25 (9, 10 or 11) */
  1018. *(rt_uint32_t *)buffer = (((buf_res[10] & 0x3f) << 1) + \
  1019. ((rt_uint32_t)(buf_res[11] & 0x80) >> 7) + 1) << \
  1020. (8 + (buf_res[13] >> 6) - SD_SECTOR_SIZE_SHIFT);
  1021. }
  1022. else
  1023. {
  1024. /* ERASE_GRP_SIZE: Bit 42~46, ERASE_GRP_MULT: Bit 37~41 */
  1025. *(rt_uint32_t *)buffer = \
  1026. ((rt_uint16_t)((buf_res[10] & 0x7c) >> 2) + 1) * \
  1027. (((buf_res[10] & 0x03) << 3) + \
  1028. ((buf_res[11] & 0xe0) >> 5) + 1);
  1029. }
  1030. }
  1031. ret = RT_EOK;
  1032. break;
  1033. case RT_DEVICE_CTRL_SD_GET_TYPE:
  1034. /* Get card type flags (1 byte) */
  1035. *(rt_uint8_t *)buffer = sdType;
  1036. ret = RT_EOK;
  1037. break;
  1038. case RT_DEVICE_CTRL_SD_GET_CSD:
  1039. /* Receive CSD as a data block (16 bytes) */
  1040. if (efm_spiSd_sendCmd(CMD9, 0x00000000, buffer))
  1041. {
  1042. sdcard_debug("SPISD: Get CSD failed!\n");
  1043. break;
  1044. }
  1045. ret = RT_EOK;
  1046. break;
  1047. case RT_DEVICE_CTRL_SD_GET_CID:
  1048. /* Receive CID as a data block (16 bytes) */
  1049. if (efm_spiSd_sendCmd(CMD10, 0x00000000, buffer))
  1050. {
  1051. sdcard_debug("SPISD: Get CID failed!\n");
  1052. break;
  1053. }
  1054. ret = RT_EOK;
  1055. break;
  1056. case RT_DEVICE_CTRL_SD_GET_OCR:
  1057. /* Receive OCR as an R3 resp (4 bytes) */
  1058. if (efm_spiSd_sendCmd(CMD58, 0x00000000, buffer))
  1059. {
  1060. sdcard_debug("SPISD: Get OCR failed!\n");
  1061. break;
  1062. }
  1063. ret = RT_EOK;
  1064. break;
  1065. case RT_DEVICE_CTRL_SD_GET_SDSTAT:
  1066. /* Receive SD statsu as a data block (64 bytes) */
  1067. if (efm_spiSd_sendCmd(ACMD13, 0x00000000, buffer))
  1068. {
  1069. sdcard_debug("SPISD: Get SD status failed!\n");
  1070. break;
  1071. }
  1072. if (efm_spiSd_readBlock(buffer, SD_BLOCK_SIZE_SDSTAT))
  1073. {
  1074. sdcard_debug("SPISD: Read SD status failed!\n");
  1075. break;
  1076. }
  1077. ret = RT_EOK;
  1078. break;
  1079. default:
  1080. break;
  1081. }
  1082. if (buf_res)
  1083. {
  1084. rt_free(buf_res);
  1085. }
  1086. return ret;
  1087. }
  1088. /***************************************************************************//**
  1089. * @brief
  1090. * Initialize all memory card related hardware and register the device to
  1091. * kernel
  1092. *
  1093. * @details
  1094. *
  1095. * @note
  1096. *
  1097. * @return
  1098. * Error code
  1099. ******************************************************************************/
  1100. rt_err_t efm_spiSd_init(void)
  1101. {
  1102. struct efm32_usart_device_t *usart;
  1103. #if defined(EFM32_G290_DK)
  1104. /* Enable SPI access to MicroSD card */
  1105. DVK_writeRegister(BC_SPI_CFG, 1);
  1106. #endif
  1107. do
  1108. {
  1109. /* Find SPI device */
  1110. spi = rt_device_find(SPISD_USING_DEVICE_NAME);
  1111. if (spi == RT_NULL)
  1112. {
  1113. sdcard_debug("SPISD: Can't find device %s!\n",
  1114. SPISD_USING_DEVICE_NAME);
  1115. break;
  1116. }
  1117. sdcard_debug("SPISD: Find device %s\n", SPISD_USING_DEVICE_NAME);
  1118. /* Config chip slect pin */
  1119. usart = (struct efm32_usart_device_t *)(spi->user_data);
  1120. if (!(usart->state & USART_STATE_AUTOCS))
  1121. {
  1122. GPIO_PinModeSet(SD_CS_PORT, SD_CS_PIN, gpioModePushPull, 1);
  1123. sdAutoCs = false;
  1124. }
  1125. /* Register SPI SD device */
  1126. sd_device.init = rt_spiSd_init;
  1127. sd_device.open = rt_spiSd_open;
  1128. sd_device.close = rt_spiSd_close;
  1129. sd_device.read = rt_spiSd_read;
  1130. sd_device.write = rt_spiSd_write;
  1131. sd_device.control = rt_spiSd_control;
  1132. sd_device.user_data = RT_NULL;
  1133. rt_device_register(
  1134. &sd_device,
  1135. SPISD_DEVICE_NAME,
  1136. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  1137. sdcard_debug("SPISD: HW init OK, card type %x\n", sdType);
  1138. return RT_EOK;
  1139. } while (0);
  1140. /* Release buffer */
  1141. rt_kprintf("SPISD: HW init failed!\n");
  1142. return -RT_ERROR;
  1143. }
  1144. /***************************************************************************//**
  1145. * @brief
  1146. * De-initialize memory card device
  1147. *
  1148. * @details
  1149. *
  1150. * @note
  1151. ******************************************************************************/
  1152. void efm_spiSd_deinit(void)
  1153. {
  1154. /* Close SPI device */
  1155. if (spi != RT_NULL)
  1156. {
  1157. spi->close(spi);
  1158. spi = RT_NULL;
  1159. sdcard_debug("SPISD: Close device %s\n", SPISD_USING_DEVICE_NAME);
  1160. }
  1161. /* Delete timer */
  1162. if (sdTimer != RT_NULL)
  1163. {
  1164. rt_timer_delete(sdTimer);
  1165. sdTimer = RT_NULL;
  1166. sdcard_debug("SPISD: Delete timer\n");
  1167. }
  1168. sdcard_debug("SPISD: Deinit OK\n");
  1169. }
  1170. /*******************************************************************************
  1171. * Export to FINSH
  1172. ******************************************************************************/
  1173. #ifdef RT_USING_FINSH
  1174. #include <finsh.h>
  1175. void list_sd(void)
  1176. {
  1177. rt_uint8_t buf_res[16];
  1178. rt_uint32_t capacity, temp32;
  1179. rt_uint16_t temp16;
  1180. rt_kprintf(" SD Card on %s\n", SPISD_USING_DEVICE_NAME);
  1181. rt_kprintf(" ------------------------------\n");
  1182. sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_CID, buf_res);
  1183. rt_kprintf(" Manufacturer ID:\t%x\n", buf_res[0]);
  1184. rt_kprintf(" OEM/Application ID:\t%x%x\n", buf_res[1], buf_res[2]);
  1185. rt_kprintf(" Product revision:\t%x\n", buf_res[8]);
  1186. buf_res[8] = 0;
  1187. rt_kprintf(" Product name:\t\t%s\n", &buf_res[3]);
  1188. rt_kprintf(" Serial number:\t\t%x%x%x%x\n", \
  1189. buf_res[9], buf_res[10], buf_res[11], buf_res[12]);
  1190. rt_kprintf(" Manufacturing date:\t%d.%d\n", \
  1191. 2000 + ((buf_res[13] & 0x0F) << 4) + ((buf_res[14] & 0xF0) >> 4), \
  1192. buf_res[14] & 0x0F);
  1193. rt_kprintf(" Card type:\t\t");
  1194. sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_TYPE, buf_res);
  1195. if (buf_res[0] == CT_MMC)
  1196. {
  1197. rt_kprintf("%s\n", "MMC");
  1198. }
  1199. else if (buf_res[0] == CT_SDC)
  1200. {
  1201. rt_kprintf("%s\n", "SDXC");
  1202. }
  1203. else if (buf_res[0] == CT_SD1)
  1204. {
  1205. rt_kprintf("%s\n", "SDSC");
  1206. }
  1207. else if (buf_res[0] == CT_SD2)
  1208. {
  1209. rt_kprintf("%s\n", "SDHC");
  1210. }
  1211. sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_SSIZE, &temp16);
  1212. sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_SCOUNT, &temp32);
  1213. capacity = ((temp32 & 0x0000FFFF) * temp16) >> 16;
  1214. capacity += ((temp32 >> 16) * temp16);
  1215. capacity >>= 4;
  1216. rt_kprintf(" Card capacity:\t\t%dMB\n", capacity);
  1217. }
  1218. FINSH_FUNCTION_EXPORT(list_sd, list the SD card.)
  1219. #endif
  1220. #endif /* defined(EFM32_USING_SPISD) */
  1221. /***************************************************************************//**
  1222. * @}
  1223. ******************************************************************************/