drv_spi_flash.c 2.6 KB

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