drv_sdcard.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #ifdef BSP_USING_SDCARD
  13. #include <dfs_elm.h>
  14. #include <dfs_fs.h>
  15. #include <dfs_file.h>
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <sys/stat.h>
  19. #include <sys/statfs.h>
  20. #include "drv_spi.h"
  21. #include "spi_msd.h"
  22. #define DBG_TAG "app.card"
  23. #define DBG_LVL DBG_INFO
  24. #include <rtdbg.h>
  25. static void sd_mount(void *parameter)
  26. {
  27. while (1)
  28. {
  29. rt_thread_mdelay(500);
  30. if(rt_device_find("sd0") != RT_NULL)
  31. {
  32. if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK)
  33. {
  34. LOG_I("sd card mount to '/'");
  35. break;
  36. }
  37. else
  38. {
  39. LOG_W("sd card mount to '/' failed!");
  40. }
  41. }
  42. }
  43. }
  44. static int onboard_sdcard_mount(void)
  45. {
  46. rt_thread_t tid;
  47. if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK)
  48. {
  49. LOG_I("sd card mount to '/'");
  50. }
  51. else
  52. {
  53. tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
  54. 1024, RT_THREAD_PRIORITY_MAX - 2, 20);
  55. if (tid != RT_NULL)
  56. {
  57. rt_thread_startup(tid);
  58. }
  59. else
  60. {
  61. LOG_E("create sd_mount thread err!");
  62. }
  63. }
  64. return RT_EOK;
  65. }
  66. INIT_APP_EXPORT(onboard_sdcard_mount);
  67. static int rt_hw_spi1_tfcard(void)
  68. {
  69. __HAL_RCC_GPIOC_CLK_ENABLE();
  70. rt_hw_spi_device_attach("spi1", "spi10", GPIOC, GPIO_PIN_3);
  71. return msd_init("sd0", "spi10");
  72. }
  73. INIT_DEVICE_EXPORT(rt_hw_spi1_tfcard);
  74. #endif /* BSP_USING_SDCARD */