EEE4STM32/ee.c

405 lines
13 KiB
C
Raw Normal View History

2020-05-12 06:05:35 +02:00
#include "ee.h"
#include "eeConfig.h"
#include <string.h>
#define PAGE 0
#define SECTOR 1
2021-01-18 07:48:18 +01:00
#define PAGE_NUM 2
2020-05-12 06:05:35 +02:00
#if defined(STM32F103xB)
2020-05-12 06:12:35 +02:00
#define _EE_SIZE 1024
2020-05-12 06:05:35 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 127)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (127)"
2020-05-12 06:05:35 +02:00
#endif
#endif
#if defined(STM32F103x8)
2023-04-03 10:59:01 +02:00
#define _EE_SIZE 1024
2023-04-03 10:56:32 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
2020-05-12 06:05:35 +02:00
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 63)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (63)"
2020-05-12 06:05:35 +02:00
#endif
#endif
#if defined(STM32F103xC)
2020-05-12 06:12:35 +02:00
#define _EE_SIZE 2048
2020-05-12 06:05:35 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 127)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (127)"
2020-05-12 06:05:35 +02:00
#endif
#endif
#if defined(STM32F103xD)
2020-05-12 06:12:35 +02:00
#define _EE_SIZE 2048
2020-05-12 06:05:35 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 191)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (191)"
2020-05-12 06:05:35 +02:00
#endif
#endif
#if defined(STM32F103xE)
2020-05-12 06:12:35 +02:00
#define _EE_SIZE 2048
2020-05-12 06:05:35 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 255)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (255)"
2020-05-12 06:05:35 +02:00
#endif
#endif
#if defined(STM32F030x4) || defined(STM32F042x4) || defined(STM32F070x4)
2020-05-12 06:12:35 +02:00
#define _EE_SIZE 1024
2020-05-12 06:05:35 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 15)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (15)"
2020-05-12 06:05:35 +02:00
#endif
#endif
#if defined(STM32F030x6) || defined(STM32F042x6) || defined(STM32F070x6)
2020-05-12 06:12:35 +02:00
#define _EE_SIZE 1024
2020-05-12 06:05:35 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 31)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (31)"
2020-05-12 06:05:35 +02:00
#endif
#endif
#if defined(STM32F030x8) || defined(STM32F042x8)
2020-05-12 06:12:35 +02:00
#define _EE_SIZE 1024
2020-05-12 06:05:35 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 63)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (63)"
2020-05-12 06:05:35 +02:00
#endif
#endif
#if defined(STM32F070xB)
2020-05-12 06:12:35 +02:00
#define _EE_SIZE 2048
2020-05-12 06:05:35 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 63)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (63)"
2020-05-12 06:05:35 +02:00
#endif
#endif
#if defined(STM32F070xC)
2020-05-12 06:12:35 +02:00
#define _EE_SIZE 2048
2020-05-12 06:05:35 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 127)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (127)"
2020-05-12 06:05:35 +02:00
#endif
#endif
#if defined(STM32F405xx) || defined(STM32F407xx) || defined(STM32F415xx) || defined(STM32F417xx)
2020-05-12 06:12:35 +02:00
#define _EE_SIZE (1024 * 128)
2020-05-12 06:05:35 +02:00
#define _EE_ADDR_INUSE (((uint32_t)0x08020000) | (_EE_SIZE*(_EE_USE_FLASH_PAGE_OR_SECTOR - 5)))
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_VOLTAGE_RANGE _EE_VOLTAGE
2021-03-05 08:37:46 +01:00
#define _EE_PAGE_OR_SECTOR SECTOR
2020-05-12 06:05:35 +02:00
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 11)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, maximum is (11)"
2020-05-12 06:05:35 +02:00
#endif
#if (_EE_USE_FLASH_PAGE_OR_SECTOR < 5)
2021-03-05 08:37:46 +01:00
#error "Please Enter correct address, minimum is (5)"
#endif
#endif
#if defined(STM32F411xC)
#define _EE_SIZE (1024 * 128)
#define _EE_ADDR_INUSE (((uint32_t)0x08020000) | (_EE_SIZE*(_EE_USE_FLASH_PAGE_OR_SECTOR - 5)))
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_VOLTAGE_RANGE _EE_VOLTAGE
#define _EE_PAGE_OR_SECTOR SECTOR
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 5)
#error "Please Enter correct address, maximum is (5)"
#endif
#if (_EE_USE_FLASH_PAGE_OR_SECTOR < 5)
#error "Please Enter correct address, minimum is (5)"
#endif
#endif
#if defined(STM32F411xE)
#define _EE_SIZE (1024 * 128)
#define _EE_ADDR_INUSE (((uint32_t)0x08020000) | (_EE_SIZE*(_EE_USE_FLASH_PAGE_OR_SECTOR - 5)))
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_VOLTAGE_RANGE _EE_VOLTAGE
#define _EE_PAGE_OR_SECTOR SECTOR
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 7)
#error "Please Enter correct address, maximum is (7)"
#endif
#if (_EE_USE_FLASH_PAGE_OR_SECTOR < 5)
2021-01-18 07:48:18 +01:00
#error "Please Enter correct address, minimum is (5)"
#endif
#endif
2023-04-03 10:56:32 +02:00
#if defined(STM32G030xx) || defined(STM32G050xx)
2023-01-01 10:43:18 +01:00
#define _EE_SIZE 2048
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_PAGE_OR_SECTOR PAGE_NUM
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 31)
#error "Please Enter correct address, maximum is (31)"
#endif
#endif
2023-04-03 10:56:32 +02:00
#if defined(STM32G070xx)
#define _EE_SIZE 2048
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_PAGE_OR_SECTOR PAGE_NUM
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 63)
#error "Please Enter correct address, maximum is (63)"
#endif
#endif
2021-01-18 07:48:18 +01:00
#if defined(STM32L433xx)
#define _EE_SIZE 2048
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE_NUM
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 127)
#error "Please Enter correct address, maximum is (127)"
2020-05-12 06:05:35 +02:00
#endif
#endif
2021-03-05 08:44:58 +01:00
#if defined(STM32L476xx)
#define _EE_SIZE 2048
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#if (_EE_USE_FLASH_PAGE_OR_SECTOR < 256)
#define _EE_FLASH_BANK FLASH_BANK_1
#else
#define _EE_FLASH_BANK FLASH_BANK_2
#define _EE_PAGE_OR_SECTOR PAGE_NUM
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 511)
#error "Please Enter correct address, maximum is (511)"
#endif
#endif
2021-03-05 16:18:22 +01:00
#endif
2021-03-05 08:44:58 +01:00
2023-07-11 08:48:02 +02:00
// 2MB version
#if defined(STM32U575xx) || defined(STM32U585xx)
#define _EE_SIZE 8192
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
#define _EE_ICACHE_CTRL 1
#if (_EE_USE_FLASH_PAGE_OR_SECTOR < 128)
#define _EE_FLASH_BANK FLASH_BANK_1
#else
#define _EE_FLASH_BANK FLASH_BANK_2
#define _EE_PAGE_OR_SECTOR PAGE_NUM
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 256)
#error "Please Enter correct address, maximum is (255)"
#endif
#endif
#endif
2020-05-12 06:05:35 +02:00
#if (_EE_USE_RAM_BYTE > 0)
2020-05-12 07:46:58 +02:00
uint8_t ee_ram[_EE_USE_RAM_BYTE];
2020-05-12 06:05:35 +02:00
#endif
//##########################################################################################################
bool ee_init(void)
{
2021-01-18 07:48:18 +01:00
#if (_EE_USE_RAM_BYTE > 0)
return ee_read(0, _EE_USE_RAM_BYTE, NULL);
#else
2020-05-12 06:05:35 +02:00
return true;
2021-01-18 07:48:18 +01:00
#endif
2020-05-12 06:05:35 +02:00
}
//##########################################################################################################
bool ee_format(bool keepRamData)
2021-01-18 07:48:18 +01:00
{
uint32_t error;
HAL_FLASH_Unlock();
2023-07-11 08:48:02 +02:00
#if _EE_ICACHE_CTRL == 1
HAL_ICACHE_Disable();
#endif
2021-01-18 07:48:18 +01:00
FLASH_EraseInitTypeDef flashErase;
#if _EE_PAGE_OR_SECTOR == PAGE
2020-05-12 06:05:35 +02:00
flashErase.NbPages = 1;
2022-08-16 08:09:54 +02:00
flashErase.PageAddress = _EE_ADDR_INUSE;
2020-05-12 06:05:35 +02:00
flashErase.TypeErase = FLASH_TYPEERASE_PAGES;
2021-01-18 07:48:18 +01:00
#elif _EE_PAGE_OR_SECTOR == SECTOR
2020-05-12 06:05:35 +02:00
flashErase.NbSectors = 1;
2022-11-29 09:20:13 +01:00
flashErase.Sector = _EE_USE_FLASH_PAGE_OR_SECTOR;
2020-05-12 06:05:35 +02:00
flashErase.TypeErase = FLASH_TYPEERASE_SECTORS;
2021-01-18 07:48:18 +01:00
#elif _EE_PAGE_OR_SECTOR == PAGE_NUM
flashErase.NbPages = 1;
flashErase.Page = _EE_USE_FLASH_PAGE_OR_SECTOR;
flashErase.TypeErase = FLASH_TYPEERASE_PAGES;
#endif
#ifdef _EE_FLASH_BANK
flashErase.Banks = _EE_FLASH_BANK;
#endif
#ifdef _EE_VOLTAGE_RANGE
2020-05-12 06:05:35 +02:00
flashErase.VoltageRange = _EE_VOLTAGE_RANGE;
2021-01-18 07:48:18 +01:00
#endif
if (HAL_FLASHEx_Erase(&flashErase, &error) == HAL_OK)
{
HAL_FLASH_Lock();
if (error != 0xFFFFFFFF)
2023-07-11 08:48:02 +02:00
{
#if _EE_ICACHE_CTRL == 1
HAL_ICACHE_Enable();
#endif
return false;
}
2021-01-18 07:48:18 +01:00
else
2020-05-12 06:05:35 +02:00
{
2021-01-18 07:48:18 +01:00
#if (_EE_USE_RAM_BYTE > 0)
2020-05-12 06:05:35 +02:00
if (keepRamData == false)
memset(ee_ram, 0xFF, _EE_USE_RAM_BYTE);
2023-07-11 08:48:02 +02:00
#endif
#if _EE_ICACHE_CTRL == 1
HAL_ICACHE_Enable();
2021-01-18 07:48:18 +01:00
#endif
return true;
2020-05-12 06:05:35 +02:00
}
2021-01-18 07:48:18 +01:00
}
HAL_FLASH_Lock();
2023-07-11 08:48:02 +02:00
#if _EE_ICACHE_CTRL == 1
HAL_ICACHE_Enable();
#endif
2021-01-18 07:48:18 +01:00
return false;
2020-05-12 06:05:35 +02:00
}
//##########################################################################################################
2020-05-12 07:46:58 +02:00
bool ee_read(uint32_t startVirtualAddress, uint32_t len, uint8_t* data)
2020-05-12 06:05:35 +02:00
{
2021-01-18 07:48:18 +01:00
if ((startVirtualAddress + len) > _EE_SIZE)
return false;
for (uint32_t i = startVirtualAddress; i < len + startVirtualAddress; i++)
{
2020-05-12 06:05:35 +02:00
if (data != NULL)
{
2021-01-18 07:48:18 +01:00
*data = (*(__IO uint8_t*) (i + _EE_ADDR_INUSE));
2020-05-12 06:05:35 +02:00
data++;
}
2021-01-18 07:48:18 +01:00
#if (_EE_USE_RAM_BYTE > 0)
if (i < _EE_USE_RAM_BYTE)
ee_ram[i] = (*(__IO uint8_t*) (i + _EE_ADDR_INUSE));
#endif
}
return true;
2020-05-12 06:05:35 +02:00
}
//##########################################################################################################
2021-01-18 07:48:18 +01:00
bool ee_write(uint32_t startVirtualAddress, uint32_t len, uint8_t *data)
2020-05-12 06:05:35 +02:00
{
2021-01-18 07:48:18 +01:00
if ((startVirtualAddress + len) > _EE_SIZE)
return false;
2020-05-12 06:05:35 +02:00
if (data == NULL)
return false;
2021-01-18 07:48:18 +01:00
HAL_FLASH_Unlock();
2023-07-11 08:48:02 +02:00
#if _EE_ICACHE_CTRL == 1
HAL_ICACHE_Disable();
#endif
2021-01-18 07:48:18 +01:00
#ifdef FLASH_TYPEPROGRAM_BYTE
2020-05-12 07:46:58 +02:00
for (uint32_t i = 0; i < len ; i++)
2021-03-05 08:47:26 +01:00
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, ((i + startVirtualAddress)) + _EE_ADDR_INUSE, (uint64_t)(data[i])) != HAL_OK)
{
HAL_FLASH_Lock();
2023-07-11 08:48:02 +02:00
#if _EE_ICACHE_CTRL == 1
HAL_ICACHE_Enable();
#endif
2021-03-05 08:47:26 +01:00
return false;
}
}
2021-01-18 07:48:18 +01:00
#endif
#ifdef FLASH_TYPEPROGRAM_HALFWORD
2020-05-12 07:46:58 +02:00
for (uint32_t i = 0; i < len ; i+=2)
2021-03-05 08:47:26 +01:00
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ((i + startVirtualAddress)) + _EE_ADDR_INUSE, (uint64_t)(data[i] | (data[i+1] << 8))) != HAL_OK)
{
HAL_FLASH_Lock();
2023-07-11 08:48:02 +02:00
#if _EE_ICACHE_CTRL == 1
HAL_ICACHE_Enable();
#endif
2021-03-05 08:47:26 +01:00
return false;
}
}
2021-01-18 07:48:18 +01:00
#endif
#ifdef FLASH_TYPEPROGRAM_DOUBLEWORD
for (uint32_t i = 0; i < len; i += 8)
{
2023-07-11 08:48:02 +02:00
uint8_t DoubleWord[8] =
{
data[i + 0], data[i + 1], data[i + 2], data[i + 3], data[i + 4],\
data[i + 5], data[i + 6], data[i + 7]
};
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, ((i + startVirtualAddress)) + _EE_ADDR_INUSE, (uint32_t)DoubleWord) != HAL_OK)
2021-01-18 07:48:18 +01:00
{
HAL_FLASH_Lock();
2023-07-11 08:48:02 +02:00
#if _EE_ICACHE_CTRL == 1
HAL_ICACHE_Enable();
#endif
return false;
}
}
#endif
#ifdef FLASH_TYPEPROGRAM_QUADWORD
for (uint32_t i = 0; i < len; i += 16)
{
uint8_t QuadWord[16] =
{
data[i + 0], data[i + 1], data[i + 2], data[i + 3], data[i + 4],\
data[i + 5], data[i + 6], data[i + 7], data[i + 8], data[i + 9],\
data[i + 10], data[i + 11], data[i + 12], data[i + 13], data[i + 14],\
data[i + 15]
};
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, ((i + startVirtualAddress)) + _EE_ADDR_INUSE, (uint32_t)QuadWord) != HAL_OK)
{
HAL_FLASH_Lock();
#if _EE_ICACHE_CTRL == 1
HAL_ICACHE_Enable();
#endif
2021-01-18 07:48:18 +01:00
return false;
}
}
#endif
2021-03-05 08:47:26 +01:00
HAL_FLASH_Lock();
2023-07-11 08:48:02 +02:00
#if _EE_ICACHE_CTRL == 1
HAL_ICACHE_Enable();
#endif
2021-03-05 08:47:26 +01:00
return true;
2020-05-12 06:05:35 +02:00
}
//##########################################################################################################
2020-05-12 07:46:58 +02:00
bool ee_writeToRam(uint32_t startVirtualAddress, uint32_t len, uint8_t* data)
2020-05-12 06:05:35 +02:00
{
2021-01-18 07:48:18 +01:00
#if (_EE_USE_RAM_BYTE > 0)
if ((startVirtualAddress + len) > _EE_USE_RAM_BYTE)
return false;
2020-05-12 06:05:35 +02:00
if (data == NULL)
return false;
2021-01-18 07:48:18 +01:00
memcpy(&ee_ram[startVirtualAddress], data, len);
2020-05-12 06:05:35 +02:00
return true;
2021-01-18 07:48:18 +01:00
#else
2020-05-12 06:05:35 +02:00
return false;
2021-01-18 07:48:18 +01:00
#endif
2020-05-12 06:05:35 +02:00
}
//##########################################################################################################
2020-05-14 06:29:07 +02:00
bool ee_commit(void)
2020-05-12 06:05:35 +02:00
{
2021-01-18 07:48:18 +01:00
#if (_EE_USE_RAM_BYTE > 0)
2020-05-12 06:05:35 +02:00
if (ee_format(true) == false)
return false;
2021-01-18 07:48:18 +01:00
return ee_write(0, _EE_USE_RAM_BYTE, ee_ram);
#else
2020-05-12 06:05:35 +02:00
return false;
2021-01-18 07:48:18 +01:00
#endif
2020-05-12 06:05:35 +02:00
}
//##########################################################################################################
uint32_t ee_maxVirtualAddress(void)
{
2020-05-12 07:46:58 +02:00
return (_EE_SIZE);
2020-05-12 06:05:35 +02:00
}
//##########################################################################################################