#include "bsp/bsp_driver.h"

void sched_timer_enable(u32 us) {
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(SCHED_TIMER_RCU);

    timer_deinit(SCHED_TIMER);
    /* initialize TIMER init parameter struct */
    timer_struct_para_init(&timer_initpara);

    timer_initpara.prescaler         = TIM_CLOCK_MHz-1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = us-1;//400;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
	timer_initpara.repetitioncounter = 0;
    timer_init(SCHED_TIMER, &timer_initpara);
	timer_counter_value_config(SCHED_TIMER,0);

	timer_interrupt_flag_clear(SCHED_TIMER, TIMER_INT_UP);
	
    timer_interrupt_enable(SCHED_TIMER, TIMER_INT_UP);

	nvic_irq_enable(SCHED_TIMER_IRQ, SCHED_TIMER_IRQ_PRIORITY, 0);

	timer_enable(SCHED_TIMER);
}
__weak void Sched_MC_mTask(void) {}
void SCHED_TIMER_IRQHandler(void) {
	if(SET == timer_interrupt_flag_get(SCHED_TIMER, TIMER_INT_UP)) {
		timer_interrupt_flag_clear(SCHED_TIMER, TIMER_INT_UP);
		Sched_MC_mTask();
	}
}
