Skip to content
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
2 changes: 1 addition & 1 deletion cpu/efm32/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ typedef struct {
typedef enum {
PWM_LEFT = timerModeUp, /**< use left aligned PWM */
PWM_RIGHT = timerModeDown, /**< use right aligned PWM */
PWM_CENTER = timerModeUp /**< not supported, use left aligned */
PWM_CENTER = timerModeUpDown /**< use center aligned PWM */
} pwm_mode_t;
/** @} */
#endif /* ndef DOXYGEN */
Expand Down
15 changes: 13 additions & 2 deletions cpu/efm32/periph/pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res)
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(pwm_config[dev].cmu, true);

/* in up/down counting mode a full period takes twice the number of ticks */
uint32_t ticks = (mode == PWM_CENTER) ? ((uint32_t)res * 2) : (uint32_t)res;

/* calculate the prescaler by determining the best prescaler */
uint32_t freq_timer = CMU_ClockFreqGet(pwm_config[dev].cmu);
TIMER_Prescale_TypeDef prescaler = TIMER_PrescalerCalc(freq * res,
TIMER_Prescale_TypeDef prescaler = TIMER_PrescalerCalc(freq * ticks,
freq_timer);

if (prescaler > timerPrescale1024) {
Expand All @@ -62,6 +65,14 @@ uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res)

init_channel.mode = timerCCModePWM;

if (mode == PWM_RIGHT) {
/* down counting sets the output at the start of the period and clears
* it on compare match, so the output has to be inverted to obtain a
* right aligned pulse with the requested duty cycle. */
init_channel.coist = true;
init_channel.outInvert = true;
}

for (int i = 0; i < pwm_config[dev].channels; i++) {
pwm_chan_conf_t channel = pwm_config[dev].channel[i];

Expand All @@ -84,7 +95,7 @@ uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res)
/* enable peripheral */
TIMER_Enable(pwm_config[dev].dev, true);

return freq_timer / TIMER_Prescaler2Div(prescaler) / res;
return freq_timer / TIMER_Prescaler2Div(prescaler) / ticks;
}

uint8_t pwm_channels(pwm_t dev)
Expand Down