Compare commits
27 Commits
main
...
e4bd6d2e04
| Author | SHA1 | Date | |
|---|---|---|---|
| e4bd6d2e04 | |||
| c3bc42fa18 | |||
| a3d4ffd548 | |||
| 8449ca098e | |||
| 57ab8bda6a | |||
| e7797e8d1c | |||
| 82838c25cb | |||
| 2647e1cf14 | |||
| 0a2a040cf0 | |||
| f8b62d4b00 | |||
| 5bb3ebe1bf | |||
| 22ac8240a2 | |||
|
|
f1edc6e15a | ||
|
|
a05c53401f | ||
|
|
8bac1f4787 | ||
| 4adcb7eba9 | |||
| f320bfefb7 | |||
| 8f7e5036e7 | |||
| 78de20e05b | |||
| 417ecf4128 | |||
| dda6c7a2ad | |||
| 6199f3c43f | |||
| 6d5c8d226f | |||
| 0b9d6fa780 | |||
| 989d5a1f13 | |||
| c50b3d90bf | |||
| dd4ff43515 |
BIN
docs/uDCCD_Controller_R9V1_Schematic.PDF
Normal file
BIN
docs/uDCCD_Controller_R9V1_Schematic.PDF
Normal file
Binary file not shown.
35
firmware/src/bsp/ain.cpp
Normal file
35
firmware/src/bsp/ain.cpp
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "mcu/mcu_hal.h"
|
||||||
|
#include "ain.h"
|
||||||
|
|
||||||
|
using namespace bsp;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
bsp::AnalogIn::AnalogIn(uint8_t adc_ch)
|
||||||
|
{
|
||||||
|
this->adc_ch = adc_ch;
|
||||||
|
this->mul = DEF_AIN_MUL;
|
||||||
|
this->div = DEF_AIN_DIV;
|
||||||
|
this->offset = DEF_AIN_OFFSET;
|
||||||
|
this->last_read = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t bsp::AnalogIn::read(void)
|
||||||
|
{
|
||||||
|
//Read ADC
|
||||||
|
uint16_t raw = mcu::adc_read(this->adc_ch);
|
||||||
|
|
||||||
|
//Convert to mV
|
||||||
|
this->last_read = util::convert_muldivoff(raw, this->mul, this->div, this->offset);
|
||||||
|
|
||||||
|
return this->last_read;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
|
|
||||||
37
firmware/src/bsp/ain.h
Normal file
37
firmware/src/bsp/ain.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef ANALOG_IN_H_
|
||||||
|
#define ANALOG_IN_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace bsp {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
static const uint8_t DEF_AIN_MUL = 215;
|
||||||
|
static const uint8_t DEF_AIN_DIV = 44;
|
||||||
|
static const int16_t DEF_AIN_OFFSET = 0;
|
||||||
|
|
||||||
|
class AnalogIn
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
uint8_t adc_ch;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AnalogIn(uint8_t adc_ch);
|
||||||
|
|
||||||
|
uint8_t mul;
|
||||||
|
uint8_t div;
|
||||||
|
int16_t offset;
|
||||||
|
uint16_t last_read;
|
||||||
|
|
||||||
|
uint16_t read(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* ANALOG_IN_H_ */
|
||||||
63
firmware/src/bsp/board.cpp
Normal file
63
firmware/src/bsp/board.cpp
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "board.h"
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
void board_init(void)
|
||||||
|
{
|
||||||
|
// MCU setup
|
||||||
|
mcu::startupCfg_t mcu_cfg;
|
||||||
|
mcu_cfg.adc_clk = mcu::ADC_DIV2;
|
||||||
|
mcu_cfg.pwm_clk = mcu::TIM_DIV1;
|
||||||
|
mcu_cfg.pwm_top = 200;
|
||||||
|
mcu_cfg.pwm_ch1_en = 1;
|
||||||
|
|
||||||
|
mcu::startup(&mcu_cfg);
|
||||||
|
|
||||||
|
// Board setup
|
||||||
|
// Fixed function AIN mV and mA scale
|
||||||
|
dccd_i.mul = 215;
|
||||||
|
dccd_i.div = 22;
|
||||||
|
dccd_i.offset = 0;
|
||||||
|
dccd_i.last_read = 0;
|
||||||
|
|
||||||
|
dccd_u.mul = 20;
|
||||||
|
dccd_u.div = 1;
|
||||||
|
dccd_u.offset = 0;
|
||||||
|
dccd_u.last_read = 0;
|
||||||
|
|
||||||
|
bat_u.mul = 20;
|
||||||
|
bat_u.div = 1;
|
||||||
|
bat_u.offset = 0;
|
||||||
|
bat_u.last_read = 12000;
|
||||||
|
|
||||||
|
bat_i.mul = 235;
|
||||||
|
bat_i.div = 6;
|
||||||
|
bat_i.offset = 0;
|
||||||
|
bat_i.last_read = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void board_read(void)
|
||||||
|
{
|
||||||
|
dccd_i.read();
|
||||||
|
dccd_u.read();
|
||||||
|
bat_u.read();
|
||||||
|
bat_i.read();
|
||||||
|
ain1.read();
|
||||||
|
ain2.read();
|
||||||
|
|
||||||
|
din1.read();
|
||||||
|
din2.read();
|
||||||
|
din3.read();
|
||||||
|
din4.read();
|
||||||
|
hvdin1.read();
|
||||||
|
hvdin2.read();
|
||||||
|
hvdin3.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
41
firmware/src/bsp/board.h
Normal file
41
firmware/src/bsp/board.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#ifndef BSP_BOARD_H_
|
||||||
|
#define BSP_BOARD_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "mcu/mcu_hal.h"
|
||||||
|
#include "ain.h"
|
||||||
|
#include "din.h"
|
||||||
|
#include "dout.h"
|
||||||
|
#include "dio.h"
|
||||||
|
#include "halfbridge.h"
|
||||||
|
#include "pwm.h"
|
||||||
|
|
||||||
|
static bsp::AnalogIn dccd_i = bsp::AnalogIn(mcu::ADC0);
|
||||||
|
static bsp::AnalogIn dccd_u = bsp::AnalogIn(mcu::ADC1);
|
||||||
|
static bsp::AnalogIn bat_u = bsp::AnalogIn(mcu::ADC2);
|
||||||
|
static bsp::AnalogIn bat_i = bsp::AnalogIn(mcu::ADC3);
|
||||||
|
static bsp::Hafbridge hbridge = bsp::Hafbridge(mcu::PWM0, mcu::GPIO15, 95);
|
||||||
|
static bsp::AnalogIn ain1 = bsp::AnalogIn(mcu::ADC5); // mode
|
||||||
|
static bsp::AnalogIn ain2 = bsp::AnalogIn(mcu::ADC4); // pot
|
||||||
|
static bsp::DigitalIn din1 = bsp::DigitalIn(mcu::GPIO0, 0, bsp::DIN_HIGH); //mode
|
||||||
|
static bsp::DigitalIn din2 = bsp::DigitalIn(mcu::GPIO1, 0, bsp::DIN_HIGH); //pot
|
||||||
|
static bsp::DigitalIn din3 = bsp::DigitalIn(mcu::GPIO2, 0, bsp::DIN_HIGH); //down
|
||||||
|
static bsp::DigitalIn din4 = bsp::DigitalIn(mcu::GPIO3, 0, bsp::DIN_HIGH); //up
|
||||||
|
static bsp::DigitalIn hvdin1 = bsp::DigitalIn(mcu::GPIO4, 1, bsp::DIN_LOW); //dimm
|
||||||
|
static bsp::DigitalIn hvdin2 = bsp::DigitalIn(mcu::GPIO5, 1, bsp::DIN_LOW); //brakes
|
||||||
|
static bsp::DigitalIn hvdin3 = bsp::DigitalIn(mcu::GPIO6, 1, bsp::DIN_LOW); //hbrake
|
||||||
|
static bsp::DigitalIO hvdin3_pull = bsp::DigitalIO(mcu::GPIO7, bsp::DIN_HIGH); //hbrake pull
|
||||||
|
static bsp::DigitalOut odout1 = bsp::DigitalOut(mcu::GPIO9, 1);
|
||||||
|
static bsp::DigitalOut odout2 = bsp::DigitalOut(mcu::GPIO10, 1);
|
||||||
|
static bsp::DigitalOut odout3 = bsp::DigitalOut(mcu::GPIO11, 1);
|
||||||
|
static bsp::DigitalOut odout4 = bsp::DigitalOut(mcu::GPIO12, 1);
|
||||||
|
static bsp::DigitalOut odout5 = bsp::DigitalOut(mcu::GPIO13, 1);
|
||||||
|
static bsp::DigitalOut odout6 = bsp::DigitalOut(mcu::GPIO14, 1);
|
||||||
|
static bsp::PWMout od_pwm = bsp::PWMout(mcu::PWM1);
|
||||||
|
|
||||||
|
void board_init(void);
|
||||||
|
void board_read(void);
|
||||||
|
|
||||||
|
#endif /* BSP_BOARD_H_ */
|
||||||
40
firmware/src/bsp/din.cpp
Normal file
40
firmware/src/bsp/din.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "mcu/mcu_hal.h"
|
||||||
|
#include "din.h"
|
||||||
|
|
||||||
|
using namespace bsp;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
bsp::DigitalIn::DigitalIn(uint8_t gpio_ch, uint8_t inverted, uint8_t init_value)
|
||||||
|
{
|
||||||
|
this->gpio_ch = gpio_ch;
|
||||||
|
this->invert = inverted;
|
||||||
|
|
||||||
|
if(init_value) this->last_read = DIN_HIGH;
|
||||||
|
else this->last_read = DIN_LOW;
|
||||||
|
}
|
||||||
|
|
||||||
|
bsp::DigitalIn::~DigitalIn(void)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t bsp::DigitalIn::read(void)
|
||||||
|
{
|
||||||
|
uint8_t lvl = mcu::gpio_read(this->gpio_ch);
|
||||||
|
|
||||||
|
if(this->invert) lvl = util::invert(lvl);
|
||||||
|
|
||||||
|
if(lvl>0) this->last_read = DIN_HIGH;
|
||||||
|
else this->last_read = DIN_LOW;
|
||||||
|
|
||||||
|
return this->last_read;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
35
firmware/src/bsp/din.h
Normal file
35
firmware/src/bsp/din.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#ifndef DIGITAL_INPUT_H_
|
||||||
|
#define DIGITAL_INPUT_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace bsp {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
const uint8_t DIN_LOW = 0;
|
||||||
|
const uint8_t DIN_HIGH = 1;
|
||||||
|
|
||||||
|
class DigitalIn
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
uint8_t gpio_ch;
|
||||||
|
uint8_t invert;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DigitalIn(uint8_t gpio_ch, uint8_t inverted, uint8_t init_value);
|
||||||
|
~DigitalIn(void);
|
||||||
|
|
||||||
|
uint8_t last_read;
|
||||||
|
|
||||||
|
uint8_t read(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* DIGITAL_INPUT_H_ */
|
||||||
34
firmware/src/bsp/dio.cpp
Normal file
34
firmware/src/bsp/dio.cpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "mcu/mcu_hal.h"
|
||||||
|
#include "dio.h"
|
||||||
|
|
||||||
|
using namespace bsp;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
bsp::DigitalIO::DigitalIO(uint8_t gpio_ch, uint8_t init_value) : DigitalIn(gpio_ch, 0, init_value), DigitalOut(gpio_ch, 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bsp::DigitalIO::~DigitalIO(void)
|
||||||
|
{
|
||||||
|
this->write(DOUT_HIZ);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t bsp::DigitalIO::is_io_match(void)
|
||||||
|
{
|
||||||
|
if(this->last_set == DOUT_HIZ) return 1;
|
||||||
|
|
||||||
|
uint8_t read_lvl = this->read();
|
||||||
|
|
||||||
|
if(read_lvl == (uint8_t)this->last_set) return 1;
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
32
firmware/src/bsp/dio.h
Normal file
32
firmware/src/bsp/dio.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef DIGITAL_IO_H_
|
||||||
|
#define DIGITAL_IO_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "din.h"
|
||||||
|
#include "dout.h"
|
||||||
|
|
||||||
|
namespace bsp {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
const int8_t DIO_LOW = 0;
|
||||||
|
const int8_t DIO_HIGH = 1;
|
||||||
|
const int8_t DIO_HIZ = -1;
|
||||||
|
|
||||||
|
class DigitalIO : public DigitalIn, public DigitalOut
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DigitalIO(uint8_t gpio_ch, uint8_t init_value);
|
||||||
|
~DigitalIO(void);
|
||||||
|
|
||||||
|
uint8_t is_io_match(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* DIGITAL_IO_H_ */
|
||||||
52
firmware/src/bsp/dout.cpp
Normal file
52
firmware/src/bsp/dout.cpp
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "mcu/mcu_hal.h"
|
||||||
|
#include "dout.h"
|
||||||
|
|
||||||
|
using namespace bsp;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
bsp::DigitalOut::DigitalOut(uint8_t gpio_ch, uint8_t inverted)
|
||||||
|
{
|
||||||
|
this->gpio_ch = gpio_ch;
|
||||||
|
this->invert = inverted;
|
||||||
|
this->write(DOUT_HIZ);
|
||||||
|
}
|
||||||
|
|
||||||
|
bsp::DigitalOut::~DigitalOut(void)
|
||||||
|
{
|
||||||
|
this->write(DOUT_HIZ);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bsp::DigitalOut::write(int8_t level)
|
||||||
|
{
|
||||||
|
if(level > 0)
|
||||||
|
{
|
||||||
|
this->last_set = DOUT_HIGH;
|
||||||
|
if(this->invert) mcu::gpio_write(this->gpio_ch, mcu::LEVEL_LOW);
|
||||||
|
else mcu::gpio_write(this->gpio_ch, mcu::LEVEL_HIGH);
|
||||||
|
}
|
||||||
|
else if(level == 0)
|
||||||
|
{
|
||||||
|
this->last_set = DOUT_LOW;
|
||||||
|
if(this->invert) mcu::gpio_write(this->gpio_ch, mcu::LEVEL_HIGH);
|
||||||
|
else mcu::gpio_write(this->gpio_ch, mcu::LEVEL_LOW);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this->last_set = DOUT_HIZ;
|
||||||
|
mcu::gpio_write(this->gpio_ch, mcu::LEVEL_HIZ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t bsp::DigitalOut::get_set_level(void)
|
||||||
|
{
|
||||||
|
return this->last_set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
36
firmware/src/bsp/dout.h
Normal file
36
firmware/src/bsp/dout.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef DIGITAL_OUTPUT_H_
|
||||||
|
#define DIGITAL_OUTPUT_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace bsp {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
const int8_t DOUT_LOW = 0;
|
||||||
|
const int8_t DOUT_HIGH = 1;
|
||||||
|
const int8_t DOUT_HIZ = -1;
|
||||||
|
|
||||||
|
class DigitalOut
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
uint8_t gpio_ch;
|
||||||
|
uint8_t invert;
|
||||||
|
int8_t last_set;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DigitalOut(uint8_t gpio_ch, uint8_t inverted);
|
||||||
|
~DigitalOut(void);
|
||||||
|
|
||||||
|
void write(int8_t level);
|
||||||
|
int8_t get_set_level(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* DIGITAL_OUTPUT_H_ */
|
||||||
74
firmware/src/bsp/halfbridge.cpp
Normal file
74
firmware/src/bsp/halfbridge.cpp
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "mcu/mcu_hal.h"
|
||||||
|
#include "halfbridge.h"
|
||||||
|
|
||||||
|
using namespace bsp;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
bsp::Hafbridge::Hafbridge(uint8_t hs_pwm_ch, uint8_t ls_gpio_ch, uint8_t max_dc)
|
||||||
|
{
|
||||||
|
this->pwm_ch = hs_pwm_ch;
|
||||||
|
this->gpio_ch = ls_gpio_ch;
|
||||||
|
|
||||||
|
if(max_dc>100) max_dc = 100;
|
||||||
|
|
||||||
|
this->max_dc = util::percent_to_16b(max_dc);
|
||||||
|
|
||||||
|
this->disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
bsp::Hafbridge::~Hafbridge(void)
|
||||||
|
{
|
||||||
|
this->last_duty = 0;
|
||||||
|
this->disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
void bsp::Hafbridge::write(uint16_t dividend)
|
||||||
|
{
|
||||||
|
// Limit duty
|
||||||
|
if(dividend > this->max_dc) dividend = this->max_dc;
|
||||||
|
this->last_duty = dividend;
|
||||||
|
|
||||||
|
if(this->enabled == 0) return;
|
||||||
|
|
||||||
|
// Set PWM
|
||||||
|
mcu::pwm_write(this->pwm_ch, dividend);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bsp::Hafbridge::write(uint8_t percent)
|
||||||
|
{
|
||||||
|
// Convert to dividend/0xFFFF
|
||||||
|
this->write(util::percent_to_16b(percent));
|
||||||
|
}
|
||||||
|
|
||||||
|
void bsp::Hafbridge::enable(void)
|
||||||
|
{
|
||||||
|
mcu::gpio_write(this->gpio_ch, mcu::LEVEL_HIGH);
|
||||||
|
this->enabled = 1;
|
||||||
|
this->write(this->last_duty);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bsp::Hafbridge::disable(void)
|
||||||
|
{
|
||||||
|
mcu::pwm_write(this->pwm_ch, 0);
|
||||||
|
mcu::gpio_write(this->gpio_ch, mcu::LEVEL_LOW);
|
||||||
|
this->enabled = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t bsp::Hafbridge::get_set_duty(void)
|
||||||
|
{
|
||||||
|
return this->last_duty;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t bsp::Hafbridge::is_enabled(void)
|
||||||
|
{
|
||||||
|
return this->enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
38
firmware/src/bsp/halfbridge.h
Normal file
38
firmware/src/bsp/halfbridge.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#ifndef HALFBRIDGE_H_
|
||||||
|
#define HALFBRIDGE_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace bsp {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
class Hafbridge
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
uint8_t pwm_ch;
|
||||||
|
uint8_t gpio_ch;
|
||||||
|
uint16_t last_duty;
|
||||||
|
uint8_t enabled;
|
||||||
|
uint16_t max_dc;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Hafbridge(uint8_t hs_pwm_ch, uint8_t ls_gpio_ch, uint8_t max_dc);
|
||||||
|
~Hafbridge(void);
|
||||||
|
|
||||||
|
void write(uint16_t dividend);
|
||||||
|
void write(uint8_t percent);
|
||||||
|
void enable(void);
|
||||||
|
void disable(void);
|
||||||
|
uint8_t get_set_duty(void);
|
||||||
|
uint8_t is_enabled(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* HALFBRIDGE_H_ */
|
||||||
98
firmware/src/bsp/mcu/mcu_hal.h
Normal file
98
firmware/src/bsp/mcu/mcu_hal.h
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
#ifndef MCU_HAL_H_
|
||||||
|
#define MCU_HAL_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace mcu {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
/*
|
||||||
|
*/
|
||||||
|
|
||||||
|
const uint8_t GPIO0 = 0; //PC5 Mode
|
||||||
|
const uint8_t GPIO1 = 1; //PC4 Pot
|
||||||
|
const uint8_t GPIO2 = 2; //PE1 Down
|
||||||
|
const uint8_t GPIO3 = 3; //PE3 Up
|
||||||
|
const uint8_t GPIO4 = 4; //PD7 Dimm
|
||||||
|
const uint8_t GPIO5 = 5; //PB7 Brakes
|
||||||
|
const uint8_t GPIO6 = 6; //PB6 Handbrake
|
||||||
|
const uint8_t GPIO7 = 7; //PB5 Handbrake pull
|
||||||
|
const uint8_t GPIO8 = 8; //PD6 Speed pull
|
||||||
|
const uint8_t GPIO9 = 9; //PD0 LED0
|
||||||
|
const uint8_t GPIO10 = 10; //PD1 LED1
|
||||||
|
const uint8_t GPIO11 = 11; //PD2 LED2
|
||||||
|
const uint8_t GPIO12 = 12; //PD3 LED3
|
||||||
|
const uint8_t GPIO13 = 13; //PD4 LED4
|
||||||
|
const uint8_t GPIO14 = 14; //PD5 LED5
|
||||||
|
const uint8_t GPIO15 = 15; //PB0 DCCD Enable
|
||||||
|
const uint8_t GPIO16 = 16; //PB1 DCCD PWM
|
||||||
|
const uint8_t GPIO17 = 17; //PB2 LED PWM
|
||||||
|
|
||||||
|
const uint8_t LEVEL_LOW = 0;
|
||||||
|
const uint8_t LEVEL_HIGH = 1;
|
||||||
|
const int8_t LEVEL_HIZ = -1;
|
||||||
|
|
||||||
|
const uint8_t ADC0 = 0; //Output current
|
||||||
|
const uint8_t ADC1 = 1; //Output voltage
|
||||||
|
const uint8_t ADC2 = 2; //Battery voltage
|
||||||
|
const uint8_t ADC3 = 3; //Battery current
|
||||||
|
const uint8_t ADC4 = 4; //Potentiometer
|
||||||
|
const uint8_t ADC5 = 5; //Mode
|
||||||
|
const uint8_t ADC8 = 8; //MCU temperature
|
||||||
|
const uint8_t ADC14 = 14; //MCU internal reference
|
||||||
|
const uint8_t ADC15 = 15; //MCU ground
|
||||||
|
|
||||||
|
const uint8_t PWM0 = 0; //DCCD
|
||||||
|
const uint8_t PWM1 = 1; //LED
|
||||||
|
|
||||||
|
//ADC definitions
|
||||||
|
typedef enum {
|
||||||
|
ADC_DIV2 = 0x01,
|
||||||
|
ADC_DIV4 = 0x02,
|
||||||
|
ADC_DIV8 = 0x03,
|
||||||
|
ADC_DIV16 = 0x04,
|
||||||
|
ADC_DIV32 = 0x05,
|
||||||
|
ADC_DIV64 = 0x06,
|
||||||
|
ADC_DIV128 = 0x07
|
||||||
|
} adcClkDiv_t;
|
||||||
|
|
||||||
|
//Timer definitions
|
||||||
|
typedef enum {
|
||||||
|
TIM_DIV1 = 0x01,
|
||||||
|
TIM_DIV8 = 0x02,
|
||||||
|
TIM_DIV64 = 0x03,
|
||||||
|
TIM_DIV256 = 0x04,
|
||||||
|
TIM_DIV1024 = 0x05
|
||||||
|
} timerClkDiv_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
adcClkDiv_t adc_clk;
|
||||||
|
timerClkDiv_t pwm_clk;
|
||||||
|
uint16_t pwm_top;
|
||||||
|
uint8_t pwm_ch1_en;
|
||||||
|
} startupCfg_t;
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
void startup(startupCfg_t* hwCfg);
|
||||||
|
|
||||||
|
uint8_t gpio_read(uint8_t ch);
|
||||||
|
void gpio_write(uint8_t ch, int8_t lvl);
|
||||||
|
void gpio_write_pull(uint8_t ch, int8_t lvl);
|
||||||
|
|
||||||
|
uint16_t adc_read(uint8_t ch);
|
||||||
|
|
||||||
|
void pwm_write(uint8_t ch, uint16_t dc);
|
||||||
|
uint16_t pwm_read(uint8_t ch);
|
||||||
|
|
||||||
|
uint8_t eeprom_read8b(uint16_t address);
|
||||||
|
uint16_t eeprom_read16b(uint16_t address);
|
||||||
|
uint32_t eeprom_read32b(uint16_t address);
|
||||||
|
|
||||||
|
void eeprom_write8b(uint16_t address, uint8_t value);
|
||||||
|
void eeprom_write16b(uint16_t address, uint16_t value);
|
||||||
|
void eeprom_write32b(uint16_t address, uint32_t value);
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* MCU_HAL_H_ */
|
||||||
@@ -3,16 +3,18 @@
|
|||||||
#include <avr/eeprom.h>
|
#include <avr/eeprom.h>
|
||||||
#include "mcu_hal.h"
|
#include "mcu_hal.h"
|
||||||
|
|
||||||
|
using namespace mcu;
|
||||||
|
|
||||||
/**** Private definitions ****/
|
/**** Private definitions ****/
|
||||||
/**** Private constants ****/
|
/**** Private constants ****/
|
||||||
/**** Private variables ****/
|
/**** Private variables ****/
|
||||||
/**** Private function declarations ****/
|
/**** Private function declarations ****/
|
||||||
static uint8_t gpio_read(uint8_t pin_reg, uint8_t mask);
|
static uint8_t gpio_read_level(uint8_t pin_reg, uint8_t mask);
|
||||||
static void pwm_write_ocx(uint8_t ch, uint16_t value);
|
static void pwm_write_ocx(uint8_t ch, uint16_t value);
|
||||||
static uint16_t pwm_read_ocx(uint8_t ch);
|
static uint16_t pwm_read_ocx(uint8_t ch);
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
/**** Public function definitions ****/
|
||||||
void mcu_startup(startupCfg_t* hwCfg)
|
void mcu::startup(startupCfg_t* hwCfg)
|
||||||
{
|
{
|
||||||
// Fail-safe GPIO init
|
// Fail-safe GPIO init
|
||||||
PORTB = 0xF8; // Set PORTB pull-ups
|
PORTB = 0xF8; // Set PORTB pull-ups
|
||||||
@@ -32,7 +34,7 @@ void mcu_startup(startupCfg_t* hwCfg)
|
|||||||
DDRB |= 0x03; //Set as output
|
DDRB |= 0x03; //Set as output
|
||||||
|
|
||||||
// Common OD PWM pin
|
// Common OD PWM pin
|
||||||
if(hwCfg->pwm_chb_en) PORTB &= ~0x04; //Set low
|
if(hwCfg->pwm_ch1_en) PORTB &= ~0x04; //Set low
|
||||||
else PORTB |= 0x04; //Set high
|
else PORTB |= 0x04; //Set high
|
||||||
DDRB |= 0x04; //Set as output
|
DDRB |= 0x04; //Set as output
|
||||||
|
|
||||||
@@ -78,7 +80,7 @@ void mcu_startup(startupCfg_t* hwCfg)
|
|||||||
//DCCD and LED PWM configuration
|
//DCCD and LED PWM configuration
|
||||||
PRR0 &= ~0x80; //Enable Timer1 power
|
PRR0 &= ~0x80; //Enable Timer1 power
|
||||||
TCCR1A = 0xC2; //Connect OC1A, inverted mode
|
TCCR1A = 0xC2; //Connect OC1A, inverted mode
|
||||||
if(hwCfg->pwm_chb_en) TCCR1A |= 0x30; //Connect OC1B, inverted mode
|
if(hwCfg->pwm_ch1_en) TCCR1A |= 0x30; //Connect OC1B, inverted mode
|
||||||
TCCR1B = 0x18; //PWM, Phase & Frequency Correct ICR1 top, no clock, WGM:0xE
|
TCCR1B = 0x18; //PWM, Phase & Frequency Correct ICR1 top, no clock, WGM:0xE
|
||||||
TCCR1C = 0x00;
|
TCCR1C = 0x00;
|
||||||
TCNT1 = 0x0000;
|
TCNT1 = 0x0000;
|
||||||
@@ -93,7 +95,7 @@ void mcu_startup(startupCfg_t* hwCfg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ADC Interface functions
|
// ADC Interface functions
|
||||||
uint16_t mcu_adc_read(uint8_t ch)
|
uint16_t mcu::adc_read(uint8_t ch)
|
||||||
{
|
{
|
||||||
//check if ADC is enabled
|
//check if ADC is enabled
|
||||||
if(!(ADCSRA&0x80)) return 0xFFFF;
|
if(!(ADCSRA&0x80)) return 0xFFFF;
|
||||||
@@ -111,7 +113,7 @@ uint16_t mcu_adc_read(uint8_t ch)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PWM Timer Interface functions
|
// PWM Timer Interface functions
|
||||||
void mcu_pwm_write(uint8_t ch, uint16_t dc)
|
void mcu::pwm_write(uint8_t ch, uint16_t dc)
|
||||||
{
|
{
|
||||||
dc = 0xFFFF - dc;
|
dc = 0xFFFF - dc;
|
||||||
|
|
||||||
@@ -128,7 +130,7 @@ void mcu_pwm_write(uint8_t ch, uint16_t dc)
|
|||||||
pwm_write_ocx(ch, ocrx);
|
pwm_write_ocx(ch, ocrx);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t mcu_pwm_read(uint8_t ch)
|
uint16_t mcu::pwm_read(uint8_t ch)
|
||||||
{
|
{
|
||||||
uint16_t ocrx = pwm_read_ocx(ch);
|
uint16_t ocrx = pwm_read_ocx(ch);
|
||||||
|
|
||||||
@@ -146,74 +148,74 @@ uint16_t mcu_pwm_read(uint8_t ch)
|
|||||||
return (uint16_t)temp;
|
return (uint16_t)temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t mcu_gpio_read(uint8_t ch)
|
uint8_t mcu::gpio_read(uint8_t ch)
|
||||||
{
|
{
|
||||||
switch(ch)
|
switch(ch)
|
||||||
{
|
{
|
||||||
case MCU_GPIO0: // Mode DIN1
|
case GPIO0: // Mode DIN1
|
||||||
return gpio_read(PINC,0x20);
|
return gpio_read_level(PINC,0x20);
|
||||||
|
|
||||||
case MCU_GPIO1: // Pot DIN2
|
case GPIO1: // Pot DIN2
|
||||||
return gpio_read(PINC,0x10);
|
return gpio_read_level(PINC,0x10);
|
||||||
|
|
||||||
case MCU_GPIO2: // Down DIN3
|
case GPIO2: // Down DIN3
|
||||||
return gpio_read(PINE,0x02);
|
return gpio_read_level(PINE,0x02);
|
||||||
|
|
||||||
case MCU_GPIO3: // Up DIN4
|
case GPIO3: // Up DIN4
|
||||||
return gpio_read(PINE,0x08);
|
return gpio_read_level(PINE,0x08);
|
||||||
|
|
||||||
case MCU_GPIO4: // Dimm DIN5
|
case GPIO4: // Dimm DIN5
|
||||||
return gpio_read(PIND,0x80);
|
return gpio_read_level(PIND,0x80);
|
||||||
|
|
||||||
case MCU_GPIO5: // Brakes DIN6
|
case GPIO5: // Brakes DIN6
|
||||||
return gpio_read(PINB,0x80);
|
return gpio_read_level(PINB,0x80);
|
||||||
|
|
||||||
case MCU_GPIO6: // Handbrake DIN7
|
case GPIO6: // Handbrake DIN7
|
||||||
return gpio_read(PINB,0x40);
|
return gpio_read_level(PINB,0x40);
|
||||||
|
|
||||||
case MCU_GPIO7: // Handbrake pull DIN8
|
case GPIO7: // Handbrake pull DIN8
|
||||||
return gpio_read(PINB,0x20);
|
return gpio_read_level(PINB,0x20);
|
||||||
|
|
||||||
case MCU_GPIO8: // Speed-pull
|
case GPIO8: // Speed-pull
|
||||||
return gpio_read(PIND,0x40);
|
return gpio_read_level(PIND,0x40);
|
||||||
|
|
||||||
case MCU_GPIO9: // LED 0
|
case GPIO9: // LED 0
|
||||||
return gpio_read(PIND,0x01);
|
return gpio_read_level(PIND,0x01);
|
||||||
|
|
||||||
case MCU_GPIO10: // LED 1
|
case GPIO10: // LED 1
|
||||||
return gpio_read(PIND,0x02);
|
return gpio_read_level(PIND,0x02);
|
||||||
|
|
||||||
case MCU_GPIO11: // LED 2
|
case GPIO11: // LED 2
|
||||||
return gpio_read(PIND,0x04);
|
return gpio_read_level(PIND,0x04);
|
||||||
|
|
||||||
case MCU_GPIO12: // LED 3
|
case GPIO12: // LED 3
|
||||||
return gpio_read(PIND,0x08);
|
return gpio_read_level(PIND,0x08);
|
||||||
|
|
||||||
case MCU_GPIO13: // LED 4
|
case GPIO13: // LED 4
|
||||||
return gpio_read(PIND,0x10);
|
return gpio_read_level(PIND,0x10);
|
||||||
|
|
||||||
case MCU_GPIO14: // LED 5
|
case GPIO14: // LED 5
|
||||||
return gpio_read(PIND,0x20);
|
return gpio_read_level(PIND,0x20);
|
||||||
|
|
||||||
case MCU_GPIO15: // DCCD Enable
|
case GPIO15: // DCCD Enable
|
||||||
return gpio_read(PINB,0x01);
|
return gpio_read_level(PINB,0x01);
|
||||||
|
|
||||||
case MCU_GPIO16: // DCCD PWM
|
case GPIO16: // DCCD PWM
|
||||||
return gpio_read(PINB,0x02);
|
return gpio_read_level(PINB,0x02);
|
||||||
|
|
||||||
case MCU_GPIO17: // LED PWM
|
case GPIO17: // LED PWM
|
||||||
return gpio_read(PINB,0x04);
|
return gpio_read_level(PINB,0x04);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mcu_gpio_write(uint8_t ch, int8_t lvl)
|
void mcu::gpio_write(uint8_t ch, int8_t lvl)
|
||||||
{
|
{
|
||||||
switch(ch)
|
switch(ch)
|
||||||
{
|
{
|
||||||
case MCU_GPIO0: // Mode DIN1
|
case GPIO0: // Mode DIN1
|
||||||
if(lvl>0)
|
if(lvl>0)
|
||||||
{
|
{
|
||||||
PORTC |= 0x20;
|
PORTC |= 0x20;
|
||||||
@@ -231,7 +233,7 @@ void mcu_gpio_write(uint8_t ch, int8_t lvl)
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO1: // Pot DIN2
|
case GPIO1: // Pot DIN2
|
||||||
if(lvl>0)
|
if(lvl>0)
|
||||||
{
|
{
|
||||||
PORTC |= 0x10;
|
PORTC |= 0x10;
|
||||||
@@ -249,7 +251,7 @@ void mcu_gpio_write(uint8_t ch, int8_t lvl)
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO2: // Down DIN3
|
case GPIO2: // Down DIN3
|
||||||
if(lvl>0)
|
if(lvl>0)
|
||||||
{
|
{
|
||||||
PORTE |= 0x02;
|
PORTE |= 0x02;
|
||||||
@@ -267,7 +269,7 @@ void mcu_gpio_write(uint8_t ch, int8_t lvl)
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO3: // Up DIN4
|
case GPIO3: // Up DIN4
|
||||||
if(lvl>0)
|
if(lvl>0)
|
||||||
{
|
{
|
||||||
PORTE |= 0x08;
|
PORTE |= 0x08;
|
||||||
@@ -285,7 +287,7 @@ void mcu_gpio_write(uint8_t ch, int8_t lvl)
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO7: // Handbrake pull DIN
|
case GPIO7: // Handbrake pull DIN
|
||||||
if(lvl>0)
|
if(lvl>0)
|
||||||
{
|
{
|
||||||
PORTB |= 0x20;
|
PORTB |= 0x20;
|
||||||
@@ -303,42 +305,42 @@ void mcu_gpio_write(uint8_t ch, int8_t lvl)
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO8: // Speed-pull
|
case GPIO8: // Speed-pull
|
||||||
if(lvl>0) PORTD |= 0x40;
|
if(lvl>0) PORTD |= 0x40;
|
||||||
else PORTD &= ~0x40;
|
else PORTD &= ~0x40;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO9: // LED 0
|
case GPIO9: // LED 0
|
||||||
if(lvl>0) PORTD |= 0x01;
|
if(lvl>0) PORTD |= 0x01;
|
||||||
else PORTD &= ~0x01;
|
else PORTD &= ~0x01;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO10: // LED 1
|
case GPIO10: // LED 1
|
||||||
if(lvl>0) PORTD |= 0x02;
|
if(lvl>0) PORTD |= 0x02;
|
||||||
else PORTD &= ~0x02;
|
else PORTD &= ~0x02;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO11: // LED 2
|
case GPIO11: // LED 2
|
||||||
if(lvl>0) PORTD |= 0x04;
|
if(lvl>0) PORTD |= 0x04;
|
||||||
else PORTD &= ~0x04;
|
else PORTD &= ~0x04;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO12: // LED 3
|
case GPIO12: // LED 3
|
||||||
if(lvl>0) PORTD |= 0x08;
|
if(lvl>0) PORTD |= 0x08;
|
||||||
else PORTD &= ~0x08;
|
else PORTD &= ~0x08;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO13: // LED 4
|
case GPIO13: // LED 4
|
||||||
if(lvl>0) PORTD |= 0x10;
|
if(lvl>0) PORTD |= 0x10;
|
||||||
else PORTD &= ~0x10;
|
else PORTD &= ~0x10;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO14: // LED 5
|
case GPIO14: // LED 5
|
||||||
if(lvl>0) PORTD |= 0x20;
|
if(lvl>0) PORTD |= 0x20;
|
||||||
else PORTD &= ~0x20;
|
else PORTD &= ~0x20;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_GPIO15: // DCCD Enable
|
case GPIO15: // DCCD Enable
|
||||||
if(lvl>0) PORTB |= 0x01;
|
if(lvl>0) PORTB |= 0x01;
|
||||||
else PORTB &= ~0x01;
|
else PORTB &= ~0x01;
|
||||||
return;
|
return;
|
||||||
@@ -348,52 +350,96 @@ void mcu_gpio_write(uint8_t ch, int8_t lvl)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t mcu_ee_read8b(uint16_t address)
|
void mcu::gpio_write_pull(uint8_t ch, int8_t lvl)
|
||||||
|
{
|
||||||
|
switch(ch)
|
||||||
|
{
|
||||||
|
case GPIO0: // Mode DIN1
|
||||||
|
if(lvl>0) PORTC |= 0x20;
|
||||||
|
else PORTC &= ~0x20;
|
||||||
|
return;
|
||||||
|
|
||||||
|
case GPIO1: // Pot DIN2
|
||||||
|
if(lvl>0) PORTC |= 0x10;
|
||||||
|
else PORTC &= ~0x10;
|
||||||
|
return;
|
||||||
|
|
||||||
|
case GPIO2: // Down DIN3
|
||||||
|
if(lvl>0) PORTE |= 0x02;
|
||||||
|
else PORTE &= ~0x02;
|
||||||
|
return;
|
||||||
|
|
||||||
|
case GPIO3: // Up DIN4
|
||||||
|
if(lvl>0) PORTE |= 0x08;
|
||||||
|
else PORTE &= ~0x08;
|
||||||
|
return;
|
||||||
|
|
||||||
|
case GPIO4: // Dimm
|
||||||
|
if(lvl>0) PORTD |= 0x80;
|
||||||
|
else PORTD &= ~0x80;
|
||||||
|
return;
|
||||||
|
|
||||||
|
case GPIO5: // Brakes
|
||||||
|
if(lvl>0) PORTB |= 0x80;
|
||||||
|
else PORTB &= ~0x80;
|
||||||
|
return;
|
||||||
|
|
||||||
|
case GPIO6: // Handbrake
|
||||||
|
if(lvl>0) PORTB |= 0x40;
|
||||||
|
else PORTB &= ~0x40;
|
||||||
|
return;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t mcu::eeprom_read8b(uint16_t address)
|
||||||
{
|
{
|
||||||
return eeprom_read_byte((uint8_t*)address);
|
return eeprom_read_byte((uint8_t*)address);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t mcu_ee_read16b(uint16_t address)
|
uint16_t mcu::eeprom_read16b(uint16_t address)
|
||||||
{
|
{
|
||||||
return eeprom_read_word((uint16_t*)address);
|
return eeprom_read_word((uint16_t*)address);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t mcu_ee_read32b(uint16_t address)
|
uint32_t mcu::eeprom_read32b(uint16_t address)
|
||||||
{
|
{
|
||||||
return eeprom_read_dword((uint32_t*)address);
|
return eeprom_read_dword((uint32_t*)address);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mcu_ee_write8b(uint16_t address, uint8_t value)
|
void mcu::eeprom_write8b(uint16_t address, uint8_t value)
|
||||||
{
|
{
|
||||||
eeprom_write_byte((uint8_t*)address, value);
|
eeprom_write_byte((uint8_t*)address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mcu_ee_write16b(uint16_t address, uint16_t value)
|
void mcu::eeprom_write16b(uint16_t address, uint16_t value)
|
||||||
{
|
{
|
||||||
eeprom_write_word((uint16_t*)address, value);
|
eeprom_write_word((uint16_t*)address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mcu_ee_write32b(uint16_t address, uint32_t value)
|
void mcu::eeprom_write32b(uint16_t address, uint32_t value)
|
||||||
{
|
{
|
||||||
eeprom_write_dword((uint32_t*)address, value);
|
eeprom_write_dword((uint32_t*)address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
/**** Private function definitions ****/
|
||||||
static uint8_t gpio_read(uint8_t pin_reg, uint8_t mask)
|
static uint8_t gpio_read_level(uint8_t pin_reg, uint8_t mask)
|
||||||
{
|
{
|
||||||
if(pin_reg&mask) return MCU_GPIO_HIGH;
|
if(pin_reg&mask) return LEVEL_HIGH;
|
||||||
else return MCU_GPIO_LOW;
|
else return LEVEL_LOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pwm_write_ocx(uint8_t ch, uint16_t value)
|
static void pwm_write_ocx(uint8_t ch, uint16_t value)
|
||||||
{
|
{
|
||||||
switch(ch)
|
switch(ch)
|
||||||
{
|
{
|
||||||
case MCU_PWM0:
|
case PWM0:
|
||||||
OCR1A = value;
|
OCR1A = value;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case MCU_PWM1:
|
case PWM1:
|
||||||
OCR1B = value;
|
OCR1B = value;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -406,13 +452,13 @@ static uint16_t pwm_read_ocx(uint8_t ch)
|
|||||||
{
|
{
|
||||||
switch(ch)
|
switch(ch)
|
||||||
{
|
{
|
||||||
case MCU_PWM0:
|
case PWM0:
|
||||||
return OCR1A;
|
return OCR1A;
|
||||||
|
|
||||||
case MCU_PWM1:
|
case PWM1:
|
||||||
return OCR1B ;
|
return OCR1B ;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return 0x0000;
|
return 0x0000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
40
firmware/src/bsp/pwm.cpp
Normal file
40
firmware/src/bsp/pwm.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "mcu/mcu_hal.h"
|
||||||
|
#include "pwm.h"
|
||||||
|
|
||||||
|
using namespace bsp;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
bsp::PWMout::PWMout(uint8_t pwm_ch)
|
||||||
|
{
|
||||||
|
this->pwm_ch = pwm_ch;
|
||||||
|
this->write(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bsp::PWMout::~PWMout(void)
|
||||||
|
{
|
||||||
|
this->write(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bsp::PWMout::write(uint8_t duty)
|
||||||
|
{
|
||||||
|
// Convert percent to 16b duty cycle
|
||||||
|
uint16_t dc = util::percent_to_16b(duty);
|
||||||
|
|
||||||
|
// Set PWM
|
||||||
|
mcu::pwm_write(this->pwm_ch, dc);
|
||||||
|
this->last_duty = duty;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t bsp::PWMout::get_set_duty(void)
|
||||||
|
{
|
||||||
|
return this->last_duty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
31
firmware/src/bsp/pwm.h
Normal file
31
firmware/src/bsp/pwm.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef PWM_H_
|
||||||
|
#define PWM_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace bsp {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
class PWMout
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
uint8_t pwm_ch;
|
||||||
|
uint8_t last_duty;
|
||||||
|
|
||||||
|
public:
|
||||||
|
PWMout(uint8_t pwm_ch);
|
||||||
|
~PWMout(void);
|
||||||
|
|
||||||
|
void write(uint8_t duty);
|
||||||
|
uint8_t get_set_duty(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* PWM_H_ */
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "board/utils/utils.h"
|
|
||||||
#include "board/ain.h"
|
|
||||||
#include "analog.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
static uint8_t ain_mapping(uint8_t analog_ch, uint8_t* ain_ch);
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
uint16_t analog_ch_get(uint8_t analog_ch)
|
|
||||||
{
|
|
||||||
uint8_t ain_ch;
|
|
||||||
// Get channel config
|
|
||||||
if(ain_mapping(analog_ch, &ain_ch)) return 0;
|
|
||||||
|
|
||||||
// Read input as mV
|
|
||||||
uint16_t ch_mv = bsp_ain_read(ain_ch);
|
|
||||||
|
|
||||||
// Return result
|
|
||||||
return ch_mv;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
static uint8_t ain_mapping(uint8_t analog_ch, uint8_t* ain_ch)
|
|
||||||
{
|
|
||||||
switch(analog_ch)
|
|
||||||
{
|
|
||||||
case ANALOG_1: // Pot
|
|
||||||
*ain_ch = BSP_AIN2;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case ANALOG_2: // Mode
|
|
||||||
*ain_ch = BSP_AIN1;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
default: //Invalid channel
|
|
||||||
*ain_ch = BSP_AIN5;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
#ifndef ANALOG_H_
|
|
||||||
#define ANALOG_H_
|
|
||||||
|
|
||||||
/*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define ANALOG_1 1 //Potentiometer
|
|
||||||
#define ANALOG_2 2 //Mode
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
uint16_t analog_ch_get(uint8_t analog_ch);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* ANALOG_H_ */
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "utils/utils.h"
|
|
||||||
#include "mcu/mcu_hal.h"
|
|
||||||
#include "ain.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
typedef struct {
|
|
||||||
uint8_t adc_ch;
|
|
||||||
uint8_t mul;
|
|
||||||
uint8_t div;
|
|
||||||
int16_t offset;
|
|
||||||
} ainchcfg_t;
|
|
||||||
|
|
||||||
/**** Private constants ****/
|
|
||||||
static const uint8_t MV_MUL = BSP_AIN_DEF_MV_MUL;
|
|
||||||
static const uint8_t MV_DIV = BSP_AIN_DEF_MV_DIV;
|
|
||||||
static const int16_t MV_OFFSET = BSP_AIN_DEF_MV_OFFSET;
|
|
||||||
|
|
||||||
/**** Private variables ****/
|
|
||||||
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
static uint8_t ain_mapping(uint8_t ain_ch, ainchcfg_t* ain_ch_cfg);
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
uint16_t bsp_ain_read(uint8_t ch)
|
|
||||||
{
|
|
||||||
ainchcfg_t cfg;
|
|
||||||
// Get analog input config, and check validity
|
|
||||||
if(ain_mapping(ch, &cfg)) return 0;
|
|
||||||
|
|
||||||
//Read ADC
|
|
||||||
uint16_t raw = mcu_adc_read(cfg.adc_ch);
|
|
||||||
|
|
||||||
//Convert to mV
|
|
||||||
raw = util_convert_muldivoff(raw, cfg.mul, cfg.div, cfg.offset);
|
|
||||||
|
|
||||||
// Return result
|
|
||||||
return raw;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
static uint8_t ain_mapping(uint8_t ain_ch, ainchcfg_t* ain_ch_cfg)
|
|
||||||
{
|
|
||||||
// Default 10bit ADC with 5V reference to mV conversion
|
|
||||||
ain_ch_cfg->mul = MV_MUL;
|
|
||||||
ain_ch_cfg->div = MV_DIV;
|
|
||||||
ain_ch_cfg->offset = MV_OFFSET;
|
|
||||||
|
|
||||||
switch(ain_ch)
|
|
||||||
{
|
|
||||||
case BSP_AIN1: // Mode
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC5;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_AIN2: // Pot
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC4;
|
|
||||||
return 0;;
|
|
||||||
|
|
||||||
case BSP_AIN3: // MCU Temp
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC8;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_AIN4: // MCU Internal reference
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC14;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_AIN5: // MCU Ground
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC15;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
default: // Invalid channel
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC15;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
#ifndef AIN_H_
|
|
||||||
#define AIN_H_
|
|
||||||
|
|
||||||
/*
|
|
||||||
AIN1 MODE
|
|
||||||
AIN2 POT
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
#include "config.h"
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define BSP_AIN1 1 // Mode
|
|
||||||
#define BSP_AIN2 2 // Pot
|
|
||||||
#define BSP_AIN3 3 // MCU Temp
|
|
||||||
#define BSP_AIN4 4 // MCU Internal reference
|
|
||||||
#define BSP_AIN5 5 // MCU Ground
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
uint16_t bsp_ain_read(uint8_t ch);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* AIN_H_ */
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#ifndef BSP_CONFIG_H_
|
|
||||||
#define BSP_CONFIG_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define BSP_AIN_DEF_MV_MUL 215
|
|
||||||
#define BSP_AIN_DEF_MV_DIV 44
|
|
||||||
#define BSP_AIN_DEF_MV_OFFSET 0
|
|
||||||
|
|
||||||
#define BSP_HB_MV_MUL BSP_AIN_DEF_MV_MUL
|
|
||||||
#define BSP_HB_MV_DIV BSP_AIN_DEF_MV_DIV
|
|
||||||
#define BSP_HB_MV_OFFSET BSP_AIN_DEF_MV_OFFSET
|
|
||||||
|
|
||||||
#define BSP_HB_UDIV_MUL 20
|
|
||||||
#define BSP_HB_UDIV_DIV 1
|
|
||||||
#define BSP_HB_UDIV_OFFSET 0
|
|
||||||
|
|
||||||
#define BSP_HB_ISUP_MUL 235
|
|
||||||
#define BSP_HB_ISUP_DIV 6
|
|
||||||
#define BSP_HB_ISUP_OFFSET 0
|
|
||||||
|
|
||||||
#define BSP_HB_IOUT_MUL 215
|
|
||||||
#define BSP_HB_IOUT_DIV 22
|
|
||||||
#define BSP_HB_IOUT_OFFSET 0
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* BSP_CONFIG_H_ */
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "utils/utils.h"
|
|
||||||
#include "mcu/mcu_hal.h"
|
|
||||||
#include "din.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
typedef struct {
|
|
||||||
uint8_t gpio_ch;
|
|
||||||
uint8_t invert;
|
|
||||||
} dinchcfg_t;
|
|
||||||
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
static uint8_t din_mapping(uint8_t din_ch, dinchcfg_t* din_ch_cfg);
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
uint8_t bsp_din_read(uint8_t ch)
|
|
||||||
{
|
|
||||||
dinchcfg_t cfg;
|
|
||||||
// Get digital input config, and check validity
|
|
||||||
if(din_mapping(ch, &cfg)) return BSP_DIN_LOW;
|
|
||||||
|
|
||||||
//Read GPIO
|
|
||||||
uint8_t raw = mcu_gpio_read(cfg.gpio_ch);
|
|
||||||
|
|
||||||
// Check config and invert
|
|
||||||
if(cfg.invert) raw = util_invert_8b(raw);
|
|
||||||
|
|
||||||
// Return result
|
|
||||||
return raw;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ***/
|
|
||||||
static uint8_t din_mapping(uint8_t din_ch, dinchcfg_t* din_ch_cfg)
|
|
||||||
{
|
|
||||||
// By default ch is not inverted
|
|
||||||
din_ch_cfg->invert = 0;
|
|
||||||
|
|
||||||
switch(din_ch)
|
|
||||||
{
|
|
||||||
case BSP_DIN1: //Mode
|
|
||||||
din_ch_cfg->gpio_ch = MCU_GPIO0;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DIN2: //Pot
|
|
||||||
din_ch_cfg->gpio_ch = MCU_GPIO1;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DIN3: //Down
|
|
||||||
din_ch_cfg->gpio_ch = MCU_GPIO2;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DIN4: //Up
|
|
||||||
din_ch_cfg->gpio_ch = MCU_GPIO3;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DIN5: //Dimm
|
|
||||||
din_ch_cfg->gpio_ch = MCU_GPIO4;
|
|
||||||
din_ch_cfg->invert = 1;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DIN6: //Brakes
|
|
||||||
din_ch_cfg->gpio_ch = MCU_GPIO5;
|
|
||||||
din_ch_cfg->invert = 1;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DIN7: //Handbrake
|
|
||||||
din_ch_cfg->gpio_ch = MCU_GPIO6;
|
|
||||||
din_ch_cfg->invert = 1;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DIN7N: //Handbrake pull
|
|
||||||
din_ch_cfg->gpio_ch = MCU_GPIO7;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
default: //Invalid channel
|
|
||||||
din_ch_cfg->gpio_ch = 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#ifndef DIN_H_
|
|
||||||
#define DIN_H_
|
|
||||||
|
|
||||||
/*
|
|
||||||
DIN1 MODE
|
|
||||||
DIN2 POT
|
|
||||||
DIN3 DOWN
|
|
||||||
DIN4 UP
|
|
||||||
DIN5 HV DIM
|
|
||||||
DIN6 HV BRAKES
|
|
||||||
DIN7 HV HANDBRAKE
|
|
||||||
DIN8 HBRAKE PULL
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define BSP_DIN1 1
|
|
||||||
#define BSP_DIN2 2
|
|
||||||
#define BSP_DIN3 3
|
|
||||||
#define BSP_DIN4 4
|
|
||||||
#define BSP_DIN5 5
|
|
||||||
#define BSP_DIN6 6
|
|
||||||
#define BSP_DIN7 7
|
|
||||||
#define BSP_DIN7N 8
|
|
||||||
|
|
||||||
#define BSP_DIN_LOW 0
|
|
||||||
#define BSP_DIN_HIGH 1
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
uint8_t bsp_din_read(uint8_t ch);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* DIN_H_ */
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "utils/utils.h"
|
|
||||||
#include "mcu/mcu_hal.h"
|
|
||||||
#include "dout.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
static uint8_t dout_mapping(uint8_t dout_ch, uint8_t* gpio_ch);
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
void bsp_dout_write(uint8_t ch, int8_t lvl)
|
|
||||||
{
|
|
||||||
uint8_t gpio_ch;
|
|
||||||
// Get digital input config, and check validity
|
|
||||||
if(dout_mapping(ch, &gpio_ch)) return;
|
|
||||||
|
|
||||||
// Write GPIO
|
|
||||||
mcu_gpio_write(gpio_ch, lvl);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ***/
|
|
||||||
static uint8_t dout_mapping(uint8_t dout_ch, uint8_t* gpio_ch)
|
|
||||||
{
|
|
||||||
switch(dout_ch)
|
|
||||||
{
|
|
||||||
case BSP_DOUT1: //Mode
|
|
||||||
*gpio_ch = MCU_GPIO0;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DOUT2: //Pot
|
|
||||||
*gpio_ch = MCU_GPIO1;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DOUT3: //Down
|
|
||||||
*gpio_ch = MCU_GPIO2;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DOUT4: //Up
|
|
||||||
*gpio_ch = MCU_GPIO3;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DOUT5: //Handbrake pull
|
|
||||||
*gpio_ch = MCU_GPIO7;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_DOUT6: //Speed pull
|
|
||||||
*gpio_ch = MCU_GPIO8;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
default: //Invalid channel
|
|
||||||
*gpio_ch = 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
#ifndef DOUT_H_
|
|
||||||
#define DOUT_H_
|
|
||||||
|
|
||||||
/*
|
|
||||||
DOUT1 MODE
|
|
||||||
DOUT2 POT
|
|
||||||
DOUT3 DOWN
|
|
||||||
DOUT4 UP
|
|
||||||
DOUT5 HBRAKE PULL
|
|
||||||
DOUT6 SPEED PULL
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define BSP_DOUT1 1
|
|
||||||
#define BSP_DOUT2 2
|
|
||||||
#define BSP_DOUT3 3
|
|
||||||
#define BSP_DOUT4 4
|
|
||||||
#define BSP_DOUT5 5
|
|
||||||
#define BSP_DOUT6 6
|
|
||||||
|
|
||||||
#define BSP_DOUT_LOW 0
|
|
||||||
#define BSP_DOUT_HIGH 1
|
|
||||||
#define BSP_DOUT_HIZ -1
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void bsp_dout_write(uint8_t ch, int8_t lvl);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* DOUT_H_ */
|
|
||||||
@@ -1,179 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "utils/utils.h"
|
|
||||||
#include "mcu/mcu_hal.h"
|
|
||||||
#include "halfbridge.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
typedef struct {
|
|
||||||
uint8_t adc_ch;
|
|
||||||
uint8_t mul;
|
|
||||||
uint8_t div;
|
|
||||||
int16_t offset;
|
|
||||||
} ainchcfg_t;
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
AIN_OUT_VOLTAGE,
|
|
||||||
AIN_OUT_CURRENT,
|
|
||||||
AIN_SUP_VOLTAGE,
|
|
||||||
AIN_SUP_CURRENT
|
|
||||||
} hb_ainch_t;
|
|
||||||
|
|
||||||
/**** Private constants ****/
|
|
||||||
// Analog channels conversion coefficients
|
|
||||||
static const uint8_t MV_MUL = BSP_HB_MV_MUL;
|
|
||||||
static const uint8_t MV_DIV = BSP_HB_MV_DIV;
|
|
||||||
static const int16_t MV_OFFSET = BSP_HB_MV_OFFSET;
|
|
||||||
|
|
||||||
static const uint8_t UDIV_MUL = BSP_HB_UDIV_MUL;
|
|
||||||
static const uint8_t UDIV_DIV = BSP_HB_UDIV_DIV;
|
|
||||||
static const int16_t UDIV_OFFSET = BSP_HB_UDIV_OFFSET;
|
|
||||||
|
|
||||||
static const uint8_t ISUP_MUL = BSP_HB_ISUP_MUL;
|
|
||||||
static const uint8_t ISUP_DIV = BSP_HB_ISUP_DIV;
|
|
||||||
static const int16_t ISUP_OFFSET = BSP_HB_ISUP_OFFSET;
|
|
||||||
|
|
||||||
static const uint8_t IOUT_MUL = BSP_HB_IOUT_MUL;
|
|
||||||
static const uint8_t IOUT_DIV = BSP_HB_IOUT_DIV;
|
|
||||||
static const int16_t IOUT_OFFSET = BSP_HB_IOUT_OFFSET;
|
|
||||||
|
|
||||||
/**** Private variables ****/
|
|
||||||
|
|
||||||
/**** Mapping function declarations ****/
|
|
||||||
static uint8_t hb_pwm_mapping(uint8_t* pwm_ch);
|
|
||||||
static uint8_t hb_low_side_mapping(uint8_t* gpio_ch);
|
|
||||||
static uint8_t hb_ain_mapping(hb_ainch_t ain_ch, ainchcfg_t* ain_ch_cfg);
|
|
||||||
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
static uint16_t limit_pwm(uint16_t pwm_in);
|
|
||||||
static uint16_t read_ain(hb_ainch_t ch);
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
void bsp_hb_write_low(uint8_t state)
|
|
||||||
{
|
|
||||||
uint8_t gpio_ch;
|
|
||||||
// Get GPIO channel, and check validity
|
|
||||||
if(hb_low_side_mapping(&gpio_ch)) return;
|
|
||||||
|
|
||||||
// Set low side on or off
|
|
||||||
if(state) mcu_gpio_write(gpio_ch, MCU_GPIO_HIGH);
|
|
||||||
else mcu_gpio_write(gpio_ch, MCU_GPIO_LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
void bsp_hb_write_pwm(uint16_t pwm)
|
|
||||||
{
|
|
||||||
uint8_t pwm_ch;
|
|
||||||
// Get PWM channel, and check validity
|
|
||||||
if(hb_pwm_mapping(&pwm_ch)) return;
|
|
||||||
|
|
||||||
// Limit PWM, because of charge pump
|
|
||||||
pwm = limit_pwm(pwm);
|
|
||||||
|
|
||||||
// Set pwm
|
|
||||||
mcu_pwm_write(pwm_ch, pwm);
|
|
||||||
}
|
|
||||||
|
|
||||||
void bsp_hb_read_meas(hb_meas_t* measurements)
|
|
||||||
{
|
|
||||||
// Read analog inputs
|
|
||||||
measurements->out_voltage = read_ain(AIN_OUT_VOLTAGE);
|
|
||||||
measurements->out_current = read_ain(AIN_OUT_CURRENT);
|
|
||||||
measurements->sup_voltage = read_ain(AIN_SUP_VOLTAGE);
|
|
||||||
measurements->sup_current = read_ain(AIN_SUP_CURRENT);
|
|
||||||
|
|
||||||
// Calculate secondary measurements
|
|
||||||
measurements->out_power = util_sat_mul_kilo(measurements->out_voltage, measurements->out_current);
|
|
||||||
measurements->sup_power = util_sat_mul_kilo(measurements->sup_voltage, measurements->sup_current);
|
|
||||||
|
|
||||||
measurements->out_impedance = util_sat_div_kilo(measurements->out_voltage, measurements->out_current);
|
|
||||||
|
|
||||||
uint8_t ch;
|
|
||||||
//Read low side control GPIO level
|
|
||||||
if(hb_low_side_mapping(&ch)) measurements->low_side_ctrl = 0;
|
|
||||||
else measurements->low_side_ctrl = mcu_gpio_read(ch);
|
|
||||||
|
|
||||||
//Read PWM duty cycle in 16b format
|
|
||||||
if(hb_pwm_mapping(&ch)) measurements->pwm = 0;
|
|
||||||
else measurements->pwm = mcu_pwm_read(ch);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
static uint16_t read_ain(hb_ainch_t ch)
|
|
||||||
{
|
|
||||||
ainchcfg_t cfg;
|
|
||||||
// Get analog input config, and check validity
|
|
||||||
if(hb_ain_mapping(ch, &cfg)) return 0;
|
|
||||||
|
|
||||||
//Read ADC
|
|
||||||
uint16_t raw = mcu_adc_read(cfg.adc_ch);
|
|
||||||
|
|
||||||
//Convert to target units
|
|
||||||
raw = util_convert_muldivoff(raw, cfg.mul, cfg.div, cfg.offset);
|
|
||||||
|
|
||||||
// Return result
|
|
||||||
return raw;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint16_t limit_pwm(uint16_t pwm_in)
|
|
||||||
{
|
|
||||||
// Limit to ~95%
|
|
||||||
if (pwm_in > 0xFC00) return 0xFC00;
|
|
||||||
else return pwm_in;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Mapping function definitions ****/
|
|
||||||
static uint8_t hb_pwm_mapping(uint8_t* pwm_ch)
|
|
||||||
{
|
|
||||||
*pwm_ch = MCU_PWM0;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t hb_low_side_mapping(uint8_t* gpio_ch)
|
|
||||||
{
|
|
||||||
*gpio_ch = MCU_GPIO15;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t hb_ain_mapping(hb_ainch_t ain_ch, ainchcfg_t* ain_ch_cfg)
|
|
||||||
{
|
|
||||||
switch(ain_ch)
|
|
||||||
{
|
|
||||||
case AIN_OUT_VOLTAGE:
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC1;
|
|
||||||
ain_ch_cfg->mul = UDIV_MUL;
|
|
||||||
ain_ch_cfg->div = UDIV_DIV;
|
|
||||||
ain_ch_cfg->offset = UDIV_OFFSET;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case AIN_OUT_CURRENT:
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC0;
|
|
||||||
ain_ch_cfg->mul = IOUT_MUL;
|
|
||||||
ain_ch_cfg->div = IOUT_DIV;
|
|
||||||
ain_ch_cfg->offset = IOUT_OFFSET;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case AIN_SUP_VOLTAGE:
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC2;
|
|
||||||
ain_ch_cfg->mul = UDIV_MUL;
|
|
||||||
ain_ch_cfg->div = UDIV_DIV;
|
|
||||||
ain_ch_cfg->offset = UDIV_OFFSET;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case AIN_SUP_CURRENT:
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC3;
|
|
||||||
ain_ch_cfg->mul = ISUP_MUL;
|
|
||||||
ain_ch_cfg->div = ISUP_DIV;
|
|
||||||
ain_ch_cfg->offset = ISUP_OFFSET;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
default: //Invalid channel
|
|
||||||
ain_ch_cfg->adc_ch = MCU_ADC15;
|
|
||||||
// Default 10bit ADC with 5V reference to mV conversion
|
|
||||||
ain_ch_cfg->mul = MV_MUL;
|
|
||||||
ain_ch_cfg->div = MV_DIV;
|
|
||||||
ain_ch_cfg->offset = MV_OFFSET;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
#ifndef HALFBRIDGE_H_
|
|
||||||
#define HALFBRIDGE_H_
|
|
||||||
|
|
||||||
/*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
#include "config.h"
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
typedef struct {
|
|
||||||
uint16_t out_voltage;
|
|
||||||
uint16_t out_current;
|
|
||||||
uint16_t sup_voltage;
|
|
||||||
uint16_t sup_current;
|
|
||||||
uint16_t out_power;
|
|
||||||
uint16_t sup_power;
|
|
||||||
uint16_t out_impedance;
|
|
||||||
uint8_t low_side_ctrl;
|
|
||||||
uint16_t pwm;
|
|
||||||
} hb_meas_t;
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void bsp_hb_write_low(uint8_t state);
|
|
||||||
void bsp_hb_write_pwm(uint16_t pwm);
|
|
||||||
|
|
||||||
// Feedback functions
|
|
||||||
void bsp_hb_read_meas(hb_meas_t* measurements);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* HALFBRIDGE_H_ */
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
#ifndef MCU_HAL_H_
|
|
||||||
#define MCU_HAL_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
/*
|
|
||||||
GPIO0 Down
|
|
||||||
GPIO1 Up
|
|
||||||
GPIO2 Mode
|
|
||||||
GPIO3 Handbrake
|
|
||||||
GPIO4 Brakes
|
|
||||||
GPIO5 Dimm
|
|
||||||
GPIO6 LED0
|
|
||||||
GPIO7 LED1
|
|
||||||
GPIO8 LED2
|
|
||||||
GPIO9 LED3
|
|
||||||
GPIO10 LED4
|
|
||||||
GPIO11 LED5
|
|
||||||
GPIO12 DCCD Enable
|
|
||||||
GPIO13 Handbrake pull
|
|
||||||
GPIO14 Speed pull
|
|
||||||
GPIO15 DCCD PWM
|
|
||||||
GPIO16 LED PWM
|
|
||||||
|
|
||||||
ADC0 Output current
|
|
||||||
ADC1 Output voltage
|
|
||||||
ADC2 Battery current
|
|
||||||
ADC3 Battery voltage
|
|
||||||
ADC4 Potentiometer
|
|
||||||
ADC5 Mode
|
|
||||||
ADC8 MCU temperature
|
|
||||||
ADC14 MCU internal reference
|
|
||||||
ADC15 MCU ground
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define MCU_GPIO0 0 //Mode
|
|
||||||
#define MCU_GPIO1 1 //Pot
|
|
||||||
#define MCU_GPIO2 2 //Down
|
|
||||||
#define MCU_GPIO3 3 //Up
|
|
||||||
#define MCU_GPIO4 4 //Dimm
|
|
||||||
#define MCU_GPIO5 5 //Brakes
|
|
||||||
#define MCU_GPIO6 6 //Handbrake
|
|
||||||
#define MCU_GPIO7 7 //Handbrake pull
|
|
||||||
#define MCU_GPIO8 8 //Speed pull
|
|
||||||
#define MCU_GPIO9 9 //LED0
|
|
||||||
#define MCU_GPIO10 10 //LED1
|
|
||||||
#define MCU_GPIO11 11 //LED2
|
|
||||||
#define MCU_GPIO12 12 //LED3
|
|
||||||
#define MCU_GPIO13 13 //LED4
|
|
||||||
#define MCU_GPIO14 14 //LED5
|
|
||||||
#define MCU_GPIO15 15 //DCCD Enable
|
|
||||||
#define MCU_GPIO16 16 //DCCD PWM
|
|
||||||
#define MCU_GPIO17 17 //LED PWM
|
|
||||||
|
|
||||||
#define MCU_GPIO_LOW 0
|
|
||||||
#define MCU_GPIO_HIGH 1
|
|
||||||
#define MCU_GPIO_HIZ -1
|
|
||||||
|
|
||||||
#define MCU_ADC0 0 //Output current
|
|
||||||
#define MCU_ADC1 1 //Output voltage
|
|
||||||
#define MCU_ADC2 2 //Battery voltage
|
|
||||||
#define MCU_ADC3 3 //Battery current
|
|
||||||
#define MCU_ADC4 4 //Potentiometer
|
|
||||||
#define MCU_ADC5 5 //Mode
|
|
||||||
#define MCU_ADC8 8 //MCU temperature
|
|
||||||
#define MCU_ADC14 14 //MCU internal reference
|
|
||||||
#define MCU_ADC15 15 //MCU ground
|
|
||||||
|
|
||||||
#define MCU_PWM0 0 //DCCD
|
|
||||||
#define MCU_PWM1 1 //LED
|
|
||||||
|
|
||||||
//ADC definitions
|
|
||||||
typedef enum {
|
|
||||||
MCU_ADC_DIV2 = 0x01,
|
|
||||||
MCU_ADC_DIV4 = 0x02,
|
|
||||||
MCU_ADC_DIV8 = 0x03,
|
|
||||||
MCU_ADC_DIV16 = 0x04,
|
|
||||||
MCU_ADC_DIV32 = 0x05,
|
|
||||||
MCU_ADC_DIV64 = 0x06,
|
|
||||||
MCU_ADC_DIV128 = 0x07
|
|
||||||
} adcClkDiv_t;
|
|
||||||
|
|
||||||
//Timer definitions
|
|
||||||
typedef enum {
|
|
||||||
MCU_TIM_DIV1 = 0x01,
|
|
||||||
MCU_TIM_DIV8 = 0x02,
|
|
||||||
MCU_TIM_DIV64 = 0x03,
|
|
||||||
MCU_TIM_DIV256 = 0x04,
|
|
||||||
MCU_TIM_DIV1024 = 0x05
|
|
||||||
} timerClkDiv_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
adcClkDiv_t adc_clk;
|
|
||||||
timerClkDiv_t pwm_clk;
|
|
||||||
uint16_t pwm_top;
|
|
||||||
uint8_t pwm_chb_en;
|
|
||||||
} startupCfg_t;
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void mcu_startup(startupCfg_t* hwCfg);
|
|
||||||
|
|
||||||
uint8_t mcu_gpio_read(uint8_t ch);
|
|
||||||
void mcu_gpio_write(uint8_t ch, int8_t lvl);
|
|
||||||
|
|
||||||
uint16_t mcu_adc_read(uint8_t ch);
|
|
||||||
|
|
||||||
void mcu_pwm_write(uint8_t ch, uint16_t dc);
|
|
||||||
uint16_t mcu_pwm_read(uint8_t ch);
|
|
||||||
|
|
||||||
uint8_t mcu_ee_read8b(uint16_t address);
|
|
||||||
uint16_t mcu_ee_read16b(uint16_t address);
|
|
||||||
uint32_t mcu_ee_read32b(uint16_t address);
|
|
||||||
|
|
||||||
void mcu_ee_write8b(uint16_t address, uint8_t value);
|
|
||||||
void mcu_ee_write16b(uint16_t address, uint16_t value);
|
|
||||||
void mcu_ee_write32b(uint16_t address, uint32_t value);
|
|
||||||
|
|
||||||
#endif /* MCU_HAL_H_ */
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "utils/utils.h"
|
|
||||||
#include "mcu/mcu_hal.h"
|
|
||||||
#include "odout.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
static uint8_t odout_pwm_mapping(uint8_t* pwm_ch);
|
|
||||||
static uint8_t odout_mapping(uint8_t od_ch, uint8_t* gpio_ch);
|
|
||||||
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
void bsp_odout_write(uint8_t ch, int8_t lvl)
|
|
||||||
{
|
|
||||||
uint8_t gpio_ch;
|
|
||||||
// Get GPIO channel config, and check validity
|
|
||||||
if(odout_mapping(ch, &gpio_ch)) return;
|
|
||||||
|
|
||||||
// Set output level
|
|
||||||
if(lvl==0) mcu_gpio_write(gpio_ch, 1); // Output active
|
|
||||||
else mcu_gpio_write(gpio_ch, 0); // Output off
|
|
||||||
}
|
|
||||||
|
|
||||||
void bsp_odout_write_common(uint8_t percent)
|
|
||||||
{
|
|
||||||
uint8_t pwm_ch;
|
|
||||||
// Get PWM channel config, and check validity
|
|
||||||
if(odout_pwm_mapping(&pwm_ch)) return;
|
|
||||||
|
|
||||||
// Convert percent to 16b duty cycle
|
|
||||||
uint16_t dc = util_percent_to_16b(percent);
|
|
||||||
|
|
||||||
// Set PWM
|
|
||||||
mcu_pwm_write(pwm_ch, dc);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
static uint8_t odout_pwm_mapping(uint8_t* pwm_ch)
|
|
||||||
{
|
|
||||||
*pwm_ch = MCU_PWM1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t odout_mapping(uint8_t od_ch, uint8_t* gpio_ch)
|
|
||||||
{
|
|
||||||
switch(od_ch)
|
|
||||||
{
|
|
||||||
case BSP_OD1: // LED0
|
|
||||||
*gpio_ch = MCU_GPIO9;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_OD2: // LED1
|
|
||||||
*gpio_ch = MCU_GPIO10;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_OD3: // LED2
|
|
||||||
*gpio_ch = MCU_GPIO11;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_OD4: // LED3
|
|
||||||
*gpio_ch = MCU_GPIO12;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_OD5: // LED4
|
|
||||||
*gpio_ch = MCU_GPIO13;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BSP_OD6: // LED5
|
|
||||||
*gpio_ch = MCU_GPIO14;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
default: //Invalid channel
|
|
||||||
*gpio_ch = 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#ifndef ODOUT_H_
|
|
||||||
#define ODOUT_H_
|
|
||||||
|
|
||||||
/*
|
|
||||||
OD1 LED 0
|
|
||||||
OD2 LED 1
|
|
||||||
OD3 LED 2
|
|
||||||
OD4 LED 3
|
|
||||||
OD5 LED 4
|
|
||||||
OD6 LED 5
|
|
||||||
|
|
||||||
COMMON LED PWM
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define BSP_OD1 1
|
|
||||||
#define BSP_OD2 2
|
|
||||||
#define BSP_OD3 3
|
|
||||||
#define BSP_OD4 4
|
|
||||||
#define BSP_OD5 5
|
|
||||||
#define BSP_OD6 6
|
|
||||||
|
|
||||||
#define BSP_ODOUT_LOW 0
|
|
||||||
#define BSP_ODOUT_HIGH 1
|
|
||||||
#define BSP_ODOUT_HIZ -1
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void bsp_odout_write(uint8_t ch, int8_t lvl);
|
|
||||||
void bsp_odout_write_common(uint8_t percent);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* ODOUT_H_ */
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "utils/utils.h"
|
|
||||||
#include "mcu/mcu_hal.h"
|
|
||||||
#include "setup.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
void bsp_startup(void)
|
|
||||||
{
|
|
||||||
startupCfg_t mcu_cfg;
|
|
||||||
|
|
||||||
mcu_cfg.adc_clk = MCU_ADC_DIV2;
|
|
||||||
mcu_cfg.pwm_clk = MCU_TIM_DIV1;
|
|
||||||
mcu_cfg.pwm_top = 511;
|
|
||||||
mcu_cfg.pwm_chb_en = 1;
|
|
||||||
|
|
||||||
mcu_startup(&mcu_cfg);
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#ifndef BSP_SETUP_H_
|
|
||||||
#define BSP_SETUP_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void bsp_startup(void);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* BSP_SETUP_H_ */
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "faults.h"
|
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Mapping function declarations ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint8_t fault_process(fault_t* fault, uint8_t w_trig, uint8_t f_trig, fault_cfg_t* cfg)
|
|
||||||
{
|
|
||||||
// Override warning trigger in case of fault trigger
|
|
||||||
if(f_trig) w_trig = 1;
|
|
||||||
|
|
||||||
// Increase warning time, if reoccurring
|
|
||||||
if((w_trig)&&(fault->severity != FAULT_LVL_OK)) fault->w_time = util_sat_add_16b(fault->w_time, 1);
|
|
||||||
else fault->w_time = 0;
|
|
||||||
|
|
||||||
// Check if waring can be considered fault
|
|
||||||
if((cfg->wtof > 0)&&(fault->w_time > cfg->wtof)) f_trig = 1;
|
|
||||||
|
|
||||||
// Increase fault time, if reoccurring
|
|
||||||
if((f_trig)&&(fault->severity != FAULT_LVL_OK)) fault->f_time = util_sat_add_16b(fault->f_time, 1);
|
|
||||||
else fault->f_time = 0;
|
|
||||||
|
|
||||||
// Modify fault trigger
|
|
||||||
if((cfg->delay)&&(fault->f_time < cfg->delay)) f_trig = 0;
|
|
||||||
|
|
||||||
// Process fault level
|
|
||||||
if(f_trig)
|
|
||||||
{
|
|
||||||
fault->severity = FAULT_LVL_FAULT;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(w_trig) fault->severity = FAULT_LVL_WARNING;
|
|
||||||
else fault->severity = FAULT_LVL_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t fault_is_active(fault_t* fault)
|
|
||||||
{
|
|
||||||
if(fault->severity == FAULT_LVL_FAULT) return 1;
|
|
||||||
else return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t fault_is_warning(fault_t* fault)
|
|
||||||
{
|
|
||||||
if(fault->severity != FAULT_LVL_OK) return 1;
|
|
||||||
else return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void fault_reset(fault_t* fault)
|
|
||||||
{
|
|
||||||
fault->severity = FAULT_LVL_OK;
|
|
||||||
fault->w_time = 0;
|
|
||||||
fault->f_time = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#ifndef FAULTS_H_
|
|
||||||
#define FAULTS_H_
|
|
||||||
|
|
||||||
/*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
typedef enum {
|
|
||||||
FAULT_LVL_OK,
|
|
||||||
FAULT_LVL_WARNING,
|
|
||||||
FAULT_LVL_FAULT
|
|
||||||
} fault_lvl_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
fault_lvl_t severity;
|
|
||||||
uint16_t w_time;
|
|
||||||
uint16_t f_time;
|
|
||||||
} fault_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint16_t delay;
|
|
||||||
uint16_t wtof;
|
|
||||||
} fault_cfg_t;
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
uint8_t fault_process(fault_t* fault, uint8_t w_trig, uint8_t f_trig, fault_cfg_t* cfg);
|
|
||||||
uint8_t fault_is_active(fault_t* fault);
|
|
||||||
uint8_t fault_is_warning(fault_t* fault);
|
|
||||||
void fault_reset(fault_t* fault);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* FAULTS_H_ */
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "fuses.h"
|
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Mapping function declarations ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void fuse_reset(fuse_t* fuse)
|
|
||||||
{
|
|
||||||
fuse->state = FUSE_OFF;
|
|
||||||
fuse->timer = 0;
|
|
||||||
fuse->count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t fuse_process(fuse_t* fuse, uint8_t fault, fuse_cfg_t* cfg)
|
|
||||||
{
|
|
||||||
// Active fault condition
|
|
||||||
if(fault)
|
|
||||||
{
|
|
||||||
// Note fuse time count
|
|
||||||
if((fuse->state == FUSE_OFF)||(fuse->state == FUSE_RETRY)) fuse->count++;
|
|
||||||
|
|
||||||
// Go to fused state in any case
|
|
||||||
fuse->state = FUSE_ACTIVE;
|
|
||||||
fuse->timer = 0;
|
|
||||||
return 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
// No active fault condition
|
|
||||||
if(fuse->state==FUSE_ACTIVE)
|
|
||||||
{
|
|
||||||
// Go to cooldown
|
|
||||||
fuse->state = FUSE_COOLDOWN;
|
|
||||||
fuse->timer = cfg->cooldown_time;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Wait for timeout
|
|
||||||
if(fuse->timer)
|
|
||||||
{
|
|
||||||
fuse->timer--;
|
|
||||||
if(fuse->state == FUSE_RETRY) return 0;
|
|
||||||
else return 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Timeout end, transition logic
|
|
||||||
switch(fuse->state)
|
|
||||||
{
|
|
||||||
case FUSE_COOLDOWN:
|
|
||||||
// Cooldown end
|
|
||||||
if(cfg->retry_time)
|
|
||||||
{
|
|
||||||
fuse->state = FUSE_RETRY;
|
|
||||||
fuse->timer = cfg->retry_time;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fuse->state = FUSE_OFF;
|
|
||||||
fuse->timer = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FUSE_RETRY:
|
|
||||||
// Go back to normal
|
|
||||||
fuse->state = FUSE_OFF;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
// Nothing to do
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
#ifndef FUSES_H_
|
|
||||||
#define FUSES_H_
|
|
||||||
|
|
||||||
/*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
typedef enum {
|
|
||||||
FUSE_OFF,
|
|
||||||
FUSE_ACTIVE,
|
|
||||||
FUSE_COOLDOWN,
|
|
||||||
FUSE_RETRY
|
|
||||||
} fuse_state_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
fuse_state_t state;
|
|
||||||
uint8_t count;
|
|
||||||
uint16_t timer;
|
|
||||||
} fuse_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint16_t cooldown_time;
|
|
||||||
uint16_t retry_time;
|
|
||||||
} fuse_cfg_t;
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void fuse_reset(fuse_t* fuse);
|
|
||||||
uint8_t fuse_process(fuse_t* fuse, uint8_t fault, fuse_cfg_t* cfg);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* FUSES_H_ */
|
|
||||||
@@ -1,272 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
#ifndef TESTING
|
|
||||||
static uint8_t find_interval_end_index(uint16_t val, uint16_t* axis_values, uint8_t len_axis);
|
|
||||||
static uint16_t interpolate_u16b(uint16_t x, uint16_t x0, uint16_t x1, uint16_t y0, uint16_t y1);
|
|
||||||
static uint16_t index2d_to_index1d(uint8_t ix, uint8_t iy, uint8_t len_x);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
uint8_t util_invert_8b(uint8_t x)
|
|
||||||
{
|
|
||||||
if(x!=0) return 0;
|
|
||||||
else return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t util_sat_add_8b(uint8_t x, uint8_t y)
|
|
||||||
{
|
|
||||||
uint8_t z = x + y;
|
|
||||||
// Check for overflow
|
|
||||||
if((z < x)||(z < y)) return 0xFF;
|
|
||||||
else return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t util_sat_subtract_8b(uint8_t x, uint8_t y)
|
|
||||||
{
|
|
||||||
uint8_t z = x - y;
|
|
||||||
// Check for underflow
|
|
||||||
if(z > x) return 0;
|
|
||||||
else return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_sat_add_16b(uint16_t x, uint16_t y)
|
|
||||||
{
|
|
||||||
uint16_t z = x + y;
|
|
||||||
// Check for overflow
|
|
||||||
if((z < x)||(z < y)) return 0xFFFF;
|
|
||||||
else return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_sat_subtract_16b(uint16_t x, uint16_t y)
|
|
||||||
{
|
|
||||||
uint16_t z = x - y;
|
|
||||||
// Check for underflow
|
|
||||||
if(z > x) return 0;
|
|
||||||
else return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_limit_u32b_to_u16b(uint32_t in)
|
|
||||||
{
|
|
||||||
if(in == 0) return 0;
|
|
||||||
else if(in >= 0x0000FFFF) return 0xFFFF;
|
|
||||||
else return (uint16_t)in;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_limit_s32b_to_u16b(int32_t in)
|
|
||||||
{
|
|
||||||
if(in <= 0) return 0;
|
|
||||||
else if(in >= 0x0000FFFF) return 0xFFFF;
|
|
||||||
else return (uint16_t)in;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_convert_muldivoff(uint16_t raw, uint8_t mul, uint8_t div, int16_t offset)
|
|
||||||
{
|
|
||||||
int32_t temp = (int32_t)raw;
|
|
||||||
|
|
||||||
temp = temp * mul;
|
|
||||||
if(div>1) temp /= div;
|
|
||||||
temp += offset;
|
|
||||||
|
|
||||||
return util_limit_s32b_to_u16b(temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_sat_mul_kilo(uint16_t xk, uint16_t yk)
|
|
||||||
{
|
|
||||||
uint32_t temp = (uint32_t)xk * (uint32_t)yk;
|
|
||||||
temp /= 1000;
|
|
||||||
|
|
||||||
return util_limit_u32b_to_u16b(temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_sat_div_kilo(uint16_t top, uint16_t bot)
|
|
||||||
{
|
|
||||||
//Sanity check bot
|
|
||||||
if(bot==0) return 0xFFFF; //aka infinity
|
|
||||||
|
|
||||||
uint32_t temp = (uint32_t)top * 1000;
|
|
||||||
temp /= (uint32_t)bot;
|
|
||||||
|
|
||||||
return util_limit_u32b_to_u16b(temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_sat_ratio_16b(uint16_t top, uint16_t bot)
|
|
||||||
{
|
|
||||||
//Sanity check bot
|
|
||||||
if(bot==0) return 0xFFFF; //aka infinity
|
|
||||||
|
|
||||||
//Easy option
|
|
||||||
if(top>=bot) return 0xFFFF;
|
|
||||||
|
|
||||||
uint32_t temp = (uint32_t)top * 0x0000FFFF;
|
|
||||||
temp /= (uint32_t)bot;
|
|
||||||
|
|
||||||
return util_limit_u32b_to_u16b(temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_percent_to_16b(uint8_t percent)
|
|
||||||
{
|
|
||||||
uint32_t temp = (uint32_t)percent * 0x0000FFFF;
|
|
||||||
temp /= 100;
|
|
||||||
|
|
||||||
// Limit to 16 bits
|
|
||||||
uint16_t pwm = util_limit_u32b_to_u16b(temp);
|
|
||||||
|
|
||||||
return pwm;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_interpolate_1d_u16b(uint16_t x, uint16_t* x_axis, uint16_t* y_values, uint8_t len_axis)
|
|
||||||
{
|
|
||||||
// validate axis length
|
|
||||||
if(len_axis==0) return 0; // Empty data set
|
|
||||||
if(len_axis==1) return y_values[0]; // Only one data point
|
|
||||||
|
|
||||||
uint16_t y;
|
|
||||||
|
|
||||||
uint8_t i = find_interval_end_index(x, x_axis, len_axis);
|
|
||||||
if(i==0)
|
|
||||||
{
|
|
||||||
//Less then start
|
|
||||||
y = y_values[0];
|
|
||||||
}
|
|
||||||
else if(i==len_axis)
|
|
||||||
{
|
|
||||||
//More than end
|
|
||||||
y = y_values[len_axis-1];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Do interpolate
|
|
||||||
y = interpolate_u16b(x, x_axis[i-1], x_axis[i], y_values[i-1], y_values[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return y;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t util_interpolate_2d_u16b(uint16_t x, uint16_t y, uint16_t* x_axis, uint8_t len_x_axis, uint16_t* y_axis, uint8_t len_y_axis, uint16_t* z_values)
|
|
||||||
{
|
|
||||||
// validate axis length
|
|
||||||
if((len_x_axis==0)&&(len_y_axis==0)) return 0; // Empty data set
|
|
||||||
if((len_x_axis==1)&&(len_y_axis==1)) return z_values[0]; // Only one data point
|
|
||||||
|
|
||||||
uint8_t ix = find_interval_end_index(x, x_axis, len_x_axis);
|
|
||||||
uint8_t iy = find_interval_end_index(y, y_axis, len_y_axis);
|
|
||||||
|
|
||||||
// Check corners - easy answers
|
|
||||||
if((ix==0)&&(iy==0))
|
|
||||||
{
|
|
||||||
return z_values[0]; //[0][0] [Y][X]
|
|
||||||
}
|
|
||||||
else if((ix==len_x_axis)&&(iy==0))
|
|
||||||
{
|
|
||||||
return z_values[len_x_axis-1]; //[0][end]
|
|
||||||
}
|
|
||||||
else if((ix==0)&&(iy==len_y_axis))
|
|
||||||
{
|
|
||||||
uint16_t i = index2d_to_index1d(0, len_y_axis-1, len_x_axis);
|
|
||||||
return z_values[i]; //[end][0]
|
|
||||||
}
|
|
||||||
else if((ix==len_x_axis)&&(iy==len_y_axis))
|
|
||||||
{
|
|
||||||
uint16_t i = index2d_to_index1d(len_x_axis-1, len_y_axis-1, len_x_axis);
|
|
||||||
return z_values[i]; //[end][end]
|
|
||||||
};
|
|
||||||
|
|
||||||
// Check boundaries - 1D interpolation
|
|
||||||
if(ix==0)
|
|
||||||
{
|
|
||||||
// On ix=0 line
|
|
||||||
uint16_t i = 0;
|
|
||||||
uint16_t z0 = z_values[i];
|
|
||||||
i = index2d_to_index1d(0, len_y_axis-1, len_x_axis);
|
|
||||||
uint16_t z1 = z_values[i];
|
|
||||||
return interpolate_u16b(y, y_axis[0], y_axis[len_y_axis-1], z0, z1);
|
|
||||||
}
|
|
||||||
else if(ix==len_x_axis)
|
|
||||||
{
|
|
||||||
// On ix=END line
|
|
||||||
uint16_t i = len_x_axis-1;
|
|
||||||
uint16_t z0 = z_values[i];
|
|
||||||
i = index2d_to_index1d(len_x_axis-1, len_y_axis-1, len_x_axis);
|
|
||||||
uint16_t z1 = z_values[i];
|
|
||||||
return interpolate_u16b(y, y_axis[0], y_axis[len_y_axis-1], z0, z1);
|
|
||||||
}
|
|
||||||
else if(iy==0)
|
|
||||||
{
|
|
||||||
// On iy=0 line
|
|
||||||
uint16_t i = 0;
|
|
||||||
uint16_t z0 = z_values[i];
|
|
||||||
i = len_x_axis-1;
|
|
||||||
uint16_t z1 = z_values[i];
|
|
||||||
return interpolate_u16b(x, x_axis[0], x_axis[len_x_axis-1], z0, z1);
|
|
||||||
}
|
|
||||||
else if(iy==len_y_axis)
|
|
||||||
{
|
|
||||||
// On iy=END line
|
|
||||||
uint16_t i = index2d_to_index1d(0, len_y_axis-1, len_x_axis);
|
|
||||||
uint16_t z0 = z_values[i];
|
|
||||||
i = index2d_to_index1d(len_x_axis-1, len_y_axis-1, len_x_axis);
|
|
||||||
uint16_t z1 = z_values[i];
|
|
||||||
return interpolate_u16b(x, x_axis[0], x_axis[len_x_axis-1], z0, z1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do interpolation
|
|
||||||
// Get axis values
|
|
||||||
uint16_t x0 = x_axis[ix-1];
|
|
||||||
uint16_t x1 = x_axis[ix];
|
|
||||||
uint16_t y0 = y_axis[iy-1];
|
|
||||||
uint16_t y1 = y_axis[iy];
|
|
||||||
|
|
||||||
// Do y0 line calculation
|
|
||||||
// Get z values at x0 and x1 points on y0 line
|
|
||||||
uint16_t i = index2d_to_index1d(ix-1, iy-1, len_x_axis);
|
|
||||||
uint16_t z0 = z_values[i];
|
|
||||||
uint16_t z1 = z_values[i+1];
|
|
||||||
// Interpolate z value on y0 line
|
|
||||||
uint16_t zy0 = interpolate_u16b(x, x0, x1, z0, z1);
|
|
||||||
|
|
||||||
// Do y1 line calculation
|
|
||||||
// Get z values at x0 and x1 points on y1 line
|
|
||||||
i = index2d_to_index1d(ix-1, iy, len_x_axis);
|
|
||||||
z0 = z_values[i];
|
|
||||||
z1 = z_values[i+1];
|
|
||||||
// Interpolate z value on y0 line
|
|
||||||
uint16_t zy1 = interpolate_u16b(x, x0, x1, z0, z1);
|
|
||||||
|
|
||||||
// Do calculation in y axis on xz line
|
|
||||||
return interpolate_u16b(y, y0, y1, zy0, zy1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
static uint16_t interpolate_u16b(uint16_t x, uint16_t x0, uint16_t x1, uint16_t y0, uint16_t y1)
|
|
||||||
{
|
|
||||||
int32_t dy = (int32_t)y1 - (int32_t)y0;
|
|
||||||
int32_t dx = (int32_t)x1 - (int32_t)x0;
|
|
||||||
int32_t d = (int32_t)x - (int32_t)x0;
|
|
||||||
|
|
||||||
int32_t y = dy * d;
|
|
||||||
y /= dx;
|
|
||||||
y += y0;
|
|
||||||
|
|
||||||
return util_limit_s32b_to_u16b(y);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t find_interval_end_index(uint16_t val, uint16_t* axis_values, uint8_t len_axis)
|
|
||||||
{
|
|
||||||
for(uint8_t i=0; i<len_axis; i++)
|
|
||||||
{
|
|
||||||
if(val < axis_values[i]) return i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return len_axis;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint16_t index2d_to_index1d(uint8_t ix, uint8_t iy, uint8_t len_x)
|
|
||||||
{
|
|
||||||
return ((uint16_t)len_x * iy) + ix;
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#ifndef UTILS_H_
|
|
||||||
#define UTILS_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
uint8_t util_invert_8b(uint8_t x);
|
|
||||||
|
|
||||||
uint16_t util_limit_u32b_to_u16b(uint32_t in);
|
|
||||||
uint16_t util_limit_s32b_to_u16b(int32_t in);
|
|
||||||
|
|
||||||
uint16_t util_convert_muldivoff(uint16_t raw, uint8_t mul, uint8_t div, int16_t offset);
|
|
||||||
uint16_t util_sat_mul_kilo(uint16_t xk, uint16_t yk);
|
|
||||||
uint16_t util_sat_div_kilo(uint16_t top, uint16_t bot);
|
|
||||||
uint16_t util_sat_ratio_16b(uint16_t top, uint16_t bot);
|
|
||||||
uint16_t util_percent_to_16b(uint8_t percent);
|
|
||||||
|
|
||||||
uint8_t util_sat_add_8b(uint8_t x, uint8_t y);
|
|
||||||
uint8_t util_sat_subtract_8b(uint8_t x, uint8_t y);
|
|
||||||
|
|
||||||
uint16_t util_sat_add_16b(uint16_t x, uint16_t y);
|
|
||||||
uint16_t util_sat_subtract_16b(uint16_t x, uint16_t y);
|
|
||||||
|
|
||||||
uint16_t util_interpolate_1d_u16b(uint16_t x, uint16_t* x_axis, uint16_t* y_values, uint8_t len_axis);
|
|
||||||
uint16_t util_interpolate_2d_u16b(uint16_t x, uint16_t y, uint16_t* x_axis, uint8_t len_x_axis, uint16_t* y_axis, uint8_t len_y_axis, uint16_t* z_values);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
// Access to private functions for unit testing
|
|
||||||
static uint8_t find_interval_end_index(uint16_t val, uint16_t* axis_values, uint8_t len_axis);
|
|
||||||
static uint16_t interpolate_u16b(uint16_t x, uint16_t x0, uint16_t x1, uint16_t y0, uint16_t y1);
|
|
||||||
static uint16_t index2d_to_index1d(uint8_t ix, uint8_t iy, uint8_t len_x);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* UTILS_H_ */
|
|
||||||
94
firmware/src/hw/button.cpp
Normal file
94
firmware/src/hw/button.cpp
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "button.h"
|
||||||
|
|
||||||
|
using namespace hw;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
hw::Button::Button(bsp::DigitalIn* din_ch, uint8_t act_lvl, uint8_t dbnc_lim, uint8_t init_state)
|
||||||
|
{
|
||||||
|
this->din_ch = din_ch;
|
||||||
|
|
||||||
|
if(act_lvl) this->act_lvl = bsp::DIN_HIGH;
|
||||||
|
else this->act_lvl = bsp::DIN_LOW;
|
||||||
|
|
||||||
|
this->dbnc_cnter = 0;
|
||||||
|
this->dbnc_lim = dbnc_lim;
|
||||||
|
|
||||||
|
if(init_state) this->state = BUTTON_ON;
|
||||||
|
else this->state = BUTTON_OFF;
|
||||||
|
|
||||||
|
this->time = 0;
|
||||||
|
this->is_new = 0;
|
||||||
|
|
||||||
|
this->hold_time = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
hw::Button::~Button(void)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t hw::Button::update(void)
|
||||||
|
{
|
||||||
|
// Read din level
|
||||||
|
uint8_t lvl = this->din_ch->last_read;
|
||||||
|
|
||||||
|
// Increase state counter
|
||||||
|
this->time = util::sat_add(this->time, 1);
|
||||||
|
|
||||||
|
// Repeat new flag after hold time
|
||||||
|
if((this->state == BUTTON_ON)&&(this->time > this->hold_time)&&(this->hold_time > 0))
|
||||||
|
{
|
||||||
|
this->time = 0;
|
||||||
|
this->is_new = 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Determine next state
|
||||||
|
uint8_t next_state = BUTTON_OFF;
|
||||||
|
if(lvl==this->act_lvl) next_state = BUTTON_ON;
|
||||||
|
|
||||||
|
// Advance debounce sample counter
|
||||||
|
if(next_state != this->state) this->dbnc_cnter++;
|
||||||
|
else this->dbnc_cnter = 0;
|
||||||
|
|
||||||
|
// Check for debounce end
|
||||||
|
if(this->dbnc_cnter < this->dbnc_lim) return this->state;
|
||||||
|
|
||||||
|
// Debounce end. Apply new state.
|
||||||
|
this->state = next_state;
|
||||||
|
this->time = 0;
|
||||||
|
this->is_new = 1;
|
||||||
|
this->dbnc_cnter = 0;
|
||||||
|
|
||||||
|
return this->state;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t hw::Button::force_update(void)
|
||||||
|
{
|
||||||
|
// Read din level
|
||||||
|
uint8_t lvl = this->din_ch->read();
|
||||||
|
|
||||||
|
// Cancels active debounce
|
||||||
|
this->dbnc_cnter = 0;
|
||||||
|
|
||||||
|
// Determine next state
|
||||||
|
uint8_t next_state = BUTTON_OFF;
|
||||||
|
if(lvl==this->act_lvl) next_state = BUTTON_ON;
|
||||||
|
|
||||||
|
if(next_state != this->state)
|
||||||
|
{
|
||||||
|
this->state = next_state;
|
||||||
|
this->time = 0;
|
||||||
|
this->is_new = 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
return this->state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
42
firmware/src/hw/button.h
Normal file
42
firmware/src/hw/button.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#ifndef BUTTON_H_
|
||||||
|
#define BUTTON_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "../bsp/din.h"
|
||||||
|
|
||||||
|
namespace hw {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
const uint8_t BUTTON_OFF = 0;
|
||||||
|
const uint8_t BUTTON_ON = 1;
|
||||||
|
|
||||||
|
class Button
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
bsp::DigitalIn* din_ch;
|
||||||
|
uint8_t act_lvl;
|
||||||
|
uint8_t dbnc_cnter;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Button(bsp::DigitalIn* din_ch, uint8_t act_lvl, uint8_t dbnc_lim, uint8_t init_state);
|
||||||
|
~Button(void);
|
||||||
|
|
||||||
|
uint8_t state;
|
||||||
|
uint16_t time;
|
||||||
|
uint8_t dbnc_lim;
|
||||||
|
uint8_t is_new;
|
||||||
|
uint16_t hold_time;
|
||||||
|
|
||||||
|
uint8_t update(void);
|
||||||
|
uint8_t force_update(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* BUTTON_H_ */
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "board/utils/utils.h"
|
|
||||||
#include "board/din.h"
|
|
||||||
#include "board/dout.h"
|
|
||||||
#include "buttons.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
static const uint8_t DEF_DBNC_LIM = HW_BTN_DEF_DBNC_LIM;
|
|
||||||
static const uint16_t DEF_REPEAT_TIME = HW_BTN_DEF_REPEAT_TIME;
|
|
||||||
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
static uint8_t buttons_mapping(uint8_t btn_ch, uint8_t* din_ch, btn_cfg_t* cfg);
|
|
||||||
static uint8_t buttons_pull_mapping(uint8_t btn_ch, uint8_t* dout_ch);
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
void btn_reset(btn_t* btn)
|
|
||||||
{
|
|
||||||
btn->state = BTN_INACTIVE;
|
|
||||||
btn->new_state = 0;
|
|
||||||
btn->state_time = 0;
|
|
||||||
btn->transition_cntr = 0;
|
|
||||||
btn->dbnc_active = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t btn_ch_process(uint8_t btn_ch, btn_t* btn)
|
|
||||||
{
|
|
||||||
// Get channel config
|
|
||||||
uint8_t din_ch;
|
|
||||||
btn_cfg_t cfg;
|
|
||||||
if(buttons_mapping(btn_ch, &din_ch, &cfg)) return BTN_INACTIVE;
|
|
||||||
|
|
||||||
// Read din level
|
|
||||||
uint8_t lvl = bsp_din_read(din_ch);
|
|
||||||
|
|
||||||
// Process button logic
|
|
||||||
uint8_t btn_act = btn_process(lvl, btn, &cfg);
|
|
||||||
return btn_act;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t btn_process(uint8_t lvl, btn_t* btn, btn_cfg_t* cfg)
|
|
||||||
{
|
|
||||||
// Increase state time
|
|
||||||
btn->state_time = util_sat_add_16b(btn->state_time, 1);
|
|
||||||
|
|
||||||
// Check repeated new flag
|
|
||||||
if((cfg->repeat_time)&&(btn->state_time < 0xFFFF))
|
|
||||||
{
|
|
||||||
if((btn->state_time)%(cfg->repeat_time) == 0) btn->new_state = 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Calculate saved level from state and active level cfg
|
|
||||||
uint8_t prev_lvl = 0;
|
|
||||||
if(((btn->dbnc_active)&&(btn->state))||((!btn->dbnc_active)&&(!btn->state))) prev_lvl = util_invert_8b(cfg->act_lvl);
|
|
||||||
else prev_lvl = cfg->act_lvl;
|
|
||||||
|
|
||||||
// Check if level changed
|
|
||||||
uint8_t lvl_chnaged = 0;
|
|
||||||
if(lvl!=prev_lvl) lvl_chnaged = 1;
|
|
||||||
|
|
||||||
//Process debounce logic
|
|
||||||
if(lvl_chnaged)
|
|
||||||
{
|
|
||||||
// Changed debounce state
|
|
||||||
if(!btn->dbnc_active) btn->dbnc_active = 1; // Start debounce
|
|
||||||
else btn->dbnc_active = 0; // Stop debounce
|
|
||||||
// Reset debounce counter
|
|
||||||
btn->transition_cntr = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Continue debounce
|
|
||||||
if(btn->dbnc_active) btn->transition_cntr += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for debounce end
|
|
||||||
if((btn->dbnc_active)&&(btn->transition_cntr >= cfg->dbnc_lim))
|
|
||||||
{
|
|
||||||
// Reset debounce
|
|
||||||
btn->dbnc_active = 0; //End debounce
|
|
||||||
btn->transition_cntr = 0;
|
|
||||||
// Set new state
|
|
||||||
if(lvl==cfg->act_lvl) btn->state = BTN_ACTIVE;
|
|
||||||
else btn->state = BTN_INACTIVE;
|
|
||||||
btn->state_time = 0; //Reset timer
|
|
||||||
btn->new_state = 1; //Set new flag
|
|
||||||
};
|
|
||||||
|
|
||||||
return btn->state;
|
|
||||||
}
|
|
||||||
|
|
||||||
void btn_ch_set_pull(uint8_t btn_ch, int8_t lvl)
|
|
||||||
{
|
|
||||||
// Get channel config
|
|
||||||
uint8_t dout_ch;
|
|
||||||
if(buttons_pull_mapping(btn_ch, &dout_ch)) return;
|
|
||||||
|
|
||||||
// Set button pull
|
|
||||||
bsp_dout_write(dout_ch, lvl);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
static uint8_t buttons_mapping(uint8_t btn_ch, uint8_t* din_ch, btn_cfg_t* cfg)
|
|
||||||
{
|
|
||||||
// Default, most common config
|
|
||||||
cfg->act_lvl = BSP_DIN_LOW;
|
|
||||||
cfg->dbnc_lim = DEF_DBNC_LIM;
|
|
||||||
cfg->repeat_time = DEF_REPEAT_TIME;
|
|
||||||
|
|
||||||
switch(btn_ch)
|
|
||||||
{
|
|
||||||
case BTN_1: // Mode
|
|
||||||
*din_ch = BSP_DIN1;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BTN_2: // Down
|
|
||||||
*din_ch = BSP_DIN3;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BTN_3: // Up
|
|
||||||
*din_ch = BSP_DIN4;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BTN_4: // Dimm
|
|
||||||
*din_ch = BSP_DIN5;
|
|
||||||
cfg->act_lvl = BSP_DIN_HIGH;
|
|
||||||
cfg->repeat_time = 0;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BTN_5: // Brakes
|
|
||||||
*din_ch = BSP_DIN6;
|
|
||||||
cfg->act_lvl = BSP_DIN_HIGH;
|
|
||||||
cfg->repeat_time = 0;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BTN_6: // Handbrake
|
|
||||||
*din_ch = BSP_DIN7;
|
|
||||||
cfg->repeat_time = 0;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BTN_6N: // Handbrake inverted
|
|
||||||
*din_ch = BSP_DIN7N;
|
|
||||||
cfg->repeat_time = 0;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
default: //Invalid channel
|
|
||||||
*din_ch = 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t buttons_pull_mapping(uint8_t btn_ch, uint8_t* dout_ch)
|
|
||||||
{
|
|
||||||
switch(btn_ch)
|
|
||||||
{
|
|
||||||
case BTN_6: // Handbrake
|
|
||||||
*dout_ch = BSP_DOUT5;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case BTN_6N: // Handbrake inverted
|
|
||||||
*dout_ch = BSP_DOUT5;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
default: //Invalid channel
|
|
||||||
*dout_ch = 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
#ifndef BUTTONS_H_
|
|
||||||
#define BUTTONS_H_
|
|
||||||
|
|
||||||
/*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
#include "config.h"
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
typedef struct {
|
|
||||||
uint8_t act_lvl;
|
|
||||||
uint8_t dbnc_lim;
|
|
||||||
uint16_t repeat_time;
|
|
||||||
} btn_cfg_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t state;
|
|
||||||
uint8_t new_state;
|
|
||||||
uint16_t state_time;
|
|
||||||
uint8_t transition_cntr;
|
|
||||||
uint8_t dbnc_active;
|
|
||||||
} btn_t;
|
|
||||||
|
|
||||||
#define BTN_1 1 //DIN1 Mode
|
|
||||||
#define BTN_2 2 //DIN3 Down
|
|
||||||
#define BTN_3 3 //DIN4 Up
|
|
||||||
#define BTN_4 4 //DIN5 Dimm
|
|
||||||
#define BTN_5 5 //DIN6 Brakes
|
|
||||||
#define BTN_6 6 //DIN7 Handbrake
|
|
||||||
#define BTN_6N 7 //DIN7N Direct handbrake
|
|
||||||
|
|
||||||
#define BTN_INACTIVE 0
|
|
||||||
#define BTN_ACTIVE 1
|
|
||||||
|
|
||||||
#define BTN_PULL_LOW 0
|
|
||||||
#define BTN_PULL_HIGH 1
|
|
||||||
#define BTN_PULL_NONE -1
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void btn_reset(btn_t* btn);
|
|
||||||
|
|
||||||
// Using internal map
|
|
||||||
uint8_t btn_ch_process(uint8_t btn_ch, btn_t* btn);
|
|
||||||
void btn_ch_set_pull(uint8_t btn_ch, int8_t lvl);
|
|
||||||
|
|
||||||
// Manual process
|
|
||||||
uint8_t btn_process(uint8_t lvl, btn_t* btn, btn_cfg_t* cfg);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* BUTTONS_H_ */
|
|
||||||
41
firmware/src/hw/cc_output.cpp
Normal file
41
firmware/src/hw/cc_output.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "cc_output.h"
|
||||||
|
|
||||||
|
using namespace hw;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
hw::CCoutput::CCoutput(bsp::Hafbridge* hbridge, bsp::AnalogIn* supply_u, bsp::AnalogIn* out_u, bsp::AnalogIn* out_i) : CVoutput(hbridge, supply_u)
|
||||||
|
{
|
||||||
|
this->out_voltage = out_u;
|
||||||
|
this->out_currnet = out_i;
|
||||||
|
|
||||||
|
this->out_impedance = 0xFFFF;
|
||||||
|
this->target_voltage = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw::CCoutput::update(void)
|
||||||
|
{
|
||||||
|
// Calculate output impedance
|
||||||
|
if((this->out_currnet == 0)||(this->out_voltage->last_read == 0)) this->out_impedance = 0xFFFF;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this->out_impedance = util::sat_div_kilo(this->out_voltage->last_read, this->out_currnet->last_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check target
|
||||||
|
if((this->target < this->min_out)&&(this->target > 0)) this->target = this->min_out;
|
||||||
|
|
||||||
|
// Convert target current to voltage
|
||||||
|
this->target_voltage = util::sat_mul_kilo(this->target, this->out_impedance);
|
||||||
|
|
||||||
|
// Set output
|
||||||
|
this->hbridge->write(util::sat_ratio(this->target_voltage, this->supply->last_read));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
35
firmware/src/hw/cc_output.h
Normal file
35
firmware/src/hw/cc_output.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#ifndef CONST_CURRENT_OUTPUT_H_
|
||||||
|
#define CONST_CURRENT_OUTPUT_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "../bsp/ain.h"
|
||||||
|
#include "cv_output.h"
|
||||||
|
|
||||||
|
namespace hw {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
|
||||||
|
class CCoutput : public CVoutput
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
bsp::AnalogIn* out_voltage;
|
||||||
|
bsp::AnalogIn* out_currnet;
|
||||||
|
|
||||||
|
public:
|
||||||
|
CCoutput(bsp::Hafbridge* hbridge, bsp::AnalogIn* supply_u, bsp::AnalogIn* out_u, bsp::AnalogIn* out_i);
|
||||||
|
|
||||||
|
void update(void);
|
||||||
|
|
||||||
|
uint16_t out_impedance;
|
||||||
|
uint16_t target_voltage;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* CONST_VOLTAGE_OUTPUT_H_ */
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
#ifndef HW_CONFIG_H_
|
|
||||||
#define HW_CONFIG_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define HW_BTN_DEF_DBNC_LIM 10
|
|
||||||
#define HW_BTN_DEF_REPEAT_TIME 1000
|
|
||||||
|
|
||||||
#define HW_HB_SUPPLY_VOLT_MIN_W 10000
|
|
||||||
#define HW_HB_SUPPLY_VOLT_MIN_F 8000
|
|
||||||
|
|
||||||
#define HW_HB_SUPPLY_VOLT_MAX_W 16000
|
|
||||||
#define HW_HB_SUPPLY_VOLT_MAX_F 19500
|
|
||||||
|
|
||||||
#define HW_HB_SUPPLY_CURRENT_MAX_W 6000
|
|
||||||
#define HW_HB_SUPPLY_CURRENT_MAX_F 8000
|
|
||||||
|
|
||||||
#define HW_HB_SUPPLY_POWER_MAX_W 30000
|
|
||||||
#define HW_HB_SUPPLY_POWER_MAX_F 45000
|
|
||||||
|
|
||||||
#define HW_HB_OUT_CURRENT_MAX_W 6000
|
|
||||||
#define HW_HB_OUT_CURRENT_MAX_F 8000
|
|
||||||
|
|
||||||
#define HW_HB_OUT_VOLTAGE_MAX_W 8000
|
|
||||||
#define HW_HB_OUT_VOLTAGE_MAX_F 10000
|
|
||||||
|
|
||||||
#define HW_HB_OUT_POWER_MAX_W 30000
|
|
||||||
#define HW_HB_OUT_POWER_MAX_F 40000
|
|
||||||
|
|
||||||
#define HW_HB_OUT_RESISTANCE_MIN_W 750
|
|
||||||
#define HW_HB_OUT_RESISTANCE_MIN_F 500
|
|
||||||
|
|
||||||
#define HW_HB_OUT_RESISTANCE_MAX_W 5000
|
|
||||||
#define HW_HB_OUT_RESISTANCE_MAX_F 10000
|
|
||||||
|
|
||||||
#define HW_HB_FAULT_DELAY 500
|
|
||||||
#define HW_HB_WTOF_DELAY 0
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* HW_CONFIG_H_ */
|
|
||||||
53
firmware/src/hw/cv_output.cpp
Normal file
53
firmware/src/hw/cv_output.cpp
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "cv_output.h"
|
||||||
|
|
||||||
|
using namespace hw;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
hw::CVoutput::CVoutput(bsp::Hafbridge* hbridge, bsp::AnalogIn* supply_u)
|
||||||
|
{
|
||||||
|
this->hbridge = hbridge;
|
||||||
|
this->supply = supply_u;
|
||||||
|
this->target = 0;
|
||||||
|
this->min_out = 0;
|
||||||
|
this->hbridge->disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
hw::CVoutput::~CVoutput(void)
|
||||||
|
{
|
||||||
|
this->hbridge->write((uint16_t)0x0000);
|
||||||
|
this->hbridge->disable();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw::CVoutput::update(void)
|
||||||
|
{
|
||||||
|
// Check target
|
||||||
|
if((this->target < this->min_out)&&(this->target > 0)) this->target = this->min_out;
|
||||||
|
|
||||||
|
// Set output
|
||||||
|
this->hbridge->write(util::sat_ratio(this->target, this->supply->last_read));
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw::CVoutput::enable(void)
|
||||||
|
{
|
||||||
|
this->hbridge->enable();
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw::CVoutput::disable(void)
|
||||||
|
{
|
||||||
|
this->hbridge->disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t hw::CVoutput::is_enabled(void)
|
||||||
|
{
|
||||||
|
return this->hbridge->is_enabled();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
39
firmware/src/hw/cv_output.h
Normal file
39
firmware/src/hw/cv_output.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#ifndef CONST_VOLTAGE_OUTPUT_H_
|
||||||
|
#define CONST_VOLTAGE_OUTPUT_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "../bsp/ain.h"
|
||||||
|
#include "../bsp/halfbridge.h"
|
||||||
|
|
||||||
|
namespace hw {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
|
||||||
|
class CVoutput
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
bsp::Hafbridge* hbridge;
|
||||||
|
bsp::AnalogIn* supply;
|
||||||
|
|
||||||
|
public:
|
||||||
|
CVoutput(bsp::Hafbridge* hbridge, bsp::AnalogIn* supply_u);
|
||||||
|
~CVoutput(void);
|
||||||
|
|
||||||
|
uint16_t target;
|
||||||
|
uint16_t min_out;
|
||||||
|
|
||||||
|
void update(void);
|
||||||
|
void enable(void);
|
||||||
|
void disable(void);
|
||||||
|
uint8_t is_enabled(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* CONST_VOLTAGE_OUTPUT_H_ */
|
||||||
65
firmware/src/hw/devices.cpp
Normal file
65
firmware/src/hw/devices.cpp
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "devices.h"
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
static const uint16_t def_button_hold_time = 1000;
|
||||||
|
static const uint16_t def_min_current = 100;
|
||||||
|
static const uint16_t def_fuse_treshold = 6000;
|
||||||
|
static const uint16_t def_fuse_hold_cycles = 50;
|
||||||
|
static const uint16_t def_fuse_cooldown_cycles = 1000;
|
||||||
|
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
void devices_init(void)
|
||||||
|
{
|
||||||
|
board_init();
|
||||||
|
|
||||||
|
btn_up.hold_time = def_button_hold_time;
|
||||||
|
btn_down.hold_time = def_button_hold_time;
|
||||||
|
|
||||||
|
ccout.target = 0;
|
||||||
|
ccout.min_out = def_min_current;
|
||||||
|
|
||||||
|
sup_fuse.hold_current = def_fuse_treshold;
|
||||||
|
sup_fuse.trip_cycles = def_fuse_hold_cycles;
|
||||||
|
sup_fuse.cooldown_cycles = def_fuse_cooldown_cycles;
|
||||||
|
|
||||||
|
out_fuse.hold_current = def_fuse_treshold;
|
||||||
|
out_fuse.trip_cycles = def_fuse_hold_cycles;
|
||||||
|
out_fuse.cooldown_cycles = def_fuse_cooldown_cycles;
|
||||||
|
|
||||||
|
hvdin3_pull.write(bsp::DOUT_HIGH);
|
||||||
|
|
||||||
|
devices_update_inputs();
|
||||||
|
|
||||||
|
display.write(0x00);
|
||||||
|
display.set_brigthness(100);
|
||||||
|
|
||||||
|
ccout.update();
|
||||||
|
ccout.enable();
|
||||||
|
}
|
||||||
|
|
||||||
|
void devices_update_inputs(void)
|
||||||
|
{
|
||||||
|
board_read();
|
||||||
|
|
||||||
|
pot.update();
|
||||||
|
|
||||||
|
btn_mode.update();
|
||||||
|
btn_up.update();
|
||||||
|
btn_down.update();
|
||||||
|
|
||||||
|
sw_dimm.update();
|
||||||
|
sw_brakes.update();
|
||||||
|
sw_hbrake.update();
|
||||||
|
|
||||||
|
sup_fuse.update();
|
||||||
|
out_fuse.update();
|
||||||
|
|
||||||
|
display.process_timer();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
35
firmware/src/hw/devices.h
Normal file
35
firmware/src/hw/devices.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#ifndef HW_DEVICES_H_
|
||||||
|
#define HW_DEVICES_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "../bsp/board.h"
|
||||||
|
|
||||||
|
#include "button.h"
|
||||||
|
#include "potentiometer.h"
|
||||||
|
#include "display_led.h"
|
||||||
|
#include "cv_output.h"
|
||||||
|
#include "cc_output.h"
|
||||||
|
#include "fuse.h"
|
||||||
|
|
||||||
|
static hw::Button btn_mode = hw::Button(&din1, bsp::DIN_LOW, 10, hw::BUTTON_OFF);
|
||||||
|
static hw::Button btn_up = hw::Button(&din4, bsp::DIN_LOW, 10, hw::BUTTON_OFF);
|
||||||
|
static hw::Button btn_down = hw::Button(&din3, bsp::DIN_LOW, 10, hw::BUTTON_OFF);
|
||||||
|
|
||||||
|
static hw::Button sw_dimm = hw::Button(&hvdin1, bsp::DIN_HIGH, 10, hw::BUTTON_OFF);
|
||||||
|
static hw::Button sw_brakes = hw::Button(&hvdin2, bsp::DIN_HIGH, 10, hw::BUTTON_OFF);
|
||||||
|
static hw::Button sw_hbrake = hw::Button(&hvdin3, bsp::DIN_LOW, 10, hw::BUTTON_OFF);
|
||||||
|
|
||||||
|
static hw::Potentiometer pot = hw::Potentiometer(&ain2, 500, 4500);
|
||||||
|
|
||||||
|
static hw::DisplayLed display = hw::DisplayLed(&odout1, &odout2, &odout3, &odout4, &odout5, &odout6, &od_pwm);
|
||||||
|
|
||||||
|
static hw::CCoutput ccout = hw::CCoutput(&hbridge, &bat_u, &dccd_u, &dccd_i);
|
||||||
|
static hw::Fuse sup_fuse = hw::Fuse(&bat_i);
|
||||||
|
static hw::Fuse out_fuse = hw::Fuse(&dccd_i);
|
||||||
|
|
||||||
|
void devices_init(void);
|
||||||
|
void devices_update_inputs(void);
|
||||||
|
|
||||||
|
#endif /* BSP_BOARD_H_ */
|
||||||
195
firmware/src/hw/display_led.cpp
Normal file
195
firmware/src/hw/display_led.cpp
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "display_led.h"
|
||||||
|
|
||||||
|
using namespace hw;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
static uint8_t img_gen_dot10(uint8_t percent);
|
||||||
|
static uint8_t img_gen_dot20(uint8_t percent);
|
||||||
|
static uint8_t img_gen_bar(uint8_t percent);
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
hw::DisplayLed::DisplayLed(bsp::DigitalOut* led0, bsp::DigitalOut* led1, bsp::DigitalOut* led2, bsp::DigitalOut* led3, bsp::DigitalOut* led4, bsp::DigitalOut* led5, bsp::PWMout* common)
|
||||||
|
{
|
||||||
|
this->led0 = led0;
|
||||||
|
this->led1 = led1;
|
||||||
|
this->led2 = led2;
|
||||||
|
this->led3 = led3;
|
||||||
|
this->led4 = led4;
|
||||||
|
this->led5 = led5;
|
||||||
|
this->common = common;
|
||||||
|
|
||||||
|
this->led0->write(0);
|
||||||
|
this->led1->write(0);
|
||||||
|
this->led2->write(0);
|
||||||
|
this->led3->write(0);
|
||||||
|
this->led4->write(0);
|
||||||
|
this->led5->write(0);
|
||||||
|
this->common->write(0);
|
||||||
|
|
||||||
|
this->lock_counter = 0;
|
||||||
|
this->locked = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
hw::DisplayLed::~DisplayLed(void)
|
||||||
|
{
|
||||||
|
this->led0->write(0);
|
||||||
|
this->led1->write(0);
|
||||||
|
this->led2->write(0);
|
||||||
|
this->led3->write(0);
|
||||||
|
this->led4->write(0);
|
||||||
|
this->led5->write(0);
|
||||||
|
this->common->write(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw::DisplayLed::show_percent(uint8_t percent, style_t style)
|
||||||
|
{
|
||||||
|
uint8_t image = 0x00;
|
||||||
|
|
||||||
|
switch(style)
|
||||||
|
{
|
||||||
|
case LED_DSP_BAR:
|
||||||
|
image = img_gen_bar(percent);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case LED_DSP_DOT10:
|
||||||
|
image = img_gen_dot10(percent);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
image = img_gen_dot20(percent);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->write(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw::DisplayLed::write(uint8_t image)
|
||||||
|
{
|
||||||
|
if(image&0x01) this->led0->write(1);
|
||||||
|
else this->led0->write(0);
|
||||||
|
|
||||||
|
if(image&0x02) this->led1->write(1);
|
||||||
|
else this->led1->write(0);
|
||||||
|
|
||||||
|
if(image&0x04) this->led2->write(1);
|
||||||
|
else this->led2->write(0);
|
||||||
|
|
||||||
|
if(image&0x08) this->led3->write(1);
|
||||||
|
else this->led3->write(0);
|
||||||
|
|
||||||
|
if(image&0x10) this->led4->write(1);
|
||||||
|
else this->led4->write(0);
|
||||||
|
|
||||||
|
if(image&0x20) this->led5->write(1);
|
||||||
|
else this->led5->write(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw::DisplayLed::set_brigthness(uint8_t percent)
|
||||||
|
{
|
||||||
|
this->common->write(percent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw::DisplayLed::set_lock(uint16_t timeout)
|
||||||
|
{
|
||||||
|
this->lock_counter = timeout;
|
||||||
|
this->locked = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw::DisplayLed::process_timer(void)
|
||||||
|
{
|
||||||
|
if(this->lock_counter) this->lock_counter--;
|
||||||
|
else this->locked = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
|
static uint8_t img_gen_dot10(uint8_t percent)
|
||||||
|
{
|
||||||
|
switch(percent)
|
||||||
|
{
|
||||||
|
case 0 ... 5:
|
||||||
|
return 0x01;
|
||||||
|
|
||||||
|
case 6 ... 15:
|
||||||
|
return 0x03;
|
||||||
|
|
||||||
|
case 16 ... 25:
|
||||||
|
return 0x02;
|
||||||
|
|
||||||
|
case 26 ... 35:
|
||||||
|
return 0x06;
|
||||||
|
|
||||||
|
case 36 ... 45:
|
||||||
|
return 0x04;
|
||||||
|
|
||||||
|
case 46 ... 55:
|
||||||
|
return 0x0C;
|
||||||
|
|
||||||
|
case 56 ... 65:
|
||||||
|
return 0x08;
|
||||||
|
|
||||||
|
case 66 ... 75:
|
||||||
|
return 0x18;
|
||||||
|
|
||||||
|
case 76 ... 85:
|
||||||
|
return 0x10;
|
||||||
|
|
||||||
|
case 86 ... 95:
|
||||||
|
return 0x30;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 0x20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t img_gen_dot20(uint8_t percent)
|
||||||
|
{
|
||||||
|
switch(percent)
|
||||||
|
{
|
||||||
|
case 0 ... 10:
|
||||||
|
return 0x01;
|
||||||
|
|
||||||
|
case 11 ... 30:
|
||||||
|
return 0x02;
|
||||||
|
|
||||||
|
case 31 ... 50:
|
||||||
|
return 0x04;
|
||||||
|
|
||||||
|
case 51 ... 70:
|
||||||
|
return 0x08;
|
||||||
|
|
||||||
|
case 71 ... 90:
|
||||||
|
return 0x10;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 0x20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t img_gen_bar(uint8_t percent)
|
||||||
|
{
|
||||||
|
switch(percent)
|
||||||
|
{
|
||||||
|
case 0 ... 10:
|
||||||
|
return 0x01;
|
||||||
|
|
||||||
|
case 11 ... 30:
|
||||||
|
return 0x03;
|
||||||
|
|
||||||
|
case 31 ... 50:
|
||||||
|
return 0x07;
|
||||||
|
|
||||||
|
case 51 ... 70:
|
||||||
|
return 0x0F;
|
||||||
|
|
||||||
|
case 71 ... 90:
|
||||||
|
return 0x1F;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 0x3F;
|
||||||
|
}
|
||||||
|
}
|
||||||
54
firmware/src/hw/display_led.h
Normal file
54
firmware/src/hw/display_led.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#ifndef DISPLAY_LED_H_
|
||||||
|
#define DISPLAY_LED_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "../bsp/dout.h"
|
||||||
|
#include "../bsp/pwm.h"
|
||||||
|
|
||||||
|
namespace hw {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
|
||||||
|
class DisplayLed
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
bsp::DigitalOut* led0;
|
||||||
|
bsp::DigitalOut* led1;
|
||||||
|
bsp::DigitalOut* led2;
|
||||||
|
bsp::DigitalOut* led3;
|
||||||
|
bsp::DigitalOut* led4;
|
||||||
|
bsp::DigitalOut* led5;
|
||||||
|
bsp::PWMout* common;
|
||||||
|
|
||||||
|
uint16_t lock_counter;
|
||||||
|
|
||||||
|
public:
|
||||||
|
typedef enum {
|
||||||
|
LED_DSP_DOT20,
|
||||||
|
LED_DSP_DOT10,
|
||||||
|
LED_DSP_BAR
|
||||||
|
} style_t;
|
||||||
|
|
||||||
|
DisplayLed(bsp::DigitalOut* led0, bsp::DigitalOut* led1, bsp::DigitalOut* led2, bsp::DigitalOut* led3, bsp::DigitalOut* led4, bsp::DigitalOut* led5, bsp::PWMout* common);
|
||||||
|
~DisplayLed(void);
|
||||||
|
|
||||||
|
void show_percent(uint8_t percent, style_t style);
|
||||||
|
void write(uint8_t image);
|
||||||
|
|
||||||
|
void set_brigthness(uint8_t percent);
|
||||||
|
|
||||||
|
void set_lock(uint16_t timeout);
|
||||||
|
void process_timer(void);
|
||||||
|
|
||||||
|
uint8_t locked;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* DISPLAY_LED_H_ */
|
||||||
66
firmware/src/hw/fuse.cpp
Normal file
66
firmware/src/hw/fuse.cpp
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "fuse.h"
|
||||||
|
|
||||||
|
using namespace hw;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
hw::Fuse::Fuse(bsp::AnalogIn* ain_ch)
|
||||||
|
{
|
||||||
|
this->hold_current = 0;
|
||||||
|
this->trip_cycles = 0;
|
||||||
|
this->warning = 0;
|
||||||
|
this->fault = 0;
|
||||||
|
this->cooldown_counter = 0;
|
||||||
|
this->cooldown_cycles = 0;
|
||||||
|
this->retry_cnt = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
hw::Fuse::~Fuse(void)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw::Fuse::update(void)
|
||||||
|
{
|
||||||
|
// Under threshold
|
||||||
|
if(this->ain_ch->last_read <= this->hold_current)
|
||||||
|
{
|
||||||
|
// Clear warning flag
|
||||||
|
this->warning = 0;
|
||||||
|
|
||||||
|
// OC energy counter
|
||||||
|
if(this->oc_counter > 0) this->oc_counter--;
|
||||||
|
|
||||||
|
// Cool down fuse
|
||||||
|
if(this->cooldown_counter > 0) this->cooldown_counter--;
|
||||||
|
|
||||||
|
// Auto reset logic
|
||||||
|
if((this->fault)&&(this->cooldown_counter==0))
|
||||||
|
{
|
||||||
|
this->fault = 0;
|
||||||
|
this->retry_cnt = util::sat_add(this->retry_cnt, 1);
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Over current condition
|
||||||
|
this->warning = 1;
|
||||||
|
|
||||||
|
// PC energy counter
|
||||||
|
this->oc_counter = util::sat_add(this->oc_counter, 1);
|
||||||
|
|
||||||
|
// Check for trip threshold
|
||||||
|
if(this->oc_counter < this->trip_cycles) return;
|
||||||
|
|
||||||
|
// Trip fuse
|
||||||
|
this->fault = 1;
|
||||||
|
this->cooldown_counter = this->cooldown_cycles;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
40
firmware/src/hw/fuse.h
Normal file
40
firmware/src/hw/fuse.h
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#ifndef FUSE_H_
|
||||||
|
#define FUSE_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "../bsp/ain.h"
|
||||||
|
|
||||||
|
namespace hw {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
|
||||||
|
class Fuse
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
bsp::AnalogIn* ain_ch;
|
||||||
|
uint16_t oc_counter;
|
||||||
|
uint16_t cooldown_counter;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Fuse(bsp::AnalogIn* ain_ch);
|
||||||
|
~Fuse(void);
|
||||||
|
|
||||||
|
uint16_t hold_current;
|
||||||
|
uint16_t trip_cycles;
|
||||||
|
uint8_t warning;
|
||||||
|
uint8_t fault;
|
||||||
|
uint16_t cooldown_cycles;
|
||||||
|
uint8_t retry_cnt;
|
||||||
|
|
||||||
|
void update(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* POTENTIOMETER_H_ */
|
||||||
@@ -1,399 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "board/utils/utils.h"
|
|
||||||
#include "board/halfbridge.h"
|
|
||||||
#include "hb_control.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
// Fault condition thresholds
|
|
||||||
static const uint16_t SUPPLY_VOLT_MIN_W = HW_HB_SUPPLY_VOLT_MIN_W;
|
|
||||||
static const uint16_t SUPPLY_VOLT_MIN_F = HW_HB_SUPPLY_VOLT_MIN_F;
|
|
||||||
|
|
||||||
static const uint16_t SUPPLY_VOLT_MAX_W = HW_HB_SUPPLY_VOLT_MAX_W;
|
|
||||||
static const uint16_t SUPPLY_VOLT_MAX_F = HW_HB_SUPPLY_VOLT_MAX_F;
|
|
||||||
|
|
||||||
static const uint16_t SUPPLY_CURRENT_MAX_W = HW_HB_SUPPLY_CURRENT_MAX_W;
|
|
||||||
static const uint16_t SUPPLY_CURRENT_MAX_F = HW_HB_SUPPLY_CURRENT_MAX_F;
|
|
||||||
|
|
||||||
static const uint16_t SUPPLY_POWER_MAX_W = HW_HB_SUPPLY_POWER_MAX_W;
|
|
||||||
static const uint16_t SUPPLY_POWER_MAX_F = HW_HB_SUPPLY_POWER_MAX_F;
|
|
||||||
|
|
||||||
static const uint16_t OUT_CURRENT_MAX_W = HW_HB_OUT_CURRENT_MAX_W;
|
|
||||||
static const uint16_t OUT_CURRENT_MAX_F = HW_HB_OUT_CURRENT_MAX_F;
|
|
||||||
|
|
||||||
static const uint16_t OUT_VOLTAGE_MAX_W = HW_HB_OUT_VOLTAGE_MAX_W;
|
|
||||||
static const uint16_t OUT_VOLTAGE_MAX_F = HW_HB_OUT_VOLTAGE_MAX_F;
|
|
||||||
|
|
||||||
static const uint16_t OUT_POWER_MAX_W = HW_HB_OUT_POWER_MAX_W;
|
|
||||||
static const uint16_t OUT_POWER_MAX_F = HW_HB_OUT_POWER_MAX_F;
|
|
||||||
|
|
||||||
static const uint16_t OUT_RESISTANCE_MIN_W = HW_HB_OUT_RESISTANCE_MIN_W;
|
|
||||||
static const uint16_t OUT_RESISTANCE_MIN_F = HW_HB_OUT_RESISTANCE_MIN_F;
|
|
||||||
|
|
||||||
static const uint16_t OUT_RESISTANCE_MAX_W = HW_HB_OUT_RESISTANCE_MAX_W;
|
|
||||||
static const uint16_t OUT_RESISTANCE_MAX_F = HW_HB_OUT_RESISTANCE_MAX_F;
|
|
||||||
|
|
||||||
static const uint16_t FAULT_DELAY = HW_HB_FAULT_DELAY;
|
|
||||||
static const uint16_t WTOF_DELAY = HW_HB_WTOF_DELAY;
|
|
||||||
|
|
||||||
/**** Private variables ****/
|
|
||||||
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
// Protection functions
|
|
||||||
static void faults_chs_reset(hb_fault_chs_t* ch_list);
|
|
||||||
static void faults_init(hb_faults_t* faults, hb_fault_chs_t* en_list);
|
|
||||||
static void faults_logic(hb_meas_t* measurements, hb_faults_t* faults);
|
|
||||||
|
|
||||||
static uint8_t faults_check(hb_faults_t* faults, hb_fault_chs_t* en_list, hb_fault_chs_t* fb_list, fault_lvl_t check);
|
|
||||||
static uint8_t warnings_feedback(hb_faults_t* faults, hb_fault_chs_t* en_list, hb_fault_chs_t* fb_list);
|
|
||||||
static uint8_t faults_feedback(hb_faults_t* faults, hb_fault_chs_t* en_list, hb_fault_chs_t* fb_list);
|
|
||||||
static uint8_t faults_is_any(hb_fault_chs_t* fb_list);
|
|
||||||
|
|
||||||
static uint8_t is_outside(uint16_t meas, uint16_t min, uint16_t max);
|
|
||||||
|
|
||||||
static uint8_t fuse_subprocess(hb_feedback_t* hb_fb, hb_control_t* hb_ctrl);
|
|
||||||
static void target_to_control(int16_t target, uint16_t ref_voltage, uint16_t* pwm_out, uint8_t* low_out, hb_fault_chs_t* fault_ch_en);
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
void hb_init(hb_feedback_t* hb_fb, hb_control_t* hb_ctrl)
|
|
||||||
{
|
|
||||||
// Initialize control
|
|
||||||
hb_ctrl->enabled = 0;
|
|
||||||
|
|
||||||
// Initialize protection structures
|
|
||||||
faults_init(&hb_ctrl->out_faults, &hb_ctrl->out_faults_en);
|
|
||||||
|
|
||||||
fuse_reset(&hb_ctrl->out_fuse);
|
|
||||||
hb_ctrl->out_fuse_cfg.cooldown_time = 1000;
|
|
||||||
hb_ctrl->out_fuse_cfg.retry_time = 1000;
|
|
||||||
|
|
||||||
// Initialize feedback structure
|
|
||||||
bsp_hb_read_meas(&hb_fb->analog);
|
|
||||||
faults_chs_reset(&hb_fb->faults);
|
|
||||||
faults_chs_reset(&hb_fb->warnings);
|
|
||||||
|
|
||||||
hb_fb->enabled = 0;
|
|
||||||
hb_fb->warning_act = 0;
|
|
||||||
hb_fb->fault_act = 0;
|
|
||||||
hb_fb->fused = 0;
|
|
||||||
|
|
||||||
bsp_hb_write_pwm(0);
|
|
||||||
bsp_hb_write_low(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void hb_enable(hb_control_t* hb_ctrl)
|
|
||||||
{
|
|
||||||
bsp_hb_write_pwm(0);
|
|
||||||
bsp_hb_write_low(1);
|
|
||||||
hb_ctrl->enabled = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void hb_disable(hb_control_t* hb_ctrl)
|
|
||||||
{
|
|
||||||
hb_ctrl->enabled = 0;
|
|
||||||
bsp_hb_write_pwm(0);
|
|
||||||
bsp_hb_write_low(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void hb_process(int16_t target, hb_feedback_t* hb_fb, hb_control_t* hb_ctrl)
|
|
||||||
{
|
|
||||||
uint16_t out_pwm = 0;
|
|
||||||
uint8_t low_on = 0;
|
|
||||||
|
|
||||||
// Read feedback
|
|
||||||
bsp_hb_read_meas(&hb_fb->analog);
|
|
||||||
|
|
||||||
// Process fuse
|
|
||||||
uint8_t fuse_act = fuse_subprocess(hb_fb, hb_ctrl);
|
|
||||||
|
|
||||||
// Act on fuse state
|
|
||||||
if((fuse_act)||(!hb_ctrl->enabled))
|
|
||||||
{
|
|
||||||
// Turn off output
|
|
||||||
bsp_hb_write_pwm(0);
|
|
||||||
bsp_hb_write_low(0);
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Process new target
|
|
||||||
target_to_control(target, hb_fb->analog.sup_voltage, &out_pwm, &low_on, &hb_ctrl->out_faults_en);
|
|
||||||
|
|
||||||
// Apply new controls
|
|
||||||
bsp_hb_write_low(low_on);
|
|
||||||
bsp_hb_write_pwm(out_pwm);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
static void target_to_control(int16_t target, uint16_t ref_voltage, uint16_t* pwm_out, uint8_t* low_out, hb_fault_chs_t* fault_ch_en)
|
|
||||||
{
|
|
||||||
if(target < 0)
|
|
||||||
{
|
|
||||||
// Fast decay
|
|
||||||
*pwm_out = 0;
|
|
||||||
*low_out = 0;
|
|
||||||
// Set appropriate fault channels
|
|
||||||
fault_ch_en->out_short = 0;
|
|
||||||
fault_ch_en->out_ovp = 0;
|
|
||||||
}
|
|
||||||
else if(target == 0)
|
|
||||||
{
|
|
||||||
// Slow decay
|
|
||||||
*pwm_out = 0;
|
|
||||||
*low_out = 1;
|
|
||||||
// Set appropriate fault channels
|
|
||||||
fault_ch_en->out_short = 0;
|
|
||||||
fault_ch_en->out_ovp = 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Calculate target PWM
|
|
||||||
*pwm_out = util_sat_ratio_16b((uint16_t)target, ref_voltage);
|
|
||||||
*low_out = 1;
|
|
||||||
fault_ch_en->out_short = 1;
|
|
||||||
fault_ch_en->out_ovp = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t fuse_subprocess(hb_feedback_t* hb_fb, hb_control_t* hb_ctrl)
|
|
||||||
{
|
|
||||||
// Process faults
|
|
||||||
faults_logic(&hb_fb->analog, &hb_ctrl->out_faults);
|
|
||||||
|
|
||||||
// Check if any enabled fault is active
|
|
||||||
uint8_t warn_act = warnings_feedback(&hb_ctrl->out_faults, &hb_ctrl->out_faults_en, &hb_fb->warnings);
|
|
||||||
uint8_t fault_act = faults_feedback(&hb_ctrl->out_faults, &hb_ctrl->out_faults_en, &hb_fb->faults);
|
|
||||||
|
|
||||||
// Process fuse state
|
|
||||||
uint8_t fuse_act = fuse_process(&hb_ctrl->out_fuse, fault_act, &hb_ctrl->out_fuse_cfg);
|
|
||||||
|
|
||||||
// Copy feedback data
|
|
||||||
hb_fb->enabled = hb_ctrl->enabled;
|
|
||||||
hb_fb->warning_act = warn_act;
|
|
||||||
hb_fb->fault_act = fault_act;
|
|
||||||
hb_fb->fused = fuse_act;
|
|
||||||
|
|
||||||
return fuse_act;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fault logic functions
|
|
||||||
static void faults_chs_reset(hb_fault_chs_t* ch_list)
|
|
||||||
{
|
|
||||||
// Zero all channels
|
|
||||||
ch_list->sup_uvp = 0;
|
|
||||||
ch_list->sup_ovp = 0;
|
|
||||||
ch_list->sup_ocp = 0;
|
|
||||||
ch_list->sup_opp = 0;
|
|
||||||
ch_list->out_ovp = 0;
|
|
||||||
ch_list->out_ocp = 0;
|
|
||||||
ch_list->out_opp = 0;
|
|
||||||
ch_list->out_short = 0;
|
|
||||||
ch_list->out_open = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void faults_init(hb_faults_t* faults, hb_fault_chs_t* en_list)
|
|
||||||
{
|
|
||||||
// Init state
|
|
||||||
fault_reset(&faults->sup_uvp);
|
|
||||||
fault_reset(&faults->sup_ovp);
|
|
||||||
fault_reset(&faults->sup_ocp);
|
|
||||||
fault_reset(&faults->sup_opp);
|
|
||||||
fault_reset(&faults->out_ovp);
|
|
||||||
fault_reset(&faults->out_ocp);
|
|
||||||
fault_reset(&faults->out_opp);
|
|
||||||
fault_reset(&faults->out_short);
|
|
||||||
fault_reset(&faults->out_open);
|
|
||||||
|
|
||||||
// Init enabled channels
|
|
||||||
faults_chs_reset(en_list);
|
|
||||||
en_list->sup_ocp = 1;
|
|
||||||
en_list->out_ocp = 1;
|
|
||||||
en_list->out_short = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void faults_logic(hb_meas_t* measurements, hb_faults_t* faults)
|
|
||||||
{
|
|
||||||
uint8_t w_trig = 0;
|
|
||||||
uint8_t f_trig = 0;
|
|
||||||
|
|
||||||
fault_cfg_t fault_cfg;
|
|
||||||
fault_cfg.delay = FAULT_DELAY;
|
|
||||||
fault_cfg.wtof = WTOF_DELAY;
|
|
||||||
|
|
||||||
// Check supply voltage
|
|
||||||
w_trig = is_outside(measurements->sup_voltage, SUPPLY_VOLT_MIN_W, 0);
|
|
||||||
f_trig = is_outside(measurements->sup_voltage, SUPPLY_VOLT_MIN_F, 0);
|
|
||||||
fault_process(&faults->sup_uvp, w_trig, f_trig, &fault_cfg);
|
|
||||||
|
|
||||||
w_trig = is_outside(measurements->sup_voltage, 0, SUPPLY_VOLT_MAX_W);
|
|
||||||
f_trig = is_outside(measurements->sup_voltage, 0, SUPPLY_VOLT_MAX_F);
|
|
||||||
fault_process(&faults->sup_ovp, w_trig, f_trig, &fault_cfg);
|
|
||||||
|
|
||||||
// Check supply current
|
|
||||||
w_trig = is_outside(measurements->sup_current, 0, SUPPLY_CURRENT_MAX_W);
|
|
||||||
f_trig = is_outside(measurements->sup_current, 0, SUPPLY_CURRENT_MAX_F);
|
|
||||||
fault_process(&faults->sup_ocp, w_trig, f_trig, &fault_cfg);
|
|
||||||
|
|
||||||
// Check supply power
|
|
||||||
w_trig = is_outside(measurements->sup_power, 0, SUPPLY_POWER_MAX_W);
|
|
||||||
f_trig = is_outside(measurements->sup_power, 0, SUPPLY_POWER_MAX_F);
|
|
||||||
fault_process(&faults->sup_opp, w_trig, f_trig, &fault_cfg);
|
|
||||||
|
|
||||||
// Check output voltage
|
|
||||||
w_trig = is_outside(measurements->out_voltage, 0, OUT_VOLTAGE_MAX_W);
|
|
||||||
f_trig = is_outside(measurements->out_voltage, 0, OUT_VOLTAGE_MAX_F);
|
|
||||||
fault_process(&faults->out_ovp, w_trig, f_trig, &fault_cfg);
|
|
||||||
|
|
||||||
// Check output current
|
|
||||||
w_trig = is_outside(measurements->out_current, 0, OUT_CURRENT_MAX_W);
|
|
||||||
f_trig = is_outside(measurements->out_current, 0, OUT_CURRENT_MAX_F);
|
|
||||||
fault_process(&faults->out_ocp, w_trig, f_trig, &fault_cfg);
|
|
||||||
|
|
||||||
// Check output power
|
|
||||||
w_trig = is_outside(measurements->out_power, 0, OUT_POWER_MAX_W);
|
|
||||||
f_trig = is_outside(measurements->out_power, 0, OUT_POWER_MAX_F);
|
|
||||||
fault_process(&faults->out_opp, w_trig, f_trig, &fault_cfg);
|
|
||||||
|
|
||||||
// Check output resistance
|
|
||||||
w_trig = is_outside(measurements->out_impedance, OUT_RESISTANCE_MIN_W, 0);
|
|
||||||
f_trig = is_outside(measurements->out_impedance, OUT_RESISTANCE_MIN_F, 0);
|
|
||||||
fault_process(&faults->out_short, w_trig, f_trig, &fault_cfg);
|
|
||||||
|
|
||||||
w_trig = is_outside(measurements->out_impedance, 0, OUT_RESISTANCE_MAX_W);
|
|
||||||
f_trig = is_outside(measurements->out_impedance, 0, OUT_RESISTANCE_MAX_F);
|
|
||||||
fault_process(&faults->out_open, w_trig, f_trig, &fault_cfg);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t warnings_feedback(hb_faults_t* faults, hb_fault_chs_t* en_list, hb_fault_chs_t* fb_list)
|
|
||||||
{
|
|
||||||
return faults_check(faults, en_list, fb_list, FAULT_LVL_WARNING);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t faults_feedback(hb_faults_t* faults, hb_fault_chs_t* en_list, hb_fault_chs_t* fb_list)
|
|
||||||
{
|
|
||||||
return faults_check(faults, en_list, fb_list, FAULT_LVL_FAULT);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Utilities
|
|
||||||
static uint8_t is_outside(uint16_t meas, uint16_t min, uint16_t max)
|
|
||||||
{
|
|
||||||
if((meas < min)||((max!=0)&&(meas > max))) return 1;
|
|
||||||
else return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t faults_check(hb_faults_t* faults, hb_fault_chs_t* en_list, hb_fault_chs_t* fb_list, fault_lvl_t check)
|
|
||||||
{
|
|
||||||
if((en_list->sup_uvp)&&(faults->sup_uvp.severity == check)) fb_list->sup_uvp = 1;
|
|
||||||
if((en_list->sup_ovp)&&(faults->sup_ovp.severity == check)) fb_list->sup_ovp = 1;
|
|
||||||
if((en_list->sup_ocp)&&(faults->sup_ocp.severity == check)) fb_list->sup_ocp = 1;
|
|
||||||
if((en_list->sup_opp)&&(faults->sup_opp.severity == check)) fb_list->sup_opp = 1;
|
|
||||||
if((en_list->out_ovp)&&(faults->out_ovp.severity == check)) fb_list->out_ovp = 1;
|
|
||||||
if((en_list->out_ocp)&&(faults->out_ocp.severity == check)) fb_list->out_ocp = 1;
|
|
||||||
if((en_list->out_opp)&&(faults->out_opp.severity == check)) fb_list->out_opp = 1;
|
|
||||||
if((en_list->out_short)&&(faults->out_short.severity == check)) fb_list->out_short = 1;
|
|
||||||
if((en_list->out_open)&&(faults->out_open.severity == check)) fb_list->out_open = 1;
|
|
||||||
|
|
||||||
return faults_is_any(fb_list);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t faults_is_any(hb_fault_chs_t* fb_list)
|
|
||||||
{
|
|
||||||
if(fb_list->sup_uvp) return 1;
|
|
||||||
if(fb_list->sup_ovp) return 1;
|
|
||||||
if(fb_list->sup_ocp) return 1;
|
|
||||||
if(fb_list->sup_opp) return 1;
|
|
||||||
if(fb_list->out_ovp) return 1;
|
|
||||||
if(fb_list->out_ocp) return 1;
|
|
||||||
if(fb_list->out_opp) return 1;
|
|
||||||
if(fb_list->out_short) return 1;
|
|
||||||
if(fb_list->out_open) return 1;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
uint8_t hb_is_equal_fb_struct(hb_feedback_t* f1, hb_feedback_t* f2)
|
|
||||||
{
|
|
||||||
if(f1->analog.out_voltage != f2->analog.out_voltage ) return 0;
|
|
||||||
if(f1->analog.out_current != f2->analog.out_current ) return 0;
|
|
||||||
if(f1->analog.sup_voltage != f2->analog.sup_voltage ) return 0;
|
|
||||||
if(f1->analog.sup_current != f2->analog.sup_current ) return 0;
|
|
||||||
if(f1->analog.out_power != f2->analog.out_power ) return 0;
|
|
||||||
if(f1->analog.sup_power != f2->analog.sup_power ) return 0;
|
|
||||||
if(f1->analog.out_impedance != f2->analog.out_impedance ) return 0;
|
|
||||||
if(f1->analog.low_side_ctrl != f2->analog.low_side_ctrl ) return 0;
|
|
||||||
if(f1->analog.pwm != f2->analog.pwm ) return 0;
|
|
||||||
if(f1->enabled != f2->enabled ) return 0;
|
|
||||||
if(f1->warning_act != f2->warning_act ) return 0;
|
|
||||||
if(f1->fault_act != f2->fault_act ) return 0;
|
|
||||||
if(f1->fused != f2->fused ) return 0;
|
|
||||||
if(f1->warnings.sup_uvp != f2->warnings.sup_uvp ) return 0;
|
|
||||||
if(f1->warnings.sup_ovp != f2->warnings.sup_ovp ) return 0;
|
|
||||||
if(f1->warnings.sup_ocp != f2->warnings.sup_ocp ) return 0;
|
|
||||||
if(f1->warnings.sup_opp != f2->warnings.sup_opp ) return 0;
|
|
||||||
if(f1->warnings.out_ovp != f2->warnings.out_ovp ) return 0;
|
|
||||||
if(f1->warnings.out_ocp != f2->warnings.out_ocp ) return 0;
|
|
||||||
if(f1->warnings.out_opp != f2->warnings.out_opp ) return 0;
|
|
||||||
if(f1->warnings.out_short != f2->warnings.out_short ) return 0;
|
|
||||||
if(f1->warnings.out_open != f2->warnings.out_open ) return 0;
|
|
||||||
if(f1->faults.sup_uvp != f2->faults.sup_uvp ) return 0;
|
|
||||||
if(f1->faults.sup_ovp != f2->faults.sup_ovp ) return 0;
|
|
||||||
if(f1->faults.sup_ocp != f2->faults.sup_ocp ) return 0;
|
|
||||||
if(f1->faults.sup_opp != f2->faults.sup_opp ) return 0;
|
|
||||||
if(f1->faults.out_ovp != f2->faults.out_ovp ) return 0;
|
|
||||||
if(f1->faults.out_ocp != f2->faults.out_ocp ) return 0;
|
|
||||||
if(f1->faults.out_opp != f2->faults.out_opp ) return 0;
|
|
||||||
if(f1->faults.out_short != f2->faults.out_short ) return 0;
|
|
||||||
if(f1->faults.out_open != f2->faults.out_open ) return 0;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t hb_is_equal_ctrl_struct(hb_control_t* c1, hb_control_t* c2)
|
|
||||||
{
|
|
||||||
if(c1->enabled != c2->enabled ) return 0;
|
|
||||||
if(c1->out_faults.sup_uvp.severity != c2->out_faults.sup_uvp.severity ) return 0;
|
|
||||||
if(c1->out_faults.sup_uvp.w_time != c2->out_faults.sup_uvp.w_time ) return 0;
|
|
||||||
if(c1->out_faults.sup_uvp.f_time != c2->out_faults.sup_uvp.f_time ) return 0;
|
|
||||||
if(c1->out_faults.sup_ovp.severity != c2->out_faults.sup_ovp.severity ) return 0;
|
|
||||||
if(c1->out_faults.sup_ovp.w_time != c2->out_faults.sup_ovp.w_time ) return 0;
|
|
||||||
if(c1->out_faults.sup_ovp.f_time != c2->out_faults.sup_ovp.f_time ) return 0;
|
|
||||||
if(c1->out_faults.sup_ocp.severity != c2->out_faults.sup_ocp.severity ) return 0;
|
|
||||||
if(c1->out_faults.sup_ocp.w_time != c2->out_faults.sup_ocp.w_time ) return 0;
|
|
||||||
if(c1->out_faults.sup_ocp.f_time != c2->out_faults.sup_ocp.f_time ) return 0;
|
|
||||||
if(c1->out_faults.sup_opp.severity != c2->out_faults.sup_opp.severity ) return 0;
|
|
||||||
if(c1->out_faults.sup_opp.w_time != c2->out_faults.sup_opp.w_time ) return 0;
|
|
||||||
if(c1->out_faults.sup_opp.f_time != c2->out_faults.sup_opp.f_time ) return 0;
|
|
||||||
if(c1->out_faults.out_ovp.severity != c2->out_faults.out_ovp.severity ) return 0;
|
|
||||||
if(c1->out_faults.out_ovp.w_time != c2->out_faults.out_ovp.w_time ) return 0;
|
|
||||||
if(c1->out_faults.out_ovp.f_time != c2->out_faults.out_ovp.f_time ) return 0;
|
|
||||||
if(c1->out_faults.out_ocp.severity != c2->out_faults.out_ocp.severity ) return 0;
|
|
||||||
if(c1->out_faults.out_ocp.w_time != c2->out_faults.out_ocp.w_time ) return 0;
|
|
||||||
if(c1->out_faults.out_ocp.f_time != c2->out_faults.out_ocp.f_time ) return 0;
|
|
||||||
if(c1->out_faults.out_opp.severity != c2->out_faults.out_opp.severity ) return 0;
|
|
||||||
if(c1->out_faults.out_opp.w_time != c2->out_faults.out_opp.w_time ) return 0;
|
|
||||||
if(c1->out_faults.out_opp.f_time != c2->out_faults.out_opp.f_time ) return 0;
|
|
||||||
if(c1->out_faults.out_short.severity != c2->out_faults.out_short.severity ) return 0;
|
|
||||||
if(c1->out_faults.out_short.w_time != c2->out_faults.out_short.w_time ) return 0;
|
|
||||||
if(c1->out_faults.out_short.f_time != c2->out_faults.out_short.f_time ) return 0;
|
|
||||||
if(c1->out_faults.out_open.severity != c2->out_faults.out_open.severity ) return 0;
|
|
||||||
if(c1->out_faults.out_open.w_time != c2->out_faults.out_open.w_time ) return 0;
|
|
||||||
if(c1->out_faults.out_open.f_time != c2->out_faults.out_open.f_time ) return 0;
|
|
||||||
if(c1->out_faults_en.sup_uvp != c2->out_faults_en.sup_uvp ) return 0;
|
|
||||||
if(c1->out_faults_en.sup_ovp != c2->out_faults_en.sup_ovp ) return 0;
|
|
||||||
if(c1->out_faults_en.sup_ocp != c2->out_faults_en.sup_ocp ) return 0;
|
|
||||||
if(c1->out_faults_en.sup_opp != c2->out_faults_en.sup_opp ) return 0;
|
|
||||||
if(c1->out_faults_en.out_ovp != c2->out_faults_en.out_ovp ) return 0;
|
|
||||||
if(c1->out_faults_en.out_ocp != c2->out_faults_en.out_ocp ) return 0;
|
|
||||||
if(c1->out_faults_en.out_opp != c2->out_faults_en.out_opp ) return 0;
|
|
||||||
if(c1->out_faults_en.out_short != c2->out_faults_en.out_short ) return 0;
|
|
||||||
if(c1->out_faults_en.out_open != c2->out_faults_en.out_open ) return 0;
|
|
||||||
if(c1->out_fuse.state != c2->out_fuse.state ) return 0;
|
|
||||||
if(c1->out_fuse.count != c2->out_fuse.count ) return 0;
|
|
||||||
if(c1->out_fuse.timer != c2->out_fuse.timer ) return 0;
|
|
||||||
if(c1->out_fuse_cfg.cooldown_time != c2->out_fuse_cfg.cooldown_time ) return 0;
|
|
||||||
if(c1->out_fuse_cfg.retry_time != c2->out_fuse_cfg.retry_time ) return 0;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
#ifndef HB_CONTROL_H_
|
|
||||||
#define HB_CONTROL_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
#include "board/utils/faults.h"
|
|
||||||
#include "board/utils/fuses.h"
|
|
||||||
#include "board/halfbridge.h"
|
|
||||||
#include "config.h"
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
typedef struct {
|
|
||||||
fault_t sup_uvp;
|
|
||||||
fault_t sup_ovp;
|
|
||||||
fault_t sup_ocp;
|
|
||||||
fault_t sup_opp;
|
|
||||||
fault_t out_ovp;
|
|
||||||
fault_t out_ocp;
|
|
||||||
fault_t out_opp;
|
|
||||||
fault_t out_short;
|
|
||||||
fault_t out_open;
|
|
||||||
} hb_faults_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t sup_uvp;
|
|
||||||
uint8_t sup_ovp;
|
|
||||||
uint8_t sup_ocp;
|
|
||||||
uint8_t sup_opp;
|
|
||||||
uint8_t out_ovp;
|
|
||||||
uint8_t out_ocp;
|
|
||||||
uint8_t out_opp;
|
|
||||||
uint8_t out_short;
|
|
||||||
uint8_t out_open;
|
|
||||||
} hb_fault_chs_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
hb_meas_t analog;
|
|
||||||
uint8_t enabled;
|
|
||||||
uint8_t warning_act;
|
|
||||||
uint8_t fault_act;
|
|
||||||
uint8_t fused;
|
|
||||||
hb_fault_chs_t warnings;
|
|
||||||
hb_fault_chs_t faults;
|
|
||||||
} hb_feedback_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t enabled;
|
|
||||||
hb_faults_t out_faults;
|
|
||||||
hb_fault_chs_t out_faults_en;
|
|
||||||
fuse_t out_fuse;
|
|
||||||
fuse_cfg_t out_fuse_cfg;
|
|
||||||
} hb_control_t;
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void hb_init(hb_feedback_t* hb_fb, hb_control_t* hb_ctrl);
|
|
||||||
|
|
||||||
void hb_enable(hb_control_t* hb_ctrl);
|
|
||||||
void hb_disable(hb_control_t* hb_ctrl);
|
|
||||||
|
|
||||||
void hb_process(int16_t target, hb_feedback_t* hb_fb, hb_control_t* hb_ctrl);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
uint8_t hb_is_equal_fb_struct(hb_feedback_t* f1, hb_feedback_t* f2);
|
|
||||||
uint8_t hb_is_equal_ctrl_struct(hb_control_t* c1, hb_control_t* c2);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* HB_CONTROL_H_ */
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "board/odout.h"
|
|
||||||
#include "led_display.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
void led_dsp_set_image(uint8_t image)
|
|
||||||
{
|
|
||||||
if(image&0x01) bsp_odout_write(BSP_OD1, BSP_ODOUT_LOW);
|
|
||||||
else bsp_odout_write(BSP_OD1, BSP_ODOUT_HIZ);
|
|
||||||
|
|
||||||
if(image&0x02) bsp_odout_write(BSP_OD2, BSP_ODOUT_LOW);
|
|
||||||
else bsp_odout_write(BSP_OD2, BSP_ODOUT_HIZ);
|
|
||||||
|
|
||||||
if(image&0x04) bsp_odout_write(BSP_OD3, BSP_ODOUT_LOW);
|
|
||||||
else bsp_odout_write(BSP_OD3, BSP_ODOUT_HIZ);
|
|
||||||
|
|
||||||
if(image&0x08) bsp_odout_write(BSP_OD4, BSP_ODOUT_LOW);
|
|
||||||
else bsp_odout_write(BSP_OD4, BSP_ODOUT_HIZ);
|
|
||||||
|
|
||||||
if(image&0x10) bsp_odout_write(BSP_OD5, BSP_ODOUT_LOW);
|
|
||||||
else bsp_odout_write(BSP_OD5, BSP_ODOUT_HIZ);
|
|
||||||
|
|
||||||
if(image&0x20) bsp_odout_write(BSP_OD6, BSP_ODOUT_LOW);
|
|
||||||
else bsp_odout_write(BSP_OD6, BSP_ODOUT_HIZ);
|
|
||||||
}
|
|
||||||
|
|
||||||
void led_dsp_backligth_set(uint8_t percent)
|
|
||||||
{
|
|
||||||
bsp_odout_write_common(percent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
#ifndef LED_DISPLAY_H_
|
|
||||||
#define LED_DISPLAY_H_
|
|
||||||
|
|
||||||
/*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void led_dsp_set_image(uint8_t image);
|
|
||||||
void led_dsp_backligth_set(uint8_t percent);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* LED_DISPLAY_H_ */
|
|
||||||
37
firmware/src/hw/potentiometer.cpp
Normal file
37
firmware/src/hw/potentiometer.cpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "../utils/interpolate.h"
|
||||||
|
#include "potentiometer.h"
|
||||||
|
|
||||||
|
using namespace hw;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
hw::Potentiometer::Potentiometer(bsp::AnalogIn* ain_ch, uint16_t low_deadzone, uint16_t high_deadzone)
|
||||||
|
{
|
||||||
|
this->ain_ch = ain_ch;
|
||||||
|
this->low_deadzone = low_deadzone;
|
||||||
|
this->high_deadzone = high_deadzone;
|
||||||
|
this->percent = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
hw::Potentiometer::~Potentiometer(void)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t hw::Potentiometer::update(void)
|
||||||
|
{
|
||||||
|
// Calculate percent
|
||||||
|
if(this->ain_ch->last_read <= this->low_deadzone) this->percent = 0;
|
||||||
|
else if(this->ain_ch->last_read >= this->high_deadzone ) this->percent = 100;
|
||||||
|
else this->percent = util::interpolate(this->ain_ch->last_read, this->low_deadzone, this->high_deadzone, 0, 100);
|
||||||
|
|
||||||
|
return this->percent;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
35
firmware/src/hw/potentiometer.h
Normal file
35
firmware/src/hw/potentiometer.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#ifndef POTENTIOMETER_H_
|
||||||
|
#define POTENTIOMETER_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "../bsp/ain.h"
|
||||||
|
|
||||||
|
namespace hw {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
|
||||||
|
class Potentiometer
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
bsp::AnalogIn* ain_ch;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Potentiometer(bsp::AnalogIn* ain_ch, uint16_t low_deadzone, uint16_t high_deadzone);
|
||||||
|
~Potentiometer(void);
|
||||||
|
|
||||||
|
uint16_t low_deadzone;
|
||||||
|
uint16_t high_deadzone;
|
||||||
|
uint8_t percent;
|
||||||
|
|
||||||
|
uint8_t update(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* POTENTIOMETER_H_ */
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "board/utils/utils.h"
|
|
||||||
#include "board/setup.h"
|
|
||||||
#include "startup.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
void hw_startup(void)
|
|
||||||
{
|
|
||||||
bsp_startup();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#ifndef HW_STARTUP_H_
|
|
||||||
#define HW_STARTUP_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void hw_startup(void);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* HW_STARTUP_H_ */
|
|
||||||
51
firmware/src/logic/button_force.cpp
Normal file
51
firmware/src/logic/button_force.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "button_force.h"
|
||||||
|
|
||||||
|
using namespace logic;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
logic::ButtonForce::ButtonForce(hw::Button* btn_up, hw::Button* btn_down)
|
||||||
|
{
|
||||||
|
this->btn_up = btn_up;
|
||||||
|
this->btn_down = btn_down;
|
||||||
|
this->force = 0;
|
||||||
|
this->step = 10;
|
||||||
|
this->is_new = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
logic::ButtonForce::~ButtonForce(void)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t logic::ButtonForce::update(void)
|
||||||
|
{
|
||||||
|
uint8_t next_force = 0;
|
||||||
|
|
||||||
|
if((this->btn_up->is_new)&&(this->btn_up->state == hw::BUTTON_ON))
|
||||||
|
{
|
||||||
|
next_force = util::sat_add(this->force, this->step);
|
||||||
|
if(next_force > 100) next_force = 100;
|
||||||
|
this->btn_up->is_new = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
if((this->btn_down->is_new)&&(this->btn_down->state == hw::BUTTON_ON))
|
||||||
|
{
|
||||||
|
next_force = util::sat_subtract(this->force, this->step);
|
||||||
|
this->btn_down->is_new = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
if(next_force != this->force) this->is_new = 1;
|
||||||
|
|
||||||
|
this->force = next_force;
|
||||||
|
|
||||||
|
return this->force;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
36
firmware/src/logic/button_force.h
Normal file
36
firmware/src/logic/button_force.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef BUTTON_FORCE_H_
|
||||||
|
#define BUTTON_FORCE_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "../hw/button.h"
|
||||||
|
|
||||||
|
namespace logic {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
|
||||||
|
class ButtonForce
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
hw::Button* btn_up;
|
||||||
|
hw::Button* btn_down;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ButtonForce(hw::Button* btn_up, hw::Button* btn_down);
|
||||||
|
~ButtonForce(void);
|
||||||
|
|
||||||
|
uint8_t force;
|
||||||
|
uint8_t step;
|
||||||
|
uint8_t is_new;
|
||||||
|
|
||||||
|
uint8_t update(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* BUTTON_FORCE_H_ */
|
||||||
185
firmware/src/logic/cfg_mem.cpp
Normal file
185
firmware/src/logic/cfg_mem.cpp
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "cfg_mem.h"
|
||||||
|
#include "../bsp/mcu/mcu_hal.h"
|
||||||
|
|
||||||
|
using namespace logic;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
static const uint16_t addr_btn_force = 0x0000;
|
||||||
|
static const uint16_t addr_bmode = 0x0001;
|
||||||
|
static const uint16_t addr_pot_mode = 0x0002;
|
||||||
|
static const uint16_t addr_dsp_brigth = 0x0003;
|
||||||
|
static const uint16_t addr_dsp_dimm = 0x0004;
|
||||||
|
static const uint16_t addr_brake_force = 0x0005;
|
||||||
|
static const uint16_t addr_max_hbrake_time = 0x0006;
|
||||||
|
static const uint16_t addr_lock_current = 0x0008;
|
||||||
|
|
||||||
|
static const uint8_t def_btn_force = 0;
|
||||||
|
static const uint8_t def_pot_mode = 0;
|
||||||
|
static const uint8_t def_bmode = 0;
|
||||||
|
static const uint8_t def_dimm = 50;
|
||||||
|
static const uint8_t def_brigth = 100;
|
||||||
|
static const uint8_t def_brake_force = 100;
|
||||||
|
static const uint16_t def_max_hbrake_time = 1000;
|
||||||
|
static const uint16_t def_lock_current = 4500;
|
||||||
|
|
||||||
|
static const uint16_t max_lock_current = 6000;
|
||||||
|
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
logic::CfgMemory::CfgMemory(void)
|
||||||
|
{
|
||||||
|
this->mem_btn_force = 0;
|
||||||
|
this->mem_bmode = 0;
|
||||||
|
this->mem_pot_mode = 0;
|
||||||
|
this->mem_dsp_brigth = 0;
|
||||||
|
this->mem_dsp_dimm = 0;
|
||||||
|
this->mem_brake_force = 0;
|
||||||
|
this->mem_max_hbrake_time = 0;
|
||||||
|
this->mem_lock_current = 0;
|
||||||
|
|
||||||
|
this->restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
logic::CfgMemory::~CfgMemory(void)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void logic::CfgMemory::init(void)
|
||||||
|
{
|
||||||
|
this->mem_btn_force = mcu::eeprom_read8b(addr_btn_force);
|
||||||
|
this->mem_bmode = mcu::eeprom_read8b(addr_bmode);
|
||||||
|
this->mem_pot_mode = mcu::eeprom_read8b(addr_pot_mode);
|
||||||
|
this->mem_dsp_brigth = mcu::eeprom_read8b(addr_dsp_brigth);
|
||||||
|
this->mem_dsp_dimm = mcu::eeprom_read8b(addr_dsp_dimm);
|
||||||
|
this->mem_brake_force = mcu::eeprom_read8b(addr_brake_force);
|
||||||
|
this->mem_max_hbrake_time = mcu::eeprom_read16b(addr_max_hbrake_time);
|
||||||
|
this->mem_lock_current = mcu::eeprom_read16b(addr_lock_current);
|
||||||
|
|
||||||
|
// Validate EEPROM data
|
||||||
|
if(this->mem_btn_force > 100)
|
||||||
|
{
|
||||||
|
this->mem_btn_force = def_btn_force;
|
||||||
|
mcu::eeprom_write8b(addr_btn_force, this->mem_btn_force);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->mem_bmode > 2)
|
||||||
|
{
|
||||||
|
this->mem_bmode = def_bmode;
|
||||||
|
mcu::eeprom_write8b(addr_bmode, this->mem_bmode);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->mem_pot_mode > 1)
|
||||||
|
{
|
||||||
|
this->mem_pot_mode = def_pot_mode;
|
||||||
|
mcu::eeprom_write8b(addr_pot_mode, this->mem_pot_mode);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->mem_dsp_brigth > 100)
|
||||||
|
{
|
||||||
|
this->mem_dsp_brigth = def_brigth;
|
||||||
|
mcu::eeprom_write8b(addr_dsp_brigth, this->mem_dsp_brigth);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->mem_dsp_dimm > 100)
|
||||||
|
{
|
||||||
|
this->mem_dsp_dimm = def_dimm;
|
||||||
|
mcu::eeprom_write8b(addr_dsp_dimm, this->mem_dsp_dimm);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->mem_brake_force > 100)
|
||||||
|
{
|
||||||
|
this->mem_brake_force = def_brake_force;
|
||||||
|
mcu::eeprom_write8b(addr_brake_force, this->mem_brake_force);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
No wrong value
|
||||||
|
if(this->mem_max_hbrake_time > 1000)
|
||||||
|
{
|
||||||
|
this->mem_max_hbrake_time = def_max_hbrake_time;
|
||||||
|
mcu::eeprom_write16b(addr_lock_current, this->mem_max_hbrake_time);
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
if(this->mem_lock_current > max_lock_current)
|
||||||
|
{
|
||||||
|
this->mem_lock_current = def_lock_current;
|
||||||
|
mcu::eeprom_write16b(addr_lock_current, this->mem_lock_current);
|
||||||
|
};
|
||||||
|
|
||||||
|
this->restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
void logic::CfgMemory::save(void)
|
||||||
|
{
|
||||||
|
if(this->btn_force != this->mem_btn_force)
|
||||||
|
{
|
||||||
|
this->mem_btn_force = this->btn_force;
|
||||||
|
mcu::eeprom_write8b(addr_btn_force, this->mem_btn_force);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->bmode != this->mem_bmode)
|
||||||
|
{
|
||||||
|
this->mem_bmode = this->bmode;
|
||||||
|
mcu::eeprom_write8b(addr_bmode, this->mem_bmode);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void logic::CfgMemory::save_all(void)
|
||||||
|
{
|
||||||
|
this->save();
|
||||||
|
|
||||||
|
if(this->pot_mode != this->mem_pot_mode)
|
||||||
|
{
|
||||||
|
this->mem_pot_mode = this->pot_mode;
|
||||||
|
mcu::eeprom_write8b(addr_pot_mode, this->mem_pot_mode);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->dsp_brigth != this->mem_dsp_brigth)
|
||||||
|
{
|
||||||
|
this->mem_dsp_brigth = this->dsp_brigth;
|
||||||
|
mcu::eeprom_write8b(addr_dsp_brigth, this->mem_dsp_brigth);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->dsp_dimm != this->mem_dsp_dimm)
|
||||||
|
{
|
||||||
|
this->mem_dsp_dimm = this->dsp_dimm;
|
||||||
|
mcu::eeprom_write8b(addr_dsp_dimm, this->mem_dsp_dimm);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->brake_force != this->mem_brake_force)
|
||||||
|
{
|
||||||
|
this->mem_brake_force = this->brake_force;
|
||||||
|
mcu::eeprom_write8b(addr_brake_force, this->mem_brake_force);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->max_hbrake_time != this->mem_max_hbrake_time)
|
||||||
|
{
|
||||||
|
this->mem_max_hbrake_time = this->max_hbrake_time;
|
||||||
|
mcu::eeprom_write16b(addr_max_hbrake_time, this->mem_max_hbrake_time);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this->lock_current != this->mem_lock_current)
|
||||||
|
{
|
||||||
|
this->mem_lock_current = this->lock_current;
|
||||||
|
mcu::eeprom_write16b(addr_lock_current, this->mem_lock_current);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void logic::CfgMemory::restore(void)
|
||||||
|
{
|
||||||
|
this->btn_force = this->mem_btn_force;
|
||||||
|
this->bmode = this->mem_bmode;
|
||||||
|
this->pot_mode = this->mem_pot_mode;
|
||||||
|
this->dsp_brigth = this->mem_dsp_brigth;
|
||||||
|
this->dsp_dimm = this->mem_dsp_dimm;
|
||||||
|
this->brake_force = this->mem_brake_force;
|
||||||
|
this->max_hbrake_time = this->mem_max_hbrake_time;
|
||||||
|
this->lock_current = this->mem_lock_current;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
50
firmware/src/logic/cfg_mem.h
Normal file
50
firmware/src/logic/cfg_mem.h
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#ifndef CONFIG_H_
|
||||||
|
#define CONFIG_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "../hw/button.h"
|
||||||
|
|
||||||
|
namespace logic {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
|
||||||
|
class CfgMemory
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
uint8_t mem_btn_force;
|
||||||
|
uint8_t mem_bmode;
|
||||||
|
uint8_t mem_pot_mode;
|
||||||
|
uint8_t mem_dsp_brigth;
|
||||||
|
uint8_t mem_dsp_dimm;
|
||||||
|
uint8_t mem_brake_force;
|
||||||
|
uint16_t mem_max_hbrake_time;
|
||||||
|
uint16_t mem_lock_current;
|
||||||
|
|
||||||
|
public:
|
||||||
|
CfgMemory(void);
|
||||||
|
~CfgMemory(void);
|
||||||
|
|
||||||
|
uint8_t btn_force;
|
||||||
|
uint8_t bmode;
|
||||||
|
uint8_t pot_mode;
|
||||||
|
uint8_t dsp_brigth;
|
||||||
|
uint8_t dsp_dimm;
|
||||||
|
uint8_t brake_force;
|
||||||
|
uint16_t max_hbrake_time;
|
||||||
|
uint16_t lock_current;
|
||||||
|
|
||||||
|
void init(void);
|
||||||
|
void save(void);
|
||||||
|
void save_all(void);
|
||||||
|
void restore(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* DCCD_FORCE_H_ */
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "coil.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
static const int16_t TARGET_HANDBRAKE = COIL_TARGET_HANDBRAKE;
|
|
||||||
static const int16_t LOCK_VOLTAGE = COIL_LOCK_VOLTAGE;
|
|
||||||
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
int16_t coil_target(uint8_t force, uint8_t hbrake_act)
|
|
||||||
{
|
|
||||||
if(hbrake_act)
|
|
||||||
{
|
|
||||||
return TARGET_HANDBRAKE;
|
|
||||||
}
|
|
||||||
else if(force==0)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if(force >= 100)
|
|
||||||
{
|
|
||||||
return LOCK_VOLTAGE;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Calculate target
|
|
||||||
uint32_t t = (uint32_t)force * LOCK_VOLTAGE;
|
|
||||||
t /= 100;
|
|
||||||
if(t > LOCK_VOLTAGE) return LOCK_VOLTAGE;
|
|
||||||
else if(t < 0) return 0;
|
|
||||||
else return (int16_t)t;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
#ifndef COIL_H_
|
|
||||||
#define COIL_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define COIL_TARGET_HANDBRAKE -1
|
|
||||||
#define COIL_LOCK_VOLTAGE 6500
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
int16_t coil_target(uint8_t force, uint8_t hbrake_act);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* COIL_H_ */
|
|
||||||
84
firmware/src/logic/dccd_force.cpp
Normal file
84
firmware/src/logic/dccd_force.cpp
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "dccd_force.h"
|
||||||
|
|
||||||
|
using namespace logic;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
static const uint16_t def_max_hbrake_time = 1000;
|
||||||
|
static const uint16_t def_brake_force = 100;
|
||||||
|
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
logic::DccdForce::DccdForce(hw::Button* btn_mode, hw::Button* sw_hbrake, hw::Button* sw_brakes)
|
||||||
|
{
|
||||||
|
this->mode = btn_mode;
|
||||||
|
this->handbrake = sw_hbrake;
|
||||||
|
this->brakes = sw_brakes;
|
||||||
|
|
||||||
|
this->is_new = 0;
|
||||||
|
this->force = 0;
|
||||||
|
this->brake_mode = 0;
|
||||||
|
|
||||||
|
this->max_hbrake_time = def_max_hbrake_time;
|
||||||
|
this->brake_force = def_brake_force;
|
||||||
|
}
|
||||||
|
|
||||||
|
logic::DccdForce::~DccdForce(void)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t logic::DccdForce::update(uint8_t user_force)
|
||||||
|
{
|
||||||
|
// Process mode button
|
||||||
|
if((this->mode->is_new)&&(this->mode->state == hw::BUTTON_ON))
|
||||||
|
{
|
||||||
|
// Cycle brake mode
|
||||||
|
switch(this->brake_mode)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
this->brake_mode = 1;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
this->brake_mode = 2;
|
||||||
|
|
||||||
|
default:
|
||||||
|
this->brake_mode = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->mode->is_new = 0;
|
||||||
|
this->is_new_bmode = 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Determine target force source
|
||||||
|
uint8_t next_force = user_force;
|
||||||
|
if((this->handbrake->state == hw::BUTTON_ON)&&((this->handbrake->time < this->max_hbrake_time)||(this->max_hbrake_time == 0)))
|
||||||
|
{
|
||||||
|
next_force = 0;
|
||||||
|
}
|
||||||
|
else if(this->brakes->state == hw::BUTTON_ON)
|
||||||
|
{
|
||||||
|
switch(this->brake_mode)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
next_force = 0;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
next_force = user_force;
|
||||||
|
|
||||||
|
default:
|
||||||
|
next_force = this->brake_force;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if(next_force != this->force) this->is_new = 1;
|
||||||
|
|
||||||
|
this->force = next_force;
|
||||||
|
|
||||||
|
return this->force;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
42
firmware/src/logic/dccd_force.h
Normal file
42
firmware/src/logic/dccd_force.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#ifndef DCCD_FORCE_H_
|
||||||
|
#define DCCD_FORCE_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "../hw/button.h"
|
||||||
|
|
||||||
|
namespace logic {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
|
||||||
|
class DccdForce
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
hw::Button* mode;
|
||||||
|
hw::Button* handbrake;
|
||||||
|
hw::Button* brakes;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DccdForce(hw::Button* btn_mode, hw::Button* sw_hbrake, hw::Button* sw_brakes);
|
||||||
|
~DccdForce(void);
|
||||||
|
|
||||||
|
uint8_t force;
|
||||||
|
uint8_t is_new;
|
||||||
|
|
||||||
|
uint8_t brake_mode;
|
||||||
|
uint8_t is_new_bmode;
|
||||||
|
|
||||||
|
uint16_t max_hbrake_time;
|
||||||
|
uint8_t brake_force;
|
||||||
|
|
||||||
|
uint8_t update(uint8_t user_force);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* DCCD_FORCE_H_ */
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "display.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
static const uint8_t BACKLIGHT_DIMM = DSP_BACKLIGHT_DIMM_PERCENT;
|
|
||||||
static const uint8_t BACKLIGHT_BRIGTH = DSP_BACKLIGHT_BRIGTH_PERCENT;
|
|
||||||
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
static uint8_t img_gen_dot10(uint8_t percent);
|
|
||||||
static uint8_t img_gen_dot20(uint8_t percent);
|
|
||||||
static uint8_t img_gen_bar(uint8_t percent);
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
void dsp_init_ctrl(dsp_ctrl_t* ctrl)
|
|
||||||
{
|
|
||||||
ctrl->img_lock = 0;
|
|
||||||
ctrl->act_img = 0x00;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dsp_set_lock(dsp_ctrl_t* ctrl)
|
|
||||||
{
|
|
||||||
ctrl->img_lock = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dsp_reset_lock(dsp_ctrl_t* ctrl)
|
|
||||||
{
|
|
||||||
ctrl->img_lock = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t dsp_img_percent(uint8_t value, dsp_style_t style, dsp_ctrl_t* ctrl)
|
|
||||||
{
|
|
||||||
if(ctrl->img_lock) return ctrl->act_img;
|
|
||||||
|
|
||||||
switch(style)
|
|
||||||
{
|
|
||||||
case LED_DSP_BAR:
|
|
||||||
ctrl->act_img = img_gen_bar(value);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LED_DSP_DOT10:
|
|
||||||
ctrl->act_img = img_gen_dot10(value);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
ctrl->act_img = img_gen_dot20(value);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ctrl->act_img;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t dsp_img_raw(uint8_t image, dsp_ctrl_t* ctrl)
|
|
||||||
{
|
|
||||||
if(ctrl->img_lock) return ctrl->act_img;
|
|
||||||
|
|
||||||
ctrl->act_img = image & 0x3F;
|
|
||||||
|
|
||||||
return ctrl->act_img;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t dsp_get_act_img(dsp_ctrl_t* ctrl)
|
|
||||||
{
|
|
||||||
return ctrl->act_img;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t dsp_backlight(uint8_t dimm_act)
|
|
||||||
{
|
|
||||||
if(dimm_act) return BACKLIGHT_DIMM;
|
|
||||||
else return BACKLIGHT_BRIGTH;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
static uint8_t img_gen_dot10(uint8_t percent)
|
|
||||||
{
|
|
||||||
if(percent<6) return 0x01;
|
|
||||||
else if(percent<16) return 0x03;
|
|
||||||
else if(percent<26) return 0x02;
|
|
||||||
else if(percent<36) return 0x06;
|
|
||||||
else if(percent<46) return 0x04;
|
|
||||||
else if(percent<56) return 0x0C;
|
|
||||||
else if(percent<66) return 0x08;
|
|
||||||
else if(percent<76) return 0x18;
|
|
||||||
else if(percent<86) return 0x10;
|
|
||||||
else if(percent<96) return 0x30;
|
|
||||||
else return 0x20;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t img_gen_dot20(uint8_t percent)
|
|
||||||
{
|
|
||||||
if(percent<11) return 0x01;
|
|
||||||
else if(percent<31) return 0x02;
|
|
||||||
else if(percent<51) return 0x04;
|
|
||||||
else if(percent<71) return 0x08;
|
|
||||||
else if(percent<91) return 0x10;
|
|
||||||
else return 0x20;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t img_gen_bar(uint8_t percent)
|
|
||||||
{
|
|
||||||
if(percent<11) return 0x01;
|
|
||||||
else if(percent<31) return 0x03;
|
|
||||||
else if(percent<51) return 0x07;
|
|
||||||
else if(percent<71) return 0x0F;
|
|
||||||
else if(percent<91) return 0x1F;
|
|
||||||
else return 0x3F;
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#ifndef LOGIC_DISPLAY_H_
|
|
||||||
#define LOGIC_DISPLAY_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define DSP_BACKLIGHT_DIMM_PERCENT 50
|
|
||||||
#define DSP_BACKLIGHT_BRIGTH_PERCENT 100
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
LED_DSP_DOT20,
|
|
||||||
LED_DSP_DOT10,
|
|
||||||
LED_DSP_BAR
|
|
||||||
} dsp_style_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t img_lock;
|
|
||||||
uint8_t act_img;
|
|
||||||
} dsp_ctrl_t;
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
void dsp_init_ctrl(dsp_ctrl_t* ctrl);
|
|
||||||
|
|
||||||
void dsp_set_lock(dsp_ctrl_t* ctrl);
|
|
||||||
void dsp_reset_lock(dsp_ctrl_t* ctrl);
|
|
||||||
|
|
||||||
uint8_t dsp_img_percent(uint8_t value, dsp_style_t style, dsp_ctrl_t* ctrl);
|
|
||||||
uint8_t dsp_img_raw(uint8_t image, dsp_ctrl_t* ctrl);
|
|
||||||
uint8_t dsp_get_act_img(dsp_ctrl_t* ctrl);
|
|
||||||
|
|
||||||
uint8_t dsp_backlight(uint8_t dimm_act);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* LOGIC_DISPLAY_H_ */
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "force.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
static const uint16_t MAX_HBRAKE_HOLD_TIME = FORCE_MAX_HBRAKE_HOLD_TIME;
|
|
||||||
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint8_t force_next(uint8_t handbrake, uint8_t brakes, fbrake_mode_t bmode, uint8_t user_force, uint16_t hbarke_act_time)
|
|
||||||
{
|
|
||||||
// Do force logic
|
|
||||||
if((handbrake)&&(hbarke_act_time < MAX_HBRAKE_HOLD_TIME))
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if(brakes)
|
|
||||||
{
|
|
||||||
switch(bmode)
|
|
||||||
{
|
|
||||||
case FORCE_BMODE_KEEP:
|
|
||||||
return user_force;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FORCE_BMODE_LOCK:
|
|
||||||
return 100;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default: //FORCE_BMODE_OPEN
|
|
||||||
return 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return user_force;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
fbrake_mode_t force_cycle_bmode(fbrake_mode_t bmode)
|
|
||||||
{
|
|
||||||
switch(bmode)
|
|
||||||
{
|
|
||||||
case FORCE_BMODE_OPEN:
|
|
||||||
return FORCE_BMODE_KEEP;
|
|
||||||
|
|
||||||
case FORCE_BMODE_KEEP:
|
|
||||||
return FORCE_BMODE_LOCK;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return FORCE_BMODE_OPEN;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
#ifndef FORCE_H_
|
|
||||||
#define FORCE_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define FORCE_MAX_HBRAKE_HOLD_TIME 1000
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
FORCE_BMODE_OPEN,
|
|
||||||
FORCE_BMODE_KEEP,
|
|
||||||
FORCE_BMODE_LOCK
|
|
||||||
} fbrake_mode_t;
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
uint8_t force_next(uint8_t handbrake, uint8_t brakes, fbrake_mode_t bmode, uint8_t user_force, uint16_t hbarke_act_time);
|
|
||||||
fbrake_mode_t force_cycle_bmode(fbrake_mode_t bmode);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* FORCE_H_ */
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "pot.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint8_t pot_mv_to_percent(uint16_t value, pot_cfg_t* cfg)
|
|
||||||
{
|
|
||||||
// Setup limits
|
|
||||||
uint16_t bottom = 0;
|
|
||||||
uint16_t top = cfg->reference;
|
|
||||||
|
|
||||||
// Adjust for top and bottom deadband
|
|
||||||
if(bottom < cfg->deadband) bottom = cfg->deadband;
|
|
||||||
if(top > cfg->deadband) top -= cfg->deadband;
|
|
||||||
|
|
||||||
// Calculate percent
|
|
||||||
if(value <= bottom) return 0;
|
|
||||||
else if(value >= top) return 100;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Adjust values for offset
|
|
||||||
if(bottom)
|
|
||||||
{
|
|
||||||
value = value - bottom;
|
|
||||||
top = top - bottom;
|
|
||||||
};
|
|
||||||
|
|
||||||
uint32_t y = (uint32_t)value * 100;
|
|
||||||
y = y/(uint32_t)top;
|
|
||||||
|
|
||||||
return (uint16_t)y;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
#ifndef POTENTIOMETER_H_
|
|
||||||
#define POTENTIOMETER_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
typedef struct {
|
|
||||||
uint16_t reference;
|
|
||||||
uint16_t deadband;
|
|
||||||
} pot_cfg_t;
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
uint8_t pot_mv_to_percent(uint16_t value, pot_cfg_t* cfg);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* POTENTIOMETER_H_ */
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include "user_force.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
static const uint8_t MAX_PERCENT = USER_FORCE_MAX_PERCENT;
|
|
||||||
static const uint8_t MIN_PERCENT = USER_FORCE_MIN_PERCENT;
|
|
||||||
|
|
||||||
/**** Private variables ****/
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint8_t user_force_btn(uint8_t prev_force, uint8_t up_act, uint8_t down_act, uint8_t delta)
|
|
||||||
{
|
|
||||||
uint8_t new_froce = prev_force;
|
|
||||||
|
|
||||||
if(up_act)
|
|
||||||
{
|
|
||||||
new_froce = prev_force + delta;
|
|
||||||
// Limit overflow and top value
|
|
||||||
if(new_froce < prev_force) new_froce = 100;
|
|
||||||
else if(new_froce < MIN_PERCENT) new_froce = MIN_PERCENT;
|
|
||||||
};
|
|
||||||
|
|
||||||
if(down_act)
|
|
||||||
{
|
|
||||||
new_froce = prev_force - delta;
|
|
||||||
// Limit overflow and top value
|
|
||||||
if(new_froce > prev_force) new_froce = 0;
|
|
||||||
else if(new_froce > MAX_PERCENT) new_froce = MAX_PERCENT;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Do deadband
|
|
||||||
if(new_froce < MIN_PERCENT) new_froce = 0;
|
|
||||||
else if(new_froce > MAX_PERCENT) new_froce = 100;
|
|
||||||
|
|
||||||
return new_froce;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t user_force_pot(uint8_t prev_force, uint8_t pot, uint8_t hyst)
|
|
||||||
{
|
|
||||||
uint8_t new_froce = pot;
|
|
||||||
|
|
||||||
if(new_froce < MIN_PERCENT) new_froce = 0;
|
|
||||||
else if(new_froce > MAX_PERCENT) new_froce = 100;
|
|
||||||
|
|
||||||
return new_froce;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
#ifndef USER_FORCE_H_
|
|
||||||
#define USER_FORCE_H_
|
|
||||||
|
|
||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/**** Public definitions ****/
|
|
||||||
#define USER_FORCE_MAX_PERCENT 90
|
|
||||||
#define USER_FORCE_MIN_PERCENT 10
|
|
||||||
|
|
||||||
/**** Public function declarations ****/
|
|
||||||
uint8_t user_force_btn(uint8_t prev_force, uint8_t up_act, uint8_t down_act, uint8_t delta);
|
|
||||||
uint8_t user_force_pot(uint8_t prev_force, uint8_t pot, uint8_t hyst);
|
|
||||||
|
|
||||||
#ifdef TESTING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* USER_FORCE_H_ */
|
|
||||||
@@ -1,127 +0,0 @@
|
|||||||
/**** Includes ****/
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
// Hardware IO
|
|
||||||
#include "hw/startup.h"
|
|
||||||
#include "hw/analog.h"
|
|
||||||
#include "hw/buttons.h"
|
|
||||||
#include "hw/hb_control.h"
|
|
||||||
#include "hw/led_display.h"
|
|
||||||
|
|
||||||
// Logic blocks
|
|
||||||
#include "logic/coil.h"
|
|
||||||
#include "logic/display.h"
|
|
||||||
#include "logic/force.h"
|
|
||||||
#include "logic/pot.h"
|
|
||||||
#include "logic/user_force.h"
|
|
||||||
|
|
||||||
/**** Private definitions ****/
|
|
||||||
/**** Private constants ****/
|
|
||||||
/**** Private variables ****/
|
|
||||||
static volatile uint8_t user_force = 0;
|
|
||||||
static volatile uint8_t user_force_step = 10;
|
|
||||||
|
|
||||||
static volatile fbrake_mode_t bmode = FORCE_BMODE_OPEN;
|
|
||||||
|
|
||||||
static volatile uint8_t next_force = 0;
|
|
||||||
static volatile int16_t next_target = 0;
|
|
||||||
|
|
||||||
static volatile hb_feedback_t hb_feedback;
|
|
||||||
static volatile hb_control_t hb_ctrl;
|
|
||||||
|
|
||||||
static volatile uint8_t backlight = 0;
|
|
||||||
static volatile dsp_ctrl_t dsp_logic_ctrl;
|
|
||||||
|
|
||||||
/**** Private function declarations ****/
|
|
||||||
|
|
||||||
/**** Public function definitions ****/
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
hw_startup();
|
|
||||||
|
|
||||||
// Create objects for all buttons
|
|
||||||
btn_t btn_mode;
|
|
||||||
btn_t btn_down;
|
|
||||||
btn_t btn_up;
|
|
||||||
btn_t btn_dimm;
|
|
||||||
btn_t btn_brakes;
|
|
||||||
btn_t btn_handbrake;
|
|
||||||
btn_t btn_handbrake_dir;
|
|
||||||
|
|
||||||
// Set button pulls
|
|
||||||
//btn_ch_set_pull(BTN_1, BTN_PULL_HIGH);
|
|
||||||
//btn_ch_set_pull(BTN_2, BTN_PULL_HIGH);
|
|
||||||
//btn_ch_set_pull(BTN_3, BTN_PULL_HIGH);
|
|
||||||
btn_ch_set_pull(BTN_6, BTN_PULL_HIGH);
|
|
||||||
|
|
||||||
// Create object for half-bridge control
|
|
||||||
|
|
||||||
// Initialize half-bridge
|
|
||||||
hb_init(&hb_feedback, &hb_ctrl);
|
|
||||||
hb_enable(&hb_ctrl);
|
|
||||||
|
|
||||||
// Init display backlight
|
|
||||||
dsp_init_ctrl(&dsp_logic_ctrl);
|
|
||||||
backlight = dsp_backlight(1);
|
|
||||||
led_dsp_backligth_set(backlight);
|
|
||||||
|
|
||||||
while(1)
|
|
||||||
{
|
|
||||||
// Read buttons
|
|
||||||
btn_ch_process(BTN_1, &btn_mode);
|
|
||||||
btn_ch_process(BTN_2, &btn_down);
|
|
||||||
btn_ch_process(BTN_3, &btn_up);
|
|
||||||
btn_ch_process(BTN_4, &btn_dimm);
|
|
||||||
btn_ch_process(BTN_5, &btn_brakes);
|
|
||||||
btn_ch_process(BTN_6, &btn_handbrake);
|
|
||||||
btn_ch_process(BTN_6N, &btn_handbrake_dir);
|
|
||||||
|
|
||||||
// Process user force changes
|
|
||||||
if((btn_up.new_state)||(btn_down.new_state))
|
|
||||||
{
|
|
||||||
uint8_t up_act = btn_up.new_state & btn_up.state;
|
|
||||||
uint8_t down_act = btn_down.new_state & btn_down.state;
|
|
||||||
|
|
||||||
user_force = user_force_btn(user_force, up_act, down_act, user_force_step);
|
|
||||||
btn_up.new_state = 0;
|
|
||||||
btn_down.new_state = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Process brake mode changes
|
|
||||||
if(btn_mode.new_state)
|
|
||||||
{
|
|
||||||
if(btn_mode.state) bmode = force_cycle_bmode(bmode);
|
|
||||||
btn_mode.new_state = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Calculate next force
|
|
||||||
next_force = force_next(btn_handbrake.state, btn_brakes.state, bmode, user_force, btn_handbrake.state_time);
|
|
||||||
|
|
||||||
// Calculate next coil target
|
|
||||||
next_target = coil_target(next_force, btn_handbrake.state);
|
|
||||||
|
|
||||||
// Read Half-bridge status and apply next target
|
|
||||||
hb_process(next_target, &hb_feedback, &hb_ctrl);
|
|
||||||
|
|
||||||
// Generate image of current force
|
|
||||||
uint8_t img = 0x00;
|
|
||||||
|
|
||||||
if(hb_feedback.fused) img = 0xAA;
|
|
||||||
else img = dsp_img_percent(next_force, LED_DSP_DOT10);
|
|
||||||
|
|
||||||
// Apply image
|
|
||||||
led_dsp_set_image(img);
|
|
||||||
|
|
||||||
// Process display backlight
|
|
||||||
if(btn_dimm.new_state)
|
|
||||||
{
|
|
||||||
backlight = dsp_backlight(btn_dimm.state);
|
|
||||||
led_dsp_backligth_set(backlight);
|
|
||||||
btn_dimm.new_state = 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**** Private function definitions ****/
|
|
||||||
110
firmware/src/main.cpp
Normal file
110
firmware/src/main.cpp
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "utils/utils.h"
|
||||||
|
|
||||||
|
#include "hw/devices.h"
|
||||||
|
|
||||||
|
#include "logic/button_force.h"
|
||||||
|
#include "logic/dccd_force.h"
|
||||||
|
|
||||||
|
#include "logic/cfg_mem.h"
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
static const uint16_t dsp_lock_bmode = 1000;
|
||||||
|
static const uint16_t dsp_lock_force = 50;
|
||||||
|
|
||||||
|
/**** Private variables ****/
|
||||||
|
static logic::CfgMemory cfg_mem = logic::CfgMemory();
|
||||||
|
|
||||||
|
static logic::ButtonForce button_force = logic::ButtonForce(&btn_up, &btn_down);
|
||||||
|
static logic::DccdForce dccd_force = logic::DccdForce(&btn_mode, &sw_hbrake, &sw_brakes);
|
||||||
|
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// HW setup
|
||||||
|
devices_init();
|
||||||
|
|
||||||
|
// Read saved config
|
||||||
|
cfg_mem.init();
|
||||||
|
|
||||||
|
uint8_t user_force = 0;
|
||||||
|
|
||||||
|
button_force.force = cfg_mem.btn_force;
|
||||||
|
dccd_force.brake_mode = cfg_mem.bmode;
|
||||||
|
dccd_force.max_hbrake_time = cfg_mem.max_hbrake_time;
|
||||||
|
dccd_force.brake_force = cfg_mem.brake_force;
|
||||||
|
|
||||||
|
// Super loop
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
// Update inputs
|
||||||
|
devices_update_inputs();
|
||||||
|
|
||||||
|
// Update user setting
|
||||||
|
button_force.update();
|
||||||
|
|
||||||
|
// Select user force input
|
||||||
|
if(cfg_mem.pot_mode) user_force = pot.percent;
|
||||||
|
else user_force = button_force.force;
|
||||||
|
|
||||||
|
// Calculate next target force
|
||||||
|
dccd_force.update(user_force);
|
||||||
|
|
||||||
|
// Override force in case of fault
|
||||||
|
if((sup_fuse.fault)||(out_fuse.fault)) dccd_force.force = 0;
|
||||||
|
|
||||||
|
// Convert force to current
|
||||||
|
ccout.target = util::percent_of(dccd_force.force, cfg_mem.lock_current);
|
||||||
|
|
||||||
|
// Execute outputs
|
||||||
|
ccout.update();
|
||||||
|
|
||||||
|
// Set display
|
||||||
|
if(dccd_force.is_new_bmode)
|
||||||
|
{
|
||||||
|
uint8_t bmode_img = 0x03;
|
||||||
|
switch(dccd_force.brake_mode)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
bmode_img = 0x0C;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
bmode_img = 0x30;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
bmode_img = 0x03;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
display.write(bmode_img);
|
||||||
|
display.set_lock(dsp_lock_bmode);
|
||||||
|
dccd_force.is_new_bmode = 0;
|
||||||
|
}
|
||||||
|
else if((button_force.is_new)&&(cfg_mem.pot_mode==0))
|
||||||
|
{
|
||||||
|
display.show_percent(dccd_force.force, hw::DisplayLed::LED_DSP_DOT10);
|
||||||
|
display.set_lock(dsp_lock_force);
|
||||||
|
button_force.is_new = 0;
|
||||||
|
}
|
||||||
|
else if(display.locked==0) display.show_percent(dccd_force.force, hw::DisplayLed::LED_DSP_DOT10);
|
||||||
|
|
||||||
|
// Process dimm
|
||||||
|
if(sw_dimm.state == hw::BUTTON_ON) display.set_brigthness(cfg_mem.dsp_dimm);
|
||||||
|
else display.set_brigthness(cfg_mem.dsp_brigth);
|
||||||
|
|
||||||
|
// Save user setting changes
|
||||||
|
cfg_mem.btn_force = button_force.force;
|
||||||
|
cfg_mem.bmode = dccd_force.brake_mode;
|
||||||
|
cfg_mem.save();
|
||||||
|
|
||||||
|
continue; // End of super loop
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escape the matrix
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ***/
|
||||||
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||||||
# Atmel Studio Solution File, Format Version 11.00
|
# Atmel Studio Solution File, Format Version 11.00
|
||||||
VisualStudioVersion = 14.0.23107.0
|
VisualStudioVersion = 14.0.23107.0
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{54F91283-7BC4-4236-8FF9-10F437C3AD48}") = "uDCCD_Controller", "uDCCD_Controller.cproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}"
|
Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "uDCCD", "uDCCD.cppproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -41,7 +41,7 @@
|
|||||||
<Attribute>template</Attribute>
|
<Attribute>template</Attribute>
|
||||||
<Category>source</Category>
|
<Category>source</Category>
|
||||||
<Condition>C Exe</Condition>
|
<Condition>C Exe</Condition>
|
||||||
<FileContentHash>YLr2MkKo6ZooP7MhARWYNA==</FileContentHash>
|
<FileContentHash>KjvOcFWd++tbnsEMfVPd/w==</FileContentHash>
|
||||||
<FileVersion></FileVersion>
|
<FileVersion></FileVersion>
|
||||||
<Name>templates/main.c</Name>
|
<Name>templates/main.c</Name>
|
||||||
<SelectString>Main file (.c)</SelectString>
|
<SelectString>Main file (.c)</SelectString>
|
||||||
@@ -52,7 +52,7 @@
|
|||||||
<Attribute>template</Attribute>
|
<Attribute>template</Attribute>
|
||||||
<Category>source</Category>
|
<Category>source</Category>
|
||||||
<Condition>C Exe</Condition>
|
<Condition>C Exe</Condition>
|
||||||
<FileContentHash>mkKaE95TOoATsuBGv6jmxg==</FileContentHash>
|
<FileContentHash>w5aB/d0+DbxGZ7yY0aMMjw==</FileContentHash>
|
||||||
<FileVersion></FileVersion>
|
<FileVersion></FileVersion>
|
||||||
<Name>templates/main.cpp</Name>
|
<Name>templates/main.cpp</Name>
|
||||||
<SelectString>Main file (.cpp)</SelectString>
|
<SelectString>Main file (.cpp)</SelectString>
|
||||||
@@ -3,18 +3,18 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<SchemaVersion>2.0</SchemaVersion>
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
<ProjectVersion>7.0</ProjectVersion>
|
<ProjectVersion>7.0</ProjectVersion>
|
||||||
<ToolchainName>com.Atmel.AVRGCC8.C</ToolchainName>
|
<ToolchainName>com.Atmel.AVRGCC8.CPP</ToolchainName>
|
||||||
<ProjectGuid>dce6c7e3-ee26-4d79-826b-08594b9ad897</ProjectGuid>
|
<ProjectGuid>dce6c7e3-ee26-4d79-826b-08594b9ad897</ProjectGuid>
|
||||||
<avrdevice>ATmega328PB</avrdevice>
|
<avrdevice>ATmega328PB</avrdevice>
|
||||||
<avrdeviceseries>none</avrdeviceseries>
|
<avrdeviceseries>none</avrdeviceseries>
|
||||||
<OutputType>Executable</OutputType>
|
<OutputType>Executable</OutputType>
|
||||||
<Language>C</Language>
|
<Language>CPP</Language>
|
||||||
<OutputFileName>$(MSBuildProjectName)</OutputFileName>
|
<OutputFileName>$(MSBuildProjectName)</OutputFileName>
|
||||||
<OutputFileExtension>.elf</OutputFileExtension>
|
<OutputFileExtension>.elf</OutputFileExtension>
|
||||||
<OutputDirectory>$(MSBuildProjectDirectory)\$(Configuration)</OutputDirectory>
|
<OutputDirectory>$(MSBuildProjectDirectory)\$(Configuration)</OutputDirectory>
|
||||||
<AssemblyName>uDCCD_Controller</AssemblyName>
|
<AssemblyName>uDCCD</AssemblyName>
|
||||||
<Name>uDCCD_Controller</Name>
|
<Name>uDCCD</Name>
|
||||||
<RootNamespace>uDCCD_Controller</RootNamespace>
|
<RootNamespace>uDCCD</RootNamespace>
|
||||||
<ToolchainFlavour>Native</ToolchainFlavour>
|
<ToolchainFlavour>Native</ToolchainFlavour>
|
||||||
<KeepTimersRunning>true</KeepTimersRunning>
|
<KeepTimersRunning>true</KeepTimersRunning>
|
||||||
<OverrideVtor>false</OverrideVtor>
|
<OverrideVtor>false</OverrideVtor>
|
||||||
@@ -26,40 +26,24 @@
|
|||||||
<OverrideVtorValue>exception_table</OverrideVtorValue>
|
<OverrideVtorValue>exception_table</OverrideVtorValue>
|
||||||
<BootSegment>2</BootSegment>
|
<BootSegment>2</BootSegment>
|
||||||
<ResetRule>0</ResetRule>
|
<ResetRule>0</ResetRule>
|
||||||
<eraseonlaunchrule>1</eraseonlaunchrule>
|
<eraseonlaunchrule>0</eraseonlaunchrule>
|
||||||
<EraseKey />
|
<EraseKey />
|
||||||
<AsfFrameworkConfig>
|
<AsfFrameworkConfig>
|
||||||
<framework-data>
|
<framework-data xmlns="">
|
||||||
<options />
|
<options />
|
||||||
<configurations />
|
<configurations />
|
||||||
<files />
|
<files />
|
||||||
<documentation help="" />
|
<documentation help="" />
|
||||||
<offline-documentation help="" />
|
<offline-documentation help="" />
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.44.1" />
|
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.52.0" />
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</framework-data>
|
</framework-data>
|
||||||
</AsfFrameworkConfig>
|
</AsfFrameworkConfig>
|
||||||
<avrtool>com.atmel.avrdbg.tool.atmelice</avrtool>
|
|
||||||
<avrtoolserialnumber>J42700001490</avrtoolserialnumber>
|
|
||||||
<avrdeviceexpectedsignature>0x1E9516</avrdeviceexpectedsignature>
|
|
||||||
<com_atmel_avrdbg_tool_atmelice>
|
|
||||||
<ToolOptions>
|
|
||||||
<InterfaceProperties>
|
|
||||||
<IspClock>125000</IspClock>
|
|
||||||
</InterfaceProperties>
|
|
||||||
<InterfaceName>debugWIRE</InterfaceName>
|
|
||||||
</ToolOptions>
|
|
||||||
<ToolType>com.atmel.avrdbg.tool.atmelice</ToolType>
|
|
||||||
<ToolNumber>J42700001490</ToolNumber>
|
|
||||||
<ToolName>Atmel-ICE</ToolName>
|
|
||||||
</com_atmel_avrdbg_tool_atmelice>
|
|
||||||
<avrtoolinterface>debugWIRE</avrtoolinterface>
|
|
||||||
<avrtoolinterfaceclock>125000</avrtoolinterfaceclock>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||||
<ToolchainSettings>
|
<ToolchainSettings>
|
||||||
<AvrGcc>
|
<AvrGccCpp>
|
||||||
<avrgcc.common.Device>-mmcu=atmega328pb -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega328pb"</avrgcc.common.Device>
|
<avrgcc.common.Device>-mmcu=atmega328pb -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega328pb"</avrgcc.common.Device>
|
||||||
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
|
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
|
||||||
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
|
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
|
||||||
@@ -82,22 +66,38 @@
|
|||||||
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
||||||
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
||||||
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
||||||
<avrgcc.linker.libraries.Libraries>
|
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||||
|
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||||
|
<avrgcccpp.compiler.symbols.DefSymbols>
|
||||||
<ListValues>
|
<ListValues>
|
||||||
<Value>libm</Value>
|
<Value>NDEBUG</Value>
|
||||||
</ListValues>
|
</ListValues>
|
||||||
</avrgcc.linker.libraries.Libraries>
|
</avrgcccpp.compiler.symbols.DefSymbols>
|
||||||
<avrgcc.assembler.general.IncludePaths>
|
<avrgcccpp.compiler.directories.IncludePaths>
|
||||||
<ListValues>
|
<ListValues>
|
||||||
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
||||||
</ListValues>
|
</ListValues>
|
||||||
</avrgcc.assembler.general.IncludePaths>
|
</avrgcccpp.compiler.directories.IncludePaths>
|
||||||
</AvrGcc>
|
<avrgcccpp.compiler.optimization.level>Optimize for size (-Os)</avrgcccpp.compiler.optimization.level>
|
||||||
|
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
|
||||||
|
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
|
||||||
|
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
|
||||||
|
<avrgcccpp.linker.libraries.Libraries>
|
||||||
|
<ListValues>
|
||||||
|
<Value>libm</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcccpp.linker.libraries.Libraries>
|
||||||
|
<avrgcccpp.assembler.general.IncludePaths>
|
||||||
|
<ListValues>
|
||||||
|
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcccpp.assembler.general.IncludePaths>
|
||||||
|
</AvrGccCpp>
|
||||||
</ToolchainSettings>
|
</ToolchainSettings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||||
<ToolchainSettings>
|
<ToolchainSettings>
|
||||||
<AvrGcc>
|
<AvrGccCpp>
|
||||||
<avrgcc.common.Device>-mmcu=atmega328pb -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega328pb"</avrgcc.common.Device>
|
<avrgcc.common.Device>-mmcu=atmega328pb -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega328pb"</avrgcc.common.Device>
|
||||||
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
|
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
|
||||||
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
|
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
|
||||||
@@ -116,161 +116,173 @@
|
|||||||
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
||||||
</ListValues>
|
</ListValues>
|
||||||
</avrgcc.compiler.directories.IncludePaths>
|
</avrgcc.compiler.directories.IncludePaths>
|
||||||
|
<avrgcc.compiler.optimization.level>Optimize debugging experience (-Og)</avrgcc.compiler.optimization.level>
|
||||||
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
||||||
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
||||||
<avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel>
|
<avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel>
|
||||||
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
||||||
<avrgcc.linker.libraries.Libraries>
|
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||||
|
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||||
|
<avrgcccpp.compiler.symbols.DefSymbols>
|
||||||
<ListValues>
|
<ListValues>
|
||||||
<Value>libm</Value>
|
<Value>DEBUG</Value>
|
||||||
</ListValues>
|
</ListValues>
|
||||||
</avrgcc.linker.libraries.Libraries>
|
</avrgcccpp.compiler.symbols.DefSymbols>
|
||||||
<avrgcc.assembler.general.IncludePaths>
|
<avrgcccpp.compiler.directories.IncludePaths>
|
||||||
<ListValues>
|
<ListValues>
|
||||||
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
||||||
</ListValues>
|
</ListValues>
|
||||||
</avrgcc.assembler.general.IncludePaths>
|
</avrgcccpp.compiler.directories.IncludePaths>
|
||||||
<avrgcc.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcc.assembler.debugging.DebugLevel>
|
<avrgcccpp.compiler.optimization.level>Optimize debugging experience (-Og)</avrgcccpp.compiler.optimization.level>
|
||||||
</AvrGcc>
|
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
|
||||||
|
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
|
||||||
|
<avrgcccpp.compiler.optimization.DebugLevel>Default (-g2)</avrgcccpp.compiler.optimization.DebugLevel>
|
||||||
|
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
|
||||||
|
<avrgcccpp.linker.libraries.Libraries>
|
||||||
|
<ListValues>
|
||||||
|
<Value>libm</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcccpp.linker.libraries.Libraries>
|
||||||
|
<avrgcccpp.assembler.general.IncludePaths>
|
||||||
|
<ListValues>
|
||||||
|
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcccpp.assembler.general.IncludePaths>
|
||||||
|
<avrgcccpp.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcccpp.assembler.debugging.DebugLevel>
|
||||||
|
</AvrGccCpp>
|
||||||
</ToolchainSettings>
|
</ToolchainSettings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="hw\analog.c">
|
<Compile Include="bsp\ain.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\analog.h">
|
<Compile Include="bsp\ain.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\config.h">
|
<Compile Include="bsp\board.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\dout.c">
|
<Compile Include="bsp\board.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\dout.h">
|
<Compile Include="bsp\din.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\setup.c">
|
<Compile Include="bsp\din.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\setup.h">
|
<Compile Include="bsp\dio.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\utils\faults.c">
|
<Compile Include="bsp\dio.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\utils\faults.h">
|
<Compile Include="bsp\halfbridge.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\utils\fuses.c">
|
<Compile Include="bsp\halfbridge.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\utils\fuses.h">
|
<Compile Include="bsp\mcu\mcu_hal.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\buttons.c">
|
<Compile Include="bsp\mcu\mcu_hal_r8.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\buttons.h">
|
<Compile Include="bsp\dout.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\ain.c">
|
<Compile Include="bsp\dout.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\ain.h">
|
<Compile Include="bsp\pwm.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\din.c">
|
<Compile Include="bsp\pwm.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\din.h">
|
<Compile Include="hw\button.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\halfbridge.c">
|
<Compile Include="hw\button.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\halfbridge.h">
|
<Compile Include="hw\cc_output.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\mcu\mcu_hal.h">
|
<Compile Include="hw\cc_output.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\mcu\mcu_hal_r8.c">
|
<Compile Include="hw\cv_output.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\odout.c">
|
<Compile Include="hw\cv_output.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\odout.h">
|
<Compile Include="hw\display_led.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\utils\utils.c">
|
<Compile Include="hw\display_led.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\board\utils\utils.h">
|
<Compile Include="hw\fuse.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\config.h">
|
<Compile Include="hw\fuse.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\hb_control.c">
|
<Compile Include="hw\devices.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\hb_control.h">
|
<Compile Include="hw\devices.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\led_display.c">
|
<Compile Include="hw\potentiometer.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\led_display.h">
|
<Compile Include="hw\potentiometer.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\startup.c">
|
<Compile Include="logic\button_force.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="hw\startup.h">
|
<Compile Include="logic\button_force.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="logic\coil.c">
|
<Compile Include="logic\cfg_mem.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="logic\coil.h">
|
<Compile Include="logic\cfg_mem.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="logic\display.c">
|
<Compile Include="logic\dccd_force.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="logic\display.h">
|
<Compile Include="logic\dccd_force.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="logic\force.c">
|
<Compile Include="main.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="logic\force.h">
|
<Compile Include="utils\interpolate.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="logic\pot.c">
|
<Compile Include="utils\interpolate.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="logic\pot.h">
|
<Compile Include="utils\utils.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="logic\user_force.c">
|
<Compile Include="utils\utils.h">
|
||||||
<SubType>compile</SubType>
|
|
||||||
</Compile>
|
|
||||||
<Compile Include="logic\user_force.h">
|
|
||||||
<SubType>compile</SubType>
|
|
||||||
</Compile>
|
|
||||||
<Compile Include="main.c">
|
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Folder Include="bsp" />
|
||||||
|
<Folder Include="bsp\mcu" />
|
||||||
<Folder Include="hw" />
|
<Folder Include="hw" />
|
||||||
<Folder Include="hw\board" />
|
|
||||||
<Folder Include="hw\board\mcu" />
|
|
||||||
<Folder Include="hw\board\utils" />
|
|
||||||
<Folder Include="logic" />
|
<Folder Include="logic" />
|
||||||
|
<Folder Include="utils" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
|
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
|
||||||
</Project>
|
</Project>
|
||||||
163
firmware/src/utils/interpolate.cpp
Normal file
163
firmware/src/utils/interpolate.cpp
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "utils.h"
|
||||||
|
#include "interpolate.h"
|
||||||
|
|
||||||
|
using namespace util;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
uint16_t util::interpolate_1d(uint16_t x, uint16_t* x_axis, uint16_t* y_values, uint8_t len_axis)
|
||||||
|
{
|
||||||
|
// validate axis length
|
||||||
|
if(len_axis==0) return 0; // Empty data set
|
||||||
|
if(len_axis==1) return y_values[0]; // Only one data point
|
||||||
|
|
||||||
|
uint16_t y;
|
||||||
|
|
||||||
|
uint8_t i = find_interval_end_index(x, x_axis, len_axis);
|
||||||
|
if(i==0)
|
||||||
|
{
|
||||||
|
//Less then start
|
||||||
|
y = y_values[0];
|
||||||
|
}
|
||||||
|
else if(i==len_axis)
|
||||||
|
{
|
||||||
|
//More than end
|
||||||
|
y = y_values[len_axis-1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Do interpolate
|
||||||
|
y = interpolate(x, x_axis[i-1], x_axis[i], y_values[i-1], y_values[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::interpolate_2d(uint16_t x, uint16_t y, uint16_t* x_axis, uint8_t len_x_axis, uint16_t* y_axis, uint8_t len_y_axis, uint16_t* z_values)
|
||||||
|
{
|
||||||
|
// validate axis length
|
||||||
|
if((len_x_axis==0)&&(len_y_axis==0)) return 0; // Empty data set
|
||||||
|
if((len_x_axis==1)&&(len_y_axis==1)) return z_values[0]; // Only one data point
|
||||||
|
|
||||||
|
uint8_t ix = find_interval_end_index(x, x_axis, len_x_axis);
|
||||||
|
uint8_t iy = find_interval_end_index(y, y_axis, len_y_axis);
|
||||||
|
|
||||||
|
// Check corners - easy answers
|
||||||
|
if((ix==0)&&(iy==0))
|
||||||
|
{
|
||||||
|
return z_values[0]; //[0][0] [Y][X]
|
||||||
|
}
|
||||||
|
else if((ix==len_x_axis)&&(iy==0))
|
||||||
|
{
|
||||||
|
return z_values[len_x_axis-1]; //[0][end]
|
||||||
|
}
|
||||||
|
else if((ix==0)&&(iy==len_y_axis))
|
||||||
|
{
|
||||||
|
uint16_t i = index2d_to_index1d(0, len_y_axis-1, len_x_axis);
|
||||||
|
return z_values[i]; //[end][0]
|
||||||
|
}
|
||||||
|
else if((ix==len_x_axis)&&(iy==len_y_axis))
|
||||||
|
{
|
||||||
|
uint16_t i = index2d_to_index1d(len_x_axis-1, len_y_axis-1, len_x_axis);
|
||||||
|
return z_values[i]; //[end][end]
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check boundaries - 1D interpolation
|
||||||
|
if(ix==0)
|
||||||
|
{
|
||||||
|
// On ix=0 line
|
||||||
|
uint16_t i = 0;
|
||||||
|
uint16_t z0 = z_values[i];
|
||||||
|
i = index2d_to_index1d(0, len_y_axis-1, len_x_axis);
|
||||||
|
uint16_t z1 = z_values[i];
|
||||||
|
return interpolate(y, y_axis[0], y_axis[len_y_axis-1], z0, z1);
|
||||||
|
}
|
||||||
|
else if(ix==len_x_axis)
|
||||||
|
{
|
||||||
|
// On ix=END line
|
||||||
|
uint16_t i = len_x_axis-1;
|
||||||
|
uint16_t z0 = z_values[i];
|
||||||
|
i = index2d_to_index1d(len_x_axis-1, len_y_axis-1, len_x_axis);
|
||||||
|
uint16_t z1 = z_values[i];
|
||||||
|
return interpolate(y, y_axis[0], y_axis[len_y_axis-1], z0, z1);
|
||||||
|
}
|
||||||
|
else if(iy==0)
|
||||||
|
{
|
||||||
|
// On iy=0 line
|
||||||
|
uint16_t i = 0;
|
||||||
|
uint16_t z0 = z_values[i];
|
||||||
|
i = len_x_axis-1;
|
||||||
|
uint16_t z1 = z_values[i];
|
||||||
|
return interpolate(x, x_axis[0], x_axis[len_x_axis-1], z0, z1);
|
||||||
|
}
|
||||||
|
else if(iy==len_y_axis)
|
||||||
|
{
|
||||||
|
// On iy=END line
|
||||||
|
uint16_t i = index2d_to_index1d(0, len_y_axis-1, len_x_axis);
|
||||||
|
uint16_t z0 = z_values[i];
|
||||||
|
i = index2d_to_index1d(len_x_axis-1, len_y_axis-1, len_x_axis);
|
||||||
|
uint16_t z1 = z_values[i];
|
||||||
|
return interpolate(x, x_axis[0], x_axis[len_x_axis-1], z0, z1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do interpolation
|
||||||
|
// Get axis values
|
||||||
|
uint16_t x0 = x_axis[ix-1];
|
||||||
|
uint16_t x1 = x_axis[ix];
|
||||||
|
uint16_t y0 = y_axis[iy-1];
|
||||||
|
uint16_t y1 = y_axis[iy];
|
||||||
|
|
||||||
|
// Do y0 line calculation
|
||||||
|
// Get z values at x0 and x1 points on y0 line
|
||||||
|
uint16_t i = index2d_to_index1d(ix-1, iy-1, len_x_axis);
|
||||||
|
uint16_t z0 = z_values[i];
|
||||||
|
uint16_t z1 = z_values[i+1];
|
||||||
|
// Interpolate z value on y0 line
|
||||||
|
uint16_t zy0 = interpolate(x, x0, x1, z0, z1);
|
||||||
|
|
||||||
|
// Do y1 line calculation
|
||||||
|
// Get z values at x0 and x1 points on y1 line
|
||||||
|
i = index2d_to_index1d(ix-1, iy, len_x_axis);
|
||||||
|
z0 = z_values[i];
|
||||||
|
z1 = z_values[i+1];
|
||||||
|
// Interpolate z value on y0 line
|
||||||
|
uint16_t zy1 = interpolate(x, x0, x1, z0, z1);
|
||||||
|
|
||||||
|
// Do calculation in y axis on xz line
|
||||||
|
return interpolate(y, y0, y1, zy0, zy1);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::interpolate(uint16_t x, uint16_t x0, uint16_t x1, uint16_t y0, uint16_t y1)
|
||||||
|
{
|
||||||
|
int32_t dy = (int32_t)y1 - (int32_t)y0;
|
||||||
|
int32_t dx = (int32_t)x1 - (int32_t)x0;
|
||||||
|
int32_t d = (int32_t)x - (int32_t)x0;
|
||||||
|
|
||||||
|
int32_t y = dy * d;
|
||||||
|
y /= dx;
|
||||||
|
y += y0;
|
||||||
|
|
||||||
|
return util::sat_cast(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t util::find_interval_end_index(uint16_t val, uint16_t* axis_values, uint8_t len_axis)
|
||||||
|
{
|
||||||
|
for(uint8_t i=0; i<len_axis; i++)
|
||||||
|
{
|
||||||
|
if(val < axis_values[i]) return i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return len_axis;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::index2d_to_index1d(uint8_t ix, uint8_t iy, uint8_t len_x)
|
||||||
|
{
|
||||||
|
return ((uint16_t)len_x * iy) + ix;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
23
firmware/src/utils/interpolate.h
Normal file
23
firmware/src/utils/interpolate.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef UTILS_INTERPOLATE_H_
|
||||||
|
#define UTILS_INTERPOLATE_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
uint16_t interpolate_1d(uint16_t x, uint16_t* x_axis, uint16_t* y_values, uint8_t len_axis);
|
||||||
|
uint16_t interpolate_2d(uint16_t x, uint16_t y, uint16_t* x_axis, uint8_t len_x_axis, uint16_t* y_axis, uint8_t len_y_axis, uint16_t* z_values);
|
||||||
|
|
||||||
|
uint8_t find_interval_end_index(uint16_t val, uint16_t* axis_values, uint8_t len_axis);
|
||||||
|
uint16_t interpolate(uint16_t x, uint16_t x0, uint16_t x1, uint16_t y0, uint16_t y1);
|
||||||
|
uint16_t index2d_to_index1d(uint8_t ix, uint8_t iy, uint8_t len_x);
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* UTILS_INTERPOLATE_H_ */
|
||||||
164
firmware/src/utils/utils.cpp
Normal file
164
firmware/src/utils/utils.cpp
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace util;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
#ifndef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
uint8_t util::invert(uint8_t x)
|
||||||
|
{
|
||||||
|
if(x!=0) return 0;
|
||||||
|
else return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t util::sat_add(uint8_t x, uint8_t y)
|
||||||
|
{
|
||||||
|
uint8_t z = x + y;
|
||||||
|
// Check for overflow
|
||||||
|
if((z < x)||(z < y)) return 0xFF;
|
||||||
|
else return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::sat_add(uint16_t x, uint16_t y)
|
||||||
|
{
|
||||||
|
uint16_t z = x + y;
|
||||||
|
// Check for overflow
|
||||||
|
if((z < x)||(z < y)) return 0xFF;
|
||||||
|
else return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t util::sat_add(uint32_t x, uint32_t y)
|
||||||
|
|
||||||
|
{
|
||||||
|
uint32_t z = x + y;
|
||||||
|
// Check for overflow
|
||||||
|
if((z < x)||(z < y)) return 0xFF;
|
||||||
|
else return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t util::sat_subtract(uint8_t x, uint8_t y)
|
||||||
|
{
|
||||||
|
uint8_t z = x - y;
|
||||||
|
// Check for underflow
|
||||||
|
if(z > x) return 0;
|
||||||
|
else return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::sat_subtract(uint16_t x, uint16_t y)
|
||||||
|
{
|
||||||
|
uint16_t z = x - y;
|
||||||
|
// Check for underflow
|
||||||
|
if(z > x) return 0;
|
||||||
|
else return z;
|
||||||
|
}
|
||||||
|
uint32_t util::sat_subtract(uint32_t x, uint32_t y)
|
||||||
|
{
|
||||||
|
uint32_t z = x - y;
|
||||||
|
// Check for underflow
|
||||||
|
if(z > x) return 0;
|
||||||
|
else return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t util::abs_subtract(uint8_t x, uint8_t y)
|
||||||
|
{
|
||||||
|
if(x > y) return x - y;
|
||||||
|
else return y-x;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::abs_subtract(uint16_t x, uint16_t y)
|
||||||
|
{
|
||||||
|
if(x > y) return x - y;
|
||||||
|
else return y-x;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t util::abs_subtract(uint32_t x, uint32_t y)
|
||||||
|
{
|
||||||
|
if(x > y) return x - y;
|
||||||
|
else return y-x;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint16_t util::sat_cast(uint32_t x)
|
||||||
|
{
|
||||||
|
if(x > 0x0000FFFF) return 0xFFFF;
|
||||||
|
else return (uint16_t)x;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::sat_cast(int32_t x)
|
||||||
|
{
|
||||||
|
if(x < 0) return 0x0000;
|
||||||
|
else if(x > 0x0000FFFF) return 0xFFFF;
|
||||||
|
else return (uint16_t)x;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::convert_muldivoff(uint16_t raw, uint8_t mul, uint8_t div, int16_t offset)
|
||||||
|
{
|
||||||
|
int32_t temp = (int32_t)raw;
|
||||||
|
|
||||||
|
temp = temp * mul;
|
||||||
|
if(div>1) temp /= div;
|
||||||
|
temp += offset;
|
||||||
|
|
||||||
|
return sat_cast(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::sat_mul_kilo(uint16_t xk, uint16_t yk)
|
||||||
|
{
|
||||||
|
uint32_t temp = (uint32_t)xk * (uint32_t)yk;
|
||||||
|
temp /= 1000;
|
||||||
|
|
||||||
|
return sat_cast(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::sat_div_kilo(uint16_t top, uint16_t bot)
|
||||||
|
{
|
||||||
|
//Sanity check bot
|
||||||
|
if(bot==0) return 0xFFFF; //aka infinity
|
||||||
|
|
||||||
|
uint32_t temp = (uint32_t)top * 1000;
|
||||||
|
temp /= (uint32_t)bot;
|
||||||
|
|
||||||
|
return sat_cast(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::sat_ratio(uint16_t top, uint16_t bot)
|
||||||
|
{
|
||||||
|
//Sanity check bot
|
||||||
|
if(bot==0) return 0xFFFF; //aka infinity
|
||||||
|
|
||||||
|
//Easy option
|
||||||
|
if(top>=bot) return 0xFFFF;
|
||||||
|
|
||||||
|
uint32_t temp = (uint32_t)top * 0x0000FFFF;
|
||||||
|
temp /= (uint32_t)bot;
|
||||||
|
|
||||||
|
return sat_cast(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::percent_to_16b(uint8_t percent)
|
||||||
|
{
|
||||||
|
uint32_t temp = (uint32_t)percent * 0x0000FFFF;
|
||||||
|
temp /= 100;
|
||||||
|
|
||||||
|
// Limit to 16 bits
|
||||||
|
uint16_t pwm = sat_cast(temp);
|
||||||
|
|
||||||
|
return pwm;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t util::percent_of(uint8_t percent, uint16_t value)
|
||||||
|
{
|
||||||
|
if(percent == 0) return 0;
|
||||||
|
else if(percent >= 100) return value;
|
||||||
|
|
||||||
|
uint32_t temp = (uint32_t)value * percent;
|
||||||
|
return temp/100;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
44
firmware/src/utils/utils.h
Normal file
44
firmware/src/utils/utils.h
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#ifndef UTILS_H_
|
||||||
|
#define UTILS_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
/**** Public function declarations ****/
|
||||||
|
uint8_t invert(uint8_t x);
|
||||||
|
|
||||||
|
uint16_t sat_cast(uint32_t x);
|
||||||
|
uint16_t sat_cast(int32_t x);
|
||||||
|
|
||||||
|
uint16_t convert_muldivoff(uint16_t raw, uint8_t mul, uint8_t div, int16_t offset);
|
||||||
|
uint16_t sat_mul_kilo(uint16_t xk, uint16_t yk);
|
||||||
|
uint16_t sat_div_kilo(uint16_t top, uint16_t bot);
|
||||||
|
uint16_t sat_ratio(uint16_t top, uint16_t bot);
|
||||||
|
uint16_t percent_to_16b(uint8_t percent);
|
||||||
|
|
||||||
|
uint16_t percent_of(uint8_t percent, uint16_t value);
|
||||||
|
|
||||||
|
uint8_t sat_add(uint8_t x, uint8_t y);
|
||||||
|
uint16_t sat_add(uint16_t x, uint16_t y);
|
||||||
|
uint32_t sat_add(uint32_t x, uint32_t y);
|
||||||
|
|
||||||
|
uint8_t sat_subtract(uint8_t x, uint8_t y);
|
||||||
|
uint16_t sat_subtract(uint16_t x, uint16_t y);
|
||||||
|
uint32_t sat_subtract(uint32_t x, uint32_t y);
|
||||||
|
|
||||||
|
uint8_t abs_subtract(uint8_t x, uint8_t y);
|
||||||
|
uint16_t abs_subtract(uint16_t x, uint16_t y);
|
||||||
|
uint32_t abs_subtract(uint32_t x, uint32_t y);
|
||||||
|
|
||||||
|
uint16_t interpolate_1d(uint16_t x, uint16_t* x_axis, uint16_t* y_values, uint8_t len_axis);
|
||||||
|
uint16_t interpolate_2d(uint16_t x, uint16_t y, uint16_t* x_axis, uint8_t len_x_axis, uint16_t* y_axis, uint8_t len_y_axis, uint16_t* z_values);
|
||||||
|
|
||||||
|
#ifdef TESTING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
|
||||||
|
#endif /* UTILS_H_ */
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
|
||||||
<CodeBlocks_workspace_file>
|
|
||||||
<Workspace title="Workspace">
|
|
||||||
<Project filename="uDCCD_Unit_Tests_BSP.cbp" />
|
|
||||||
<Project filename="uDCCD_Unit_Tests_HW.cbp" />
|
|
||||||
<Project filename="uDCCD_Unit_Tests_Logic.cbp" />
|
|
||||||
</Workspace>
|
|
||||||
</CodeBlocks_workspace_file>
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
|
||||||
<CodeBlocks_workspace_layout_file>
|
|
||||||
<FileVersion major="1" minor="0" />
|
|
||||||
<ActiveProject path="uDCCD_Unit_Tests_Logic.cbp" />
|
|
||||||
<PreferredTarget name="Debug" />
|
|
||||||
</CodeBlocks_workspace_layout_file>
|
|
||||||
@@ -1,147 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "ut_utils/ut_utils.h"
|
|
||||||
#include "ut_utils/ut_fault.h"
|
|
||||||
#include "ut_utils/ut_fuses.h"
|
|
||||||
|
|
||||||
#include "ut_board/ut_ain.h"
|
|
||||||
#include "ut_board/ut_din.h"
|
|
||||||
#include "ut_board/ut_dout.h"
|
|
||||||
#include "ut_board/ut_halfbridge.h"
|
|
||||||
#include "ut_board/ut_odout.h"
|
|
||||||
#include "ut_board/ut_setup.h"
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int tr;
|
|
||||||
int pass = 1;
|
|
||||||
|
|
||||||
/* UTILITIES TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_util_invert_8b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_sat_add_8b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_sat_subtract_8b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_sat_util_sat_add_16b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_sat_subtract_16b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_limit_u32b_to_u16b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_limit_s32b_to_u16b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_convert_muldivoff_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_sat_mul_kilo_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_sat_div_kilo_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_sat_ratio_16b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_percent_to_16b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
/* FAULT TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_fault_process_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_fault_is_active_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_fault_is_warning_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_fault_reset_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
/* FUSE TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_fuse_reset_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
/* AIN TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_bsp_ain_read_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
/* DIN TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_bsp_din_read_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
/* DOUT TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_bsp_dout_write_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
/* HALFBRIDGE TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_bsp_hb_write_low_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_bsp_hb_write_pwm_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_bsp_hb_read_meas_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
/* ODOUT TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_bsp_odout_write_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_bsp_odout_write_common_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
/* SETUP TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_bsp_startup_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
/* FINAL RESULT */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_util_interpolate_1d_u16b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
tr = ut_util_interpolate_2d_u16b_test();
|
|
||||||
if(!tr) pass = 0;
|
|
||||||
|
|
||||||
|
|
||||||
if(pass)
|
|
||||||
{
|
|
||||||
printf("ALL PASS\n\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("SOME FAIL\n\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "ut_hw/ut_analog.h"
|
|
||||||
#include "ut_hw/ut_buttons.h"
|
|
||||||
#include "ut_hw/ut_hb_control.h"
|
|
||||||
#include "ut_hw/ut_led_display.h"
|
|
||||||
#include "ut_hw/ut_startup.h"
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int tr;
|
|
||||||
int pass = 1;
|
|
||||||
int t_cnt = 0;
|
|
||||||
int f_cnt = 0;
|
|
||||||
|
|
||||||
/* ANALOG TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_analog_ch_get_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
/* BUTTON TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_btn_reset_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_btn_process_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_btn_ch_process_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_btn_ch_set_pull_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
/* HALFBRIDGE CONTROL TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_hb_is_equal_fb_struct_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_hb_is_equal_ctrl_struct_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_hb_init_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_hb_enable_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_hb_disable_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_hb_process_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
/* DISPLAY TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_led_dsp_set_image_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_led_dsp_backligth_set_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
/* STARTUP TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_hw_startup_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
printf("******************************************************\n");
|
|
||||||
printf("\n%d TESTS DONE\n", t_cnt);
|
|
||||||
if(pass)
|
|
||||||
{
|
|
||||||
printf("ALL PASS\n\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("%d FAILED\n\n", f_cnt);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "ut_logic/ut_coil.h"
|
|
||||||
#include "ut_logic/ut_display.h"
|
|
||||||
#include "ut_logic/ut_force.h"
|
|
||||||
#include "ut_logic/ut_pot.h"
|
|
||||||
#include "ut_logic/ut_user_force.h"
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int tr;
|
|
||||||
int pass = 1;
|
|
||||||
int t_cnt = 0;
|
|
||||||
int f_cnt = 0;
|
|
||||||
|
|
||||||
/* COIL TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_coil_target_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
/* DISPLAY TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_dsp_init_ctrl_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_dsp_set_lock_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_dsp_reset_lock_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_dsp_img_percent_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_dsp_img_raw_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_dsp_get_act_img_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_dsp_backlight_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
/* FORCE TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_force_cycle_bmode_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_force_next_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
/* POT TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_pot_mv_to_percent_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
/* USER FORCE TESTS */
|
|
||||||
printf("******************************************************\n");
|
|
||||||
|
|
||||||
tr = ut_user_force_btn_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
tr = ut_user_force_pot_test();
|
|
||||||
t_cnt++;
|
|
||||||
if(!tr){ pass = 0; f_cnt++;};
|
|
||||||
|
|
||||||
printf("******************************************************\n");
|
|
||||||
printf("\n%d TESTS DONE\n", t_cnt);
|
|
||||||
if(pass)
|
|
||||||
{
|
|
||||||
printf("ALL PASS\n\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("%d FAILED\n\n", f_cnt);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#include "mock_board_ain.h"
|
|
||||||
|
|
||||||
#define AIN_CH_CNT 5
|
|
||||||
static uint8_t ain_ch = 0;
|
|
||||||
static uint16_t ain_data[AIN_CH_CNT+1];
|
|
||||||
|
|
||||||
uint16_t bsp_ain_read(uint8_t ch)
|
|
||||||
{
|
|
||||||
if(ch > AIN_CH_CNT) return 0;
|
|
||||||
ain_ch = ch;
|
|
||||||
return ain_data[ch];
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t mock_board_ain_read_ch(void)
|
|
||||||
{
|
|
||||||
return ain_ch;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mock_board_ain_write_ch(uint8_t ch)
|
|
||||||
{
|
|
||||||
ain_ch = ch;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t mock_board_ain_read_data(uint8_t ch)
|
|
||||||
{
|
|
||||||
return ain_data[ch];
|
|
||||||
}
|
|
||||||
|
|
||||||
void mock_board_ain_write_data(uint8_t ch, uint16_t value)
|
|
||||||
{
|
|
||||||
ain_data[ch] = value;
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#ifndef MOCK_BOARD_AIN_
|
|
||||||
#define MOCK_BOARD_AIN_
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "..\..\src\hw\board\ain.h"
|
|
||||||
|
|
||||||
uint8_t mock_board_ain_read_ch(void);
|
|
||||||
void mock_board_ain_write_ch(uint8_t ch);
|
|
||||||
uint16_t mock_board_ain_read_data(uint8_t ch);
|
|
||||||
void mock_board_ain_write_data(uint8_t ch, uint16_t value);
|
|
||||||
|
|
||||||
#endif /* MOCK_BOARD_AIN_ */
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#include "mock_board_din.h"
|
|
||||||
|
|
||||||
#define DIN_CH_CNT 9
|
|
||||||
static uint8_t din_ch = 0;
|
|
||||||
static uint8_t din_data[DIN_CH_CNT];
|
|
||||||
|
|
||||||
uint8_t bsp_din_read(uint8_t ch)
|
|
||||||
{
|
|
||||||
if(ch >= DIN_CH_CNT) return BSP_DIN_LOW;
|
|
||||||
din_ch = ch;
|
|
||||||
return din_data[ch];
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t mock_board_din_read_ch(void)
|
|
||||||
{
|
|
||||||
return din_ch;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mock_board_din_write_ch(uint8_t ch)
|
|
||||||
{
|
|
||||||
din_ch = ch;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t mock_board_din_read_data(uint8_t ch)
|
|
||||||
{
|
|
||||||
return din_data[ch];
|
|
||||||
}
|
|
||||||
|
|
||||||
void mock_board_din_write_data(uint8_t ch, uint8_t value)
|
|
||||||
{
|
|
||||||
din_data[ch] = value;
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#ifndef MOCK_BOARD_DIN_
|
|
||||||
#define MOCK_BOARD_DIN_
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "..\..\src\hw\board\din.h"
|
|
||||||
|
|
||||||
uint8_t mock_board_din_read_ch(void);
|
|
||||||
void mock_board_din_write_ch(uint8_t ch);
|
|
||||||
uint8_t mock_board_din_read_data(uint8_t ch);
|
|
||||||
void mock_board_din_write_data(uint8_t ch, uint8_t value);
|
|
||||||
|
|
||||||
#endif /* MOCK_BOARD_DIN_ */
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
#include "mock_board_dout.h"
|
|
||||||
|
|
||||||
#define DOUT_CH_CNT 7
|
|
||||||
static uint8_t dout_ch = 0;
|
|
||||||
static int8_t dout_data[DOUT_CH_CNT];
|
|
||||||
|
|
||||||
void bsp_dout_write(uint8_t ch, int8_t lvl)
|
|
||||||
{
|
|
||||||
if(ch < DOUT_CH_CNT) dout_data[ch] = lvl;
|
|
||||||
dout_ch = ch;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t mock_board_dout_read_ch(void)
|
|
||||||
{
|
|
||||||
return dout_ch;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mock_board_dout_write_ch(uint8_t ch)
|
|
||||||
{
|
|
||||||
dout_ch = ch;
|
|
||||||
}
|
|
||||||
|
|
||||||
int8_t mock_board_dout_read_data(uint8_t ch)
|
|
||||||
{
|
|
||||||
return dout_data[ch];
|
|
||||||
}
|
|
||||||
|
|
||||||
void mock_board_dout_write_data(uint8_t ch, int8_t lvl)
|
|
||||||
{
|
|
||||||
dout_data[ch] = lvl;
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#ifndef MOCK_BOARD_DOUT_
|
|
||||||
#define MOCK_BOARD_DOUT_
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "..\..\src\hw\board\dout.h"
|
|
||||||
|
|
||||||
uint8_t mock_board_dout_read_ch(void);
|
|
||||||
void mock_board_dout_write_ch(uint8_t ch);
|
|
||||||
int8_t mock_board_dout_read_data(uint8_t ch);
|
|
||||||
void mock_board_dout_write_data(uint8_t ch, int8_t lvl);
|
|
||||||
|
|
||||||
#endif /* MOCK_BOARD_DOUT_ */
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
#include "mock_board_halfbridge.h"
|
|
||||||
|
|
||||||
#define DOUT_CH_CNT 6
|
|
||||||
static uint8_t low_ctrl = 0;
|
|
||||||
static uint16_t pwm_ctrl = 0;
|
|
||||||
static hb_meas_t feedback;
|
|
||||||
|
|
||||||
void bsp_hb_write_low(uint8_t state)
|
|
||||||
{
|
|
||||||
low_ctrl = state;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bsp_hb_write_pwm(uint16_t pwm)
|
|
||||||
{
|
|
||||||
pwm_ctrl = pwm;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bsp_hb_read_meas(hb_meas_t* measurements)
|
|
||||||
{
|
|
||||||
measurements->out_voltage = feedback.out_voltage;
|
|
||||||
measurements->out_current = feedback.out_current;
|
|
||||||
measurements->sup_voltage = feedback.sup_voltage;
|
|
||||||
measurements->sup_current = feedback.sup_current;
|
|
||||||
measurements->out_power = feedback.out_power;
|
|
||||||
measurements->sup_power = feedback.sup_power;
|
|
||||||
measurements->out_impedance = feedback.out_impedance;
|
|
||||||
measurements->low_side_ctrl = feedback.low_side_ctrl;
|
|
||||||
measurements->pwm = feedback.pwm;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
uint8_t mock_board_halfbridge_read_low(void)
|
|
||||||
{
|
|
||||||
return low_ctrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mock_board_halfbridge_write_low(uint8_t state)
|
|
||||||
{
|
|
||||||
low_ctrl = state;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t mock_board_halfbridge_read_pwm(void)
|
|
||||||
{
|
|
||||||
return pwm_ctrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mock_board_halfbridge_write_pwm(uint16_t pwm)
|
|
||||||
{
|
|
||||||
pwm_ctrl = pwm;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mock_board_halfbridge_read_feedback(hb_meas_t* measurements)
|
|
||||||
{
|
|
||||||
measurements->out_voltage = feedback.out_voltage;
|
|
||||||
measurements->out_current = feedback.out_current;
|
|
||||||
measurements->sup_voltage = feedback.sup_voltage;
|
|
||||||
measurements->sup_current = feedback.sup_current;
|
|
||||||
measurements->out_power = feedback.out_power;
|
|
||||||
measurements->sup_power = feedback.sup_power;
|
|
||||||
measurements->out_impedance = feedback.out_impedance;
|
|
||||||
measurements->low_side_ctrl = feedback.low_side_ctrl;
|
|
||||||
measurements->pwm = feedback.pwm;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mock_board_halfbridge_write_feedback(hb_meas_t* new_fb)
|
|
||||||
{
|
|
||||||
feedback.out_voltage = new_fb->out_voltage;
|
|
||||||
feedback.out_current = new_fb->out_current;
|
|
||||||
feedback.sup_voltage = new_fb->sup_voltage;
|
|
||||||
feedback.sup_current = new_fb->sup_current;
|
|
||||||
feedback.out_power = new_fb->out_power;
|
|
||||||
feedback.sup_power = new_fb->sup_power;
|
|
||||||
feedback.out_impedance = new_fb->out_impedance;
|
|
||||||
feedback.low_side_ctrl = new_fb->low_side_ctrl;
|
|
||||||
feedback.pwm = new_fb->pwm;
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
#ifndef MOCK_BOARD_HALFBRIDGE_
|
|
||||||
#define MOCK_BOARD_HALFBRIDGE_
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "..\..\src\hw\board\halfbridge.h"
|
|
||||||
|
|
||||||
uint8_t mock_board_halfbridge_read_low(void);
|
|
||||||
void mock_board_halfbridge_write_low(uint8_t state);
|
|
||||||
|
|
||||||
uint16_t mock_board_halfbridge_read_pwm(void);
|
|
||||||
void mock_board_halfbridge_write_pwm(uint16_t pwm);
|
|
||||||
|
|
||||||
void mock_board_halfbridge_read_feedback(hb_meas_t* measurements);
|
|
||||||
void mock_board_halfbridge_write_feedback(hb_meas_t* new_fb);
|
|
||||||
|
|
||||||
#endif /* MOCK_BOARD_HALFBRIDGE_ */
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user