#ifndef MCU_HAL_H_
#define MCU_HAL_H_

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

namespace mcu {

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

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