/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2026-02-25
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the Geehy Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "inttypes.h"
#include "Board.h"
#include "stdio.h"
#include "g32a10xx_gpio.h"
#include "g32a10xx_misc.h"
#include "g32a10xx_dma.h"
#include "g32a10xx_adc.h"
#include "g32a10xx_tmr.h"
#include "user_dma_config.h"
#include "g32a10xx_syscfg.h"
#include "g32a10xx_int.h"
/** @addtogroup Examples
  @{
  */

/** @addtogroup DMA_Examples
  @{
  */

/** @addtogroup DMA_ADCTIMTransfer
  @{
  */

/** @defgroup DMA_ADCTIMTransfer_Macros Macros
  @{
  */

/* printf function configs to USART1*/
#define DEBUG_USART  USART1

/**@} end of group DMA_ADCTIMTransfer_Macros */

/** @defgroup DMA_ADCTIMTransfer_Functions Functions
  @{
  */

/* ADC Init */
static void Adc_Init(void);
/* DMA Init */
static void Dma_Init(void);
/* TMR1 PWM initiate*/
static void Tmr1_PWMOutInit(void);
/* TMR3 config */
static void Tmr3_Init(void);
/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    /* TMR1 PWM config */
    Tmr1_PWMOutInit();
    
    /* TMR3 config */
    Tmr3_Init();
    
    /* ADC initialization */
    Adc_Init();
    
    /* DMA initialization */
    Dma_Init();

    /* Wait until ADC is ready */
    while ((uint8_t)RESET == Adc_ReadStatusFlag(ADC_FLAG_ADRDY))
    {
        /* do nothing */
    }
    
    /* ADC start conversion command */
    Adc_StartConversion();

    for (;;)
    {
    }
}

/*!
 * @brief     ADC Init
 *
 * @param     None
 *
 * @retval    None
 */
static void Adc_Init(void)
{
    /* Enable GPIOB clock */
    Rcm_EnableAhbPeriphClock(ADC_PERIPH_CLK_PORT);
    
    /* GPIO configuration */
    Gpio_Config(ADC_INPUT_PORT, &adcGpioConfig);
    
    /* Enable ADC clock */
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_ADC1);

    /* ADC reset */
    Adc_Reset();
    /* ADC configuration */
    Adc_Config(&adcConfig);

    /* ADC channel Convert configuration */
    Adc_ConfigChannel((uint32_t)ADC_INPUT_CHANNEL, (uint8_t)ADC_SAMPLE_TIME);

    /* Calibration */
    Adc_ReadCalibrationFactor();

    /* Enable ADC DMA*/
    Adc_EnableDma();
    Adc_DmaRequestMode(ADC_DMA_MODE_CIRCULAR);
    
    /* Enable ADC */
    Adc_Enable();
}

/*!
* @brief       DMA Init
*
* @param       None
*
* @retval      None
*
* @note
*/
static void Dma_Init(void)
{
    /* Enable DMA clock */
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_DMA1);
    /* DMA Configure */
    Dma_Config(DMA_CHANNEL, &dmaConfig);
    /* Enable DMA */
    Dma_Enable(DMA_CHANNEL);
}


/*!
 * @brief       TMR1 PWM Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
static void Tmr1_PWMOutInit(void)
{
    /* Enable Clock */
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOC);

    /*  Connect TMR1 to CH1 */
    Gpio_ConfigPinAF(GPIOC, GPIO_PIN_SOURCE_10, GPIO_AF_PIN2);
    /* Configure PWM gpio pin */
    Gpio_Config(GPIOC, &pwmGpioConfig);
    
    /* Enable Clock*/
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1);
    /* Configure timer */
    Tmr_ConfigTimeBase(TMR1, &time1BaseConfig);
    Tmr_OC1Config(TMR1, &time1Occonfig);
    /* Enable PWM output ,frequency = 1MHz/4095 = 244Hz*/
    Tmr1_EnablePWMOutputs();

    /* Enable master slave mode */
    Tmr_EnableMasterSlaveMode(TMR1);
    /* Select update signal as TRGO source */
    Tmr_SelectOutputTrigger(TMR1, TMR_TRGOSOURCE_UPDATE);

    /*  Enable TMR1  */
    Tmr_Enable(TMR1);
}

/*!
 * @brief       TMR3 Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
static void Tmr3_Init(void)
{
    /* Enable Clock*/
    Rcm_EnableApb1PeriphClock(RCM_APB1_PERIPH_TMR3);
    
    /* Configure time3 */
    Tmr_ConfigTimeBase(TMR3, &time3BaseConfig);

    /* Configure time3occonfig */
    Tmr_OC1Config(TMR3, &time3Occonfig);

    /* Select update signal as TRG3 source */
    Tmr_SelectOutputTrigger(TMR3, TMR_TRGOSOURCE_UPDATE);

    /*  Enable TMR3  */
    Tmr_Enable(TMR3);
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, (uint8_t)ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == (uint8_t)RESET)
    {
        /* do nothing */
    }

    return (ch);
}

#elif defined (__GNUC__)

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @retval      The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

    return ch;
}

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       file:  Meaningless in this function.
*
* @param       *ptr:  Buffer pointer for data to be sent.
*
* @param       len:  Length of data to be sent.
*
* @retval      The characters that need to be send.
*
* @note
*/
int _write(int file, char* ptr, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        __io_putchar(*ptr++);
    }

    return len;
}

#else
#pragma message("Not supported compiler type")
#endif

/**@} end of group DMA_ADCTIMTransfer_Functions */
/**@} end of group DMA_ADCTIMTransfer */
/**@} end of group DMA_Examples */
/**@} end of group Examples */
