drv_filesystem.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <drv_gpio.h>
  13. #include <dfs_elm.h>
  14. #include <dfs_fs.h>
  15. #include <dfs_file.h>
  16. #include <dfs_romfs.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. #include <drv_spi.h>
  28. #include <spi_msd.h>
  29. static int rt_hw_spi1_tfcard(void)
  30. {
  31. __HAL_RCC_GPIOC_CLK_ENABLE();
  32. rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(C, 3));
  33. return msd_init("sd0", "spi10");
  34. }
  35. INIT_DEVICE_EXPORT(rt_hw_spi1_tfcard);
  36. static int onboard_sdcard_mount(void)
  37. {
  38. if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
  39. {
  40. LOG_I("SD card mount to '/sdcard'");
  41. }
  42. else
  43. {
  44. LOG_E("SD card mount to '/sdcard' failed!");
  45. }
  46. return RT_EOK;
  47. }
  48. #endif /* BSP_USING_SDCARD_FATFS */
  49. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  50. #endif /* BSP_USING_SPI_FLASH_LITTLEFS */
  51. static const struct romfs_dirent _romfs_root[] =
  52. {
  53. #ifdef BSP_USING_SDCARD_FATFS
  54. {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
  55. #endif
  56. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  57. {ROMFS_DIRENT_DIR, "spiflash", RT_NULL, 0},
  58. #endif
  59. };
  60. const struct romfs_dirent romfs_root =
  61. {
  62. ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
  63. };
  64. static int filesystem_mount(void)
  65. {
  66. if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
  67. {
  68. LOG_E("rom mount to '/' failed!");
  69. }
  70. #ifdef BSP_USING_SDCARD_FATFS
  71. onboard_sdcard_mount();
  72. #endif
  73. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  74. onboard_spiflash_mount();
  75. #endif
  76. return RT_EOK;
  77. }
  78. INIT_APP_EXPORT(filesystem_mount);