drv_spi_flash.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * 2012-07-27 aozima fixed variable uninitialized.
  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_PERIPH SPI5
  20. #define SPI_BUS_NAME "spi5"
  21. #define SPI_FLASH_DEVICE_NAME "spi50"
  22. #define SPI_FLASH_CHIP "gd25q16"
  23. static int rt_hw_spi5_init(void)
  24. {
  25. /* register spi bus */
  26. {
  27. rt_err_t result;
  28. rcu_periph_clock_enable(RCU_GPIOG);
  29. rcu_periph_clock_enable(RCU_SPI5);
  30. /* SPI5_CLK(PG13), SPI5_MISO(PG12), SPI5_MOSI(PG14),SPI5_IO2(PG10) and SPI5_IO3(PG11) GPIO pin configuration */
  31. gpio_af_set(GPIOG, GPIO_AF_5, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14);
  32. gpio_mode_set(GPIOG, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14);
  33. gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14);
  34. result = gd32_spi_bus_register(SPI5, SPI_BUS_NAME);
  35. if (result != RT_EOK)
  36. {
  37. return result;
  38. }
  39. }
  40. /* attach cs */
  41. {
  42. static struct rt_spi_device spi_device;
  43. static struct gd32_spi_cs spi_cs;
  44. rt_err_t result;
  45. spi_cs.GPIOx = GPIOG;
  46. spi_cs.GPIO_Pin = GPIO_PIN_9;
  47. /* SPI5_CS(PG9) GPIO pin configuration */
  48. gpio_mode_set(GPIOG, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_9);
  49. gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  50. gpio_bit_set(GPIOG,GPIO_PIN_9);
  51. result = rt_spi_bus_attach_device(&spi_device, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs);
  52. if (result != RT_EOK)
  53. {
  54. return result;
  55. }
  56. }
  57. return RT_EOK;
  58. }
  59. INIT_DEVICE_EXPORT(rt_hw_spi5_init);
  60. #ifdef RT_USING_SFUD
  61. static int rt_hw_spi_flash_with_sfud_init(void)
  62. {
  63. if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_CHIP, SPI_FLASH_DEVICE_NAME))
  64. {
  65. return RT_ERROR;
  66. };
  67. return RT_EOK;
  68. }
  69. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init)
  70. #endif