Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
Private defines
#define UPLOAD_FILENAME
#define DOWNLOAD_FILENAME
Private variables
TmpReadSize
RamAddress
LastPGAddress
RAM_Buf
USBH_fatfs
MyFile
MyFileR
Private function prototypes
COMMAND_Upload()
COMMAND_Download()
COMMAND_Jump()
COMMAND_ProgramFlashMemory()
Files
loading...
CodeScopeSTM32 Libraries and SamplesFWupgrade_StandaloneSrc/command.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file USB_Host/FWupgrade_Standalone/Src/command.c * @author MCD Application Team * @brief This file provides all the IAP command 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 defines -----------------------------------------------------------*/ #define UPLOAD_FILENAME "0:UPLOAD.BIN" #define DOWNLOAD_FILENAME "0:image.BIN" Private defines /* Private macros ------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ 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; /* File object for upload operation */ FIL MyFileR; /* File object for download operation */ extern DIR dir; extern FILINFO fno; Private variables /* Private function prototypes -----------------------------------------------*/ static void COMMAND_ProgramFlashMemory(void); Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief IAP Read all flash memory. * @param None * @retval None *//* ... */ 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; /* Get the read out protection status */ readoutstatus = FLASH_If_ReadOutProtectionStatus(); if(readoutstatus == RESET) { /* Remove UPLOAD file if it exists on flash disk */ f_unlink(UPLOAD_FILENAME); /* Init written byte counter */ indexoffset = (APPLICATION_ADDRESS - USER_FLASH_STARTADDRESS); /* Open binary file to write on it */ if(( Appli_state == APPLICATION_READY) && (f_open(&MyFile, UPLOAD_FILENAME, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)) { /* Upload On Going: Turn LED4 On and LED3 Off */ BSP_LED_On(LED4); BSP_LED_Off(LED3); /* Read flash memory */ while ((indexoffset < USER_FLASH_SIZE) && ( Appli_state == APPLICATION_READY)) { for(counterread = 0; counterread < BUFFER_SIZE; counterread++) { /* Check the read bytes versus the end of flash */ if(indexoffset + counterread < USER_FLASH_SIZE) { tmpcounter = counterread; RAM_Buf[tmpcounter] = (*(uint8_t*)(address++)); }if (indexoffset + counterread < USER_FLASH_SIZE) { ... } /* In this case all flash was read */ else { break; }else { ... } }for (counterread = 0; counterread < BUFFER_SIZE; counterread++) { ... } /* Write buffer to file */ f_write (&MyFile, RAM_Buf, BUFFER_SIZE, (void *)&byteswritten); /* Number of byte written */ indexoffset = indexoffset + counterread; }while ((indexoffset < USER_FLASH_SIZE) && ( Appli_state == APPLICATION_READY)) { ... } /* Turn LED1 On: Upload Done */ BSP_LED_Off(LED4); BSP_LED_Off(LED2); BSP_LED_On(LED1); /* Close file and filesystem */ 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)) { ... } /* Keep These LEDS OFF when Device connected */ BSP_LED_Off(LED2); BSP_LED_Off(LED3); }if (readoutstatus == RESET) { ... } else { /* Message ROP active: Turn LED2 On and Toggle LED3 in infinite loop */ BSP_LED_On(LED2); Fail_Handler(); }else { ... } }{ ... } /** * @brief IAP Write Flash memory. * @param None * @retval None *//* ... */ void COMMAND_Download(void) { /* Open the binary file to be downloaded */ if(f_open(&MyFileR, DOWNLOAD_FILENAME, FA_READ) == FR_OK) { if(f_size(&MyFileR) > USER_FLASH_SIZE) { /* No available Flash memory size for the binary file: Turn LED4 On and Toggle LED3 in infinite loop *//* ... */ BSP_LED_On(LED4); Fail_Handler(); }if (f_size(&MyFileR) > USER_FLASH_SIZE) { ... } else { /* Download On Going: Turn LED4 On */ BSP_LED_On(LED4); /* Erase FLASH sectors to download image */ if(FLASH_If_EraseSectors(APPLICATION_ADDRESS) != 0x00) { /* Flash erase error: Turn LED4 On and Toggle LED2 and LED3 in infinite loop *//* ... */ BSP_LED_Off(LED4); Erase_Fail_Handler(); }if (FLASH_If_EraseSectors(APPLICATION_ADDRESS) != 0x00) { ... } /* Program flash memory */ COMMAND_ProgramFlashMemory(); /* Download Done: Turn LED4 Off and LED2 On */ BSP_LED_Off(LED4); BSP_LED_On(LED2); /* Close file */ f_close(&MyFileR); }else { ... } }if (f_open(&MyFileR, DOWNLOAD_FILENAME, FA_READ) == FR_OK) { ... } else { /* The binary file is not available: Turn LED1, LED2 and LED4 On and Toggle LED3 in infinite loop *//* ... */ BSP_LED_On(LED1); BSP_LED_On(LED2); BSP_LED_On(LED4); Fail_Handler(); }else { ... } }{ ... } /** * @brief IAP jump to user program. * @param None * @retval None *//* ... */ void COMMAND_Jump(void) { /* Software reset */ NVIC_SystemReset(); }{ ... } /** * @brief Programs the internal Flash memory. * @param None * @retval None *//* ... */ static void COMMAND_ProgramFlashMemory(void) { uint32_t programcounter = 0x00; uint8_t readflag = TRUE; uint32_t bytesread; /* RAM Address Initialization */ RamAddress = (uint32_t) &RAM_Buf; /* Erase address init */ LastPGAddress = APPLICATION_ADDRESS; /* While file still contain data */ while ((readflag == TRUE)) { /* Read maximum 512 Kbyte from the selected file */ f_read (&MyFileR, RAM_Buf, BUFFER_SIZE, (void *)&bytesread); /* Temp variable */ TmpReadSize = bytesread; /* The read data < "BUFFER_SIZE" Kbyte */ if(TmpReadSize < BUFFER_SIZE) { readflag = FALSE; }if (TmpReadSize < BUFFER_SIZE) { ... } /* Program flash memory */ for (programcounter = 0; programcounter < TmpReadSize; programcounter += 4) { /* Write word into flash memory */ if (FLASH_If_Write((LastPGAddress + programcounter), *(uint32_t *) (RamAddress + programcounter)) != 0x00) { /* Flash programming error: Turn LED2 On and Toggle LED3 in infinite * loop *//* ... */ BSP_LED_On(LED2); Fail_Handler(); }if (FLASH_If_Write((LastPGAddress + programcounter), *(uint32_t *) (RamAddress + programcounter)) != 0x00) { ... } }for (programcounter = 0; programcounter < TmpReadSize; programcounter += 4) { ... } /* Update last programmed address value */ LastPGAddress += TmpReadSize; }while ((readflag == TRUE)) { ... } }{ ... }