drv_sd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. * 2017-08-08 Yang the first version
  9. */
  10. #include <string.h>
  11. #include <board.h>
  12. #include "drv_sd.h"
  13. static struct mci_device *_mci_device;
  14. static uint8_t sdio_buffer[1024];
  15. static rt_err_t rt_mci_init(rt_device_t dev)
  16. {
  17. rt_err_t result = RT_EOK;
  18. return result;
  19. }
  20. static rt_err_t rt_mci_open(rt_device_t dev, rt_uint16_t oflag)
  21. {
  22. return RT_EOK;
  23. }
  24. static rt_err_t rt_mci_close(rt_device_t dev)
  25. {
  26. return RT_EOK;
  27. }
  28. static rt_ssize_t rt_mci_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  29. {
  30. rt_uint8_t status = kStatus_Success;
  31. struct mci_device *mci = (struct mci_device *)dev;
  32. rt_mutex_take(&mci->lock, RT_WAITING_FOREVER);
  33. {
  34. /* non-aligned. */
  35. uint32_t i;
  36. rt_size_t sector_adr;
  37. uint8_t* copy_buffer;
  38. sector_adr = pos;
  39. copy_buffer = (uint8_t*)buffer;
  40. for(i=0; i<size; i++)
  41. {
  42. status=SD_ReadBlocks(&mci->card, sdio_buffer, sector_adr, 1);
  43. memcpy(copy_buffer, sdio_buffer, mci->card.blockSize);
  44. sector_adr ++;
  45. copy_buffer += mci->card.blockSize;
  46. }
  47. }
  48. rt_mutex_release(&_mci_device->lock);
  49. if (status == kStatus_Success) return size;
  50. return 0;
  51. }
  52. static rt_ssize_t rt_mci_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  53. {
  54. rt_uint8_t status = kStatus_Success;
  55. struct mci_device *mci = (struct mci_device *)dev;
  56. rt_mutex_take(&mci->lock, RT_WAITING_FOREVER);
  57. {
  58. /* non-aligned. */
  59. uint32_t i;
  60. rt_size_t sector_adr;
  61. uint8_t* copy_buffer;
  62. sector_adr = pos;
  63. copy_buffer = (uint8_t*)buffer;
  64. for(i = 0; i < size; i++)
  65. {
  66. memcpy(sdio_buffer, copy_buffer, mci->card.blockSize);
  67. status = SD_WriteBlocks(&mci->card, sdio_buffer, sector_adr, 1);
  68. sector_adr ++;
  69. copy_buffer += mci->card.blockSize;
  70. }
  71. }
  72. /* release and exit */
  73. rt_mutex_release(&_mci_device->lock);
  74. if (status == kStatus_Success) return size;
  75. return 0;
  76. }
  77. static rt_err_t rt_mci_control(rt_device_t dev, int cmd, void *args)
  78. {
  79. struct mci_device *mci = (struct mci_device *)dev;
  80. RT_ASSERT(dev != RT_NULL);
  81. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  82. {
  83. struct rt_device_blk_geometry *geometry;
  84. geometry = (struct rt_device_blk_geometry *)args;
  85. if (geometry == RT_NULL) return -RT_ERROR;
  86. geometry->bytes_per_sector = mci->card.blockSize;
  87. geometry->block_size = mci->card.csd.eraseSectorSize;
  88. geometry->sector_count = mci->card.blockCount;
  89. }
  90. return RT_EOK;
  91. }
  92. void sdio_init_pins(void)
  93. {
  94. const uint32_t port2_pin10_config = (
  95. IOCON_PIO_FUNC2 | /* Pin is configured as SD_CARD_DET_N */
  96. IOCON_PIO_MODE_INACT | /* No addition pin function */
  97. IOCON_PIO_INV_DI | /* Input function is not inverted */
  98. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  99. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  100. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  101. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  102. );
  103. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN10_IDX, port2_pin10_config); /* PORT2 PIN10 (coords: P1) is configured as SD_CARD_DET_N */
  104. const uint32_t port2_pin3_config = (
  105. IOCON_PIO_FUNC2 | /* Pin is configured as SD_CLK */
  106. IOCON_PIO_MODE_INACT | /* No addition pin function */
  107. IOCON_PIO_INV_DI | /* Input function is not inverted */
  108. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  109. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  110. IOCON_PIO_SLEW_FAST | /* Fast mode, slew rate control is disabled */
  111. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  112. );
  113. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN3_IDX, port2_pin3_config); /* PORT2 PIN3 (coords: B1) is configured as SD_CLK */
  114. const uint32_t port2_pin4_config = (
  115. IOCON_PIO_FUNC2 | /* Pin is configured as SD_CMD */
  116. IOCON_PIO_MODE_INACT | /* No addition pin function */
  117. IOCON_PIO_INV_DI | /* Input function is not inverted */
  118. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  119. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  120. IOCON_PIO_SLEW_FAST | /* Fast mode, slew rate control is disabled */
  121. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  122. );
  123. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN4_IDX, port2_pin4_config); /* PORT2 PIN4 (coords: D3) is configured as SD_CMD */
  124. const uint32_t port2_pin5_config = (
  125. IOCON_PIO_FUNC2 | /* Pin is configured as SD_POW_EN */
  126. IOCON_PIO_MODE_INACT | /* No addition pin function */
  127. IOCON_PIO_INV_DI | /* Input function is not inverted */
  128. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  129. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  130. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  131. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  132. );
  133. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN5_IDX, port2_pin5_config); /* PORT2 PIN5 (coords: C1) is configured as SD_POW_EN */
  134. const uint32_t port2_pin6_config = (
  135. IOCON_PIO_FUNC2 | /* Pin is configured as SD_D(0) */
  136. IOCON_PIO_MODE_INACT | /* No addition pin function */
  137. IOCON_PIO_INV_DI | /* Input function is not inverted */
  138. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  139. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  140. IOCON_PIO_SLEW_FAST | /* Fast mode, slew rate control is disabled */
  141. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  142. );
  143. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN6_IDX, port2_pin6_config); /* PORT2 PIN6 (coords: F3) is configured as SD_D(0) */
  144. const uint32_t port2_pin7_config = (
  145. IOCON_PIO_FUNC2 | /* Pin is configured as SD_D(1) */
  146. IOCON_PIO_MODE_INACT | /* No addition pin function */
  147. IOCON_PIO_INV_DI | /* Input function is not inverted */
  148. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  149. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  150. IOCON_PIO_SLEW_FAST | /* Fast mode, slew rate control is disabled */
  151. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  152. );
  153. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN7_IDX, port2_pin7_config); /* PORT2 PIN7 (coords: J2) is configured as SD_D(1) */
  154. const uint32_t port2_pin8_config = (
  155. IOCON_PIO_FUNC2 | /* Pin is configured as SD_D(2) */
  156. IOCON_PIO_MODE_INACT | /* No addition pin function */
  157. IOCON_PIO_INV_DI | /* Input function is not inverted */
  158. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  159. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  160. IOCON_PIO_SLEW_FAST | /* Fast mode, slew rate control is disabled */
  161. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  162. );
  163. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN8_IDX, port2_pin8_config); /* PORT2 PIN8 (coords: F4) is configured as SD_D(2) */
  164. const uint32_t port2_pin9_config = (
  165. IOCON_PIO_FUNC2 | /* Pin is configured as SD_D(3) */
  166. IOCON_PIO_MODE_INACT | /* No addition pin function */
  167. IOCON_PIO_INV_DI | /* Input function is not inverted */
  168. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  169. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  170. IOCON_PIO_SLEW_FAST | /* Fast mode, slew rate control is disabled */
  171. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  172. );
  173. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN9_IDX, port2_pin9_config); /* PORT2 PIN9 (coords: K2) is configured as SD_D(3) */
  174. const uint32_t port3_pin15_config = (
  175. IOCON_PIO_FUNC2 | /* Pin is configured as SD_WR_PRT */
  176. IOCON_PIO_MODE_INACT | /* No addition pin function */
  177. IOCON_PIO_INV_DI | /* Input function is not inverted */
  178. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  179. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  180. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  181. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  182. );
  183. IOCON_PinMuxSet(IOCON, PORT3_IDX, PIN15_IDX, port3_pin15_config); /* PORT3 PIN15 (coords: D2) is configured as SD_WR_PRT */
  184. }
  185. sd_card_t g_sd;
  186. /*! @brief Data written to the card */
  187. uint8_t g_dataWrite[FSL_SDMMC_DEFAULT_BLOCK_SIZE * 5U];
  188. /*! @brief Data read from the card */
  189. uint8_t g_dataRead[FSL_SDMMC_DEFAULT_BLOCK_SIZE * 5U];
  190. rt_err_t mci_hw_init(const char *device_name)
  191. #if 0
  192. {
  193. sd_card_t *card = &g_sd;
  194. bool isReadOnly;
  195. bool failedFlag = false;
  196. char ch = '0';
  197. /* attach main clock to SDIF */
  198. CLOCK_AttachClk(kMCLK_to_SDIO_CLK);
  199. /* need call this function to clear the halt bit in clock divider register */
  200. CLOCK_SetClkDiv(kCLOCK_DivSdioClk, 1U, true);
  201. sdio_init_pins();
  202. card->host.base = SDIF;
  203. card->host.sourceClock_Hz = CLOCK_GetFreq(kCLOCK_SDio);
  204. /* Init card. */
  205. if (SD_Init(card))
  206. {
  207. rt_kprintf("\r\nSD card init failed.\r\n");
  208. return -1;
  209. }
  210. rt_kprintf("\r\nRead/Write/Erase the card continuously until encounter error......\r\n");
  211. /* Check if card is readonly. */
  212. isReadOnly = SD_CheckReadOnly(card);
  213. if (isReadOnly)
  214. {
  215. //while (true)
  216. {
  217. /*if (failedFlag || (ch == 'q'))
  218. {
  219. break;
  220. }*/
  221. rt_kprintf("\r\nRead one data block......\r\n");
  222. if (kStatus_Success != SD_ReadBlocks(card, g_dataRead, 2U, 1U))
  223. {
  224. rt_kprintf("Read one data block failed.\r\n");
  225. failedFlag = true;
  226. //continue;
  227. }
  228. rt_kprintf("Read multiple data blocks......\r\n");
  229. if (kStatus_Success != SD_ReadBlocks(card, g_dataRead, 2U, 5U))
  230. {
  231. rt_kprintf("Read multiple data blocks failed.\r\n");
  232. failedFlag = true;
  233. //continue;
  234. }
  235. rt_kprintf(
  236. "\r\nInput 'q' to quit read process.\
  237. \r\nInput other char to read data blocks again.\r\n");
  238. //ch = GETCHAR();
  239. //PUTCHAR(ch);
  240. }
  241. }
  242. else
  243. {
  244. memset(g_dataWrite, 0x67U, sizeof(g_dataWrite));
  245. //while (true)
  246. {
  247. /*if (failedFlag || (ch == 'q'))
  248. {
  249. break;
  250. }*/
  251. rt_kprintf("\r\nWrite/read one data block......\r\n");
  252. if (kStatus_Success != SD_WriteBlocks(card, g_dataWrite, 2U, 1U))
  253. {
  254. rt_kprintf("Write one data block failed.\r\n");
  255. failedFlag = true;
  256. //continue;
  257. }
  258. memset(g_dataRead, 0U, sizeof(g_dataRead));
  259. if (kStatus_Success != SD_ReadBlocks(card, g_dataRead, 2U, 1U))
  260. {
  261. rt_kprintf("Read one data block failed.\r\n");
  262. failedFlag = true;
  263. //continue;
  264. }
  265. rt_kprintf("Compare the read/write content......\r\n");
  266. if (memcmp(g_dataRead, g_dataWrite, FSL_SDMMC_DEFAULT_BLOCK_SIZE))
  267. {
  268. rt_kprintf("The read/write content isn't consistent.\r\n");
  269. failedFlag = true;
  270. //continue;
  271. }
  272. rt_kprintf("The read/write content is consistent.\r\n");
  273. rt_kprintf("Write/read multiple data blocks......\r\n");
  274. if (kStatus_Success != SD_WriteBlocks(card, g_dataWrite, 2U, 5U))
  275. {
  276. rt_kprintf("Write multiple data blocks failed.\r\n");
  277. failedFlag = true;
  278. //continue;
  279. }
  280. memset(g_dataRead, 0U, sizeof(g_dataRead));
  281. if (kStatus_Success != SD_ReadBlocks(card, g_dataRead, 2U, 5U))
  282. {
  283. rt_kprintf("Read multiple data blocks failed.\r\n");
  284. failedFlag = true;
  285. //continue;
  286. }
  287. rt_kprintf("Compare the read/write content......\r\n");
  288. if (memcmp(g_dataRead, g_dataWrite, FSL_SDMMC_DEFAULT_BLOCK_SIZE))
  289. {
  290. rt_kprintf("The read/write content isn't consistent.\r\n");
  291. failedFlag = true;
  292. //continue;
  293. }
  294. rt_kprintf("The read/write content is consistent.\r\n");
  295. rt_kprintf("Erase multiple data blocks......\r\n");
  296. if (kStatus_Success != SD_EraseBlocks(card, 2U, 5U))
  297. {
  298. rt_kprintf("Erase multiple data blocks failed.\r\n");
  299. failedFlag = true;
  300. //continue;
  301. }
  302. rt_kprintf(
  303. "\r\nInput 'q' to quit read/write/erase process.\
  304. \r\nInput other char to read/write/erase data blocks again.\r\n");
  305. //ch = GETCHAR();
  306. //PUTCHAR(ch);
  307. }
  308. }
  309. rt_kprintf("\r\nThe example will not read/write data blocks again.\r\n");
  310. SD_Deinit(card);
  311. }
  312. #else
  313. {
  314. _mci_device = (struct mci_device *)rt_malloc(sizeof(struct mci_device));
  315. if (_mci_device == RT_NULL)
  316. {
  317. rt_kprintf("mci_hw_init _mci_device rt_malloc failed!\n");
  318. return -RT_ERROR;
  319. }
  320. rt_memset(_mci_device, 0, sizeof(struct mci_device));
  321. /* attach main clock to SDIF */
  322. CLOCK_AttachClk(kMCLK_to_SDIO_CLK);
  323. /* need call this function to clear the halt bit in clock divider register */
  324. CLOCK_SetClkDiv(kCLOCK_DivSdioClk, 1U, true);
  325. sdio_init_pins();
  326. /* Save host information. */
  327. _mci_device->card.host.base = SDIF;
  328. _mci_device->card.host.sourceClock_Hz = CLOCK_GetFreq(kCLOCK_SDio);
  329. if (kStatus_Success != SD_Init(&_mci_device->card))
  330. {
  331. SD_Deinit(&_mci_device->card);
  332. memset(&_mci_device->card, 0U, sizeof(_mci_device->card));
  333. rt_kprintf("SD_Init failed!\n");
  334. return -RT_ERROR;
  335. }
  336. /*
  337. follow the page: https://community.nxp.com/thread/454769
  338. The issue concerns sdmmc library bug (I finally solved) in SD_Init() in the file sdmmc/src/fsl_sd.c:SD_SelectBusTiming()
  339. calls SD_SwitchFunction() which sets block size to 64bytes (512bits).Therefore SD_SetBlockSize(card, FSL_SDMMC_DEFAULT_BLOCK_SIZE)
  340. should be called again before SD_Init() exits.
  341. */
  342. if (kStatus_Success != SDMMC_SetBlockSize(_mci_device->card.host.base, _mci_device->card.host.transfer, FSL_SDMMC_DEFAULT_BLOCK_SIZE))
  343. {
  344. SD_Deinit(&_mci_device->card);
  345. memset(&_mci_device->card, 0U, sizeof(_mci_device->card));
  346. rt_kprintf("SD_Init failed!\n");
  347. return -RT_ERROR;
  348. }
  349. /* initialize mutex lock */
  350. rt_mutex_init(&_mci_device->lock, device_name, RT_IPC_FLAG_PRIO);
  351. /* create finish event */
  352. _mci_device->finish_event = rt_event_create(device_name, RT_IPC_FLAG_FIFO);
  353. /* register sdcard device */
  354. _mci_device->parent.type = RT_Device_Class_Block;
  355. _mci_device->geometry.bytes_per_sector = 0;
  356. _mci_device->geometry.sector_count = 0;
  357. _mci_device->geometry.block_size = 0;
  358. _mci_device->parent.init = rt_mci_init;
  359. _mci_device->parent.open = rt_mci_open;
  360. _mci_device->parent.close = rt_mci_close;
  361. _mci_device->parent.read = rt_mci_read;
  362. _mci_device->parent.write = rt_mci_write;
  363. _mci_device->parent.control = rt_mci_control;
  364. /* no private, no callback */
  365. _mci_device->parent.user_data = RT_NULL;
  366. _mci_device->parent.rx_indicate = RT_NULL;
  367. _mci_device->parent.tx_complete = RT_NULL;
  368. rt_device_register(&_mci_device->parent, device_name,
  369. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE );
  370. return RT_EOK;
  371. }
  372. #endif