drv_spi_flash.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-12-31 BruceOu first implementation
  9. * 2023-06-03 CX fixed sf probe error bug
  10. * 2024-05-30 godmial refactor driver for multi-SPI bus auto-mount
  11. * 2025-11-28 godmial add configurable SPI Flash initialization
  12. * Only initialize flash on SPI buses explicitly configured
  13. * via BSP_USING_SPIx_FLASH options to avoid conflicts
  14. * with other SPI devices (e.g., OLED, WIFI)
  15. */
  16. #include <board.h>
  17. #include "drv_spi.h"
  18. #include "dev_spi_flash.h"
  19. #ifdef RT_USING_SFUD
  20. #include "dev_spi_flash_sfud.h"
  21. #endif
  22. #include <rthw.h>
  23. #include <finsh.h>
  24. #ifdef RT_USING_DFS
  25. #include <dfs_fs.h>
  26. #endif
  27. struct spi_flash_config
  28. {
  29. const char *bus_name;
  30. const char *device_name;
  31. const char *flash_name;
  32. rt_base_t cs_pin;
  33. };
  34. static const struct spi_flash_config flash_configs[] =
  35. {
  36. #if defined(BSP_USING_SPI0) && defined(BSP_USING_SPI0_FLASH)
  37. {
  38. .bus_name = "spi0",
  39. .device_name = "spi00",
  40. .flash_name = "gd25q_spi0",
  41. .cs_pin = GET_PIN(A, 4),
  42. },
  43. #endif
  44. #if defined(BSP_USING_SPI1) && defined(BSP_USING_SPI1_FLASH)
  45. {
  46. .bus_name = "spi1",
  47. .device_name = "spi10",
  48. .flash_name = "gd25q_spi1",
  49. .cs_pin = GET_PIN(B, 9),
  50. },
  51. #endif
  52. #if defined(BSP_USING_SPI2) && defined(BSP_USING_SPI2_FLASH)
  53. {
  54. .bus_name = "spi2",
  55. .device_name = "spi20",
  56. .flash_name = "gd25q_spi2",
  57. .cs_pin = GET_PIN(B, 12),
  58. },
  59. #endif
  60. #if defined(BSP_USING_SPI3) && defined(BSP_USING_SPI3_FLASH)
  61. {
  62. .bus_name = "spi3",
  63. .device_name = "spi30",
  64. .flash_name = "gd25q_spi3",
  65. .cs_pin = GET_PIN(E, 4),
  66. },
  67. #endif
  68. #if defined(BSP_USING_SPI4) && defined(BSP_USING_SPI4_FLASH)
  69. {
  70. .bus_name = "spi4",
  71. .device_name = "spi40",
  72. .flash_name = "gd25q_spi4",
  73. .cs_pin = GET_PIN(F, 6),
  74. },
  75. #endif
  76. #if defined(BSP_USING_SPI5) && defined(BSP_USING_SPI5_FLASH)
  77. {
  78. .bus_name = "spi5",
  79. .device_name = "spi50",
  80. .flash_name = "gd25q_spi5",
  81. .cs_pin = GET_PIN(F, 6), /* Note: Update CS pin according to actual hardware */
  82. },
  83. #endif
  84. };
  85. static int spi_flash_init(void)
  86. {
  87. #ifdef BSP_USING_SPI_FLASH
  88. int result = RT_EOK;
  89. for (size_t i = 0; i < sizeof(flash_configs) / sizeof(flash_configs[0]); i++)
  90. {
  91. const struct spi_flash_config *cfg = &flash_configs[i];
  92. result = rt_hw_spi_device_attach(cfg->bus_name, cfg->device_name, cfg->cs_pin);
  93. if (result != RT_EOK)
  94. {
  95. rt_kprintf("Failed to attach device %s on bus %s\n", cfg->device_name, cfg->bus_name);
  96. continue;
  97. }
  98. #ifdef RT_USING_SFUD
  99. if (RT_NULL == rt_sfud_flash_probe(cfg->flash_name, cfg->device_name))
  100. {
  101. rt_kprintf("SFUD probe failed: %s\n", cfg->flash_name);
  102. continue;
  103. }
  104. #endif
  105. }
  106. return result;
  107. #else
  108. /* SPI Flash auto-initialization is disabled. User should initialize SPI Flash manually in board code. */
  109. return RT_EOK;
  110. #endif
  111. }
  112. INIT_COMPONENT_EXPORT(spi_flash_init);