From 8d37ae885f4c48ad448170998bc63c285b3cb392 Mon Sep 17 00:00:00 2001 From: Petr Ledvina Date: Mon, 5 Jun 2017 20:16:51 +0200 Subject: [PATCH] Use variable for eeprom emulation Linker section is not necessary --- src/main/config/config_eeprom.c | 7 +++ src/main/fc/cli.c | 10 +++- src/main/target/SITL/parameter_group.ld | 13 ---- src/main/target/SITL/target.c | 79 ++++++++++++++----------- src/main/target/SITL/target.h | 2 + 5 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/main/config/config_eeprom.c b/src/main/config/config_eeprom.c index 0993ac50d5..f404e182fe 100644 --- a/src/main/config/config_eeprom.c +++ b/src/main/config/config_eeprom.c @@ -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; diff --git a/src/main/fc/cli.c b/src/main/fc/cli.c index 736236f30d..8e133119ba 100755 --- a/src/main/fc/cli.c +++ b/src/main/fc/cli.c @@ -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))); diff --git a/src/main/target/SITL/parameter_group.ld b/src/main/target/SITL/parameter_group.ld index 6f9bfbe96d..6d13829808 100644 --- a/src/main/target/SITL/parameter_group.ld +++ b/src/main/target/SITL/parameter_group.ld @@ -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; - - - diff --git a/src/main/target/SITL/target.c b/src/main/target/SITL/target.c index 2189ddf3e1..22738ba6a7 100644 --- a/src/main/target/SITL/target.c +++ b/src/main/target/SITL/target.c @@ -19,10 +19,13 @@ #include #include #include +#include #include #include +#include "common/maths.h" + #include "drivers/io.h" #include "drivers/dma.h" #include "drivers/serial.h" @@ -92,17 +95,17 @@ 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]); +// 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]); +// printf("[gyr]%lf,%lf,%lf\n", pkt->imu_angular_velocity_rpy[0], pkt->imu_angular_velocity_rpy[1], pkt->imu_angular_velocity_rpy[2]); #if defined(SKIP_IMU_CALC) #if defined(SET_IMU_FROM_EULER) @@ -142,12 +145,12 @@ void updateState(const fdm_packet* pkt) { if (deltaSim < 0.02 && deltaSim > 0) { // simulator should run faster than 50Hz -// simRate = simRate * 0.5 + (1e6 * deltaSim / (realtime_now - last_realtime)) * 0.5; +// simRate = simRate * 0.5 + (1e6 * deltaSim / (realtime_now - last_realtime)) * 0.5; struct timespec out_ts; timeval_sub(&out_ts, &now_ts, &last_ts); simRate = deltaSim / (out_ts.tv_sec + 1e-9*out_ts.tv_nsec); } -// printf("simRate = %lf, millis64 = %lu, millis64_real = %lu, deltaSim = %lf\n", simRate, millis64(), millis64_real(), deltaSim*1e6); +// printf("simRate = %lf, millis64 = %lu, millis64_real = %lu, deltaSim = %lf\n", simRate, millis64(), millis64_real(), deltaSim*1e6); last_timestamp = pkt->timestamp; last_realtime = micros64_real(); @@ -169,7 +172,7 @@ static void* udpThread(void* data) { while (workerRunning) { n = udpRecv(&stateLink, &fdmPkt, sizeof(fdm_packet), 100); if (n == sizeof(fdm_packet)) { -// printf("[data]new fdm %d\n", n); +// printf("[data]new fdm %d\n", n); updateState(&fdmPkt); } } @@ -304,7 +307,7 @@ uint64_t micros64() { last = now; return out*1e-3; -// return micros64_real(); +// return micros64_real(); } uint64_t millis64() { @@ -316,7 +319,7 @@ uint64_t millis64() { last = now; return out*1e-6; -// return millis64_real(); +// return millis64_real(); } uint32_t micros(void) { @@ -446,7 +449,7 @@ void pwmCompleteMotorUpdate(uint8_t motorCount) { // get one "fdm_packet" can only send one "servo_packet"!! if (pthread_mutex_trylock(&updateLock) != 0) return; udpSend(&pwmLink, &pwmPkt, sizeof(servo_packet)); -// printf("[pwm]%u:%u,%u,%u,%u\n", idlePulse, motorsPwm[0], motorsPwm[1], motorsPwm[2], motorsPwm[3]); +// printf("[pwm]%u:%u,%u,%u,%u\n", idlePulse, motorsPwm[0], motorsPwm[1], motorsPwm[2], motorsPwm[3]); } void pwmWriteServo(uint8_t index, float value) { @@ -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,44 +481,53 @@ 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"); } } FLASH_Status FLASH_ErasePage(uintptr_t Page_Address) { UNUSED(Page_Address); -// printf("[FLASH_ErasePage]%x\n", Page_Address); +// printf("[FLASH_ErasePage]%x\n", 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; } diff --git a/src/main/target/SITL/target.h b/src/main/target/SITL/target.h index 765c44e1a8..928c38a116 100644 --- a/src/main/target/SITL/target.h +++ b/src/main/target/SITL/target.h @@ -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