#ifndef FAN_MOTOR_CONTROL_H_
#define FAN_MOTOR_CONTROL_H_

#include "board.h"

#if BOARD==SAMG55_XPLAINED_PRO
#define FAN_PIO				PIOA
#define PIN_FAN_FREQ		PIO_PA0_IDX
#define PINMODE_FAN_FREQ	IOPORT_MODE_MUX_B
#define PIN_FAN_PWM			PIO_PA16_IDX
#define PINMODE_FAN_PWM		IOPORT_MODE_MUX_B
#endif

// Nominal fan speed = 10500 RPM (175 Hz) @ 100% PWM
// One fan rotation takes about 1/175Hz = 5.7msec
// But...the pulse output is 1/6th of a rotation
// Minimum pulse (at max fan speed) is then 
// 5.7ms / 6 = 0.95msec
// Ideally get at least ~10 bits of resolution, so 
// I want each count to be about 1usec = 1MHz clock
#define PCK3_SRC			PMC_PCK_CSS_PLLA_CLK	
#define PCK3_DIV_RATIO		(98UL)
#define PCK3_DIV			PMC_PCK_PRES(PCK3_DIV_RATIO-1)

#define FAN_FG_TIMERCLK		(CONFIG_PLL0_FREQ / PCK3_DIV_RATIO)

#define FAN_PWM_TIMERCLK	(CONFIG_PLL0_FREQ / 8UL)
#define FAN_PWM_FREQ		(25000UL)
#define FAN_PWM_RC			(FAN_PWM_TIMERCLK / FAN_PWM_FREQ - 1)

#define FAN_INTERRUPT_PRIO	(7)

typedef enum {
	FAN_STOPPED,
	FAN_RUNNING	
	
}Fan_State_T;



void init_fan(void);
void disable_fan(void);
void set_fan_pwm(uint16_t duty_cycle);
uint16_t get_fan_speed(void);

#endif /* FAN_MOTOR_CONTROL_H_ */
