/**
  ******************************************************************************
  * @file     ADC_CFG.c
  * @author   SINOMCU-AE
  * @brief    ADC config
  *    
  *          This file provides functions to ADC config:
  *              ADC OPAMP1 out,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"
#include "SysTick_Delay.h"

/**
  * @brief  ADC Initialization Function
  * @param  None
  * @retval None
  */
void adc_init(void) {
  MS32_ADC_InitTypeDef AdcInitStruct = {0};
  MS32_ADC_REG_InitTypeDef AdcRegInitStruct = {0};

  //from here,configure need ADSTART=0   
  //if (MS32_ADC_IsEnabled(ADC1) == 0)                // ADC must be disabled. 
  MS32_ADC_StructInit(&AdcInitStruct); 
  AdcInitStruct.Clock = MS32_ADC_CLOCK_ASYNC;         // need ADC close
  AdcInitStruct.Resolution = MS32_ADC_RESOLUTION_12B;
  AdcInitStruct.DataAlignment = MS32_ADC_DATA_ALIGN_RIGHT;
  AdcInitStruct.LowPowerMode = MS32_ADC_LP_MODE_NONE;
  MS32_ADC_Init(ADC1, &AdcInitStruct);                // init function had enable clock
  
  MS32_ADC_REG_SetSequencerChAdd(ADC1, MS32_ADC_CHANNEL_OPAMP1); 

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

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

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