/**
  ******************************************************************************
  * @file     ADC_CFG.c
  * @author   SINOMCU-AE
  * @brief    ADC config
  *    
  *          This file provides functions to ADC config:
  *              ADC IN1(PA1),software Trig
  *         
  ******************************************************************************
  * @attention 
  *                           DISCLAIMER
  * The present firmware which is for guidance only aims at providing customers
  * with coding information regarding their products in order for them to save
  * time. As a result, SinoMCU shall not be held liable for any direct,indirect
  * or consequential damages with respect to any claims arising from the content 
  * of such firmware and/or the use made by customers of the coding information 
  * contained herein in connection with their products.
  *
  * <h2><center>&copy; Copyright (c) 2021 sinomcu All rights reserved.</center></h2>
  *
  ******************************************************************************
  */

#include "ADC_CFG.h"

/**
  * @brief  ADC IN1 Pin PA1 Initialization
  * @param  None
  * @retval None
  */
void ADC_IN1_Pin_Init(void)
{
  MS32_GPIO_InitTypeDef GPIO_InitStruct = {0};

  MS32_AHB1_GRP1_EnableClock(MS32_AHB1_GRP1_PERIPH_GPIOA);
  // PA1   --> ADC_IN1
  MS32_GPIO_StructInit(&GPIO_InitStruct);  
  GPIO_InitStruct.Pin = MS32_GPIO_PIN_1;
  GPIO_InitStruct.Mode = MS32_GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = MS32_GPIO_PULL_NO;
  MS32_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

/**
  * @brief  ADC Initialization Function
  * @param  None
  * @retval None
  */
void ADC_Init(void)
{
  MS32_ADC_InitTypeDef ADC_InitStruct = {0};
  MS32_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};

  ADC_IN1_Pin_Init();
  MS32_APB1_GRP2_EnableClock(MS32_APB1_GRP2_PERIPH_ADC1);

  // from here,configure need ADSTART=0   

  MS32_ADC_REG_SetSequencerChAdd(ADC1, MS32_ADC_CHANNEL_1); 

  // if (MS32_ADC_IsEnabled(ADC1) == 0)                    // ADC must be disabled. 
  MS32_ADC_StructInit(&ADC_InitStruct); 
  ADC_InitStruct.Clock = MS32_ADC_CLOCK_ASYNC;             // need ADC close
  ADC_InitStruct.Resolution = MS32_ADC_RESOLUTION_12B;
  ADC_InitStruct.DataAlignment = MS32_ADC_DATA_ALIGN_RIGHT;
  ADC_InitStruct.LowPowerMode = MS32_ADC_LP_MODE_NONE;
  MS32_ADC_Init(ADC1, &ADC_InitStruct);

  MS32_ADC_REG_StructInit(&ADC_REG_InitStruct);
  ADC_REG_InitStruct.TriggerSource = MS32_ADC_REG_TRIG_SOFTWARE;
  ADC_REG_InitStruct.SequencerDiscont = MS32_ADC_REG_SEQ_DISCONT_DISABLE;
  ADC_REG_InitStruct.ContinuousMode = MS32_ADC_REG_CONV_SINGLE;
  ADC_REG_InitStruct.DMATransfer = MS32_ADC_REG_DMA_TRANSFER_LIMITED;
  ADC_REG_InitStruct.Overrun = MS32_ADC_REG_OVR_DATA_PRESERVED;
  MS32_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);

  MS32_ADC_REG_SetSequencerScanDirection(ADC1, MS32_ADC_REG_SEQ_SCAN_DIR_FORWARD);
  MS32_ADC_SetSamplingTimeCommonChannels(ADC1, MS32_ADC_SAMPLINGTIME_1CYCLE_5);  
}

/******************************** END OF FILE *********************************/
