20 #define BOOTLOADER_VERSION_MAJOR 1
21 #define BOOTLOADER_VERSION_MINOR 1
22 #define BOOTLOADER_VERSION_PATCH 3
23 #define BOOTLOADER_VERSION_RC 0
39 __HAL_RCC_SYSCFG_CLK_ENABLE();
40 __HAL_RCC_FLASH_CLK_ENABLE();
44 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
58 uint32_t NbrOfPages = 0;
59 uint32_t PageError = 0;
60 FLASH_EraseInitTypeDef pEraseInit;
61 HAL_StatusTypeDef status = HAL_OK;
66 NbrOfPages = (FLASH_BASE + FLASH_SIZE -
APP_ADDRESS) / FLASH_PAGE_SIZE;
70 pEraseInit.Banks = FLASH_BANK_1;
73 pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
74 status = HAL_FLASHEx_Erase(&pEraseInit, &PageError);
81 pEraseInit.Banks = FLASH_BANK_2;
82 pEraseInit.NbPages = NbrOfPages;
84 pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
85 status = HAL_FLASHEx_Erase(&pEraseInit, &PageError);
122 if(!(
flash_ptr <= (FLASH_BASE + FLASH_SIZE - 8)) ||
129 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,
flash_ptr, data) ==
173 FLASH_OBProgramInitTypeDef OBStruct = {0};
179 OBStruct.PCROPConfig = FLASH_BANK_1;
180 OBStruct.WRPArea = OB_WRPAREA_BANK1_AREAA;
181 HAL_FLASHEx_OBGetConfig(&OBStruct);
183 if(OBStruct.PCROPEndAddr > OBStruct.PCROPStartAddr)
191 if(OBStruct.WRPEndOffset > OBStruct.WRPStartOffset)
193 if((OBStruct.WRPStartOffset * FLASH_PAGE_SIZE + FLASH_BASE) >=
200 OBStruct.WRPArea = OB_WRPAREA_BANK1_AREAB;
201 HAL_FLASHEx_OBGetConfig(&OBStruct);
203 if(OBStruct.WRPEndOffset > OBStruct.WRPStartOffset)
205 if((OBStruct.WRPStartOffset * FLASH_PAGE_SIZE + FLASH_BASE) >=
213 OBStruct.PCROPConfig = FLASH_BANK_2;
214 OBStruct.WRPArea = OB_WRPAREA_BANK2_AREAA;
215 HAL_FLASHEx_OBGetConfig(&OBStruct);
217 if(OBStruct.PCROPEndAddr > OBStruct.PCROPStartAddr)
225 if(OBStruct.WRPEndOffset > OBStruct.WRPStartOffset)
227 if((OBStruct.WRPStartOffset * FLASH_PAGE_SIZE + FLASH_BASE +
234 OBStruct.WRPArea = OB_WRPAREA_BANK2_AREAB;
235 HAL_FLASHEx_OBGetConfig(&OBStruct);
237 if(OBStruct.WRPEndOffset > OBStruct.WRPStartOffset)
239 if((OBStruct.WRPStartOffset * FLASH_PAGE_SIZE + FLASH_BASE +
247 if(OBStruct.RDPLevel != OB_RDP_LEVEL_0)
265 FLASH_OBProgramInitTypeDef OBStruct = {0};
266 HAL_StatusTypeDef status = HAL_ERROR;
268 status = HAL_FLASH_Unlock();
269 status |= HAL_FLASH_OB_Unlock();
272 OBStruct.WRPArea = OB_WRPAREA_BANK1_AREAA;
273 OBStruct.OptionType = OPTIONBYTE_WRP;
277 OBStruct.WRPStartOffset = (
APP_ADDRESS - FLASH_BASE) / FLASH_PAGE_SIZE;
283 OBStruct.WRPStartOffset = 0xFF;
284 OBStruct.WRPEndOffset = 0x00;
286 status |= HAL_FLASHEx_OBProgram(&OBStruct);
289 OBStruct.WRPArea = OB_WRPAREA_BANK1_AREAB;
290 OBStruct.OptionType = OPTIONBYTE_WRP;
291 OBStruct.WRPStartOffset = 0xFF;
292 OBStruct.WRPEndOffset = 0x00;
293 status |= HAL_FLASHEx_OBProgram(&OBStruct);
296 OBStruct.WRPArea = OB_WRPAREA_BANK2_AREAA;
297 OBStruct.OptionType = OPTIONBYTE_WRP;
301 OBStruct.WRPStartOffset = 0x00;
307 OBStruct.WRPStartOffset = 0xFF;
308 OBStruct.WRPEndOffset = 0x00;
310 status |= HAL_FLASHEx_OBProgram(&OBStruct);
313 OBStruct.WRPArea = OB_WRPAREA_BANK2_AREAB;
314 OBStruct.OptionType = OPTIONBYTE_WRP;
315 OBStruct.WRPStartOffset = 0xFF;
316 OBStruct.WRPEndOffset = 0x00;
317 status |= HAL_FLASHEx_OBProgram(&OBStruct);
322 status |= HAL_FLASH_OB_Launch();
325 status |= HAL_FLASH_OB_Lock();
326 status |= HAL_FLASH_Lock();
356 CRC_HandleTypeDef CrcHandle;
357 volatile uint32_t calculatedCrc = 0;
359 __HAL_RCC_CRC_CLK_ENABLE();
360 CrcHandle.Instance = CRC;
361 CrcHandle.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE;
362 CrcHandle.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
363 CrcHandle.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
364 CrcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
365 CrcHandle.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS;
366 if(HAL_CRC_Init(&CrcHandle) != HAL_OK)
374 __HAL_RCC_CRC_FORCE_RESET();
375 __HAL_RCC_CRC_RELEASE_RESET();
412 uint32_t JumpAddress = *(__IO uint32_t*)(
APP_ADDRESS + 4);
422 #if(SET_VECTOR_TABLE)
451 __HAL_RCC_SYSCFG_CLK_ENABLE();
452 __HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH();