/*******************************************************************************
*
*
* File:          demo_hal_timer.c
* Description：  timer demo源文件
* Version：      V0.1
* Date：         2023-07-27
* Author：       zzf
 *******************************************************************************/

/*******************************************************************************
* 1.Included files
*******************************************************************************/
#include "hal_timer.h"
#include "hal_gpio.h"
#include "tau_log.h"

/*******************************************************************************
* 2.Global constant and macro definitions using #define
*******************************************************************************/
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "timer-log"

#define TIMER_DEMO_NORMAL      0           //定时器常规使用，循环超时
#define TIMER_DEMO_RESTART     1           //中断重启定时器

#define TIMER_SEL              TIMER_NUM0  //timer0-3 都ok

/*******************************************************************************
* 3.Global structures, unions and enumerations using typedef
*******************************************************************************/

/*******************************************************************************
* 4.Global variable extern declarations
*******************************************************************************/
static io_pad_e sg_timer_io_pad = IO_PAD_GPIO1;
static gpio_level_e sg_timer_io_lvl = IO_LVL_LOW;
#if TIMER_DEMO_RESTART
static uint16_t sg_timer_count = 1;
#endif

/*******************************************************************************
* 5.Global function prototypes
*******************************************************************************/
#if TIMER_DEMO_RESTART
/**
* @brief  重新循环中断回调函数
* @param  None
* @retval None
*/
static void demo_timer_restart_callback(void *data)
{
    /* 翻转GPIO */
    sg_timer_io_lvl = (sg_timer_io_lvl ? IO_LVL_LOW : IO_LVL_HIGH);
    hal_gpio_set_output_data(sg_timer_io_pad, sg_timer_io_lvl);

    /* 重新启动定时器 */
    sg_timer_count = sg_timer_count * 10;
    if (sg_timer_count > 100)
    {
        sg_timer_count = 1;
    }
    hal_timer_start(TIMER_SEL, sg_timer_count * 10, demo_timer_restart_callback, NULL);
}

/**
* @brief  重新中断例程初始化
* @param  None
* @retval None
*/
static void demo_timer_case_restart(void)
{
    /*重新初始化定时器*/
    hal_timer_init(TIMER_SEL);
    hal_timer_set_repeat(TIMER_SEL, false);

    /*初始化GPIO低电平*/
    hal_gpio_init_output(sg_timer_io_pad, sg_timer_io_lvl);

    /*更新当前timer测试参数*/
    hal_timer_start(TIMER_SEL, sg_timer_count * 10, demo_timer_restart_callback, NULL);
}
#endif

#if TIMER_DEMO_NORMAL
/**
* @brief  多次循环中断回调函数
* @param  None
* @retval None
*/
static void demo_timer_normal_callback(void *data)
{
    /* 翻转GPIO */
    sg_timer_io_lvl = (sg_timer_io_lvl ? IO_LVL_LOW : IO_LVL_HIGH);
    hal_gpio_set_output_data(sg_timer_io_pad, sg_timer_io_lvl);
}

/**
* @brief  重新中断例程初始化
* @param  None
* @retval None
*/
static void demo_timer_case_normal(void)
{
    /*重新初始化定时器*/
    hal_timer_init(TIMER_SEL);
    hal_timer_set_repeat(TIMER_SEL, true);

    /*初始化GPIO低电平*/
    hal_gpio_init_output(sg_timer_io_pad, sg_timer_io_lvl);

    /*更新当前timer测试参数*/
    hal_timer_start(TIMER_SEL, 10, demo_timer_normal_callback, NULL);
}
#endif

/**
* @brief  demo测试接口函数
* @param  None
* @retval None
*/
void demo_timer_case(void)
{
    TAU_LOGI("start test\r\n");

#if TIMER_DEMO_NORMAL
    demo_timer_case_normal();   // 10ms定时进行IO电平翻转
#elif TIMER_DEMO_RESTART
    demo_timer_case_restart();
#endif

    TAU_LOGI("end test\r\n");
}

