1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
22
23
26
27
28
29
30
33
34
35
40
48
49
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
119
120
126
127
128
129
130
131
132
133
134
135
136
137
139
140
141
142
143
144
145
150
155
156
157
162
163
164
165
166
170
174
178
182
186
190
194
198
202
206
210
214
215
216
217
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
/* ... */
/* ... */
#include "flash_if.h"
Includes
static uint32_t GetSector(uint32_t Address);
Private function prototypes
/* ... */
void FLASH_If_Init(void)
{
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
}{ ... }
/* ... */
uint32_t FLASH_If_Erase(uint32_t StartSector)
{
uint32_t UserStartSector;
uint32_t SectorError;
FLASH_EraseInitTypeDef pEraseInit;
FLASH_If_Init();
UserStartSector = GetSector(APPLICATION_ADDRESS);
pEraseInit.TypeErase = TYPEERASE_SECTORS;
pEraseInit.Sector = UserStartSector;
pEraseInit.NbSectors = 10;
pEraseInit.VoltageRange = VOLTAGE_RANGE_3;
if (HAL_FLASHEx_Erase(&pEraseInit, &SectorError) != HAL_OK)
{
return (1);
}if (HAL_FLASHEx_Erase(&pEraseInit, &SectorError) != HAL_OK) { ... }
return (0);
}{ ... }
/* ... */
uint32_t FLASH_If_Write(uint32_t FlashAddress, uint32_t* Data ,uint32_t DataLength)
{
uint32_t i = 0;
for (i = 0; (i < DataLength) && (FlashAddress <= (USER_FLASH_END_ADDRESS-4)); i++)
{
/* ... */
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, FlashAddress, *(uint32_t*)(Data+i)) == HAL_OK)
{
if (*(uint32_t*)FlashAddress != *(uint32_t*)(Data+i))
{
return(FLASHIF_WRITINGCTRL_ERROR);
}if (*(uint32_t*)FlashAddress != *(uint32_t*)(Data+i)) { ... }
FlashAddress += 4;
}if (HAL_FLASH_Program(TYPEPROGRAM_WORD, FlashAddress, *(uint32_t*)(Data+i)) == HAL_OK) { ... }
else
{
return (FLASHIF_WRITING_ERROR);
}else { ... }
}for (i = 0; (i < DataLength) && (FlashAddress <= (USER_FLASH_END_ADDRESS-4)); i++) { ... }
return (FLASHIF_OK);
}{ ... }
/* ... */
uint16_t FLASH_If_GetWriteProtectionStatus(void)
{
uint32_t ProtectedSECTOR = 0xFFF;
FLASH_OBProgramInitTypeDef OptionsBytesStruct;
HAL_FLASH_Unlock();
HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
/* ... */
HAL_FLASH_Lock();
ProtectedSECTOR = ~(OptionsBytesStruct.WRPSector) & FLASH_SECTOR_TO_BE_PROTECTED;
if(ProtectedSECTOR != 0)
{
return FLASHIF_PROTECTION_WRPENABLED;
}if (ProtectedSECTOR != 0) { ... }
else
{
return FLASHIF_PROTECTION_NONE;
}else { ... }
}{ ... }
/* ... */
static uint32_t GetSector(uint32_t Address)
{
uint32_t sector = 0;
if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
{
sector = FLASH_SECTOR_0;
}if ((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0)) { ... }
else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
{
sector = FLASH_SECTOR_1;
}else if ((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1)) { ... }
else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
{
sector = FLASH_SECTOR_2;
}else if ((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2)) { ... }
else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
{
sector = FLASH_SECTOR_3;
}else if ((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3)) { ... }
else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
{
sector = FLASH_SECTOR_4;
}else if ((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4)) { ... }
else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
{
sector = FLASH_SECTOR_5;
}else if ((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5)) { ... }
else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
{
sector = FLASH_SECTOR_6;
}else if ((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6)) { ... }
else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
{
sector = FLASH_SECTOR_7;
}else if ((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7)) { ... }
else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
{
sector = FLASH_SECTOR_8;
}else if ((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8)) { ... }
else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
{
sector = FLASH_SECTOR_9;
}else if ((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9)) { ... }
else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
{
sector = FLASH_SECTOR_10;
}else if ((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10)) { ... }
else
{
sector = FLASH_SECTOR_11;
}else { ... }
return sector;
}{ ... }
/* ... */
HAL_StatusTypeDef FLASH_If_WriteProtectionConfig(uint32_t modifier)
{
uint32_t ProtectedSECTOR = 0xFFF;
FLASH_OBProgramInitTypeDef config_new, config_old;
HAL_StatusTypeDef result = HAL_OK;
HAL_FLASHEx_OBGetConfig(&config_old);
config_new.WRPState = modifier;
config_new.OptionType = OPTIONBYTE_WRP;
config_new.RDPLevel = OB_RDP_LEVEL_0;
config_new.USERConfig = config_old.USERConfig;
ProtectedSECTOR = config_old.WRPSector | FLASH_SECTOR_TO_BE_PROTECTED;
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
config_new.WRPSector = ProtectedSECTOR;
result = HAL_FLASHEx_OBProgram(&config_new);
return result;
}{ ... }
/* ... */