This commit is contained in:
Nima Askari ----- نیما عسکری 2021-01-18 10:18:18 +03:30 committed by GitHub
parent f2b5627110
commit f2624dd856
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 72 deletions

171
ee.c
View File

@ -5,6 +5,7 @@
#define PAGE 0
#define SECTOR 1
#define PAGE_NUM 2
#if defined(STM32F103xB)
#define _EE_SIZE 1024
@ -12,7 +13,7 @@
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 127)
#error "Please Enter currect address, maximum is (127)"
#error "Please Enter correct address, maximum is (127)"
#endif
#endif
@ -22,7 +23,7 @@
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 63)
#error "Please Enter currect address, maximum is (63)"
#error "Please Enter correct address, maximum is (63)"
#endif
#endif
@ -32,7 +33,7 @@
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 127)
#error "Please Enter currect address, maximum is (127)"
#error "Please Enter correct address, maximum is (127)"
#endif
#endif
@ -42,7 +43,7 @@
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 191)
#error "Please Enter currect address, maximum is (191)"
#error "Please Enter correct address, maximum is (191)"
#endif
#endif
@ -52,7 +53,7 @@
#define _EE_FLASH_BANK FLASH_BANK_1
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 255)
#error "Please Enter currect address, maximum is (255)"
#error "Please Enter correct address, maximum is (255)"
#endif
#endif
@ -61,7 +62,7 @@
#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)
#error "Please Enter currect address, maximum is (15)"
#error "Please Enter correct address, maximum is (15)"
#endif
#endif
@ -70,7 +71,7 @@
#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)
#error "Please Enter currect address, maximum is (31)"
#error "Please Enter correct address, maximum is (31)"
#endif
#endif
@ -79,7 +80,7 @@
#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)
#error "Please Enter currect address, maximum is (63)"
#error "Please Enter correct address, maximum is (63)"
#endif
#endif
@ -88,7 +89,7 @@
#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)
#error "Please Enter currect address, maximum is (63)"
#error "Please Enter correct address, maximum is (63)"
#endif
#endif
@ -97,11 +98,10 @@
#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)
#error "Please Enter currect address, maximum is (127)"
#error "Please Enter correct address, maximum is (127)"
#endif
#endif
#if defined(STM32F405xx) || defined(STM32F407xx) || defined(STM32F415xx) || defined(STM32F417xx)
#define _EE_SIZE (1024 * 128)
#define _EE_ADDR_INUSE (((uint32_t)0x08020000) | (_EE_SIZE*(_EE_USE_FLASH_PAGE_OR_SECTOR - 5)))
@ -109,10 +109,20 @@
#define _EE_VOLTAGE_RANGE _EE_VOLTAGE
#define _EE_PAGE_OR_SECTOR PAGE
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 11)
#error "Please Enter currect address, maximum is (11)"
#error "Please Enter correct address, maximum is (11)"
#endif
#if (_EE_USE_FLASH_PAGE_OR_SECTOR < 5)
#error "Please Enter currect address, minimum is (5)"
#error "Please Enter correct address, minimum is (5)"
#endif
#endif
#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)"
#endif
#endif
@ -123,78 +133,82 @@ uint8_t ee_ram[_EE_USE_RAM_BYTE];
//##########################################################################################################
bool ee_init(void)
{
#if (_EE_USE_RAM_BYTE > 0)
return ee_read(0, _EE_USE_RAM_BYTE, NULL);
#else
#if (_EE_USE_RAM_BYTE > 0)
return ee_read(0, _EE_USE_RAM_BYTE, NULL);
#else
return true;
#endif
#endif
}
//##########################################################################################################
bool ee_format(bool keepRamData)
{
uint32_t error;
HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef flashErase;
#if _EE_PAGE_OR_SECTOR == PAGE
{
uint32_t error;
HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef flashErase;
#if _EE_PAGE_OR_SECTOR == PAGE
flashErase.NbPages = 1;
flashErase.PageAddress = _EE_ADDR_INUSE;
flashErase.TypeErase = FLASH_TYPEERASE_PAGES;
#else
#elif _EE_PAGE_OR_SECTOR == SECTOR
flashErase.NbSectors = 1;
flashErase.Sector = _EE_ADDR_INUSE;
flashErase.TypeErase = FLASH_TYPEERASE_SECTORS;
#endif
#ifdef _EE_FLASH_BANK
flashErase.Banks = _EE_FLASH_BANK;
#endif
#ifdef _EE_VOLTAGE_RANGE
#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
flashErase.VoltageRange = _EE_VOLTAGE_RANGE;
#endif
if (HAL_FLASHEx_Erase(&flashErase, &error) == HAL_OK)
{
HAL_FLASH_Lock();
if(error != 0xFFFFFFFF)
return false;
else
#endif
if (HAL_FLASHEx_Erase(&flashErase, &error) == HAL_OK)
{
HAL_FLASH_Lock();
if (error != 0xFFFFFFFF)
return false;
else
{
#if (_EE_USE_RAM_BYTE > 0)
#if (_EE_USE_RAM_BYTE > 0)
if (keepRamData == false)
memset(ee_ram, 0xFF, _EE_USE_RAM_BYTE);
#endif
return true;
#endif
return true;
}
}
HAL_FLASH_Lock();
return false;
}
HAL_FLASH_Lock();
return false;
}
//##########################################################################################################
bool ee_read(uint32_t startVirtualAddress, uint32_t len, uint8_t* data)
{
if ((startVirtualAddress + len) > _EE_SIZE)
return false;
for (uint32_t i = startVirtualAddress ; i < len + startVirtualAddress ; i++)
{
if ((startVirtualAddress + len) > _EE_SIZE)
return false;
for (uint32_t i = startVirtualAddress; i < len + startVirtualAddress; i++)
{
if (data != NULL)
{
*data = (*(__IO uint8_t*)(i + _EE_ADDR_INUSE));
*data = (*(__IO uint8_t*) (i + _EE_ADDR_INUSE));
data++;
}
#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;
#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;
}
//##########################################################################################################
bool ee_write(uint32_t startVirtualAddress, uint32_t len, uint8_t* data)
bool ee_write(uint32_t startVirtualAddress, uint32_t len, uint8_t *data)
{
if ((startVirtualAddress + len) > _EE_SIZE)
return false;
if ((startVirtualAddress + len) > _EE_SIZE)
return false;
if (data == NULL)
return false;
HAL_FLASH_Unlock();
#ifdef FLASH_TYPEPROGRAM_BYTE
HAL_FLASH_Unlock();
#ifdef FLASH_TYPEPROGRAM_BYTE
for (uint32_t i = 0; i < len ; i++)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, ((i + startVirtualAddress)) + _EE_ADDR_INUSE, (uint64_t)(data[i])) != HAL_OK)
@ -203,7 +217,8 @@ bool ee_write(uint32_t startVirtualAddress, uint32_t len, uint8_t* data)
return false;
}
}
#else
#endif
#ifdef FLASH_TYPEPROGRAM_HALFWORD
for (uint32_t i = 0; i < len ; i+=2)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ((i + startVirtualAddress)) + _EE_ADDR_INUSE, (uint64_t)(data[i] | (data[i+1] << 8))) != HAL_OK)
@ -212,34 +227,52 @@ bool ee_write(uint32_t startVirtualAddress, uint32_t len, uint8_t* data)
return false;
}
}
#endif
#endif
#ifdef FLASH_TYPEPROGRAM_DOUBLEWORD
for (uint32_t i = 0; i < len; i += 8)
{
uint64_t data64 = data[i];
data64 += data[i + 1] * 0x100;
data64 += data[i + 2] * 0x10000;
data64 += data[i + 3] * 0x1000000;
data64 += data[i + 4] * 0x100000000;
data64 += data[i + 5] * 0x10000000000;
data64 += data[i + 6] * 0x1000000000000;
data64 += data[i + 7] * 0x100000000000000;
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, ((i + startVirtualAddress)) + _EE_ADDR_INUSE, data64) != HAL_OK)
{
HAL_FLASH_Lock();
return false;
}
}
#endif
HAL_FLASH_Lock();
return true;
}
//##########################################################################################################
bool ee_writeToRam(uint32_t startVirtualAddress, uint32_t len, uint8_t* data)
{
#if (_EE_USE_RAM_BYTE > 0)
if ((startVirtualAddress + len) > _EE_USE_RAM_BYTE)
return false;
#if (_EE_USE_RAM_BYTE > 0)
if ((startVirtualAddress + len) > _EE_USE_RAM_BYTE)
return false;
if (data == NULL)
return false;
memcpy(&ee_ram[startVirtualAddress], data, len);
memcpy(&ee_ram[startVirtualAddress], data, len);
return true;
#else
#else
return false;
#endif
#endif
}
//##########################################################################################################
bool ee_commit(void)
{
#if (_EE_USE_RAM_BYTE > 0)
#if (_EE_USE_RAM_BYTE > 0)
if (ee_format(true) == false)
return false;
return ee_write(0, _EE_USE_RAM_BYTE, ee_ram);
#else
return ee_write(0, _EE_USE_RAM_BYTE, ee_ram);
#else
return false;
#endif
#endif
}
//##########################################################################################################
uint32_t ee_maxVirtualAddress(void)

7
ee.h
View File

@ -7,7 +7,10 @@
Instagram: http://instagram.com/github.NimaLTD
Youtube: https://www.youtube.com/channel/UCUhY7qY1klJm1d2kulr9ckw
Version: 2.0.1
Version: 2.0.2
(2.0.2)
Add L4().
(2.0.1)
Change function name to ee_commit().
@ -23,7 +26,7 @@
#endif
#include <stdbool.h>
#include "gpio.h"
#include "main.h"
//################################################################################################################
bool ee_init(void);

View File

@ -1,7 +1,7 @@
#ifndef __EECONFIG_H
#define __EECONFIG_H
#define _EE_USE_FLASH_PAGE_OR_SECTOR (63)
#define _EE_USE_FLASH_PAGE_OR_SECTOR (127)
#define _EE_USE_RAM_BYTE (1024)
#define _EE_VOLTAGE FLASH_VOLTAGE_RANGE_3 // use in some devices
#endif