Skip to content

Commit affd3fc

Browse files
committed
Clear the pending IRQ in the NVIC as well.
1 parent 00d5f63 commit affd3fc

5 files changed

Lines changed: 37 additions & 12 deletions

File tree

ports/nrf/common-hal/audiobusio/I2SOut.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ bool common_hal_audiobusio_i2sout_get_playing(audiobusio_i2sout_obj_t* self) {
322322
void i2s_background(void) {
323323
if (NVIC_GetPendingIRQ(I2S_IRQn) && NRF_I2S->EVENTS_TXPTRUPD) {
324324
NRF_I2S->EVENTS_TXPTRUPD = 0;
325+
NVIC_ClearPendingIRQ(I2S_IRQn);
325326
if (instance) {
326327
i2s_buffer_fill(instance);
327328
} else {

ports/nrf/common-hal/audiopwmio/PWMAudioOut.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ void audiopwmout_reset() {
8888
}
8989

9090
STATIC void fill_buffers(audiopwmio_pwmaudioout_obj_t *self, int buf) {
91-
self->pwm->EVENTS_SEQSTARTED[1-buf] = 0;
9291
uint16_t *dev_buffer = self->buffers[buf];
9392
uint8_t *buffer;
9493
uint32_t buffer_length;
@@ -145,8 +144,15 @@ STATIC void audiopwmout_background_obj(audiopwmio_pwmaudioout_obj_t *self) {
145144
if (stopped)
146145
self->pwm->TASKS_STOP = 1;
147146
} else if (!self->paused && !self->single_buffer) {
148-
if (self->pwm->EVENTS_SEQSTARTED[0]) fill_buffers(self, 1);
149-
if (self->pwm->EVENTS_SEQSTARTED[1]) fill_buffers(self, 0);
147+
if (self->pwm->EVENTS_SEQSTARTED[0]) {
148+
fill_buffers(self, 1);
149+
self->pwm->EVENTS_SEQSTARTED[0] = 0;
150+
}
151+
if (self->pwm->EVENTS_SEQSTARTED[1]) {
152+
fill_buffers(self, 0);
153+
self->pwm->EVENTS_SEQSTARTED[1] = 0;
154+
}
155+
NVIC_ClearPendingIRQ(self->pwm_irq);
150156
}
151157
}
152158

@@ -168,7 +174,8 @@ void audiopwmout_background() {
168174
// Caller validates that pins are free.
169175
void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t* self,
170176
const mcu_pin_obj_t* left_channel, const mcu_pin_obj_t* right_channel, uint16_t quiescent_value) {
171-
self->pwm = pwmout_allocate(256, PWM_PRESCALER_PRESCALER_DIV_1, true, NULL, NULL);
177+
self->pwm = pwmout_allocate(256, PWM_PRESCALER_PRESCALER_DIV_1, true, NULL, NULL,
178+
&self->pwm_irq);
172179
if (!self->pwm) {
173180
mp_raise_RuntimeError(translate("All timers in use"));
174181
}

ports/nrf/common-hal/audiopwmio/PWMAudioOut.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ typedef struct {
4444
uint8_t sample_channel_count;
4545
uint8_t bytes_per_sample;
4646

47+
IRQn_Type pwm_irq;
48+
4749
bool playing;
4850
bool stopping;
4951
bool paused;

ports/nrf/common-hal/pulseio/PWMOut.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,15 @@ bool convert_frequency(uint32_t frequency, uint16_t *countertop, nrf_pwm_clk_t *
139139
return false;
140140
}
141141

142+
// We store these in an array because we cannot compute them.
143+
static IRQn_Type pwm_irqs[4] = {PWM0_IRQn, PWM1_IRQn, PWM2_IRQn, PWM3_IRQn};
144+
142145
NRF_PWM_Type *pwmout_allocate(uint16_t countertop, nrf_pwm_clk_t base_clock,
143-
bool variable_frequency, int8_t *channel_out, bool *pwm_already_in_use_out) {
146+
bool variable_frequency, int8_t *channel_out, bool *pwm_already_in_use_out,
147+
IRQn_Type* irq) {
144148
for (size_t pwm_index = 0; pwm_index < MP_ARRAY_SIZE(pwms); pwm_index++) {
145149
NRF_PWM_Type *pwm = pwms[pwm_index];
146-
bool pwm_already_in_use = pwm->ENABLE & SPIM_ENABLE_ENABLE_Msk;
150+
bool pwm_already_in_use = pwm->ENABLE & PWM_ENABLE_ENABLE_Msk;
147151
if (pwm_already_in_use) {
148152
if (variable_frequency) {
149153
// 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,
156160
for (size_t chan = 0; chan < CHANNELS_PER_PWM; chan++) {
157161
if (pwm->PSEL.OUT[chan] == 0xFFFFFFFF) {
158162
// Channel is free.
159-
if(channel_out)
163+
if (channel_out) {
160164
*channel_out = chan;
161-
if(pwm_already_in_use_out)
165+
}
166+
if (pwm_already_in_use_out) {
162167
*pwm_already_in_use_out = pwm_already_in_use;
168+
}
169+
if (irq) {
170+
*irq = pwm_irqs[pwm_index];
171+
}
163172
return pwm;
164173
}
165174
}
166175
}
167176
} else {
168177
// PWM not yet in use, so we can start to use it. Use channel 0.
169-
if(channel_out)
178+
if (channel_out) {
170179
*channel_out = 0;
171-
if(pwm_already_in_use_out)
180+
}
181+
if (pwm_already_in_use_out) {
172182
*pwm_already_in_use_out = pwm_already_in_use;
183+
}
184+
if (irq) {
185+
*irq = pwm_irqs[pwm_index];
186+
}
173187
return pwm;
174188
}
175189
}
@@ -208,7 +222,7 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
208222
int8_t channel;
209223
bool pwm_already_in_use;
210224
self->pwm = pwmout_allocate(countertop, base_clock, variable_frequency,
211-
&channel, &pwm_already_in_use);
225+
&channel, &pwm_already_in_use, NULL);
212226

213227
if (self->pwm == NULL) {
214228
return PWMOUT_ALL_TIMERS_IN_USE;

ports/nrf/common-hal/pulseio/PWMOut.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ typedef struct {
4242

4343
void pwmout_reset(void);
4444
NRF_PWM_Type *pwmout_allocate(uint16_t countertop, nrf_pwm_clk_t base_clock,
45-
bool variable_frequency, int8_t *channel_out, bool *pwm_already_in_use_out);
45+
bool variable_frequency, int8_t *channel_out, bool *pwm_already_in_use_out,
46+
IRQn_Type *irq);
4647
void pwmout_free_channel(NRF_PWM_Type *pwm, int8_t channel);
4748

4849
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_PULSEIO_PWMOUT_H

0 commit comments

Comments
 (0)