1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
30
35
36
37
42
46
47
52
53
54
55
56
57
58
59
60
64
68
69
70
71
72
78
79
80
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
118
119
124
128
129
130
131
132
137
138
139
140
141
145
149
153
157
161
165
169
173
177
181
185
189
190
/* ... */
#include "main.h"
Includes
uint32_t FirstSector = 0;
uint32_t NbOfSectors = 0;
uint32_t SectorError = 0;
uint32_t OB_RDP_LEVEL;
Private variables
static uint32_t FLASH_If_GetSectorNumber(uint32_t Address);
static FLASH_OBProgramInitTypeDef FLASH_OBProgramInitStruct;
static FLASH_EraseInitTypeDef FLASH_EraseInitStruct;
Private function prototypes
/* ... */
void FLASH_If_FlashUnlock(void)
{
HAL_FLASH_Unlock();
}{ ... }
/* ... */
FlagStatus FLASH_If_ReadOutProtectionStatus(void)
{
FlagStatus readoutstatus = RESET;
FLASH_OBProgramInitStruct.RDPLevel = OB_RDP_LEVEL;
HAL_FLASHEx_OBGetConfig(&FLASH_OBProgramInitStruct);
if(OB_RDP_LEVEL == SET)
{
readoutstatus = SET;
}if (OB_RDP_LEVEL == SET) { ... }
else
{
readoutstatus = RESET;
}else { ... }
return readoutstatus;
}{ ... }
/* ... */
uint32_t FLASH_If_EraseSectors(uint32_t Address)
{
/* ... */
if(Address <= (uint32_t) USER_FLASH_LAST_PAGE_ADDRESS)
{
FirstSector = FLASH_If_GetSectorNumber(Address);
NbOfSectors = FLASH_If_GetSectorNumber(USER_FLASH_LAST_PAGE_ADDRESS) - FirstSector + 1;
FLASH_EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
FLASH_EraseInitStruct.Sector = FirstSector;
FLASH_EraseInitStruct.NbSectors = NbOfSectors;
FLASH_EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
if(HAL_FLASHEx_Erase(&FLASH_EraseInitStruct, &SectorError) != HAL_OK)
return (1);
}if (Address <= (uint32_t) USER_FLASH_LAST_PAGE_ADDRESS) { ... }
else
{
return (1);
}else { ... }
return (0);
}{ ... }
/* ... */
uint32_t FLASH_If_Write(uint32_t Address, uint32_t Data)
{
/* ... */
if(Address <= (uint32_t) USER_FLASH_LAST_PAGE_ADDRESS)
{
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, Data)!= HAL_OK)
return (1);
}if (Address <= (uint32_t) USER_FLASH_LAST_PAGE_ADDRESS) { ... }
else
{
return (1);
}else { ... }
return (0);
}{ ... }
/* ... */
static uint32_t FLASH_If_GetSectorNumber(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;
}{ ... }