/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2024. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V3.1.0  AC7840x  Demo
 ******************************************************************************/

#include "timer_sample.h"
#include "gpio_drv.h"
#include "gpio_hw.h"
#include "timer_drv.h"
#include "string.h"
#include "osif.h"

/*!
* @brief TIMER中断回调函数，
*
* @param[in] none
* @return none
*/
void TIMER_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
    printf("\r\n");
    for(int i=0;i<10;i++)
    {
        printf("this is a timer interrupt %d \r\n",i);
        for(int flag=0;flag<10;flag++)
        {
            __ASM("nop");
        }
    }
}

/*!
 * @brief 	Timer模块配置
 *          Timer_Channel_0配置为32bit周期计数器，不使能链接模式，定时2s.
 * @param 	none
 * @return	none
 */
void TIMER_Init(void)
{
    timer_user_channel_config_t timer_cfg;

    timer_cfg.periodUnits = TIMER_PERIOD_UNITS_COUNTS;
    timer_cfg.triggerSource = TIMER_TRIGGER_SOURCE_INTERNAL;      /*触发输入选择*/
    timer_cfg.triggerSelect = 0U;                                 /*内部触发源选择 */
    timer_cfg.enableReloadOnTrigger = DISABLE;                         /* 触发时重装载使能位    */
    timer_cfg.enableStopOnInterrupt = DISABLE;                         /* 单次计数使能位，使能时，timer完成一次超时或捕获后即停止计数 */
    timer_cfg.enableStartOnTrigger = DISABLE; /*检测到触发输入时才开始计数使能位，使能时，timer必须等检测到一次触发输入才会开始计数，一旦开始计数，后面的触发输入会被忽略*/
    timer_cfg.chainChannel = DISABLE;  /*链接模式使能位 */
    timer_cfg.isInterruptEnabled = ENABLE; /*中断使能位*/
    timer_cfg.timerMode = TIMER_PERIODIC_COUNTER; /* 计数模式配置 ，选择32bit周期计数器*/
    timer_cfg.period =4000000 -1;  /*计数值*/

    TIMER_DRV_Init(0U, ENABLE); /*Timer 时钟和调试模式控制*/
    TIMER_DRV_InitChannel(0U, TIMER_CHANNEL_0 , &timer_cfg); /*Timer 通道模式配置*/
    TIMER_DRV_StartChannels(0U, 1 << TIMER_CHANNEL_0); /*Timer 通道使能控制*/
    TIMER_DRV_InstallCallback(TIMER_CHANNEL_0, &TIMER_Callback);

    NVIC_SetPriority(TIMER_CHANNEL0_IRQn, 4);
}
