drv_sdh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-7-7 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #define NU_SDH_HOTPLUG
  14. #define NU_SDH_MOUNT_ON_ROOT
  15. #undef BSP_USING_SDH
  16. #if defined(BSP_USING_SDH)
  17. #include <rtdevice.h>
  18. #include "NuMicro.h"
  19. #include <drv_pdma.h>
  20. #include <string.h>
  21. #include <dfs_fs.h>
  22. #include <dfs_file.h>
  23. #include <unistd.h>
  24. #include <stdio.h>
  25. #include <sys/stat.h>
  26. #include <sys/statfs.h>
  27. /* Private define ---------------------------------------------------------------*/
  28. #if defined(NU_SDH_MOUNT_ON_ROOT)
  29. #ifndef NU_SDH_MOUNTPOINT_SDH0
  30. #define NU_SDH_MOUNTPOINT_SDH0 "/"
  31. #endif
  32. #else
  33. #if !defined(NU_SDH_MOUNTPOINT_ROOT)
  34. #define NU_SDH_MOUNTPOINT_ROOT "/mnt"
  35. #endif
  36. #endif
  37. #if !defined(NU_SDH_MOUNTPOINT_SDH0)
  38. #define NU_SDH_MOUNTPOINT_SDH0 NU_SDH_MOUNTPOINT_ROOT"/sd0"
  39. #endif
  40. #if defined(NU_SDH_USING_PDMA)
  41. #define NU_SDH_MEMCPY nu_pdma_memcpy
  42. #else
  43. #define NU_SDH_MEMCPY memcpy
  44. #endif
  45. enum
  46. {
  47. SDH_START = -1,
  48. #if defined(BSP_USING_SDH0)
  49. SDH0_IDX,
  50. #endif
  51. SDH_CNT
  52. };
  53. #define SDH_BLOCK_SIZE 512ul
  54. #if defined(NU_SDH_HOTPLUG)
  55. #define NU_SDH_TID_STACK_SIZE 1024
  56. #endif
  57. #if defined(NU_SDH_HOTPLUG)
  58. enum
  59. {
  60. NU_SDH_CARD_DETECTED_SD0 = (1 << 0),
  61. NU_SDH_CARD_EVENT_ALL = (NU_SDH_CARD_DETECTED_SD0)
  62. };
  63. #endif
  64. /* Private typedef --------------------------------------------------------------*/
  65. struct nu_sdh
  66. {
  67. struct rt_device dev;
  68. char *name;
  69. #if defined(NU_SDH_HOTPLUG)
  70. char *mounted_point;
  71. #endif
  72. SDH_T *base;
  73. uint32_t is_card_inserted;
  74. SDH_INFO_T *info;
  75. struct rt_semaphore lock;
  76. uint8_t *pbuf;
  77. };
  78. typedef struct nu_sdh *nu_sdh_t;
  79. #if defined(NU_SDH_HOTPLUG)
  80. static struct rt_thread sdh_tid;
  81. static rt_uint8_t sdh_stack[NU_SDH_TID_STACK_SIZE];
  82. #endif
  83. /* Private functions ------------------------------------------------------------*/
  84. static void nu_sdh_isr(nu_sdh_t sdh);
  85. static rt_err_t nu_sdh_init(rt_device_t dev);
  86. static rt_err_t nu_sdh_open(rt_device_t dev, rt_uint16_t oflag);
  87. static rt_err_t nu_sdh_close(rt_device_t dev);
  88. static rt_size_t nu_sdh_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t blk_nb);
  89. static rt_err_t nu_sdh_control(rt_device_t dev, int cmd, void *args);
  90. static int rt_hw_sdh_init(void);
  91. #if defined(NU_SDH_HOTPLUG)
  92. static rt_bool_t nu_sdh_hotplug_is_mounted(const char *mounting_path);
  93. static void sdh_hotplugger(void *param);
  94. static rt_err_t nu_sdh_hotplug_mount(nu_sdh_t sdh);
  95. static rt_err_t nu_sdh_hotplug_unmount(nu_sdh_t sdh);
  96. #endif
  97. /* Public functions -------------------------------------------------------------*/
  98. /* Private variables ------------------------------------------------------------*/
  99. static struct nu_sdh nu_sdh_arr [] =
  100. {
  101. #if defined(BSP_USING_SDH0)
  102. {
  103. .name = "sdh0",
  104. #if defined(NU_SDH_HOTPLUG)
  105. .mounted_point = NU_SDH_MOUNTPOINT_SDH0,
  106. #endif
  107. .base = SDH0,
  108. .info = &SD0,
  109. },
  110. #endif
  111. {0}
  112. }; /* struct nu_sdh nu_sdh_arr [] */
  113. static struct rt_event sdh_event;
  114. static void nu_sdh_isr(nu_sdh_t sdh)
  115. {
  116. SDH_T *sdh_base = sdh->base;
  117. unsigned int volatile isr;
  118. unsigned int volatile ier;
  119. // FMI data abort interrupt
  120. if (sdh_base->GINTSTS & SDH_GINTSTS_DTAIF_Msk)
  121. {
  122. /* ResetAllEngine() */
  123. sdh_base->GCTL |= SDH_GCTL_GCTLRST_Msk;
  124. }
  125. //----- SD interrupt status
  126. isr = sdh_base->INTSTS;
  127. if (isr & SDH_INTSTS_BLKDIF_Msk)
  128. {
  129. // block down
  130. g_u8SDDataReadyFlag = TRUE;
  131. SDH_CLR_INT_FLAG(sdh_base, SDH_INTSTS_BLKDIF_Msk);
  132. }
  133. if (isr & SDH_INTSTS_CDIF_Msk) // card detect
  134. {
  135. #if defined(NU_SDH_HOTPLUG)
  136. if (sdh->base == SDH0)
  137. rt_event_send(&sdh_event, NU_SDH_CARD_DETECTED_SD0);
  138. #endif
  139. /* Clear CDIF interrupt flag */
  140. SDH_CLR_INT_FLAG(sdh_base, SDH_INTSTS_CDIF_Msk);
  141. }
  142. // CRC error interrupt
  143. if (isr & SDH_INTSTS_CRCIF_Msk)
  144. {
  145. if (!(isr & SDH_INTSTS_CRC16_Msk))
  146. {
  147. /* CRC_16 error */
  148. // TODO: handle CRC 16 error
  149. }
  150. else if (!(isr & SDH_INTSTS_CRC7_Msk))
  151. {
  152. if (!g_u8R3Flag)
  153. {
  154. /* CRC_7 error */
  155. // TODO: handle CRC 7 error
  156. }
  157. }
  158. /* Clear CRCIF interrupt flag */
  159. SDH_CLR_INT_FLAG(sdh_base, SDH_INTSTS_CRCIF_Msk);
  160. }
  161. /* Data-in timeout */
  162. if (isr & SDH_INTSTS_DITOIF_Msk)
  163. {
  164. sdh_base->INTSTS |= SDH_INTSTS_DITOIF_Msk;
  165. }
  166. /* Response-in timeout interrupt */
  167. if (isr & SDH_INTSTS_RTOIF_Msk)
  168. {
  169. sdh_base->INTSTS |= SDH_INTSTS_RTOIF_Msk;
  170. }
  171. }
  172. #if defined(BSP_USING_SDH0)
  173. void SDH0_IRQHandler(void)
  174. {
  175. /* enter interrupt */
  176. rt_interrupt_enter();
  177. nu_sdh_isr(&nu_sdh_arr[SDH0_IDX]);
  178. /* leave interrupt */
  179. rt_interrupt_leave();
  180. }
  181. #endif
  182. /* RT-Thread Device Driver Interface */
  183. static rt_err_t nu_sdh_init(rt_device_t dev)
  184. {
  185. return RT_EOK;
  186. }
  187. static rt_err_t nu_sdh_open(rt_device_t dev, rt_uint16_t oflag)
  188. {
  189. nu_sdh_t sdh = (nu_sdh_t)dev;
  190. RT_ASSERT(dev != RT_NULL);
  191. return (SDH_Probe(sdh->base) == 0) ? RT_EOK : -(RT_ERROR);
  192. }
  193. static rt_err_t nu_sdh_close(rt_device_t dev)
  194. {
  195. return RT_EOK;
  196. }
  197. static rt_size_t nu_sdh_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t blk_nb)
  198. {
  199. rt_uint32_t ret = 0;
  200. nu_sdh_t sdh = (nu_sdh_t)dev;
  201. rt_err_t result;
  202. RT_ASSERT(dev != RT_NULL);
  203. RT_ASSERT(buffer != RT_NULL);
  204. result = rt_sem_take(&sdh->lock, RT_WAITING_FOREVER);
  205. RT_ASSERT(result == RT_EOK);
  206. /* Check alignment. */
  207. if (((uint32_t)buffer & 0x03) != 0)
  208. {
  209. /* Non-aligned. */
  210. uint32_t i;
  211. uint8_t *copy_buffer = (uint8_t *)buffer;
  212. sdh->pbuf = rt_malloc(SDH_BLOCK_SIZE);
  213. if (sdh->pbuf == RT_NULL)
  214. goto exit_nu_sdh_read;
  215. for (i = 0; i < blk_nb; i++)
  216. {
  217. /* Read to temp buffer from specified sector. */
  218. ret = SDH_Read(sdh->base, &sdh->pbuf[0], pos, 1);
  219. if (ret != Successful)
  220. goto exit_nu_sdh_read;
  221. /* Move to user's buffer */
  222. NU_SDH_MEMCPY((void *)copy_buffer, (void *)&sdh->pbuf[0], SDH_BLOCK_SIZE);
  223. pos ++;
  224. copy_buffer += SDH_BLOCK_SIZE;
  225. }
  226. }
  227. else
  228. {
  229. /* Read to user's buffer from specified sector. */
  230. ret = SDH_Read(sdh->base, (uint8_t *)buffer, pos, blk_nb);
  231. }
  232. exit_nu_sdh_read:
  233. if (sdh->pbuf)
  234. {
  235. rt_free(sdh->pbuf);
  236. sdh->pbuf = RT_NULL;
  237. }
  238. result = rt_sem_release(&sdh->lock);
  239. RT_ASSERT(result == RT_EOK);
  240. if (ret == Successful)
  241. return blk_nb;
  242. rt_kprintf("Read failed: %d, buffer 0x%08x\n", ret, buffer);
  243. rt_set_errno(-RT_ENOSYS);
  244. return 0;
  245. }
  246. static rt_size_t nu_sdh_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t blk_nb)
  247. {
  248. rt_uint32_t ret = 0;
  249. nu_sdh_t sdh = (nu_sdh_t)dev;
  250. rt_err_t result;
  251. RT_ASSERT(dev != RT_NULL);
  252. RT_ASSERT(buffer != RT_NULL);
  253. result = rt_sem_take(&sdh->lock, RT_WAITING_FOREVER);
  254. RT_ASSERT(result == RT_EOK);
  255. /* Check alignment. */
  256. if (((uint32_t)buffer & 0x03) != 0)
  257. {
  258. /* Non-aligned. */
  259. uint32_t i;
  260. uint8_t *copy_buffer = (uint8_t *)buffer;
  261. sdh->pbuf = rt_malloc(SDH_BLOCK_SIZE);
  262. if (sdh->pbuf == RT_NULL)
  263. goto exit_nu_sdh_write;
  264. for (i = 0; i < blk_nb; i++)
  265. {
  266. NU_SDH_MEMCPY((void *)&sdh->pbuf[0], copy_buffer, SDH_BLOCK_SIZE);
  267. ret = SDH_Write(sdh->base, (uint8_t *)&sdh->pbuf[0], pos, 1);
  268. if (ret != Successful)
  269. goto exit_nu_sdh_write;
  270. pos++;
  271. copy_buffer += SDH_BLOCK_SIZE;
  272. }
  273. }
  274. else
  275. {
  276. /* Write to device directly. */
  277. ret = SDH_Write(sdh->base, (uint8_t *)buffer, pos, blk_nb);
  278. }
  279. exit_nu_sdh_write:
  280. if (sdh->pbuf)
  281. {
  282. rt_free(sdh->pbuf);
  283. sdh->pbuf = RT_NULL;
  284. }
  285. result = rt_sem_release(&sdh->lock);
  286. RT_ASSERT(result == RT_EOK);
  287. if (ret == Successful) return blk_nb;
  288. rt_kprintf("write failed: %d, buffer 0x%08x\n", ret, buffer);
  289. rt_set_errno(-RT_ENOSYS);
  290. return 0;
  291. }
  292. static rt_err_t nu_sdh_control(rt_device_t dev, int cmd, void *args)
  293. {
  294. nu_sdh_t sdh = (nu_sdh_t)dev;
  295. RT_ASSERT(dev != RT_NULL);
  296. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  297. {
  298. SDH_INFO_T *sdh_info = sdh->info;
  299. struct rt_device_blk_geometry *geometry;
  300. geometry = (struct rt_device_blk_geometry *)args;
  301. if (geometry == RT_NULL) return -RT_ERROR;
  302. geometry->bytes_per_sector = sdh_info->sectorSize;
  303. geometry->block_size = sdh_info->sectorSize;
  304. geometry->sector_count = sdh_info->totalSectorN;
  305. }
  306. return RT_EOK;
  307. }
  308. static int rt_hw_sdh_init(void)
  309. {
  310. int i;
  311. rt_err_t ret = RT_EOK;
  312. rt_uint32_t flags = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE;
  313. rt_event_init(&sdh_event, "sdh_event", RT_IPC_FLAG_FIFO);
  314. for (i = (SDH_START + 1); i < SDH_CNT; i++)
  315. {
  316. /* Register sdcard device */
  317. nu_sdh_arr[i].dev.type = RT_Device_Class_Block;
  318. nu_sdh_arr[i].dev.init = nu_sdh_init;
  319. nu_sdh_arr[i].dev.open = nu_sdh_open;
  320. nu_sdh_arr[i].dev.close = nu_sdh_close;
  321. nu_sdh_arr[i].dev.read = nu_sdh_read;
  322. nu_sdh_arr[i].dev.write = nu_sdh_write;
  323. nu_sdh_arr[i].dev.control = nu_sdh_control;
  324. /* Private */
  325. nu_sdh_arr[i].dev.user_data = (void *)&nu_sdh_arr[i];
  326. ret = rt_sem_init(&nu_sdh_arr[i].lock, "sdhlock", 1, RT_IPC_FLAG_FIFO);
  327. RT_ASSERT(ret == RT_EOK);
  328. SDH_Open(nu_sdh_arr[i].base, CardDetect_From_GPIO);
  329. /* Enable NVIC IRQ */
  330. if (nu_sdh_arr[i].base == SDH0)
  331. NVIC_EnableIRQ(SDH0_IRQn);
  332. nu_sdh_arr[i].pbuf = RT_NULL;
  333. ret = rt_device_register(&nu_sdh_arr[i].dev, nu_sdh_arr[i].name, flags);
  334. RT_ASSERT(ret == RT_EOK);
  335. }
  336. return (int)ret;
  337. }
  338. INIT_BOARD_EXPORT(rt_hw_sdh_init);
  339. #if defined(NU_SDH_HOTPLUG)
  340. static rt_bool_t nu_sdh_hotplug_is_mounted(const char *mounting_path)
  341. {
  342. rt_bool_t ret = RT_FALSE;
  343. struct dfs_filesystem *psFS = dfs_filesystem_lookup(mounting_path);
  344. if (psFS == RT_NULL)
  345. {
  346. goto exit_nu_sdh_hotplug_is_mounted;
  347. }
  348. else if (!rt_memcmp(psFS->path, mounting_path, rt_strlen(mounting_path)))
  349. {
  350. ret = RT_TRUE;
  351. }
  352. else
  353. {
  354. ret = RT_FALSE;
  355. }
  356. exit_nu_sdh_hotplug_is_mounted:
  357. return ret;
  358. }
  359. static rt_err_t nu_sdh_hotplug_mount(nu_sdh_t sdh)
  360. {
  361. rt_err_t ret = RT_ERROR;
  362. DIR *t;
  363. if (nu_sdh_hotplug_is_mounted(sdh->mounted_point) == RT_TRUE)
  364. {
  365. ret = RT_EOK;
  366. goto exit_nu_sdh_hotplug_mount;
  367. }
  368. /* Check the SD folder path is valid. */
  369. if ((t = opendir(sdh->mounted_point)) != RT_NULL)
  370. {
  371. closedir(t);
  372. }
  373. #if !defined(NU_SDH_MOUNT_ON_ROOT)
  374. else
  375. {
  376. /* Check the ROOT path is valid. */
  377. if ((t = opendir(NU_SDH_MOUNTPOINT_ROOT)) != RT_NULL)
  378. {
  379. closedir(t);
  380. }
  381. else if ((ret = mkdir(NU_SDH_MOUNTPOINT_ROOT, 0)) != RT_EOK)
  382. {
  383. rt_kprintf("Failed to mkdir %s\n", NU_SDH_MOUNTPOINT_ROOT);
  384. goto exit_nu_sdh_hotplug_mount;
  385. }
  386. if ((ret = mkdir(sdh->mounted_point, 0)) != RT_EOK)
  387. {
  388. rt_kprintf("Failed to mkdir %s\n", sdh->mounted_point);
  389. goto exit_nu_sdh_hotplug_mount;
  390. }
  391. } //else
  392. #endif
  393. if ((ret = dfs_mount(sdh->name, sdh->mounted_point, "elm", 0, 0)) == 0)
  394. {
  395. rt_kprintf("Mounted %s on %s\n", sdh->name, sdh->mounted_point);
  396. }
  397. else
  398. {
  399. rt_kprintf("Failed to mount %s on %s\n", sdh->name, sdh->mounted_point);
  400. ret = RT_ERROR;
  401. }
  402. exit_nu_sdh_hotplug_mount:
  403. return -(ret);
  404. }
  405. static rt_err_t nu_sdh_hotplug_unmount(nu_sdh_t sdh)
  406. {
  407. rt_err_t ret = RT_ERROR;
  408. if (nu_sdh_hotplug_is_mounted(sdh->mounted_point) == RT_FALSE)
  409. {
  410. ret = RT_EOK;
  411. goto exit_nu_sdh_hotplug_unmount;
  412. }
  413. ret = dfs_unmount(sdh->mounted_point);
  414. if (ret != RT_EOK)
  415. {
  416. rt_kprintf("Failed to unmount %s.\n", sdh->mounted_point);
  417. }
  418. else
  419. {
  420. rt_kprintf("Succeed to unmount %s.\n", sdh->mounted_point);
  421. ret = RT_EOK;
  422. }
  423. exit_nu_sdh_hotplug_unmount:
  424. return -(ret);
  425. }
  426. static void nu_card_detector(nu_sdh_t sdh)
  427. {
  428. SDH_T *sdh_base = sdh->base;
  429. unsigned int volatile isr = sdh_base->INTSTS;
  430. if (isr & SDH_INTSTS_CDSTS_Msk)
  431. {
  432. /* Card removed */
  433. sdh->info->IsCardInsert = FALSE; // SDISR_CD_Card = 1 means card remove for GPIO mode
  434. rt_memset((void *)sdh->info, 0, sizeof(SDH_INFO_T));
  435. nu_sdh_hotplug_unmount(sdh);
  436. }
  437. else
  438. {
  439. SDH_Open(sdh_base, CardDetect_From_GPIO);
  440. if (!SDH_Probe(sdh_base))
  441. {
  442. /* Card inserted */
  443. nu_sdh_hotplug_mount(sdh);
  444. }
  445. }
  446. }
  447. static void sdh_hotplugger(void *param)
  448. {
  449. rt_uint32_t e;
  450. int i;
  451. for (i = (SDH_START + 1); i < SDH_CNT; i++)
  452. {
  453. if (SDH_IS_CARD_PRESENT(nu_sdh_arr[i].base))
  454. {
  455. nu_sdh_hotplug_mount(&nu_sdh_arr[i]);
  456. }
  457. }
  458. while (1)
  459. {
  460. if (rt_event_recv(&sdh_event, (NU_SDH_CARD_EVENT_ALL),
  461. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  462. RT_WAITING_FOREVER, &e) == RT_EOK)
  463. {
  464. /* Debounce */
  465. rt_thread_delay(200);
  466. switch (e)
  467. {
  468. #if defined(BSP_USING_SDH0)
  469. case NU_SDH_CARD_DETECTED_SD0:
  470. nu_card_detector(&nu_sdh_arr[SDH0_IDX]);
  471. break;
  472. #endif
  473. default:
  474. break;
  475. } //switch(e)
  476. } //if
  477. } /* while(1) */
  478. }
  479. int mnt_init_sdcard_hotplug(void)
  480. {
  481. rt_err_t result;
  482. result = rt_thread_init(&sdh_tid, "hotplug", sdh_hotplugger, NULL, sdh_stack, sizeof(sdh_stack), RT_THREAD_PRIORITY_MAX - 2, 10);
  483. RT_ASSERT(result == RT_EOK);
  484. rt_thread_startup(&sdh_tid);
  485. return 0;
  486. }
  487. INIT_ENV_EXPORT(mnt_init_sdcard_hotplug);
  488. #endif
  489. #endif //#if defined(BSP_USING_SDH)