spi_msd.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-04-17 Bernard first version.
  9. * 2010-07-15 aozima Modify read/write according new block driver interface.
  10. * 2012-02-01 aozima use new RT-Thread SPI drivers.
  11. * 2012-04-11 aozima get max. data transfer rate from CSD[TRAN_SPEED].
  12. * 2012-05-21 aozima update MMC card support.
  13. * 2018-03-09 aozima fixed CSD Version 2.0 sector count calc.
  14. */
  15. #include <string.h>
  16. #include "spi_msd.h"
  17. //#define MSD_TRACE
  18. #ifdef MSD_TRACE
  19. #define MSD_DEBUG(...) rt_kprintf("[MSD] %d ", rt_tick_get()); rt_kprintf(__VA_ARGS__);
  20. #else
  21. #define MSD_DEBUG(...)
  22. #endif /* #ifdef MSD_TRACE */
  23. #define DUMMY 0xFF
  24. #define CARD_NCR_MAX 9
  25. #define CARD_NRC 1
  26. #define CARD_NCR 1
  27. static struct msd_device _msd_device;
  28. /* function define */
  29. static rt_bool_t rt_tick_timeout(rt_tick_t tick_start, rt_tick_t tick_long);
  30. static rt_err_t MSD_take_owner(struct rt_spi_device *spi_device);
  31. static rt_err_t _wait_token(struct rt_spi_device *device, uint8_t token);
  32. static rt_err_t _wait_ready(struct rt_spi_device *device);
  33. static rt_err_t rt_msd_init(rt_device_t dev);
  34. static rt_err_t rt_msd_open(rt_device_t dev, rt_uint16_t oflag);
  35. static rt_err_t rt_msd_close(rt_device_t dev);
  36. static rt_size_t rt_msd_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  37. static rt_size_t rt_msd_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  38. static rt_size_t rt_msd_sdhc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  39. static rt_size_t rt_msd_sdhc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  40. static rt_err_t rt_msd_control(rt_device_t dev, int cmd, void *args);
  41. static rt_err_t MSD_take_owner(struct rt_spi_device *spi_device)
  42. {
  43. rt_err_t result;
  44. result = rt_mutex_take(&(spi_device->bus->lock), RT_WAITING_FOREVER);
  45. if (result == RT_EOK)
  46. {
  47. if (spi_device->bus->owner != spi_device)
  48. {
  49. /* not the same owner as current, re-configure SPI bus */
  50. result = spi_device->bus->ops->configure(spi_device, &spi_device->config);
  51. if (result == RT_EOK)
  52. {
  53. /* set SPI bus owner */
  54. spi_device->bus->owner = spi_device;
  55. }
  56. }
  57. }
  58. return result;
  59. }
  60. static rt_bool_t rt_tick_timeout(rt_tick_t tick_start, rt_tick_t tick_long)
  61. {
  62. rt_tick_t tick_end = tick_start + tick_long;
  63. rt_tick_t tick_now = rt_tick_get();
  64. rt_bool_t result = RT_FALSE;
  65. if (tick_end >= tick_start)
  66. {
  67. if (tick_now >= tick_end)
  68. {
  69. result = RT_TRUE;
  70. }
  71. else
  72. {
  73. result = RT_FALSE;
  74. }
  75. }
  76. else
  77. {
  78. if ((tick_now < tick_start) && (tick_now >= tick_end))
  79. {
  80. result = RT_TRUE;
  81. }
  82. else
  83. {
  84. result = RT_FALSE;
  85. }
  86. }
  87. return result;
  88. }
  89. static uint8_t crc7(const uint8_t *buf, int len)
  90. {
  91. unsigned char i, j, crc, ch, ch2, ch3;
  92. crc = 0;
  93. for (i = 0; i < len; i ++)
  94. {
  95. ch = buf[i];
  96. for (j = 0; j < 8; j ++, ch <<= 1)
  97. {
  98. ch2 = (crc & 0x40) ? 1 : 0;
  99. ch3 = (ch & 0x80) ? 1 : 0;
  100. if (ch2 ^ ch3)
  101. {
  102. crc ^= 0x04;
  103. crc <<= 1;
  104. crc |= 0x01;
  105. }
  106. else
  107. {
  108. crc <<= 1;
  109. }
  110. }
  111. }
  112. return crc;
  113. }
  114. static rt_err_t _send_cmd(
  115. struct rt_spi_device *device,
  116. uint8_t cmd,
  117. uint32_t arg,
  118. uint8_t crc,
  119. response_type type,
  120. uint8_t *response
  121. )
  122. {
  123. struct rt_spi_message message;
  124. uint8_t cmd_buffer[8];
  125. uint8_t recv_buffer[sizeof(cmd_buffer)];
  126. uint32_t i;
  127. cmd_buffer[0] = DUMMY;
  128. cmd_buffer[1] = (cmd | 0x40);
  129. cmd_buffer[2] = (uint8_t)(arg >> 24);
  130. cmd_buffer[3] = (uint8_t)(arg >> 16);
  131. cmd_buffer[4] = (uint8_t)(arg >> 8);
  132. cmd_buffer[5] = (uint8_t)(arg);
  133. if (crc == 0x00)
  134. {
  135. crc = crc7(&cmd_buffer[1], 5);
  136. crc = (crc << 1) | 0x01;
  137. }
  138. cmd_buffer[6] = (crc);
  139. cmd_buffer[7] = DUMMY;
  140. /* initial message */
  141. message.send_buf = cmd_buffer;
  142. message.recv_buf = recv_buffer;
  143. message.length = sizeof(cmd_buffer);
  144. message.cs_take = message.cs_release = 0;
  145. _wait_ready(device);
  146. /* transfer message */
  147. device->bus->ops->xfer(device, &message);
  148. for (i = CARD_NCR; i < (CARD_NCR_MAX + 1); i++)
  149. {
  150. uint8_t send = DUMMY;
  151. /* initial message */
  152. message.send_buf = &send;
  153. message.recv_buf = response;
  154. message.length = 1;
  155. message.cs_take = message.cs_release = 0;
  156. /* transfer message */
  157. device->bus->ops->xfer(device, &message);
  158. if (0 == (response[0] & 0x80))
  159. {
  160. break;
  161. }
  162. } /* wait response */
  163. if ((CARD_NCR_MAX + 1) == i)
  164. {
  165. return RT_ERROR;//fail
  166. }
  167. //recieve other byte
  168. if (type == response_r1)
  169. {
  170. return RT_EOK;
  171. }
  172. else if (type == response_r1b)
  173. {
  174. rt_tick_t tick_start = rt_tick_get();
  175. uint8_t recv;
  176. while (1)
  177. {
  178. /* initial message */
  179. message.send_buf = RT_NULL;
  180. message.recv_buf = &recv;
  181. message.length = 1;
  182. message.cs_take = message.cs_release = 0;
  183. /* transfer message */
  184. device->bus->ops->xfer(device, &message);
  185. if (recv == DUMMY)
  186. {
  187. return RT_EOK;
  188. }
  189. if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(2000)))
  190. {
  191. return RT_ETIMEOUT;
  192. }
  193. }
  194. }
  195. else if (type == response_r2)
  196. {
  197. /* initial message */
  198. /* Prevent non-aligned address access, use recv_buffer to receive data */
  199. message.send_buf = RT_NULL;
  200. message.recv_buf = recv_buffer;
  201. message.length = 1;
  202. message.cs_take = message.cs_release = 0;
  203. /* transfer message */
  204. device->bus->ops->xfer(device, &message);
  205. response[1] = recv_buffer[0];
  206. }
  207. else if ((type == response_r3) || (type == response_r7))
  208. {
  209. /* initial message */
  210. message.send_buf = RT_NULL;
  211. message.recv_buf = recv_buffer;
  212. message.length = 4;
  213. message.cs_take = message.cs_release = 0;
  214. /* transfer message */
  215. device->bus->ops->xfer(device, &message);
  216. response[1] = recv_buffer[0];
  217. response[2] = recv_buffer[1];
  218. response[3] = recv_buffer[2];
  219. response[4] = recv_buffer[3];
  220. }
  221. else
  222. {
  223. return RT_ERROR; // unknow type?
  224. }
  225. return RT_EOK;
  226. }
  227. static rt_err_t _wait_token(struct rt_spi_device *device, uint8_t token)
  228. {
  229. struct rt_spi_message message;
  230. rt_tick_t tick_start;
  231. uint8_t send, recv;
  232. tick_start = rt_tick_get();
  233. /* wati token */
  234. /* initial message */
  235. send = DUMMY;
  236. message.send_buf = &send;
  237. message.recv_buf = &recv;
  238. message.length = 1;
  239. message.cs_take = message.cs_release = 0;
  240. while (1)
  241. {
  242. /* transfer message */
  243. device->bus->ops->xfer(device, &message);
  244. if (recv == token)
  245. {
  246. return RT_EOK;
  247. }
  248. if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_WAIT_TOKEN_TIMES)))
  249. {
  250. MSD_DEBUG("[err] wait data start token timeout!\r\n");
  251. return RT_ETIMEOUT;
  252. }
  253. } /* wati token */
  254. }
  255. static rt_err_t _wait_ready(struct rt_spi_device *device)
  256. {
  257. struct rt_spi_message message;
  258. rt_tick_t tick_start;
  259. uint8_t send, recv;
  260. tick_start = rt_tick_get();
  261. send = DUMMY;
  262. /* initial message */
  263. message.send_buf = &send;
  264. message.recv_buf = &recv;
  265. message.length = 1;
  266. message.cs_take = message.cs_release = 0;
  267. while (1)
  268. {
  269. /* transfer message */
  270. device->bus->ops->xfer(device, &message);
  271. if (recv == DUMMY)
  272. {
  273. return RT_EOK;
  274. }
  275. if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(1000)))
  276. {
  277. MSD_DEBUG("[err] wait ready timeout!\r\n");
  278. return RT_ETIMEOUT;
  279. }
  280. }
  281. }
  282. static rt_err_t _read_block(struct rt_spi_device *device, void *buffer, uint32_t block_size)
  283. {
  284. struct rt_spi_message message;
  285. rt_err_t result;
  286. /* wati token */
  287. result = _wait_token(device, MSD_TOKEN_READ_START);
  288. if (result != RT_EOK)
  289. {
  290. return result;
  291. }
  292. /* read data */
  293. {
  294. /* initial message */
  295. message.send_buf = RT_NULL;
  296. message.recv_buf = buffer;
  297. message.length = block_size;
  298. message.cs_take = message.cs_release = 0;
  299. /* transfer message */
  300. device->bus->ops->xfer(device, &message);
  301. } /* read data */
  302. /* get crc */
  303. {
  304. uint8_t recv_buffer[2];
  305. /* initial message */
  306. message.send_buf = RT_NULL;
  307. message.recv_buf = recv_buffer;
  308. message.length = 2;
  309. message.cs_take = message.cs_release = 0;
  310. /* transfer message */
  311. device->bus->ops->xfer(device, &message);
  312. } /* get crc */
  313. return RT_EOK;
  314. }
  315. static rt_err_t _write_block(struct rt_spi_device *device, const void *buffer, uint32_t block_size, uint8_t token)
  316. {
  317. struct rt_spi_message message;
  318. uint8_t send_buffer[16];
  319. rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
  320. send_buffer[sizeof(send_buffer) - 1] = token;
  321. /* send start block token */
  322. {
  323. /* initial message */
  324. message.send_buf = send_buffer;
  325. message.recv_buf = RT_NULL;
  326. message.length = sizeof(send_buffer);
  327. message.cs_take = message.cs_release = 0;
  328. /* transfer message */
  329. device->bus->ops->xfer(device, &message);
  330. }
  331. /* send data */
  332. {
  333. /* initial message */
  334. message.send_buf = buffer;
  335. message.recv_buf = RT_NULL;
  336. message.length = block_size;
  337. message.cs_take = message.cs_release = 0;
  338. /* transfer message */
  339. device->bus->ops->xfer(device, &message);
  340. }
  341. /* put crc and get data response */
  342. {
  343. uint8_t recv_buffer[3];
  344. uint8_t response;
  345. /* initial message */
  346. message.send_buf = send_buffer;
  347. message.recv_buf = recv_buffer;
  348. message.length = sizeof(recv_buffer);
  349. message.cs_take = message.cs_release = 0;
  350. /* transfer message */
  351. device->bus->ops->xfer(device, &message);
  352. // response = 0x0E & recv_buffer[2];
  353. response = MSD_GET_DATA_RESPONSE(recv_buffer[2]);
  354. if (response != MSD_DATA_OK)
  355. {
  356. MSD_DEBUG("[err] write block fail! data response : 0x%02X\r\n", response);
  357. return RT_ERROR;
  358. }
  359. }
  360. /* wati ready */
  361. return _wait_ready(device);
  362. }
  363. #ifdef RT_USING_DEVICE_OPS
  364. const static struct rt_device_ops msd_ops =
  365. {
  366. rt_msd_init,
  367. rt_msd_open,
  368. rt_msd_close,
  369. rt_msd_read,
  370. rt_msd_write,
  371. rt_msd_control
  372. };
  373. const static struct rt_device_ops msd_sdhc_ops =
  374. {
  375. rt_msd_init,
  376. rt_msd_open,
  377. rt_msd_close,
  378. rt_msd_sdhc_read,
  379. rt_msd_sdhc_write,
  380. rt_msd_control
  381. };
  382. #endif
  383. /* RT-Thread Device Driver Interface */
  384. static rt_err_t rt_msd_init(rt_device_t dev)
  385. {
  386. struct msd_device *msd = (struct msd_device *)dev;
  387. uint8_t response[MSD_RESPONSE_MAX_LEN];
  388. rt_err_t result = RT_EOK;
  389. rt_tick_t tick_start;
  390. uint32_t OCR;
  391. if (msd->spi_device == RT_NULL)
  392. {
  393. MSD_DEBUG("[err] the SPI SD device has no SPI!\r\n");
  394. return RT_EIO;
  395. }
  396. /* config spi */
  397. {
  398. struct rt_spi_configuration cfg;
  399. cfg.data_width = 8;
  400. cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
  401. cfg.max_hz = 1000 * 400; /* 400kbit/s */
  402. rt_spi_configure(msd->spi_device, &cfg);
  403. } /* config spi */
  404. /* init SD card */
  405. {
  406. struct rt_spi_message message;
  407. result = MSD_take_owner(msd->spi_device);
  408. if (result != RT_EOK)
  409. {
  410. goto _exit;
  411. }
  412. rt_spi_release(msd->spi_device);
  413. /* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and
  414. start to supply at least 74 SD clocks to the SD card with keeping CMD line to high.
  415. In case of SPI mode, CS shall be held to high during 74 clock cycles. */
  416. {
  417. uint8_t send_buffer[100]; /* 100byte > 74 clock */
  418. /* initial message */
  419. rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
  420. message.send_buf = send_buffer;
  421. message.recv_buf = RT_NULL;
  422. message.length = sizeof(send_buffer);
  423. message.cs_take = message.cs_release = 0;
  424. /* transfer message */
  425. msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
  426. } /* send 74 clock */
  427. /* Send CMD0 (GO_IDLE_STATE) to put MSD in SPI mode */
  428. {
  429. tick_start = rt_tick_get();
  430. while (1)
  431. {
  432. rt_spi_take(msd->spi_device);
  433. result = _send_cmd(msd->spi_device, GO_IDLE_STATE, 0x00, 0x95, response_r1, response);
  434. rt_spi_release(msd->spi_device);
  435. if ((result == RT_EOK) && (response[0] == MSD_IN_IDLE_STATE))
  436. {
  437. break;
  438. }
  439. if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
  440. {
  441. MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
  442. result = RT_ETIMEOUT;
  443. goto _exit;
  444. }
  445. }
  446. MSD_DEBUG("[info] SD card goto IDLE mode OK!\r\n");
  447. } /* Send CMD0 (GO_IDLE_STATE) to put MSD in SPI mode */
  448. /* CMD8 */
  449. {
  450. tick_start = rt_tick_get();
  451. do
  452. {
  453. rt_spi_take(msd->spi_device);
  454. result = _send_cmd(msd->spi_device, SEND_IF_COND, 0x01AA, 0x87, response_r7, response);
  455. rt_spi_release(msd->spi_device);
  456. if (result == RT_EOK)
  457. {
  458. MSD_DEBUG("[info] CMD8 response : 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\r\n",
  459. response[0], response[1], response[2], response[3], response[4]);
  460. if (response[0] & (1 << 2))
  461. {
  462. /* illegal command, SD V1.x or MMC card */
  463. MSD_DEBUG("[info] CMD8 is illegal command.\r\n");
  464. MSD_DEBUG("[info] maybe Ver1.X SD Memory Card or MMC card!\r\n");
  465. msd->card_type = MSD_CARD_TYPE_SD_V1_X;
  466. break;
  467. }
  468. else
  469. {
  470. /* SD V2.0 or later or SDHC or SDXC memory card! */
  471. MSD_DEBUG("[info] Ver2.00 or later or SDHC or SDXC memory card!\r\n");
  472. msd->card_type = MSD_CARD_TYPE_SD_V2_X;
  473. }
  474. if ((0xAA == response[4]) && (0x00 == response[3]))
  475. {
  476. /* SD2.0 not support current voltage */
  477. MSD_DEBUG("[err] VCA = 0, SD2.0 not surpport current operation voltage range\r\n");
  478. result = RT_ERROR;
  479. goto _exit;
  480. }
  481. }
  482. else
  483. {
  484. if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(200)))
  485. {
  486. MSD_DEBUG("[err] CMD8 SEND_IF_COND timeout!\r\n");
  487. result = RT_ETIMEOUT;
  488. goto _exit;
  489. }
  490. }
  491. }
  492. while (0xAA != response[4]);
  493. } /* CMD8 */
  494. /* Ver1.X SD Memory Card or MMC card */
  495. if (msd->card_type == MSD_CARD_TYPE_SD_V1_X)
  496. {
  497. rt_bool_t is_sd_v1_x = RT_FALSE;
  498. rt_tick_t tick_start;
  499. /* try SD Ver1.x */
  500. while (1)
  501. {
  502. rt_spi_take(msd->spi_device);
  503. result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
  504. if (result != RT_EOK)
  505. {
  506. rt_spi_release(msd->spi_device);
  507. MSD_DEBUG("[info] It maybe SD1.x or MMC But it is Not response to CMD58!\r\n");
  508. goto _exit;
  509. }
  510. if (0 != (response[0] & 0xFE))
  511. {
  512. rt_spi_release(msd->spi_device);
  513. MSD_DEBUG("[info] It look CMD58 as illegal command so it is not SD card!\r\n");
  514. break;
  515. }
  516. rt_spi_release(msd->spi_device);
  517. OCR = response[1];
  518. OCR = (OCR << 8) + response[2];
  519. OCR = (OCR << 8) + response[3];
  520. OCR = (OCR << 8) + response[4];
  521. MSD_DEBUG("[info] OCR is 0x%08X\r\n", OCR);
  522. if (0 == (OCR & (0x1 << 15)))
  523. {
  524. MSD_DEBUG(("[err] SD 1.x But not surpport current voltage\r\n"));
  525. result = RT_ERROR;
  526. goto _exit;
  527. }
  528. /* --Send ACMD41 to make card ready */
  529. tick_start = rt_tick_get();
  530. /* try CMD55 + ACMD41 */
  531. while (1)
  532. {
  533. if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES_ACMD41)))
  534. {
  535. rt_spi_release(msd->spi_device);
  536. MSD_DEBUG("[info] try CMD55 + ACMD41 timeout! mabey MMC card!\r\n");
  537. break;
  538. }
  539. rt_spi_take(msd->spi_device);
  540. /* CMD55 APP_CMD */
  541. result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
  542. if (result != RT_EOK)
  543. {
  544. rt_spi_release(msd->spi_device);
  545. continue;
  546. }
  547. if (0 != (response[0] & 0xFE))
  548. {
  549. rt_spi_release(msd->spi_device);
  550. MSD_DEBUG("[info] Not SD card2 , may be MMC\r\n");
  551. break;
  552. }
  553. /* ACMD41 SD_SEND_OP_COND */
  554. result = _send_cmd(msd->spi_device, SD_SEND_OP_COND, 0x00, 0x00, response_r1, response);
  555. if (result != RT_EOK)
  556. {
  557. rt_spi_release(msd->spi_device);
  558. continue;
  559. }
  560. if (0 != (response[0] & 0xFE))
  561. {
  562. rt_spi_release(msd->spi_device);
  563. MSD_DEBUG("[info] Not SD card4 , may be MMC\r\n");
  564. break;
  565. }
  566. if (0 == (response[0] & 0xFF))
  567. {
  568. rt_spi_release(msd->spi_device);
  569. is_sd_v1_x = RT_TRUE;
  570. MSD_DEBUG("[info] It is Ver1.X SD Memory Card!!!\r\n");
  571. break;
  572. }
  573. } /* try CMD55 + ACMD41 */
  574. break;
  575. } /* try SD Ver1.x */
  576. /* try MMC */
  577. if (is_sd_v1_x != RT_TRUE)
  578. {
  579. uint32_t i;
  580. MSD_DEBUG("[info] try MMC card!\r\n");
  581. rt_spi_release(msd->spi_device);
  582. /* send dummy clock */
  583. {
  584. uint8_t send_buffer[100];
  585. /* initial message */
  586. rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
  587. message.send_buf = send_buffer;
  588. message.recv_buf = RT_NULL;
  589. message.length = sizeof(send_buffer);
  590. message.cs_take = message.cs_release = 0;
  591. for (i = 0; i < 10; i++)
  592. {
  593. /* transfer message */
  594. msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
  595. }
  596. } /* send dummy clock */
  597. /* send CMD0 goto IDLE state */
  598. tick_start = rt_tick_get();
  599. while (1)
  600. {
  601. rt_spi_take(msd->spi_device);
  602. result = _send_cmd(msd->spi_device, GO_IDLE_STATE, 0x00, 0x95, response_r1, response);
  603. rt_spi_release(msd->spi_device);
  604. if ((result == RT_EOK) && (response[0] == MSD_IN_IDLE_STATE))
  605. {
  606. break;
  607. }
  608. if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
  609. {
  610. MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
  611. result = RT_ETIMEOUT;
  612. goto _exit;
  613. }
  614. } /* send CMD0 goto IDLE stat */
  615. /* send CMD1 */
  616. tick_start = rt_tick_get();
  617. while (1)
  618. {
  619. rt_spi_take(msd->spi_device);
  620. result = _send_cmd(msd->spi_device, SEND_OP_COND, 0x00, 0x00, response_r1, response);
  621. rt_spi_release(msd->spi_device);
  622. if ((result == RT_EOK) && (response[0] == MSD_RESPONSE_NO_ERROR))
  623. {
  624. MSD_DEBUG("[info] It is MMC card!!!\r\n");
  625. msd->card_type = MSD_CARD_TYPE_MMC;
  626. break;
  627. }
  628. if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
  629. {
  630. MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
  631. result = RT_ETIMEOUT;
  632. goto _exit;
  633. }
  634. } /* send CMD1 */
  635. } /* try MMC */
  636. }
  637. else if (msd->card_type == MSD_CARD_TYPE_SD_V2_X)
  638. {
  639. rt_spi_take(msd->spi_device);
  640. result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
  641. if (result != RT_EOK)
  642. {
  643. rt_spi_release(msd->spi_device);
  644. MSD_DEBUG("[err] It maybe SD2.0 But it is Not response to CMD58!\r\n");
  645. goto _exit;
  646. }
  647. if ((response[0] & 0xFE) != 0)
  648. {
  649. rt_spi_release(msd->spi_device);
  650. MSD_DEBUG("[err] It look CMD58 as illegal command so it is not SD card!\r\n");
  651. result = RT_ERROR;
  652. goto _exit;
  653. }
  654. rt_spi_release(msd->spi_device);
  655. OCR = response[1];
  656. OCR = (OCR << 8) + response[2];
  657. OCR = (OCR << 8) + response[3];
  658. OCR = (OCR << 8) + response[4];
  659. MSD_DEBUG("[info] OCR is 0x%08X\r\n", OCR);
  660. if (0 == (OCR & (0x1 << 15)))
  661. {
  662. MSD_DEBUG(("[err] SD 1.x But not surpport current voltage\r\n"));
  663. result = RT_ERROR;
  664. goto _exit;
  665. }
  666. /* --Send ACMD41 to make card ready */
  667. tick_start = rt_tick_get();
  668. /* try CMD55 + ACMD41 */
  669. do
  670. {
  671. rt_spi_take(msd->spi_device);
  672. if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES_ACMD41)))
  673. {
  674. rt_spi_release(msd->spi_device);
  675. MSD_DEBUG("[err] SD Ver2.x or later try CMD55 + ACMD41 timeout!\r\n");
  676. result = RT_ERROR;
  677. goto _exit;
  678. }
  679. /* CMD55 APP_CMD */
  680. result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x65, response_r1, response);
  681. // if((result != RT_EOK) || (response[0] == 0x01))
  682. if (result != RT_EOK)
  683. {
  684. rt_spi_release(msd->spi_device);
  685. continue;
  686. }
  687. if ((response[0] & 0xFE) != 0)
  688. {
  689. rt_spi_release(msd->spi_device);
  690. MSD_DEBUG("[err] Not SD ready!\r\n");
  691. result = RT_ERROR;
  692. goto _exit;
  693. }
  694. /* ACMD41 SD_SEND_OP_COND */
  695. result = _send_cmd(msd->spi_device, SD_SEND_OP_COND, 0x40000000, 0x77, response_r1, response);
  696. if (result != RT_EOK)
  697. {
  698. rt_spi_release(msd->spi_device);
  699. MSD_DEBUG("[err] ACMD41 fail!\r\n");
  700. result = RT_ERROR;
  701. goto _exit;
  702. }
  703. if ((response[0] & 0xFE) != 0)
  704. {
  705. rt_spi_release(msd->spi_device);
  706. MSD_DEBUG("[info] Not SD card4 , response : 0x%02X\r\n", response[0]);
  707. // break;
  708. }
  709. }
  710. while (response[0] != MSD_RESPONSE_NO_ERROR);
  711. rt_spi_release(msd->spi_device);
  712. /* try CMD55 + ACMD41 */
  713. /* --Read OCR again */
  714. rt_spi_take(msd->spi_device);
  715. result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
  716. if (result != RT_EOK)
  717. {
  718. rt_spi_release(msd->spi_device);
  719. MSD_DEBUG("[err] It maybe SD2.0 But it is Not response to 2nd CMD58!\r\n");
  720. goto _exit;
  721. }
  722. if ((response[0] & 0xFE) != 0)
  723. {
  724. rt_spi_release(msd->spi_device);
  725. MSD_DEBUG("[err] It look 2nd CMD58 as illegal command so it is not SD card!\r\n");
  726. result = RT_ERROR;
  727. goto _exit;
  728. }
  729. rt_spi_release(msd->spi_device);
  730. OCR = response[1];
  731. OCR = (OCR << 8) + response[2];
  732. OCR = (OCR << 8) + response[3];
  733. OCR = (OCR << 8) + response[4];
  734. MSD_DEBUG("[info] OCR 2nd read is 0x%08X\r\n", OCR);
  735. if ((OCR & 0x40000000) != 0)
  736. {
  737. MSD_DEBUG("[info] It is SD2.0 SDHC Card!!!\r\n");
  738. msd->card_type = MSD_CARD_TYPE_SD_SDHC;
  739. }
  740. else
  741. {
  742. MSD_DEBUG("[info] It is SD2.0 standard capacity Card!!!\r\n");
  743. }
  744. } /* MSD_CARD_TYPE_SD_V2_X */
  745. else
  746. {
  747. MSD_DEBUG("[err] SD card type unkonw!\r\n");
  748. result = RT_ERROR;
  749. goto _exit;
  750. }
  751. } /* init SD card */
  752. if (msd->card_type == MSD_CARD_TYPE_SD_SDHC)
  753. {
  754. #ifdef RT_USING_DEVICE_OPS
  755. dev->ops = &msd_sdhc_ops;
  756. #else
  757. dev->read = rt_msd_sdhc_read;
  758. dev->write = rt_msd_sdhc_write;
  759. #endif
  760. }
  761. else
  762. {
  763. #ifdef RT_USING_DEVICE_OPS
  764. dev->ops = &msd_ops;
  765. #else
  766. dev->read = rt_msd_read;
  767. dev->write = rt_msd_write;
  768. #endif
  769. }
  770. /* set CRC */
  771. {
  772. rt_spi_release(msd->spi_device);
  773. rt_spi_take(msd->spi_device);
  774. #ifdef MSD_USE_CRC
  775. result = _send_cmd(msd->spi_device, CRC_ON_OFF, 0x01, 0x83, response_r1, response);
  776. #else
  777. result = _send_cmd(msd->spi_device, CRC_ON_OFF, 0x00, 0x91, response_r1, response);
  778. #endif
  779. rt_spi_release(msd->spi_device);
  780. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  781. {
  782. MSD_DEBUG("[err] CMD59 CRC_ON_OFF fail! response : 0x%02X\r\n", response[0]);
  783. result = RT_ERROR;
  784. goto _exit;
  785. }
  786. } /* set CRC */
  787. /* CMD16 SET_BLOCKLEN */
  788. {
  789. rt_spi_release(msd->spi_device);
  790. rt_spi_take(msd->spi_device);
  791. result = _send_cmd(msd->spi_device, SET_BLOCKLEN, SECTOR_SIZE, 0x00, response_r1, response);
  792. rt_spi_release(msd->spi_device);
  793. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  794. {
  795. MSD_DEBUG("[err] CMD16 SET_BLOCKLEN fail! response : 0x%02X\r\n", response[0]);
  796. result = RT_ERROR;
  797. goto _exit;
  798. }
  799. msd->geometry.block_size = SECTOR_SIZE;
  800. msd->geometry.bytes_per_sector = SECTOR_SIZE;
  801. }
  802. /* read CSD */
  803. {
  804. uint8_t CSD_buffer[MSD_CSD_LEN];
  805. rt_spi_take(msd->spi_device);
  806. // result = _send_cmd(msd->spi_device, SEND_CSD, 0x00, 0xAF, response_r1, response);
  807. result = _send_cmd(msd->spi_device, SEND_CSD, 0x00, 0x00, response_r1, response);
  808. if (result != RT_EOK)
  809. {
  810. rt_spi_release(msd->spi_device);
  811. MSD_DEBUG("[err] CMD9 SEND_CSD timeout!\r\n");
  812. goto _exit;
  813. }
  814. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  815. {
  816. rt_spi_release(msd->spi_device);
  817. MSD_DEBUG("[err] CMD9 SEND_CSD fail! response : 0x%02X\r\n", response[0]);
  818. result = RT_ERROR;
  819. goto _exit;
  820. }
  821. result = _read_block(msd->spi_device, CSD_buffer, MSD_CSD_LEN);
  822. rt_spi_release(msd->spi_device);
  823. if (result != RT_EOK)
  824. {
  825. MSD_DEBUG("[err] read CSD fail!\r\n");
  826. goto _exit;
  827. }
  828. /* Analyze CSD */
  829. {
  830. uint8_t CSD_STRUCTURE;
  831. uint32_t C_SIZE;
  832. uint32_t card_capacity;
  833. uint8_t tmp8;
  834. uint16_t tmp16;
  835. uint32_t tmp32;
  836. /* get CSD_STRUCTURE */
  837. tmp8 = CSD_buffer[0] & 0xC0; /* 0b11000000 */
  838. CSD_STRUCTURE = tmp8 >> 6;
  839. /* MMC CSD Analyze. */
  840. if (msd->card_type == MSD_CARD_TYPE_MMC)
  841. {
  842. uint8_t C_SIZE_MULT;
  843. uint8_t READ_BL_LEN;
  844. if (CSD_STRUCTURE > 2)
  845. {
  846. MSD_DEBUG("[err] bad CSD Version : %d\r\n", CSD_STRUCTURE);
  847. result = RT_ERROR;
  848. goto _exit;
  849. }
  850. if (CSD_STRUCTURE == 0)
  851. {
  852. MSD_DEBUG("[info] CSD version No. 1.0\r\n");
  853. }
  854. else if (CSD_STRUCTURE == 1)
  855. {
  856. MSD_DEBUG("[info] CSD version No. 1.1\r\n");
  857. }
  858. else if (CSD_STRUCTURE == 2)
  859. {
  860. MSD_DEBUG("[info] CSD version No. 1.2\r\n");
  861. }
  862. /* get TRAN_SPEED 8bit [103:96] */
  863. tmp8 = CSD_buffer[3];
  864. tmp8 &= 0x03; /* [2:0] transfer rate unit.*/
  865. if (tmp8 == 0)
  866. {
  867. msd->max_clock = 100 * 1000; /* 0=100kbit/s. */
  868. }
  869. else if (tmp8 == 1)
  870. {
  871. msd->max_clock = 1 * 1000 * 1000; /* 1=1Mbit/s. */
  872. }
  873. else if (tmp8 == 2)
  874. {
  875. msd->max_clock = 10 * 1000 * 1000; /* 2=10Mbit/s. */
  876. }
  877. else if (tmp8 == 3)
  878. {
  879. msd->max_clock = 100 * 1000 * 1000; /* 3=100Mbit/s. */
  880. }
  881. if (tmp8 == 0)
  882. {
  883. MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dkbit/s.\r\n", tmp8, msd->max_clock / 1000);
  884. }
  885. else
  886. {
  887. MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dMbit/s.\r\n", tmp8, msd->max_clock / 1000 / 1000);
  888. }
  889. /* get READ_BL_LEN 4bit [83:80] */
  890. tmp8 = CSD_buffer[5] & 0x0F; /* 0b00001111; */
  891. READ_BL_LEN = tmp8; /* 4 bit */
  892. MSD_DEBUG("[info] CSD : READ_BL_LEN : %d %dbyte\r\n", READ_BL_LEN, (1 << READ_BL_LEN));
  893. /* get C_SIZE 12bit [73:62] */
  894. tmp16 = CSD_buffer[6] & 0x03; /* get [73:72] 0b00000011 */
  895. tmp16 = tmp16 << 8;
  896. tmp16 += CSD_buffer[7]; /* get [71:64] */
  897. tmp16 = tmp16 << 2;
  898. tmp8 = CSD_buffer[8] & 0xC0; /* get [63:62] 0b11000000 */
  899. tmp8 = tmp8 >> 6;
  900. tmp16 = tmp16 + tmp8;
  901. C_SIZE = tmp16; //12 bit
  902. MSD_DEBUG("[info] CSD : C_SIZE : %d\r\n", C_SIZE);
  903. /* get C_SIZE_MULT 3bit [49:47] */
  904. tmp8 = CSD_buffer[9] & 0x03;//0b00000011;
  905. tmp8 = tmp8 << 1;
  906. tmp8 = tmp8 + ((CSD_buffer[10] & 0x80/*0b10000000*/) >> 7);
  907. C_SIZE_MULT = tmp8; // 3 bit
  908. MSD_DEBUG("[info] CSD : C_SIZE_MULT : %d\r\n", C_SIZE_MULT);
  909. /* memory capacity = BLOCKNR * BLOCK_LEN */
  910. /* BLOCKNR = (C_SIZE+1) * MULT */
  911. /* MULT = 2^(C_SIZE_MULT+2) */
  912. /* BLOCK_LEN = 2^READ_BL_LEN */
  913. card_capacity = (1 << READ_BL_LEN) * ((C_SIZE + 1) * (1 << (C_SIZE_MULT + 2)));
  914. msd->geometry.sector_count = card_capacity / msd->geometry.bytes_per_sector;
  915. MSD_DEBUG("[info] card capacity : %d Mbyte\r\n", card_capacity / (1024 * 1024));
  916. }
  917. else /* SD CSD Analyze. */
  918. {
  919. if (CSD_STRUCTURE == 0)
  920. {
  921. uint8_t C_SIZE_MULT;
  922. uint8_t READ_BL_LEN;
  923. MSD_DEBUG("[info] CSD Version 1.0\r\n");
  924. /* get TRAN_SPEED 8bit [103:96] */
  925. tmp8 = CSD_buffer[3];
  926. if (tmp8 == 0x32)
  927. {
  928. msd->max_clock = 1000 * 1000 * 10; /* 10Mbit/s. */
  929. }
  930. else if (tmp8 == 0x5A)
  931. {
  932. msd->max_clock = 1000 * 1000 * 50; /* 50Mbit/s. */
  933. }
  934. else
  935. {
  936. msd->max_clock = 1000 * 1000 * 1; /* 1Mbit/s default. */
  937. }
  938. MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dMbit/s.\r\n", tmp8, msd->max_clock / 1000 / 1000);
  939. /* get READ_BL_LEN 4bit [83:80] */
  940. tmp8 = CSD_buffer[5] & 0x0F; /* 0b00001111; */
  941. READ_BL_LEN = tmp8; /* 4 bit */
  942. MSD_DEBUG("[info] CSD : READ_BL_LEN : %d %dbyte\r\n", READ_BL_LEN, (1 << READ_BL_LEN));
  943. /* get C_SIZE 12bit [73:62] */
  944. tmp16 = CSD_buffer[6] & 0x03; /* get [73:72] 0b00000011 */
  945. tmp16 = tmp16 << 8;
  946. tmp16 += CSD_buffer[7]; /* get [71:64] */
  947. tmp16 = tmp16 << 2;
  948. tmp8 = CSD_buffer[8] & 0xC0; /* get [63:62] 0b11000000 */
  949. tmp8 = tmp8 >> 6;
  950. tmp16 = tmp16 + tmp8;
  951. C_SIZE = tmp16; //12 bit
  952. MSD_DEBUG("[info] CSD : C_SIZE : %d\r\n", C_SIZE);
  953. /* get C_SIZE_MULT 3bit [49:47] */
  954. tmp8 = CSD_buffer[9] & 0x03;//0b00000011;
  955. tmp8 = tmp8 << 1;
  956. tmp8 = tmp8 + ((CSD_buffer[10] & 0x80/*0b10000000*/) >> 7);
  957. C_SIZE_MULT = tmp8; // 3 bit
  958. MSD_DEBUG("[info] CSD : C_SIZE_MULT : %d\r\n", C_SIZE_MULT);
  959. /* memory capacity = BLOCKNR * BLOCK_LEN */
  960. /* BLOCKNR = (C_SIZE+1) * MULT */
  961. /* MULT = 2^(C_SIZE_MULT+2) */
  962. /* BLOCK_LEN = 2^READ_BL_LEN */
  963. card_capacity = (1 << READ_BL_LEN) * ((C_SIZE + 1) * (1 << (C_SIZE_MULT + 2)));
  964. msd->geometry.sector_count = card_capacity / msd->geometry.bytes_per_sector;
  965. MSD_DEBUG("[info] card capacity : %d Mbyte\r\n", card_capacity / (1024 * 1024));
  966. }
  967. else if (CSD_STRUCTURE == 1)
  968. {
  969. MSD_DEBUG("[info] CSD Version 2.0\r\n");
  970. /* get TRAN_SPEED 8bit [103:96] */
  971. tmp8 = CSD_buffer[3];
  972. if (tmp8 == 0x32)
  973. {
  974. msd->max_clock = 1000 * 1000 * 10; /* 10Mbit/s. */
  975. }
  976. else if (tmp8 == 0x5A)
  977. {
  978. msd->max_clock = 1000 * 1000 * 50; /* 50Mbit/s. */
  979. }
  980. else if (tmp8 == 0x0B)
  981. {
  982. msd->max_clock = 1000 * 1000 * 100; /* 100Mbit/s. */
  983. /* UHS50 Card sets TRAN_SPEED to 0Bh (100Mbit/sec), */
  984. /* for both SDR50 and DDR50 modes. */
  985. }
  986. else if (tmp8 == 0x2B)
  987. {
  988. msd->max_clock = 1000 * 1000 * 200; /* 200Mbit/s. */
  989. /* UHS104 Card sets TRAN_SPEED to 2Bh (200Mbit/sec). */
  990. }
  991. else
  992. {
  993. msd->max_clock = 1000 * 1000 * 1; /* 1Mbit/s default. */
  994. }
  995. MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dMbit/s.\r\n", tmp8, msd->max_clock / 1000 / 1000);
  996. /* get C_SIZE 22bit [69:48] */
  997. tmp32 = CSD_buffer[7] & 0x3F; /* 0b00111111 */
  998. tmp32 = tmp32 << 8;
  999. tmp32 += CSD_buffer[8];
  1000. tmp32 = tmp32 << 8;
  1001. tmp32 += CSD_buffer[9];
  1002. C_SIZE = tmp32;
  1003. MSD_DEBUG("[info] CSD : C_SIZE : %d\r\n", C_SIZE);
  1004. /* memory capacity = (C_SIZE+1) * 512K byte */
  1005. card_capacity = (C_SIZE + 1) / 2; /* unit : Mbyte */
  1006. msd->geometry.sector_count = (C_SIZE + 1) * 1024; /* 512KB = 1024sector */
  1007. MSD_DEBUG("[info] card capacity : %d.%d Gbyte\r\n", card_capacity / 1024, (card_capacity % 1024) * 100 / 1024);
  1008. MSD_DEBUG("[info] sector_count : %d\r\n", msd->geometry.sector_count);
  1009. }
  1010. else
  1011. {
  1012. MSD_DEBUG("[err] bad CSD Version : %d\r\n", CSD_STRUCTURE);
  1013. result = RT_ERROR;
  1014. goto _exit;
  1015. }
  1016. } /* SD CSD Analyze. */
  1017. } /* Analyze CSD */
  1018. } /* read CSD */
  1019. /* config spi to high speed */
  1020. {
  1021. struct rt_spi_configuration cfg;
  1022. cfg.data_width = 8;
  1023. cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
  1024. cfg.max_hz = msd->max_clock;
  1025. rt_spi_configure(msd->spi_device, &cfg);
  1026. } /* config spi */
  1027. _exit:
  1028. rt_spi_release(msd->spi_device);
  1029. rt_mutex_release(&(msd->spi_device->bus->lock));
  1030. return result;
  1031. }
  1032. static rt_err_t rt_msd_open(rt_device_t dev, rt_uint16_t oflag)
  1033. {
  1034. // struct msd_device * msd = (struct msd_device *)dev;
  1035. return RT_EOK;
  1036. }
  1037. static rt_err_t rt_msd_close(rt_device_t dev)
  1038. {
  1039. // struct msd_device * msd = (struct msd_device *)dev;
  1040. return RT_EOK;
  1041. }
  1042. static rt_size_t rt_msd_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  1043. {
  1044. struct msd_device *msd = (struct msd_device *)dev;
  1045. uint8_t response[MSD_RESPONSE_MAX_LEN];
  1046. rt_err_t result = RT_EOK;
  1047. result = MSD_take_owner(msd->spi_device);
  1048. if (result != RT_EOK)
  1049. {
  1050. goto _exit;
  1051. }
  1052. /* SINGLE_BLOCK? */
  1053. if (size == 1)
  1054. {
  1055. rt_spi_take(msd->spi_device);
  1056. result = _send_cmd(msd->spi_device, READ_SINGLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
  1057. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1058. {
  1059. MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
  1060. size = 0;
  1061. goto _exit;
  1062. }
  1063. result = _read_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector);
  1064. if (result != RT_EOK)
  1065. {
  1066. MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
  1067. size = 0;
  1068. }
  1069. }
  1070. else if (size > 1)
  1071. {
  1072. uint32_t i;
  1073. rt_spi_take(msd->spi_device);
  1074. result = _send_cmd(msd->spi_device, READ_MULTIPLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
  1075. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1076. {
  1077. MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
  1078. size = 0;
  1079. goto _exit;
  1080. }
  1081. for (i = 0; i < size; i++)
  1082. {
  1083. result = _read_block(msd->spi_device,
  1084. (uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
  1085. msd->geometry.bytes_per_sector);
  1086. if (result != RT_EOK)
  1087. {
  1088. MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
  1089. size = i;
  1090. break;
  1091. }
  1092. }
  1093. /* send CMD12 stop transfer */
  1094. result = _send_cmd(msd->spi_device, STOP_TRANSMISSION, 0x00, 0x00, response_r1b, response);
  1095. if (result != RT_EOK)
  1096. {
  1097. MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK, send stop token fail!\r\n");
  1098. }
  1099. } /* READ_MULTIPLE_BLOCK */
  1100. _exit:
  1101. /* release and exit */
  1102. rt_spi_release(msd->spi_device);
  1103. rt_mutex_release(&(msd->spi_device->bus->lock));
  1104. return size;
  1105. }
  1106. static rt_size_t rt_msd_sdhc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  1107. {
  1108. struct msd_device *msd = (struct msd_device *)dev;
  1109. uint8_t response[MSD_RESPONSE_MAX_LEN];
  1110. rt_err_t result = RT_EOK;
  1111. result = MSD_take_owner(msd->spi_device);
  1112. if (result != RT_EOK)
  1113. {
  1114. goto _exit;
  1115. }
  1116. /* SINGLE_BLOCK? */
  1117. if (size == 1)
  1118. {
  1119. rt_spi_take(msd->spi_device);
  1120. result = _send_cmd(msd->spi_device, READ_SINGLE_BLOCK, pos, 0x00, response_r1, response);
  1121. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1122. {
  1123. MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
  1124. size = 0;
  1125. goto _exit;
  1126. }
  1127. result = _read_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector);
  1128. if (result != RT_EOK)
  1129. {
  1130. MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
  1131. size = 0;
  1132. }
  1133. }
  1134. else if (size > 1)
  1135. {
  1136. uint32_t i;
  1137. rt_spi_take(msd->spi_device);
  1138. result = _send_cmd(msd->spi_device, READ_MULTIPLE_BLOCK, pos, 0x00, response_r1, response);
  1139. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1140. {
  1141. MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
  1142. size = 0;
  1143. goto _exit;
  1144. }
  1145. for (i = 0; i < size; i++)
  1146. {
  1147. result = _read_block(msd->spi_device,
  1148. (uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
  1149. msd->geometry.bytes_per_sector);
  1150. if (result != RT_EOK)
  1151. {
  1152. MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
  1153. size = i;
  1154. break;
  1155. }
  1156. }
  1157. /* send CMD12 stop transfer */
  1158. result = _send_cmd(msd->spi_device, STOP_TRANSMISSION, 0x00, 0x00, response_r1b, response);
  1159. if (result != RT_EOK)
  1160. {
  1161. MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK, send stop token fail!\r\n");
  1162. }
  1163. } /* READ_MULTIPLE_BLOCK */
  1164. _exit:
  1165. /* release and exit */
  1166. rt_spi_release(msd->spi_device);
  1167. rt_mutex_release(&(msd->spi_device->bus->lock));
  1168. return size;
  1169. }
  1170. static rt_size_t rt_msd_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  1171. {
  1172. struct msd_device *msd = (struct msd_device *)dev;
  1173. uint8_t response[MSD_RESPONSE_MAX_LEN];
  1174. rt_err_t result;
  1175. result = MSD_take_owner(msd->spi_device);
  1176. if (result != RT_EOK)
  1177. {
  1178. MSD_DEBUG("[err] get SPI owner fail!\r\n");
  1179. goto _exit;
  1180. }
  1181. /* SINGLE_BLOCK? */
  1182. if (size == 1)
  1183. {
  1184. rt_spi_take(msd->spi_device);
  1185. result = _send_cmd(msd->spi_device, WRITE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
  1186. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1187. {
  1188. MSD_DEBUG("[err] CMD WRITE_BLOCK fail!\r\n");
  1189. size = 0;
  1190. goto _exit;
  1191. }
  1192. result = _write_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector, MSD_TOKEN_WRITE_SINGLE_START);
  1193. if (result != RT_EOK)
  1194. {
  1195. MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
  1196. size = 0;
  1197. }
  1198. }
  1199. else if (size > 1)
  1200. {
  1201. struct rt_spi_message message;
  1202. uint32_t i;
  1203. rt_spi_take(msd->spi_device);
  1204. #ifdef MSD_USE_PRE_ERASED
  1205. if (msd->card_type != MSD_CARD_TYPE_MMC)
  1206. {
  1207. /* CMD55 APP_CMD */
  1208. result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
  1209. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1210. {
  1211. MSD_DEBUG("[err] CMD55 APP_CMD fail!\r\n");
  1212. size = 0;
  1213. goto _exit;
  1214. }
  1215. /* ACMD23 Pre-erased */
  1216. result = _send_cmd(msd->spi_device, SET_WR_BLK_ERASE_COUNT, size, 0x00, response_r1, response);
  1217. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1218. {
  1219. MSD_DEBUG("[err] ACMD23 SET_BLOCK_COUNT fail!\r\n");
  1220. size = 0;
  1221. goto _exit;
  1222. }
  1223. }
  1224. #endif
  1225. result = _send_cmd(msd->spi_device, WRITE_MULTIPLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
  1226. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1227. {
  1228. MSD_DEBUG("[err] CMD WRITE_MULTIPLE_BLOCK fail!\r\n");
  1229. size = 0;
  1230. goto _exit;
  1231. }
  1232. /* write all block */
  1233. for (i = 0; i < size; i++)
  1234. {
  1235. result = _write_block(msd->spi_device,
  1236. (const uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
  1237. msd->geometry.bytes_per_sector,
  1238. MSD_TOKEN_WRITE_MULTIPLE_START);
  1239. if (result != RT_EOK)
  1240. {
  1241. MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
  1242. size = i;
  1243. break;
  1244. }
  1245. } /* write all block */
  1246. /* send stop token */
  1247. {
  1248. uint8_t send_buffer[18];
  1249. rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
  1250. send_buffer[sizeof(send_buffer) - 1] = MSD_TOKEN_WRITE_MULTIPLE_STOP;
  1251. /* initial message */
  1252. message.send_buf = send_buffer;
  1253. message.recv_buf = RT_NULL;
  1254. message.length = sizeof(send_buffer);
  1255. message.cs_take = message.cs_release = 0;
  1256. /* transfer message */
  1257. msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
  1258. }
  1259. /* wait ready */
  1260. result = _wait_ready(msd->spi_device);
  1261. if (result != RT_EOK)
  1262. {
  1263. MSD_DEBUG("[warning] wait WRITE_MULTIPLE_BLOCK stop token ready timeout!\r\n");
  1264. }
  1265. } /* size > 1 */
  1266. _exit:
  1267. /* release and exit */
  1268. rt_spi_release(msd->spi_device);
  1269. rt_mutex_release(&(msd->spi_device->bus->lock));
  1270. return size;
  1271. }
  1272. static rt_size_t rt_msd_sdhc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  1273. {
  1274. struct msd_device *msd = (struct msd_device *)dev;
  1275. uint8_t response[MSD_RESPONSE_MAX_LEN];
  1276. rt_err_t result;
  1277. result = MSD_take_owner(msd->spi_device);
  1278. if (result != RT_EOK)
  1279. {
  1280. goto _exit;
  1281. }
  1282. /* SINGLE_BLOCK? */
  1283. if (size == 1)
  1284. {
  1285. rt_spi_take(msd->spi_device);
  1286. result = _send_cmd(msd->spi_device, WRITE_BLOCK, pos, 0x00, response_r1, response);
  1287. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1288. {
  1289. MSD_DEBUG("[err] CMD WRITE_BLOCK fail!\r\n");
  1290. size = 0;
  1291. goto _exit;
  1292. }
  1293. result = _write_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector, MSD_TOKEN_WRITE_SINGLE_START);
  1294. if (result != RT_EOK)
  1295. {
  1296. MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
  1297. size = 0;
  1298. }
  1299. }
  1300. else if (size > 1)
  1301. {
  1302. struct rt_spi_message message;
  1303. uint32_t i;
  1304. rt_spi_take(msd->spi_device);
  1305. #ifdef MSD_USE_PRE_ERASED
  1306. /* CMD55 APP_CMD */
  1307. result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
  1308. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1309. {
  1310. MSD_DEBUG("[err] CMD55 APP_CMD fail!\r\n");
  1311. size = 0;
  1312. goto _exit;
  1313. }
  1314. /* ACMD23 Pre-erased */
  1315. result = _send_cmd(msd->spi_device, SET_WR_BLK_ERASE_COUNT, size, 0x00, response_r1, response);
  1316. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1317. {
  1318. MSD_DEBUG("[err] ACMD23 SET_BLOCK_COUNT fail!\r\n");
  1319. size = 0;
  1320. goto _exit;
  1321. }
  1322. #endif
  1323. result = _send_cmd(msd->spi_device, WRITE_MULTIPLE_BLOCK, pos, 0x00, response_r1, response);
  1324. if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
  1325. {
  1326. MSD_DEBUG("[err] CMD WRITE_MULTIPLE_BLOCK fail!\r\n");
  1327. size = 0;
  1328. goto _exit;
  1329. }
  1330. /* write all block */
  1331. for (i = 0; i < size; i++)
  1332. {
  1333. result = _write_block(msd->spi_device,
  1334. (const uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
  1335. msd->geometry.bytes_per_sector,
  1336. MSD_TOKEN_WRITE_MULTIPLE_START);
  1337. if (result != RT_EOK)
  1338. {
  1339. MSD_DEBUG("[err] write MULTIPLE_BLOCK #%d fail!\r\n", pos);
  1340. size = i;
  1341. break;
  1342. }
  1343. } /* write all block */
  1344. /* send stop token */
  1345. {
  1346. uint8_t send_buffer[18];
  1347. rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
  1348. send_buffer[sizeof(send_buffer) - 1] = MSD_TOKEN_WRITE_MULTIPLE_STOP;
  1349. /* initial message */
  1350. message.send_buf = send_buffer;
  1351. message.recv_buf = RT_NULL;
  1352. message.length = sizeof(send_buffer);
  1353. message.cs_take = message.cs_release = 0;
  1354. /* transfer message */
  1355. msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
  1356. }
  1357. result = _wait_ready(msd->spi_device);
  1358. if (result != RT_EOK)
  1359. {
  1360. MSD_DEBUG("[warning] wait WRITE_MULTIPLE_BLOCK stop token ready timeout!\r\n");
  1361. }
  1362. } /* size > 1 */
  1363. _exit:
  1364. /* release and exit */
  1365. rt_spi_release(msd->spi_device);
  1366. rt_mutex_release(&(msd->spi_device->bus->lock));
  1367. return size;
  1368. }
  1369. static rt_err_t rt_msd_control(rt_device_t dev, int cmd, void *args)
  1370. {
  1371. struct msd_device *msd = (struct msd_device *)dev;
  1372. RT_ASSERT(dev != RT_NULL);
  1373. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  1374. {
  1375. struct rt_device_blk_geometry *geometry;
  1376. geometry = (struct rt_device_blk_geometry *)args;
  1377. if (geometry == RT_NULL) return -RT_ERROR;
  1378. geometry->bytes_per_sector = msd->geometry.bytes_per_sector;
  1379. geometry->block_size = msd->geometry.block_size;
  1380. geometry->sector_count = msd->geometry.sector_count;
  1381. }
  1382. return RT_EOK;
  1383. }
  1384. rt_err_t msd_init(const char *sd_device_name, const char *spi_device_name)
  1385. {
  1386. rt_err_t result = RT_EOK;
  1387. struct rt_spi_device *spi_device;
  1388. spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
  1389. if (spi_device == RT_NULL)
  1390. {
  1391. MSD_DEBUG("spi device %s not found!\r\n", spi_device_name);
  1392. return -RT_ENOSYS;
  1393. }
  1394. rt_memset(&_msd_device, 0, sizeof(_msd_device));
  1395. _msd_device.spi_device = spi_device;
  1396. /* register sdcard device */
  1397. _msd_device.parent.type = RT_Device_Class_Block;
  1398. _msd_device.geometry.bytes_per_sector = 0;
  1399. _msd_device.geometry.sector_count = 0;
  1400. _msd_device.geometry.block_size = 0;
  1401. #ifdef RT_USING_DEVICE_OPS
  1402. _msd_device.parent.ops = &msd_ops;
  1403. #else
  1404. _msd_device.parent.init = rt_msd_init;
  1405. _msd_device.parent.open = rt_msd_open;
  1406. _msd_device.parent.close = rt_msd_close;
  1407. _msd_device.parent.read = RT_NULL;
  1408. _msd_device.parent.write = RT_NULL;
  1409. _msd_device.parent.control = rt_msd_control;
  1410. #endif
  1411. /* no private, no callback */
  1412. _msd_device.parent.user_data = RT_NULL;
  1413. _msd_device.parent.rx_indicate = RT_NULL;
  1414. _msd_device.parent.tx_complete = RT_NULL;
  1415. result = rt_device_register(&_msd_device.parent, sd_device_name,
  1416. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  1417. return result;
  1418. }