/**
  ******************************************************************************
  * @file    ADC/AnalogWatchdog/main.c 
  * @author  IOT Development Team
  * @version V1.0.0
  * @date    10-January-2020
  * @brief   Main program body
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "apm32f10x.h"
#include "platform_config.h"

/** @addtogroup APM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup ADC_AnalogWatchdog
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_ConfigStruct_T  ADC_configStruct;
    
/* Private function prototypes -----------------------------------------------*/
void RCM_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_apm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_apm32f10x.c file
     */     
       
  /* System clocks configuration ---------------------------------------------*/
  RCM_Configuration();

  /* NVIC configuration ------------------------------------------------------*/
  NVIC_Configuration();

  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();

  /* Configure LED GPIO Pin ------------------------------------------------- */
#if defined(APM32_MINI)
    APM_MINI_LEDInit(LED2);
#elif defined (APM32_DISCOVERY)
    APM_DISCOVERY_LEDInit(LED2);
#elif defined(APM32F103V_EVAL)	
    APM_EVAL_LEDInit(LED1);
#endif
  /* ADC1 Configuration ------------------------------------------------------*/
  ADC_configStruct.Mode = ADC_MODE_INDEPENDENT;
  ADC_configStruct.ScanConvMode = DISABLE;
  ADC_configStruct.ContinuosConvMode = ENABLE;
  ADC_configStruct.ExternalTrigConv = ADC_EXTERNALTRIGCONV_None;
  ADC_configStruct.DataAlign = ADC_DATA_ALIGN_RIGHT;
  ADC_configStruct.NbrOfChannel = ADC_CHANNEL_1;
  ADC_Config(ADC1, &ADC_configStruct);

  /* ADC1 regular channel14 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_14, 1, ADC_SAMPLE_TIME_13CYCLE5);

  /* Configure high and low analog watchdog thresholds */
  ADC_AnalogWatchdogThresholdsConfig(ADC1, 0x0B00, 0x0300);
  /* Configure channel14 as the single analog watchdog guarded channel */
  ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_CHANNEL_14);
  /* Enable analog watchdog on one regular channel */
  ADC_AnalogWatchdogEnable(ADC1, ADC_ANALOG_WATCHDOG_SINGLE_REG_ENABLE);

  /* Enable AWD interrupt */
  ADC_EnableInterrupt(ADC1, ADC_IT_AWD);

  /* Enable ADC1 */
  ADC_Enable(ADC1);

  /* Enable ADC1 reset calibration register */   
  ADC_Calibration_Reset(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_ReadCalibrationResetFlag(ADC1));

  /* Start ADC1 calibration */
  ADC_CalibrationStart(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_ReadCalibrationStartFlag(ADC1));

  /* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConvEnable(ADC1);

  while (1)
  {
  }
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCM_Configuration(void)
{
  /* ADCCLK = PCLK2/2 */
  RCM_ConfigADCCLK(RCM_PCLK2_DIV_2);
/* Enable peripheral clocks --------------------------------------------------*/
  /* Enable ADC1 and GPIO_LED clock */
  RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_ADC1);
}

/**
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
  GPIO_ConfigStruct_T GPIO_configStruct;

  /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
  GPIO_configStruct.pin = GPIO_PIN_4;
  GPIO_configStruct.mode = GPIO_MODE_ANALOG;
  GPIO_Config(GPIOC, &GPIO_configStruct);
}

/**
  * @brief  Configures NVIC and Vector Table base location.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)             
{
  /* Configure and enable ADC interrupt */
  NVIC_EnableIRQRequest(ADC1_2_IRQn, 0x00, 0x00);
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}

#endif

/**
  * @}
  */ 

/**
  * @}
  */ 

