summaryrefslogtreecommitdiff
path: root/bsp/gd32/arm/libraries/gd32_drivers/drv_spi_flash.c
blob: 99f36c81d966f40878b9fb293832893859e82c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * 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
 * 2025-11-28     godmial      add configurable SPI Flash initialization
 *                             Only initialize flash on SPI buses explicitly configured
 *                             via BSP_USING_SPIx_FLASH options to avoid conflicts
 *                             with other SPI devices (e.g., OLED, WIFI)
 */
#include <board.h>
#include "drv_spi.h"
#include "dev_spi_flash.h"

#ifdef RT_USING_SFUD
#include "dev_spi_flash_sfud.h"
#endif

#include <rthw.h>
#include <finsh.h>

#ifdef RT_USING_DFS
#include <dfs_fs.h>
#endif

struct spi_flash_config
{
    const char *bus_name;
    const char *device_name;
    const char *flash_name;
    rt_base_t   cs_pin;
};

static const struct spi_flash_config flash_configs[] =
{
#if defined(BSP_USING_SPI0) && defined(BSP_USING_SPI0_FLASH)
    {
        .bus_name    = "spi0",
        .device_name = "spi00",
        .flash_name  = "gd25q_spi0",
        .cs_pin      = GET_PIN(A, 4),
    },
#endif

#if defined(BSP_USING_SPI1) && defined(BSP_USING_SPI1_FLASH)
    {
        .bus_name    = "spi1",
        .device_name = "spi10",
        .flash_name  = "gd25q_spi1",
        .cs_pin      = GET_PIN(B, 9),
    },
#endif

#if defined(BSP_USING_SPI2) && defined(BSP_USING_SPI2_FLASH)
    {
        .bus_name    = "spi2",
        .device_name = "spi20",
        .flash_name  = "gd25q_spi2",
        .cs_pin      = GET_PIN(B, 12),
    },
#endif

#if defined(BSP_USING_SPI3) && defined(BSP_USING_SPI3_FLASH)
    {
        .bus_name    = "spi3",
        .device_name = "spi30",
        .flash_name  = "gd25q_spi3",
        .cs_pin      = GET_PIN(E, 4),
    },
#endif

#if defined(BSP_USING_SPI4) && defined(BSP_USING_SPI4_FLASH)
    {
        .bus_name    = "spi4",
        .device_name = "spi40",
        .flash_name  = "gd25q_spi4",
        .cs_pin      = GET_PIN(F, 6),
    },
#endif

#if defined(BSP_USING_SPI5) && defined(BSP_USING_SPI5_FLASH)
    {
        .bus_name    = "spi5",
        .device_name = "spi50",
        .flash_name  = "gd25q_spi5",
        .cs_pin      = GET_PIN(F, 6),  /* Note: Update CS pin according to actual hardware */
    },
#endif
};


static int spi_flash_init(void)
{
#ifdef BSP_USING_SPI_FLASH
    int result = RT_EOK;

    for (size_t i = 0; i < sizeof(flash_configs) / sizeof(flash_configs[0]); i++)
    {
        const struct spi_flash_config *cfg = &flash_configs[i];

        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
    }

    return result;
#else
    /* SPI Flash auto-initialization is disabled. User should initialize SPI Flash manually in board code. */
    return RT_EOK;
#endif
}
INIT_COMPONENT_EXPORT(spi_flash_init);