#include "hal/hal.h"
#include "hal/tim4.h"
#include "stm32f3xx_ll_tim.h"

static TIM_HandleTypeDef timx;
void TIM4_Init(void){
	timx.Instance = TIM6;
	timx.Init.Prescaler = 7200; //72000000 / 7200 = 10000 // 10us
	timx.Init.Period = 100; //10ms
	timx.Init.RepetitionCounter = 0;
	timx.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
	if (HAL_TIM_Base_Init(&timx) != HAL_OK)
	{
	  Error_Handler();
	}	
}
void TIM4_Enable(void){
	LL_TIM_SetCounter( timx.Instance, 0);
	LL_TIM_EnableCounter(timx.Instance);
	LL_TIM_ClearFlag_UPDATE(timx.Instance);
	LL_TIM_EnableIT_UPDATE(timx.Instance);
	HAL_NVIC_EnableIRQ(TIM6_DAC1_IRQn);
}
void TIM4_Disable(void){
	LL_TIM_DisableCounter(timx.Instance);
	HAL_NVIC_DisableIRQ(TIM6_DAC1_IRQn);
}


void TIM4_Enable_IRQ(bool enable){
	if (enable) {
		HAL_NVIC_EnableIRQ(TIM6_DAC1_IRQn);
	}else {
		HAL_NVIC_DisableIRQ(TIM6_DAC1_IRQn);
	}
}
