drv_spi_flash.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "spi_flash_sfud.h"
  19. #include <rthw.h>
  20. #include <finsh.h>
  21. static int rt_hw_spi5_init(void)
  22. {
  23. /* register spi bus */
  24. {
  25. GPIO_InitTypeDef GPIO_InitStructure;
  26. rt_err_t result;
  27. __HAL_RCC_GPIOF_CLK_ENABLE();
  28. GPIO_InitStructure.Alternate = GPIO_AF5_SPI5;
  29. GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  30. GPIO_InitStructure.Pull = GPIO_PULLUP;
  31. GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
  32. GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
  33. HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
  34. result = stm32_spi_bus_register(SPI5, "spi5");
  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 stm32_spi_cs spi_cs;
  44. rt_err_t result;
  45. GPIO_InitTypeDef GPIO_InitStructure;
  46. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  47. GPIO_InitStructure.Pull = GPIO_PULLUP;
  48. GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
  49. spi_cs.GPIOx = GPIOF;
  50. spi_cs.GPIO_Pin = GPIO_PIN_6;
  51. //__HAL_RCC_GPIOF_CLK_ENABLE();
  52. GPIO_InitStructure.Pin = spi_cs.GPIO_Pin;
  53. HAL_GPIO_WritePin(spi_cs.GPIOx, spi_cs.GPIO_Pin, GPIO_PIN_SET);
  54. HAL_GPIO_Init(spi_cs.GPIOx, &GPIO_InitStructure);
  55. result = rt_spi_bus_attach_device(&spi_device, "spi50", "spi5", (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. static int rt_hw_spi_flash_with_sfud_init(void)
  65. {
  66. if (RT_NULL == rt_sfud_flash_probe("W25Q256", "spi50"))
  67. {
  68. return RT_ERROR;
  69. };
  70. return RT_EOK;
  71. }
  72. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init);