/*!
 * @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 "Board.h"
#include "stdio.h"
#include "g32a10xx_gpio.h"
#include "g32a10xx_adc.h"
#include "g32a10xx_misc.h"
#include "g32a10xx_usart.h"
#include "g32a10xx_dma.h"
#include "user_adc_config.h"
#include "string.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup ADC_Examples
  @{
  */

/** @addtogroup ADC_MultiChannelScan
  @{
  */

/** @addtogroup ADC_MultiChannelScan_Variables
  @{
  */

volatile uint16_t adcData[ADC_CH_SIZE];
static volatile uint8_t dmaIntFlag = 0u;
/**@} end of group ADC_MultiChannelScan_Variables */

/** @defgroup ADC_MultiChannelScan_Macros Macros
  @{
  */

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

/**@} end of group ADC_MultiChannelScan_Macros */

/** @defgroup ADC_MultiChannelScan_Functions Functions
  @{
  */

/* ADC init */
void Adc_Init(void);
/* DMA init */
void Dma_Init(void);
void Adc_MultiChannelPolling(void);

/*!
 * @brief     Main program
 *
 * @param     None
 *
 * @retval    None
 */
int main(void)
{
    /* COM1 init*/
    Board_ComInit(COM1);

    /* ADC1 initialization */
    Adc_Init();
    
    /* DMA initialization */
    Dma_Init();
    
    /* Wait until ADC is ready */
    while (0u == Adc_ReadStatusFlag(ADC_FLAG_ADRDY))
    {
        /* nothing */
    }
    
    /* ADC start conversion command */
    Adc_StartConversion();
    
    while (1)
    {
        if(dmaIntFlag == 1u)
        {
            Adc_MultiChannelPolling();
        }
        else
        {
            /* nothing */
        }
    }
}

/*!
 * @brief     DMA Init
 *
 * @param     None
 *
 * @retval    None
 */
void Dma_Init(void)
{
    /* Enable DMA clock */
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_DMA1);

    /* DMA Configure */
    Dma_Config(DMA_CHANNEL, &dmaConfig);

    /* Clear Transfer Complete interrupt flag */
    Dma_ClearIntFlag((uint32_t)DMA1_INT_FLAG_TF1);
    /* Enable DMA Interrupt*/
    Dma_EnableInterrupt(DMA_CHANNEL, (uint32_t)DMA_INT_TFIE);
    Nvic_EnableIrqRequest(DMA_CH1_IRQn, 2);

    Dma_Enable(DMA_CHANNEL);
}

/*!
 * @brief     ADC Init
 *
 * @param     None
 *
 * @retval    None
 */
void Adc_Init(void)
{
    /* Enable GPIOA clock */
    Rcm_EnableAhbPeriphClock(ADC_PERIPH_CLK_PORT0_4);

    /* Enable GPIOB clock */
    Rcm_EnableAhbPeriphClock(ADC_PERIPH_CLK_PORT5);
    
    /* GPIO configuration */
    Gpio_Config(ADC_INPUT_PORT0_4, &gpioConfig0_4);
    Gpio_Config(ADC_INPUT_PORT5, &gpioConfig5);
    
    /* 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_CHANNEL0, (uint8_t)ADC_SAMPLE_TIME);
    Adc_ConfigChannel((uint32_t)ADC_INPUT_CHANNEL1, (uint8_t)ADC_SAMPLE_TIME);
    Adc_ConfigChannel((uint32_t)ADC_INPUT_CHANNEL2, (uint8_t)ADC_SAMPLE_TIME);
    Adc_ConfigChannel((uint32_t)ADC_INPUT_CHANNEL3, (uint8_t)ADC_SAMPLE_TIME);
    Adc_ConfigChannel((uint32_t)ADC_INPUT_CHANNEL4, (uint8_t)ADC_SAMPLE_TIME);
    Adc_ConfigChannel((uint32_t)ADC_INPUT_CHANNEL5, (uint8_t)ADC_SAMPLE_TIME);

    /* Calibration */
    Adc_ReadCalibrationFactor();

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

/*!
 * @brief     ADC multi channel polling
 *
 * @param     None
 *
 * @retval    None
 */
void Adc_MultiChannelPolling(void)
{
    uint16_t voltage = 0u;
    uint32_t i = 0u;

    /* Calculate CH0 voltage */
    voltage = (adcData[0] * 3300U) / 4095U;
    printf("ADC CH[0] voltage = %d mV\r\n", (int)voltage);
    
    /* Calculate CH1 voltage */
    voltage = (adcData[1] * 3300U) / 4095U;
    printf("ADC CH[1] voltage = %d mV\r\n", (int)voltage);
    
    /* Calculate CH2 voltage */
    voltage = (adcData[2] * 3300U) / 4095U;
    printf("ADC CH[2] voltage = %d mV\r\n", (int)voltage);
    
    /* Calculate CH3 voltage */
    voltage = (adcData[3] * 3300U) / 4095U;
    printf("ADC CH[3] voltage = %d mV\r\n", (int)voltage);
    
    /* Calculate CH4 voltage */
    voltage = (adcData[4] * 3300U) / 4095U;
    printf("ADC CH[4] voltage = %d mV\r\n", (int)voltage);
    
    /* Calculate CH9 voltage */
    voltage = (adcData[5] * 3300U) / 4095U;
    printf("ADC CH[9] voltage = %d mV\r\n", (int)voltage);
    
    printf("\r\n");
    for (i = 0U; i < (uint32_t)ADC_CH_SIZE; i++)
    {
       adcData[i] = 0U;
    }
}

/*!
 * @brief       DMA_Interrupt
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Adc_DmaIsr(void)
{
    if (0u != Dma_ReadStatusFlag(DMA1_FLAG_TF1))
    {
        /* clear flag */
        Dma_ClearStatusFlag((uint32_t)DMA1_FLAG_TF1);
        dmaIntFlag = 1;
    }
    else
    {
         /*  nothing */
    }
}


#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)
    {
        /* 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) == (uint8_t)RESET)
    {
        /* nothing */
    }

    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 ADC_MultiChannelScan_Functions */
/**@} end of group ADC_MultiChannelScan */
/**@} end of group ADC_Examples */
/**@} end of group Examples */
