drv_spi_flash.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. * 2021-08-23 lianzhian first implementation.
  9. */
  10. #include <board.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "spi_flash.h"
  14. #include "spi_flash_sfud.h"
  15. #define SPI_BUS_NAME "spi0"
  16. #define SPI_DEVICE_NAME "spi01"
  17. #define SPI_FLASH_DEVICE_NAME "gd25q"
  18. #define GD25Q_SPI_CS_PIN 67 /* PE3,在 drv_gpio.c 文件 pin_index pins[]中查到 PE3 编号为 67 */
  19. static int rt_hw_gd25q40_init(void)
  20. {
  21. rt_err_t res;
  22. static struct rt_spi_device spi_dev_gd25q; /* SPI设备对象 */
  23. static rt_base_t gd25q_cs_pin; /* SPI设备CS片选引脚 */
  24. gd25q_cs_pin = GD25Q_SPI_CS_PIN;
  25. rt_pin_mode(GD25Q_SPI_CS_PIN, GPIO_MODE_OUT_PP);
  26. res = rt_spi_bus_attach_device(&spi_dev_gd25q, SPI_DEVICE_NAME, SPI_BUS_NAME, (void*)gd25q_cs_pin);
  27. if (res != RT_EOK)
  28. {
  29. rt_kprintf("rt_spi_bus_attach_device() run failed!\n");
  30. return res;
  31. }
  32. return RT_EOK;
  33. }
  34. INIT_DEVICE_EXPORT(rt_hw_gd25q40_init);
  35. static int rt_hw_spi_flash_with_sfud_init(void)
  36. {
  37. if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_DEVICE_NAME, SPI_DEVICE_NAME))
  38. {
  39. return RT_ERROR;
  40. }
  41. return RT_EOK;
  42. }
  43. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init);
  44. #ifdef RT_USING_DFS
  45. #include <dfs_fs.h>
  46. int mnt_init(void)
  47. {
  48. if (dfs_mount(SPI_FLASH_DEVICE_NAME, "/", "elm", 0, 0) == 0)
  49. {
  50. rt_kprintf("spi flash mount success !\n");
  51. }
  52. else
  53. {
  54. rt_kprintf("spi flash mount failed!\n");
  55. }
  56. return 0;
  57. }
  58. MSH_CMD_EXPORT(mnt_init, mount spi flash to file system);
  59. #endif