init_sdtask.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rtthread.h>
  10. #if defined(RT_USING_DFS)
  11. #include <dfs.h>
  12. #include <dfs_fs.h>
  13. #include <dfs_file.h>
  14. #include <ioremap.h>
  15. #include <drv_sdio.h>
  16. #define DBG_TAG "app.filesystem"
  17. #define DBG_LVL DBG_INFO
  18. #include <rtdbg.h>
  19. void filesysytem_try_mount(char *device_name, char *mount_point, char *fs_type_name, int mkfs_count)
  20. {
  21. struct statfs fs_stat;
  22. int rc = 0;
  23. LOG_I("mount(\"%s\",\"%s\",\"%s\");", device_name, mount_point, fs_type_name);
  24. if (rt_device_find(device_name) == NULL)
  25. {
  26. LOG_I("%s not find!!!", device_name);
  27. return;
  28. }
  29. mkdir(mount_point, 0);
  30. _remount:
  31. rc = dfs_mount(device_name, mount_point, fs_type_name, 0, 0);
  32. if (rc == 0)
  33. {
  34. LOG_I("mounted %s on %s", device_name, mount_point);
  35. if (dfs_statfs(mount_point, &fs_stat) >= 0)
  36. {
  37. LOG_I("%s size:%d, total: %d, free: %d", mount_point,
  38. fs_stat.f_bsize, fs_stat.f_blocks, fs_stat.f_bfree);
  39. }
  40. }
  41. else
  42. {
  43. if (mkfs_count > 0)
  44. {
  45. LOG_I("[%s]try mkfs -t %s %s ", mkfs_count, fs_type_name, device_name);
  46. dfs_mkfs(fs_type_name, device_name);
  47. mkfs_count--;
  48. goto _remount;
  49. }
  50. LOG_I("mount failed :%d ", rc);
  51. }
  52. }
  53. void filesysytem_try_unmount(char *mount_point)
  54. {
  55. struct stat filestat = {0};
  56. LOG_I("unmount(\"%s\");", mount_point);
  57. if ((dfs_file_stat(mount_point, &filestat) >= 0) && (S_ISDIR(filestat.st_mode)))
  58. {
  59. dfs_unmount(mount_point);
  60. }
  61. }
  62. static void sd_task_entry(void *parameter)
  63. {
  64. volatile unsigned int *IN_STATUS;
  65. IN_STATUS = (volatile unsigned int *)rt_ioremap((void *)0x2190030, 4); // cd status
  66. int change = 0;
  67. while (1)
  68. {
  69. rt_thread_mdelay(200);
  70. change = 0;
  71. if (((*IN_STATUS >> 6) & 0x1) == 1)
  72. {
  73. *IN_STATUS = 0x40;
  74. change = 1;
  75. }
  76. if (((*IN_STATUS >> 7) & 0x1) == 1)
  77. {
  78. *IN_STATUS = (0x80);
  79. change = 2;
  80. }
  81. if (change > 0)
  82. {
  83. LOG_D("sdio host change: %d", change);
  84. mmcsd_wait_cd_changed(0); // clear
  85. host_change(); // send cd change to host
  86. int result = mmcsd_wait_cd_changed(RT_TICK_PER_SECOND);
  87. if (result == MMCSD_HOST_PLUGED)
  88. {
  89. LOG_D("mmcsd change pluged");
  90. filesysytem_try_mount("sd0", "/mnt/sd0", "elm", 0);
  91. }
  92. else if (result == MMCSD_HOST_UNPLUGED)
  93. {
  94. LOG_D("mmcsd change unpluged");
  95. filesysytem_try_unmount("/mnt/sd0");
  96. }
  97. else
  98. {
  99. LOG_I("mmcsd wait_cd_changed %d", result);
  100. }
  101. }
  102. }
  103. }
  104. int sd_task_init(void)
  105. {
  106. rt_thread_t tid;
  107. tid = rt_thread_create("tsdcard", sd_task_entry, RT_NULL,
  108. 2048, RT_THREAD_PRIORITY_MAX - 2, 20);
  109. if (tid != RT_NULL)
  110. {
  111. rt_thread_startup(tid);
  112. }
  113. else
  114. {
  115. LOG_E("create sd mount task error!");
  116. }
  117. return RT_EOK;
  118. }
  119. INIT_APP_EXPORT(sd_task_init);
  120. #endif