/*
 * bldc.h
 *
 * Created: 31/07/2021 14:56:02
 *  Author: Nick-XMG
 */ 

#ifndef BLDC_H_
#define BLDC_H_

// ----------------------------------------------------------------------
// 	header files
// ----------------------------------------------------------------------
#include "atmel_start.h"
#include "bldc_types.h"
#include "motorparameters.h"
#include "statemachine.h"

#define PWM_TOP					(1000)
#define MAX_PWM					(600)
//#define MAX_VEL					3800

#define CW						(0) //CBA
#define DIRECTION_CW_OFFSET		(0) //CBA
#define CCW						(1) //ABC
#define DIRECTION_CCW_OFFSET	(8) //CBA

/* if the Hall sensor reads 0x0 or 0x7 that means either one of the hall sensor is damaged or disconnected*/
#define INVALID_HALL_0              (0)
#define INVALID_HALL_7              (7)

//#define AVERAGE_SPEED_MEASURE	
#define n_SAMPLE 8

// ----------------------------------------------------------------------
// 	ADC Parameters
// ----------------------------------------------------------------------
#define ADC_VOLTAGE_REFERENCE		(3.3f)
#define ADC_RESOLUTION				(12)
#define ADC_MAX_COUNTS				(1<<ADC_RESOLUTION)
#define ADC_LSB_SIZE				(ADC_VOLTAGE_REFERENCE/ADC_MAX_COUNTS)
#define LSB_TO_PU					(ADC_LSB_SIZE * ONEON_CURRENT_SENSOR_SENSITIVITY)

// ----------------------------------------------------------------------
// Define the control and PWM frequencies:
// ----------------------------------------------------------------------
// 16kHz is the maximum frequency according to the calculation duration in the mode run and spin.
#define DEVICE_MCU_FREQUENCY_Hz        (120000000U)
#define DEVICE_SPEEDTC_DIV			   (4U)
#define DEVICE_SPEEDTC_FREQUENCY_Hz    (DEVICE_MCU_FREQUENCY_Hz/DEVICE_SPEEDTC_DIV)
#define DEVICE_PWM_FREQUENCY_kHz       (25.0f)
#define DEVICE_ISR_FREQUENCY_kHz       DEVICE_PWM_FREQUENCY_kHz
#define DEVICE_ISR_PERIOD_Sec		   (0.001f/DEVICE_ISR_FREQUENCY_kHz)
#define HZ_TO_RPM					   (DEVICE_SPEEDTC_FREQUENCY_Hz * 60)
//#define HZ_TO_RPM					   MOTOR_COMUTATION_STATES/(DEVICE_SPEEDTC_FREQUENCY_Hz * 60)

// ----------------------------------------------------------------------
// Define the device quantities (voltage, current, speed)
// ----------------------------------------------------------------------
#define DEVICE_DC_VOLTAGE_V					24.0f   // max. ADC bus voltage(PEAK) [V]
#define DEVICE_SHUNT_CURRENT_A				2.5f    // phase current(PEAK) [A]
#define CURRENT_SENSOR_SENSITIVITY			0.4f    //V/A
#define ONEON_CURRENT_SENSOR_SENSITIVITY	2.5f    //V/A

// ----------------------------------------------------------------------
// 	global variables
// ----------------------------------------------------------------------

static const uint8_t HALL_PATTERN_ARRAY[16]		= {0, 5, 3, 1, 6, 4, 2, 0, 0, 3, 6, 2, 5, 1, 4, 0 };
static const uint8_t MOTOR_COMMUTATION_STEPS[8]		= {9, 1, 3, 2, 5, 6, 4, 9};

volatile BLDCMotor_t Motor1;
volatile BLDCMotor_t Motor2;

volatile MOTOR_STATE_t Motor1_Status;
volatile MOTOR_STATE_t Motor2_Status;

// ----------------------------------------------------------------------
// 	functions
// ----------------------------------------------------------------------
void motor_StateMachine(BLDCMotor_t* const motor);
void BldcInitStruct(BLDCMotor_t* const motor, BLDCMotor_param_t* constmotor_param);
void exec_commutation(BLDCMotor_t* const motor);
void select_active_phase(BLDCMotor_t* const Motor);
void calculate_motor_speed(BLDCMotor_t* const motor);
// ----------------------------------------------------------------------
// 	Static Functions
// ----------------------------------------------------------------------
static void BLDC_runSpeedCntl(BLDCMotor_t *motor, const float32_t speedfbk, const float32_t speedRef);
static void BLDC_runCurrentCntl(BLDCMotor_t *motor, const float32_t curfbk, const float32_t curRef);
static void BLDC_runPosCntl(BLDCMotor_t *motor, int16_t posfbk, int16_t posRef);
static void BLDC_runOpenLoop(BLDCMotor_t *motor, int16_t duty);
volatile uint8_t readHallSensorM1(void);
volatile uint8_t readHallSensorM2(void);
volatile uint8_t readHallSensorM1v2(void);

#endif /* BLDC_H_ */