1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-14 20:10:18 +03:00

Allow the use of EEPROM_IN_RAM on MCUs.

If the MCU also supports PERSISTENT ram (not erased on a warm boot) it
allows testing of config changes without wearing out your flash.

e.g.

linker script
-------------
comment out __config_start/__config_end

target.c
--------
PERSISTENT uint8_t eepromData[EEPROM_SIZE]; // persistent, so it
survives warm boots.

target.h
--------
extern uint8_t eepromData[EEPROM_SIZE];

SITL actually keeps the EEPROM in a FILE, loaded into RAM using
alternate FLASH_* implementation.
This commit is contained in:
Dominic Clifton 2018-11-24 16:37:36 +01:00
parent 13389970c4
commit e83ba0db0e
8 changed files with 61 additions and 32 deletions

View file

@ -31,10 +31,6 @@
// FIXME remove this for targets that don't need a CLI. Perhaps use a no-op macro when USE_CLI is not enabled // FIXME remove this for targets that don't need a CLI. Perhaps use a no-op macro when USE_CLI is not enabled
// signal that we're in cli mode // signal that we're in cli mode
uint8_t cliMode = 0; uint8_t cliMode = 0;
#ifndef EEPROM_IN_RAM
extern uint8_t __config_start; // configured via linker script when building binaries.
extern uint8_t __config_end;
#endif
#ifdef USE_CLI #ifdef USE_CLI
@ -4280,12 +4276,7 @@ static void cliStatus(char *cmdline)
#endif #endif
cliPrintLinefeed(); cliPrintLinefeed();
#ifdef EEPROM_IN_RAM cliPrintLinef("Config size: %d, Max available config: %d", getEEPROMConfigSize(), getEEPROMStorageSize());
#define CONFIG_SIZE EEPROM_SIZE
#else
#define CONFIG_SIZE (&__config_end - &__config_start)
#endif
cliPrintLinef("Config size: %d, Max available config: %d", getEEPROMConfigSize(), CONFIG_SIZE);
// Sensors // Sensors

View file

@ -36,11 +36,6 @@
#include "drivers/system.h" #include "drivers/system.h"
#ifndef EEPROM_IN_RAM
extern uint8_t __config_start; // configured via linker script when building binaries.
extern uint8_t __config_end;
#endif
static uint16_t eepromConfigSize; static uint16_t eepromConfigSize;
typedef enum { typedef enum {
@ -92,6 +87,12 @@ void initEEPROM(void)
STATIC_ASSERT(sizeof(configFooter_t) == 2, footer_size_failed); STATIC_ASSERT(sizeof(configFooter_t) == 2, footer_size_failed);
STATIC_ASSERT(sizeof(configRecord_t) == 6, record_size_failed); STATIC_ASSERT(sizeof(configRecord_t) == 6, record_size_failed);
#ifdef TARGET_EEPROM_INIT
void targetEEPROMInit(void);
targetEEPROMInit();
#endif
} }
bool isEEPROMVersionValid(void) bool isEEPROMVersionValid(void)
@ -158,6 +159,15 @@ uint16_t getEEPROMConfigSize(void)
return eepromConfigSize; return eepromConfigSize;
} }
size_t getEEPROMStorageSize(void)
{
#ifdef EEPROM_IN_RAM
return EEPROM_SIZE;
#else
return &__config_end - &__config_start;
#endif
}
// find config record for reg + classification (profile info) in EEPROM // find config record for reg + classification (profile info) in EEPROM
// return NULL when record is not found // return NULL when record is not found
// this function assumes that EEPROM content is valid // this function assumes that EEPROM content is valid

View file

@ -29,4 +29,6 @@ bool isEEPROMVersionValid(void);
bool isEEPROMStructureValid(void); bool isEEPROMStructureValid(void);
bool loadEEPROM(void); bool loadEEPROM(void);
void writeConfigToEEPROM(void); void writeConfigToEEPROM(void);
uint16_t getEEPROMConfigSize(void); uint16_t getEEPROMConfigSize(void);
size_t getEEPROMStorageSize(void);

View file

@ -26,11 +26,6 @@
#include "config/config_streamer.h" #include "config/config_streamer.h"
#ifndef EEPROM_IN_RAM
extern uint8_t __config_start; // configured via linker script when building binaries.
extern uint8_t __config_end;
#endif
// @todo this is not strictly correct for F4/F7, where sector sizes are variable // @todo this is not strictly correct for F4/F7, where sector sizes are variable
#if !defined(FLASH_PAGE_SIZE) #if !defined(FLASH_PAGE_SIZE)
// F1 // F1
@ -83,7 +78,9 @@ void config_streamer_start(config_streamer_t *c, uintptr_t base, int size)
c->address = base; c->address = base;
c->size = size; c->size = size;
if (!c->unlocked) { if (!c->unlocked) {
#if defined(STM32F7) || defined(STM32H7) #if defined(EEPROM_IN_RAM)
// NOP
#elif defined(STM32F7) || defined(STM32H7)
HAL_FLASH_Unlock(); HAL_FLASH_Unlock();
#else #else
FLASH_Unlock(); FLASH_Unlock();
@ -91,7 +88,9 @@ void config_streamer_start(config_streamer_t *c, uintptr_t base, int size)
c->unlocked = true; c->unlocked = true;
} }
#if defined(STM32F10X) #if defined(EEPROM_IN_RAM) || defined(EEPROM_IN_FILE)
// NOP
#elif defined(STM32F10X)
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
#elif defined(STM32F303) #elif defined(STM32F303)
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
@ -109,7 +108,9 @@ void config_streamer_start(config_streamer_t *c, uintptr_t base, int size)
c->err = 0; c->err = 0;
} }
#if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx) #if defined(EEPROM_IN_RAM)
// No flash sector method required.
#elif defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
/* /*
Sector 0 0x08000000 - 0x08007FFF 32 Kbytes Sector 0 0x08000000 - 0x08007FFF 32 Kbytes
Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes
@ -339,7 +340,20 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t
if (c->err != 0) { if (c->err != 0) {
return c->err; return c->err;
} }
#if defined(STM32H7) #if defined(EEPROM_IN_RAM)
if (c->address == (uintptr_t)&eepromData[0]) {
memset(eepromData, 0, sizeof(eepromData));
}
uint64_t *dest_addr = (uint64_t *)c->address;
uint64_t *src_addr = (uint64_t*)buffer;
uint8_t row_index = 4;
/* Program the 256 bits flash word */
do
{
*dest_addr++ = *src_addr++;
} while (--row_index != 0);
#elif defined(STM32H7)
if (c->address % FLASH_PAGE_SIZE == 0) { if (c->address % FLASH_PAGE_SIZE == 0) {
FLASH_EraseInitTypeDef EraseInitStruct = { FLASH_EraseInitTypeDef EraseInitStruct = {
.TypeErase = FLASH_TYPEERASE_SECTORS, .TypeErase = FLASH_TYPEERASE_SECTORS,
@ -385,7 +399,7 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t
if (c->address % FLASH_PAGE_SIZE == 0) { if (c->address % FLASH_PAGE_SIZE == 0) {
#if defined(STM32F4) #if defined(STM32F4)
const FLASH_Status status = FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3); //0x08080000 to 0x080A0000 const FLASH_Status status = FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3); //0x08080000 to 0x080A0000
#else // STM32F3 or STM32F1 #else // STM32F3, STM32F1 or EEPROM_IN_FILE
const FLASH_Status status = FLASH_ErasePage(c->address); const FLASH_Status status = FLASH_ErasePage(c->address);
#endif #endif
if (status != FLASH_COMPLETE) { if (status != FLASH_COMPLETE) {
@ -432,7 +446,9 @@ int config_streamer_flush(config_streamer_t *c)
int config_streamer_finish(config_streamer_t *c) int config_streamer_finish(config_streamer_t *c)
{ {
if (c->unlocked) { if (c->unlocked) {
#if defined(STM32F7) || defined(STM32H7) #if defined(EEPROM_IN_RAM)
// NOP
#elif defined(STM32F7) || defined(STM32H7)
HAL_FLASH_Lock(); HAL_FLASH_Lock();
#else #else
FLASH_Lock(); FLASH_Lock();

View file

@ -204,6 +204,11 @@ static void* tcpThread(void* data) {
return NULL; return NULL;
} }
void targetEEPROMInit(void)
{
FLASH_Unlock(); // load existing config file into eepromData
}
// system // system
void systemInit(void) { void systemInit(void) {
int ret; int ret;
@ -212,7 +217,6 @@ void systemInit(void) {
printf("[system]Init...\n"); printf("[system]Init...\n");
SystemCoreClock = 500 * 1e6; // fake 500MHz SystemCoreClock = 500 * 1e6; // fake 500MHz
FLASH_Unlock();
if (pthread_mutex_init(&updateLock, NULL) != 0) { if (pthread_mutex_init(&updateLock, NULL) != 0) {
printf("Create updateLock error!\n"); printf("Create updateLock error!\n");

View file

@ -42,7 +42,7 @@
// file name to save config // file name to save config
#define EEPROM_FILENAME "eeprom.bin" #define EEPROM_FILENAME "eeprom.bin"
#define EEPROM_IN_RAM #define EEPROM_IN_FILE
#define EEPROM_SIZE 32768 #define EEPROM_SIZE 32768
#define U_ID_0 0 #define U_ID_0 0
@ -144,13 +144,10 @@
extern uint32_t SystemCoreClock; extern uint32_t SystemCoreClock;
#ifdef EEPROM_IN_RAM #ifdef EEPROM_IN_FILE
extern uint8_t eepromData[EEPROM_SIZE]; extern uint8_t eepromData[EEPROM_SIZE];
#define __config_start (*eepromData) #define __config_start (*eepromData)
#define __config_end (*ARRAYEND(eepromData)) #define __config_end (*ARRAYEND(eepromData))
#else
extern uint8_t __config_start; // configured via linker script when building binaries.
extern uint8_t __config_end;
#endif #endif
typedef enum typedef enum

View file

@ -337,3 +337,8 @@
#ifdef STM32F7 #ifdef STM32F7
#undef USE_ESCSERIAL #undef USE_ESCSERIAL
#endif #endif
#if !defined(EEPROM_IN_RAM) && !defined(EEPROM_IN_FILE)
extern uint8_t __config_start; // configured via linker script when building binaries.
extern uint8_t __config_end;
#endif

View file

@ -225,6 +225,10 @@ uint8_t getMotorCount() {
return 4; return 4;
} }
size_t getEEPROMStorageSize() {
return 0;
}
void setPrintfSerialPort(struct serialPort_s) {} void setPrintfSerialPort(struct serialPort_s) {}