drv_filesystem.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-14 balanceTWK add sdcard port file
  9. * 2021-02-26 Meco Man fix a bug that cannot use fatfs in the main thread at starting up
  10. */
  11. #include <rtthread.h>
  12. #include <dfs_elm.h>
  13. #include <dfs_fs.h>
  14. #include <dfs_file.h>
  15. #include <dfs_romfs.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. #include <drv_spi.h>
  27. #include <spi_msd.h>
  28. static int rt_hw_spi1_tfcard(void)
  29. {
  30. __HAL_RCC_GPIOC_CLK_ENABLE();
  31. rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(C, 3));
  32. return msd_init("sd0", "spi10");
  33. }
  34. INIT_DEVICE_EXPORT(rt_hw_spi1_tfcard);
  35. static int onboard_sdcard_mount(void)
  36. {
  37. if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
  38. {
  39. LOG_I("SD card mount to '/sdcard'");
  40. }
  41. else
  42. {
  43. LOG_E("SD card mount to '/sdcard' failed!");
  44. }
  45. return RT_EOK;
  46. }
  47. #endif /* BSP_USING_SDCARD_FATFS */
  48. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  49. #endif /* BSP_USING_SPI_FLASH_LITTLEFS */
  50. static const struct romfs_dirent _romfs_root[] =
  51. {
  52. #ifdef BSP_USING_SDCARD_FATFS
  53. {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
  54. #endif
  55. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  56. {ROMFS_DIRENT_DIR, "spiflash", RT_NULL, 0},
  57. #endif
  58. };
  59. const struct romfs_dirent romfs_root =
  60. {
  61. ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
  62. };
  63. static int filesystem_mount(void)
  64. {
  65. if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
  66. {
  67. LOG_E("rom mount to '/' failed!");
  68. }
  69. #ifdef BSP_USING_SDCARD_FATFS
  70. onboard_sdcard_mount();
  71. #endif
  72. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  73. onboard_spiflash_mount();
  74. #endif
  75. return RT_EOK;
  76. }
  77. INIT_APP_EXPORT(filesystem_mount);