@@ -40,6 +40,9 @@ ADC_HandleTypeDef AdcHandle;
4040
4141void analogin_init (analogin_t * obj , PinName pin )
4242{
43+ uint32_t function = (uint32_t )NC ;
44+ obj -> adc = (ADCName )NC ;
45+
4346#if defined(ADC1 )
4447 static int adc1_inited = 0 ;
4548#endif
@@ -49,20 +52,27 @@ void analogin_init(analogin_t *obj, PinName pin)
4952#if defined(ADC3 )
5053 static int adc3_inited = 0 ;
5154#endif
52- // Get the peripheral name from the pin and assign it to the object
53- obj -> adc = (ADCName )pinmap_peripheral (pin , PinMap_ADC );
55+ // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
56+ // are described in PinNames.h and PeripheralPins.c
57+ // Pin value must be between 0xF0 and 0xFF
58+ if ((pin < 0xF0 ) || (pin >= 0x100 )) {
59+ // Normal channels
60+ // Get the peripheral name from the pin and assign it to the object
61+ obj -> adc = (ADCName )pinmap_peripheral (pin , PinMap_ADC );
62+ // Get the functions (adc channel) from the pin and assign it to the object
63+ function = pinmap_function (pin , PinMap_ADC );
64+ // Configure GPIO
65+ pinmap_pinout (pin , PinMap_ADC );
66+ } else {
67+ // Internal channels
68+ obj -> adc = (ADCName )pinmap_peripheral (pin , PinMap_ADC_Internal );
69+ function = pinmap_function (pin , PinMap_ADC_Internal );
70+ // No GPIO configuration for internal channels
71+ }
5472 MBED_ASSERT (obj -> adc != (ADCName )NC );
55-
56- // Get the functions (adc channel) from the pin and assign it to the object
57- uint32_t function = pinmap_function (pin , PinMap_ADC );
5873 MBED_ASSERT (function != (uint32_t )NC );
59- obj -> channel = STM_PIN_CHANNEL (function );
6074
61- // Configure GPIO excepted for internal channels (Temperature, Vref, Vbat, ...)
62- // ADC Internal Channels "pins" are described in PinNames.h and must have a value >= 0xF0
63- if (pin < 0xF0 ) {
64- pinmap_pinout (pin , PinMap_ADC );
65- }
75+ obj -> channel = STM_PIN_CHANNEL (function );
6676
6777 // Save pin number for the read function
6878 obj -> pin = pin ;
0 commit comments