From b22cc4a0327f5f47d5943292826575b5ae724da9 Mon Sep 17 00:00:00 2001 From: Przemyslaw Stekiel Date: Mon, 26 Aug 2019 13:18:50 +0200 Subject: [PATCH] STM32F4 Analogin driver: Add explicit pinmap support --- .../TARGET_STM32F4/analogin_device.c | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c b/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c index 75ac50f609..94f8f46071 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c @@ -36,25 +36,29 @@ #include "mbed_error.h" #include "PeripheralPins.h" -void analogin_init(analogin_t *obj, PinName pin) +#if EXPLICIT_PINMAP_READY +#define ANALOGIN_INIT_DIRECT analogin_init_direct +void analogin_init_direct(analogin_t* obj, const PinMap *pinmap) +#else +#define ANALOGIN_INIT_DIRECT _analogin_init_direct +static void _analogin_init_direct(analogin_t* obj, const PinMap *pinmap) +#endif { - uint32_t function = (uint32_t)NC; + uint32_t function = (uint32_t)pinmap->function; + + // Get the peripheral name from the pin and assign it to the object + obj->handle.Instance = (ADC_TypeDef *)pinmap->peripheral; // 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)) { + if ((pinmap->pin < 0xF0) || (pinmap->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); + pin_function(pinmap->pin, pinmap->function); + pin_mode(pinmap->pin, PullNone); } 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); @@ -63,7 +67,7 @@ void analogin_init(analogin_t *obj, PinName pin) obj->channel = STM_PIN_CHANNEL(function); // Save pin number for the read function - obj->pin = pin; + obj->pin = pinmap->pin; // Configure ADC object structures obj->handle.State = HAL_ADC_STATE_RESET; @@ -101,6 +105,24 @@ void analogin_init(analogin_t *obj, PinName pin) } } +void analogin_init(analogin_t* obj, PinName pin) +{ + int peripheral; + int function; + + if ((pin < 0xF0) || (pin >= 0x100)) { + peripheral = (int)pinmap_peripheral(pin, PinMap_ADC); + function = (int)pinmap_find_function(pin, PinMap_ADC); + } else { + peripheral = (int)pinmap_peripheral(pin, PinMap_ADC_Internal); + function = (int)pinmap_find_function(pin, PinMap_ADC_Internal); + } + + const PinMap explicit_pinmap = {pin, peripheral, function}; + + ANALOGIN_INIT_DIRECT(obj, &explicit_pinmap); +} + uint16_t adc_read(analogin_t *obj) { ADC_ChannelConfTypeDef sConfig = {0};