There is a common practice of today’s embedded systems to perform firmware upgrade using custom procedures in order to support different communication protocols during the data transfer such as (KWP2000 etc). For that reason the solid STM bootloader is not useful and the development should proceed by creating a custom bootloader that supports the requested functionalities as well as the core procedure of writing the new firmware (transferred data) to the flash memory.

Below is the header/source files which can be used in almost all the stm32 series by just adjusting the memory addresses (Flash sectors start address) etc.. Beware that this code is the minimum required to write to the flash memory. You are strongly advised to extend it by adding fail condition handlers.

/*! @file   flash.h
    @brief  < brief description here >

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Created by: Io.D
*/


#ifndef LIBRARIES_LBR_AES_H_
	#define LIBRARIES_LBR_AES_H_

	#include <stdint.h>

	// Include the appropriate HAL Library here
	#include "stm32f4xx_hal.h"

	void flash_initialize();
	void flash_deinitialize();
	void flash_erase(uint8_t sector);
	uint8_t flash_write(volatile uint32_t* address, uint32_t *data, uint16_t size);

#endif
/*! @file   flash.c
    @brief  < brief description here >

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Created by: Io.D
*/

#include "flash.h"

void flash_initialize()
{
	HAL_FLASH_Unlock();
}

void flash_deinitialize()
{
	HAL_FLASH_Lock();
}

void flash_erase(uint8_t sector)
{
    uint32_t error = 0;
    FLASH_EraseInitTypeDef FLASH_EraseInitStruct =
    {
    	.TypeErase = FLASH_TYPEERASE_SECTORS,
	.Sector = (uint32_t)sector,
	.NbSectors = 1,
	.VoltageRange = FLASH_VOLTAGE_RANGE_3
    };

    HAL_FLASHEx_Erase(&FLASH_EraseInitStruct,&error);
}

uint8_t flash_write(volatile uint32_t* address, uint32_t *data, uint16_t size)
{
   for (int i = 0; i < size; i++)
     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (*address)+(i*4), data[i]) != HAL_OK)
    	return 1;
   return 0;
}

 

Author

Write A Comment