From 45d89fdba8bd681e82de883f2d1e173b9ec61297 Mon Sep 17 00:00:00 2001 From: andreika-git Date: Fri, 22 Dec 2017 15:49:03 +0200 Subject: [PATCH] Backup NV-RAM refactor (#518) * Backup NV-RAM refactor * More comments on BACKUP_IGNITION_SWITCH_COUNTER --- firmware/hw_layer/backup_ram.cpp | 33 +++++++++++++++++++++++++++++ firmware/hw_layer/backup_ram.h | 36 ++++++++++++++++++++++++++++++++ firmware/hw_layer/hw_layer.mk | 3 ++- firmware/hw_layer/stepper.cpp | 4 ++-- firmware/hw_layer/stepper.h | 1 + 5 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 firmware/hw_layer/backup_ram.cpp create mode 100644 firmware/hw_layer/backup_ram.h diff --git a/firmware/hw_layer/backup_ram.cpp b/firmware/hw_layer/backup_ram.cpp new file mode 100644 index 0000000000..ea98e5c91d --- /dev/null +++ b/firmware/hw_layer/backup_ram.cpp @@ -0,0 +1,33 @@ +/** + * @file backup_ram.cpp + * + * @date Dec 19, 2017 + */ + +#include "backup_ram.h" + +uint32_t backupRamLoad(backup_ram_e idx) { + switch (idx) { + case BACKUP_STEPPER_POS: + return RTCD1.rtc->BKP0R & 0xffff; + case BACKUP_IGNITION_SWITCH_COUNTER: + return (RTCD1.rtc->BKP0R >> 16) & 0xff; + default: + //scheduleMsg(logger, "Invalid backup ram idx %d", idx); + return 0; + } +} + +void backupRamSave(backup_ram_e idx, uint32_t value) { + switch (idx) { + case BACKUP_STEPPER_POS: + RTCD1.rtc->BKP0R = (RTCD1.rtc->BKP0R & ~0x0000ffff) | (value & 0xffff); + break; + case BACKUP_IGNITION_SWITCH_COUNTER: + RTCD1.rtc->BKP0R = (RTCD1.rtc->BKP0R & ~0x00ff0000) | ((value & 0xff) << 16); + break; + default: + //scheduleMsg(logger, "Invalid backup ram idx %d, value 0x08x", idx, value); + break; + } +} diff --git a/firmware/hw_layer/backup_ram.h b/firmware/hw_layer/backup_ram.h new file mode 100644 index 0000000000..1ce331d1e8 --- /dev/null +++ b/firmware/hw_layer/backup_ram.h @@ -0,0 +1,36 @@ +/** + * @file backup_ram.cpp + * @brief Non-volatile backup-RAM registers support + * + * @date Dec 19, 2017 + */ + +#ifndef BACKUP_RAM_H_ +#define BACKUP_RAM_H_ + +#include "main.h" +#include "efiGpio.h" + +typedef enum { + /** + * IAC Stepper motor position, 16-bit (stored in BKP0R 0..15) + * Used in stepper.cpp + */ + BACKUP_STEPPER_POS, + /** + * Ignition switch counter, 8-bit (stored in BKP0R 16..23) + * The counter stores the number of times the ignition switch is turned on. Used for prime injection pulse. + * We need a protection against 'fake' ignition switch on and off (i.e. no engine started), to avoid repeated prime pulses. + * So we check and update the ignition switch counter in non-volatile backup-RAM. + * See startPrimeInjectionPulse() in controllers/trigger/main_trigger_callback.cpp + */ + BACKUP_IGNITION_SWITCH_COUNTER, +} backup_ram_e; + + +// load data from backup-power RTC registers (non-volatile memory) +uint32_t backupRamLoad(backup_ram_e idx); +// use backup-power RTC registers (non-volatile memory) to store the data +void backupRamSave(backup_ram_e idx, uint32_t value); + +#endif /* BACKUP_RAM_H_ */ diff --git a/firmware/hw_layer/hw_layer.mk b/firmware/hw_layer/hw_layer.mk index d219273147..cd088f1ae8 100644 --- a/firmware/hw_layer/hw_layer.mk +++ b/firmware/hw_layer/hw_layer.mk @@ -30,5 +30,6 @@ HW_LAYER_EMS_CPP = $(HW_LAYER_EGT_CPP) \ $(PROJECT_DIR)/hw_layer/servo.cpp \ $(PROJECT_DIR)/hw_layer/io_pins.cpp \ $(PROJECT_DIR)/hw_layer/stm32f4/mpu_util.cpp \ - $(PROJECT_DIR)/hw_layer/rtc_helper.cpp + $(PROJECT_DIR)/hw_layer/rtc_helper.cpp \ + $(PROJECT_DIR)/hw_layer/backup_ram.cpp \ No newline at end of file diff --git a/firmware/hw_layer/stepper.cpp b/firmware/hw_layer/stepper.cpp index 2b35b25fe2..5f90aa8721 100644 --- a/firmware/hw_layer/stepper.cpp +++ b/firmware/hw_layer/stepper.cpp @@ -20,11 +20,11 @@ static Logging *logger; static void saveStepperPos(int pos) { // use backup-power RTC registers to store the data - RTCD1.rtc->BKP0R = (pos + 1); + backupRamSave(BACKUP_STEPPER_POS, pos + 1); } static int loadStepperPos() { - return (int)RTCD1.rtc->BKP0R - 1; + return (int)backupRamLoad(BACKUP_STEPPER_POS) - 1; } static msg_t stThread(StepperMotor *motor) { diff --git a/firmware/hw_layer/stepper.h b/firmware/hw_layer/stepper.h index 084935e552..28357c23a4 100644 --- a/firmware/hw_layer/stepper.h +++ b/firmware/hw_layer/stepper.h @@ -9,6 +9,7 @@ #include "main.h" #include "efiGpio.h" +#include "backup_ram.h" class StepperMotor { public: