drv_spi_flash.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-12-31 BruceOu first implementation
  9. * 2023-06-03 CX fixed sf probe error bug
  10. */
  11. #include <board.h>
  12. #include "drv_spi.h"
  13. #include "spi_flash.h"
  14. #ifdef RT_USING_SFUD
  15. #include "spi_flash_sfud.h"
  16. #endif
  17. #include <rthw.h>
  18. #include <finsh.h>
  19. #define SPI_BUS_NAME "spi0"
  20. #define SPI_DEVICE_NAME "spi01"
  21. #define SPI_FLASH_DEVICE_NAME "gd25q"
  22. #define GD25Q_SPI_CS_GPIOX_CLK RCU_GPIOE
  23. #define GD25Q_SPI_CS_GPIOX GPIOE
  24. #define GD25Q_SPI_CS_GPIOX_PIN_X GPIO_PIN_3
  25. static int rt_hw_spi_flash_init(void)
  26. {
  27. rt_err_t res;
  28. static struct rt_spi_device spi_dev_gd25q; /* SPI device */
  29. static struct gd32_spi_cs spi_cs;
  30. spi_cs.GPIOx = GD25Q_SPI_CS_GPIOX;
  31. spi_cs.GPIO_Pin = GD25Q_SPI_CS_GPIOX_PIN_X;
  32. rcu_periph_clock_enable(GD25Q_SPI_CS_GPIOX_CLK);
  33. #if defined SOC_SERIES_GD32F4xx
  34. gpio_mode_set(spi_cs.GPIOx, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, spi_cs.GPIO_Pin);
  35. gpio_output_options_set(spi_cs.GPIOx, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, spi_cs.GPIO_Pin);
  36. gpio_bit_set(spi_cs.GPIOx, spi_cs.GPIO_Pin);
  37. #else
  38. gpio_init(spi_cs.GPIOx, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, spi_cs.GPIO_Pin);
  39. #endif
  40. res = rt_spi_bus_attach_device(&spi_dev_gd25q, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs);
  41. if (res != RT_EOK)
  42. {
  43. rt_kprintf("rt_spi_bus_attach_device() run failed!\n");
  44. return res;
  45. }
  46. return RT_EOK;
  47. }
  48. INIT_DEVICE_EXPORT(rt_hw_spi_flash_init);
  49. #ifdef RT_USING_SFUD
  50. static int rt_hw_spi_flash_with_sfud_init(void)
  51. {
  52. if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_DEVICE_NAME, SPI_DEVICE_NAME))
  53. {
  54. return -RT_ERROR;
  55. };
  56. return RT_EOK;
  57. }
  58. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init)
  59. #endif
  60. #ifdef RT_USING_DFS
  61. #include <dfs_fs.h>
  62. int mnt_init(void)
  63. {
  64. if (dfs_mount(SPI_FLASH_DEVICE_NAME, "/", "elm", 0, 0) == 0)
  65. {
  66. rt_kprintf("spi flash mount success !\n");
  67. }
  68. else
  69. {
  70. rt_kprintf("spi flash mount failed!\n");
  71. }
  72. return 0;
  73. }
  74. MSH_CMD_EXPORT(mnt_init, mount spi flash to file system);
  75. #endif