drv_filesystem.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * 2018-12-13 balanceTWK add sdcard port file
  9. * 2021-05-10 Meco Man fix a bug that cannot use fatfs in the main thread at starting up
  10. * 2021-07-28 Meco Man implement romfs as the root filesystem
  11. */
  12. #include <rtthread.h>
  13. #ifdef BSP_USING_FS
  14. #include <dfs_romfs.h>
  15. #include <dfs_fs.h>
  16. #include <dfs_posix.h>
  17. #if DFS_FILESYSTEMS_MAX < 4
  18. #error "Please define DFS_FILESYSTEMS_MAX more than 4"
  19. #endif
  20. #if DFS_FILESYSTEM_TYPES_MAX < 4
  21. #error "Please define DFS_FILESYSTEM_TYPES_MAX more than 4"
  22. #endif
  23. #define DBG_TAG "app.filesystem"
  24. #define DBG_LVL DBG_INFO
  25. #include <rtdbg.h>
  26. #ifdef BSP_USING_SDCARD
  27. static void sd_mount(void *parameter)
  28. {
  29. while (1)
  30. {
  31. rt_thread_mdelay(500);
  32. if(rt_device_find("sd0") != RT_NULL)
  33. {
  34. if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
  35. {
  36. LOG_I("sd card mount to '/sdcard'");
  37. break;
  38. }
  39. else
  40. {
  41. LOG_W("sd card mount to '/sdcard' failed!");
  42. }
  43. }
  44. }
  45. }
  46. static int onboard_sdcard_mount(void)
  47. {
  48. rt_thread_t tid;
  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. tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
  56. 1024, RT_THREAD_PRIORITY_MAX - 2, 20);
  57. if (tid != RT_NULL)
  58. {
  59. rt_thread_startup(tid);
  60. }
  61. else
  62. {
  63. LOG_E("create sd_mount thread err!");
  64. }
  65. }
  66. return RT_EOK;
  67. }
  68. #endif
  69. static const struct romfs_dirent _romfs_root[] =
  70. {
  71. #ifdef BSP_USING_SDCARD
  72. {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
  73. #endif
  74. // {ROMFS_DIRENT_DIR, "flash", RT_NULL, 0},
  75. };
  76. const struct romfs_dirent romfs_root =
  77. {
  78. ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
  79. };
  80. static int filesystem_mount(void)
  81. {
  82. if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
  83. {
  84. LOG_E("rom mount to '/' failed!");
  85. }
  86. #ifdef BSP_USING_SDCARD
  87. onboard_sdcard_mount();
  88. #endif
  89. return RT_EOK;
  90. }
  91. INIT_APP_EXPORT(filesystem_mount);
  92. #endif /* BSP_USING_FS */