drv_sdcard.c 36 KB

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