mnt_diskfs.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022/12/25 flyingcys first version
  9. */
  10. #include <rtthread.h>
  11. #ifdef RT_USING_DFS
  12. #include <dfs_fs.h>
  13. #include "dfs_romfs.h"
  14. #define DBG_TAG "app.filesystem"
  15. #define DBG_LVL DBG_LOG
  16. #include <rtdbg.h>
  17. static const struct romfs_dirent _romfs_root[] =
  18. {
  19. #ifdef BSP_USING_ON_CHIP_FLASH
  20. {ROMFS_DIRENT_DIR, "flash", RT_NULL, 0},
  21. #endif
  22. {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0}
  23. };
  24. const struct romfs_dirent romfs_root =
  25. {
  26. ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
  27. };
  28. static void sd_mount(void *parameter)
  29. {
  30. while (1)
  31. {
  32. rt_thread_mdelay(500);
  33. if (rt_device_find("sd0") != RT_NULL)
  34. {
  35. if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
  36. {
  37. LOG_I("sd card mount to '/sdcard'");
  38. break;
  39. }
  40. else
  41. {
  42. LOG_W("sd card mount to '/sdcard' failed! %d\n", rt_get_errno());
  43. }
  44. }
  45. }
  46. }
  47. int mount_init(void)
  48. {
  49. if(dfs_mount(RT_NULL, "/", "rom", 0, &romfs_root) != 0)
  50. {
  51. LOG_E("rom mount to '/' failed!");
  52. }
  53. #ifdef BSP_USING_ON_CHIP_FLASH_FS
  54. struct rt_device *flash_dev = RT_NULL;
  55. /* 使用 filesystem 分区创建块设备,块设备名称为 filesystem */
  56. flash_dev = fal_blk_device_create("filesystem");
  57. if(flash_dev == RT_NULL)
  58. {
  59. LOG_E("Failed to create device.\n");
  60. return -RT_ERROR;
  61. }
  62. if (dfs_mount("filesystem", "/flash", "lfs", 0, 0) != 0)
  63. {
  64. LOG_I("file system initialization failed!\n");
  65. if(dfs_mkfs("lfs", "filesystem") == 0)
  66. {
  67. if (dfs_mount("filesystem", "/flash", "lfs", 0, 0) == 0)
  68. {
  69. LOG_I("mount to '/flash' success!");
  70. }
  71. }
  72. }
  73. else
  74. {
  75. LOG_I("mount to '/flash' success!");
  76. }
  77. #endif
  78. #ifdef BSP_USING_SDH
  79. rt_thread_t tid;
  80. tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
  81. 4096, RT_THREAD_PRIORITY_MAX - 2, 20);
  82. if (tid != RT_NULL)
  83. {
  84. rt_thread_startup(tid);
  85. }
  86. else
  87. {
  88. LOG_E("create sd_mount thread err!");
  89. }
  90. #endif
  91. return RT_EOK;
  92. }
  93. INIT_APP_EXPORT(mount_init);
  94. #endif /* RT_USING_DFS */