drv_sdcard.c 1.7 KB

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