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); }
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.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; obj->handle.Init.Resolution = ADC_RESOLUTION_12B; obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; obj->handle.Init.ScanConvMode = DISABLE; obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; obj->handle.Init.LowPowerAutoWait = DISABLE; obj->handle.Init.ContinuousConvMode = DISABLE; obj->handle.Init.NbrOfConversion = 1; obj->handle.Init.DiscontinuousConvMode = DISABLE; obj->handle.Init.NbrOfDiscConversion = 0; obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; obj->handle.Init.DMAContinuousRequests = DISABLE; obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; #if defined(ADC1) if ((ADCName)obj->handle.Instance == ADC_1) { __HAL_RCC_ADC1_CLK_ENABLE(); } #endif #if defined(ADC2) if ((ADCName)obj->handle.Instance == ADC_2) { __HAL_RCC_ADC2_CLK_ENABLE(); } #endif #if defined(ADC3) if ((ADCName)obj->handle.Instance == ADC_3) { __HAL_RCC_ADC34_CLK_ENABLE(); } #endif #if defined(ADC4) if ((ADCName)obj->handle.Instance == ADC_4) { __HAL_RCC_ADC34_CLK_ENABLE(); } #endif if (HAL_ADC_Init(&obj->handle) != HAL_OK) { error("Cannot initialize ADC\n"); } if (!HAL_ADCEx_Calibration_GetValue(&obj->handle, ADC_SINGLE_ENDED)) { HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED); } }