drv_sdcard.c 31 KB

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