/*!
 * @file        user_dma_config.c
 *
 * @brief       DMA configurations
 *
 * @version     V1.0.0
 *
 * @date        2026-03-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 "user_dma_config.h"
#include "g32a10xx_gpio.h"
#include "g32a10xx_tmr.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup DMA_Examples
  @{
  */

/** @addtogroup DMA
  @{
*/

/** @defgroup DMA_Variables
  @{
*/

extern uint16_t adcData[ADC_CH_SIZE];

/* Configure PWM gpio pin */
Gpio_ConfigType pwmGpioConfig = {
    .mode  = GPIO_MODE_AF,
    .outtype = GPIO_OUT_TYPE_PP,
    .pin   = (uint16_t)GPIO_PIN_10,
    .pupd  = GPIO_PUPD_NO,
    .speed = GPIO_SPEED_50MHz,
};

/* Configure ADC gpio pin */
Gpio_ConfigType adcGpioConfig = {
    .pin        = (uint16_t)GPIO_PIN_1,
    .mode       = GPIO_MODE_AN,
    .outtype    = GPIO_OUT_TYPE_PP,
    .speed      = GPIO_SPEED_10MHz,
    .pupd       = GPIO_PUPD_NO,
};


/* Configure adc */
Adc_ConfigType adcConfig = {
    /* Set resolution*/
    .resolution            = ADC_RESOLUTION_12B,
    /* Set scanDir*/
    .scanDir               = ADC_SCAN_DIR_UPWARD,
    /* Set convMode continous*/
    .convMode              = ADC_CONVERSION_CONTINUOUS,
    /* Set dataAlign*/
    .dataAlign             = ADC_DATA_ALIGN_RIGHT,
    /* Set extTrigEdge*/
    .extTrigEdge           = ADC_EXT_TRIG_EDGE_NONE,
    /* Set extTrigConv*/
    .extTrigConv           = ADC_EXT_TRIG_CONV_TRG0,
};


/* Configure dma */
Dma_ConfigType dmaConfig = {
    /* size of buffer*/
    .bufferSize = ADC_CH_SIZE,
    /* set memory Data Size*/
    .memoryDataSize = DMA_MEMORY_DATASIZE_HALFWORD,
    /* Set peripheral Data Size*/
    .peripheralDataSize = DMA_PERIPHERAL_DATASIZE_HALFWORD,
    /* Enable Memory Address increase*/
    .memoryInc = DMA_MEMORY_INC_DISABLE,
    /* Disable Peripheral Address increase*/
    .peripheralInc = DMA_PERIPHERAL_INC_DISABLE,
    /* Reset Circular Mode*/
    .circular = DMA_CIRCULAR_ENABLE,
    /* Disable M2M*/
    .memoryTomemory = DMA_M2M_DISABLE,
    /* set priority*/
    .priority = DMA_PRIORITY_LEVEL_HIGHT,
    /* read from peripheral*/
    .direction = DMA_DIR_PERIPHERAL,
    /* Set memory Address*/
    .memoryAddress = (uint32_t)&TMR1->CC1_R.CC1,
    /* Set Peripheral Address, ADC->DATA = [0-4095] ,the frequency range is 0% to 100% */
    .peripheralAddress = (uint32_t)&ADC->DATA_R.DATA,
};

/* Configure timer */
Tmr_TimeBaseType  timeBaseConfig = {
    /* Set clockDivision = 1 */
    .clockDivision =  TMR_CKD_DIV1,
    /* Up-counter */
    .counterMode =  TMR_COUNTER_MODE_UP,
    /* Set divider = 63 .So TMR1 clock freq = 64/(63+1) = 1MHz */
    .div = 64-1,
    /* Set counter = 4095-1 */
    .period = 4095-1,
    /* Repetition counter = 0x0 */
    .repetitionCounter =  0,
};


Tmr_OcConfigType  occonfig = {
    /* PWM1 mode */
    .OC_Mode =  TMR_OC_MODE_PWM1,
    /* Enable CH1 ouput */
    .OC_OutputState  =  TMR_OUTPUT_STATE_ENABLE,
    /* Set compare value = 1024 ,default duty cycle is 1024/4095 = 25% */
    .Pulse = 1024,
    .OC_OutputNState = TMR_OUTPUT_NSTATE_DISABLE,
    .OC_Polarity = TMR_OC_POLARITY_HIGH,
    .OC_NPolarity = TMR_OC_NPOLARITY_HIGH,
    .OC_Idlestate = TMR_OCIDLESTATE_RESET,
    .OC_NIdlestate = TMR_OCNIDLESTATE_RESET,
};


/**@} end of group DMA_Variables */
/**@} end of group DMA */
/**@} end of group DMA_Examples */
/**@} end of group Examples */
