filesystem.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-13 balanceTWK add sdcard port file
  9. * 2019-06-11 WillianChan Add SD card hot plug detection
  10. */
  11. #include <rtthread.h>
  12. #ifdef BSP_USING_FS
  13. #if DFS_FILESYSTEMS_MAX < 4
  14. #error "Please define DFS_FILESYSTEMS_MAX more than 4"
  15. #endif
  16. #if DFS_FILESYSTEM_TYPES_MAX < 4
  17. #error "Please define DFS_FILESYSTEM_TYPES_MAX more than 4"
  18. #endif
  19. #include <dfs_fs.h>
  20. #include "dfs_romfs.h"
  21. #ifdef BSP_USING_SDCARD_FS
  22. #include <board.h>
  23. #include "drv_sdio.h"
  24. #endif
  25. #ifdef BSP_USING_SPI_FLASH_FS
  26. #include "fal.h"
  27. #endif
  28. #define DBG_TAG "app.filesystem"
  29. #define DBG_LVL DBG_INFO
  30. #include <rtdbg.h>
  31. static const struct romfs_dirent _romfs_root[] =
  32. {
  33. {ROMFS_DIRENT_DIR, "flash", RT_NULL, 0},
  34. {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0}
  35. };
  36. const struct romfs_dirent romfs_root =
  37. {
  38. ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
  39. };
  40. #ifdef BSP_USING_SDCARD_FS
  41. /* SD Card hot plug detection pin */
  42. #define SD_CHECK_PIN GET_PIN(D, 5)
  43. static void _sdcard_mount(void)
  44. {
  45. rt_device_t device;
  46. device = rt_device_find("sd0");
  47. if (device == NULL)
  48. {
  49. mmcsd_wait_cd_changed(0);
  50. sdcard_change();
  51. mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
  52. device = rt_device_find("sd0");
  53. }
  54. if (device != RT_NULL)
  55. {
  56. if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
  57. {
  58. LOG_I("sd card mount to '/sdcard'");
  59. }
  60. else
  61. {
  62. LOG_W("sd card mount to '/sdcard' failed!");
  63. }
  64. }
  65. }
  66. static void _sdcard_unmount(void)
  67. {
  68. rt_thread_mdelay(200);
  69. dfs_unmount("/sdcard");
  70. LOG_I("Unmount \"/sdcard\"");
  71. mmcsd_wait_cd_changed(0);
  72. sdcard_change();
  73. mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
  74. }
  75. static void sd_mount(void *parameter)
  76. {
  77. rt_uint8_t re_sd_check_pin = 1;
  78. rt_thread_mdelay(200);
  79. if (rt_pin_read(SD_CHECK_PIN))
  80. {
  81. _sdcard_mount();
  82. }
  83. while (1)
  84. {
  85. rt_thread_mdelay(200);
  86. if (!re_sd_check_pin && (re_sd_check_pin = rt_pin_read(SD_CHECK_PIN)) != 0)
  87. {
  88. _sdcard_mount();
  89. }
  90. if (re_sd_check_pin && (re_sd_check_pin = rt_pin_read(SD_CHECK_PIN)) == 0)
  91. {
  92. _sdcard_unmount();
  93. }
  94. }
  95. }
  96. #endif /* BSP_USING_SDCARD_FS */
  97. int mount_init(void)
  98. {
  99. if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
  100. {
  101. LOG_E("rom mount to '/' failed!");
  102. }
  103. #ifdef BSP_USING_SPI_FLASH_FS
  104. struct rt_device *flash_dev = RT_NULL;
  105. #ifndef RT_USING_WIFI
  106. fal_init();
  107. #endif
  108. flash_dev = fal_mtd_nor_device_create("filesystem");
  109. if (flash_dev)
  110. {
  111. //mount filesystem
  112. if (dfs_mount(flash_dev->parent.name, "/flash", "lfs", 0, 0) != 0)
  113. {
  114. LOG_W("mount to '/flash' failed! try to mkfs %s", flash_dev->parent.name);
  115. dfs_mkfs("lfs", flash_dev->parent.name);
  116. if (dfs_mount(flash_dev->parent.name, "/flash", "lfs", 0, 0) == 0)
  117. {
  118. LOG_I("mount to '/flash' success!");
  119. }
  120. }
  121. else
  122. {
  123. LOG_I("mount to '/flash' success!");
  124. }
  125. }
  126. else
  127. {
  128. LOG_E("Can't create block device filesystem or bt_image partition.");
  129. }
  130. #endif
  131. #ifdef BSP_USING_SDCARD_FS
  132. rt_thread_t tid;
  133. rt_pin_mode(SD_CHECK_PIN, PIN_MODE_INPUT_PULLUP);
  134. tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
  135. 2048, RT_THREAD_PRIORITY_MAX - 2, 20);
  136. if (tid != RT_NULL)
  137. {
  138. rt_thread_startup(tid);
  139. }
  140. else
  141. {
  142. LOG_E("create sd_mount thread err!");
  143. }
  144. #endif
  145. return RT_EOK;
  146. }
  147. INIT_APP_EXPORT(mount_init);
  148. #endif /* BSP_USING_FS */