|
7 | 7 | * Date Author Notes
|
8 | 8 | * 2021-12-31 BruceOu first implementation
|
9 | 9 | * 2023-06-03 CX fixed sf probe error bug
|
| 10 | + * 2024-05-30 godmial refactor driver for multi-SPI bus auto-mount |
10 | 11 | */
|
11 | 12 | #include <board.h>
|
12 | 13 | #include "drv_spi.h"
|
|
19 | 20 | #include <rthw.h>
|
20 | 21 | #include <finsh.h>
|
21 | 22 |
|
22 |
| -#define SPI_BUS_NAME "spi0" |
23 |
| -#define SPI_DEVICE_NAME "spi01" |
24 |
| -#define SPI_FLASH_DEVICE_NAME "gd25q" |
| 23 | +#ifdef RT_USING_DFS |
| 24 | +#include <dfs_fs.h> |
| 25 | +#endif |
25 | 26 |
|
26 |
| -#define GD25Q_SPI_CS_GPIOX_CLK RCU_GPIOE |
27 |
| -#define GD25Q_SPI_CS_GPIOX GPIOE |
28 |
| -#define GD25Q_SPI_CS_GPIOX_PIN_X GPIO_PIN_3 |
| 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 | +}; |
29 | 34 |
|
30 |
| -static int rt_hw_spi_flash_init(void) |
| 35 | +static const struct spi_flash_config flash_configs[] = |
31 | 36 | {
|
32 |
| - rt_err_t res; |
33 |
| - static struct rt_spi_device spi_dev_gd25q; /* SPI device */ |
34 |
| - static struct gd32_spi_cs spi_cs; |
35 |
| - spi_cs.GPIOx = GD25Q_SPI_CS_GPIOX; |
36 |
| - spi_cs.GPIO_Pin = GD25Q_SPI_CS_GPIOX_PIN_X; |
| 37 | +#ifdef BSP_USING_SPI0 |
| 38 | + { |
| 39 | + .bus_name = "spi0", |
| 40 | + .device_name = "spi00", |
| 41 | + .flash_name = "gd25q_spi0", |
| 42 | + .cs_pin = GET_PIN(A, 4), |
| 43 | + }, |
| 44 | +#endif |
37 | 45 |
|
38 |
| - rcu_periph_clock_enable(GD25Q_SPI_CS_GPIOX_CLK); |
39 |
| -#if defined SOC_SERIES_GD32F4xx |
40 |
| - gpio_mode_set(spi_cs.GPIOx, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, spi_cs.GPIO_Pin); |
41 |
| - gpio_output_options_set(spi_cs.GPIOx, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, spi_cs.GPIO_Pin); |
| 46 | +#ifdef BSP_USING_SPI1 |
| 47 | + { |
| 48 | + .bus_name = "spi1", |
| 49 | + .device_name = "spi10", |
| 50 | + .flash_name = "gd25q_spi1", |
| 51 | + .cs_pin = GET_PIN(B, 9), |
| 52 | + }, |
| 53 | +#endif |
42 | 54 |
|
43 |
| - gpio_bit_set(spi_cs.GPIOx, spi_cs.GPIO_Pin); |
44 |
| -#else |
45 |
| - gpio_init(spi_cs.GPIOx, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, spi_cs.GPIO_Pin); |
| 55 | +#ifdef BSP_USING_SPI2 |
| 56 | + { |
| 57 | + .bus_name = "spi2", |
| 58 | + .device_name = "spi20", |
| 59 | + .flash_name = "gd25q_spi2", |
| 60 | + .cs_pin = GET_PIN(B, 12), |
| 61 | + }, |
| 62 | +#endif |
46 | 63 |
|
| 64 | +#ifdef BSP_USING_SPI3 |
| 65 | + { |
| 66 | + .bus_name = "spi3", |
| 67 | + .device_name = "spi30", |
| 68 | + .flash_name = "gd25q_spi3", |
| 69 | + .cs_pin = GET_PIN(E, 4), |
| 70 | + }, |
47 | 71 | #endif
|
48 |
| - res = rt_spi_bus_attach_device(&spi_dev_gd25q, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs); |
49 | 72 |
|
50 |
| - if (res != RT_EOK) |
| 73 | +#ifdef BSP_USING_SPI4 |
51 | 74 | {
|
52 |
| - rt_kprintf("rt_spi_bus_attach_device() run failed!\n"); |
53 |
| - return res; |
54 |
| - } |
| 75 | + .bus_name = "spi4", |
| 76 | + .device_name = "spi40", |
| 77 | + .flash_name = "gd25q_spi4", |
| 78 | + .cs_pin = GET_PIN(F, 6), |
| 79 | + }, |
| 80 | +#endif |
| 81 | +}; |
55 | 82 |
|
56 |
| - return RT_EOK; |
57 |
| -} |
58 |
| -INIT_DEVICE_EXPORT(rt_hw_spi_flash_init); |
59 | 83 |
|
60 |
| -#ifdef RT_USING_SFUD |
61 |
| -static int rt_hw_spi_flash_with_sfud_init(void) |
| 84 | +static int spi_flash_init(void) |
62 | 85 | {
|
63 |
| - if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_DEVICE_NAME, SPI_DEVICE_NAME)) |
| 86 | + int result = RT_EOK; |
| 87 | + |
| 88 | + for (size_t i = 0; i < sizeof(flash_configs) / sizeof(flash_configs[0]); i++) |
64 | 89 | {
|
65 |
| - return -RT_ERROR; |
66 |
| - }; |
| 90 | + const struct spi_flash_config *cfg = &flash_configs[i]; |
67 | 91 |
|
68 |
| - return RT_EOK; |
69 |
| -} |
70 |
| -INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init); |
| 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 | + |
| 99 | +#ifdef RT_USING_SFUD |
| 100 | + if (RT_NULL == rt_sfud_flash_probe(cfg->flash_name, cfg->device_name)) |
| 101 | + { |
| 102 | + rt_kprintf("SFUD probe failed: %s\n", cfg->flash_name); |
| 103 | + continue; |
| 104 | + } |
71 | 105 | #endif
|
72 | 106 |
|
73 | 107 | #ifdef RT_USING_DFS
|
74 |
| -#include <dfs_fs.h> |
75 |
| - |
76 |
| -int mnt_init(void) |
77 |
| -{ |
78 |
| - if (dfs_mount(SPI_FLASH_DEVICE_NAME, "/", "elm", 0, 0) == 0) |
79 |
| - { |
80 |
| - rt_kprintf("spi flash mount success !\n"); |
81 |
| - } |
82 |
| - else |
83 |
| - { |
84 |
| - rt_kprintf("spi flash mount failed!\n"); |
| 108 | + if (dfs_mount(cfg->flash_name, "/", "elm", 0, 0) == RT_EOK) |
| 109 | + { |
| 110 | + rt_kprintf("SPI flash %s mount success!\n", cfg->flash_name); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + rt_kprintf("SPI flash %s mount failed!\n", cfg->flash_name); |
| 115 | + } |
| 116 | +#endif |
85 | 117 | }
|
86 | 118 |
|
87 |
| - return 0; |
| 119 | + return result; |
88 | 120 | }
|
89 |
| -MSH_CMD_EXPORT(mnt_init, mount spi flash to file system); |
90 |
| -#endif |
| 121 | +INIT_COMPONENT_EXPORT(spi_flash_init); |
0 commit comments