#ifndef MCU_HAL_H_
#define MCU_HAL_H_

/**** Includes ****/
#include <stdint.h>

namespace mcu {

/**** Public definitions ****/
/*
*/
const uint8_t LEVEL_LOW  = 0;
const uint8_t LEVEL_HIGH = 1;
const int8_t  LEVEL_HIZ  = -1;

const uint8_t GPIO_DIN1 = 0;
const uint8_t GPIO_DIN2 = 1;
const uint8_t GPIO_DIN3 = 2;
const uint8_t GPIO_DIN4 = 3;
const uint8_t GPIO_HVDIN1 = 4;
const uint8_t GPIO_HVDIN2 = 5;
const uint8_t GPIO_HVDIN3 = 6;
const uint8_t GPIO_HVDIN3_PULL = 7;
const uint8_t GPIO_OD1 = 8;
const uint8_t GPIO_OD2 = 9;
const uint8_t GPIO_OD3 = 10;
const uint8_t GPIO_OD4 = 11;
const uint8_t GPIO_OD5 = 12;
const uint8_t GPIO_OD6 = 13;
const uint8_t GPIO_OUT_LOW = 14;
const uint8_t GPIO_OUT_HIGH = 15;
const uint8_t GPIO_OD_PWM = 16;
const uint8_t GPIO_FREQ1 = 17;
const uint8_t GPIO_FREQ2 = 18;
const uint8_t GPIO_FREQ_PULL = 19;
const uint8_t GPIO_TX = 20;
const uint8_t GPIO_RX = 21;

const uint8_t ADC_IOUT  = 0;	//Output current
const uint8_t ADC_VOUT  = 1;	//Output voltage
const uint8_t ADC_VBAT  = 2;	//Battery voltage
const uint8_t ADC_IBAT  = 3;	//Battery current
const uint8_t ADC_AIN2  = 4;	//Potentiometer
const uint8_t ADC_AIN1  = 5;	//Mode
const uint8_t ADC_TEMP  = 8;	//MCU temperature
const uint8_t ADC_IVREF = 14;	//MCU internal reference
const uint8_t ADC_GND   = 15;	//MCU ground

const uint8_t PWM_OUT = 0;	//DCCD
const uint8_t PWM_OD  = 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 od_common_is_pwm;
} startupCfg_t;

/**** Public function declarations ****/
void startup(startupCfg_t* hwCfg);

void rtc_set_calibration(uint16_t coef);

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

void adc_start(uint8_t ch);
uint8_t adc_is_running(void);
uint8_t adc_is_new(void);
uint16_t adc_read(void);
uint16_t adc_read(uint8_t ch);

void pwm_write(uint8_t ch, uint16_t dc);
uint16_t pwm_read(uint8_t ch);

void timer_reset(uint8_t ch);
uint16_t timer_read(uint8_t ch);
uint16_t timer_read_top(uint8_t ch);

uint32_t timer_convert_us(uint8_t ch, uint16_t raw);
uint32_t timer_convert_ms(uint8_t ch, uint16_t raw);

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