#include "bsp/bsp_driver.h"

void sched_timer_enable(u32 us) {
    TIM_TimeBaseInitType TIM_TimeBaseStructure;

    rcu_apb1_periph_clock_enable(SCHED_TIMER_RCU);

    TIM_DeInit(SCHED_TIMER);

	TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.Period    = us-1;
    TIM_TimeBaseStructure.Prescaler = TIM_SCHED_CLK_MHz-1;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;

    TIM_InitTimeBase(TIM5, &TIM_TimeBaseStructure);

	TIM_SetCnt(SCHED_TIMER,0);

	TIM_ClearFlag(SCHED_TIMER, TIM_FLAG_UPDATE);

	TIM_ConfigInt(SCHED_TIMER, TIM_INT_UPDATE, ENABLE);

	nvic_irq_enable(SCHED_TIMER_IRQ, SCHED_TIMER_IRQ_PRIORITY, 0);

	TIM_Enable(SCHED_TIMER, ENABLE);
}
__weak void Sched_MC_mTask(void) {}
void SCHED_TIMER_IRQHandler(void) {
	if(SET == TIM_GetIntStatus(SCHED_TIMER, TIM_INT_UPDATE)) {
		TIM_ClrIntPendingBit(SCHED_TIMER, TIM_INT_UPDATE);
		Sched_MC_mTask();
	}
}
