drv_spi_flash.c 1.8 KB

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