/* 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) 2023. 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: V1.0.0
 ******************************************************************************/

/*!
 * @file timer_clock_counter.c
 *
 * @brief Timmer work on clock counter mode, set the APB_CLK as the timer clock source.
 * channel0: clk is APB_CLK/1, count to APB_FREQ timeout, T = 1s,
 * channel1: clk is APB_CLK/2, count to APB_FREQ timeout, T = 2s,
 * channel2: clk is APB_CLK/4, count to APB_FREQ timeout, T = 4s,
 * channel3: clk is APB_CLK/8, count to APB_FREQ timeout, T = 8s.
 *
 */

/* ===========================================  Includes =========================================== */
#include <stdint.h>
#include <string.h>

#include "ac7803x_debugout.h"
#include "ac780x_timer.h"
#include "ac780x_timer_reg.h"

#include "timer_clock_counter.h"

/* ============================================  Define  ============================================ */

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */

/* ====================================  Functions declaration  ===================================== */

/* ======================================  Functions define  ======================================== */
/*!
 * @brief Timer channel0 timeout callback function.
 *
 * @param device: the Timer channel0.
 * @param wpara: the value of TIMER_SR(Timer interrupt flag register).
 * @param lpara: unused.
 */
void TIMER_CH0Callback(void* device, uint32_t wpara, uint32_t lpara)
{
    printf("ch0 arrive!, T = 1s--\r\n");
}

/*!
 * @brief Timer channel1 timeout callback function.
 *
 * @param device: the Timer channel1.
 * @param wpara: the value of TIMER_SR(Timer interrupt flag register).
 * @param lpara: unused.
 */
void TIMER_CH1Callback(void* device, uint32_t wpara, uint32_t lpara)
{
    printf("ch1 arrive!, T = 2s----\r\n");
}

/*!
 * @brief Timer channel2 timeout callback function.
 *
 * @param device: the Timer channel2.
 * @param wpara: the value of TIMER_SR(Timer interrupt flag register).
 * @param lpara: unused.
 */
void TIMER_CH2Callback(void* device, uint32_t wpara, uint32_t lpara)
{
    printf("ch2 arrive!, T = 4s--------\r\n");
}

/*!
 * @brief Timer channel3 timeout callback function.
 *
 * @param device: the Timer channel3.
 * @param wpara: the value of TIMER_SR(Timer interrupt flag register).
 * @param lpara: unused.
 */
void TIMER_CH3Callback(void* device, uint32_t wpara, uint32_t lpara)
{
    printf("ch3 arrive!, T = 8s--------------------------------\r\n");
}

/*!
 * @brief Init the timer.
 *
 */
void Timer_Init(void)
{
    TIMER_ConfigType config;
    uint32_t overFlowVal = APB_BUS_FREQ; /* TVAL, with no prescal,T = 1s */
    memset(&config, 0U, sizeof(config));

    /* Initialization, complete before operating other registers, otherwise a hardfault error occurs(no clk) */
    TIMER_Init(TIMER_CLKSR_APB); /* timer clk source：APB_BUS_FREQ */

    // channel 0, T = 1s
    config.timeMode    = TIMER_WORKMODE_TIMER;            /* timer mode */
    config.chainModeEn = DISABLE;                         /* disable link mode */
    config.bypEn       = ENABLE;                          /* bypass enable, unuse the prescaler */
    config.prescaler   = TIMER_PRESCALE_4_GLITCHFILTER_1; /* 4 times prescal,no effect case of bypass prescaler enable(bypEn) */
    config.interruptEn = ENABLE;                          /* enable the channel interrupt */
    config.periodValue = overFlowVal - 1;                 /* trigger an interrupt when counting to periodValue+1 */
    config.callback    = TIMER_CH0Callback;               /* set callback */
    config.cvalReadSel = TIMER_BCLK_FCLK_1;               /* BUSCLK/FUNCTION_CLK == 1 */
    TIMER_InitChannel(TIMER_CHANNEL0, &config);           /* TIMER_InitChannel will not change the para config */

    // channel 1, T = 2s
    config.bypEn       = DISABLE;           /* bypass disable, use the prescaler */
    config.prescaler   = TIMER_PRESCALE_2;  /* 2 times prescal */
    config.callback    = TIMER_CH1Callback; /* set callback */
    config.cvalReadSel = TIMER_BCLK_FCLK_1; /* BUSCLK/FUNCTION_CLK == 2 */
    TIMER_InitChannel(TIMER_CHANNEL1, &config);

    // channel 2, T = 4s
    config.bypEn       = DISABLE;                         /* bypass disable, use the prescaler */
    config.prescaler   = TIMER_PRESCALE_4_GLITCHFILTER_1; /* 4 times prescal */
    config.callback    = TIMER_CH2Callback;               /* set callback */
    config.cvalReadSel = TIMER_BCLK_FCLK_4;               /* BUSCLK/FUNCTION_CLK == 4 */
    TIMER_InitChannel(TIMER_CHANNEL2, &config);

    // channel 3, T = 8s
    config.bypEn       = DISABLE;                         /* bypass disable, use the prescaler */
    config.prescaler   = TIMER_PRESCALE_8_GLITCHFILTER_3; /* 8 times prescal */
    config.callback    = TIMER_CH3Callback;               /* set callback */
    config.cvalReadSel = TIMER_BCLK_FCLK_4;               /* BUSCLK/FUNCTION_CLK == 8 */
    TIMER_InitChannel(TIMER_CHANNEL3, &config);
}
/* =============================================  EOF  ============================================== */
