STATIC void adc_wait_for_eoc_or_timeout(int32_t timeout) { uint32_t tickstart = HAL_GetTick(); #if defined(STM32F4) || defined(STM32F7) while ((ADCx->SR & ADC_FLAG_EOC) != ADC_FLAG_EOC) { #elif defined(STM32F0) || defined(STM32H7) || defined(STM32L4) while (READ_BIT(ADCx->ISR, ADC_FLAG_EOC) != ADC_FLAG_EOC) { #else #error Unsupported processor #endif if (((HAL_GetTick() - tickstart ) > timeout)) { break; // timeout } } } STATIC void adcx_clock_enable(void) { #if defined(STM32F0) || defined(STM32F4) || defined(STM32F7) ADCx_CLK_ENABLE(); #elif defined(STM32H7) __HAL_RCC_ADC3_CLK_ENABLE(); __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_CLKP); #elif defined(STM32L4) __HAL_RCC_ADC_CLK_ENABLE(); #else #error Unsupported processor #endif }
/***************************************************************** * ADC MSP initialization * * This function configures the hardware resources used in this example: * - Enable clock of ADC peripheral * - Configure the GPIO associated to the peripheral channels * - Configure the DMA associated to the peripheral * - Configure the NVIC associated to the peripheral interruptions * @param hadc: ADC handle pointer * @retval None */ void HAL_ADC_MspInit (ADC_HandleTypeDef *hadc) { GPIO_InitTypeDef GPIO_InitStruct; DMA_HandleTypeDef DmaHandle; /*##-1- Enable peripherals and GPIO Clocks ###################*/ /* Enable clock of GPIO associated to the peripheral channels */ ADCx_CHANNELa_GPIO_CLK_ENABLE(); /* Enable clock of ADCx peripheral */ ADCx_CLK_ENABLE(); /* ADC Periph interface clock configuration */ __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); /* Enable clock of DMA associated to the peripheral */ ADCx_DMA_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ############################*/ /* Configure GPIO pin of the selected ADC channel */ GPIO_InitStruct.Pin = ADCx_CHANNELa_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCx_CHANNELa_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the DMA ####################################*/ /* Configure DMA parameters */ DmaHandle.Instance = ADCx_DMA; DmaHandle.Init.Request = DMA_REQUEST_0; 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(ADCx_DMA_IRQn, 1, 0); HAL_NVIC_EnableIRQ(ADCx_DMA_IRQn); /* NVIC configuration for ADC interrupt */ /* Priority: high-priority */ HAL_NVIC_SetPriority(ADCx_IRQn, 0, 0); HAL_NVIC_EnableIRQ(ADCx_IRQn); }
/** * @brief ADC MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param hadc: ADC handle pointer * @retval None */ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) { GPIO_InitTypeDef GPIO_InitStruct; GPIO_TypeDef *port; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* ADC Periph clock enable */ if(hadc->Instance == ADC1) { #ifdef __HAL_RCC_ADC1_CLK_ENABLE __HAL_RCC_ADC1_CLK_ENABLE(); #endif #ifdef __HAL_RCC_ADC12_CLK_ENABLE __HAL_RCC_ADC12_CLK_ENABLE(); #endif } #ifdef ADC2 else if(hadc->Instance == ADC2) { #ifdef __HAL_RCC_ADC2_CLK_ENABLE __HAL_RCC_ADC2_CLK_ENABLE(); #endif #ifdef __HAL_RCC_ADC12_CLK_ENABLE __HAL_RCC_ADC12_CLK_ENABLE(); #endif } #endif #ifdef ADC3 else if(hadc->Instance == ADC3) { #ifdef __HAL_RCC_ADC3_CLK_ENABLE __HAL_RCC_ADC3_CLK_ENABLE(); #endif #ifdef __HAL_RCC_ADC34_CLK_ENABLE __HAL_RCC_ADC34_CLK_ENABLE(); #endif } #endif #ifdef ADC4 else if(hadc->Instance == ADC4) { __HAL_RCC_ADC34_CLK_ENABLE(); } #endif #ifdef __HAL_RCC_ADC_CLK_ENABLE __HAL_RCC_ADC_CLK_ENABLE(); #endif /* For STM32F1xx, ADC prescaler is confgured in SystemClock_Config (variant.cpp) */ #if defined(__HAL_RCC_ADC_CONFIG) && !defined(STM32F1xx) /* ADC Periph interface clock configuration */ __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); #endif /* Enable GPIO clock ****************************************/ port = set_GPIO_Port_Clock(STM_PORT(g_current_pin)); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADC Channel GPIO pin configuration */ GPIO_InitStruct.Pin = STM_GPIO_PIN(g_current_pin); #ifdef GPIO_MODE_ANALOG_ADC_CONTROL GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL; #else GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; #endif GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(port, &GPIO_InitStruct); }