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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-11-30 flybreak first version
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "board.h"
#include "hal_data.h"
#include "drv_common.h"
#if defined(RT_USING_FAL)
#include "fal.h"
#endif
//#define DRV_DEBUG
#define LOG_TAG "drv.flash"
#ifdef DRV_DEBUG
#define DBG_LVL DBG_LOG
#else
#define DBG_LVL DBG_INFO
#endif /* DRV_DEBUG */
#include <rtdbg.h>
#if BSP_FEATURE_FLASH_HP_VERSION
/* FLASH API */
#define R_FLASH_Open R_FLASH_HP_Open
#define R_FLASH_Reset R_FLASH_HP_Reset
#define R_FLASH_Write R_FLASH_HP_Write
#define R_FLASH_Erase R_FLASH_HP_Erase
#define R_FLASH_StartUpAreaSelect R_FLASH_HP_StartUpAreaSelect
/* BSP_FEATURE_FLASH */
#define FLASH_CF_WRITE_SIZE BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE
#else /* FLASH LP */
/* FLASH API */
#define R_FLASH_Open R_FLASH_LP_Open
#define R_FLASH_Reset R_FLASH_LP_Reset
#define R_FLASH_Write R_FLASH_LP_Write
#define R_FLASH_Erase R_FLASH_LP_Erase
#define R_FLASH_StartUpAreaSelect R_FLASH_LP_StartUpAreaSelect
/* BSP_FEATURE_FLASH */
#define FLASH_CF_WRITE_SIZE BSP_FEATURE_FLASH_LP_CF_WRITE_SIZE
#endif
int _flash_init(void)
{
fsp_err_t err = FSP_SUCCESS;
/* Open Flash_HP */
err = R_FLASH_Open(&g_flash_ctrl, &g_flash_cfg);
/* Handle Error */
if (FSP_SUCCESS != err)
{
LOG_E("\r\n Flah_HP_Open API failed");
}
/* Setup Default Block 0 as Startup Setup Block */
err = R_FLASH_StartUpAreaSelect(&g_flash_ctrl, FLASH_STARTUP_AREA_BLOCK0, true);
if (err != FSP_SUCCESS)
{
LOG_E("\r\n Flah_HP_StartUpAreaSelect API failed");
}
return 0;
}
/**
* Read data from flash.
* @note This operation's units is word.
*
* @param addr flash address
* @param buf buffer to store read data
* @param size read bytes size
*
* @return result
*/
int _flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
{
size_t i;
for (i = 0; i < size; i++, buf++, addr++)
{
*buf = *(rt_uint8_t *) addr;
}
return size;
}
/**
* Write data to flash.
* @note This operation's units is word.
* @note This operation must after erase. @see flash_erase.
*
* @param addr flash address
* @param buf the write data buffer
* @param size write bytes size
*
* @return result
*/
int _flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
{
rt_err_t result = RT_EOK;
rt_base_t level;
fsp_err_t err = FSP_SUCCESS;
size_t written_size = 0;
if (size % FLASH_CF_WRITE_SIZE)
{
LOG_E("Flash Write size must be an integer multiple of %d", FLASH_CF_WRITE_SIZE);
}
while (written_size < size)
{
level = rt_hw_interrupt_disable();
R_FLASH_Reset(&g_flash_ctrl);
/* Write code flash data*/
err = R_FLASH_Write(&g_flash_ctrl, (uint32_t)(buf + written_size), addr + written_size, FLASH_CF_WRITE_SIZE);
rt_hw_interrupt_enable(level);
/* Error Handle */
if (FSP_SUCCESS != err)
{
LOG_E("Write API failed");
return -RT_EIO;
}
written_size += FLASH_CF_WRITE_SIZE;
}
if (result != RT_EOK)
{
return result;
}
return size;
}
typedef bool (*block_func)(int no, size_t addr, size_t size, void *parm1, void *parm2, void *parm3);
void fal_block_iter(const struct fal_flash_dev *flash, block_func func, void *parm1, void *parm2, void *parm3)
{
int no = 0;
size_t addr = flash->addr;
for (int i = 0; i < FAL_DEV_BLK_MAX; i++)
{
/* blocks[i] */
const struct flash_blk *cur_blk = &flash->blocks[i];
if (cur_blk->size == 0 || cur_blk->count == 0)
{
break;
}
int j = cur_blk->count;
while (j--)
{
/* block_no */
if (func(no, addr, cur_blk->size, parm1, parm2, parm3) == true)
{
return;
}
addr += cur_blk->size;
no ++;
}
}
}
bool calculate_block_num(int no, size_t addr, size_t size, void *erase_addr, void *erase_size, void *number)
{
rt_uint32_t e_addr = (rt_uint32_t)erase_addr;
size_t e_size = (size_t)erase_size;
int *i = (int *)number;
LOG_D("block_num: no: %d, addr is (0x%p), size: %d\n, i:%d", no, addr, size, *i);
LOG_D("erase_addr: (0x%p), erase_size: %d\n", e_addr, e_size);
if (e_addr >= addr && e_addr < addr + size)
{
(*i) ++;
return false;
}
else if (addr > e_addr && addr < e_addr + e_size)
{
(*i) ++;
return false;
}
else if (addr >= e_addr + e_size)
{
return true;
}
return false;
}
/**
* Erase data on flash.
* @note This operation is irreversible.
* @note This operation's units is different which on many chips.
*
* @param addr flash address
* @param size erase bytes size
*
* @return result
*/
#if BSP_FEATURE_FLASH_HP_VERSION
int _flash_hp0_erase(rt_uint32_t addr, size_t size)
#else
int _flash_lp_erase(rt_uint32_t addr, size_t size)
#endif
{
fsp_err_t err = FSP_SUCCESS;
rt_base_t level;
int block_num = 0;
if ((addr + size) > BSP_FEATURE_FLASH_CODE_FLASH_START + BSP_ROM_SIZE_BYTES)
{
LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
return -RT_EINVAL;
}
if (size < 1)
{
return -RT_EINVAL;
}
level = rt_hw_interrupt_disable();
R_FLASH_Reset(&g_flash_ctrl);
#if BSP_FEATURE_FLASH_HP_VERSION
fal_block_iter(&_onchip_flash_hp0, &calculate_block_num, (void *)addr, (void *)size, &block_num);
#else
fal_block_iter(&_onchip_flash_lp, &calculate_block_num, (void *)addr, (void *)size, &block_num);
#endif
/* Erase Block */
#if BSP_FEATURE_FLASH_HP_VERSION
err = R_FLASH_Erase(&g_flash_ctrl,
RT_ALIGN_DOWN(addr, BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE),
block_num);
#else
err = R_FLASH_Erase(&g_flash_ctrl,
RT_ALIGN_DOWN(addr, BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE),
((size - 1) / BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE + 1));
#endif
rt_hw_interrupt_enable(level);
if (err != FSP_SUCCESS)
{
LOG_E("Erase failed:addr (0x%p), size %d", (void *)addr, size);
return -RT_EIO;
}
LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
return size;
}
#if defined(RT_USING_FAL)
#if BSP_FEATURE_FLASH_HP_VERSION
static int fal_flash_hp0_read(long offset, rt_uint8_t *buf, size_t size);
static int fal_flash_hp0_write(long offset, const rt_uint8_t *buf, size_t size);
static int fal_flash_hp0_erase(long offset, size_t size);
const struct fal_flash_dev _onchip_flash_hp0 =
{
"onchip_flash_hp0",
BSP_FEATURE_FLASH_CODE_FLASH_START,
BSP_ROM_SIZE_BYTES,
BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE,
{
_flash_init,
fal_flash_hp0_read,
fal_flash_hp0_write,
fal_flash_hp0_erase
},
(BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE * 8)
, {
{
.size = BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE,
.count = BSP_FEATURE_FLASH_HP_CF_REGION0_SIZE / BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE
},
{
.size = BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE,
.count = (BSP_ROM_SIZE_BYTES - BSP_FEATURE_FLASH_HP_CF_REGION0_SIZE) / BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE
},
}
};
/* code flash region0 */
static int fal_flash_hp0_read(long offset, rt_uint8_t *buf, size_t size)
{
return _flash_read(_onchip_flash_hp0.addr + offset, buf, size);
}
static int fal_flash_hp0_write(long offset, const rt_uint8_t *buf, size_t size)
{
return _flash_write(_onchip_flash_hp0.addr + offset, buf, size);
}
static int fal_flash_hp0_erase(long offset, size_t size)
{
return _flash_hp0_erase(_onchip_flash_hp0.addr + offset, size);
}
#else /* flash lp code flash */
static int fal_flash_lp_read(long offset, rt_uint8_t *buf, size_t size);
static int fal_flash_lp_write(long offset, const rt_uint8_t *buf, size_t size);
static int fal_flash_lp_erase(long offset, size_t size);
const struct fal_flash_dev _onchip_flash_lp =
{
"onchip_flash_lp",
FLASH_START_ADDRESS,
BSP_ROM_SIZE_BYTES,
BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE,
{
_flash_init,
fal_flash_lp_read,
fal_flash_lp_write,
fal_flash_lp_erase
},
(BSP_FEATURE_FLASH_LP_CF_WRITE_SIZE * 8)
};
static int fal_flash_lp_read(long offset, rt_uint8_t *buf, size_t size)
{
return _flash_read(_onchip_flash_lp.addr + offset, buf, size);
}
static int fal_flash_lp_write(long offset, const rt_uint8_t *buf, size_t size)
{
return _flash_write(_onchip_flash_lp.addr + offset, buf, size);
}
static int fal_flash_lp_erase(long offset, size_t size)
{
return _flash_lp_erase(_onchip_flash_lp.addr + offset, size);
}
/*
* This callback is intentionally left empty.
* No action is required on flash events in this application.
* If event handling is needed, implement it here.
*/
void flash_callback(flash_callback_args_t * p_args)
{
}
#endif
#endif
|