drv_spi_flash.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * File : gd32f20x_40x_spi.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-01-01 aozima first implementation.
  13. * 2012-07-27 aozima fixed variable uninitialized.
  14. */
  15. #include <board.h>
  16. #include "drv_spi.h"
  17. #include "spi_flash.h"
  18. #ifdef RT_USING_SFUD
  19. #include "spi_flash_sfud.h"
  20. #endif
  21. #ifdef RT_USING_W25QXX
  22. #include "spi_flash_w25qxx.h"
  23. #endif
  24. #include <rthw.h>
  25. #include <finsh.h>
  26. #if defined(RT_USING_SFUD) && defined(RT_USING_W25QXX)
  27. #error "RT_USING_SFUD and RT_USING_W25QXX only need one"
  28. #endif
  29. #define SPI_PERIPH SPI5
  30. #define SPI_BUS_NAME "spi5"
  31. #define SPI_FLASH_DEVICE_NAME "spi50"
  32. #define SPI_FLASH_CHIP "gd25q16"
  33. static int rt_hw_spi5_init(void)
  34. {
  35. /* register spi bus */
  36. {
  37. rt_err_t result;
  38. rcu_periph_clock_enable(RCU_GPIOG);
  39. rcu_periph_clock_enable(RCU_SPI5);
  40. /* SPI5_CLK(PG13), SPI5_MISO(PG12), SPI5_MOSI(PG14),SPI5_IO2(PG10) and SPI5_IO3(PG11) GPIO pin configuration */
  41. gpio_af_set(GPIOG, GPIO_AF_5, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14);
  42. 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);
  43. 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);
  44. result = gd32_spi_bus_register(SPI5, SPI_BUS_NAME);
  45. if (result != RT_EOK)
  46. {
  47. return result;
  48. }
  49. }
  50. /* attach cs */
  51. {
  52. static struct rt_spi_device spi_device;
  53. static struct gd32_spi_cs spi_cs;
  54. rt_err_t result;
  55. spi_cs.GPIOx = GPIOG;
  56. spi_cs.GPIO_Pin = GPIO_PIN_9;
  57. /* SPI5_CS(PG9) GPIO pin configuration */
  58. gpio_mode_set(GPIOG, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_9);
  59. gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  60. gpio_bit_set(GPIOG,GPIO_PIN_9);
  61. result = rt_spi_bus_attach_device(&spi_device, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs);
  62. if (result != RT_EOK)
  63. {
  64. return result;
  65. }
  66. }
  67. return RT_EOK;
  68. }
  69. INIT_DEVICE_EXPORT(rt_hw_spi5_init);
  70. #ifdef RT_USING_SFUD
  71. static int rt_hw_spi_flash_with_sfud_init(void)
  72. {
  73. if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_CHIP, SPI_FLASH_DEVICE_NAME))
  74. {
  75. return RT_ERROR;
  76. };
  77. return RT_EOK;
  78. }
  79. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init)
  80. #endif
  81. #ifdef RT_USING_W25QXX
  82. static int rt_hw_spi_flash_init(void)
  83. {
  84. return w25qxx_init(SPI_FLASH_CHIP, SPI_FLASH_DEVICE_NAME);
  85. }
  86. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init)
  87. #endif