drv_sdcard.c 3.3 KB

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