Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "usbd_dfu_flash.h"
#include "stm32f4xx_hal_conf.h"
Private define
#define FLASH_DESC_STR
#define FLASH_ERASE_TIME
#define FLASH_PROGRAM_TIME
Private function prototypes
Extern function prototypes
USBD_DFU_Flash_fops
Flash_If_Init()
Flash_If_DeInit()
Flash_If_Erase(uint32_t)
Flash_If_Write(uint8_t *, uint8_t *, uint32_t)
Flash_If_Read(uint8_t *, uint8_t *, uint32_t)
Flash_If_GetStatus(uint32_t, uint8_t, uint8_t *)
GetSector(uint32_t)
Files
loading...
SourceVuSTM32 Libraries and SamplesDFU_StandaloneSrc/usbd_dfu_flash.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file USB_Device/DFU_Standalone/Src/usbd_dfu_flash.c * @author MCD Application Team * @brief Memory management layer ****************************************************************************** * @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 "usbd_dfu_flash.h" #include "stm32f4xx_hal_conf.h" Includes /* Private typedef ----------------------------------------------------------- */ /* Private define ------------------------------------------------------------ */ #define FLASH_DESC_STR "@Internal Flash /0x08000000/03*016Ka,01*016Kg,01*064Kg,07*128Kg,04*016Kg,01*064Kg,07*128Kg" #define FLASH_ERASE_TIME (uint16_t)50 #define FLASH_PROGRAM_TIME (uint16_t)50 Private define /* Private macro ------------------------------------------------------------- */ /* Private variables --------------------------------------------------------- */ /* Private function prototypes ----------------------------------------------- */ static uint32_t GetSector(uint32_t Address); Private function prototypes /* Extern function prototypes ------------------------------------------------ */ uint16_t Flash_If_Init(void); uint16_t Flash_If_Erase(uint32_t Add); uint16_t Flash_If_Write(uint8_t * src, uint8_t * dest, uint32_t Len); uint8_t *Flash_If_Read(uint8_t * src, uint8_t * dest, uint32_t Len); uint16_t Flash_If_DeInit(void); uint16_t Flash_If_GetStatus(uint32_t Add, uint8_t Cmd, uint8_t * buffer); #if defined ( __ICCARM__ ) /* !< IAR Compiler */ #pragma data_alignment=4 #endif __ALIGN_BEGIN USBD_DFU_MediaTypeDef USBD_DFU_Flash_fops __ALIGN_END = { (uint8_t *) FLASH_DESC_STR, Flash_If_Init, Flash_If_DeInit, Flash_If_Erase, Flash_If_Write, Flash_If_Read, Flash_If_GetStatus, ...}; Extern function prototypes /* Private functions --------------------------------------------------------- */ /** * @brief Initializes Memory. * @param None * @retval 0 if operation is successeful, MAL_FAIL else. *//* ... */ uint16_t Flash_If_Init(void) { /* Unlock the internal flash */ HAL_FLASH_Unlock(); return 0; }{ ... } /** * @brief De-Initializes Memory. * @param None * @retval 0 if operation is successeful, MAL_FAIL else. *//* ... */ uint16_t Flash_If_DeInit(void) { /* Lock the internal flash */ HAL_FLASH_Lock(); return 0; }{ ... } /** * @brief Erases sector. * @param Add: Address of sector to be erased. * @retval 0 if operation is successeful, MAL_FAIL else. *//* ... */ uint16_t Flash_If_Erase(uint32_t Add) { uint32_t startsector = 0; uint32_t sectornb = 0; /* Variable contains Flash operation status */ HAL_StatusTypeDef status; FLASH_EraseInitTypeDef eraseinitstruct; /* Get the number of sector */ startsector = GetSector(Add); eraseinitstruct.TypeErase = FLASH_TYPEERASE_SECTORS; eraseinitstruct.VoltageRange = FLASH_VOLTAGE_RANGE_3; eraseinitstruct.Sector = startsector; eraseinitstruct.NbSectors = 1; status = HAL_FLASHEx_Erase(&eraseinitstruct, &sectornb); if (status != HAL_OK) { return 1; }if (status != HAL_OK) { ... } return 0; }{ ... } /** * @brief Writes Data into Memory. * @param src: Pointer to the source buffer. Address to be written to. * @param dest: Pointer to the destination buffer. * @param Len: Number of data to be written (in bytes). * @retval 0 if operation is successeful, MAL_FAIL else. *//* ... */ uint16_t Flash_If_Write(uint8_t * src, uint8_t * dest, uint32_t Len) { uint32_t i = 0; for (i = 0; i < Len; i += 4) { /* Device voltage range supposed to be [2.7V to 3.6V], the operation will * be done by byte *//* ... */ if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD, (uint32_t) (dest + i), *(uint32_t *) (src + i)) == HAL_OK) { /* Check the written value */ if (*(uint32_t *) (src + i) != *(uint32_t *) (dest + i)) { /* Flash content doesn't match SRAM content */ return 2; }if (*(uint32_t *) (src + i) != *(uint32_t *) (dest + i)) { ... } }if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD, (uint32_t) (dest + i), *(uint32_t *) (src + i)) == HAL_OK) { ... } else { /* Error occurred while writing data in Flash memory */ return 1; }else { ... } }for (i = 0; i < Len; i += 4) { ... } return 0; }{ ... } /** * @brief Reads Data into Memory. * @param src: Pointer to the source buffer. Address to be written to. * @param dest: Pointer to the destination buffer. * @param Len: Number of data to be read (in bytes). * @retval Pointer to the physical address where data should be read. *//* ... */ uint8_t *Flash_If_Read(uint8_t * src, uint8_t * dest, uint32_t Len) { uint32_t i = 0; uint8_t *psrc = src; for (i = 0; i < Len; i++) { dest[i] = *psrc++; }for (i = 0; i < Len; i++) { ... } /* Return a valid address to avoid HardFault */ return (uint8_t *) (dest); }{ ... } /** * @brief Gets Memory Status. * @param Add: Address to be read from. * @param Cmd: Number of data to be read (in bytes). * @retval 0 if operation is successeful *//* ... */ uint16_t Flash_If_GetStatus(uint32_t Add, uint8_t Cmd, uint8_t * buffer) { switch (Cmd) { case DFU_MEDIA_PROGRAM: buffer[1] = (uint8_t) FLASH_PROGRAM_TIME; buffer[2] = (uint8_t) (FLASH_PROGRAM_TIME << 8); buffer[3] = 0; break; case DFU_MEDIA_PROGRAM: case DFU_MEDIA_ERASE: default: buffer[1] = (uint8_t) FLASH_ERASE_TIME; buffer[2] = (uint8_t) (FLASH_ERASE_TIME << 8); buffer[3] = 0; break;default }switch (Cmd) { ... } return 0; }{ ... } /** * @brief Gets the sector of a given address * @param Address Address of the FLASH Memory * @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 if ((Address < ADDR_FLASH_SECTOR_12) && (Address >= ADDR_FLASH_SECTOR_11)) { sector = FLASH_SECTOR_11; }else if ((Address < ADDR_FLASH_SECTOR_12) && (Address >= ADDR_FLASH_SECTOR_11)) { ... } else if ((Address < ADDR_FLASH_SECTOR_13) && (Address >= ADDR_FLASH_SECTOR_12)) { sector = FLASH_SECTOR_12; }else if ((Address < ADDR_FLASH_SECTOR_13) && (Address >= ADDR_FLASH_SECTOR_12)) { ... } else if ((Address < ADDR_FLASH_SECTOR_14) && (Address >= ADDR_FLASH_SECTOR_13)) { sector = FLASH_SECTOR_13; }else if ((Address < ADDR_FLASH_SECTOR_14) && (Address >= ADDR_FLASH_SECTOR_13)) { ... } else if ((Address < ADDR_FLASH_SECTOR_15) && (Address >= ADDR_FLASH_SECTOR_14)) { sector = FLASH_SECTOR_14; }else if ((Address < ADDR_FLASH_SECTOR_15) && (Address >= ADDR_FLASH_SECTOR_14)) { ... } else if ((Address < ADDR_FLASH_SECTOR_16) && (Address >= ADDR_FLASH_SECTOR_15)) { sector = FLASH_SECTOR_15; }else if ((Address < ADDR_FLASH_SECTOR_16) && (Address >= ADDR_FLASH_SECTOR_15)) { ... } else if ((Address < ADDR_FLASH_SECTOR_17) && (Address >= ADDR_FLASH_SECTOR_16)) { sector = FLASH_SECTOR_16; }else if ((Address < ADDR_FLASH_SECTOR_17) && (Address >= ADDR_FLASH_SECTOR_16)) { ... } else if ((Address < ADDR_FLASH_SECTOR_18) && (Address >= ADDR_FLASH_SECTOR_17)) { sector = FLASH_SECTOR_17; }else if ((Address < ADDR_FLASH_SECTOR_18) && (Address >= ADDR_FLASH_SECTOR_17)) { ... } else if ((Address < ADDR_FLASH_SECTOR_19) && (Address >= ADDR_FLASH_SECTOR_18)) { sector = FLASH_SECTOR_18; }else if ((Address < ADDR_FLASH_SECTOR_19) && (Address >= ADDR_FLASH_SECTOR_18)) { ... } else if ((Address < ADDR_FLASH_SECTOR_20) && (Address >= ADDR_FLASH_SECTOR_19)) { sector = FLASH_SECTOR_19; }else if ((Address < ADDR_FLASH_SECTOR_20) && (Address >= ADDR_FLASH_SECTOR_19)) { ... } else if ((Address < ADDR_FLASH_SECTOR_21) && (Address >= ADDR_FLASH_SECTOR_20)) { sector = FLASH_SECTOR_20; }else if ((Address < ADDR_FLASH_SECTOR_21) && (Address >= ADDR_FLASH_SECTOR_20)) { ... } else if ((Address < ADDR_FLASH_SECTOR_22) && (Address >= ADDR_FLASH_SECTOR_21)) { sector = FLASH_SECTOR_21; }else if ((Address < ADDR_FLASH_SECTOR_22) && (Address >= ADDR_FLASH_SECTOR_21)) { ... } else if ((Address < ADDR_FLASH_SECTOR_23) && (Address >= ADDR_FLASH_SECTOR_22)) { sector = FLASH_SECTOR_22; }else if ((Address < ADDR_FLASH_SECTOR_23) && (Address >= ADDR_FLASH_SECTOR_22)) { ... } else { sector = FLASH_SECTOR_23; }else { ... } return sector; }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.