#include "timer.h"

AT(.com_rodata.isr)
const char str_1[] = "time1 is up_INTERRUPT Method!\r\n";

AT(.com_rodata.isr)
const char str_2[] = "time2 is up_INTERRUPT Method!\r\n";

/*******************
//notes of function_isq():
1.Variables need to be declared add"volatile";
2.“printk” replace “printf";
3.Internal functions AT(.com_text.isr)
************************/
AT(.com_text.isr)
void timer1_isq(void)
{
    if (tmr_get_flag(TMR1, TMR_FLAG_UPDATE1) != RESET) {
        tmr_clear_flag(TMR1, TMR_FLAG_UPDATE1);
        printk(str_1);
    }
}

AT(.com_text.isr)
void timer2_isq(void)
{
    if (tmr_get_flag(TMR2, TMR_FLAG_UPDATE1) != RESET) {
        tmr_clear_flag(TMR2, TMR_FLAG_UPDATE1);

        printk(str_2);
    }
}

void timer1_init(u32 period)
{
    tmr_base_init_typedef tmr_base_init_struct;

    clock_gate0_cmd(CLOCK_GATE0_TMR1, ENABLE);

    tmr_base_init_struct.clock_source = TMR_INC_SOURCE;
    tmr_base_init_struct.increase_clock = TMR_COUNTER_RISING;
    tmr_base_init_struct.period = period;
    tmr_base_init_struct.pwm_mode = TMR_PWM_MODE_NULL;

    tmr_base_init(TMR1, &tmr_base_init_struct);

    tmr_pic_config(TMR1, timer1_isq, 0, TMR_IT_UPDATE1, ENABLE);

    tmr_cmd(TMR1, ENABLE);
}

void timer2_init(u32 period)
{
    tmr_base_init_typedef tmr_base_init_struct;

    clock_gate0_cmd(CLOCK_GATE0_TMR2, ENABLE);

    tmr_base_init_struct.clock_source = TMR_INC_SOURCE;
    tmr_base_init_struct.increase_clock = TMR_COUNTER_RISING;
    tmr_base_init_struct.period = period;
    tmr_base_init_struct.pwm_mode = TMR_PWM_MODE_NULL;

    tmr_base_init(TMR2, &tmr_base_init_struct);

    tmr_pic_config(TMR2, timer2_isq, 0, TMR_IT_UPDATE1, ENABLE);

    tmr_cmd(TMR2, ENABLE);
}

