|
@@ -7,6 +7,7 @@
|
|
* Date Author Notes
|
|
* Date Author Notes
|
|
* 2021-12-31 BruceOu first implementation
|
|
* 2021-12-31 BruceOu first implementation
|
|
* 2023-06-03 CX fixed sf probe error bug
|
|
* 2023-06-03 CX fixed sf probe error bug
|
|
|
|
+ * 2024-05-30 godmial refactor driver for multi-SPI bus auto-mount
|
|
*/
|
|
*/
|
|
#include <board.h>
|
|
#include <board.h>
|
|
#include "drv_spi.h"
|
|
#include "drv_spi.h"
|
|
@@ -19,72 +20,102 @@
|
|
#include <rthw.h>
|
|
#include <rthw.h>
|
|
#include <finsh.h>
|
|
#include <finsh.h>
|
|
|
|
|
|
-#define SPI_BUS_NAME "spi0"
|
|
|
|
-#define SPI_DEVICE_NAME "spi01"
|
|
|
|
-#define SPI_FLASH_DEVICE_NAME "gd25q"
|
|
|
|
|
|
+#ifdef RT_USING_DFS
|
|
|
|
+#include <dfs_fs.h>
|
|
|
|
+#endif
|
|
|
|
|
|
-#define GD25Q_SPI_CS_GPIOX_CLK RCU_GPIOE
|
|
|
|
-#define GD25Q_SPI_CS_GPIOX GPIOE
|
|
|
|
-#define GD25Q_SPI_CS_GPIOX_PIN_X GPIO_PIN_3
|
|
|
|
|
|
+struct spi_flash_config
|
|
|
|
+{
|
|
|
|
+ const char *bus_name;
|
|
|
|
+ const char *device_name;
|
|
|
|
+ const char *flash_name;
|
|
|
|
+ rt_base_t cs_pin;
|
|
|
|
+};
|
|
|
|
|
|
-static int rt_hw_spi_flash_init(void)
|
|
|
|
|
|
+static const struct spi_flash_config flash_configs[] =
|
|
{
|
|
{
|
|
- rt_err_t res;
|
|
|
|
- static struct rt_spi_device spi_dev_gd25q; /* SPI device */
|
|
|
|
- static struct gd32_spi_cs spi_cs;
|
|
|
|
- spi_cs.GPIOx = GD25Q_SPI_CS_GPIOX;
|
|
|
|
- spi_cs.GPIO_Pin = GD25Q_SPI_CS_GPIOX_PIN_X;
|
|
|
|
|
|
+#ifdef BSP_USING_SPI0
|
|
|
|
+ {
|
|
|
|
+ .bus_name = "spi0",
|
|
|
|
+ .device_name = "spi00",
|
|
|
|
+ .flash_name = "gd25q_spi0",
|
|
|
|
+ .cs_pin = GET_PIN(A, 4),
|
|
|
|
+ },
|
|
|
|
+#endif
|
|
|
|
|
|
- rcu_periph_clock_enable(GD25Q_SPI_CS_GPIOX_CLK);
|
|
|
|
-#if defined SOC_SERIES_GD32F4xx
|
|
|
|
- gpio_mode_set(spi_cs.GPIOx, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, spi_cs.GPIO_Pin);
|
|
|
|
- gpio_output_options_set(spi_cs.GPIOx, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, spi_cs.GPIO_Pin);
|
|
|
|
|
|
+#ifdef BSP_USING_SPI1
|
|
|
|
+ {
|
|
|
|
+ .bus_name = "spi1",
|
|
|
|
+ .device_name = "spi10",
|
|
|
|
+ .flash_name = "gd25q_spi1",
|
|
|
|
+ .cs_pin = GET_PIN(B, 9),
|
|
|
|
+ },
|
|
|
|
+#endif
|
|
|
|
|
|
- gpio_bit_set(spi_cs.GPIOx, spi_cs.GPIO_Pin);
|
|
|
|
-#else
|
|
|
|
- gpio_init(spi_cs.GPIOx, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, spi_cs.GPIO_Pin);
|
|
|
|
|
|
+#ifdef BSP_USING_SPI2
|
|
|
|
+ {
|
|
|
|
+ .bus_name = "spi2",
|
|
|
|
+ .device_name = "spi20",
|
|
|
|
+ .flash_name = "gd25q_spi2",
|
|
|
|
+ .cs_pin = GET_PIN(B, 12),
|
|
|
|
+ },
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
+#ifdef BSP_USING_SPI3
|
|
|
|
+ {
|
|
|
|
+ .bus_name = "spi3",
|
|
|
|
+ .device_name = "spi30",
|
|
|
|
+ .flash_name = "gd25q_spi3",
|
|
|
|
+ .cs_pin = GET_PIN(E, 4),
|
|
|
|
+ },
|
|
#endif
|
|
#endif
|
|
- res = rt_spi_bus_attach_device(&spi_dev_gd25q, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs);
|
|
|
|
|
|
|
|
- if (res != RT_EOK)
|
|
|
|
|
|
+#ifdef BSP_USING_SPI4
|
|
{
|
|
{
|
|
- rt_kprintf("rt_spi_bus_attach_device() run failed!\n");
|
|
|
|
- return res;
|
|
|
|
- }
|
|
|
|
|
|
+ .bus_name = "spi4",
|
|
|
|
+ .device_name = "spi40",
|
|
|
|
+ .flash_name = "gd25q_spi4",
|
|
|
|
+ .cs_pin = GET_PIN(F, 6),
|
|
|
|
+ },
|
|
|
|
+#endif
|
|
|
|
+};
|
|
|
|
|
|
- return RT_EOK;
|
|
|
|
-}
|
|
|
|
-INIT_DEVICE_EXPORT(rt_hw_spi_flash_init);
|
|
|
|
|
|
|
|
-#ifdef RT_USING_SFUD
|
|
|
|
-static int rt_hw_spi_flash_with_sfud_init(void)
|
|
|
|
|
|
+static int spi_flash_init(void)
|
|
{
|
|
{
|
|
- if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_DEVICE_NAME, SPI_DEVICE_NAME))
|
|
|
|
|
|
+ int result = RT_EOK;
|
|
|
|
+
|
|
|
|
+ for (size_t i = 0; i < sizeof(flash_configs) / sizeof(flash_configs[0]); i++)
|
|
{
|
|
{
|
|
- return -RT_ERROR;
|
|
|
|
- };
|
|
|
|
|
|
+ const struct spi_flash_config *cfg = &flash_configs[i];
|
|
|
|
|
|
- return RT_EOK;
|
|
|
|
-}
|
|
|
|
-INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init);
|
|
|
|
|
|
+ result = rt_hw_spi_device_attach(cfg->bus_name, cfg->device_name, cfg->cs_pin);
|
|
|
|
+ if (result != RT_EOK)
|
|
|
|
+ {
|
|
|
|
+ rt_kprintf("Failed to attach device %s on bus %s\n", cfg->device_name, cfg->bus_name);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+#ifdef RT_USING_SFUD
|
|
|
|
+ if (RT_NULL == rt_sfud_flash_probe(cfg->flash_name, cfg->device_name))
|
|
|
|
+ {
|
|
|
|
+ rt_kprintf("SFUD probe failed: %s\n", cfg->flash_name);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#ifdef RT_USING_DFS
|
|
#ifdef RT_USING_DFS
|
|
-#include <dfs_fs.h>
|
|
|
|
-
|
|
|
|
-int mnt_init(void)
|
|
|
|
-{
|
|
|
|
- if (dfs_mount(SPI_FLASH_DEVICE_NAME, "/", "elm", 0, 0) == 0)
|
|
|
|
- {
|
|
|
|
- rt_kprintf("spi flash mount success !\n");
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- rt_kprintf("spi flash mount failed!\n");
|
|
|
|
|
|
+ if (dfs_mount(cfg->flash_name, "/", "elm", 0, 0) == RT_EOK)
|
|
|
|
+ {
|
|
|
|
+ rt_kprintf("SPI flash %s mount success!\n", cfg->flash_name);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ rt_kprintf("SPI flash %s mount failed!\n", cfg->flash_name);
|
|
|
|
+ }
|
|
|
|
+#endif
|
|
}
|
|
}
|
|
|
|
|
|
- return 0;
|
|
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
-MSH_CMD_EXPORT(mnt_init, mount spi flash to file system);
|
|
|
|
-#endif
|
|
|
|
|
|
+INIT_COMPONENT_EXPORT(spi_flash_init);
|