Skip to content

Review get adc/dac/timer channel functions #1735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cores/arduino/stm32/analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,20 @@ extern "C" {
#endif

/* Exported functions ------------------------------------------------------- */
#if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY)
uint32_t get_adc_channel(PinName pin, uint32_t *bank);
uint32_t get_adc_internal_channel(PinName pin);
uint16_t adc_read_value(PinName pin, uint32_t resolution);
#endif
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
uint32_t get_dac_channel(PinName pin);
void dac_write_value(PinName pin, uint32_t value, uint8_t do_init);
void dac_stop(PinName pin);
uint16_t adc_read_value(PinName pin, uint32_t resolution);
#endif
#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
void pwm_start(PinName pin, uint32_t clock_freq, uint32_t value, TimerCompareFormat_t resolution);
void pwm_stop(PinName pin);
#endif
uint32_t get_pwm_channel(PinName pin);

#ifdef __cplusplus
}
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/stm32/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ uint8_t getTimerClkSrc(TIM_TypeDef *tim);
IRQn_Type getTimerUpIrq(TIM_TypeDef *tim);
IRQn_Type getTimerCCIrq(TIM_TypeDef *tim);

uint32_t getTimerChannel(PinName pin);

#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion libraries/SrcWrapper/src/HardwareTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin)
_ChannelMode[channel - 1] = mode;

if (pin != NC) {
if ((int)get_pwm_channel(pin) == timChannel) {
if ((int)getTimerChannel(pin) == timChannel) {
/* Configure PWM GPIO pins */
pinmap_pinout(pin, PinMap_TIM);
#if defined(STM32F1xx)
Expand Down
58 changes: 25 additions & 33 deletions libraries/SrcWrapper/src/stm32/analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ static PinName g_current_pin = NC;
#define ADC_REGULAR_RANK_1 1
#endif

/* Private Functions */
static uint32_t get_adc_channel(PinName pin, uint32_t *bank)
/* Exported Functions */
/**
* @brief Return ADC HAL channel linked to a PinName
* @param pin: PinName
* @param bank: pointer to get ADC channel bank if required
* @retval Valid HAL channel
*/
uint32_t get_adc_channel(PinName pin, uint32_t *bank)
{
uint32_t function = pinmap_function(pin, PinMap_ADC);
uint32_t channel = 0;
Expand Down Expand Up @@ -218,7 +224,7 @@ static uint32_t get_adc_channel(PinName pin, uint32_t *bank)
#endif
#endif
default:
channel = 0;
_Error_Handler("ADC: Unknown adc channel", (int)(STM_PIN_CHANNEL(function)));
break;
}
#ifdef ADC_CHANNELS_BANK_B
Expand All @@ -233,7 +239,14 @@ static uint32_t get_adc_channel(PinName pin, uint32_t *bank)
return channel;
}

static uint32_t get_adc_internal_channel(PinName pin)
/**
* @brief Return ADC HAL internal channel linked to a PinName
* @param pin: specific PinName's for ADC internal. Value can be:
* PADC_TEMP, PADC_TEMP_ADC5, PADC_VREF, PADC_VBAT
* Note that not all of these values ​​may be available for all series.
* @retval Valid HAL internal channel.
*/
uint32_t get_adc_internal_channel(PinName pin)
{
uint32_t channel = 0;
switch (pin) {
Expand Down Expand Up @@ -263,41 +276,20 @@ static uint32_t get_adc_internal_channel(PinName pin)
break;
#endif
default:
channel = 0;
_Error_Handler("ADC: Unknown adc internal PiName", (int)(pin));
break;
}
return channel;
}
#endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY */

#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
uint32_t get_pwm_channel(PinName pin)
{
uint32_t function = pinmap_function(pin, PinMap_TIM);
uint32_t channel = 0;
switch (STM_PIN_CHANNEL(function)) {
case 1:
channel = TIM_CHANNEL_1;
break;
case 2:
channel = TIM_CHANNEL_2;
break;
case 3:
channel = TIM_CHANNEL_3;
break;
case 4:
channel = TIM_CHANNEL_4;
break;
default:
channel = 0;
break;
}
return channel;
}
#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */

#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
static uint32_t get_dac_channel(PinName pin)
/**
* @brief Return DAC HAL channel linked to a PinName
* @param pin: specific PinName's for ADC internal.
* @retval Valid HAL channel
*/
uint32_t get_dac_channel(PinName pin)
{
uint32_t function = pinmap_function(pin, PinMap_DAC);
uint32_t channel = 0;
Expand All @@ -316,7 +308,7 @@ static uint32_t get_dac_channel(PinName pin)
break;
#endif
default:
channel = 0;
_Error_Handler("DAC: Unknown dac channel", (int)(STM_PIN_CHANNEL(function)));
break;
}
return channel;
Expand Down
28 changes: 28 additions & 0 deletions libraries/SrcWrapper/src/stm32/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,34 @@ uint8_t getTimerClkSrc(TIM_TypeDef *tim)
return clkSrc;
}

/**
* @brief Return HAL timer channel linked to a PinName
* @param pin: PinName
* @retval Valid HAL channel
*/
uint32_t getTimerChannel(PinName pin)
{
uint32_t function = pinmap_function(pin, PinMap_TIM);
uint32_t channel = 0;
switch (STM_PIN_CHANNEL(function)) {
case 1:
channel = TIM_CHANNEL_1;
break;
case 2:
channel = TIM_CHANNEL_2;
break;
case 3:
channel = TIM_CHANNEL_3;
break;
case 4:
channel = TIM_CHANNEL_4;
break;
default:
_Error_Handler("TIM: Unknown timer channel", (int)(STM_PIN_CHANNEL(function)));
break;
}
return channel;
}

#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */

Expand Down