drv_sdcard.c 37 KB

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