drv_spi_flash.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include "spi_flash_sfud.h"
  15. #include <rthw.h>
  16. #include <finsh.h>
  17. static int rt_hw_spi5_init(void)
  18. {
  19. /* register spi bus */
  20. {
  21. GPIO_InitTypeDef GPIO_InitStructure;
  22. rt_err_t result;
  23. __HAL_RCC_GPIOF_CLK_ENABLE();
  24. GPIO_InitStructure.Alternate = GPIO_AF5_SPI5;
  25. GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  26. GPIO_InitStructure.Pull = GPIO_PULLUP;
  27. GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
  28. GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
  29. HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
  30. result = stm32_spi_bus_register(SPI5, "spi5");
  31. if (result != RT_EOK)
  32. {
  33. return result;
  34. }
  35. }
  36. /* attach cs */
  37. {
  38. static struct rt_spi_device spi_device;
  39. static struct stm32_spi_cs spi_cs;
  40. rt_err_t result;
  41. GPIO_InitTypeDef GPIO_InitStructure;
  42. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  43. GPIO_InitStructure.Pull = GPIO_PULLUP;
  44. GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
  45. spi_cs.GPIOx = GPIOF;
  46. spi_cs.GPIO_Pin = GPIO_PIN_6;
  47. //__HAL_RCC_GPIOF_CLK_ENABLE();
  48. GPIO_InitStructure.Pin = spi_cs.GPIO_Pin;
  49. HAL_GPIO_WritePin(spi_cs.GPIOx, spi_cs.GPIO_Pin, GPIO_PIN_SET);
  50. HAL_GPIO_Init(spi_cs.GPIOx, &GPIO_InitStructure);
  51. result = rt_spi_bus_attach_device(&spi_device, "spi50", "spi5", (void*)&spi_cs);
  52. if (result != RT_EOK)
  53. {
  54. return result;
  55. }
  56. }
  57. return RT_EOK;
  58. }
  59. INIT_DEVICE_EXPORT(rt_hw_spi5_init);
  60. static int rt_hw_spi_flash_with_sfud_init(void)
  61. {
  62. if (RT_NULL == rt_sfud_flash_probe("W25Q256", "spi50"))
  63. {
  64. return RT_ERROR;
  65. };
  66. return RT_EOK;
  67. }
  68. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init);