drv_sdcard.c 30 KB

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