Skip to content

Commit

Permalink
Merge pull request #10982 from gschorcht/cpu/esp8266/periph/pwm/pr
Browse files Browse the repository at this point in the history
cpu/esp8266: fix pwm_set func
  • Loading branch information
MrKevinWeiss authored Jun 27, 2019
2 parents 14f9138 + d3e0b78 commit 135ad38
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions cpu/esp8266/periph/pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,35 @@ void pwm_set(pwm_t pwm, uint8_t channel, uint16_t value)
CHECK_PARAM (value <= _pwm_dev.res);

uint32_t state = irq_disable();
uint32_t phase = _pwm_dev.cycles - _pwm_dev.cycles % _pwm_dev.res + _pwm_dev.res;
uint32_t phase = _pwm_dev.cycles - _pwm_dev.cycles % _pwm_dev.res;
uint32_t next_on = phase;
uint32_t next_off;

switch (_pwm_dev.mode) {
case PWM_LEFT:
_pwm_dev.chn[channel].next_on = phase;
next_on = phase;
break;

case PWM_RIGHT:
_pwm_dev.chn[channel].next_on = phase + _pwm_dev.res - value;
next_on = phase + _pwm_dev.res - value;
break;

case PWM_CENTER:
_pwm_dev.chn[channel].next_on = phase + (_pwm_dev.res - value) / 2;
next_on = phase + (_pwm_dev.res - value) / 2;
break;
}

_pwm_dev.chn[channel].next_off = _pwm_dev.chn[channel].next_on + value;
next_off = next_on + value;

if (_pwm_dev.cycles >= next_on) {
next_on += _pwm_dev.res;
}
if (_pwm_dev.cycles >= next_off) {
next_off += _pwm_dev.res;
}

_pwm_dev.chn[channel].next_on = next_on;
_pwm_dev.chn[channel].next_off = next_off;
_pwm_dev.chn[channel].duty = value;

irq_restore(state);
Expand Down

0 comments on commit 135ad38

Please sign in to comment.