mnt.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <rtthread.h>
  2. #include "hal_data.h"
  3. #ifdef BSP_USING_FS
  4. #include <dfs_fs.h>
  5. #define DBG_TAG "app.filesystem"
  6. #define DBG_LVL DBG_INFO
  7. #include <rtdbg.h>
  8. #ifdef BSP_USING_SDCARD_FS
  9. #include <drv_sdhi.h>
  10. /* SD Card hot plug detection pin */
  11. #define SD_CHECK_PIN "p405"
  12. static rt_base_t sd_check_pin = 0;
  13. static void _sdcard_mount(void)
  14. {
  15. rt_device_t device;
  16. device = rt_device_find("sd");
  17. rt_kprintf("rt_device_find %x \r\n", device);
  18. if (device == NULL)
  19. {
  20. mmcsd_wait_cd_changed(0);
  21. sdcard_change();
  22. mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
  23. device = rt_device_find("sd");
  24. }
  25. if (device != RT_NULL)
  26. {
  27. if (dfs_mount("sd", "/", "elm", 0, 0) == RT_EOK)
  28. {
  29. LOG_I("sd card mount to '/'");
  30. }
  31. else
  32. {
  33. LOG_W("sd card mount to '/' failed!");
  34. }
  35. }
  36. }
  37. static void _sdcard_unmount(void)
  38. {
  39. rt_thread_mdelay(200);
  40. dfs_unmount("/sdcard");
  41. LOG_I("Unmount \"/sdcard\"");
  42. mmcsd_wait_cd_changed(0);
  43. sdcard_change();
  44. mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
  45. }
  46. static void sd_auto_mount(void *parameter)
  47. {
  48. rt_uint8_t re_sd_check_pin = 1;
  49. rt_thread_mdelay(20);
  50. if (!rt_pin_read(sd_check_pin))
  51. {
  52. _sdcard_mount();
  53. }
  54. while (1)
  55. {
  56. rt_thread_mdelay(200);
  57. if (re_sd_check_pin && (re_sd_check_pin = rt_pin_read(sd_check_pin)) == 0)
  58. {
  59. _sdcard_mount();
  60. }
  61. if (!re_sd_check_pin && (re_sd_check_pin = rt_pin_read(sd_check_pin)) != 0)
  62. {
  63. _sdcard_unmount();
  64. }
  65. }
  66. }
  67. static void sd_mount(void)
  68. {
  69. rt_thread_t tid;
  70. sd_check_pin = rt_pin_get(SD_CHECK_PIN);
  71. rt_pin_mode(sd_check_pin, PIN_MODE_INPUT_PULLUP);
  72. tid = rt_thread_create("sd_mount", sd_auto_mount, RT_NULL,
  73. 2048, RT_THREAD_PRIORITY_MAX - 2, 20);
  74. if (tid != RT_NULL)
  75. {
  76. rt_thread_startup(tid);
  77. }
  78. else
  79. {
  80. LOG_E("create sd_mount thread err!");
  81. return;
  82. }
  83. }
  84. #else
  85. #include <spi_msd.h>
  86. #include "drv_sci_spi.h"
  87. int sd_mount(void)
  88. {
  89. uint32_t cs_pin = BSP_IO_PORT_06_PIN_11;
  90. rt_hw_sci_spi_device_attach("scpi7", "scpi70", cs_pin);
  91. msd_init("sd0", "scpi70");
  92. if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
  93. {
  94. LOG_I("Mount \"/dev/sd0\" on \"/\"\n");
  95. }
  96. else
  97. {
  98. LOG_W("sd card mount to '/' failed!");
  99. }
  100. return 0;
  101. }
  102. #endif /* BSP_USING_SDCARD_FS */
  103. int mount_init(void)
  104. {
  105. sd_mount();
  106. return RT_EOK;
  107. }
  108. INIT_ENV_EXPORT(mount_init);
  109. #endif