drv_spi_flash.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. */
  10. #include <board.h>
  11. #include "drv_spi.h"
  12. #include "spi_flash.h"
  13. #ifdef RT_USING_SFUD
  14. #include "spi_flash_sfud.h"
  15. #endif
  16. #include <rthw.h>
  17. #include <finsh.h>
  18. #define SPI_BUS_NAME "spi0"
  19. #define SPI_DEVICE_NAME "spi01"
  20. #define SPI_FLASH_DEVICE_NAME "gd25q"
  21. #define GD25Q_SPI_CS_GPIOX GPIOE
  22. #define GD25Q_SPI_CS_GPIOX_PIN_X GPIO_PIN_3
  23. static int rt_hw_spi_flash_init(void)
  24. {
  25. rt_err_t res;
  26. static struct rt_spi_device spi_dev_gd25q; /* SPI device */
  27. static struct gd32_spi_cs spi_cs;
  28. spi_cs.GPIOx = GD25Q_SPI_CS_GPIOX;
  29. spi_cs.GPIO_Pin = GD25Q_SPI_CS_GPIOX_PIN_X;
  30. #if defined SOC_SERIES_GD32F4xx
  31. gpio_mode_set(spi_cs.GPIOx, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, spi_cs.GPIO_Pin);
  32. gpio_output_options_set(spi_cs.GPIOx, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, spi_cs.GPIO_Pin);
  33. gpio_bit_set(spi_cs.GPIOx, spi_cs.GPIO_Pin);
  34. #else
  35. gpio_init(spi_cs.GPIOx, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, spi_cs.GPIO_Pin);
  36. #endif
  37. res = rt_spi_bus_attach_device(&spi_dev_gd25q, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs);
  38. if (res != RT_EOK)
  39. {
  40. rt_kprintf("rt_spi_bus_attach_device() run failed!\n");
  41. return res;
  42. }
  43. return RT_EOK;
  44. }
  45. INIT_DEVICE_EXPORT(rt_hw_spi_flash_init);
  46. #ifdef RT_USING_SFUD
  47. static int rt_hw_spi_flash_with_sfud_init(void)
  48. {
  49. if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_CHIP, SPI_FLASH_DEVICE_NAME))
  50. {
  51. return -RT_ERROR;
  52. };
  53. return RT_EOK;
  54. }
  55. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init)
  56. #endif
  57. #ifdef RT_USING_DFS
  58. #include <dfs_fs.h>
  59. int mnt_init(void)
  60. {
  61. if (dfs_mount(SPI_FLASH_DEVICE_NAME, "/", "elm", 0, 0) == 0)
  62. {
  63. rt_kprintf("spi flash mount success !\n");
  64. }
  65. else
  66. {
  67. rt_kprintf("spi flash mount failed!\n");
  68. }
  69. return 0;
  70. }
  71. MSH_CMD_EXPORT(mnt_init, mount spi flash to file system);
  72. #endif