filesystem.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2018, 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. #ifdef BSP_USING_SPI_FLASH_FS
  20. #include "fal.h"
  21. #endif
  22. #include <dfs_fs.h>
  23. #include "dfs_romfs.h"
  24. #include "drv_sdmmc.h"
  25. #define DBG_TAG "app.filesystem"
  26. #define DBG_LVL DBG_INFO
  27. #include <rtdbg.h>
  28. static const struct romfs_dirent _romfs_root[] = {
  29. // {ROMFS_DIRENT_DIR, "flash", RT_NULL, 0},
  30. {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0}};
  31. const struct romfs_dirent romfs_root = {
  32. ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])};
  33. #ifdef BSP_USING_SDCARD_FS
  34. /* SD Card hot plug detection pin */
  35. #define SD_CHECK_PIN GET_PIN(N, 7)
  36. static void _sdcard_mount(void)
  37. {
  38. rt_device_t device;
  39. device = rt_device_find("sd0");
  40. if (device == NULL)
  41. {
  42. mmcsd_wait_cd_changed(0);
  43. stm32_mmcsd_change();
  44. mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
  45. device = rt_device_find("sd0");
  46. }
  47. if (device != RT_NULL)
  48. {
  49. if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
  50. {
  51. LOG_I("sd card mount to '/sdcard'");
  52. }
  53. else
  54. {
  55. LOG_W("sd card mount to '/sdcard' failed!");
  56. }
  57. }
  58. }
  59. static void _sdcard_unmount(void)
  60. {
  61. rt_thread_mdelay(200);
  62. dfs_unmount("/sdcard");
  63. LOG_I("Unmount \"/sdcard\"");
  64. mmcsd_wait_cd_changed(0);
  65. stm32_mmcsd_change();
  66. mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
  67. }
  68. static void sd_mount(void *parameter)
  69. {
  70. rt_uint8_t re_sd_check_pin = 1;
  71. rt_thread_mdelay(200);
  72. if (rt_pin_read(SD_CHECK_PIN))
  73. {
  74. _sdcard_mount();
  75. }
  76. while (1)
  77. {
  78. rt_thread_mdelay(200);
  79. if (!re_sd_check_pin && (re_sd_check_pin = rt_pin_read(SD_CHECK_PIN)) != 0)
  80. {
  81. _sdcard_mount();
  82. }
  83. if (re_sd_check_pin && (re_sd_check_pin = rt_pin_read(SD_CHECK_PIN)) == 0)
  84. {
  85. _sdcard_unmount();
  86. }
  87. }
  88. }
  89. #endif /* BSP_USING_SDCARD_FS */
  90. int mount_init(void)
  91. {
  92. if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
  93. {
  94. LOG_E("rom mount to '/' failed!");
  95. }
  96. #ifdef BSP_USING_SPI_FLASH_FS
  97. struct rt_device *flash_dev = RT_NULL;
  98. #ifndef RT_USING_WIFI
  99. fal_init();
  100. #endif
  101. flash_dev = fal_mtd_nor_device_create("filesystem");
  102. if (flash_dev)
  103. {
  104. //mount filesystem
  105. if (dfs_mount(flash_dev->parent.name, "/flash", "lfs", 0, 0) != 0)
  106. {
  107. LOG_W("mount to '/flash' failed! try to mkfs %s", flash_dev->parent.name);
  108. dfs_mkfs("lfs", flash_dev->parent.name);
  109. if (dfs_mount(flash_dev->parent.name, "/flash", "lfs", 0, 0) == 0)
  110. {
  111. LOG_I("mount to '/flash' success!");
  112. }
  113. }
  114. else
  115. {
  116. LOG_I("mount to '/flash' success!");
  117. }
  118. }
  119. else
  120. {
  121. LOG_E("Can't create block device filesystem or bt_image partition.");
  122. }
  123. #endif
  124. #ifdef BSP_USING_SDCARD_FS
  125. rt_thread_t tid;
  126. rt_pin_mode(SD_CHECK_PIN, PIN_MODE_INPUT_PULLUP);
  127. tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
  128. 2048, RT_THREAD_PRIORITY_MAX - 2, 20);
  129. if (tid != RT_NULL)
  130. {
  131. rt_thread_startup(tid);
  132. }
  133. else
  134. {
  135. LOG_E("create sd_mount thread err!");
  136. }
  137. #endif
  138. return RT_EOK;
  139. }
  140. INIT_APP_EXPORT(mount_init);
  141. #endif /* BSP_USING_FS */