summaryrefslogtreecommitdiff
path: root/bsp/hc32/libraries/hc32_drivers/drv_flash/drv_flash_f4.c
blob: 48bb75aaa5c72a2066a3a5b7d2d9907729b33251 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * 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
 * 2024-06-14       CDT         Fixed sector number calculation
 * 2024-06-18       CDT         Support HC32F460,HC32F448,HC32F472
 * 2025-07-16       CDT         Support HC32F334
 * 2026-05-27       CDT         Support HC32F4A2
 * 2026-06-03       CDT         Support HC32F467
 */

#include "board.h"

#if defined(BSP_USING_ON_CHIP_FLASH)
#include "drv_config.h"
#include "drv_flash.h"

#if defined(RT_USING_FAL)
#include "fal.h"
#endif

//#define DRV_DEBUG
#define LOG_TAG "drv.flash"
#include <drv_log.h>

/**
 * @brief  Gets the sector number.
 * @param  addr                         flash address
 * @param  size                         erase bytes size
 * @retval Sector number.
 */
static rt_uint32_t GetSectorNum(rt_uint32_t addr, size_t size)
{
    rt_uint32_t firstSector = 0, lastSector = 0;
    rt_uint32_t NumOfSectors = 0;

    firstSector = addr / EFM_SECTOR_SIZE;
    lastSector = (addr + size - 1U) / EFM_SECTOR_SIZE;
    NumOfSectors = lastSector - firstSector + 1U;

    return NumOfSectors;
}

/**
 * @brief Read data from flash.
 * @param addr                          flash address
 * @param buf                           buffer to store read data
 * @param size                          read bytes size
 * @return result
 */
int hc32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
{
    if ((addr + size) > HC32_FLASH_END_ADDRESS)
    {
        LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
        return -1;
    }

    if (LL_OK != EFM_ReadByte(addr, buf, size))
    {
        return -RT_ERROR;
    }

    return size;
}

/**
 * @brief  Write data to flash.
 * @param  addr                         flash address
 * @param  buf                          the write data buffer
 * @param  size                         write bytes size
 * @return result
 * @note   This operation's units is word.
 * @note   This operation must after erase.
 */
int hc32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
{
    uint8_t u8MemBuf[HC32_FLASH_WRITE_GRANULARITY];
    rt_err_t result = RT_EOK;
    rt_uint32_t newAddr = addr, offsetVal = 0;
    rt_uint32_t index = 0, u32Cnt = 0;
#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F472) || defined(HC32F448) || defined(HC32F4A8) || \
    defined(HC32F334) || defined(HC32F467)
    rt_uint32_t FirstSector = 0, NumOfSectors = 0;
#endif

    if ((addr + size) > HC32_FLASH_END_ADDRESS)
    {
        LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
        return -RT_EINVAL;
    }
    if (size < 1)
    {
        return -RT_EINVAL;
    }

    for (u32Cnt = 0; u32Cnt < HC32_FLASH_WRITE_GRANULARITY; u32Cnt++)
    {
        u8MemBuf[u32Cnt] = 0xFF;
    }
    /* EFM_FWMC write enable */
    EFM_FWMC_Cmd(ENABLE);
#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F472) || defined(HC32F448) || defined(HC32F4A8) || \
    defined(HC32F334) || defined(HC32F467)
    /* calculate sector information */
    FirstSector = addr / EFM_SECTOR_SIZE,
    NumOfSectors = GetSectorNum(addr, size);
    /* Sectors disable write protection */
    EFM_SequenceSectorOperateCmd(FirstSector, NumOfSectors, ENABLE);
#endif
    /* Word align */
    if (0U != (addr % HC32_FLASH_WRITE_GRANULARITY))
    {
        newAddr = (addr / HC32_FLASH_WRITE_GRANULARITY + 1U) * HC32_FLASH_WRITE_GRANULARITY;
        offsetVal = newAddr - addr;
        if (offsetVal >= size)
        {
            result = -RT_ERROR;
            index = HC32_FLASH_WRITE_GRANULARITY - offsetVal;
            if (LL_OK == EFM_ReadByte(newAddr - HC32_FLASH_WRITE_GRANULARITY, u8MemBuf, index))
            {
                for (u32Cnt = 0; u32Cnt < size; u32Cnt++)
                {
                    u8MemBuf[index + u32Cnt] = buf[u32Cnt];
                }
                /* program */
                if (LL_OK == EFM_Program(newAddr - HC32_FLASH_WRITE_GRANULARITY, u8MemBuf, HC32_FLASH_WRITE_GRANULARITY))
                {
                    result = RT_EOK;
                }
            }
            goto __exit;
        }
        else
        {
            size = size - offsetVal;
        }
    }
    /* program */
    if (LL_OK != EFM_Program(newAddr, (uint8_t *)&buf[offsetVal], size))
    {
        result = -RT_ERROR;
        goto __exit;
    }

__exit:
#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F472) || defined(HC32F448) || defined(HC32F4A8) || \
    defined(HC32F334) || defined(HC32F467)
    /* Sectors enable write protection */
    EFM_SequenceSectorOperateCmd(FirstSector, NumOfSectors, DISABLE);
#endif
    EFM_FWMC_Cmd(DISABLE);

    if (result != RT_EOK)
    {
        return result;
    }

    return size;
}

/**
 * @brief  Erase data on flash.
 * @param  addr                         flash address
 * @param  size                         erase bytes size
 * @return result
 * @note   This operation is irreversible.
 */
int hc32_flash_erase(rt_uint32_t addr, size_t size)
{
    rt_err_t result = RT_EOK;
    rt_uint32_t NumOfSectors = 0;
    rt_uint32_t SectorVal = 0, u32Addr = addr;
#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F472) || defined(HC32F448) || defined(HC32F4A8) || \
    defined(HC32F334) || defined(HC32F467)
    rt_uint32_t FirstSector = 0;
#endif

    if ((addr + size) > HC32_FLASH_END_ADDRESS)
    {
        LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
        return -RT_EINVAL;
    }
    if (size < 1)
    {
        return -RT_EINVAL;
    }

    /* EFM_FWMC write enable */
    EFM_FWMC_Cmd(ENABLE);
    /* calculate sector information */
    NumOfSectors = GetSectorNum(addr, size);
#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F472) || defined(HC32F448) || defined(HC32F4A8) || \
    defined(HC32F334) || defined(HC32F467)
    FirstSector = addr / EFM_SECTOR_SIZE,
    /* Sectors disable write protection */
        EFM_SequenceSectorOperateCmd(FirstSector, NumOfSectors, ENABLE);
#endif
    /* Erase sector */
    for (SectorVal = 0U; SectorVal < NumOfSectors; SectorVal++)
    {
        if (LL_OK != EFM_SectorErase(u32Addr))
        {
            result = -RT_ERROR;
            break;
        }
        u32Addr += EFM_SECTOR_SIZE;
    }
#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F472) || defined(HC32F448) || defined(HC32F4A8) || \
    defined(HC32F334) || defined(HC32F467)
    /* Sectors enable write protection */
    EFM_SequenceSectorOperateCmd(FirstSector, NumOfSectors, DISABLE);
#endif
    EFM_FWMC_Cmd(DISABLE);

    if (result != RT_EOK)
    {
        return result;
    }

    LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
    return size;
}

#if defined(RT_USING_FAL)
static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
static int fal_flash_erase(long offset, size_t size);

const struct fal_flash_dev hc32_onchip_flash = {
    .name = "onchip_flash",
    .addr = HC32_FLASH_START_ADDRESS,
    .len = HC32_FLASH_SIZE,
    .blk_size = HC32_FLASH_ERASE_GRANULARITY,
    .ops = { NULL, fal_flash_read, fal_flash_write, fal_flash_erase },
    .write_gran = HC32_FLASH_WRITE_GRANULARITY
};

static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
{
    return hc32_flash_read(hc32_onchip_flash.addr + offset, buf, size);
}

static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
{
    return hc32_flash_write(hc32_onchip_flash.addr + offset, buf, size);
}

static int fal_flash_erase(long offset, size_t size)
{
    return hc32_flash_erase(hc32_onchip_flash.addr + offset, size);
}
#endif

#endif /* BSP_USING_ON_CHIP_FLASH */