Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
Private variables
FirstSector
NbOfSectors
SectorError
OB_RDP_LEVEL
Private function prototypes
FLASH_OBProgramInitStruct
FLASH_EraseInitStruct
FLASH_If_FlashUnlock()
FLASH_If_ReadOutProtectionStatus()
FLASH_If_EraseSectors(uint32_t)
FLASH_If_Write(uint32_t, uint32_t)
FLASH_If_GetSectorNumber(uint32_t)
Files
loading...
CodeScopeSTM32 Libraries and SamplesFWupgrade_StandaloneSrc/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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file USB_Host/FWupgrade_Standalone/Src/flash_if.c * @author MCD Application Team * @brief This file provides all the flash layer 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. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "main.h" Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macros ------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ uint32_t FirstSector = 0; uint32_t NbOfSectors = 0; uint32_t SectorError = 0; uint32_t OB_RDP_LEVEL; Private variables /* Private function prototypes -----------------------------------------------*/ static uint32_t FLASH_If_GetSectorNumber(uint32_t Address); static FLASH_OBProgramInitTypeDef FLASH_OBProgramInitStruct; static FLASH_EraseInitTypeDef FLASH_EraseInitStruct; Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief Unlocks the Flash to enable the flash control register access. * @param None * @retval None *//* ... */ void FLASH_If_FlashUnlock(void) { HAL_FLASH_Unlock(); }{ ... } /** * @brief Gets Flash readout protection status. * @param None * @retval ReadOut protection status *//* ... */ 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; }{ ... } /** * @brief Erases the required FLASH Sectors. * @param Address: Start address for erasing data * @retval 0: Erase sectors done with success * 1: Erase error *//* ... */ uint32_t FLASH_If_EraseSectors(uint32_t Address) { /* Erase the user Flash area (area defined by APPLICATION_ADDRESS and USER_FLASH_LAST_PAGE_ADDRESS) ****//* ... */ if(Address <= (uint32_t) USER_FLASH_LAST_PAGE_ADDRESS) { /* Get the 1st sector to erase */ FirstSector = FLASH_If_GetSectorNumber(Address); /* Get the number of sector to erase from 1st sector */ 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); }{ ... } /** * @brief Writes a data buffer in flash (data are 32-bit aligned). * @note After writing data buffer, the flash content is checked. * @param Address: Start address for writing data buffer * @param Data: Pointer on data buffer * @retval 0: Data successfully written to Flash memory * 1: Error occurred while writing data in Flash memory *//* ... */ uint32_t FLASH_If_Write(uint32_t Address, uint32_t Data) { /* Program the user Flash area word by word (area defined by FLASH_USER_START_ADDR and APPLICATION_ADDRESS) ***********//* ... */ 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); }{ ... } /** * @brief Returns the Flash sector Number of the address * @param None * @retval The Flash sector Number of the address *//* ... */ 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 /*(Address < FLASH_END_ADDR && Address >= ADDR_FLASH_SECTOR_11) */ { sector = FLASH_SECTOR_11; }else { ... } return sector; }{ ... }