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
28
29
37
38
39
45
49
50
51
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
78
82
86
90
91
92
97
98
99
100
101
102
103
104
105
106
107
108
109
113
117
118
119
120
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
153
157
158
159
160
161
162
163
164
165
166
167
171
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
202
203
217
218
221
222
223
224
225
/* ... */
/* ... */
#include "main.h"
#include "common.h"
#include "flash_if.h"
#include "menu.h"
#include "ymodem.h"
5 includes
Includes
pFunction JumpToApplication;
uint32_t JumpAddress;
uint32_t FlashProtection = 0;
uint8_t aFileName[FILE_NAME_LENGTH];
Private variables
void SerialDownload(void);
void SerialUpload(void);
Private function prototypes
/* ... */
void SerialDownload(void)
{
uint8_t number[11] = {0};
uint32_t size = 0;
COM_StatusTypeDef result;
Serial_PutString((uint8_t *)"Waiting for the file to be sent ... (press 'a' to abort)\n\r");
result = Ymodem_Receive( &size );
if (result == COM_OK)
{
Serial_PutString((uint8_t *)"\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: ");
Serial_PutString(aFileName);
Int2Str(number, size);
Serial_PutString((uint8_t *)"\n\r Size: ");
Serial_PutString(number);
Serial_PutString((uint8_t *)" Bytes\r\n");
Serial_PutString((uint8_t *)"-------------------\n");
}if (result == COM_OK) { ... }
else if (result == COM_LIMIT)
{
Serial_PutString((uint8_t *)"\n\n\rThe image size is higher than the allowed space memory!\n\r");
}else if (result == COM_LIMIT) { ... }
else if (result == COM_DATA)
{
Serial_PutString((uint8_t *)"\n\n\rVerification failed!\n\r");
}else if (result == COM_DATA) { ... }
else if (result == COM_ABORT)
{
Serial_PutString((uint8_t *)"\r\n\nAborted by user.\n\r");
}else if (result == COM_ABORT) { ... }
else
{
Serial_PutString((uint8_t *)"\n\rFailed to receive the file!\n\r");
}else { ... }
}{ ... }
/* ... */
void SerialUpload(void)
{
uint8_t status = 0;
Serial_PutString((uint8_t *)"\n\n\rSelect Receive File\n\r");
HAL_UART_Receive(&UartHandle, &status, 1, RX_TIMEOUT);
if ( status == CRC16)
{
status = Ymodem_Transmit((uint8_t*)APPLICATION_ADDRESS, (const uint8_t*)"UploadedFlashImage.bin", USER_FLASH_SIZE);
if (status != 0)
{
Serial_PutString((uint8_t *)"\n\rError Occurred while Transmitting File\n\r");
}if (status != 0) { ... }
else
{
Serial_PutString((uint8_t *)"\n\rFile uploaded successfully \n\r");
}else { ... }
}if (status == CRC16) { ... }
}{ ... }
/* ... */
void Main_Menu(void)
{
uint8_t key = 0;
Serial_PutString((uint8_t *)"\r\n======================================================================");
Serial_PutString((uint8_t *)"\r\n= (C) COPYRIGHT 2016 STMicroelectronics =");
Serial_PutString((uint8_t *)"\r\n= =");
Serial_PutString((uint8_t *)"\r\n= STM32F4xx In-Application Programming Application =");
Serial_PutString((uint8_t *)"\r\n= =");
Serial_PutString((uint8_t *)"\r\n= By MCD Application Team =");
Serial_PutString((uint8_t *)"\r\n======================================================================");
Serial_PutString((uint8_t *)"\r\n\r\n");
while (1)
{
FlashProtection = FLASH_If_GetWriteProtectionStatus();
Serial_PutString((uint8_t *)"\r\n=================== Main Menu ============================\r\n\n");
Serial_PutString((uint8_t *)" Download image to the internal Flash ----------------- 1\r\n\n");
Serial_PutString((uint8_t *)" Upload image from the internal Flash ----------------- 2\r\n\n");
Serial_PutString((uint8_t *)" Execute the loaded application ----------------------- 3\r\n\n");
if(FlashProtection != FLASHIF_PROTECTION_NONE)
{
Serial_PutString((uint8_t *)" Disable the write protection ------------------------- 4\r\n\n");
}if (FlashProtection != FLASHIF_PROTECTION_NONE) { ... }
else
{
Serial_PutString((uint8_t *)" Enable the write protection -------------------------- 4\r\n\n");
}else { ... }
Serial_PutString((uint8_t *)"==========================================================\r\n\n");
__HAL_UART_FLUSH_DRREGISTER(&UartHandle);
HAL_UART_Receive(&UartHandle, &key, 1, RX_TIMEOUT);
switch (key)
{
case '1' :
SerialDownload();
break;case '1' :
case '2' :
SerialUpload();
break;case '2' :
case '3' :
Serial_PutString((uint8_t *)"Start program execution......\r\n\n");
JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
JumpToApplication = (pFunction) JumpAddress;
__set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
JumpToApplication();
break;case '3' :
case '4' :
if (FlashProtection != FLASHIF_PROTECTION_NONE)
{
if (FLASH_If_WriteProtectionConfig(OB_WRPSTATE_DISABLE) == HAL_OK)
{
Serial_PutString((uint8_t *)"Write Protection disabled...\r\n");
Serial_PutString((uint8_t *)"System will now restart...\r\n");
HAL_FLASH_OB_Launch();
HAL_FLASH_Unlock();
}if (FLASH_If_WriteProtectionConfig(OB_WRPSTATE_DISABLE) == HAL_OK) { ... }
else
{
Serial_PutString((uint8_t *)"Error: Flash write un-protection failed...\r\n");
}else { ... }
}if (FlashProtection != FLASHIF_PROTECTION_NONE) { ... }
else
{
if (FLASH_If_WriteProtectionConfig(OB_WRPSTATE_ENABLE) == HAL_OK)
{
Serial_PutString((uint8_t *)"Write Protection enabled...\r\n");
Serial_PutString((uint8_t *)"System will now restart...\r\n");
HAL_FLASH_OB_Launch();
}if (FLASH_If_WriteProtectionConfig(OB_WRPSTATE_ENABLE) == HAL_OK) { ... }
else
{
Serial_PutString((uint8_t *)"Error: Flash write protection failed...\r\n");
}else { ... }
}else { ... }
break;case '4' :
default:
Serial_PutString((uint8_t *)"Invalid Number ! ==> The number should be either 1, 2, 3 or 4\r");
break;default
}switch (key) { ... }
}while (1) { ... }
}{ ... }
/* ... */