drv_spi_flash.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. */
  12. #include <board.h>
  13. #include "drv_spi.h"
  14. #include "dev_spi_flash.h"
  15. #ifdef RT_USING_SFUD
  16. #include "dev_spi_flash_sfud.h"
  17. #endif
  18. #include <rthw.h>
  19. #include <finsh.h>
  20. #ifdef RT_USING_DFS
  21. #include <dfs_fs.h>
  22. #endif
  23. struct spi_flash_config
  24. {
  25. const char *bus_name;
  26. const char *device_name;
  27. const char *flash_name;
  28. rt_base_t cs_pin;
  29. };
  30. static const struct spi_flash_config flash_configs[] =
  31. {
  32. #ifdef BSP_USING_SPI0
  33. {
  34. .bus_name = "spi0",
  35. .device_name = "spi00",
  36. .flash_name = "gd25q_spi0",
  37. .cs_pin = GET_PIN(A, 4),
  38. },
  39. #endif
  40. #ifdef BSP_USING_SPI1
  41. {
  42. .bus_name = "spi1",
  43. .device_name = "spi10",
  44. .flash_name = "gd25q_spi1",
  45. .cs_pin = GET_PIN(B, 9),
  46. },
  47. #endif
  48. #ifdef BSP_USING_SPI2
  49. {
  50. .bus_name = "spi2",
  51. .device_name = "spi20",
  52. .flash_name = "gd25q_spi2",
  53. .cs_pin = GET_PIN(B, 12),
  54. },
  55. #endif
  56. #ifdef BSP_USING_SPI3
  57. {
  58. .bus_name = "spi3",
  59. .device_name = "spi30",
  60. .flash_name = "gd25q_spi3",
  61. .cs_pin = GET_PIN(E, 4),
  62. },
  63. #endif
  64. #ifdef BSP_USING_SPI4
  65. {
  66. .bus_name = "spi4",
  67. .device_name = "spi40",
  68. .flash_name = "gd25q_spi4",
  69. .cs_pin = GET_PIN(F, 6),
  70. },
  71. #endif
  72. };
  73. static int spi_flash_init(void)
  74. {
  75. int result = RT_EOK;
  76. for (size_t i = 0; i < sizeof(flash_configs) / sizeof(flash_configs[0]); i++)
  77. {
  78. const struct spi_flash_config *cfg = &flash_configs[i];
  79. result = rt_hw_spi_device_attach(cfg->bus_name, cfg->device_name, cfg->cs_pin);
  80. if (result != RT_EOK)
  81. {
  82. rt_kprintf("Failed to attach device %s on bus %s\n", cfg->device_name, cfg->bus_name);
  83. continue;
  84. }
  85. #ifdef RT_USING_SFUD
  86. if (RT_NULL == rt_sfud_flash_probe(cfg->flash_name, cfg->device_name))
  87. {
  88. rt_kprintf("SFUD probe failed: %s\n", cfg->flash_name);
  89. continue;
  90. }
  91. #endif
  92. #ifdef RT_USING_DFS
  93. if (dfs_mount(cfg->flash_name, "/", "elm", 0, 0) == RT_EOK)
  94. {
  95. rt_kprintf("SPI flash %s mount success!\n", cfg->flash_name);
  96. }
  97. else
  98. {
  99. rt_kprintf("SPI flash %s mount failed!\n", cfg->flash_name);
  100. }
  101. #endif
  102. }
  103. return result;
  104. }
  105. INIT_COMPONENT_EXPORT(spi_flash_init);