summaryrefslogtreecommitdiff
path: root/ports/nrf/common-hal/pulseio/PWMOut.c
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2020-03-10 16:57:41 -0700
committerScott Shawcroft <scott@tannewt.org>2020-03-13 11:16:38 -0700
commitaffd3fcc2a0c577c58973c22af16383ff807a516 (patch)
tree79b70267a0863bd28210cf47df003c88cc062511 /ports/nrf/common-hal/pulseio/PWMOut.c
parent00d5f63e7cd3e489d3291ee7e90ab4ad46110b62 (diff)
Clear the pending IRQ in the NVIC as well.
Diffstat (limited to 'ports/nrf/common-hal/pulseio/PWMOut.c')
-rw-r--r--ports/nrf/common-hal/pulseio/PWMOut.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/ports/nrf/common-hal/pulseio/PWMOut.c b/ports/nrf/common-hal/pulseio/PWMOut.c
index 6921091f8..9d42ccca0 100644
--- a/ports/nrf/common-hal/pulseio/PWMOut.c
+++ b/ports/nrf/common-hal/pulseio/PWMOut.c
@@ -139,11 +139,15 @@ bool convert_frequency(uint32_t frequency, uint16_t *countertop, nrf_pwm_clk_t *
return false;
}
+// We store these in an array because we cannot compute them.
+static IRQn_Type pwm_irqs[4] = {PWM0_IRQn, PWM1_IRQn, PWM2_IRQn, PWM3_IRQn};
+
NRF_PWM_Type *pwmout_allocate(uint16_t countertop, nrf_pwm_clk_t base_clock,
- bool variable_frequency, int8_t *channel_out, bool *pwm_already_in_use_out) {
+ bool variable_frequency, int8_t *channel_out, bool *pwm_already_in_use_out,
+ IRQn_Type* irq) {
for (size_t pwm_index = 0; pwm_index < MP_ARRAY_SIZE(pwms); pwm_index++) {
NRF_PWM_Type *pwm = pwms[pwm_index];
- bool pwm_already_in_use = pwm->ENABLE & SPIM_ENABLE_ENABLE_Msk;
+ bool pwm_already_in_use = pwm->ENABLE & PWM_ENABLE_ENABLE_Msk;
if (pwm_already_in_use) {
if (variable_frequency) {
// Variable frequency requires exclusive use of a PWM, so try the next one.
@@ -156,20 +160,30 @@ NRF_PWM_Type *pwmout_allocate(uint16_t countertop, nrf_pwm_clk_t base_clock,
for (size_t chan = 0; chan < CHANNELS_PER_PWM; chan++) {
if (pwm->PSEL.OUT[chan] == 0xFFFFFFFF) {
// Channel is free.
- if(channel_out)
+ if (channel_out) {
*channel_out = chan;
- if(pwm_already_in_use_out)
+ }
+ if (pwm_already_in_use_out) {
*pwm_already_in_use_out = pwm_already_in_use;
+ }
+ if (irq) {
+ *irq = pwm_irqs[pwm_index];
+ }
return pwm;
}
}
}
} else {
// PWM not yet in use, so we can start to use it. Use channel 0.
- if(channel_out)
+ if (channel_out) {
*channel_out = 0;
- if(pwm_already_in_use_out)
+ }
+ if (pwm_already_in_use_out) {
*pwm_already_in_use_out = pwm_already_in_use;
+ }
+ if (irq) {
+ *irq = pwm_irqs[pwm_index];
+ }
return pwm;
}
}
@@ -208,7 +222,7 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
int8_t channel;
bool pwm_already_in_use;
self->pwm = pwmout_allocate(countertop, base_clock, variable_frequency,
- &channel, &pwm_already_in_use);
+ &channel, &pwm_already_in_use, NULL);
if (self->pwm == NULL) {
return PWMOUT_ALL_TIMERS_IN_USE;