void ADC_Config(void) { ADC_ChannelConfTypeDef sConfig; ADC_AnalogWDGConfTypeDef AnalogWDGConfig; /* Configuration of ADCx init structure: ADC parameters and regular group */ AdcHandle.Instance = ADC1; AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; /* Sequencer will convert the number of channels configured below, successively from the lowest to the highest channel number */ AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; AdcHandle.Init.LowPowerAutoWait = ENABLE; AdcHandle.Init.LowPowerAutoPowerOff = DISABLE; AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */ AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */ AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_TRGO; /* Trig of conversion start done by external event */ AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; AdcHandle.Init.DMAContinuousRequests = ENABLE; AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; AdcHandle.Init.SamplingTime = ADC_SAMPLETIME_39CYCLES_5; if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { /* ADC initialization error */ char message[] = "HAL_ADC_Init\r\n"; HAL_UART_Transmit(&huart2, (uint8_t*)message, sizeof(message) - 1, HAL_MAX_DELAY); Error_Handler(); } /* Configuration of channel on ADCx regular group on sequencer rank 1 */ /* Note: Considering IT occurring after each ADC conversion if ADC */ /* conversion is out of the analog watchdog window selected (ADC IT */ /* enabled), select sampling time and ADC clock with sufficient */ /* duration to not create an overhead situation in IRQHandler. */ sConfig.Channel = ADC_CHANNEL_4; if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { /* Channel Configuration Error */ char message[] = "HAL_ADC_ConfigChannel\r\n"; HAL_UART_Transmit(&huart2, (uint8_t*)message, sizeof(message) - 1, HAL_MAX_DELAY); Error_Handler(); } /* Set analog watchdog thresholds in order to be between steps of DAC */ /* voltage. */ AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG; AnalogWDGConfig.Channel = ADC_CHANNEL_4; AnalogWDGConfig.ITMode = ENABLE; AnalogWDGConfig.HighThreshold = 3000; AnalogWDGConfig.LowThreshold = 0; HAL_ADC_AnalogWDGConfig(&AdcHandle, &AnalogWDGConfig); } void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) { GPIO_InitTypeDef GPIO_InitStruct; static DMA_HandleTypeDef DmaHandle; RCC_OscInitTypeDef RCC_OscInitStructure; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable clock of GPIO associated to the peripheral channels */ __HAL_RCC_GPIOA_CLK_ENABLE(); /* Enable clock of ADCx peripheral */ __HAL_RCC_ADC1_CLK_ENABLE(); /* Ensure that MSI is wake-up system clock */ __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI); /* Note: STM32L0xx ADC is using a dedicated asynchronous clock derived */ /* from HSI RC oscillator 16MHz. */ /* The clock source has to be enabled at RCC top level using function */ /* "HAL_RCC_OscConfig()" (see comments in stm32l0xx_hal_adc.c header) */ /* Enable asynchronous clock source of ADCx */ HAL_RCC_GetOscConfig(&RCC_OscInitStructure); RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStructure.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStructure.HSIState = RCC_HSI_ON; HAL_RCC_OscConfig(&RCC_OscInitStructure); /* Enable clock of DMA associated to the peripheral */ __HAL_RCC_DMA1_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* Configure GPIO pin of the selected ADC channel */ GPIO_InitStruct.Pin = GPIO_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* NVIC configuration for ADC interrupt */ HAL_NVIC_SetPriority(ADC1_COMP_IRQn, 0, 0); HAL_NVIC_EnableIRQ(ADC1_COMP_IRQn); /*##-3- Configure the DMA ##################################################*/ /* Configure DMA parameters */ DmaHandle.Instance = DMA1_Channel1; DmaHandle.Init.Direction = DMA_PERIPH_TO_MEMORY; DmaHandle.Init.PeriphInc = DMA_PINC_DISABLE; DmaHandle.Init.MemInc = DMA_MINC_ENABLE; DmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; /* Transfer from ADC by half-word to match with ADC configuration: ADC resolution 10 or 12 bits */ DmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; /* Transfer to memory by half-word to match with buffer variable type: half-word */ DmaHandle.Init.Mode = DMA_CIRCULAR; /* DMA in circular mode to match with ADC configuration: DMA continuous requests */ DmaHandle.Init.Priority = DMA_PRIORITY_HIGH; /* Deinitialize & Initialize the DMA for new transfer */ HAL_DMA_DeInit(&DmaHandle); HAL_DMA_Init(&DmaHandle); /* Associate the initialized DMA handle to the ADC handle */ __HAL_LINKDMA(hadc, DMA_Handle, DmaHandle); /*##-4- Configure the NVIC #################################################*/ /* NVIC configuration for DMA interrupt (transfer completion or error) */ /* Priority: high-priority */ HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); }