/** * @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; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO clock */ ADCx_CHANNEL_GPIO_CLK_ENABLE(); /* ADC3 Periph clock enable */ ADCx_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADC Channel GPIO pin configuration */ GPIO_InitStruct.Pin = ADCx_CHANNEL_DIFF_HIGH; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCx_CHANNEL_DIFF_HIGH_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = ADCx_CHANNEL_DIFF_LOW; HAL_GPIO_Init(ADCx_CHANNEL_DIFF_LOW_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the NVIC #################################################*/ /* NVIC configuration for ADC interrupt */ /* Priority: high-priority */ HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(ADC1_2_IRQn); }
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 }
STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) { if (!IS_ADC_CHANNEL(adc_obj->channel)) { return; } if (adc_obj->channel < ADC_NUM_GPIO_CHANNELS) { // Channels 0-16 correspond to real pins. Configure the GPIO pin in // ADC mode. const pin_obj_t *pin = pin_adc1[adc_obj->channel]; GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = pin->pin_mask; GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; GPIO_InitStructure.Pull = GPIO_NOPULL; HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure); } ADCx_CLK_ENABLE(); ADC_HandleTypeDef *adcHandle = &adc_obj->handle; adcHandle->Instance = ADCx; adcHandle->Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; adcHandle->Init.Resolution = ADC_RESOLUTION12b; adcHandle->Init.ScanConvMode = DISABLE; adcHandle->Init.ContinuousConvMode = DISABLE; adcHandle->Init.DiscontinuousConvMode = DISABLE; adcHandle->Init.NbrOfDiscConversion = 0; adcHandle->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; adcHandle->Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; adcHandle->Init.DataAlign = ADC_DATAALIGN_RIGHT; adcHandle->Init.NbrOfConversion = 1; adcHandle->Init.DMAContinuousRequests = DISABLE; adcHandle->Init.EOCSelection = DISABLE; HAL_ADC_Init(adcHandle); }
/** * @brief ADC MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param huart: UART handle pointer * @retval None */ void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* ADC3 Periph clock enable */ ADCx_CLK_ENABLE(); /* Enable GPIO clock ****************************************/ ADCx_REG_CHANNEL_GPIO_CLK_ENABLE(); ADCx_INJ_CHANNEL_GPIO_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADC1 Channel11 GPIO pin configuration */ GPIO_InitStruct.Pin = ADCx_REG_CHANNEL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCx_REG_CHANNEL_GPIO_PORT, &GPIO_InitStruct); /* ADC1 Channel12 GPIO pin configuration */ GPIO_InitStruct.Pin = ADCx_INJ_CHANNEL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCx_INJ_CHANNEL_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the NVIC #################################################*/ /* NVIC configuration for conversion complete complete interrupt */ HAL_NVIC_SetPriority(ADCx_IRQn, 15, 0); HAL_NVIC_EnableIRQ(ADCx_IRQn); }
/***************************************************************** * 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); }
adc::adc (void) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* ADC3 Periph clock enable */ ADCx_CLK_ENABLE() ; /* Enable GPIO clock ****************************************/ ADCx_CHANNEL_GPIO_CLK_ENABLE() ; /*##-2- Configure peripheral GPIO ##########################################*/ /* ADC3 Channel8 GPIO pin configuration */ GPIO_InitStruct.Pin = ADCx_CHANNEL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init (ADCx_CHANNEL_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the NVIC #################################################*/ /* NVIC configuration for DMA transfer complete interrupt (USART1_TX) */ HAL_NVIC_SetPriority (ADCx_IRQn, 15, 0); HAL_NVIC_EnableIRQ (ADCx_IRQn); ADC_ChannelConfTypeDef sConfig; /*##-1- Configure the ADC peripheral #######################################*/ AdcHandle.Instance = ADCx; AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV8; AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; AdcHandle.Init.ScanConvMode = DISABLE; AdcHandle.Init.ContinuousConvMode = ENABLE; AdcHandle.Init.DiscontinuousConvMode = DISABLE; AdcHandle.Init.NbrOfDiscConversion = 0; AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.NbrOfConversion = 1; AdcHandle.Init.DMAContinuousRequests = DISABLE; AdcHandle.Init.EOCSelection = DISABLE; if (HAL_ADC_Init (&AdcHandle) != HAL_OK) asm("bkpt 0"); /*##-2- Configure ADC regular channel ######################################*/ sConfig.Channel = ADCx_CHANNEL; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; sConfig.Offset = 0; if (HAL_ADC_ConfigChannel (&AdcHandle, &sConfig) != HAL_OK) asm("bkpt 0"); /*##-3- Start the conversion process and enable interrupt ##################*/ if (HAL_ADC_Start_IT (&AdcHandle) != HAL_OK) asm("bkpt 0"); }
/** * @brief ADC MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param huart: UART handle pointer * @retval None */ void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) { GPIO_InitTypeDef GPIO_InitStruct; static DMA_HandleTypeDef hdma_adc; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO clock */ ADCxyz_CHANNEL_GPIO_CLK_ENABLE(); /* ADCx Periph clock enable */ ADCx_CLK_ENABLE(); /* ADCy Periph clock enable */ ADCy_CLK_ENABLE(); /* ADCz Periph clock enable */ ADCz_CLK_ENABLE(); /* Enable DMA2 clock */ DMAxyz_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADCx and ADCy Channel12 GPIO pin configuration */ GPIO_InitStruct.Pin = ADCxyz_CHANNEL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCxyz_CHANNEL_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the DMA streams ##########################################*/ /* Set the parameters to be configured */ hdma_adc.Instance = ADCxyz_DMA_STREAM; hdma_adc.Init.Channel = ADCxyz_DMA_CHANNEL; hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE; hdma_adc.Init.MemInc = DMA_MINC_ENABLE; hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; hdma_adc.Init.Mode = DMA_CIRCULAR; hdma_adc.Init.Priority = DMA_PRIORITY_HIGH; hdma_adc.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_adc.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL; hdma_adc.Init.MemBurst = DMA_MBURST_SINGLE; hdma_adc.Init.PeriphBurst = DMA_PBURST_SINGLE; HAL_DMA_Init(&hdma_adc); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(hadc, DMA_Handle, hdma_adc); /*##-4- Configure the NVIC for DMA #########################################*/ /* NVIC configuration for DMA transfer complete interrupt */ HAL_NVIC_SetPriority(ADCxyz_DMA_IRQn, 0, 0); HAL_NVIC_EnableIRQ(ADCxyz_DMA_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; static DMA_HandleTypeDef DmaHandle; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO clock */ ADCx_CHANNEL_GPIO_CLK_ENABLE(); /* ADC1 Periph clock enable */ ADCx_CLK_ENABLE(); /* Enable DMA1 clock */ __HAL_RCC_DMA1_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADC1 Channel9 GPIO pin configuration */ GPIO_InitStruct.Pin = ADCx_CHANNEL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCx_CHANNEL_GPIO_PORT, &GPIO_InitStruct); /*##- 3- Configure 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_WORD; DmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; DmaHandle.Init.Mode = DMA_CIRCULAR; 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 the ADC handle */ __HAL_LINKDMA(hadc, DMA_Handle, DmaHandle); /*##-4- Configure the NVIC for DMA #########################################*/ /* NVIC configuration for DMA transfer complete interrupt */ HAL_NVIC_SetPriority(ADCx_DMA_IRQn, 5, 0); HAL_NVIC_EnableIRQ(ADCx_DMA_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; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* ADC3 Periph clock enable */ ADCx_CLK_ENABLE(); /* Enable GPIO clock ****************************************/ ADCx_CHANNEL_GPIO_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADC Channel GPIO pin configuration */ GPIO_InitStruct.Pin = ADCx_CHANNEL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCx_CHANNEL_GPIO_PORT, &GPIO_InitStruct); }
/*====================================================================================================*/ void HAL_ADC_MspInit( ADC_HandleTypeDef *hadc ) { static DMA_HandleTypeDef DMA_HandleStruct; GPIO_InitTypeDef GPIO_InitStruct; RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct; /* Enable clock ******************************************************************/ ADCx_GPIO_CLK_ENABLE(); ADCx_CLK_ENABLE(); ADCx_DMA_CLK_ENABLE(); /* Enable asynchronous clock source of ADCx **************************************/ RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC12; RCC_PeriphCLKInitStruct.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV1; HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct); /* Config GPIO ******************************************************************/ GPIO_InitStruct.Pin = ADCx_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCx_GPIO_PORT, &GPIO_InitStruct); /* Config DMA *****************************************************************/ DMA_HandleStruct.Instance = ADCx_DMA_CHANNEL; DMA_HandleStruct.Init.Direction = DMA_PERIPH_TO_MEMORY; DMA_HandleStruct.Init.PeriphInc = DMA_PINC_DISABLE; DMA_HandleStruct.Init.MemInc = DMA_MINC_ENABLE; DMA_HandleStruct.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; DMA_HandleStruct.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; DMA_HandleStruct.Init.Mode = DMA_CIRCULAR; DMA_HandleStruct.Init.Priority = DMA_PRIORITY_MEDIUM; HAL_DMA_DeInit(&DMA_HandleStruct); HAL_DMA_Init(&DMA_HandleStruct); __HAL_LINKDMA(hadc, DMA_Handle, DMA_HandleStruct); /* Config NVIC *****************************************************************/ // DMA interrupt - Priority: high-priority HAL_NVIC_SetPriority(ADCx_DMA_IRQn, 1, 0); HAL_NVIC_EnableIRQ(ADCx_DMA_IRQn); // 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: * - Configuration of peripheral clock * - Configuration of GPIO associated to the peripheral channels * - Configuration of DMA * - Configuration of interruptions * @param huart: UART handle pointer * @retval None */ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) { GPIO_InitTypeDef GPIO_InitStruct; static DMA_HandleTypeDef hdma_adc; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO clock */ ADCxy_CHANNEL_GPIO_CLK_ENABLE(); /* ADCx Periph clock enable */ ADCx_CLK_ENABLE(); /* ADCy Periph clock enable */ ADCy_CLK_ENABLE(); /* Enable DMA clock */ DMAxy_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADCx and ADCy Channel GPIO pin configuration */ GPIO_InitStruct.Pin = ADCxy_CHANNEL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCxy_CHANNEL_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the DMA channels ##########################################*/ /* Set the parameters to be configured */ hdma_adc.Instance = ADCxy_DMA_STREAM; /* DMA channel of ADC1 (ADC master) */ hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE; hdma_adc.Init.MemInc = DMA_MINC_ENABLE; hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; /* Transfer by half-word to match with ADC resolution 6 or 8 bits */ hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; /* Transfer by half-word to match with ADC resolution 6 or 8 bits */ hdma_adc.Init.Mode = DMA_CIRCULAR; /* DMA in circular mode to match with ADC-DMA continuous requests */ hdma_adc.Init.Priority = DMA_PRIORITY_HIGH; HAL_DMA_Init(&hdma_adc); /* Associate the initialized DMA handle to the the ADC handle */ __HAL_LINKDMA(hadc, DMA_Handle, hdma_adc); /*##-4- Configure the NVIC for DMA #########################################*/ /* NVIC configuration for DMA transfer complete interrupt */ HAL_NVIC_SetPriority(ADCxy_DMA_IRQn, 0, 0); HAL_NVIC_EnableIRQ(ADCxy_DMA_IRQn); }
/** * @brief 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; RCC_OscInitTypeDef RCC_OscInitStructure; /*##-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(); /* Note: STM32L0 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 stm32l0_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); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADCx Channel GPIO pin configuration */ GPIO_InitStruct.Pin = ADCx_CHANNELa_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCx_CHANNELa_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the DMA ##################################################*/ /* ADC does not use DMA in this ADC example */ /*##-4- Configure the NVIC #################################################*/ /* 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: * - 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; static DMA_HandleTypeDef DmaHandle; RCC_PeriphCLKInitTypeDef PeriphClkInit; /*##-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(); /* Note: In case of usage of asynchronous clock derived from ADC dedicated */ /* PLL 72MHz, with ADC setting */ /* "AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1", */ /* the clock source has to be enabled at RCC top level using macro */ /* "__HAL_RCC_ADC12_CONFIG(RCC_ADC12PLLCLK_DIV1)" or function */ /* "HAL_RCCEx_PeriphCLKConfig()" (refer to comments in file */ /* "stm32f3_hal_adc.c_ex" header). */ /* Enable asynchronous clock source of ADCx */ PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12; PeriphClkInit.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV1; HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); /* 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; 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.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: * - 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; static DMA_HandleTypeDef DmaHandle; RCC_OscInitTypeDef RCC_OscInitStructure; /*##-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(); /* Note: In case of usage of asynchronous clock derived from ADC dedicated */ /* HSI RC oscillator 14MHz, with ADC setting */ /* "AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC", */ /* the clock source has to be enabled at RCC top level using function */ /* "HAL_RCC_OscConfig()" (see comments in stm32l1_hal_adc.c header) */ /* Enable asynchronous clock source of ADCx */ /* (place oscillator HSI14 under control of the ADC) */ HAL_RCC_GetOscConfig(&RCC_OscInitStructure); RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14; RCC_OscInitStructure.HSI14CalibrationValue = RCC_HSI14CALIBRATION_DEFAULT; RCC_OscInitStructure.HSI14State = RCC_HSI14_ADC_CONTROL; HAL_RCC_OscConfig(&RCC_OscInitStructure); /* Enable clock of DMA associated to the peripheral */ ADCx_DMA_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADCx Channel GPIO pin configuration */ GPIO_InitStruct.Pin = ADCx_CHANNELa_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 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.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 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; 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); }
void ADC_Config( void ) { DMA_InitTypeDef DMA_InitStruct; ADC_InitTypeDef ADC_InitStruct; ADC_CommonInitTypeDef ADC_CommonInitStruct; GPIO_InitTypeDef GPIO_InitStruct; /* ADC Clk *******************************************************************/ RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2); ADCx_CLK_ENABLE(); ADCx_DMA_CLK_ENABLE(); /* ADC Pin *******************************************************************/ GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Pin = ADCxP_PIN; GPIO_Init(ADCxP_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = ADCxN_PIN; GPIO_Init(ADCxN_GPIO_PORT, &GPIO_InitStruct); /* ADC DMA *******************************************************************/ DMA_DeInit(ADCx_DMA_CHANNEL); DMA_InitStruct.DMA_PeripheralBaseAddr = ADCx_DR_ADDRESS; DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)ADC_DMA_ConvBuf; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStruct.DMA_BufferSize = ADC_BUF_CHENNAL * ADC_BUF_SIZE; DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_Medium; DMA_InitStruct.DMA_M2M = DMA_M2M_Disable; DMA_Init(ADCx_DMA_CHANNEL, &DMA_InitStruct); DMA_Cmd(ADCx_DMA_CHANNEL, ENABLE); /* ADC Calibration ***********************************************************/ ADC_VoltageRegulatorCmd(ADCx, ENABLE); delay_ms(10); ADC_SelectCalibrationMode(ADCx, ADC_CalibrationMode_Single); ADC_StartCalibration(ADCx); while(ADC_GetCalibrationStatus(ADCx) != RESET); calibrationValue = ADC_GetCalibrationValue(ADCx); /* ADC Common Init ***********************************************************/ ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Interleave; // ADC_Mode_Independent ADC_CommonInitStruct.ADC_Clock = ADC_Clock_AsynClkMode; ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_1; ADC_CommonInitStruct.ADC_DMAMode = ADC_DMAMode_Circular; ADC_CommonInitStruct.ADC_TwoSamplingDelay = 0; ADC_CommonInit(ADCx, &ADC_CommonInitStruct); /* ADC Init *****************************************************************/ ADC_InitStruct.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable; ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; ADC_InitStruct.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStruct.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStruct.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStruct.ADC_NbrOfRegChannel = ADC_BUF_SIZE; ADC_Init(ADCx, &ADC_InitStruct); /* ADC Regular Config *******************************************************/ ADC_RegularChannelConfig(ADCx, ADCxP_CHANNEL, 1, ADC_SampleTime_601Cycles5); ADC_RegularChannelConfig(ADCx, ADCxN_CHANNEL, 2, ADC_SampleTime_601Cycles5); /* Enable & Start ***********************************************************/ ADC_DMACmd(ADCx, ENABLE); ADC_Cmd(ADCx, ENABLE); while(!ADC_GetFlagStatus(ADCx, ADC_FLAG_RDY)); DMA_Cmd(ADCx_DMA_CHANNEL, ENABLE); ADC_StartConversion(ADCx); }
/** * @brief ADC MSP Init * @param hadc : ADC handle * @retval None */ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) { GPIO_InitTypeDef GPIO_InitStruct; static DMA_HandleTypeDef DmaHandle; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO clock ****************************************/ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); /* ADC1 Periph clock enable */ ADCx_CLK_ENABLE(); /* Enable DMA1 clock */ __HAL_RCC_DMA1_CLK_ENABLE(); /*##- 2- Configure peripheral GPIO #########################################*/ /* ADC Channel GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_3; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_0; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_0; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_1; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_2; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_3; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /*##- 3- Configure 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; DmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; DmaHandle.Init.Mode = DMA_CIRCULAR; DmaHandle.Init.Priority = DMA_PRIORITY_MEDIUM; /* Deinitialize & Initialize the DMA for new transfer */ HAL_DMA_DeInit(&DmaHandle); HAL_DMA_Init(&DmaHandle); /* Associate the DMA handle */ __HAL_LINKDMA(hadc, DMA_Handle, DmaHandle); /* NVIC configuration for DMA Input data interrupt */ HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 1, 0); HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); /* NVIC configuration for ADC interrupt */ /* Priority: high-priority */ HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(ADC1_2_IRQn); }
/** * @brief 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; static DMA_HandleTypeDef DmaHandle; RCC_PeriphCLKInitTypeDef PeriphClkInit; /*##-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(); /* Configure ADCx clock prescaler */ /* Caution: On STM32F1, ADC clock frequency max is 14MHz (refer to device */ /* datasheet). */ /* Therefore, ADC clock prescaler must be configured in function */ /* of ADC clock source frequency to remain below this maximum */ /* frequency. */ PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); /* 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; 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.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: * - 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; static 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(); /* Note: In case of usage of ADC dedicated asynchronous clock, with ADC */ /* setting "AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC", */ /* the clock source has to be enabled using this macro: */ /* __HAL_RCC_ADC12_CONFIG(RCC_ADC12PLLCLK_DIV1); */ /* Enable asynchronous clock source of ADCx */ __HAL_RCC_ADC1_CONFIG(RCC_ADC1PLLCLK_DIV1); /* Enable clock of DMA associated to the peripheral */ ADCx_DMA_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADCx Channel GPIO pin configuration */ GPIO_InitStruct.Pin = ADCx_CHANNELa_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCx_CHANNELa_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the DMA streams ##########################################*/ /* Configure DMA parameters */ DmaHandle.Instance = ADCx_DMA_STREAM; DmaHandle.Init.Direction = DMA_PERIPH_TO_MEMORY; DmaHandle.Init.PeriphInc = DMA_PINC_DISABLE; DmaHandle.Init.MemInc = DMA_MINC_ENABLE; DmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; /* Transfer from ADC by word to match with 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; 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); }