/* 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
 ******************************************************************************/

/*!
 * @file eio_pwm_sample.c
 *
 * @brief This file provides interface for EIO PWM functions.
 */

/* ===============================  Includes  =============================== */
#include "eio_pwm_sample.h"
#include "ckgen_hw.h"
#include "gpio_drv.h"
#include "gpio_hw.h"
#include "eio_comm.h"
#include "eio_hw.h"

/* ===============================   Define   =============================== */
#define EIO_INDEX (0U)

#define EIO_PWM_TIMER_CH0 (0U)
#define EIO_PWM_TIMER_CH1 (1U)
#define EIO_PWM_TIMER_CH2 (2U)
#define EIO_PWM_TIMER_CH3 (3U)

#define EIO_PWM_OUTPUTPIN0 (0U)
#define EIO_PWM_OUTPUTPIN1 (1U)
#define EIO_PWM_OUTPUTPIN2 (2U)
#define EIO_PWM_OUTPUTPIN3 (3U)

#define PWMCH0_PERIOD    (25000U) /*EIO_D0输出频率设置*/
#define PWMCH0_DUTYCYCLE (30U)    /*EIO_D0输出占空比设置*/
#define PWMCH1_PERIOD    (20000U) /*EIO_D1输出频率设置*/
#define PWMCH1_DUTYCYCLE (80U)    /*EIO_D1输出占空比设置*/
#define PWMCH2_PERIOD    (15000U) /*EIO_D2输出频率设置*/
#define PWMCH2_DUTYCYCLE (60U)    /*EIO_D2输出占空比设置*/
#define PWMCH3_PERIOD    (10000U) /*EI0_D3输出频率设置*/
#define PWMCH3_DUTYCYCLE (50U)    /*EIO_D3输出占空比设置*/

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

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

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

/* =========================  Functions definition  ========================= */

/*!
 * @brief   EIO模块PWM输出配置
 * @param[in] timer: Number of timer
 * @param[in] period :period of PWM
 * @param[in] dutycycle: dutycycle of PWM
 * @param[in] pin: Number of pin
 * @return  none
 */
void EIO_PWM_Config(uint8_t timer, uint32_t period, uint32_t dutycycle, uint8_t pin)
{
    uint8_t lowerValue  = 0;
    uint8_t upperValue  = 0;
    uint32_t sum        = 0;
    uint32_t inputClock = 0;

    CKGEN_DRV_GetFreq(EIO_CLK, &inputClock); /*获取时钟频率*/

    /*timer 用于控制EIO_Dx生成PWM波形*/
    /*计算高低电平的定时器比较值*/
    sum        = inputClock / period;
    upperValue = sum * dutycycle / 100;
    lowerValue = sum - upperValue;
    sum        = ((lowerValue - 1) << 8U) | (upperValue - 1); /*低8位为高电平周期比较值，高8位为低电平周期比较值*/

    EIO_SetTimerCompare(EIO, timer, sum);
    EIO_SetTimerConfig(EIO,
                       timer,                             /*定时器通道选择*/
                       EIO_TIMER_START_BIT_DISABLED,      /*定时器起始位禁用*/
                       EIO_TIMER_STOP_BIT_DISABLED,       /*定时器停止位禁用*/
                       EIO_TIMER_ENABLE_ALWAYS,           /*定时器始终启动 */
                       EIO_TIMER_DISABLE_NEVER,           /*定时器从不停止*/
                       EIO_TIMER_RESET_NEVER,             /*定时器永不复位 */
                       EIO_TIMER_DECREMENT_CLK_SHIFT_TMR, /*定时器递减模式选择在EIO时钟上递减计数， 移位时钟等于定时器输出*/
                       EIO_TIMER_INITOUT_ONE);            /*定时器输出在启用时为逻辑1，不受定时器复位影响*/

    EIO_SetTimerControl(EIO,
                        timer,                       /*定时器通道选择*/
                        4 * timer + 3,               /*触发选择，定时器N触发*/
                        EIO_TRIGGER_POLARITY_LOW,    /*触发极性为低电平有效*/
                        EIO_TRIGGER_SOURCE_INTERNAL, /*触发源为内部触发*/
                        pin,                         /*定时器引脚选择 EIO_Dx */
                        EIO_PIN_POLARITY_HIGH,       /*定时器引脚极性配置为高电平有效*/
                        EIO_PIN_CONFIG_OUTPUT,       /*定时器引脚配置为输出*/
                        EIO_TIMER_MODE_8BIT_PWM);    /*定时器模式选择：双8位计数器PWM模式*/
}

/*!
 * @brief   EIO模块配置
 *          EIO模块配置为双8位定时器PWM模式，
 *          定时器0----频率25KHz,占空比30%----EIO_D0(PD0)
 *          定时器1----频率20KHz,占空比80%----EIO_D1(PD1)
 *          定时器2----频率15KHz,占空比60%----EIO_D2(PE15)
 *          定时器3----频率10KHz,占空比50%----EIO_D3(PE16)
 * @param   none
 * @return  none
 */
void EIO_PWM_Init(void)
{
    eio_device_state_t EIODeviceState;

    /*GPIO config for EIO*/
    GPIO_DRV_SetMuxModeSel(PORTD, 0, PORT_MUX_ALT6);  /*PD0:EIO_D0 */
    GPIO_DRV_SetMuxModeSel(PORTD, 1, PORT_MUX_ALT6);  /*PD1:EIO_D1 */
    GPIO_DRV_SetMuxModeSel(PORTE, 15, PORT_MUX_ALT6); /*PE15:EIO_D2 */
    GPIO_DRV_SetMuxModeSel(PORTE, 16, PORT_MUX_ALT6); /*PE16:EIO_D3 */

    (void)EIO_DRV_DeinitDevice(EIO_INDEX); //复位EIO配置
    (void)EIO_DRV_InitDevice(EIO_INDEX, &EIODeviceState);

    EIO_PWM_Config(EIO_PWM_TIMER_CH0, PWMCH0_PERIOD, PWMCH0_DUTYCYCLE, EIO_PWM_OUTPUTPIN0); //配置EIO_D0生成PWM波形
    EIO_PWM_Config(EIO_PWM_TIMER_CH1, PWMCH1_PERIOD, PWMCH1_DUTYCYCLE, EIO_PWM_OUTPUTPIN1); //配置EIO_D1生成PWM波形
    EIO_PWM_Config(EIO_PWM_TIMER_CH2, PWMCH2_PERIOD, PWMCH2_DUTYCYCLE, EIO_PWM_OUTPUTPIN2); //配置EIO_D2生成PWM波形
    EIO_PWM_Config(EIO_PWM_TIMER_CH3, PWMCH3_PERIOD, PWMCH3_DUTYCYCLE, EIO_PWM_OUTPUTPIN3); //配置EIO_D3生成PWM波形
}

/* =================================   EOF  ================================= */
