mnt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * COPYRIGHT (C) 2012-2022, Shanghai Real-Thread Technology Co., Ltd
  3. * All rights reserved.
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2018-02-08 RT-Thread the first version
  7. */
  8. #include <rtthread.h>
  9. #include <rtdevice.h>
  10. #include <unistd.h>
  11. #include "drv_pin.h"
  12. #ifdef BSP_USING_SDMMC
  13. #include "drv_sdmmc.h"
  14. #endif
  15. #ifdef RT_USING_FAL
  16. #include "fal.h"
  17. #endif
  18. #ifdef RT_USING_DFS
  19. #include <dfs_fs.h>
  20. #define SD_CHECK_PIN GET_PIN(GPIO_PORT_G, GPIO_PIN_3)
  21. int sd_check_thread_entry(void *p)
  22. {
  23. rt_uint8_t old_sd_check = 0;
  24. /* 挂载sd0分区 */
  25. if (RT_NULL != rt_device_find("sd0p0"))
  26. {
  27. if (dfs_mount("sd0p0", "/", "elm", 0, 0) == 0)
  28. {
  29. rt_kprintf("Mount \"sd0p0\" on \"/\" success\n");
  30. }
  31. else
  32. {
  33. rt_kprintf("Mount \"sd0p0\" on \"/\" fail\n");
  34. return -1;
  35. }
  36. /* 挂载sd1分区 */
  37. if (RT_NULL != rt_device_find("sd0p1"))
  38. {
  39. if (dfs_mount("sd0p1", "/", "elm", 0, 0) == 0)
  40. {
  41. rt_kprintf("Mount \"sd0p1\" on \"/\" success\n");
  42. }
  43. else
  44. {
  45. rt_kprintf("Mount \"sd0p1\" on \"/data\" fail\n");
  46. }
  47. }
  48. while (1)
  49. {
  50. #ifdef SD_CARD_CHECK
  51. if (!rt_pin_read(SD_CHECK_PIN) && !old_sd_check) // mount
  52. #else
  53. if (!old_sd_check) // mount
  54. #endif
  55. {
  56. sd_mmc1_init();
  57. /* 挂载sd2分区 */
  58. if (RT_NULL != rt_device_find("sd1p0"))
  59. {
  60. if (access("/mnt", 0) != 0)
  61. {
  62. mkdir("/mnt", 0777);
  63. }
  64. if (dfs_mount("sd1p0", "/mnt", "elm", 0, 0) == 0)
  65. {
  66. rt_kprintf("Mount \"sd1p0\" on \"/mnt\" success\n");
  67. }
  68. else
  69. {
  70. rt_kprintf("Mount \"sd1p0\" on \"/mnt\" fail\n");
  71. }
  72. }
  73. old_sd_check = 1;
  74. }
  75. #ifdef SD_CARD_CHECK
  76. else if (rt_pin_read(SD_CHECK_PIN) && old_sd_check) // unmount
  77. {
  78. if (RT_NULL != rt_device_find("sd1p0"))
  79. {
  80. if (dfs_unmount("/mnt") == 0)
  81. {
  82. rt_kprintf("unMount \"sd1p0\" on \"/mnt\" success\n");
  83. }
  84. else
  85. {
  86. rt_kprintf("unMount \"sd1p0\" on \"/mnt\" fail\n");
  87. }
  88. }
  89. sd_mmc1_deinit();
  90. old_sd_check = 0;
  91. }
  92. #endif
  93. rt_thread_delay(RT_TICK_PER_SECOND);
  94. }
  95. }
  96. return 0;
  97. }
  98. int mnt_init(void)
  99. {
  100. #ifdef RT_USING_FAL
  101. {
  102. rt_thread_t thread = NULL;
  103. rt_pin_mode(SD_CHECK_PIN, PIN_MODE_INPUT_PULLUP);
  104. thread = rt_thread_create("sd", sd_check_thread_entry, NULL, 4096, 21, 10);
  105. if (thread == NULL)
  106. {
  107. return -1;
  108. }
  109. rt_thread_startup(thread);
  110. }
  111. #endif
  112. return 0;
  113. }
  114. INIT_ENV_EXPORT(mnt_init);
  115. #endif