drv_sdcard.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * 2021-03-18 Carl the first version
  9. */
  10. #include <rtthread.h>
  11. #ifdef BSP_USING_SDC
  12. #include <dfs_elm.h>
  13. #include <dfs_fs.h>
  14. #include <dfs_file.h>
  15. #include <unistd.h>
  16. #include <stdio.h>
  17. #include <sys/stat.h>
  18. #include <sys/statfs.h>
  19. #include "drv_sdctrl.h"
  20. #define DBG_TAG "app.card"
  21. #define DBG_LVL DBG_INFO
  22. #include <rtdbg.h>
  23. static rt_err_t _sdcard_mount(void)
  24. {
  25. rt_device_t device;
  26. device = rt_device_find("sd0");
  27. rt_kprintf("rt_device_find %x \r\n", device);
  28. if (device == NULL)
  29. {
  30. mmcsd_wait_cd_changed(0);
  31. ft2004_mmcsd_change();
  32. if (mmcsd_wait_cd_changed(rt_tick_from_millisecond(5000)) == -RT_ETIMEOUT)
  33. {
  34. rt_kprintf("timeout \r\n");
  35. return RT_ERROR;
  36. }
  37. device = rt_device_find("sd0");
  38. }
  39. rt_thread_mdelay(1000);
  40. LOG_I("dfs_mount \r\n");
  41. if (device != RT_NULL)
  42. {
  43. if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK)
  44. {
  45. LOG_I("sd card mount to '/'");
  46. }
  47. else
  48. {
  49. LOG_W("sd card mount to '/' failed!");
  50. return RT_ERROR;
  51. }
  52. }
  53. return RT_EOK;
  54. }
  55. static void _sdcard_unmount(void)
  56. {
  57. rt_thread_mdelay(200);
  58. dfs_unmount("/");
  59. LOG_I("Unmount \"/\"");
  60. mmcsd_wait_cd_changed(0);
  61. ft2004_mmcsd_change();
  62. mmcsd_wait_cd_changed(rt_tick_from_millisecond(5000));
  63. LOG_I("Unmount is over \r\n");
  64. }
  65. static void sd_mount(void *parameter)
  66. {
  67. rt_uint8_t state = 0; /* 1. is valid card ,0 is removal */
  68. #ifdef BSP_SDC_IRQ_CARD_REMOVE
  69. rt_uint32_t status;
  70. #endif
  71. while (1)
  72. {
  73. switch (state)
  74. {
  75. case 0:
  76. if (ft2004_card_status() == 1)
  77. {
  78. #ifdef BSP_SDC_IRQ_CARD_REMOVE
  79. ft2004_card_remove_check(0, RT_NULL); /* Clear removal flag bit */
  80. #endif
  81. if (_sdcard_mount() == RT_EOK)
  82. {
  83. state = 1;
  84. }
  85. else
  86. {
  87. /* For the critical case of frequent plug */
  88. rt_kprintf("dfs_unmount \r\n");
  89. _sdcard_unmount();
  90. ft2004_sdctrl_reset();
  91. }
  92. }
  93. else
  94. {
  95. rt_thread_mdelay(100);
  96. }
  97. break;
  98. case 1:
  99. #ifdef BSP_SDC_IRQ_CARD_REMOVE
  100. if (ft2004_card_remove_check(RT_WAITING_FOREVER, &status) == RT_EOK)
  101. {
  102. if (status & SDCTR_CARD_REMOVE_FLG)
  103. {
  104. state = 0;
  105. _sdcard_unmount();
  106. }
  107. }
  108. #else
  109. if (ft2004_card_status() == 0)
  110. {
  111. state = 0;
  112. _sdcard_unmount();
  113. }
  114. #endif
  115. else
  116. {
  117. rt_thread_mdelay(100);
  118. }
  119. break;
  120. default:
  121. state = 0;
  122. break;
  123. }
  124. }
  125. }
  126. int ft2004_sdcard_mount(void)
  127. {
  128. rt_thread_t tid;
  129. tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
  130. 8192, 2, 20);
  131. if (tid != RT_NULL)
  132. {
  133. rt_thread_startup(tid);
  134. }
  135. else
  136. {
  137. LOG_E("create sd_mount thread err!");
  138. }
  139. return RT_EOK;
  140. }
  141. INIT_APP_EXPORT(ft2004_sdcard_mount);
  142. #endif /* BSP_USING_SDCARD */