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
/***********************************************************************************************************************
* Copyright (C) All rights reserved.
***********************************************************************************************************************/
/***********************************************************************************************************************
* @file flash.c
* @brief This file implements flash sector erase and program.
* @version 1.0.0
* @date 2019/12/24
***********************************************************************************************************************/
/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "flash.h"
uint32_t flash_protect_flag = 0;
#if defined (__CC_ARM)
#pragma arm section code = "ram_fetch_code" // Arm Compiler 5
#elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION > 6010050)
#pragma clang section text = "ram_fetch_code" // Arm Compiler 6
#endif
/***********************************************************************************************************************
* Function Name: EraseChip
* @brief Chip Erase Flash
* @param adr - Any address of user code flash
* @return status: 0 - OK, 1 - Failed
***********************************************************************************************************************/
int EraseChip (uint32_t adr)
{
#ifdef FLASH_PROTCET
if(flash_protect_flag != PROTECT_VALUE)
{
return(1);
}
flash_protect_flag = 0;
#endif
__DI;
FMC->FLERMD = 0x08;
FMC->FLPROT = 0xF1;
FMC->FLOPMD1 = 0x55;
FMC->FLOPMD2 = 0xAA;
// Write data to start address of sector to trigger Erase Operation
*(uint32_t *) adr = 0xFFFFFFFF;
// polling OVER Flag
while((FMC->FLSTS & FMC_FLSTS_OVF_Msk) == 0);
FMC->FLSTS = FMC_FLSTS_OVF_Msk;
FMC->FLERMD = 0x00;
FMC->FLPROT = 0xF0;
__EI;
return(0);
}
/***********************************************************************************************************************
* Function Name: EraseSector
* @brief Sector Erase Flash
* @param adr - sector address of user code flash
* @return status: 0 - OK, 1 - Failed
***********************************************************************************************************************/
int EraseSector (uint32_t adr)
{
#ifdef FLASH_PROTCET
if(flash_protect_flag != PROTECT_VALUE)
{
return(1);
}
flash_protect_flag = 0;
#endif
__DI;
FMC->FLERMD = 0x10;
FMC->FLPROT = 0xF1;
FMC->FLOPMD1 = 0x55;
FMC->FLOPMD2 = 0xAA;
// Write data to start address of sector to trigger Erase Operation
*(uint32_t *) adr = 0xFFFFFFFF;
// polling Erase Over Flag
while((FMC->FLSTS & FMC_FLSTS_OVF_Msk) == 0);
FMC->FLSTS = FMC_FLSTS_OVF_Msk;
FMC->FLERMD = 0x00;
FMC->FLPROT = 0xF0;
if(FMC->FLSTS & FMC_FLSTS_EVF_Msk)
{
FMC->FLSTS = FMC_FLSTS_EVF_Msk;
__EI;
return(1); /* verify ng */
}
else
{
__EI;
return(0); /* verify ok */
}
}
/***********************************************************************************************************************
* Function Name: ProgramPage
* @brief Write data to Flash
* @param adr - Page Start Address
* @param sz - Page Size
* @param buf - Page Data
* @return status: 0 - OK, 1 - Failed
***********************************************************************************************************************/
int ProgramPage (uint32_t adr, uint32_t sz, uint8_t *buf)
{
uint32_t i;
uint8_t *ptr;
ptr = (uint8_t *) adr;
#ifdef FLASH_PROTCET
if(flash_protect_flag != PROTECT_VALUE)
{
return(1);
}
flash_protect_flag = 0;
#endif
FMC->FLPROT = 0xF1;
for(i=0; i<sz; i++)
{
__DI;
FMC->FLOPMD1 = 0xAA;
FMC->FLOPMD2 = 0x55;
*ptr++ = *buf++;
__EI;
// polling OVER Flag
while((FMC->FLSTS & FMC_FLSTS_OVF_Msk) == 0);
FMC->FLSTS = FMC_FLSTS_OVF_Msk;
}
FMC->FLPROT = 0xF0;
return (0);
}
/***********************************************************************************************************************
* Function Name: flash_write
* @brief Write data to Flash area. Automatically determines if erasure is needed.
* If the destination area is blank, omits Erase and Pragram directly.
* Otherwise, save data of flash to ram, erase flash, and then write flash.
* @param adr - Page Start Address
* @param sz - Page Size
* @param buf - Page Data
* @return status - MD_OK or MD_ERROR
* @note sz must be less then or equal to SECTOR_SIZE, if not, return MD_ERROR and don't change flash contents.
***********************************************************************************************************************/
FLASH_STATUS flash_write(uint32_t adr, uint32_t sz, uint8_t *buf)
{
FLASH_STATUS status = FS_OK;
uint8_t dat, tmp[SECTOR_SIZE *2];
uint8_t blank = 1;
uint8_t cross;
uint8_t *ptr;
uint8_t *ptr_base; /* sector base address */
uint16_t offset;
uint16_t i;
uint32_t sector_num;
#ifdef FLASH_PROTCET
if(flash_protect_flag != PROTECT_VALUE)
{
return(FS_ERROR);
}
flash_protect_flag = 0;
#endif
if(sz > SECTOR_SIZE)
{
status = FS_ERROR;
return(status);
}
ptr = (uint8_t *)adr;
/* Determine if the target area is blank */
for(i=0; i<sz; i++)
{
dat = *ptr++;
if(dat != 0xFFU)
{
blank = 0; /* Not blank */
}
}
/* if it is blank, omits erase and program directlly. */
if(blank)
{
/* write data to flash data */
flash_protect_flag = PROTECT_VALUE;
ProgramPage(adr, sz, buf);
/* if it isn't blank, erase and then program */
}
else
{
sector_num = (adr & ~(SECTOR_SIZE-1)) >> 9;
if((adr+sz) > ((sector_num+1) << 9))
{
cross = 1; /* write area cross sectors */
}
else
{
cross = 0;
}
ptr_base = (uint8_t *)(adr & ~(SECTOR_SIZE-1)); /* get sector base address: Each sector is 512 bytes (i.e. 128 words) */
offset = adr & (SECTOR_SIZE-1); /* get offset */
/* Save the Flash data temporarily */
if(cross)
{
/* read two sectors */
for(i = 0; i < SECTOR_SIZE * 2; i++)
{
tmp[i] = *ptr_base++;
}
}
else
{
/* read one sector */
for(i = 0; i < SECTOR_SIZE; i++)
{
tmp[i] = *ptr_base++;
}
}
/* replace flash data with write data */
for(i = 0; i < sz; i++)
{
tmp[offset+i] = *buf++;
}
/* write data to flash data */
if(cross)
{
flash_protect_flag = PROTECT_VALUE;
EraseSector((sector_num + 0 ) << 9);
flash_protect_flag = PROTECT_VALUE;
EraseSector((sector_num + 1 ) << 9);
flash_protect_flag = PROTECT_VALUE;
ProgramPage(sector_num << 9, SECTOR_SIZE * 2, tmp);
}
else
{
flash_protect_flag = PROTECT_VALUE;
EraseSector((sector_num + 0 ) << 9);
flash_protect_flag = PROTECT_VALUE;
ProgramPage(sector_num << 9, SECTOR_SIZE * 1, tmp);
}
}
return (status);
}
void flashRead(uint8_t *read_address, uint16_t size, uint8_t *readbuf)
{
uint8_t *w_ptr;
uint32_t i;
w_ptr = (uint8_t *)read_address;
for (i = 0; i < size; i++)
{
*readbuf = *w_ptr++;
readbuf++;
}
}
#if defined (__CC_ARM)
#pragma arm section code // Arm Compiler 5
#elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION > 6010050)
#pragma clang section text = "" // Arm Compiler 6
#endif