diff --git a/firmware/src/bsp/board.cpp b/firmware/src/bsp/board.cpp index 6cff7da..7440429 100644 --- a/firmware/src/bsp/board.cpp +++ b/firmware/src/bsp/board.cpp @@ -20,15 +20,17 @@ bsp::Board::~Board(void) return; } -void bsp::Board::init(void) +void bsp::Board::init(boardCfg_t* cfg) { + // Calculate settings + // Controller setup mcu::startupCfg_t mcu_cfg; mcu_cfg.adc_clk = mcu::ADC_DIV64; // 8MHz/64=125kHz mcu_cfg.pwm_clk = mcu::TIM_DIV1; // 8MHz/1 = 8MHz - mcu_cfg.pwm_top = 500; // 8000kHz/500 = 16kHz - mcu_cfg.od_common_is_pwm = 1; // Open-drain common is PWM + mcu_cfg.pwm_top = 4000/(uint16_t)cfg->pwm_f_khz; + mcu_cfg.od_common_is_pwm = cfg->od_common_is_pwm; mcu::startup(&mcu_cfg); diff --git a/firmware/src/bsp/board.h b/firmware/src/bsp/board.h index 665109c..bce363e 100644 --- a/firmware/src/bsp/board.h +++ b/firmware/src/bsp/board.h @@ -16,10 +16,15 @@ namespace bsp { class Board { public: + typedef struct { + uint8_t pwm_f_khz; + uint8_t od_common_is_pwm; + } boardCfg_t; + Board(void); ~Board(void); - void init(void); + void init(boardCfg_t* cfg); AnalogIn out_voltage; AnalogIn out_current; @@ -50,6 +55,8 @@ class Board PwmOut out_pwm; DigitalOut out_low; + Memory nvmem; + void read(void); #ifndef TESTING diff --git a/firmware/src/bsp/memory.cpp b/firmware/src/bsp/memory.cpp new file mode 100644 index 0000000..a186eae --- /dev/null +++ b/firmware/src/bsp/memory.cpp @@ -0,0 +1,55 @@ +/**** Includes ****/ +#include "mcu/mcu_hal.h" +#include "memory.h" + +using namespace bsp; + +/**** Private definitions ****/ +/**** Private constants ****/ +/**** Private variables ****/ +/**** Private function declarations ****/ +/**** Public function definitions ****/ +bsp::Memory::Memory(void) +{ + return; +} + +bsp::Memory::~Memory(void) +{ + return; +} + +uint8_t bsp::Memory::read_8b(uint16_t address) +{ + return mcu::eeprom_read8b(address); +} + + +uint16_t bsp::Memory::read_16b(uint16_t address) +{ + return mcu::eeprom_read16b(address); +} + +uint32_t bsp::Memory::read_32b(uint16_t address) +{ + return mcu::eeprom_read32b(address); +} + +void bsp::Memory::write_8b(uint16_t address, uint8_t value) +{ + mcu::eeprom_write8b(address, value); +} + + +void bsp::Memory::write_16b(uint16_t address, uint16_t value) +{ + mcu::eeprom_write16b(address, value); +} + +void bsp::Memory::write_32b(uint16_t address, uint32_t value) +{ + mcu::eeprom_write32b(address, value); +} + +/**** Private function definitions ****/ + diff --git a/firmware/src/bsp/memory.h b/firmware/src/bsp/memory.h new file mode 100644 index 0000000..141d4ee --- /dev/null +++ b/firmware/src/bsp/memory.h @@ -0,0 +1,36 @@ +#ifndef DIGITAL_IN_H_ +#define DIGITAL_IN_H_ + +/**** Includes ****/ +#include + +namespace bsp { + +/**** Public definitions ****/ +class Memory +{ + public: + Memory(void); + ~Memory(void); + + uint8_t read_8b(uint16_t address); + uint16_t read_16b(uint16_t address); + uint32_t read_32b(uint16_t address); + + void write_8b(uint16_t address, uint8_t value); + void write_16b(uint16_t address, uint16_t value); + void write_32b(uint16_t address, uint32_t value); + + #ifndef TESTING + protected: + #endif +}; + +/**** Public function declarations ****/ + +#ifdef TESTING +#endif + +} //namespace + +#endif /* DIGITAL_IN_H_ */ \ No newline at end of file diff --git a/firmware/src/hw/dccd_hw.cpp b/firmware/src/dccd/dccd_hw.cpp similarity index 55% rename from firmware/src/hw/dccd_hw.cpp rename to firmware/src/dccd/dccd_hw.cpp index fef1c58..4ebc45d 100644 --- a/firmware/src/hw/dccd_hw.cpp +++ b/firmware/src/dccd/dccd_hw.cpp @@ -1,6 +1,5 @@ /**** Includes ****/ #include "../utils/utils.h" -#include "../bsp/mcu/mcu_hal.h" #include "dccd_hw.h" using namespace hw; @@ -10,26 +9,59 @@ using namespace hw; /**** Private variables ****/ /**** Private function declarations ****/ /**** Public function definitions ****/ -hw::DccdHw::DccdHw(void) +dccd::DccdHw::DccdHw(void) { return; } -hw::DccdHw::~DccdHw(void) +dccd::DccdHw::~DccdHw(void) { return; } -void hw::DccdHw::init(dccdHwCfg_t* cfg) +void dccd::DccdHw::init(dccdHwCfg_t* cfg) { - this->board_hw.init(); + bsp::Board::boardCfg_t board_cfg; + + board_cfg.pwm_f_khz = cfg->pwm_f_khz; + board_cfg.od_common_is_pwm = 1; + + this->board_hw.init(&board_cfg); this->counter.init(0xFFFF, 900); - this->out_voltage = 0; - this->out_current = 0; - this->battery_voltage = 12000; - this->battery_current = 0; + this->out_voltage.init(&(this->board_hw.out_voltage), &(this->counter)); + this->out_voltage.under_treshold = 0; + this->out_voltage.over_treshold = 9000; + this->out_voltage.hold_time = 1000; + this->out_voltage.cooldown_time = 0; + this->out_voltage.update_ain = 0; + this->out_voltage.auto_reset = 1; + + this->out_current.init(&(this->board_hw.out_current), &(this->counter)); + this->out_current.under_treshold = 0; + this->out_current.over_treshold = 6000; + this->out_current.hold_time = 200; + this->out_current.cooldown_time = 1000; + this->out_current.update_ain = 0; + this->out_current.auto_reset = 1; + + this->battery_voltage.init(&(this->board_hw.battery_voltage), &(this->counter)); + this->battery_voltage.under_treshold = 9000; + this->battery_voltage.over_treshold = 18000; + this->battery_voltage.hold_time = 1000; + this->battery_voltage.cooldown_time = 0; + this->battery_voltage.update_ain = 0; + this->battery_voltage.auto_reset = 1; + this->battery_voltage.last_read = 12000; + + this->battery_current.init(&(this->board_hw.battery_current), &(this->counter)); + this->battery_current.under_treshold = 0; + this->battery_current.over_treshold = 8000; + this->battery_current.hold_time = 200; + this->battery_current.cooldown_time = 1000; + this->battery_current.update_ain = 0; + this->battery_current.auto_reset = 1; this->btn_up.init(&(this->board_hw.din4), 0, &(this->counter), 10); this->btn_up.update_din = 0; @@ -54,7 +86,7 @@ void hw::DccdHw::init(dccdHwCfg_t* cfg) this->outdriver.init(&(this->board_hw.out_pwm), &(this->board_hw.out_low)); - LedDisplay::doutCfg_t dsp_cfg; + hw::LedDisplay::doutCfg_t dsp_cfg; dsp_cfg.led0_dout_ch = &(this->board_hw.od1); dsp_cfg.led1_dout_ch = &(this->board_hw.od2); dsp_cfg.led2_dout_ch = &(this->board_hw.od3); @@ -85,17 +117,17 @@ void hw::DccdHw::init(dccdHwCfg_t* cfg) this->display.write(0x00); } -void hw::DccdHw::read(void) +void dccd::DccdHw::read(void) { // Update low level inputs this->board_hw.read(); this->counter.increment(); - this->out_voltage = this->board_hw.out_voltage.last_read; - this->out_current = this->board_hw.out_current.last_read; - this->battery_voltage = this->board_hw.battery_voltage.last_read; - this->battery_current = this->board_hw.battery_current.last_read; + this->out_voltage.process(); + this->out_current.process(); + this->battery_voltage.process(); + this->battery_current.process(); this->btn_up.process(); this->btn_down.process(); diff --git a/firmware/src/hw/dccd_hw.h b/firmware/src/dccd/dccd_hw.h similarity index 53% rename from firmware/src/hw/dccd_hw.h rename to firmware/src/dccd/dccd_hw.h index 2a96053..a4d864c 100644 --- a/firmware/src/hw/dccd_hw.h +++ b/firmware/src/dccd/dccd_hw.h @@ -5,18 +5,20 @@ #include #include "../bsp/board.h" #include "../utils/vcounter.h" -#include "button.h" -#include "led_display.h" -#include "potentiometer.h" -#include "out_driver.h" +#include "../hw/button.h" +#include "../hw/led_display.h" +#include "../hw/potentiometer.h" +#include "../hw/out_driver.h" +#include "../hw/safe_ain.h" -namespace hw { +namespace dccd { /**** Public definitions ****/ class DccdHw { public: typedef struct { + uint8_t pwm_f_khz; uint8_t handbrake_pull_up; uint8_t speed_hall; } dccdHwCfg_t; @@ -27,23 +29,25 @@ class DccdHw void init(dccdHwCfg_t* cfg); // Inputs - uint16_t out_voltage; - uint16_t out_current; - uint16_t battery_voltage; - uint16_t battery_current; + hw::SafeAin out_voltage; + hw::SafeAin out_current; + hw::SafeAin battery_voltage; + hw::SafeAin battery_current; - Button btn_up; - Button btn_down; - Button btn_mode; - Button handbrake; - Button brakes; - Button dimm; + hw::Button btn_up; + hw::Button btn_down; + hw::Button btn_mode; + hw::Button handbrake; + hw::Button brakes; + hw::Button dimm; - Potentiometer pot; + hw::Potentiometer pot; // Outputs - LedDisplay display; - OutDriver outdriver; + hw::LedDisplay display; + hw::OutDriver outdriver; + + // Safety void read(void); diff --git a/firmware/src/sort/voltlock.cpp b/firmware/src/hw/safe_ain.cpp similarity index 61% rename from firmware/src/sort/voltlock.cpp rename to firmware/src/hw/safe_ain.cpp index be25a93..5cbc54e 100644 --- a/firmware/src/sort/voltlock.cpp +++ b/firmware/src/hw/safe_ain.cpp @@ -1,8 +1,8 @@ /**** Includes ****/ #include "../utils/utils.h" -#include "voltlock.h" +#include "safe_ain.h" -using namespace bsp; +using namespace hw; /**** Private definitions ****/ /**** Private constants ****/ @@ -10,56 +10,60 @@ using namespace bsp; /**** Private function declarations ****/ /**** Public function definitions ****/ -bsp::VoltLock::VoltLock(void) +hw::SafeAin::SafeAin(void) { return; } -bsp::VoltLock::~VoltLock(void) +hw::SafeAin::~SafeAin(void) { return; } -void bsp::VoltLock::init(bsp::AnalogIn* ain_ch, bsp::VCounter* timerh) +void hw::SafeAin::init(bsp::AnalogIn* ain_ch, util::VCounter* timer) { this->ain_ch = ain_ch; this->timer = timer; - this->undervoltage_lim = 0xFFFF; - this->overvoltage_lim = 0; + this->under_treshold = 0; + this->over_treshold = 0xFFFF; this->hold_time = 0; this->cooldown_time = 0; - this->process_ain = 1; - this->auto_reset = 1; + this->update_ain = 0; + this->auto_reset = 0; this->warning = 0; this->fault = 0; - this->ts_oc_chnage = 0; + this->last_read = 0; + + this->ts_state_chnage = 0; } -void bsp::VoltLock::process(void) +void hw::SafeAin::process(void) { // Update analog input - if(this->process_ain) this->ain_ch->read(); + if(this->update_ain) this->ain_ch->read(); + + this->last_read = this->ain_ch->last_read; // Get current time uint16_t ts_now = this->timer->read(); // Update over current and warning condition uint8_t is_outside = 0; - if(this->ain_ch->last_read <= this->undervoltage_lim) is_outside = 1; - if(this->ain_ch->last_read >= this->overvoltage_lim) is_outside = 1; + if(this->last_read < this->under_treshold) is_outside = 1; + if(this->last_read > this->over_treshold) is_outside = 1; // Note start time if new OC condition - if(is_outside != this->warning) this->ts_oc_chnage = ts_now; + if(is_outside != this->warning) this->ts_state_chnage = ts_now; // Update warning this->warning = is_outside; // Calculate warning condition time - uint16_t td = util::time_delta(this->ts_oc_chnage, ts_now); + uint16_t td = util::time_delta(this->ts_state_chnage, ts_now); uint32_t time_ms = this->timer->convert_ms(td); // Check for fault set diff --git a/firmware/src/hw/safe_ain.h b/firmware/src/hw/safe_ain.h new file mode 100644 index 0000000..a48969a --- /dev/null +++ b/firmware/src/hw/safe_ain.h @@ -0,0 +1,50 @@ +#ifndef SAFE_AIN_H_ +#define SAFE_AIN_H_ + +/**** Includes ****/ +#include +#include "../utils/vcounter.h" +#include "../bsp/board.h" + +namespace hw { + +/**** Public definitions ****/ + +class SafeAin +{ + public: + SafeAin(void); + ~SafeAin(void); + + void init(bsp::AnalogIn* ain_ch, util::VCounter* timer); + + uint8_t warning; + uint8_t fault; + uint16_t last_read; + + uint16_t under_treshold; + uint16_t over_treshold; + uint16_t hold_time; + uint16_t cooldown_time; + + uint8_t update_ain; + uint8_t auto_reset; + + void process(void); + + #ifndef TESTING + protected: + #endif + bsp::AnalogIn* ain_ch; + util::VCounter* timer; + uint16_t ts_state_chnage; +}; + +/**** Public function declarations ****/ + +#ifdef TESTING +#endif + +} //namespace + +#endif /* SAFE_AIN_H_ */ \ No newline at end of file diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 9dcfdd8..aba810f 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -1,19 +1,19 @@ /**** Includes ****/ #include "utils/utils.h" -#include "hw/dccd_hw.h" +#include "dccd/dccd_hw.h" /**** Private definitions ****/ /**** Private constants ****/ /**** Private variables ****/ -hw::DccdHw dccd_hw; +dccd::DccdHw dccd_hw; /**** Private function declarations ****/ /**** Public function definitions ****/ int main(void) { // Setup - hw::DccdHw::dccdHwCfg_t hw_cfg; + dccd::DccdHw::dccdHwCfg_t hw_cfg; hw_cfg.handbrake_pull_up = 0; hw_cfg.speed_hall = 0; diff --git a/firmware/src/sort/fuse.cpp b/firmware/src/sort/fuse.cpp deleted file mode 100644 index 1ed0dd1..0000000 --- a/firmware/src/sort/fuse.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/**** Includes ****/ -#include "../utils/utils.h" -#include "fuse.h" - -using namespace bsp; - -/**** Private definitions ****/ -/**** Private constants ****/ -/**** Private variables ****/ -/**** Private function declarations ****/ - -/**** Public function definitions ****/ -bsp::Fuse::Fuse(void) -{ - return; -} - -bsp::Fuse::~Fuse(void) -{ - return; -} - -void bsp::Fuse::init(bsp::AnalogIn* ain_ch, bsp::VCounter* timerh) -{ - this->ain_ch = ain_ch; - this->timer = timer; - - this->current_lim = 0; - this->hold_time = 0; - this->cooldown_time = 0; - - this->process_ain = 1; - this->auto_reset = 0; - - this->warning = 0; - this->fault = 0; - - this->ts_oc_chnage = 0; -} - -void bsp::Fuse::process(void) -{ - // Update analog input - if(this->process_ain) this->ain_ch->read(); - - // Get current time - uint16_t ts_now = this->timer->read(); - - // Update over current and warning condition - uint8_t is_oc = 0; - if(this->ain_ch->last_read >= this->current_lim) is_oc = 1; - - // Note start time if new OC condition - if(is_oc != this->warning) this->ts_oc_chnage = ts_now; - - // Update warning - this->warning = is_oc; - - // Calculate OC condition time - uint16_t td = util::time_delta(this->ts_oc_chnage, ts_now); - uint32_t time_ms = this->timer->convert_ms(td); - - // Check for fault set - if((this->fault==0)&&(time_ms > (uint32_t)this->hold_time)) - { - this->fault = 1; - return; - }; - - // Check if allowed auto reset - if((this->auto_reset==0)||(this->cooldown_time==0)) return; - - // Check for fault reset - if((this->fault!=0)&&(time_ms > (uint32_t)this->cooldown_time)) - { - this->fault = 0; - return; - }; -} - -/**** Private function definitions ****/ diff --git a/firmware/src/sort/fuse.h b/firmware/src/sort/fuse.h deleted file mode 100644 index 23189b6..0000000 --- a/firmware/src/sort/fuse.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef FUSE_H_ -#define FUSE_H_ - -/**** Includes ****/ -#include -#include "ain.h" -#include "vcounter.h" - -namespace bsp { - -/**** Public definitions ****/ - -class Fuse -{ - public: - Fuse(void); - ~Fuse(void); - - void init(AnalogIn* ain_ch, VCounter* timer); - - uint8_t warning; - uint8_t fault; - - uint16_t current_lim; - uint16_t hold_time; - uint16_t cooldown_time; - - uint8_t process_ain; - uint8_t auto_reset; - - void process(void); - - #ifndef TESTING - protected: - #endif - AnalogIn* ain_ch; - VCounter* timer; - uint16_t ts_oc_chnage; -}; - -/**** Public function declarations ****/ - -#ifdef TESTING -#endif - -} //namespace - -#endif /* POTENTIOMETER_H_ */ \ No newline at end of file diff --git a/firmware/src/sort/voltlock.h b/firmware/src/sort/voltlock.h deleted file mode 100644 index f22f941..0000000 --- a/firmware/src/sort/voltlock.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef VOLTAGE_LOCKOUT_H_ -#define VOLTAGE_LOCKOUT_H_ - -/**** Includes ****/ -#include -#include "ain.h" -#include "vcounter.h" - -namespace bsp { - -/**** Public definitions ****/ - -class VoltLock -{ - public: - VoltLock(void); - ~VoltLock(void); - - void init(AnalogIn* ain_ch, VCounter* timer); - - uint8_t warning; - uint8_t fault; - - uint16_t undervoltage_lim; - uint16_t overvoltage_lim; - uint16_t hold_time; - uint16_t cooldown_time; - - uint8_t process_ain; - uint8_t auto_reset; - - void process(void); - - #ifndef TESTING - protected: - #endif - AnalogIn* ain_ch; - VCounter* timer; - uint16_t ts_oc_chnage; -}; - -/**** Public function declarations ****/ - -#ifdef TESTING -#endif - -} //namespace - -#endif /* VOLTAGE_LOCKOUT_H_ */ \ No newline at end of file diff --git a/firmware/src/uDCCD.cppproj b/firmware/src/uDCCD.cppproj index 64e52e2..5d31aad 100644 --- a/firmware/src/uDCCD.cppproj +++ b/firmware/src/uDCCD.cppproj @@ -183,6 +183,12 @@ compile + + compile + + + compile + compile @@ -195,24 +201,30 @@ compile + + compile + + + compile + compile compile - - compile - - - compile - compile compile + + compile + + + compile + compile @@ -251,6 +263,7 @@ +