1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
22
23
27
28
29
30
31
32
33
34
35
36
37
38
39
40
43
44
45
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
119
120
121
126
127
128
129
130
131
138
139
140
141
142
143
144
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
167
168
169
170
171
172
173
174
175
180
185
186
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
217
218
219
231
232
233
234
/* ... */
#include "main.h"
Includes
#define UPLOAD_FILENAME "0:UPLOAD.BIN"
#define DOWNLOAD_FILENAME "0:image.BIN"
Private defines
static uint32_t TmpReadSize = 0x00;
static uint32_t RamAddress = 0x00;
static __IO uint32_t LastPGAddress = APPLICATION_ADDRESS;
static uint8_t RAM_Buf[BUFFER_SIZE] = {0x00};
FATFS USBH_fatfs;
FIL MyFile;
FIL MyFileR;
extern DIR dir;
extern FILINFO fno;
Private variables
static void COMMAND_ProgramFlashMemory(void);
Private function prototypes
/* ... */
void COMMAND_Upload(void)
{
__IO uint32_t address = APPLICATION_ADDRESS;
__IO uint32_t counterread = 0x00;
uint32_t tmpcounter = 0x00, indexoffset = 0x00;
FlagStatus readoutstatus = SET;
uint16_t byteswritten;
readoutstatus = FLASH_If_ReadOutProtectionStatus();
if(readoutstatus == RESET)
{
f_unlink(UPLOAD_FILENAME);
indexoffset = (APPLICATION_ADDRESS - USER_FLASH_STARTADDRESS);
if(( Appli_state == APPLICATION_READY) && (f_open(&MyFile, UPLOAD_FILENAME, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK))
{
BSP_LED_On(LED4);
BSP_LED_Off(LED3);
while ((indexoffset < USER_FLASH_SIZE) && ( Appli_state == APPLICATION_READY))
{
for(counterread = 0; counterread < BUFFER_SIZE; counterread++)
{
if(indexoffset + counterread < USER_FLASH_SIZE)
{
tmpcounter = counterread;
RAM_Buf[tmpcounter] = (*(uint8_t*)(address++));
}if (indexoffset + counterread < USER_FLASH_SIZE) { ... }
else
{
break;
}else { ... }
}for (counterread = 0; counterread < BUFFER_SIZE; counterread++) { ... }
f_write (&MyFile, RAM_Buf, BUFFER_SIZE, (void *)&byteswritten);
indexoffset = indexoffset + counterread;
}while ((indexoffset < USER_FLASH_SIZE) && ( Appli_state == APPLICATION_READY)) { ... }
BSP_LED_Off(LED4);
BSP_LED_Off(LED2);
BSP_LED_On(LED1);
f_close(&MyFile);
f_mount(0, 0, 0);
}if (( Appli_state == APPLICATION_READY) && (f_open(&MyFile, UPLOAD_FILENAME, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)) { ... }
BSP_LED_Off(LED2);
BSP_LED_Off(LED3);
}if (readoutstatus == RESET) { ... }
else
{
BSP_LED_On(LED2);
Fail_Handler();
}else { ... }
}{ ... }
/* ... */
void COMMAND_Download(void)
{
if(f_open(&MyFileR, DOWNLOAD_FILENAME, FA_READ) == FR_OK)
{
if(f_size(&MyFileR) > USER_FLASH_SIZE)
{
/* ... */
BSP_LED_On(LED4);
Fail_Handler();
}if (f_size(&MyFileR) > USER_FLASH_SIZE) { ... }
else
{
BSP_LED_On(LED4);
if(FLASH_If_EraseSectors(APPLICATION_ADDRESS) != 0x00)
{
/* ... */
BSP_LED_Off(LED4);
Erase_Fail_Handler();
}if (FLASH_If_EraseSectors(APPLICATION_ADDRESS) != 0x00) { ... }
COMMAND_ProgramFlashMemory();
BSP_LED_Off(LED4);
BSP_LED_On(LED2);
f_close(&MyFileR);
}else { ... }
}if (f_open(&MyFileR, DOWNLOAD_FILENAME, FA_READ) == FR_OK) { ... }
else
{
/* ... */
BSP_LED_On(LED1);
BSP_LED_On(LED2);
BSP_LED_On(LED4);
Fail_Handler();
}else { ... }
}{ ... }
/* ... */
void COMMAND_Jump(void)
{
NVIC_SystemReset();
}{ ... }
/* ... */
static void COMMAND_ProgramFlashMemory(void)
{
uint32_t programcounter = 0x00;
uint8_t readflag = TRUE;
uint32_t bytesread;
RamAddress = (uint32_t) &RAM_Buf;
LastPGAddress = APPLICATION_ADDRESS;
while ((readflag == TRUE))
{
f_read (&MyFileR, RAM_Buf, BUFFER_SIZE, (void *)&bytesread);
TmpReadSize = bytesread;
if(TmpReadSize < BUFFER_SIZE)
{
readflag = FALSE;
}if (TmpReadSize < BUFFER_SIZE) { ... }
for (programcounter = 0; programcounter < TmpReadSize; programcounter += 4)
{
if (FLASH_If_Write((LastPGAddress + programcounter),
*(uint32_t *) (RamAddress + programcounter)) != 0x00)
{
/* ... */
BSP_LED_On(LED2);
Fail_Handler();
}if (FLASH_If_Write((LastPGAddress + programcounter), *(uint32_t *) (RamAddress + programcounter)) != 0x00) { ... }
}for (programcounter = 0; programcounter < TmpReadSize; programcounter += 4) { ... }
LastPGAddress += TmpReadSize;
}while ((readflag == TRUE)) { ... }
}{ ... }