drv_filesystem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <dfs_romfs.h>
  14. #include <dfs_fs.h>
  15. #include <dfs_file.h>
  16. #if DFS_FILESYSTEMS_MAX < 4
  17. #error "Please define DFS_FILESYSTEMS_MAX more than 4"
  18. #endif
  19. #if DFS_FILESYSTEM_TYPES_MAX < 4
  20. #error "Please define DFS_FILESYSTEM_TYPES_MAX more than 4"
  21. #endif
  22. #define DBG_TAG "app.filesystem"
  23. #define DBG_LVL DBG_INFO
  24. #include <rtdbg.h>
  25. #ifdef BSP_USING_SDCARD_FATFS
  26. static int onboard_sdcard_mount(void)
  27. {
  28. if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
  29. {
  30. LOG_I("SD card mount to '/sdcard'");
  31. }
  32. else
  33. {
  34. LOG_E("SD card mount to '/sdcard' failed!");
  35. }
  36. return RT_EOK;
  37. }
  38. #endif /* BSP_USING_SDCARD_FATFS */
  39. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  40. #include <fal.h>
  41. #define FS_PARTITION_NAME "spiflash0"
  42. static int onboard_spiflash_mount(void)
  43. {
  44. struct rt_device *mtd_dev = RT_NULL;
  45. fal_init();
  46. mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
  47. if (!mtd_dev)
  48. {
  49. LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
  50. }
  51. if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
  52. {
  53. LOG_I("spi flash mount to '/spiflash'");
  54. }
  55. else
  56. {
  57. dfs_mkfs("lfs", FS_PARTITION_NAME);
  58. if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
  59. {
  60. LOG_I("spi flash mount to '/spiflash'");
  61. }
  62. else
  63. {
  64. LOG_E("spi flash failed to mount to '/spiflash'");
  65. }
  66. }
  67. return RT_EOK;
  68. }
  69. #endif /* BSP_USING_SPI_FLASH_LITTLEFS */
  70. static const struct romfs_dirent _romfs_root[] =
  71. {
  72. #ifdef BSP_USING_SDCARD_FATFS
  73. {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
  74. #endif
  75. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  76. {ROMFS_DIRENT_DIR, "spiflash", RT_NULL, 0},
  77. #endif
  78. };
  79. static const struct romfs_dirent romfs_root =
  80. {
  81. ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
  82. };
  83. static int filesystem_mount(void)
  84. {
  85. if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
  86. {
  87. LOG_E("rom mount to '/' failed!");
  88. }
  89. #ifdef BSP_USING_SDCARD_FATFS
  90. onboard_sdcard_mount();
  91. #endif
  92. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  93. onboard_spiflash_mount();
  94. #endif
  95. return RT_EOK;
  96. }
  97. INIT_APP_EXPORT(filesystem_mount);