summaryrefslogtreecommitdiff
path: root/bsp/hc32/platform/sfud/drv_spi_flash.c
blob: 67b4c849994aeafdc951d3be5b586cd7b9ffb584 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-04-28     CDT          first version
 * 2026-05-27     CDT          Support HC32F4A2
 * 2026-06-04     CDT          Support HC32F467
 */

#include <board.h>
#include <drv_spi.h>
#include <rtdevice.h>
#include <rthw.h>
#include <finsh.h>
#include <dfs_fs.h>
#include <fal.h>

#ifdef BSP_USING_SPI_FLASH

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

#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F448) || defined(HC32F4A8) || defined(HC32F467)
#define SPI_BUS_NAME          "spi1"
#define SPI_FLASH_DEVICE_NAME "spi10"
#define SPI_FLASH_CHIP        "w25q64"
#define SPI_FLASH_SS_PIN      GET_PIN(C, 7)
#elif defined(HC32F460)
#define SPI_BUS_NAME          "spi3"
#define SPI_FLASH_DEVICE_NAME "spi30"
#define SPI_FLASH_CHIP        "w25q64"
#define SPI_FLASH_SS_PIN      GET_PIN(C, 7)
#elif defined(HC32F472)
#define SPI_BUS_NAME          "spi1"
#define SPI_FLASH_DEVICE_NAME "spi10"
#define SPI_FLASH_CHIP        "w25q64"
#define SPI_FLASH_SS_PIN      GET_PIN(B, 12)
#elif defined(HC32F334)
#define SPI_BUS_NAME          "spi1"
#define SPI_FLASH_DEVICE_NAME "spi10"
#define SPI_FLASH_CHIP        "w25q64"
#define SPI_FLASH_SS_PIN      GET_PIN(C, 1)
#endif
#define SPI_FLASH_CMD_ENABLE_RESET 0x66
#define SPI_FLASH_CMD_RESET_DEVICE 0x99

/* Partition Name */
#define FS_PARTITION_NAME "filesystem"


#ifdef RT_USING_SFUD
static void rt_hw_spi_flash_reset(char *spi_dev_name)
{
    struct rt_spi_device *spi_dev_w25;
    rt_uint8_t w25_en_reset = SPI_FLASH_CMD_ENABLE_RESET;
    rt_uint8_t w25_reset_dev = SPI_FLASH_CMD_RESET_DEVICE;

    spi_dev_w25 = (struct rt_spi_device *)rt_device_find(spi_dev_name);
    if (!spi_dev_w25)
    {
        rt_kprintf("Can't find %s device!\n", spi_dev_name);
    }
    else
    {
        rt_spi_send(spi_dev_w25, &w25_en_reset, 1U);
        rt_spi_send(spi_dev_w25, &w25_reset_dev, 1U);
        DDL_DelayMS(1U);
        rt_kprintf("Reset ext flash!\n");
    }
}

static int rt_hw_spi_flash_with_sfud_init(void)
{
    rt_hw_spi_device_attach(SPI_BUS_NAME, SPI_FLASH_DEVICE_NAME, SPI_FLASH_SS_PIN);

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

static int rt_hw_fs_init(void)
{
    struct rt_device *mtd_dev = RT_NULL;

    /* 初始化 fal */
    fal_init();
    /* 生成 mtd 设备 */
    mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
    if (!mtd_dev)
    {
        LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
        return -RT_ERROR;
    }
    else
    {
        /* 挂载 littlefs */
        if (RT_EOK == dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0))
        {
            LOG_I("Filesystem initialized!");
            return RT_EOK;
        }
        else
        {
            /* 格式化文件系统 */
            if (RT_EOK == dfs_mkfs("lfs", FS_PARTITION_NAME))
            {
                /* 挂载 littlefs */
                if (RT_EOK == dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0))
                {
                    LOG_I("Filesystem initialized!");
                    return RT_EOK;
                }
                else
                {
                    LOG_E("Failed to initialize filesystem!");
                    return -RT_ERROR;
                }
            }
            else
            {
                LOG_E("Failed to Format fs!");
                return -RT_ERROR;
            }
        }
    }
}
INIT_APP_EXPORT(rt_hw_fs_init);

#endif /* RT_USING_SFUD */

#endif /* BSP_USING_SPI_FLASH */