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