drv_filesystem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_FATFS
  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. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  70. #include <fal.h>
  71. #define FS_PARTITION_NAME "spiflash0"
  72. static int onboard_spiflash_mount(void)
  73. {
  74. struct rt_device *mtd_dev = RT_NULL;
  75. fal_init();
  76. mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
  77. if (!mtd_dev)
  78. {
  79. LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
  80. }
  81. if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
  82. {
  83. LOG_I("spi flash mount to '/spiflash'");
  84. }
  85. else
  86. {
  87. dfs_mkfs("lfs", FS_PARTITION_NAME);
  88. if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
  89. {
  90. LOG_I("spi flash mount to '/spiflash'");
  91. }
  92. else
  93. {
  94. LOG_E("spi flash failed to mount to '/spiflash'");
  95. }
  96. }
  97. return RT_EOK;
  98. }
  99. #endif
  100. static const struct romfs_dirent _romfs_root[] =
  101. {
  102. #ifdef BSP_USING_SDCARD_FATFS
  103. {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
  104. #endif
  105. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  106. {ROMFS_DIRENT_DIR, "spiflash", RT_NULL, 0},
  107. #endif
  108. };
  109. const struct romfs_dirent romfs_root =
  110. {
  111. ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
  112. };
  113. static int filesystem_mount(void)
  114. {
  115. if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
  116. {
  117. LOG_E("rom mount to '/' failed!");
  118. }
  119. #ifdef BSP_USING_SDCARD_FATFS
  120. onboard_sdcard_mount();
  121. #endif
  122. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  123. onboard_spiflash_mount();
  124. #endif
  125. return RT_EOK;
  126. }
  127. INIT_APP_EXPORT(filesystem_mount);
  128. #endif /* BSP_USING_FS */