-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,102 @@ | |
| #include <rthw.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 | ||
| 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 | ||
|
|
||
| #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); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The error handling in the for loop overwrites the result from each iteration. Consider aggregating the success state across multiple device attachments or revising the error reporting to ensure that a successful attachment isn't masked by a subsequent failure.