Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "flash_if.h"
Private function prototypes
FLASH_If_Init()
FLASH_If_Erase(uint32_t)
FLASH_If_Write(uint32_t, uint32_t *, uint32_t)
FLASH_If_GetWriteProtectionStatus()
GetSector(uint32_t)
FLASH_If_WriteProtectionConfig(uint32_t)
Files
loading...
CodeScopeSTM32 Libraries and SamplesIAP_Mainsrc/flash_if.c
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file IAP/IAP_Main/Src/flash_if.c * @author MCD Application Team * @brief This file provides all the memory related operation functions. ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /** @addtogroup STM32F4xx_IAP_Main * @{ *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "flash_if.h" Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ static uint32_t GetSector(uint32_t Address); Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief Unlocks Flash for write access * @param None * @retval None *//* ... */ void FLASH_If_Init(void) { HAL_FLASH_Unlock(); /* Clear pending flags (if any) */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); }{ ... } /** * @brief This function does an erase of all user flash area * @param StartSector: start of user flash area * @retval 0: user flash area successfully erased * 1: error occurred *//* ... */ uint32_t FLASH_If_Erase(uint32_t StartSector) { uint32_t UserStartSector; uint32_t SectorError; FLASH_EraseInitTypeDef pEraseInit; /* Unlock the Flash to enable the flash control register access *************/ FLASH_If_Init(); /* Get the sector where start the user flash area */ 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) { /* Error occurred while page erase */ return (1); }if (HAL_FLASHEx_Erase(&pEraseInit, &SectorError) != HAL_OK) { ... } return (0); }{ ... } /** * @brief This function writes a data buffer in flash (data are 32-bit aligned). * @note After writing data buffer, the flash content is checked. * @param FlashAddress: start address for writing data buffer * @param Data: pointer on data buffer * @param DataLength: length of data buffer (unit is 32-bit word) * @retval 0: Data successfully written to Flash memory * 1: Error occurred while writing data in Flash memory * 2: Written Data in flash memory is different from expected one *//* ... */ 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++) { /* Device voltage range supposed to be [2.7V to 3.6V], the operation will be done by word *//* ... */ if (HAL_FLASH_Program(TYPEPROGRAM_WORD, FlashAddress, *(uint32_t*)(Data+i)) == HAL_OK) { /* Check the written value */ if (*(uint32_t*)FlashAddress != *(uint32_t*)(Data+i)) { /* Flash content doesn't match SRAM content */ return(FLASHIF_WRITINGCTRL_ERROR); }if (*(uint32_t*)FlashAddress != *(uint32_t*)(Data+i)) { ... } /* Increment FLASH destination address */ FlashAddress += 4; }if (HAL_FLASH_Program(TYPEPROGRAM_WORD, FlashAddress, *(uint32_t*)(Data+i)) == HAL_OK) { ... } else { /* Error occurred while writing data in Flash memory */ return (FLASHIF_WRITING_ERROR); }else { ... } }for (i = 0; (i < DataLength) && (FlashAddress <= (USER_FLASH_END_ADDRESS-4)); i++) { ... } return (FLASHIF_OK); }{ ... } /** * @brief Returns the write protection status of user flash area. * @param None * @retval 0: No write protected sectors inside the user flash area * 1: Some sectors inside the user flash area are write protected *//* ... */ uint16_t FLASH_If_GetWriteProtectionStatus(void) { uint32_t ProtectedSECTOR = 0xFFF; FLASH_OBProgramInitTypeDef OptionsBytesStruct; /* Unlock the Flash to enable the flash control register access *************/ HAL_FLASH_Unlock(); /* Check if there are write protected sectors inside the user flash area ****/ HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct); /* Lock the Flash to disable the flash control register access (recommended to protect the FLASH memory against possible unwanted operation) *********//* ... */ HAL_FLASH_Lock(); /* Get pages already write protected ****************************************/ ProtectedSECTOR = ~(OptionsBytesStruct.WRPSector) & FLASH_SECTOR_TO_BE_PROTECTED; /* Check if desired pages are already write protected ***********************/ if(ProtectedSECTOR != 0) { /* Some sectors inside the user flash area are write protected */ return FLASHIF_PROTECTION_WRPENABLED; }if (ProtectedSECTOR != 0) { ... } else { /* No write protected sectors inside the user flash area */ return FLASHIF_PROTECTION_NONE; }else { ... } }{ ... } /** * @brief Gets the sector of a given address * @param Address: Flash address * @retval The sector of a given address *//* ... */ 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 /*(Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11))*/ { sector = FLASH_SECTOR_11; }else { ... } return sector; }{ ... } /** * @brief Configure the write protection status of user flash area. * @param modifier DISABLE or ENABLE the protection * @retval HAL_StatusTypeDef HAL_OK if change is applied. *//* ... */ HAL_StatusTypeDef FLASH_If_WriteProtectionConfig(uint32_t modifier) { uint32_t ProtectedSECTOR = 0xFFF; FLASH_OBProgramInitTypeDef config_new, config_old; HAL_StatusTypeDef result = HAL_OK; /* Get pages write protection status ****************************************/ HAL_FLASHEx_OBGetConfig(&config_old); /* The parameter says whether we turn the protection on or off */ config_new.WRPState = modifier; /* We want to modify only the Write protection */ config_new.OptionType = OPTIONBYTE_WRP; /* No read protection, keep BOR and reset settings */ config_new.RDPLevel = OB_RDP_LEVEL_0; config_new.USERConfig = config_old.USERConfig; /* Get pages already write protected ****************************************/ ProtectedSECTOR = config_old.WRPSector | FLASH_SECTOR_TO_BE_PROTECTED; /* Unlock the Flash to enable the flash control register access *************/ HAL_FLASH_Unlock(); /* Unlock the Options Bytes *************************************************/ HAL_FLASH_OB_Unlock(); config_new.WRPSector = ProtectedSECTOR; result = HAL_FLASHEx_OBProgram(&config_new); return result; }{ ... } /** * @} *//* ... */