drv_sdcard.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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-13 balanceTWK add sdcard port file
  9. * 2019-08-23 WillianChan add spi sdcard port code
  10. */
  11. #include <rtthread.h>
  12. #if defined BSP_USING_SDIO_SDCARD || defined BSP_USING_SPI_SDCARD
  13. #include <dfs_elm.h>
  14. #include <dfs_fs.h>
  15. #include <dfs_posix.h>
  16. #include "drv_gpio.h"
  17. #define DBG_TAG "app.card"
  18. #define DBG_LVL DBG_INFO
  19. #include <rtdbg.h>
  20. void sd_mount(void *parameter)
  21. {
  22. while (1)
  23. {
  24. rt_thread_mdelay(500);
  25. if(rt_device_find("sd0") != RT_NULL)
  26. {
  27. if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK)
  28. {
  29. LOG_I("sd card mount to '/'");
  30. break;
  31. }
  32. else
  33. {
  34. LOG_W("sd card mount to '/' failed!");
  35. }
  36. }
  37. }
  38. }
  39. int stm32_sdcard_mount(void)
  40. {
  41. rt_thread_t tid;
  42. tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
  43. 1024, RT_THREAD_PRIORITY_MAX - 2, 20);
  44. if (tid != RT_NULL)
  45. {
  46. rt_thread_startup(tid);
  47. }
  48. else
  49. {
  50. LOG_E("create sd_mount thread err!");
  51. }
  52. return RT_EOK;
  53. }
  54. INIT_APP_EXPORT(stm32_sdcard_mount);
  55. #endif /* BSP_USING_SDIO_SDCARD || BSP_USING_SPI_SDCARD */
  56. #ifdef BSP_USING_SPI_SDCARD
  57. #include "drv_spi.h"
  58. #include "spi_msd.h"
  59. static int rt_hw_spi2_tfcard(void)
  60. {
  61. __HAL_RCC_GPIOC_CLK_ENABLE();
  62. rt_hw_spi_device_attach("spi2", "spi20", GPIOD, GPIO_PIN_2);
  63. return msd_init("sd0", "spi20");
  64. }
  65. INIT_DEVICE_EXPORT(rt_hw_spi2_tfcard);
  66. #endif /* BSP_USING_SPI_SDCARD */