drv_sdcard.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. void list_sd(void)
  1178. {
  1179. rt_uint8_t buf_res[16];
  1180. rt_uint32_t capacity, temp32;
  1181. rt_uint16_t temp16;
  1182. rt_kprintf(" SD Card on %s\n", SPISD_USING_DEVICE_NAME);
  1183. rt_kprintf(" ------------------------------\n");
  1184. sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_CID, buf_res);
  1185. rt_kprintf(" Manufacturer ID:\t%x\n", buf_res[0]);
  1186. rt_kprintf(" OEM/Application ID:\t%x%x\n", buf_res[1], buf_res[2]);
  1187. rt_kprintf(" Product revision:\t%x\n", buf_res[8]);
  1188. buf_res[8] = 0;
  1189. rt_kprintf(" Product name:\t\t%s\n", &buf_res[3]);
  1190. rt_kprintf(" Serial number:\t\t%x%x%x%x\n", \
  1191. buf_res[9], buf_res[10], buf_res[11], buf_res[12]);
  1192. rt_kprintf(" Manufacturing date:\t%d.%d\n", \
  1193. 2000 + ((buf_res[13] & 0x0F) << 4) + ((buf_res[14] & 0xF0) >> 4), \
  1194. buf_res[14] & 0x0F);
  1195. rt_kprintf(" Card type:\t\t");
  1196. sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_TYPE, buf_res);
  1197. if (buf_res[0] == CT_MMC)
  1198. {
  1199. rt_kprintf("%s\n", "MMC");
  1200. }
  1201. else if (buf_res[0] == CT_SDC)
  1202. {
  1203. rt_kprintf("%s\n", "SDXC");
  1204. }
  1205. else if (buf_res[0] == CT_SD1)
  1206. {
  1207. rt_kprintf("%s\n", "SDSC");
  1208. }
  1209. else if (buf_res[0] == CT_SD2)
  1210. {
  1211. rt_kprintf("%s\n", "SDHC");
  1212. }
  1213. sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_SSIZE, &temp16);
  1214. sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_SCOUNT, &temp32);
  1215. capacity = ((temp32 & 0x0000FFFF) * temp16) >> 16;
  1216. capacity += ((temp32 >> 16) * temp16);
  1217. capacity >>= 4;
  1218. rt_kprintf(" Card capacity:\t\t%dMB\n", capacity);
  1219. }
  1220. #endif
  1221. #endif /* defined(EFM32_USING_SPISD) */
  1222. /***************************************************************************//**
  1223. * @}
  1224. ******************************************************************************/