Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bdd94d53a1 | |||
| ed469cb213 | |||
| 5271553ac6 |
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_ */
|
||||||
435
firmware/src/bsp/mcu/mcu_hal.cpp
Normal file
435
firmware/src/bsp/mcu/mcu_hal.cpp
Normal file
@@ -0,0 +1,435 @@
|
|||||||
|
/**** Includes ****/
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/eeprom.h>
|
||||||
|
#include "mcu_hal.h"
|
||||||
|
|
||||||
|
using namespace mcu;
|
||||||
|
|
||||||
|
/**** Private definitions ****/
|
||||||
|
/**** Private constants ****/
|
||||||
|
/**** Private variables ****/
|
||||||
|
/**** Private function declarations ****/
|
||||||
|
static uint8_t gpio_read_level(uint8_t pin_reg, uint8_t mask);
|
||||||
|
|
||||||
|
/**** Public function definitions ****/
|
||||||
|
void mcu::startup(startupCfg_t* hwCfg)
|
||||||
|
{
|
||||||
|
// Fail-safe GPIO init
|
||||||
|
PORTB = 0x00; // Reset PORTB pull-ups
|
||||||
|
DDRB = 0x00; // Set all as inputs
|
||||||
|
|
||||||
|
PORTC = 0x40; // Reset PORTC pull-ups, except reset
|
||||||
|
DDRC = 0x00; // Set all as inputs
|
||||||
|
|
||||||
|
PORTD = 0x00; // Reset PORTD pull-ups
|
||||||
|
DDRD = 0x00; // Set all as inputs
|
||||||
|
|
||||||
|
PORTE = 0x00; // Reset PORTE pull-ups
|
||||||
|
DDRE = 0x00; // Set all as inputs
|
||||||
|
|
||||||
|
// Pull-x set all HiZ
|
||||||
|
PORTB &= ~0xFF; // Reset pull-ups
|
||||||
|
DDRB &= ~0xFF; // Set all as inputs
|
||||||
|
|
||||||
|
// Inputs set all as HiZ inputs
|
||||||
|
PORTC &= ~0x3F; // Reset pull-ups
|
||||||
|
DDRC &= ~0x3F; // Set all as inputs
|
||||||
|
|
||||||
|
PORTE &= ~0x0C; // Reset pull-ups
|
||||||
|
DDRE &= ~0x0C; // Set all as inputs
|
||||||
|
|
||||||
|
// Outputs set all low
|
||||||
|
PORTD &= ~0xFF; // Reset all low
|
||||||
|
DDRD |= 0xFF; // Set all as outputs
|
||||||
|
|
||||||
|
// Ignition input
|
||||||
|
PORTE |= 0x01; // Set pull-up
|
||||||
|
DDRE &= ~0x01; // Set all as input
|
||||||
|
|
||||||
|
// 5V switched control
|
||||||
|
PORTE &= ~0x02; // Set low
|
||||||
|
DDRE |= 0x02; // Set all as output
|
||||||
|
|
||||||
|
//ADC configuration
|
||||||
|
PRR0 &= ~0x01; //Enable ADC power
|
||||||
|
DIDR0 &= ~0x00; //Enable digital inputs
|
||||||
|
|
||||||
|
ADMUX = 0x40; //Set AVCC reference, Right adjust
|
||||||
|
ADCSRA = 0x00; //ADC Disabled, Single conversion, no IT
|
||||||
|
ADCSRA |= (uint8_t)hwCfg->adc_clk;
|
||||||
|
ADCSRB = 0x00; //no trigger input
|
||||||
|
|
||||||
|
ADCSRA |= 0x80; //Enable ADC
|
||||||
|
}
|
||||||
|
|
||||||
|
// ADC Interface functions
|
||||||
|
uint16_t mcu::adc_read(uint8_t ch)
|
||||||
|
{
|
||||||
|
//check if ADC is enabled
|
||||||
|
if(!(ADCSRA&0x80)) return 0xFFFF;
|
||||||
|
|
||||||
|
//Safe guard mux
|
||||||
|
if(ch > 15) return 0xFFFF;
|
||||||
|
// Not available channels
|
||||||
|
if((ch > 8) && (ch<14)) return 0xFFFF;
|
||||||
|
|
||||||
|
ADMUX &= ~0x0F;
|
||||||
|
ADMUX |= ch;
|
||||||
|
ADCSRA |= 0x40;
|
||||||
|
while(ADCSRA&0x40); //wait to finish
|
||||||
|
return ADC;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GPIO functions
|
||||||
|
uint8_t mcu::gpio_read(uint8_t ch)
|
||||||
|
{
|
||||||
|
switch(ch)
|
||||||
|
{
|
||||||
|
case PIN_PB0:
|
||||||
|
return gpio_read_level(PINB,0x01);
|
||||||
|
|
||||||
|
case PIN_PB1:
|
||||||
|
return gpio_read_level(PINB,0x02);
|
||||||
|
|
||||||
|
case PIN_PB2:
|
||||||
|
return gpio_read_level(PINB,0x04);
|
||||||
|
|
||||||
|
case PIN_PB3:
|
||||||
|
return gpio_read_level(PINB,0x08);
|
||||||
|
|
||||||
|
case PIN_PB4:
|
||||||
|
return gpio_read_level(PINB,0x10);
|
||||||
|
|
||||||
|
case PIN_PB5:
|
||||||
|
return gpio_read_level(PINB,0x20);
|
||||||
|
|
||||||
|
case PIN_PB6:
|
||||||
|
return gpio_read_level(PINB,0x40);
|
||||||
|
|
||||||
|
case PIN_PB7:
|
||||||
|
return gpio_read_level(PINB,0x80);
|
||||||
|
|
||||||
|
case PIN_PC0:
|
||||||
|
return gpio_read_level(PINC,0x01);
|
||||||
|
|
||||||
|
case PIN_PC1:
|
||||||
|
return gpio_read_level(PINC,0x02);
|
||||||
|
|
||||||
|
case PIN_PC2:
|
||||||
|
return gpio_read_level(PINC,0x04);
|
||||||
|
|
||||||
|
case PIN_PC3:
|
||||||
|
return gpio_read_level(PINC,0x08);
|
||||||
|
|
||||||
|
case PIN_PC4:
|
||||||
|
return gpio_read_level(PINC,0x10);
|
||||||
|
|
||||||
|
case PIN_PC5:
|
||||||
|
return gpio_read_level(PINC,0x20);
|
||||||
|
|
||||||
|
case PIN_PD0:
|
||||||
|
return gpio_read_level(PIND,0x01);
|
||||||
|
|
||||||
|
case PIN_PD1:
|
||||||
|
return gpio_read_level(PIND,0x02);
|
||||||
|
|
||||||
|
case PIN_PD2:
|
||||||
|
return gpio_read_level(PIND,0x04);
|
||||||
|
|
||||||
|
case PIN_PD3:
|
||||||
|
return gpio_read_level(PIND,0x08);
|
||||||
|
|
||||||
|
case PIN_PD4:
|
||||||
|
return gpio_read_level(PIND,0x10);
|
||||||
|
|
||||||
|
case PIN_PD5:
|
||||||
|
return gpio_read_level(PIND,0x20);
|
||||||
|
|
||||||
|
case PIN_PD6:
|
||||||
|
return gpio_read_level(PIND,0x40);
|
||||||
|
|
||||||
|
case PIN_PD7:
|
||||||
|
return gpio_read_level(PIND,0x80);
|
||||||
|
|
||||||
|
case PIN_PE0:
|
||||||
|
return gpio_read_level(PINE,0x01);
|
||||||
|
|
||||||
|
case PIN_PE1:
|
||||||
|
return gpio_read_level(PINE,0x02);
|
||||||
|
|
||||||
|
case PIN_PE2:
|
||||||
|
return gpio_read_level(PINE,0x04);
|
||||||
|
|
||||||
|
case PIN_PE3:
|
||||||
|
return gpio_read_level(PINE,0x08);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return LEVEL_LOW;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void mcu::gpio_write(uint8_t ch, int8_t lvl)
|
||||||
|
{
|
||||||
|
switch(ch)
|
||||||
|
{
|
||||||
|
case PIN_PB0:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTB |= 0x01;
|
||||||
|
DDRB |= 0x01;
|
||||||
|
}
|
||||||
|
else if(lvl<0){
|
||||||
|
DDRB &= ~0x01;
|
||||||
|
PORTB &= ~0x01;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTB &= ~0x01;
|
||||||
|
DDRB |= 0x01;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PB1:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTB |= 0x02;
|
||||||
|
DDRB |= 0x02;
|
||||||
|
}
|
||||||
|
else if(lvl<0){
|
||||||
|
DDRB &= ~0x02;
|
||||||
|
PORTB &= ~0x02;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTB &= ~0x02;
|
||||||
|
DDRB |= 0x02;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PB2:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTB |= 0x04;
|
||||||
|
DDRB |= 0x04;
|
||||||
|
}
|
||||||
|
else if(lvl<0){
|
||||||
|
DDRB &= ~0x04;
|
||||||
|
PORTB &= ~0x04;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTB &= ~0x04;
|
||||||
|
DDRB |= 0x04;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PB3:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTB |= 0x08;
|
||||||
|
DDRB |= 0x08;
|
||||||
|
}
|
||||||
|
else if(lvl<0){
|
||||||
|
DDRB &= ~0x08;
|
||||||
|
PORTB &= ~0x08;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTB &= ~0x08;
|
||||||
|
DDRB |= 0x08;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PB4:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTB |= 0x10;
|
||||||
|
DDRB |= 0x10;
|
||||||
|
}
|
||||||
|
else if(lvl<0){
|
||||||
|
DDRB &= ~0x10;
|
||||||
|
PORTB &= ~0x10;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTB &= ~0x10;
|
||||||
|
DDRB |= 0x10;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PB5:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTB |= 0x20;
|
||||||
|
DDRB |= 0x20;
|
||||||
|
}
|
||||||
|
else if(lvl<0){
|
||||||
|
DDRB &= ~0x20;
|
||||||
|
PORTB &= ~0x20;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTB &= ~0x20;
|
||||||
|
DDRB |= 0x20;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PB6:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTB |= 0x40;
|
||||||
|
DDRB |= 0x40;
|
||||||
|
}
|
||||||
|
else if(lvl<0){
|
||||||
|
DDRB &= ~0x40;
|
||||||
|
PORTB &= ~0x40;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTB &= ~0x40;
|
||||||
|
DDRB |= 0x40;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PB7:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTB |= 0x80;
|
||||||
|
DDRB |= 0x80;
|
||||||
|
}
|
||||||
|
else if(lvl<0){
|
||||||
|
DDRB &= ~0x80;
|
||||||
|
PORTB &= ~0x80;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTB &= ~0x80;
|
||||||
|
DDRB |= 0x80;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PD0:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTD |= 0x01;
|
||||||
|
DDRD |= 0x01;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTD &= ~0x01;
|
||||||
|
DDRD |= 0x01;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PD1:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTD |= 0x02;
|
||||||
|
DDRD |= 0x02;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTD &= ~0x02;
|
||||||
|
DDRD |= 0x02;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PD2:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTD |= 0x04;
|
||||||
|
DDRD |= 0x04;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTD &= ~0x04;
|
||||||
|
DDRD |= 0x04;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PD3:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTD |= 0x08;
|
||||||
|
DDRD |= 0x08;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTD &= ~0x08;
|
||||||
|
DDRD |= 0x08;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PD4:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTD |= 0x10;
|
||||||
|
DDRD |= 0x10;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTD &= ~0x10;
|
||||||
|
DDRD |= 0x10;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PD5:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTD |= 0x20;
|
||||||
|
DDRD |= 0x20;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTD &= ~0x20;
|
||||||
|
DDRD |= 0x20;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PD6:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTD |= 0x40;
|
||||||
|
DDRD |= 0x40;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTD &= ~0x40;
|
||||||
|
DDRD |= 0x40;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PD7:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTD |= 0x80;
|
||||||
|
DDRD |= 0x80;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTD &= ~0x80;
|
||||||
|
DDRD |= 0x80;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PIN_PE1:
|
||||||
|
if(lvl>0){
|
||||||
|
PORTE |= 0x02;
|
||||||
|
DDRE |= 0x02;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
PORTE &= ~0x02;
|
||||||
|
DDRE |= 0x02;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t mcu::eeprom_read8b(uint16_t address)
|
||||||
|
{
|
||||||
|
return eeprom_read_byte((uint8_t*)address);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t mcu::eeprom_read16b(uint16_t address)
|
||||||
|
{
|
||||||
|
return eeprom_read_word((uint16_t*)address);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t mcu::eeprom_read32b(uint16_t address)
|
||||||
|
{
|
||||||
|
return eeprom_read_dword((uint32_t*)address);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mcu::eeprom_write8b(uint16_t address, uint8_t value)
|
||||||
|
{
|
||||||
|
eeprom_write_byte((uint8_t*)address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mcu::eeprom_write16b(uint16_t address, uint16_t value)
|
||||||
|
{
|
||||||
|
eeprom_write_word((uint16_t*)address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mcu::eeprom_write32b(uint16_t address, uint32_t value)
|
||||||
|
{
|
||||||
|
eeprom_write_dword((uint32_t*)address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**** Private function definitions ****/
|
||||||
|
static uint8_t gpio_read_level(uint8_t pin_reg, uint8_t mask)
|
||||||
|
{
|
||||||
|
if(pin_reg&mask) return LEVEL_HIGH;
|
||||||
|
else return LEVEL_LOW;
|
||||||
|
}
|
||||||
|
|
||||||
101
firmware/src/bsp/mcu/mcu_hal.h
Normal file
101
firmware/src/bsp/mcu/mcu_hal.h
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
#ifndef MCU_HAL_H_
|
||||||
|
#define MCU_HAL_H_
|
||||||
|
|
||||||
|
/**** Includes ****/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace mcu {
|
||||||
|
|
||||||
|
/**** Public definitions ****/
|
||||||
|
/*
|
||||||
|
*/
|
||||||
|
|
||||||
|
const uint8_t PIN_PB0 = 1; // Pull-x 2
|
||||||
|
const uint8_t PIN_PB1 = 2; // Pull-x 7
|
||||||
|
const uint8_t PIN_PB2 = 3; // Pull-x 3
|
||||||
|
const uint8_t PIN_PB3 = 4; // Pull-x 8
|
||||||
|
const uint8_t PIN_PB4 = 5; // Pull-x 4
|
||||||
|
const uint8_t PIN_PB5 = 6; // Pull-x 5
|
||||||
|
const uint8_t PIN_PB6 = 7; // Pull-x 1
|
||||||
|
const uint8_t PIN_PB7 = 8; // Pull-x 6
|
||||||
|
|
||||||
|
const uint8_t PIN_PC0 = 9; // Input 2
|
||||||
|
const uint8_t PIN_PC1 = 10; // Input 7
|
||||||
|
const uint8_t PIN_PC2 = 11; // Input 3
|
||||||
|
const uint8_t PIN_PC3 = 12; // Input 8
|
||||||
|
const uint8_t PIN_PC4 = 13; // Input 4
|
||||||
|
const uint8_t PIN_PC5 = 14; // Input 5
|
||||||
|
|
||||||
|
const uint8_t PIN_PD0 = 15; // Output 5
|
||||||
|
const uint8_t PIN_PD1 = 16; // Output 4
|
||||||
|
const uint8_t PIN_PD2 = 17; // Output 6
|
||||||
|
const uint8_t PIN_PD3 = 18; // Output 1
|
||||||
|
const uint8_t PIN_PD4 = 19; // Output 7
|
||||||
|
const uint8_t PIN_PD5 = 20; // Output 2
|
||||||
|
const uint8_t PIN_PD6 = 21; // Output 8
|
||||||
|
const uint8_t PIN_PD7 = 22; // Output 3
|
||||||
|
|
||||||
|
const uint8_t PIN_PE0 = 23; // Ignition input
|
||||||
|
const uint8_t PIN_PE1 = 24; // 5V control
|
||||||
|
const uint8_t PIN_PE2 = 25; // Input 1
|
||||||
|
const uint8_t PIN_PE3 = 26; // Input 6
|
||||||
|
|
||||||
|
const uint8_t LEVEL_LOW = 0;
|
||||||
|
const uint8_t LEVEL_HIGH = 1;
|
||||||
|
const int8_t LEVEL_HIZ = -1;
|
||||||
|
|
||||||
|
const uint8_t ADC0 = 0; // Input 1
|
||||||
|
const uint8_t ADC1 = 1; // Input 2
|
||||||
|
const uint8_t ADC2 = 2; // Input 3
|
||||||
|
const uint8_t ADC3 = 3; // Input 4
|
||||||
|
const uint8_t ADC4 = 4; // Input 5
|
||||||
|
const uint8_t ADC5 = 5; // Input 6
|
||||||
|
const uint8_t ADC6 = 6; // Input 7
|
||||||
|
const uint8_t ADC7 = 7; // Input 8
|
||||||
|
const uint8_t ADC8 = 8; // MCU temperature
|
||||||
|
const uint8_t ADC14 = 14; // MCU internal reference
|
||||||
|
const uint8_t ADC15 = 15; // MCU ground
|
||||||
|
|
||||||
|
//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;
|
||||||
|
} 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);
|
||||||
|
|
||||||
|
uint16_t adc_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_ */
|
||||||
22
firmware/src/logic-controller.atsln
Normal file
22
firmware/src/logic-controller.atsln
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Atmel Studio Solution File, Format Version 11.00
|
||||||
|
VisualStudioVersion = 14.0.23107.0
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "logic-controller", "logic-controller.cppproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|AVR = Debug|AVR
|
||||||
|
Release|AVR = Release|AVR
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.ActiveCfg = Debug|AVR
|
||||||
|
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.Build.0 = Debug|AVR
|
||||||
|
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.ActiveCfg = Release|AVR
|
||||||
|
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.Build.0 = Release|AVR
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
86
firmware/src/logic-controller.componentinfo.xml
Normal file
86
firmware/src/logic-controller.componentinfo.xml
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Store xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="AtmelPackComponentManagement">
|
||||||
|
<ProjectComponents>
|
||||||
|
<ProjectComponent z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
|
||||||
|
<CApiVersion></CApiVersion>
|
||||||
|
<CBundle></CBundle>
|
||||||
|
<CClass>Device</CClass>
|
||||||
|
<CGroup>Startup</CGroup>
|
||||||
|
<CSub></CSub>
|
||||||
|
<CVariant></CVariant>
|
||||||
|
<CVendor>Atmel</CVendor>
|
||||||
|
<CVersion>1.7.0</CVersion>
|
||||||
|
<DefaultRepoPath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs</DefaultRepoPath>
|
||||||
|
<DependentComponents xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
|
||||||
|
<Description></Description>
|
||||||
|
<Files xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
|
||||||
|
<d4p1:anyType i:type="FileInfo">
|
||||||
|
<AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\include\</AbsolutePath>
|
||||||
|
<Attribute></Attribute>
|
||||||
|
<Category>include</Category>
|
||||||
|
<Condition>C</Condition>
|
||||||
|
<FileContentHash i:nil="true" />
|
||||||
|
<FileVersion></FileVersion>
|
||||||
|
<Name>include/</Name>
|
||||||
|
<SelectString></SelectString>
|
||||||
|
<SourcePath></SourcePath>
|
||||||
|
</d4p1:anyType>
|
||||||
|
<d4p1:anyType i:type="FileInfo">
|
||||||
|
<AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\include\avr\iom328pb.h</AbsolutePath>
|
||||||
|
<Attribute></Attribute>
|
||||||
|
<Category>header</Category>
|
||||||
|
<Condition>C</Condition>
|
||||||
|
<FileContentHash>TU9y07FA4IWGxznrvGv9rQ==</FileContentHash>
|
||||||
|
<FileVersion></FileVersion>
|
||||||
|
<Name>include/avr/iom328pb.h</Name>
|
||||||
|
<SelectString></SelectString>
|
||||||
|
<SourcePath></SourcePath>
|
||||||
|
</d4p1:anyType>
|
||||||
|
<d4p1:anyType i:type="FileInfo">
|
||||||
|
<AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\templates\main.c</AbsolutePath>
|
||||||
|
<Attribute>template</Attribute>
|
||||||
|
<Category>source</Category>
|
||||||
|
<Condition>C Exe</Condition>
|
||||||
|
<FileContentHash>KjvOcFWd++tbnsEMfVPd/w==</FileContentHash>
|
||||||
|
<FileVersion></FileVersion>
|
||||||
|
<Name>templates/main.c</Name>
|
||||||
|
<SelectString>Main file (.c)</SelectString>
|
||||||
|
<SourcePath></SourcePath>
|
||||||
|
</d4p1:anyType>
|
||||||
|
<d4p1:anyType i:type="FileInfo">
|
||||||
|
<AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\templates\main.cpp</AbsolutePath>
|
||||||
|
<Attribute>template</Attribute>
|
||||||
|
<Category>source</Category>
|
||||||
|
<Condition>C Exe</Condition>
|
||||||
|
<FileContentHash>JW7SEx9Orn/ZeEv3xrLKDw==</FileContentHash>
|
||||||
|
<FileVersion></FileVersion>
|
||||||
|
<Name>templates/main.cpp</Name>
|
||||||
|
<SelectString>Main file (.cpp)</SelectString>
|
||||||
|
<SourcePath></SourcePath>
|
||||||
|
</d4p1:anyType>
|
||||||
|
<d4p1:anyType i:type="FileInfo">
|
||||||
|
<AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega328pb</AbsolutePath>
|
||||||
|
<Attribute></Attribute>
|
||||||
|
<Category>libraryPrefix</Category>
|
||||||
|
<Condition>GCC</Condition>
|
||||||
|
<FileContentHash i:nil="true" />
|
||||||
|
<FileVersion></FileVersion>
|
||||||
|
<Name>gcc/dev/atmega328pb</Name>
|
||||||
|
<SelectString></SelectString>
|
||||||
|
<SourcePath></SourcePath>
|
||||||
|
</d4p1:anyType>
|
||||||
|
</Files>
|
||||||
|
<PackName>ATmega_DFP</PackName>
|
||||||
|
<PackPath>C:/Program Files (x86)/Atmel/Studio/7.0/Packs/atmel/ATmega_DFP/1.7.374/Atmel.ATmega_DFP.pdsc</PackPath>
|
||||||
|
<PackVersion>1.7.374</PackVersion>
|
||||||
|
<PresentInProject>true</PresentInProject>
|
||||||
|
<ReferenceConditionId>ATmega328PB</ReferenceConditionId>
|
||||||
|
<RteComponents xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
|
||||||
|
<d4p1:string></d4p1:string>
|
||||||
|
</RteComponents>
|
||||||
|
<Status>Resolved</Status>
|
||||||
|
<VersionMode>Fixed</VersionMode>
|
||||||
|
<IsComponentInAtProject>true</IsComponentInAtProject>
|
||||||
|
</ProjectComponent>
|
||||||
|
</ProjectComponents>
|
||||||
|
</Store>
|
||||||
190
firmware/src/logic-controller.cppproj
Normal file
190
firmware/src/logic-controller.cppproj
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
|
||||||
|
<PropertyGroup>
|
||||||
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
|
<ProjectVersion>7.0</ProjectVersion>
|
||||||
|
<ToolchainName>com.Atmel.AVRGCC8.CPP</ToolchainName>
|
||||||
|
<ProjectGuid>dce6c7e3-ee26-4d79-826b-08594b9ad897</ProjectGuid>
|
||||||
|
<avrdevice>ATmega328PB</avrdevice>
|
||||||
|
<avrdeviceseries>none</avrdeviceseries>
|
||||||
|
<OutputType>Executable</OutputType>
|
||||||
|
<Language>CPP</Language>
|
||||||
|
<OutputFileName>$(MSBuildProjectName)</OutputFileName>
|
||||||
|
<OutputFileExtension>.elf</OutputFileExtension>
|
||||||
|
<OutputDirectory>$(MSBuildProjectDirectory)\$(Configuration)</OutputDirectory>
|
||||||
|
<AssemblyName>logic-controller</AssemblyName>
|
||||||
|
<Name>logic-controller</Name>
|
||||||
|
<RootNamespace>logic-controller</RootNamespace>
|
||||||
|
<ToolchainFlavour>Native</ToolchainFlavour>
|
||||||
|
<KeepTimersRunning>true</KeepTimersRunning>
|
||||||
|
<OverrideVtor>false</OverrideVtor>
|
||||||
|
<CacheFlash>true</CacheFlash>
|
||||||
|
<ProgFlashFromRam>true</ProgFlashFromRam>
|
||||||
|
<RamSnippetAddress />
|
||||||
|
<UncachedRange />
|
||||||
|
<preserveEEPROM>true</preserveEEPROM>
|
||||||
|
<OverrideVtorValue />
|
||||||
|
<BootSegment>2</BootSegment>
|
||||||
|
<ResetRule>0</ResetRule>
|
||||||
|
<eraseonlaunchrule>0</eraseonlaunchrule>
|
||||||
|
<EraseKey />
|
||||||
|
<AsfFrameworkConfig>
|
||||||
|
<framework-data xmlns="">
|
||||||
|
<options />
|
||||||
|
<configurations />
|
||||||
|
<files />
|
||||||
|
<documentation help="" />
|
||||||
|
<offline-documentation help="" />
|
||||||
|
<dependencies>
|
||||||
|
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.52.0" />
|
||||||
|
</dependencies>
|
||||||
|
</framework-data>
|
||||||
|
</AsfFrameworkConfig>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||||
|
<ToolchainSettings>
|
||||||
|
<AvrGccCpp>
|
||||||
|
<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.lss>True</avrgcc.common.outputfiles.lss>
|
||||||
|
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
|
||||||
|
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
|
||||||
|
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
|
||||||
|
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||||
|
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||||
|
<avrgcc.compiler.symbols.DefSymbols>
|
||||||
|
<ListValues>
|
||||||
|
<Value>NDEBUG</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcc.compiler.symbols.DefSymbols>
|
||||||
|
<avrgcc.compiler.directories.IncludePaths>
|
||||||
|
<ListValues>
|
||||||
|
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcc.compiler.directories.IncludePaths>
|
||||||
|
<avrgcc.compiler.optimization.level>Optimize for size (-Os)</avrgcc.compiler.optimization.level>
|
||||||
|
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
||||||
|
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
||||||
|
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
||||||
|
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||||
|
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||||
|
<avrgcccpp.compiler.symbols.DefSymbols>
|
||||||
|
<ListValues>
|
||||||
|
<Value>NDEBUG</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcccpp.compiler.symbols.DefSymbols>
|
||||||
|
<avrgcccpp.compiler.directories.IncludePaths>
|
||||||
|
<ListValues>
|
||||||
|
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcccpp.compiler.directories.IncludePaths>
|
||||||
|
<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>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||||
|
<ToolchainSettings>
|
||||||
|
<AvrGccCpp>
|
||||||
|
<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.lss>True</avrgcc.common.outputfiles.lss>
|
||||||
|
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
|
||||||
|
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
|
||||||
|
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
|
||||||
|
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||||
|
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||||
|
<avrgcc.compiler.symbols.DefSymbols>
|
||||||
|
<ListValues>
|
||||||
|
<Value>DEBUG</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcc.compiler.symbols.DefSymbols>
|
||||||
|
<avrgcc.compiler.directories.IncludePaths>
|
||||||
|
<ListValues>
|
||||||
|
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
||||||
|
</ListValues>
|
||||||
|
</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.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
||||||
|
<avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel>
|
||||||
|
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
||||||
|
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||||
|
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||||
|
<avrgcccpp.compiler.symbols.DefSymbols>
|
||||||
|
<ListValues>
|
||||||
|
<Value>DEBUG</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcccpp.compiler.symbols.DefSymbols>
|
||||||
|
<avrgcccpp.compiler.directories.IncludePaths>
|
||||||
|
<ListValues>
|
||||||
|
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value>
|
||||||
|
</ListValues>
|
||||||
|
</avrgcccpp.compiler.directories.IncludePaths>
|
||||||
|
<avrgcccpp.compiler.optimization.level>Optimize debugging experience (-Og)</avrgcccpp.compiler.optimization.level>
|
||||||
|
<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>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="bsp\ain.cpp">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="bsp\ain.h">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="bsp\mcu\mcu_hal.cpp">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="bsp\mcu\mcu_hal.h">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="main.cpp">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="utils\interpolate.cpp">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="utils\interpolate.h">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="utils\utils.cpp">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="utils\utils.h">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="bsp" />
|
||||||
|
<Folder Include="bsp\mcu" />
|
||||||
|
<Folder Include="utils" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
|
||||||
|
</Project>
|
||||||
18
firmware/src/main.cpp
Normal file
18
firmware/src/main.cpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* logic-controller.cpp
|
||||||
|
*
|
||||||
|
* Created: 28.04.2024 20:03:09
|
||||||
|
* Author : User
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <avr/io.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
/* Replace with your application code */
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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_ */
|
||||||
Reference in New Issue
Block a user