/*
 * motorparameters.h
 *
 * Created: 04/08/2021 21:23:43
 *  Author: Nick-XMG
 */ 


#ifndef MOTORPARAMETERS_H_
#define MOTORPARAMETERS_H_
#include "atmel_start.h"

// ----------------------------------------------------------------------
// 	M1 Hall Parameters
// ----------------------------------------------------------------------

#define M1_HALL_A_PIN					GPIO(GPIO_PORTA, 4)
#define M1_HALL_A_PORT					PORT_PA04
#define M1_HALL_A_MASK					~(1<<2)
#define M1_HALL_A_LSR					M1_HALL_A_PIN	- M1_HALL_A_GROUP*32 -2
#define M1_HALL_A_GROUP					M1_HALL_A_PIN/32

#define M1_HALL_B_PIN					GPIO(GPIO_PORTA, 5)
#define M1_HALL_B_PORT					PORT_PA05
#define M1_HALL_B_MASK					~(1<<1)
#define M1_HALL_B_LSR					M1_HALL_B_PIN	- M1_HALL_B_GROUP*32 -1
#define M1_HALL_B_GROUP					M1_HALL_B_PIN/32

#define M1_HALL_C_PIN					GPIO(GPIO_PORTA, 6)
#define M1_HALL_C_PORT					PORT_PA06
#define M1_HALL_C_MASK					~(1<<0)
#define M1_HALL_C_LSR					M1_HALL_C_PIN	- M1_HALL_C_GROUP*32 -0
#define M1_HALL_C_GROUP					M1_HALL_C_PIN/32

// ----------------------------------------------------------------------
// 	M2 Hall Parameters
// ----------------------------------------------------------------------

#define M2_HALL_A_PIN					GPIO(GPIO_PORTA, 22)
#define M2_HALL_A_PORT					PORT_PA22
#define M2_HALL_A_MASK					~(1<<2)
#define M2_HALL_A_LSR					M2_HALL_A_PIN	- M2_HALL_A_GROUP*32 -2
#define M2_HALL_A_GROUP					M2_HALL_A_PIN/32

#define M2_HALL_B_PIN					GPIO(GPIO_PORTA, 23)
#define M2_HALL_B_PORT					PORT_PA23
#define M2_HALL_B_MASK					~(1<<1)
#define M2_HALL_B_LSR					M2_HALL_B_PIN	- M2_HALL_B_GROUP*32 -1
#define M2_HALL_B_GROUP					M2_HALL_B_PIN/32

#define M2_HALL_C_PIN					GPIO(GPIO_PORTA, 24)
#define M2_HALL_C_PORT					PORT_PA24
#define M2_HALL_C_MASK					~(1<<0)
#define M2_HALL_C_LSR					M2_HALL_C_PIN	- M2_HALL_C_GROUP*32 -0
#define M2_HALL_C_GROUP					M2_HALL_C_PIN/32



/* ----------------------------------------------------------------------
// Commutation Patterns
// ----------------------------------------------------------------------
/**
**	____|___TCC0_____OTMX ||____TCC1____ OTMX|
**	W0	|	M1_0	| CC0 ||    M2_0   | CC0 |
**	W1	|	M1_0	| CC0 ||	M2_0   | CC0 |
**	W2	|	M1_0	| CC0 ||	M2_0   | CC0 |
**	W3	|	M1_0	| CC0 ||	M2_0   | CC0 |
**	W4	|   M1_0	| CC0 ||	M2_0   | CC0 |
**	W5	|   M1_0	| CC0 ||	M2_0   | CC0 |
**	W6	|	M1_0	| CC0 ||	M2_0   | CC0 |
**	W7  |	M1_0	| CC0 ||	M2_0   | CC0 |
*/

#define DISABLE_PATTERN		0x003F

//////* A&C phase swapped */
//static const uint16_t COMMUTATION_PATTERN[16] = {
	//0x003F,		// (0) invalid state
	//0x1437,					// (1)
	//0x053D,					// (2)
	//0x113D,					// (3)
	//0x111F,					// (4)
	//0x0537,					// (5)
	//0x141F,					// (6)
	//0x003F,		// (7) invalid state
	//0x003F,		// (8) invalid state
	//0x141F,					// (9)
	//0x0537,					// (10)
	//0x111F,					// (11)
	//0x113D,					// (12)
	//0x053D,					// (13)
	//0x1437,					// (14)
	//0x003F,			// (15) invalid state
//};

////* A&C phase swapped and modified to match currents */
static const uint16_t COMMUTATION_PATTERN[16] = {
	0x003F,		// (0) invalid state
	0x141F,		// (1)
	0x0537,		// (2)
	0x111F,		// (3)
	0x113D,		// (4)
	0x053D,		// (5)
	0x1437,		// (6)
	0x003F,		// (7) invalid state
	0x003F,		// (8) invalid state
	0x1437,		// (9)
	0x053D,		// (10)
	0x113D,		// (11)
	0x111F,		// (12)
	0x0537,		// (13)
	0x141F,		// (14)
	0x003F		// (15) invalid state
};

// ----------------------------------------------------------------------
// 	Motor Physical Parameters
// ----------------------------------------------------------------------
typedef struct
{
	struct pwm_descriptor const *pwm_desc;
	void *const speedtimer_hw;
	const uint16_t motor_Poles;          
	const uint16_t motor_polePairs;
	const uint16_t motor_commutationStates;
	const float32_t motor_RS_Ohm;
	const float32_t motor_LD_H;
	const float32_t motor_LQ_H;
	const float32_t motor_Flux_WB;
	const float32_t motor_Max_Spd_RPM;
	const float32_t motor_MeasureRange_RPM;	// give 20% headroom
	const float32_t motor_Max_Spd_ELEC;
	const float32_t motor_Max_Current_IDC_A;
} BLDCMotor_param_t;

//static BLDCMotor_param_t FH_22mm24BXTR;
//static BLDCMotor_param_t FH_32mm24BXTR;

/* Small Motor - 2214S024BXTR*/
const static BLDCMotor_param_t FH_22mm24BXTR = {
	.pwm_desc				 = &PWM_0,
	.speedtimer_hw			= TC2,
	.motor_Poles			 = 14,
	.motor_polePairs		 = 7,
	.motor_commutationStates = 42,			 //polePairs * 6
	.motor_RS_Ohm			 = 25.9,
	.motor_LD_H				 = 0.003150,
	.motor_LQ_H				 = 0.003150,
	.motor_Flux_WB			 = 0.001575,
	.motor_Max_Spd_RPM		 = 3000,
	.motor_MeasureRange_RPM	 = 3000 * 1.2,	//(1.2f * MOTOR_MAX_SPD_RPM)f   // give 20% headroom
	.motor_Max_Spd_ELEC		 = (3000/60)*7.0,		//(MOTOR_MAX_SPD_RPM/60)*MOTOR_POLEPAIRS
	.motor_Max_Current_IDC_A = 0.368,
};

/* Big Motor - 3216W024BXTR */
const static BLDCMotor_param_t FH_32mm24BXTR = {
	.pwm_desc				 = &PWM_1,
	.speedtimer_hw			 = TC4,
	.motor_Poles			 = 14,
	.motor_polePairs		 = 7,
	.motor_commutationStates = 42,			 //polePairs * 6
	.motor_RS_Ohm			 = 3.37,
	.motor_LD_H				 = 0.001290,
	.motor_LQ_H				 = 0.001290,
	.motor_Flux_WB			 = 0.0063879968,
	.motor_Max_Spd_RPM		 = 3000,
	.motor_MeasureRange_RPM	 = 3200,		//(1.2f * MOTOR_MAX_SPD_RPM)f   // give 20% headroom
	.motor_Max_Spd_ELEC		 = 12000,		//(MOTOR_MAX_SPD_RPM/60)*MOTOR_POLEPAIRS
	.motor_Max_Current_IDC_A = 1.2,
};

#endif /* MOTORPARAMETERS_H_ */