#include "platform.h"
#include "hal_headfile.h"
#include "system.h"
#include "debug.h"
#include "board.h"
#include "timer.h"

/**
 * time0 init ,this is for system tick.
 *
 * @param :none
 * @return: none.
 */
void timer_init(void)
{
    hal_tim_init();
    hal_tim_timer_interrupt_enable(TIM0);
    hal_tim_mode_config(TIM0, TIM_MODE_TIMER);
    hal_tim_reload_config(TIM0, TIM_RELOAD_ENABLE);
    hal_tim_counter_dire_config(TIM0, TIM_DIRECTION_INCREASE);
    hal_tim_set_reload_value(TIM0, 2400 * 5); //500us
    hal_tim_run_cmd(TIM0, ENABLE);
}

/**
 * timer0_irq_handler
 *
 * @param :none
 * @return: none.
 */
void timer0_irq_handler(void)
{
    /* enter interrupt */
    hal_tim_clear_interrupt_pending(TIM_IT_TIMER0);
    BaseTime_2ms_Cnt++;
    tick_user_handler();
}

/**
 * timer0 interrupt enable or disable,for lowpower mode.
 *
 * @param :option   ENABLE DISBALE
 * @return: none.
 */
void time0_lowper_callback(uint8_t option)
{
    if (option == 1)
    {
        hal_tim_timer_interrupt_enable(TIM0);
        hal_tim_run_cmd(TIM0, ENABLE);
        cmu_set_clk_en(TIMER_CLOCK_ENABLE, ENABLE);
    }
    else
    {
        cmu_set_clk_en(TIMER_CLOCK_ENABLE, DISABLE);
        hal_tim_timer_interrupt_disable(TIM0);
        hal_tim_run_cmd(TIM0, DISABLE);
    }
}
