/*
 * motor_params.h
 *
 * Created: 28/04/2021 09:13:40
 *  Author: Nick-XMG
 */ 


#ifndef MOTOR_PARAMS_H_
#define MOTOR_PARAMS_H_

// ----------------------------------------------------------------------
// Current Sensor
// ----------------------------------------------------------------------

#define DRV_ISEN_A_CH				ADC_INPUTCTRL_MUXPOS_AIN2_Val
#define DRV_ISEN_B_CH				ADC_INPUTCTRL_MUXPOS_AIN1_Val

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

#define HALL_A_PIN					GPIO(GPIO_PORTA, 6)
#define HALL_A_PORT					PORT_PA06
#define HALL_A_MASK					~(1<<2)
#define HALL_A_LSR					HALL_A_PIN	- HALL_A_GROUP*32 -2
#define HALL_A_GROUP				HALL_A_PIN/32

#define HALL_B_PIN					GPIO(GPIO_PORTA, 5)
#define HALL_B_PORT					PORT_PA05
#define HALL_B_MASK					~(1<<1)
#define HALL_B_LSR					HALL_B_PIN	- HALL_B_GROUP*32 -1
#define HALL_B_GROUP				HALL_B_PIN/32

#define HALL_C_PIN					GPIO(GPIO_PORTA, 4)
#define HALL_C_PORT					PORT_PA04
#define HALL_C_MASK					~(1<<0)
#define HALL_C_LSR					HALL_C_PIN	- HALL_C_GROUP*32 -0
#define HALL_C_GROUP				HALL_C_PIN/32

// ----------------------------------------------------------------------
// Motor Parameters
// ----------------------------------------------------------------------
//  these values fit for an Faulhaber 3216W024BXTR motor
#define MOTOR_POLES                 (14U)
#define MOTOR_POLEPAIRS             (MOTOR_POLES/2)
#define MOTOR_COMUTATION_STATES     (MOTOR_POLEPAIRS*6)
#define MOTOR_RS_OHM                (3.37f)
#define MOTOR_LD_H                  (0.001290f)
#define MOTOR_LQ_H                  (0.001290f)
#define MOTOR_FLUX_WB               (0.0063879968f)
//#define MOTOR_MAX_SPD_RPM           (3000.0f)
#define MOTOR_MAX_SPD_RPM           (500.0f)
#define MOTOR_MAX_SPD_ELEC          ((MOTOR_MAX_SPD_RPM/60)*MOTOR_POLEPAIRS)
#define MOTOR_MEASURINGRANGE_RPM    (1.2f * MOTOR_MAX_SPD_RPM)f   // give 20% headroom
#define MOTOR_MAX_CURRENT_IDC_A     (1.2f)
//#define MOTOR_MAX_CURRENT_IDC_A     (0.5f)

#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

#define AVERAGE_SPEED_MEASURE
#define COUNTEQUIVTO25RPM 892857142
#define n_SAMPLE 8
//#define n_SAMPLE 1
static uint32_t speed_average = 0;
static uint8_t count = 1;

// ----------------------------------------------------------------------
// Commutation Patterns
// ----------------------------------------------------------------------

/**
**	____|___TCC0_________ ||____TCC1_________|
**	W0	|	M2_0	| CC0 ||  __M3_2__ | CC0 |
**	W1	|	M2_1	| CC1 ||	  -	   | CC1 |
**	W2	|	M2_2	| CC2 ||	M1_0   | CC1 |
**	W3	|	M2_3	| CC3 ||	M1_1   | CC1 |
**	W4	| __M3_0__	| CC4 ||	M1_2   | CC1 |
**	W5	| __M3_1__	| CC5 ||	M1_3   | CC1 |
**	W6	|	M2_4	| CC0 ||	M1_4   | CC1 |
**	W7  |	M2_5	| CC1 ||	M1_5   | CC1 |
*/

static const uint16_t COMMUTATION_PATTERN_M1[] = {
	0x00FF,		// (0) invalid state
	0x30F8,		// (1)
	0x50F5,		// (2)
	0x60F8,		// (3)
	0x607D,		// (4)
	0x507D,		// (5)
	0x30F5,		// (6)
	0x00FF,		// (7) invalid state
	0x00FF,		// (8) invalid state
	0x30F5,		// (9)
	0x507D,		// (10)
	0x607D,		// (11)
	0x60F8,		// (12)
	0x50F5,		// (13)
	0x30F8,		// (14)
	0x00FF		// (15) invalid state
};

static const uint16_t COMMUTATION_PATTERN_M2[] = {
	0x00FF,		// (0) invalid state
	0xC0FB,		// (1)
	0x48DD,		// (2)
	0x88FB,		// (3)
	0x88EE,		// (4)
	0x48EE,		// (5)
	0xC0DD,		// (6)
	0x00FF,		// (7) invalid state
	0x00FF,		// (8) invalid state
	0xC0DD,		// (9)
	0x48EE,		// (10)
	0x88EE,		// (11)
	0x88FB,		// (12)
	0x48DD,		// (13)
	0xC0FB,		// (14)
	0x00FF		// (15) invalid state
};

static const uint16_t m3_TCC0_mask		=	0x0030;
static const uint16_t m3_TCC0_inv_mask	= ~(0x0030);
static const uint16_t m3_TCC1_mask		=	0x0001;
static const uint16_t m3_TCC1_inv_mask	= ~(0x0001);






#endif /* MOTOR_PARAMS_H_ */