#include <csi_config.h>
#include <drv/timer.h>
#include "app_timer.h"
#include "app_gpio.h"
#include "app_uart.h"
#include "app_codec.h"
#include "asr_config.h"


#ifdef CONFIG_XIP
#define ATTRIBUTE_DATA __attribute__((section(".ram.code")))
#else
#define ATTRIBUTE_DATA
#endif


extern void app_user_timer_thread(void);
extern volatile unsigned int asr_tick_timer;
extern volatile unsigned int asr_wakeup_tick_timer;

static csi_timer_t g_timer_handle[8];
static unsigned char g_timer_param[8];
static unsigned int userTimes[8];
volatile user_timer_struct user_timer;
ATTRIBUTE_DATA static void timer_event_cb_reload_fun(csi_timer_t *timer_handle, void *arg)
{
    if((unsigned char)(timer_handle->dev.idx) == 0)
    {
//        user_timer.f_100us_irq =1;
//        user_timer.tick_100us ++;

//        if((user_timer.tick_100us % 5) ==0)
        {
			user_timer.tick_100us += 5;
            user_timer.f_500us_irq = 1;
        }
        if((user_timer.tick_100us % 10) ==0)//1ms
        {
            user_timer.f_1ms_irq = 1;
			asr_tick_timer++;
			asr_wakeup_tick_timer ++;
        }
        if((user_timer.tick_100us % 100) ==0)//10ms
        {
            user_timer.f_10ms_irq = 1;
            app_user_timer_thread();
        }
		if((user_timer.tick_100us % 1000) ==0)//100ms
        {
            user_timer.f_100ms_irq = 1;
        }
		if((user_timer.tick_100us % 5000) ==0)//5000ms
        {
            user_timer.f_500ms_irq = 1;
        }
        if((user_timer.tick_100us % 10000) ==0)
        {
            user_timer.tick_100us = 0;
            user_timer.f_1s_irq = 1;
			user_timer.power_on_timer_1s++;
        }
    }
}
/**
  \brief       Initialize TIMER Interface. 1. Initializes the resources needed for the TIMER interface
  \param[in]   t_index         timer index
  \param[in]   t_param.........1: repeat  0: once
  \return      error code \ref csi_error_t
*/
csi_error_t app_timer_init(unsigned char t_index, unsigned char t_param, unsigned int times)
{
    //当设定多个定时器需要所注册的回调函数重入问题
    csi_error_t ret = CSI_OK;

    ret = csi_timer_init(&g_timer_handle[t_index], t_index);

    if (ret != CSI_OK) {
        #if APP_LOG_ENABLE
        printf("csi_timer_init error\n");
        #endif
        return ret;
    }
    
    g_timer_param[t_index] = t_param;
    ret = csi_timer_attach_callback(&g_timer_handle[t_index], timer_event_cb_reload_fun, &g_timer_param[t_index]);//params 1: repeat ; 0:once

    if (ret != CSI_OK) {
        #if APP_LOG_ENABLE
        printf("csi_timer_attach_callback error\n");
		#endif
        return ret;
    }

    userTimes[t_index] = times;
    ret = csi_timer_start(&g_timer_handle[t_index], times);
    if (ret != CSI_OK) {
        #if APP_LOG_ENABLE
		printf("csi_timer_start error\n");
        #endif
        return ret;
    }
	
    #if APP_LOG_ENABLE
    printf("app timer0 init\r\n");
    #endif
    return ret;
}
void app_timer_start(unsigned char t_index)
{
    csi_timer_start(&g_timer_handle[t_index], userTimes[t_index]);
}
void app_timer_stop(unsigned char t_index)
{
    csi_timer_stop(&g_timer_handle[t_index]);
}

void app_timer_uninit(unsigned char t_index)
{
    csi_timer_detach_callback(&g_timer_handle[t_index]);
    csi_timer_uninit(&g_timer_handle[t_index]);
}
/*
 *   时钟相关的处理，在主循环中执行
 * 	 会因其它处理delay
 */
void app_user_timer_process(void)
{
	if(user_timer.f_10ms_irq == 1)
    {
        user_timer.f_10ms_irq = 0;
        app_uart_tx_polling();
//		 app_codec_delay_init();
    }
	if(user_timer.f_500ms_irq == 1)
    {
        user_timer.f_500ms_irq = 0;
        app_uart_tx_heartbeat();
    }
}