STATIC void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) { adcx_clock_enable(); adch->Instance = ADCx; adch->Init.Resolution = resolution; adch->Init.ContinuousConvMode = DISABLE; adch->Init.DiscontinuousConvMode = DISABLE; #if !defined(STM32F0) adch->Init.NbrOfDiscConversion = 0; adch->Init.NbrOfConversion = 1; #endif adch->Init.EOCSelection = ADC_EOC_SINGLE_CONV; adch->Init.ExternalTrigConv = ADC_SOFTWARE_START; adch->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; #if defined(STM32F0) || defined(STM32F4) || defined(STM32F7) adch->Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; adch->Init.ScanConvMode = DISABLE; adch->Init.DataAlign = ADC_DATAALIGN_RIGHT; adch->Init.DMAContinuousRequests = DISABLE; #elif defined(STM32H7) adch->Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; adch->Init.BoostMode = ENABLE; adch->Init.ScanConvMode = DISABLE; adch->Init.LowPowerAutoWait = DISABLE; adch->Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; adch->Init.OversamplingMode = DISABLE; adch->Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; adch->Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; #elif defined(STM32L4) adch->Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; adch->Init.ScanConvMode = ADC_SCAN_DISABLE; adch->Init.LowPowerAutoWait = DISABLE; adch->Init.Overrun = ADC_OVR_DATA_PRESERVED; adch->Init.OversamplingMode = DISABLE; adch->Init.DataAlign = ADC_DATAALIGN_RIGHT; adch->Init.DMAContinuousRequests = DISABLE; #else #error Unsupported processor #endif #if defined(STM32F0) adch->Init.SamplingTimeCommon = ADC_SAMPLETIME_71CYCLES_5; #endif HAL_ADC_Init(adch); #if defined(STM32H7) HAL_ADCEx_Calibration_Start(adch, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED); #endif #if defined(STM32L4) HAL_ADCEx_Calibration_Start(adch, ADC_SINGLE_ENDED); #endif }
void analogin_init(analogin_t *obj, PinName pin) { uint32_t function = (uint32_t)NC; // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) // are described in PinNames.h and PeripheralPins.c // Pin value must be between 0xF0 and 0xFF if ((pin < 0xF0) || (pin >= 0x100)) { // Normal channels // Get the peripheral name from the pin and assign it to the object obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC); // Get the functions (adc channel) from the pin and assign it to the object function = pinmap_function(pin, PinMap_ADC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC); } else { // Internal channels obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC_Internal); function = pinmap_function(pin, PinMap_ADC_Internal); // No GPIO configuration for internal channels } MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC); MBED_ASSERT(function != (uint32_t)NC); obj->channel = STM_PIN_CHANNEL(function); // Save pin number for the read function obj->pin = pin; // Configure ADC object structures obj->handle.State = HAL_ADC_STATE_RESET; obj->handle.Init.OversamplingMode = DISABLE; obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1; obj->handle.Init.Resolution = ADC_RESOLUTION_12B; obj->handle.Init.SamplingTime = ADC_SAMPLETIME_160CYCLES_5; obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; obj->handle.Init.ContinuousConvMode = DISABLE; obj->handle.Init.DiscontinuousConvMode = DISABLE; obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE; obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here obj->handle.Init.DMAContinuousRequests = DISABLE; obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN; obj->handle.Init.LowPowerAutoWait = ENABLE; obj->handle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz obj->handle.Init.LowPowerAutoPowerOff = DISABLE; __HAL_RCC_ADC1_CLK_ENABLE(); if (HAL_ADC_Init(&obj->handle) != HAL_OK) { error("Cannot initialize ADC"); } if (!HAL_ADCEx_Calibration_GetValue(&obj->handle, ADC_SINGLE_ENDED)) { HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED); } __HAL_ADC_ENABLE(&obj->handle); }
static int adc_resume(struct soc_device *dev, enum suspend_state_t state) { static ADC_Channel chan; switch (state) { case PM_MODE_SLEEP: break; case PM_MODE_STANDBY: case PM_MODE_HIBERNATION: HAL_ADC_Init(&hal_adc_param); for (chan = ADC_CHANNEL_0; chan < ADC_CHANNEL_NUM; chan++) { if (hal_adc_chan_config[chan].is_config) HAL_ADC_ConfigChannel(chan, (ADC_Select)hal_adc_chan_config[chan].select, (ADC_IRQMode)hal_adc_chan_config[chan].irqmode, hal_adc_chan_config[chan].lowValue, hal_adc_chan_config[chan].highValue); } break; default: break; } hal_adc_suspending = 0; return 0; }
/** * @brief ADC configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_ChannelConfTypeDef sConfig; /* ADC Initialization */ AdcHandle.Instance = ADCx; AdcHandle.Init.ScanConvMode = DISABLE; AdcHandle.Init.ContinuousConvMode = DISABLE; AdcHandle.Init.DiscontinuousConvMode = DISABLE; AdcHandle.Init.NbrOfDiscConversion = 0; AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.NbrOfConversion = 1; if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { /* ADC Initiliazation Error */ Error_Handler(); } /* Configure ADC regular channel */ sConfig.Channel = ADCx_CHANNEL; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5; if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { /* Channel Configuration Error */ Error_Handler(); } }
/* ADC init function */ void MX_ADC_Init(void) { ADC_ChannelConfTypeDef sConfig; //Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) hadc.Instance = ADC1; hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC ; hadc.Init.Resolution = ADC_RESOLUTION12b; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; hadc.Init.EOCSelection = EOC_SINGLE_CONV; hadc.Init.LowPowerAutoWait = DISABLE; //**** hadc.Init.LowPowerAutoPowerOff = DISABLE; hadc.Init.ContinuousConvMode = ENABLE; //*** hadc.Init.DiscontinuousConvMode = DISABLE; //**** hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc.Init.DMAContinuousRequests = DISABLE; hadc.Init.Overrun = OVR_DATA_PRESERVED; HAL_ADC_Init(&hadc); //Configure for the selected ADC regular channel to be converted. /* sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; HAL_ADC_ConfigChannel(&hadc, &sConfig); */ }
/* ADC init function */ void MX_ADC_Init(void) { //ADC_ChannelConfTypeDef sConfig; /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc.Instance = ADC1; hadc.Init.OversamplingMode = DISABLE; hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; hadc.Init.Resolution = ADC_RESOLUTION12b; hadc.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.ContinuousConvMode = ENABLE; hadc.Init.DiscontinuousConvMode = DISABLE; hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE; hadc.Init.DMAContinuousRequests = DISABLE; hadc.Init.EOCSelection = EOC_SINGLE_CONV; hadc.Init.Overrun = OVR_DATA_PRESERVED; hadc.Init.LowPowerAutoWait = DISABLE; hadc.Init.LowPowerFrequencyMode = DISABLE; hadc.Init.LowPowerAutoPowerOff = DISABLE; HAL_ADC_Init(&hadc); /**Configure for the selected ADC regular channel to be converted. */ //sConfig.Channel = ADC_CHANNEL_11; //sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; //HAL_ADC_ConfigChannel(&hadc, &sConfig); HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED); }
/** * @brief ADC configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_ChannelConfTypeDef sConfig; /* ADC Initialization */ AdcHandle.Instance = ADCx; AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */ 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; /* Conversion start trigged at each external event */ AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; AdcHandle.Init.DMAContinuousRequests = ENABLE; AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { /* ADC initialization Error */ Error_Handler(); } /* Configure ADC regular channel */ sConfig.Channel = ADCx_CHANNEL; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { /* Channel Configuration Error */ Error_Handler(); } }
/* ADC2 init function */ void MX_ADC2_Init(void) { ADC_ChannelConfTypeDef sConfig; /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc2.Instance = ADC2; hadc2.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; hadc2.Init.Resolution = ADC_RESOLUTION12b; hadc2.Init.ScanConvMode = DISABLE; hadc2.Init.ContinuousConvMode = DISABLE; hadc2.Init.DiscontinuousConvMode = DISABLE; hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO; hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc2.Init.NbrOfConversion = 1; hadc2.Init.DMAContinuousRequests = DISABLE; hadc2.Init.EOCSelection = EOC_SINGLE_CONV; HAL_ADC_Init(&hadc2); /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_2; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; HAL_ADC_ConfigChannel(&hadc2, &sConfig); }
void analogin_init(analogin_t *obj, PinName pin) { // Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC); // Save pin number for the read function obj->pin = pin; // The ADC initialization is done once if (adc_inited == 0) { adc_inited = 1; // Enable ADC clock __ADC1_CLK_ENABLE(); // Configure ADC AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); 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 Configures the ADC. * @param None * @retval None */ static void ADC_Configuration(void) { ADC_ChannelConfTypeDef sConfig; /* ADC3 Configuration ------------------------------------------------------*/ hadc.Instance = ADC3; hadc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; hadc.Init.Resolution = ADC_RESOLUTION_12B; hadc.Init.ScanConvMode = DISABLE; hadc.Init.ContinuousConvMode = ENABLE; hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.NbrOfConversion = 1; HAL_ADC_Init(&hadc); /* ADC3 Regular Channel Config */ sConfig.Channel = ADC_CHANNEL_8; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES; sConfig.Offset = 0; HAL_ADC_ConfigChannel(&hadc, &sConfig); /* Enable EOC interupt */ HAL_ADC_Start(&hadc); }
/* ADC1 init function */ void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig; /* Enable ADC peripheral */ __HAL_RCC_ADC1_CLK_ENABLE(); /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; hadc1.Init.Resolution = ADC_RESOLUTION_12B; hadc1.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_TRGO; hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.DMAContinuousRequests = ENABLE; hadc1.Init.EOCSelection = 0; HAL_ADC_Init(&hadc1); /**Configure for the selected ADC regular channels */ sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; HAL_ADC_ConfigChannel(&hadc1, &sConfig); }
/* ADC1 init function */ void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig; /**Common config */ hadc1.Instance = ADC1; hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 2; HAL_ADC_Init(&hadc1); /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_8; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /**Configure Regular Channel */ sConfig.Rank = 2; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_9; HAL_ADC_ConfigChannel(&hadc1, &sConfig); }
/* ADC4 init function */ void MX_ADC4_Init(void) { ADC_ChannelConfTypeDef sConfig; /**Common config */ hadc4.Instance = ADC4; hadc4.Init.ClockPrescaler = ADC_CLOCK_ASYNC; hadc4.Init.Resolution = ADC_RESOLUTION12b; hadc4.Init.ScanConvMode = ADC_SCAN_DISABLE; hadc4.Init.ContinuousConvMode = DISABLE; hadc4.Init.DiscontinuousConvMode = DISABLE; hadc4.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; hadc4.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO; hadc4.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc4.Init.NbrOfConversion = 1; hadc4.Init.DMAContinuousRequests = DISABLE; hadc4.Init.EOCSelection = EOC_SINGLE_CONV; hadc4.Init.LowPowerAutoWait = DISABLE; hadc4.Init.Overrun = OVR_DATA_OVERWRITTEN; HAL_ADC_Init(&hadc4); /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_3; sConfig.Rank = 1; sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; sConfig.OffsetNumber = ADC_OFFSET_NONE; sConfig.Offset = 0; HAL_ADC_ConfigChannel(&hadc4, &sConfig); }
/* ADC1 init function */ void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig; /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; hadc1.Init.Resolution = ADC_RESOLUTION_10B; hadc1.Init.ScanConvMode = DISABLE; hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 1; hadc1.Init.DMAContinuousRequests = DISABLE; hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); } /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_0; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } }
/** * @brief Initializes ADC HAL. * @param None * @retval None */ static void ADCx_Init(void) { if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET) { /* ADC Config */ hnucleo_Adc.Instance = NUCLEO_ADCx; hnucleo_Adc.Init.OversamplingMode = DISABLE; hnucleo_Adc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; /* (must not exceed 16MHz) */ hnucleo_Adc.Init.LowPowerAutoPowerOff = DISABLE; hnucleo_Adc.Init.LowPowerFrequencyMode = ENABLE; hnucleo_Adc.Init.LowPowerAutoWait = ENABLE; hnucleo_Adc.Init.Resolution = ADC_RESOLUTION_12B; hnucleo_Adc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; hnucleo_Adc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; hnucleo_Adc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hnucleo_Adc.Init.ContinuousConvMode = DISABLE; hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE; hnucleo_Adc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hnucleo_Adc.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hnucleo_Adc.Init.DMAContinuousRequests = DISABLE; ADCx_MspInit(&hnucleo_Adc); HAL_ADC_Init(&hnucleo_Adc); } }
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 Configure ADC1 for being used with HRTIM * @param None * @retval None */ static void ADC_Config(void) { ADC_MultiModeTypeDef MultiModeConfig; ADC_InjectionConfTypeDef InjectionConfig; AdcHandle.Instance = ADC1; /* ADC2 is working independently */ MultiModeConfig.DMAAccessMode = ADC_DMAACCESSMODE_DISABLED; MultiModeConfig.Mode = ADC_MODE_INDEPENDENT; MultiModeConfig.TwoSamplingDelay = ADC_TWOSAMPLINGDELAY_1CYCLE; HAL_ADCEx_MultiModeConfigChannel(&AdcHandle, &MultiModeConfig); /* ADC2 global initialization */ /* 12-bit right-aligned format, discontinuous scan mode, running from PLL */ AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC; AdcHandle.Init.Resolution = ADC_RESOLUTION12b; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.ScanConvMode = ENABLE; AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV; AdcHandle.Init.LowPowerAutoWait = DISABLE; AdcHandle.Init.ContinuousConvMode = DISABLE; AdcHandle.Init.NbrOfConversion = 1; AdcHandle.Init.DiscontinuousConvMode = DISABLE; AdcHandle.Init.NbrOfDiscConversion = 1; AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; AdcHandle.Init.DMAContinuousRequests = DISABLE; AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN; HAL_ADC_Init(&AdcHandle); /* Discontinuous injected mode: 1st injected conversion for Vin on Ch2 */ InjectionConfig.InjectedChannel = ADC_CHANNEL_2; InjectionConfig.InjectedRank = ADC_INJECTED_RANK_1; InjectionConfig.InjectedSamplingTime = ADC_SAMPLETIME_7CYCLES_5; InjectionConfig.InjectedSingleDiff = ADC_SINGLE_ENDED; InjectionConfig.InjectedOffsetNumber = ADC_OFFSET_NONE; InjectionConfig.InjectedOffset = 0; InjectionConfig.InjectedNbrOfConversion = 2; InjectionConfig.InjectedDiscontinuousConvMode = DISABLE; InjectionConfig.AutoInjectedConv = DISABLE; InjectionConfig.QueueInjectedContext = DISABLE; InjectionConfig.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJECCONV_HRTIM_TRG2; InjectionConfig.ExternalTrigInjecConvEdge = ADC_EXTERNALTRIGINJECCONV_EDGE_RISING; HAL_ADCEx_InjectedConfigChannel(&AdcHandle, &InjectionConfig); /* Configure the 2nd injected conversion for Vout on Ch4 */ InjectionConfig.InjectedChannel = ADC_CHANNEL_4; InjectionConfig.InjectedRank = ADC_INJECTED_RANK_2; InjectionConfig.InjectedSamplingTime = ADC_SAMPLETIME_19CYCLES_5; HAL_ADCEx_InjectedConfigChannel(&AdcHandle, &InjectionConfig); /* Run the ADC calibration in single-ended mode */ HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED); /* Start ADC2 Injected Conversions */ HAL_ADCEx_InjectedStart(&AdcHandle); }
HAL_StatusTypeDef ADC_INIT(ADC_HandleTypeDef* AdcHandle) { GPIO_InitTypeDef gpioInit; ADC_ChannelConfTypeDef adcChannel; __GPIOC_CLK_ENABLE(); __ADC3_CLK_ENABLE(); gpioInit.Pin = GPIO_PIN_10; gpioInit.Mode = GPIO_MODE_ANALOG; gpioInit.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOF, &gpioInit); HAL_NVIC_SetPriority(ADC_IRQn, 0, 0); HAL_NVIC_EnableIRQ(ADC_IRQn); AdcHandle->Instance = ADC3; AdcHandle->Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV8; AdcHandle->Init.Resolution = ADC_RESOLUTION_8B; 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 = ENABLE; AdcHandle->Init.EOCSelection = DISABLE; if (HAL_ADC_Init(AdcHandle) != HAL_OK) { /* ADC initialization Error */ return HAL_ERROR; } adcChannel.Channel = ADC_CHANNEL_8; adcChannel.Rank = 1; /* ADC_SAMPLETIME_3CYCLES ADC_SAMPLETIME_15CYCLES ADC_SAMPLETIME_28CYCLES ADC_SAMPLETIME_56CYCLES ADC_SAMPLETIME_84CYCLES ADC_SAMPLETIME_112CYCLES ADC_SAMPLETIME_144CYCLES ADC_SAMPLETIME_480CYCLES */ adcChannel.SamplingTime = ADC_SAMPLETIME_3CYCLES; adcChannel.Offset = 0; if (HAL_ADC_ConfigChannel(AdcHandle, &adcChannel) != HAL_OK) { /* Channel Configuration Error */ return HAL_ERROR; } return HAL_OK; }
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"); }
/* ADC1 init function */ void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig; ADC_InjectionConfTypeDef sConfigInjected; /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; hadc1.Init.Resolution = ADC_RESOLUTION12b; hadc1.Init.ScanConvMode = ENABLE; hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = ENABLE; hadc1.Init.NbrOfDiscConversion = 3; hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T4_CC4; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 3; hadc1.Init.DMAContinuousRequests = ENABLE; hadc1.Init.EOCSelection = EOC_SEQ_CONV; HAL_ADC_Init(&hadc1); /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_7; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_15; sConfig.Rank = 3; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; sConfig.Rank = 2; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /**Configures for the selected ADC injected channel its corresponding rank in the sequencer and its sample time */ sConfigInjected.InjectedChannel = ADC_CHANNEL_14; sConfigInjected.InjectedRank = 1; sConfigInjected.InjectedNbrOfConversion = 1; sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_3CYCLES; sConfigInjected.ExternalTrigInjecConvEdge = ADC_EXTERNALTRIGINJECCONVEDGE_RISING; sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJECCONV_T5_TRGO; sConfigInjected.AutoInjectedConv = DISABLE; sConfigInjected.InjectedDiscontinuousConvMode = DISABLE; sConfigInjected.InjectedOffset = 0; HAL_ADCEx_InjectedConfigChannel(&hadc1, &sConfigInjected); }
/** * @brief Update power module instance adc measurements. * @param power_module: pointer to power module instance for which to get adc measurements. * @retval None. */ void EPS_update_power_module_state(EPS_PowerModule *power_module){ /*initialize adc handle*/ power_module->hadc_power_module->Init.NbrOfConversion = 2; //power_module->hadc_power_module->NbrOfConversionRank = 2; HAL_ADC_Init(power_module->hadc_power_module); /*setup conversion sequence for */ ADC_ChannelConfTypeDef sConfig; sConfig.SamplingTime = ADC_SAMPLETIME_192CYCLES; /*power module current*/ sConfig.Channel = power_module->ADC_channel_current ; sConfig.Rank = 1; HAL_ADC_ConfigChannel(power_module->hadc_power_module, &sConfig); /*power module voltage*/ sConfig.Channel = power_module->ADC_channel_voltage ; sConfig.Rank = 2; HAL_ADC_ConfigChannel(power_module->hadc_power_module, &sConfig); /*start dma transfer from adc to memory.*/ uint32_t adc_measurement_dma_power_modules[67]= { 0 };//2*64 +1 adc_reading_complete = ADC_TRANSFER_NOT_COMPLETED;//external global flag defined in main and shared with the adc complete transfer interrupt handler. HAL_ADC_Start_DMA(power_module->hadc_power_module, adc_measurement_dma_power_modules, 66); /*Process Measurements*/ uint32_t voltage_avg =0; uint32_t current_avg =0; /*Wait till DMA ADC sequence transfer is ready*/ while(adc_reading_complete==ADC_TRANSFER_NOT_COMPLETED){ //wait for dma transfer complete. } /*ADC must be stopped in the adc dma transfer complete callback.*/ HAL_ADC_Stop_DMA(power_module->hadc_power_module); /*de-interleave and sum voltage and current measurements.*/ for (int sum_index = 2; sum_index < 66; sum_index+=2) { /*top*/ current_avg = current_avg + adc_measurement_dma_power_modules[sum_index]; voltage_avg = voltage_avg + adc_measurement_dma_power_modules[sum_index+1]; } /*filter ting*/ /*average of 16 concecutive adc measurements.skip the first to avoid adc power up distortion.*/ power_module->voltage = voltage_avg>>5; power_module->current = current_avg>>5; }
void js_analogRead(CScriptVar *v, void *userdata){ int type = v->getParameter("type")->getInt(); int pin = v->getParameter("pin")->getInt(); if(Types[type] == 0 || Pins[pin] ==0){ v->getReturnVar()->setInt(0); return; } if(AdcHandle.Instance != ADC3){ AdcHandle.Instance = ADC3; 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; if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { v->getReturnVar()->setInt(0); return; } } ADC_ChannelConfTypeDef sConfig; sConfig.Channel = Pins[pin]; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; sConfig.Offset = 0; if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { v->getReturnVar()->setInt(0); return; } if (HAL_ADC_Start(&AdcHandle) != HAL_OK) { v->getReturnVar()->setInt(0); return; } HAL_ADC_PollForConversion(&AdcHandle, 10); if (HAL_ADC_GetState(&AdcHandle) == HAL_ADC_STATE_EOC_REG) { uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle); } v->getReturnVar()->setInt(uhADCxConvertedValue); return; }
/** * @brief ADC configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_ChannelConfTypeDef sConfig; ADC_InjectionConfTypeDef sConfigInjected; /* ADC Initialization */ AdcHandle.Instance = ADCx; AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV6; 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) { /* Turn LED3 on: in case of Initialization Error */ Error_Handler(); } /* Configure ADC regular channel */ sConfig.Channel = ADCx_REG_CHANNEL; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES; sConfig.Offset = 0; if(HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { /* Turn LED3 on: in case of Channel Configuration Error */ Error_Handler(); } /* Configure ADC injected channel */ sConfigInjected.InjectedNbrOfConversion = 1; sConfigInjected.InjectedChannel = ADCx_INJ_CHANNEL; sConfigInjected.InjectedRank = 1; sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_56CYCLES; sConfigInjected.InjectedOffset = 0; sConfigInjected.ExternalTrigInjecConvEdge = ADC_EXTERNALTRIGINJECCONVEDGE_NONE; sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJECCONV_T1_CC4; sConfigInjected.AutoInjectedConv = DISABLE; sConfigInjected.InjectedDiscontinuousConvMode = DISABLE; if(HAL_ADCEx_InjectedConfigChannel(&AdcHandle, &sConfigInjected) != HAL_OK) { /* Channel Configuration Error */ Error_Handler(); } }
/** * @brief ADC configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_ChannelConfTypeDef sConfig; ADC_InjectionConfTypeDef sConfigInjected; /* Configuration of ADCx init structure: ADC parameters and regular group */ AdcHandle.Instance = ADCx; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.ScanConvMode = ADC_SCAN_DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */ AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */ AdcHandle.Init.NbrOfConversion = 1; /* Parameter discarded because sequencer is disabled */ AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */ AdcHandle.Init.NbrOfDiscConversion = 1; /* Parameter discarded because sequencer is disabled */ AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_Tx_TRGO; /* Trig of conversion start done by external event */ if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { /* ADC initialization error */ Error_Handler(); } /* Configuration of channel on ADCx regular group on sequencer rank 1 */ /* Note: Considering IT occurring after each number of */ /* "ADCCONVERTEDVALUES_BUFFER_SIZE" ADC conversions (IT by DMA end */ /* of transfer), select sampling time and ADC clock with sufficient */ /* duration to not create an overhead situation in IRQHandler. */ sConfig.Channel = ADCx_CHANNELa; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_41CYCLES_5; if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { /* Channel Configuration Error */ Error_Handler(); } /* Configure ADC injected channel */ sConfigInjected.InjectedChannel = ADC_CHANNEL_VREFINT; sConfigInjected.InjectedRank = ADC_INJECTED_RANK_1; sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_28CYCLES_5; sConfigInjected.InjectedOffset = 0; sConfigInjected.InjectedNbrOfConversion = 1; sConfigInjected.InjectedDiscontinuousConvMode = DISABLE; sConfigInjected.AutoInjectedConv = DISABLE; sConfigInjected.ExternalTrigInjecConv = ADC_INJECTED_SOFTWARE_START; if (HAL_ADCEx_InjectedConfigChannel(&AdcHandle, &sConfigInjected) != HAL_OK) { /* Channel Configuration Error */ Error_Handler(); } }
/*====================================================================================================*/ void ADC_Config( void ) { ADC_ChannelConfTypeDef ADC_ChannelConfStruct; HAL_StatusTypeDef state; /* Config ADC *****************************************************************/ ADC_HandleStruct.Instance = ADCx; ADC_HandleStruct.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; ADC_HandleStruct.Init.Resolution = ADC_RESOLUTION_12B; ADC_HandleStruct.Init.DataAlign = ADC_DATAALIGN_RIGHT; ADC_HandleStruct.Init.ScanConvMode = ENABLE; ADC_HandleStruct.Init.EOCSelection = ADC_EOC_SINGLE_CONV; ADC_HandleStruct.Init.LowPowerAutoWait = DISABLE; ADC_HandleStruct.Init.ContinuousConvMode = ENABLE; ADC_HandleStruct.Init.NbrOfConversion = ADC_BUF_CHENNAL; ADC_HandleStruct.Init.DiscontinuousConvMode = ENABLE; ADC_HandleStruct.Init.NbrOfDiscConversion = 1; ADC_HandleStruct.Init.ExternalTrigConv = ADC_SOFTWARE_START; ADC_HandleStruct.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; ADC_HandleStruct.Init.DMAContinuousRequests = ENABLE; ADC_HandleStruct.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; HAL_ADC_DeInit(&ADC_HandleStruct); state = HAL_ADC_Init(&ADC_HandleStruct); if(state != HAL_OK) while(1) { ; } /* Config ADC Chennal **********************************************************/ ADC_ChannelConfStruct.SamplingTime = ADC_SAMPLETIME_601CYCLES_5; ADC_ChannelConfStruct.SingleDiff = ADC_SINGLE_ENDED; ADC_ChannelConfStruct.OffsetNumber = ADC_OFFSET_NONE; ADC_ChannelConfStruct.Offset = 0; // ADC1_CH1 Channel ADC_ChannelConfStruct.Channel = ADCx_CHANNEL; ADC_ChannelConfStruct.Rank = ADC_REGULAR_RANK_1; state = HAL_ADC_ConfigChannel(&ADC_HandleStruct, &ADC_ChannelConfStruct); if(state != HAL_OK) while(1) { ; } // ADC1_CH2 Channel // ADC_ChannelConfStruct.Channel = ADCx_2_CHANNEL; // ADC_ChannelConfStruct.Rank = ADC_REGULAR_RANK_2; // state = HAL_ADC_ConfigChannel(&ADC_HandleStruct, &ADC_ChannelConfStruct); // if(state != HAL_OK) // while(1) { ; } /* Setup ADC *******************************************************************/ state = HAL_ADCEx_Calibration_Start(&ADC_HandleStruct, ADC_SINGLE_ENDED); if(state != HAL_OK) while(1) { ; } state = HAL_ADC_Start_DMA(&ADC_HandleStruct, (uint32_t *)ADC_DMA_ConvBuf, ADC_BUF_SIZE * ADC_BUF_CHENNAL); if(state != HAL_OK) while(1) { ; } }
/** * @brief CustomHID_Init * Initializes the CUSTOM HID media low layer * @param None * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CustomHID_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; ADC_ChannelConfTypeDef sConfig; /* Configure the ADC peripheral */ AdcHandle.Instance = ADCx; AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; 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 = ENABLE; AdcHandle.Init.EOCSelection = DISABLE; HAL_ADC_Init(&AdcHandle); /* Configure ADC regular channel */ sConfig.Channel = ADCx_CHANNEL; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5; sConfig.Offset = 0; HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); /* Start the conversion process and enable interrupt */ HAL_ADC_Start_DMA(&AdcHandle, (uint32_t*)&ADCConvertedValue, 1); /* Configure LED1, LED2, LED3 and LED4 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); BSP_LED_Init(LED4); /* Enable GPIOE clock */ __HAL_RCC_GPIOE_CLK_ENABLE(); /* Configure PE6 pin as input floating */ GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING_FALLING; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Pin = GPIO_PIN_6; HAL_GPIO_Init(GPIOE, &GPIO_InitStructure); /* Enable and set EXTI15_10 Interrupt to the lowest priority */ HAL_NVIC_SetPriority(EXTI9_5_IRQn, 3, 0); HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); return (0); }
/** ADC Configuration * @brief Configures ADC1 Channel 16 so that temperature values can be read */ void ADC_config(void) { // TODO: Make this configuration proper so that it actually works ADC_ChannelConfTypeDef ADC1_ch16; // Initialize values for ADC1 handle type def ADC1_Handle.Instance = ADC1; ADC1_Handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; ADC1_Handle.Init.Resolution = ADC_RESOLUTION_12B; ADC1_Handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; ADC1_Handle.Init.ScanConvMode = DISABLE; ADC1_Handle.Init.EOCSelection = DISABLE; ADC1_Handle.Init.ContinuousConvMode = ENABLE; // ADC1_Handle.Init.DMAContinuousRequests = DISABLE; ADC1_Handle.Init.NbrOfConversion = 1; ADC1_Handle.Init.DiscontinuousConvMode = DISABLE; ADC1_Handle.Init.NbrOfDiscConversion = 0; ADC1_Handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; // ADC1_Handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; // // Initialize values for temperature sensor (Temperature analog channel is Ch16 of ADC1) ADC1_ch16.Channel = ADC_CHANNEL_16; ADC1_ch16.Rank = 1; ADC1_ch16.SamplingTime = ADC_SAMPLETIME_480CYCLES; ADC1_ch16.Offset = 0; // Enable ADC clock __ADC1_CLK_ENABLE(); // Initialize clock with error handling if(HAL_ADC_Init(&ADC1_Handle)!=HAL_OK){ //Error_Handler(ADC_INIT_FAIL); printf("adc init fail\n"); } // Configure temperature sensor peripheral HAL_ADC_ConfigChannel(&ADC1_Handle, &ADC1_ch16); HAL_ADC_Start(&ADC1_Handle); // Allot values to the kalman filtration struct for the temperature sensor kalman_temperature.q = 0.3; kalman_temperature.r = 1.2; kalman_temperature.x = 1000.0; kalman_temperature.p = 0.0; kalman_temperature.k = 0.0; // Initialize temperature sensor mutex temperatureMutex = osMutexCreate(temperatureMutexPtr); }
/* ADC init function */ void MX_ADC_Init(void) { ADC_ChannelConfTypeDef sConfig; /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc.Instance = ADC1; hadc.Init.OversamplingMode = DISABLE; hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1; hadc.Init.Resolution = ADC_RESOLUTION_12B; hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.ContinuousConvMode = DISABLE; hadc.Init.DiscontinuousConvMode = DISABLE; hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc.Init.DMAContinuousRequests = DISABLE; hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED; hadc.Init.LowPowerAutoWait = DISABLE; hadc.Init.LowPowerFrequencyMode = DISABLE; hadc.Init.LowPowerAutoPowerOff = DISABLE; if (HAL_ADC_Init(&hadc) != HAL_OK) { Error_Handler(); } /**Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_5; sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } /**Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_6; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } /**Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_7; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } }
/* ADC1 init function */ void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig; ADC_InjectionConfTypeDef sConfigInjected; /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; hadc1.Init.Resolution = ADC_RESOLUTION_12B; hadc1.Init.ScanConvMode = DISABLE; hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 1; hadc1.Init.DMAContinuousRequests = DISABLE; hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; if (HAL_ADC_Init(&hadc1) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_6; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configures for the selected ADC injected channel its corresponding rank in the sequencer and its sample time */ sConfigInjected.InjectedChannel = ADC_CHANNEL_6; sConfigInjected.InjectedRank = 1; sConfigInjected.InjectedNbrOfConversion = 1; sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_3CYCLES; sConfigInjected.ExternalTrigInjecConvEdge = ADC_EXTERNALTRIGINJECCONVEDGE_RISING; sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJECCONV_T1_TRGO; sConfigInjected.AutoInjectedConv = DISABLE; sConfigInjected.InjectedDiscontinuousConvMode = DISABLE; sConfigInjected.InjectedOffset = 0; if (HAL_ADCEx_InjectedConfigChannel(&hadc1, &sConfigInjected) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } }
void Adc_Init(){ ADC_ChannelConfTypeDef sConfig; GPIO_InitTypeDef GPIO_InitStruct; /*##-- Enable peripherals and GPIO Clocks #################################*/ /* ADC3 Periph clock enable */ __HAL_RCC_ADC1_CLK_ENABLE(); /* Enable GPIO clock ****************************************/ __HAL_RCC_GPIOC_CLK_ENABLE(); /*##-- Configure peripheral GPIO ##########################################*/ /* ADC3 Channel8 GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); AdcHandle.Instance = ADC1; AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; 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; if(HAL_ADC_Init(&AdcHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-- Configure ADC regular channel ######################################*/ sConfig.Channel = ADC_CHANNEL_13; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; sConfig.Offset = 0; if(HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { /* Channel Configuration Error */ Error_Handler(); } }