mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-19 14:25:20 +03:00
Use variable for eeprom emulation
Linker section is not necessary
This commit is contained in:
parent
33ac88b88d
commit
8d37ae885f
5 changed files with 61 additions and 50 deletions
|
@ -24,6 +24,7 @@
|
|||
#include "build/build_config.h"
|
||||
|
||||
#include "common/maths.h"
|
||||
#include "common/utils.h"
|
||||
|
||||
#include "config/config_eeprom.h"
|
||||
#include "config/config_streamer.h"
|
||||
|
@ -31,8 +32,14 @@
|
|||
|
||||
#include "drivers/system.h"
|
||||
|
||||
#ifdef EEPROM_IN_RAM
|
||||
extern uint8_t eepromData[EEPROM_SIZE];
|
||||
# define __config_start (*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
|
||||
|
||||
static uint16_t eepromConfigSize;
|
||||
|
||||
|
|
|
@ -28,8 +28,10 @@
|
|||
// 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
|
||||
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
|
||||
|
||||
|
@ -2882,8 +2884,12 @@ static void cliStatus(char *cmdline)
|
|||
cliPrintf("Stack used: %d, ", stackUsedSize());
|
||||
#endif
|
||||
cliPrintLinef("Stack size: %d, Stack address: 0x%x", stackTotalSize(), stackHighMem());
|
||||
|
||||
cliPrintLinef("I2C Errors: %d, config size: %d, max available config: %d", i2cErrorCounter, getEEPROMConfigSize(), &__config_end - &__config_start);
|
||||
#ifdef EEPROM_IN_RAM
|
||||
#define CONFIG_SIZE EEPROM_SIZE
|
||||
#else
|
||||
#define CONFIG_SIZE (&__config_end - &__config_start)
|
||||
#endif
|
||||
cliPrintLinef("I2C Errors: %d, config size: %d, max available config: %d", i2cErrorCounter, getEEPROMConfigSize(), CONFIG_SIZE);
|
||||
|
||||
const int gyroRate = getTaskDeltaTime(TASK_GYROPID) == 0 ? 0 : (int)(1000000.0f / ((float)getTaskDeltaTime(TASK_GYROPID)));
|
||||
const int rxRate = getTaskDeltaTime(TASK_RX) == 0 ? 0 : (int)(1000000.0f / ((float)getTaskDeltaTime(TASK_RX)));
|
||||
|
|
|
@ -23,17 +23,4 @@ SECTIONS {
|
|||
INSERT AFTER .text;
|
||||
|
||||
|
||||
__FLASH_CONFIG_Size = 0x2000; /* 8kB */
|
||||
SECTIONS {
|
||||
.FLASH_CONFIG BLOCK( DEFINED(__section_alignment__) ? __section_alignment__ : 4 ) :
|
||||
{
|
||||
PROVIDE_HIDDEN (__config_start = . );
|
||||
. = . + __FLASH_CONFIG_Size;
|
||||
PROVIDE_HIDDEN (__config_end = . );
|
||||
}
|
||||
}
|
||||
INSERT AFTER .bss;
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -19,10 +19,13 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "common/maths.h"
|
||||
|
||||
#include "drivers/io.h"
|
||||
#include "drivers/dma.h"
|
||||
#include "drivers/serial.h"
|
||||
|
@ -92,15 +95,15 @@ void updateState(const fdm_packet* pkt) {
|
|||
}
|
||||
|
||||
int16_t x,y,z;
|
||||
x = -pkt->imu_linear_acceleration_xyz[0] * ACC_SCALE;
|
||||
y = -pkt->imu_linear_acceleration_xyz[1] * ACC_SCALE;
|
||||
z = -pkt->imu_linear_acceleration_xyz[2] * ACC_SCALE;
|
||||
x = constrain(-pkt->imu_linear_acceleration_xyz[0] * ACC_SCALE, -32767, 32767);
|
||||
y = constrain(-pkt->imu_linear_acceleration_xyz[1] * ACC_SCALE, -32767, 32767);
|
||||
z = constrain(-pkt->imu_linear_acceleration_xyz[2] * ACC_SCALE, -32767, 32767);
|
||||
fakeAccSet(fakeAccDev, x, y, z);
|
||||
// printf("[acc]%lf,%lf,%lf\n", pkt->imu_linear_acceleration_xyz[0], pkt->imu_linear_acceleration_xyz[1], pkt->imu_linear_acceleration_xyz[2]);
|
||||
|
||||
x = pkt->imu_angular_velocity_rpy[0] * GYRO_SCALE * RAD2DEG;
|
||||
y = -pkt->imu_angular_velocity_rpy[1] * GYRO_SCALE * RAD2DEG;
|
||||
z = -pkt->imu_angular_velocity_rpy[2] * GYRO_SCALE * RAD2DEG;
|
||||
x = constrain(pkt->imu_angular_velocity_rpy[0] * GYRO_SCALE * RAD2DEG, -32767, 32767);
|
||||
y = constrain(-pkt->imu_angular_velocity_rpy[1] * GYRO_SCALE * RAD2DEG, -32767, 32767);
|
||||
z = constrain(-pkt->imu_angular_velocity_rpy[2] * GYRO_SCALE * RAD2DEG, -32767, 32767);
|
||||
fakeGyroSet(fakeGyroDev, x, y, z);
|
||||
// printf("[gyr]%lf,%lf,%lf\n", pkt->imu_angular_velocity_rpy[0], pkt->imu_angular_velocity_rpy[1], pkt->imu_angular_velocity_rpy[2]);
|
||||
|
||||
|
@ -464,15 +467,12 @@ char _estack;
|
|||
char _Min_Stack_Size;
|
||||
|
||||
// fake EEPROM
|
||||
uint8_t __config_start;
|
||||
uint8_t __config_end;
|
||||
static FILE *eepromFd = NULL;
|
||||
uint8_t eepromData[EEPROM_SIZE];
|
||||
|
||||
void FLASH_Unlock(void) {
|
||||
uint8_t * const eeprom = &__config_start;
|
||||
|
||||
if (eepromFd != NULL) {
|
||||
printf("[FLASH_Unlock] eepromFd != NULL\n");
|
||||
fprintf(stderr, "[FLASH_Unlock] eepromFd != NULL\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -481,29 +481,38 @@ void FLASH_Unlock(void) {
|
|||
if (eepromFd != NULL) {
|
||||
// obtain file size:
|
||||
fseek(eepromFd , 0 , SEEK_END);
|
||||
long lSize = ftell(eepromFd);
|
||||
size_t lSize = ftell(eepromFd);
|
||||
rewind(eepromFd);
|
||||
|
||||
printf("[FLASH_Unlock]size = %ld, %ld\n", lSize, (long)(&__config_end - &__config_start));
|
||||
for (unsigned i = 0; i < (uintptr_t)(&__config_end - &__config_start); i++) {
|
||||
int c = fgetc(eepromFd);
|
||||
if (c == EOF) break;
|
||||
eeprom[i] = (uint8_t)c;
|
||||
size_t n = fread(eepromData, 1, sizeof(eepromData), eepromFd);
|
||||
if (n == lSize) {
|
||||
printf("[FLASH_Unlock] loaded '%s', size = %ld / %ld\n", EEPROM_FILENAME, lSize, sizeof(eepromData));
|
||||
} else {
|
||||
fprintf(stderr, "[FLASH_Unlock] failed to load '%s'\n", EEPROM_FILENAME);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
eepromFd = fopen(EEPROM_FILENAME, "w+");
|
||||
fwrite(eeprom, sizeof(uint8_t), &__config_end - &__config_start, eepromFd);
|
||||
printf("[FLASH_Unlock] created '%s', size = %ld\n", EEPROM_FILENAME, sizeof(eepromData));
|
||||
if ((eepromFd = fopen(EEPROM_FILENAME, "w+")) == NULL) {
|
||||
fprintf(stderr, "[FLASH_Unlock] failed to create '%s'\n", EEPROM_FILENAME);
|
||||
return;
|
||||
}
|
||||
if (fwrite(eepromData, sizeof(eepromData), 1, eepromFd) != 1) {
|
||||
fprintf(stderr, "[FLASH_Unlock] write failed: %s\n", strerror(errno));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FLASH_Lock(void) {
|
||||
// flush & close
|
||||
if (eepromFd != NULL) {
|
||||
const uint8_t *eeprom = &__config_start;
|
||||
fseek(eepromFd, 0, SEEK_SET);
|
||||
fwrite(eeprom, 1, &__config_end - &__config_start, eepromFd);
|
||||
fwrite(eepromData, 1, sizeof(eepromData), eepromFd);
|
||||
fclose(eepromFd);
|
||||
eepromFd = NULL;
|
||||
printf("[FLASH_Lock] saved '%s'\n", EEPROM_FILENAME);
|
||||
} else {
|
||||
fprintf(stderr, "[FLASH_Lock] eeprom is not unlocked\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -513,12 +522,12 @@ FLASH_Status FLASH_ErasePage(uintptr_t Page_Address) {
|
|||
return FLASH_COMPLETE;
|
||||
}
|
||||
|
||||
FLASH_Status FLASH_ProgramWord(uintptr_t addr, uint32_t Data) {
|
||||
if ((addr >= (uintptr_t)&__config_start)&&(addr < (uintptr_t)&__config_end)) {
|
||||
*((uint32_t*)addr) = Data;
|
||||
printf("[FLASH_ProgramWord]0x%p = %x\n", (void*)addr, *((uint32_t*)addr));
|
||||
FLASH_Status FLASH_ProgramWord(uintptr_t addr, uint32_t value) {
|
||||
if ((addr >= (uintptr_t)eepromData) && (addr < (uintptr_t)ARRAYEND(eepromData))) {
|
||||
*((uint32_t*)addr) = value;
|
||||
printf("[FLASH_ProgramWord]%p = %08x\n", (void*)addr, *((uint32_t*)addr));
|
||||
} else {
|
||||
printf("[FLASH_ProgramWord]Out of Range! 0x%p\n", (void*)addr);
|
||||
printf("[FLASH_ProgramWord]%p out of range!\n", (void*)addr);
|
||||
}
|
||||
return FLASH_COMPLETE;
|
||||
}
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
|
||||
// file name to save config
|
||||
#define EEPROM_FILENAME "eeprom.bin"
|
||||
#define EEPROM_IN_RAM
|
||||
#define EEPROM_SIZE 8192
|
||||
|
||||
#define U_ID_0 0
|
||||
#define U_ID_1 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue