feat-hal-2 #4

Merged
andis merged 35 commits from feat-hal-2 into develop 2024-07-31 16:15:36 +00:00
13 changed files with 265 additions and 240 deletions
Showing only changes of commit 1c560a9b0f - Show all commits

View File

@@ -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);

View File

@@ -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

View File

@@ -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 ****/

36
firmware/src/bsp/memory.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef DIGITAL_IN_H_
#define DIGITAL_IN_H_
/**** Includes ****/
#include <stdint.h>
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_ */

View File

@@ -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();

View File

@@ -5,18 +5,20 @@
#include <stdint.h>
#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);

View File

@@ -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

View File

@@ -0,0 +1,50 @@
#ifndef SAFE_AIN_H_
#define SAFE_AIN_H_
/**** Includes ****/
#include <stdint.h>
#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_ */

View File

@@ -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;

View File

@@ -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 ****/

View File

@@ -1,48 +0,0 @@
#ifndef FUSE_H_
#define FUSE_H_
/**** Includes ****/
#include <stdint.h>
#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_ */

View File

@@ -1,49 +0,0 @@
#ifndef VOLTAGE_LOCKOUT_H_
#define VOLTAGE_LOCKOUT_H_
/**** Includes ****/
#include <stdint.h>
#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_ */

View File

@@ -183,6 +183,12 @@
<Compile Include="bsp\dout.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="bsp\memory.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="bsp\memory.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="bsp\pwm_out.cpp">
<SubType>compile</SubType>
</Compile>
@@ -195,24 +201,30 @@
<Compile Include="bsp\mcu\mcu_hal_r8.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="dccd\dccd_hw.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="dccd\dccd_hw.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\button.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\button.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\dccd_hw.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\dccd_hw.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\led_display.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\led_display.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\lockout.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\lockout.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\out_driver.cpp">
<SubType>compile</SubType>
</Compile>
@@ -251,6 +263,7 @@
<Folder Include="bsp" />
<Folder Include="bsp\mcu" />
<Folder Include="hw" />
<Folder Include="dccd" />
<Folder Include="utils" />
</ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />