drv_spi_flash.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * File : stm32f20x_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_BUS_NAME "spi1"
  30. #define SPI_FLASH_DEVICE_NAME "spi10"
  31. #define SPI_FLASH_CHIP "W25Q64"
  32. static int rt_hw_spi1_init(void)
  33. {
  34. /* register spi bus */
  35. {
  36. GPIO_InitTypeDef GPIO_InitStructure;
  37. rt_err_t result;
  38. __HAL_RCC_GPIOB_CLK_ENABLE();
  39. GPIO_InitStructure.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
  40. GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  41. GPIO_InitStructure.Pull = GPIO_NOPULL;
  42. GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  43. GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
  44. HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  45. result = stm32_spi_bus_register(SPI1, SPI_BUS_NAME);
  46. if (result != RT_EOK)
  47. {
  48. return result;
  49. }
  50. }
  51. /* attach cs */
  52. {
  53. static struct rt_spi_device spi_device;
  54. static struct stm32_spi_cs spi_cs;
  55. rt_err_t result;
  56. GPIO_InitTypeDef GPIO_InitStructure;
  57. __HAL_RCC_GPIOD_CLK_ENABLE();
  58. spi_cs.GPIOx = GPIOD;
  59. spi_cs.GPIO_Pin = GPIO_PIN_13;
  60. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  61. GPIO_InitStructure.Pull = GPIO_PULLUP;
  62. GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
  63. GPIO_InitStructure.Pin = spi_cs.GPIO_Pin;
  64. HAL_GPIO_Init(spi_cs.GPIOx, &GPIO_InitStructure);
  65. HAL_GPIO_WritePin(spi_cs.GPIOx, spi_cs.GPIO_Pin, GPIO_PIN_SET);
  66. result = rt_spi_bus_attach_device(&spi_device, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs);
  67. if (result != RT_EOK)
  68. {
  69. return result;
  70. }
  71. }
  72. return RT_EOK;
  73. }
  74. INIT_DEVICE_EXPORT(rt_hw_spi1_init);
  75. #ifdef RT_USING_SFUD
  76. static int rt_hw_spi_flash_with_sfud_init(void)
  77. {
  78. if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_CHIP, SPI_FLASH_DEVICE_NAME))
  79. {
  80. return RT_ERROR;
  81. };
  82. return RT_EOK;
  83. }
  84. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init)
  85. #endif
  86. #ifdef RT_USING_W25QXX
  87. static int rt_hw_spi_flash_init(void)
  88. {
  89. return w25qxx_init(SPI_FLASH_CHIP, SPI_FLASH_DEVICE_NAME);
  90. }
  91. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
  92. #endif