1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
22
23
28
29
30
33
34
35
36
37
38
39
40
41
42
43
44
53
54
55
60
66
67
72
78
79
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
105
106
107
108
115
140
141
148
149
150
151
152
153
157
158
159
160
161
167
186
187
192
193
194
195
196
200
204
208
212
216
220
224
228
232
236
240
244
245
/* ... */
#include "usbd_dfu_flash.h"
#include "stm32f4xx_hal_conf.h"
Includes
#define FLASH_DESC_STR "@Internal Flash /0x08000000/03*016Ka,01*016Kg,01*064Kg,07*128Kg"
#define FLASH_ERASE_TIME (uint16_t)50
#define FLASH_PROGRAM_TIME (uint16_t)50
Private define
static uint32_t GetSector(uint32_t Address);
Private 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__ )
#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
/* ... */
uint16_t Flash_If_Init(void)
{
HAL_FLASH_Unlock();
return 0;
}{ ... }
/* ... */
uint16_t Flash_If_DeInit(void)
{
HAL_FLASH_Lock();
return 0;
}{ ... }
/* ... */
uint16_t Flash_If_Erase(uint32_t Add)
{
uint32_t startsector = 0;
uint32_t sectornb = 0;
HAL_StatusTypeDef status;
FLASH_EraseInitTypeDef eraseinitstruct;
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, §ornb);
if (status != HAL_OK)
{
return 1;
}if (status != HAL_OK) { ... }
return 0;
}{ ... }
/* ... */
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)
{
/* ... */
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)(dest+i), *(uint32_t*)(src+i)) == HAL_OK)
{
if(*(uint32_t *)(src + i) != *(uint32_t*)(dest+i))
{
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
{
return 1;
}else { ... }
}for (i = 0; i < Len; i+=4) { ... }
return 0;
}{ ... }
/* ... */
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 (uint8_t*)(dest);
}{ ... }
/* ... */
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;
}{ ... }
/* ... */
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;
}{ ... }