drv_sdcard.c 1.7 KB

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