How to write data to the internal FLASH memory of an STM32

H

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 stock STM bootloader(DFU) 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 of the microcontroller.

Below are 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.

UPDATED!!!
Please check: https://devcoons.com/stm32-devcoons-framework-use-the-drv_flash-module-3/
/*!
	@file   drv_flash.h
	@brief  <brief description here>
	@t.odo	-
	---------------------------------------------------------------------------
	MIT License
	Copyright (c) 2019 Ioannis Deligiannis
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
/******************************************************************************
* Preprocessor Definitions & Macros
******************************************************************************/

#ifndef DRIVERS_INC_DRV_FLASH_H_
#define LDRIVERS_INC_DRV_FLASH_H_

/******************************************************************************
* Includes
******************************************************************************/

#include <inttypes.h>
#include <string.h>

#if defined(STM32H753xx)
	#include "stm32h7xx_hal.h"
	#include "stm32h7xx_hal_flash.h"
	#define FLASH_ADDR_START 	((uint32_t)0x08000000)
	#define FLASH_ADDR_END 		((uint32_t)0x08040000)

#elif defined(STM32L562xx)
	#include "stm32l5xx_hal.h"
	#include "stm32l5xx_hal_flash.h"
	#define FLASH_ADDR_START 	((uint32_t)0x08000000)
	#define FLASH_ADDR_END 		((uint32_t)0x08040000)

#elif defined(STM32L476xx)
	#include "stm32l4xx_hal.h"
	#include "stm32l4xx_hal_flash.h"
	#define FLASH_ADDR_START 	((uint32_t)0x08000000)
	#define FLASH_ADDR_END 		((uint32_t)0x08040000)

#elif defined(STM32H745xx)
	#include "stm32h7xx_hal.h"
	#include "stm32h7xx_hal_flash.h"
	#define FLASH_ADDR_START 	((uint32_t)0x08000000)
	#define FLASH_ADDR_END 		((uint32_t)0x08300000)
#else
#endif

#if __has_include("cmsis_os.h")
#include "cmsis_os.h"
#endif

#if defined(FLASH_SECTOR_SIZE)
	#define FLASH_SECTION_SIZE FLASH_SECTOR_SIZE
#elif defined(FLASH_PAGE_SIZE)
	#define FLASH_SECTION_SIZE FLASH_PAGE_SIZE
#else
	#error "Must be rewritten"
#endif

/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/

#ifndef ENUM_I_STATUS
#define ENUM_I_STATUS
typedef enum
{
	I_OK 			= 0x00,
	I_INVALID 		= 0x01,
	I_EXISTS 		= 0x02,
	I_NOTEXISTS 		= 0x03,
	I_FAILED 		= 0x04,
	I_EXPIRED 		= 0x05,
	I_UNKNOWN 		= 0x06,
	I_INPROGRESS 		= 0x07,
	I_IDLE			= 0x08,
	I_FULL			= 0x09,
	I_EMPTY			= 0x0A,
	I_YES			= 0x0B,
	I_NO			= 0x0C,
	I_SKIP			= 0x0D,
	I_LOCKED 		= 0x0E,
	I_INACTIVE 		= 0x0F,
	I_ACTIVE 		= 0x10,
	I_READY		 	= 0x11,
	I_WAIT 			= 0x12,
	I_OVERFLOW 		= 0x13,
	I_CONTINUE 		= 0x14,
	I_STOPPED 		= 0x15,
	I_WARNING 		= 0x16,
	I_SLEEP 		= 0x17,
	I_DEEPSLEEP 		= 0x18,
	I_STANDBY 		= 0x19,
	I_GRANTED 		= 0x1A,
	I_DENIED 		= 0x1B,
	I_DEBUG_01 		= 0xE0,
	I_DEBUG_02 		= 0xE1,
	I_DEBUG_03 		= 0xE2,
	I_DEBUG_04 		= 0xE3,
	I_DEBUG_05 		= 0xE4,
	I_DEBUG_06 		= 0xE5,
	I_DEBUG_07 		= 0xE6,
	I_DEBUG_08 		= 0xE7,
	I_DEBUG_09 		= 0xE8,
	I_DEBUG_10 		= 0xE9,
	I_DEBUG_11 		= 0xEA,
	I_DEBUG_12 		= 0xEB,
	I_DEBUG_13 		= 0xEC,
	I_DEBUG_14 		= 0xED,
	I_DEBUG_15 		= 0xEE,
	I_DEBUG_16 		= 0xEF,
	I_MEMALIGNED		= 0xFC,
	I_MEMUNALIGNED		= 0xFD,
	I_NOTIMPLEMENTED 	= 0xFE,
	I_ERROR 		= 0xFF
}i_status;
#endif

/******************************************************************************
* Declaration | Public Functions
******************************************************************************/

i_status flash_erase(uint32_t addr_from, uint32_t addr_to);
i_status flash_erase_nb(uint32_t addr_from, uint32_t nbp);
i_status flash_write(uint32_t address, uint8_t* data_array,uint32_t data_array_sz);
uint32_t get_flash_sector_address(uint32_t address);

/******************************************************************************
* EOF - NO CODE AFTER THIS LINE
******************************************************************************/
#endif
/*!
	@file   drv_flash.c
	@brief  <brief description here>
	@t.odo	-
	---------------------------------------------------------------------------
	MIT License
	Copyright (c) 2019 Ioannis Deligiannis
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
/******************************************************************************
* Preprocessor Definitions & Macros
******************************************************************************/

/******************************************************************************
* Includes
******************************************************************************/

#include "drv_flash.h"

#ifdef HAL_IWDG_MODULE_ENABLED
#include "iwdg.h"
#endif

/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/

static FLASH_EraseInitTypeDef flash_erase_info;
static volatile uint8_t flash_write_error = 0;
static volatile uint32_t flash_write_cur_address = 0;
static volatile uint64_t flash_write_data = 0;
static volatile uint8_t flash_write_data_arr[128];

/******************************************************************************
* Declaration | Static Functions
******************************************************************************/

static uint32_t get_flash_sector(uint32_t address);

/******************************************************************************
* Definition  | Static Functions
******************************************************************************/

static uint32_t get_flash_sector(uint32_t address)
{

#if defined(FLASH_SECTOR_SIZE)
	return address < (FLASH_BASE + FLASH_BANK_SIZE) ?
			(address - FLASH_BASE) / FLASH_SECTOR_SIZE
			:
			(address - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_SECTOR_SIZE;
#elif defined(FLASH_PAGE_SIZE)
	return address < (FLASH_BASE + FLASH_BANK_SIZE) ?
			(address - FLASH_BASE) / FLASH_PAGE_SIZE
			:
			(address - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
#else
#error "Must be rewritten"
#endif

}

static uint32_t get_flash_bank(uint32_t address)
{
	return address < (FLASH_BASE + FLASH_BANK_SIZE) ? FLASH_BANK_1 : FLASH_BANK_2;
}
/******************************************************************************
* Definition  | Public Functions
******************************************************************************/

uint32_t get_flash_sector_address(uint32_t address)
{
	return FLASH_BASE + (get_flash_sector(address)*FLASH_SECTION_SIZE);
}

i_status flash_erase(uint32_t addr_from, uint32_t addr_to)
{
	uint32_t flash_sector_error = 0;
	uint32_t first_sector 		= get_flash_sector(addr_from);
	uint32_t nb_sectors 		= get_flash_sector(addr_to) - first_sector + 1;

	if(first_sector == 0xFFFFFFFF)
		return I_FAILED;

	flash_erase_info.Banks         = get_flash_bank(addr_from);
	#if defined(FLASH_TYPEERASE_SECTORS)
	flash_erase_info.TypeErase     = FLASH_TYPEERASE_SECTORS;
	flash_erase_info.Sector        = first_sector;
	flash_erase_info.NbSectors     = nb_sectors;
	#elif defined(FLASH_TYPEERASE_PAGES)
	flash_erase_info.TypeErase     = FLASH_TYPEERASE_PAGES;
	flash_erase_info.Page        = first_sector;
	flash_erase_info.NbPages     = nb_sectors;
	#else
	#error "Must rewrite this function"
	#endif

	HAL_FLASH_Unlock();
	HAL_FLASHEx_Erase(&flash_erase_info, &flash_sector_error);
	HAL_FLASH_Lock();

	return flash_sector_error == 0xFFFFFFFF ? I_OK : I_FAILED;
}

i_status flash_erase_nb(uint32_t addr_from, uint32_t nbp)
{
	uint32_t flash_sector_error = 0;
	if(addr_from == 0xFFFFFFFF || addr_from>FLASH_ADDR_END)
		return I_FAILED;

	flash_erase_info.Banks         = get_flash_bank(addr_from);
	#if defined(FLASH_TYPEERASE_SECTORS)
	flash_erase_info.TypeErase     = FLASH_TYPEERASE_SECTORS;
	flash_erase_info.Sector        = get_flash_sector(addr_from);;
	flash_erase_info.NbSectors     = nbp;
	#elif defined(FLASH_TYPEERASE_PAGES)
	flash_erase_info.TypeErase     = FLASH_TYPEERASE_PAGES;
	flash_erase_info.Page        = get_flash_sector(addr_from);;
	flash_erase_info.NbPages     = nbp;
	#else
	#error "Must rewrite this function"
	#endif

	HAL_FLASH_Unlock();
	HAL_FLASHEx_Erase(&flash_erase_info, &flash_sector_error);
	HAL_FLASH_Lock();

	return flash_sector_error == 0xFFFFFFFF ? I_OK : I_FAILED;
}

i_status flash_write(uint32_t address, uint8_t* data_array, uint32_t data_array_sz)
{
	flash_write_error = 0;

	if((address % 8) != 0)
		return I_MEMUNALIGNED;

	#ifdef HAL_IWDG_MODULE_ENABLED
	HAL_IWDG_Refresh(&hiwdg);
	#endif

	HAL_FLASH_Unlock();

#if defined(STM32H745xx)
	uint32_t _data_array_sz = data_array_sz/(8*4);
	for(uint32_t i = 0; i< _data_array_sz; i++)
	{
		flash_write_error = 0;
		flash_write_cur_address = address+(i*(8*4));
		memmove(flash_write_data_arr,&data_array[i*(8*4)],(8*4));
		while(HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, flash_write_cur_address,(uint32_t)flash_write_data_arr) != HAL_OK)
		{
			HAL_FLASH_Lock();
			flash_write_error++;
			osDelay(5);
			HAL_FLASH_Unlock();
			if(flash_write_error>5)
				break;
		}
	}

#elif defined(FLASH_TYPEPROGRAM_FLASHWORD)
	uint32_t _data_array_sz = data_array_sz/4;
	for(uint32_t i = 0; i< _data_array_sz; i++)
	{
		flash_write_error = 0;
		flash_write_cur_address = address+(i*4);
		memmove((uint64_t*)&flash_write_data,&data_array[i*4],4);
		while(HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, flash_write_cur_address,flash_write_data) != HAL_OK)
		{
			HAL_FLASH_Lock();
			flash_write_error++;
			osDelay(5);
			HAL_FLASH_Unlock();
			if(flash_write_error>5)
				break;
		}
	}

#elif defined(FLASH_TYPEPROGRAM_DOUBLEWORD)
	uint32_t _data_array_sz = data_array_sz/8;
	for(uint32_t i = 0; i< _data_array_sz; i++)
	{
		flash_write_error = 0;
		flash_write_cur_address = address+(i*8);
		memmove((uint64_t*)&flash_write_data,&data_array[i*8],8);
		while(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_write_cur_address,flash_write_data) != HAL_OK)
		{
			HAL_FLASH_Lock();
			flash_write_error++;
			osDelay(5);
			HAL_FLASH_Unlock();
			if(flash_write_error>5)
				break;
		}
	}

#else

#endif


	HAL_FLASH_Lock();

	#ifdef HAL_IWDG_MODULE_ENABLED
	HAL_IWDG_Refresh(&hiwdg);
	#endif

	return flash_write_error == 0 ? I_OK : I_FAILED;
}

/******************************************************************************
* EOF - NO CODE AFTER THIS LINE
******************************************************************************/
For a more complete setup please checkout the STM32 Devcoons Framework
https://devcoons.com/stm32-devcoons-framework-1-how-to-use/
Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags