Skip to content

Commit ee9109d

Browse files
committed
feat: export adc/dac get channel functions
Functions now return -1 instead of 0 which was a valid value. Fixes #1669 Signed-off-by: Frederic Pillon <[email protected]>
1 parent 72d6336 commit ee9109d

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

cores/arduino/stm32/analog.h

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ extern "C" {
4949
#endif
5050

5151
/* Exported functions ------------------------------------------------------- */
52+
#if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY)
53+
uint32_t get_adc_channel(PinName pin, uint32_t *bank);
54+
uint32_t get_adc_internal_channel(PinName pin);
55+
#endif
56+
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
57+
uint32_t get_dac_channel(PinName pin);
58+
#endif
5259
void dac_write_value(PinName pin, uint32_t value, uint8_t do_init);
5360
void dac_stop(PinName pin);
5461
uint16_t adc_read_value(PinName pin, uint32_t resolution);

libraries/SrcWrapper/src/stm32/analog.cpp

+28-10
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,17 @@ static PinName g_current_pin = NC;
101101
#define ADC_REGULAR_RANK_1 1
102102
#endif
103103

104-
/* Private Functions */
105-
static uint32_t get_adc_channel(PinName pin, uint32_t *bank)
104+
/* Exported Functions */
105+
/**
106+
* @brief Return ADC HAL channel linked to a PinName
107+
* @param pin: PinName
108+
* @param bank: pointer to get ADC channel bank if required
109+
* @retval HAL channel. return -1 if pin has no ADC
110+
*/
111+
uint32_t get_adc_channel(PinName pin, uint32_t *bank)
106112
{
107113
uint32_t function = pinmap_function(pin, PinMap_ADC);
108-
uint32_t channel = 0;
114+
uint32_t channel = -1;
109115
switch (STM_PIN_CHANNEL(function)) {
110116
#ifdef ADC_CHANNEL_0
111117
case 0:
@@ -218,7 +224,7 @@ static uint32_t get_adc_channel(PinName pin, uint32_t *bank)
218224
#endif
219225
#endif
220226
default:
221-
channel = 0;
227+
channel = -1;
222228
break;
223229
}
224230
#ifdef ADC_CHANNELS_BANK_B
@@ -233,9 +239,16 @@ static uint32_t get_adc_channel(PinName pin, uint32_t *bank)
233239
return channel;
234240
}
235241

236-
static uint32_t get_adc_internal_channel(PinName pin)
242+
/**
243+
* @brief Return ADC HAL internal channel linked to a PinName
244+
* @param pin: specific PinName's for ADC internal. Value can be:
245+
* PADC_TEMP, PADC_TEMP_ADC5, PADC_VREF, PADC_VBAT
246+
* Note that not all of these values ​​may be available for all series.
247+
* @retval HAL internal channel. return -1 if pin has no ADC internal
248+
*/
249+
uint32_t get_adc_internal_channel(PinName pin)
237250
{
238-
uint32_t channel = 0;
251+
uint32_t channel = -1;
239252
switch (pin) {
240253
#if defined(ADC_CHANNEL_TEMPSENSOR)
241254
case PADC_TEMP:
@@ -263,18 +276,23 @@ static uint32_t get_adc_internal_channel(PinName pin)
263276
break;
264277
#endif
265278
default:
266-
channel = 0;
279+
channel = -1;
267280
break;
268281
}
269282
return channel;
270283
}
271284
#endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY */
272285

273286
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
274-
static uint32_t get_dac_channel(PinName pin)
287+
/**
288+
* @brief Return DAC HAL channel linked to a PinName
289+
* @param pin: specific PinName's for ADC internal. Value can be:
290+
* @retval HAL channel. return -1 if pin has no dac
291+
*/
292+
uint32_t get_dac_channel(PinName pin)
275293
{
276294
uint32_t function = pinmap_function(pin, PinMap_DAC);
277-
uint32_t channel = 0;
295+
uint32_t channel = -1;
278296
switch (STM_PIN_CHANNEL(function)) {
279297
#ifdef DAC_CHANNEL_0
280298
case 0:
@@ -290,7 +308,7 @@ static uint32_t get_dac_channel(PinName pin)
290308
break;
291309
#endif
292310
default:
293-
channel = 0;
311+
channel = -1;
294312
break;
295313
}
296314
return channel;

0 commit comments

Comments
 (0)