drv_spi_flash.c 2.5 KB

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