-
Notifications
You must be signed in to change notification settings - Fork 5.4k
bsp: gd32470z-lckfb: 增加 SPI Flash 支持及使用说明 #10347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| * Date Author Notes | ||
| * 2021-12-31 BruceOu first implementation | ||
| * 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 "drv_spi.h" | ||
|
|
@@ -19,72 +20,101 @@ | |
| #include <rthw.h> | ||
| #include <finsh.h> | ||
|
|
||
| #define SPI_BUS_NAME "spi0" | ||
| #define SPI_DEVICE_NAME "spi01" | ||
| #define SPI_FLASH_DEVICE_NAME "gd25q" | ||
|
|
||
| #define GD25Q_SPI_CS_GPIOX_CLK RCU_GPIOE | ||
| #define GD25Q_SPI_CS_GPIOX GPIOE | ||
| #define GD25Q_SPI_CS_GPIOX_PIN_X GPIO_PIN_3 | ||
| #ifdef RT_USING_DFS | ||
| #include <dfs_fs.h> | ||
| #endif | ||
|
|
||
| static int rt_hw_spi_flash_init(void) | ||
| struct spi_flash_config | ||
| { | ||
| 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; | ||
| const char *bus_name; | ||
| const char *device_name; | ||
| const char *flash_name; | ||
| rt_base_t cs_pin; | ||
| }; | ||
|
|
||
| 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); | ||
| static const struct spi_flash_config flash_configs[] = | ||
| { | ||
| #ifdef BSP_USING_SPI0 | ||
| { | ||
| .bus_name = "spi0", | ||
| .device_name = "spi00", | ||
| .flash_name = "gd25q_spi0", | ||
| .cs_pin = GET_PIN(A, 4), | ||
| }, | ||
| #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_SPI1 | ||
| { | ||
| .bus_name = "spi1", | ||
| .device_name = "spi10", | ||
| .flash_name = "gd25q_spi1", | ||
| .cs_pin = GET_PIN(B, 9), | ||
| }, | ||
| #endif | ||
|
|
||
| #ifdef BSP_USING_SPI2 | ||
| { | ||
| .bus_name = "spi2", | ||
| .device_name = "spi20", | ||
| .flash_name = "gd25q_spi2", | ||
| .cs_pin = GET_PIN(B, 12), | ||
| }, | ||
| #endif | ||
| res = rt_spi_bus_attach_device(&spi_dev_gd25q, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs); | ||
|
|
||
| if (res != RT_EOK) | ||
| { | ||
| rt_kprintf("rt_spi_bus_attach_device() run failed!\n"); | ||
| return res; | ||
| } | ||
| #ifdef BSP_USING_SPI3 | ||
| { | ||
| .bus_name = "spi3", | ||
| .device_name = "spi30", | ||
| .flash_name = "gd25q_spi3", | ||
| .cs_pin = GET_PIN(E, 4), | ||
| }, | ||
| #endif | ||
|
|
||
| return RT_EOK; | ||
| } | ||
| INIT_DEVICE_EXPORT(rt_hw_spi_flash_init); | ||
| #ifdef BSP_USING_SPI4 | ||
| { | ||
| .bus_name = "spi4", | ||
| .device_name = "spi40", | ||
| .flash_name = "gd25qsp", | ||
|
||
| .cs_pin = GET_PIN(F, 6), | ||
| }, | ||
| #endif | ||
| }; | ||
|
|
||
| #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 | ||
|
|
||
| #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); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这块格式化有点问题,建议重新刷一下格式