Skip to content

Commit e438bd6

Browse files
committed
STM32F3 ADC internal channels update
1 parent 5f21c3f commit e438bd6

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

targets/TARGET_STM/TARGET_STM32F3/analogin_device.c

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@
3535
#include "pinmap.h"
3636
#include "mbed_error.h"
3737
#include "PeripheralPins.h"
38-
#include <stdbool.h>
3938

4039
void analogin_init(analogin_t *obj, PinName pin)
4140
{
42-
static bool adc_calibrated = false;
4341
uint32_t function = (uint32_t)NC;
4442

4543
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
@@ -106,12 +104,10 @@ void analogin_init(analogin_t *obj, PinName pin)
106104
#endif
107105

108106
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
109-
error("Cannot initialize ADC");
107+
error("Cannot initialize ADC\n");
110108
}
111109

112-
// ADC calibration is done only once
113-
if (!adc_calibrated) {
114-
adc_calibrated = true;
110+
if (!HAL_ADCEx_Calibration_GetValue(&obj->handle, ADC_SINGLE_ENDED)) {
115111
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
116112
}
117113
}
@@ -171,16 +167,47 @@ uint16_t adc_read(analogin_t *obj)
171167
sConfig.Channel = ADC_CHANNEL_14;
172168
break;
173169
case 15:
174-
sConfig.Channel = ADC_CHANNEL_15;
170+
if ((ADCName)obj->handle.Instance == ADC_1) {
171+
sConfig.Channel = ADC_CHANNEL_VOPAMP1;
172+
sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5;
173+
}
174+
else {
175+
sConfig.Channel = ADC_CHANNEL_15;
176+
}
175177
break;
176178
case 16:
177-
sConfig.Channel = ADC_CHANNEL_16;
179+
if ((ADCName)obj->handle.Instance == ADC_1) {
180+
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
181+
sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5;
182+
}
183+
else {
184+
sConfig.Channel = ADC_CHANNEL_16;
185+
}
178186
break;
179187
case 17:
180-
sConfig.Channel = ADC_CHANNEL_17;
188+
sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5;
189+
if ((ADCName)obj->handle.Instance == ADC_1) {
190+
sConfig.Channel = ADC_CHANNEL_VBAT;
191+
}
192+
#if defined(ADC2)
193+
if ((ADCName)obj->handle.Instance == ADC_2) {
194+
sConfig.Channel = ADC_CHANNEL_VOPAMP2;
195+
}
196+
#endif
197+
#if defined(ADC3)
198+
if ((ADCName)obj->handle.Instance == ADC_3) {
199+
sConfig.Channel = ADC_CHANNEL_VOPAMP3;
200+
}
201+
#endif
202+
#if defined(ADC4)
203+
if ((ADCName)obj->handle.Instance == ADC_4) {
204+
sConfig.Channel = ADC_CHANNEL_VOPAMP4;
205+
}
206+
#endif
181207
break;
182208
case 18:
183-
sConfig.Channel = ADC_CHANNEL_18;
209+
sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5;
210+
sConfig.Channel = ADC_CHANNEL_VREFINT;
184211
break;
185212
default:
186213
return 0;

targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_adc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@
4747
/* Include ADC HAL Extended module */
4848
/* (include on top of file since ADC structures are defined in extended file) */
4949
#include "stm32f3xx_hal_adc_ex.h"
50-
50+
51+
/* Include low level driver */
52+
#include "stm32f3xx_ll_adc.h"
53+
5154
/** @addtogroup STM32F3xx_HAL_Driver
5255
* @{
5356
*/

targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_adc_ex.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5660,12 +5660,6 @@ HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConf
56605660
/* Set handle of the other ADC sharing the same common register */
56615661
ADC_COMMON_ADC_OTHER(hadc, &tmphadcSharingSameCommonRegister);
56625662

5663-
/* Software is allowed to change common parameters only when all ADCs */
5664-
/* of the common group are disabled. */
5665-
if ((ADC_IS_ENABLE(hadc) == RESET) &&
5666-
( (tmphadcSharingSameCommonRegister.Instance == NULL) ||
5667-
(ADC_IS_ENABLE(&tmphadcSharingSameCommonRegister) == RESET) ) )
5668-
{
56695663
/* If Channel_16 is selected, enable Temp. sensor measurement path */
56705664
/* Note: Temp. sensor internal channels available on ADC1 only */
56715665
if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) && (hadc->Instance == ADC1))
@@ -5694,17 +5688,6 @@ HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConf
56945688
{
56955689
SET_BIT(tmpADC_Common->CCR, ADC_CCR_VREFEN);
56965690
}
5697-
}
5698-
/* If the requested internal measurement path has already been */
5699-
/* enabled and other ADC of the common group are enabled, internal */
5700-
/* measurement paths cannot be enabled. */
5701-
else
5702-
{
5703-
/* Update ADC state machine to error */
5704-
SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
5705-
5706-
tmp_hal_status = HAL_ERROR;
5707-
}
57085691
}
57095692

57105693
}

0 commit comments

Comments
 (0)