1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
26
27
28
29
30
31
32
33
34
35
36
37
38
39
42
43
44
49
50
51
52
53
54
55
56
57
58
59
65
66
67
68
69
70
71
72
73
74
75
84
85
86
87
88
89
90
91
92
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
134
135
136
137
138
139
141
142
143
144
145
146
147
154
155
156
157
158
159
166
167
168
169
170
171
172
173
174
175
176
177
178
179
184
189
190
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
221
222
223
235
236
237
238
/* ... */
#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)
{
BSP_LED_On(LED2);
Fail_Handler();
}if (readoutstatus != RESET) { ... }
f_unlink(UPLOAD_FILENAME);
indexoffset = (APPLICATION_ADDRESS - USER_FLASH_STARTADDRESS);
if (Appli_state == APPLICATION_READY)
{
if (f_open(&MyFile, UPLOAD_FILENAME, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
/* ... */
BSP_LED_On(LED1);
BSP_LED_On(LED2);
BSP_LED_On(LED4);
Fail_Handler();
}if (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) { ... }
BSP_LED_Off(LED2);
BSP_LED_Off(LED3);
}{ ... }
/* ... */
void COMMAND_Download(void)
{
if (f_open(&MyFileR, DOWNLOAD_FILENAME, FA_READ) != FR_OK)
{
/* ... */
BSP_LED_On(LED1);
BSP_LED_On(LED2);
BSP_LED_On(LED4);
Fail_Handler();
}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) { ... }
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);
}{ ... }
/* ... */
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)) { ... }
}{ ... }