summaryrefslogtreecommitdiff
path: root/bsp/gd32/arm/libraries/gd32_drivers/drv_spi_flash.c
blob: 32b954c207132b28776a5168946b33c71e53341f (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
/*
 * 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
 */
#include <board.h>
#include "drv_spi.h"
#include "spi_flash.h"

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

#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   GPIOE
#define GD25Q_SPI_CS_GPIOX_PIN_X GPIO_PIN_3

static int rt_hw_spi_flash_init(void)
{
    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;

#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);

    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);

#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;
    }

    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)
{
    if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_CHIP, SPI_FLASH_DEVICE_NAME))
    {
        return RT_ERROR;
    };

    return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init)
#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");
    }

    return 0;
}
MSH_CMD_EXPORT(mnt_init, mount spi flash to file system);
#endif