Skip to content

Commit 2451c78

Browse files
committed
valid channels in audio_dma_stop; cleaner supervisor_ticks mgmt in keypad
1 parent 59b89fd commit 2451c78

File tree

8 files changed

+28
-24
lines changed

8 files changed

+28
-24
lines changed

ports/atmel-samd/common-hal/audiobusio/I2SOut.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
#include "atmel_start_pins.h"
4646
#include "hal/include/hal_gpio.h"
47-
#include "hpl//hpl_gclk_base.h"
47+
#include "hpl/gclk/hpl_gclk_base.h"
4848
#include "peripheral_clk_config.h"
4949

5050
#ifdef SAMD21

ports/raspberrypi/audio_dma.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
3737

3838
#if CIRCUITPY_AUDIOPWMIO || CIRCUITPY_AUDIOBUSIO
3939

40-
#define AUDIO_DMA_CHANNEL_COUNT NUM_DMA_CHANNELS
41-
4240
void audio_dma_reset(void) {
43-
for (size_t channel = 0; channel < AUDIO_DMA_CHANNEL_COUNT; channel++) {
41+
for (size_t channel = 0; channel < NUM_DMA_CHANNELS; channel++) {
4442
if (MP_STATE_PORT(playing_audio)[channel] == NULL) {
4543
continue;
4644
}
@@ -171,6 +169,7 @@ void audio_dma_load_next_block(audio_dma_t *dma) {
171169
!dma_channel_is_busy(dma->channel[1])) {
172170
// No data has been read, and both DMA channels have now finished, so it's safe to stop.
173171
audio_dma_stop(dma);
172+
dma->playing_in_progress = false;
174173
} else {
175174
// Set channel trigger to ourselves so we don't keep going.
176175
dma_channel_hw_t *c = &dma_hw->ch[dma_channel];
@@ -318,14 +317,22 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t *dma,
318317
irq_set_mask_enabled(1 << DMA_IRQ_0, true);
319318
}
320319

320+
dma->playing_in_progress = true;
321321
dma_channel_start(dma->channel[0]);
322322

323323
return AUDIO_DMA_OK;
324324
}
325325

326326
void audio_dma_stop(audio_dma_t *dma) {
327327
// Disable our interrupts.
328-
dma_hw->inte0 &= ~((1 << dma->channel[0]) | (1 << dma->channel[1]));
328+
uint32_t channel_mask = 0;
329+
if (dma->channel[0] < NUM_DMA_CHANNELS) {
330+
channel_mask |= 1 << dma->channel[0];
331+
}
332+
if (dma->channel[1] < NUM_DMA_CHANNELS) {
333+
channel_mask |= 1 << dma->channel[1];
334+
}
335+
dma_hw->inte0 &= ~channel_mask;
329336
irq_set_mask_enabled(1 << DMA_IRQ_0, false);
330337

331338
// Run any remaining audio tasks because we remove ourselves from
@@ -334,6 +341,10 @@ void audio_dma_stop(audio_dma_t *dma) {
334341

335342
for (size_t i = 0; i < 2; i++) {
336343
size_t channel = dma->channel[i];
344+
if (channel == NUM_DMA_CHANNELS) {
345+
// Channel not in use.
346+
continue;
347+
}
337348

338349
dma_channel_config c = dma_channel_get_default_config(dma->channel[i]);
339350
channel_config_set_enable(&c, false);
@@ -357,6 +368,7 @@ void audio_dma_stop(audio_dma_t *dma) {
357368
MP_STATE_PORT(playing_audio)[channel] = NULL;
358369
dma->channel[i] = NUM_DMA_CHANNELS;
359370
}
371+
dma->playing_in_progress = false;
360372

361373
// Hold onto our buffers.
362374
}
@@ -381,7 +393,7 @@ void audio_dma_resume(audio_dma_t *dma) {
381393
}
382394

383395
bool audio_dma_get_paused(audio_dma_t *dma) {
384-
if (dma->channel[0] >= AUDIO_DMA_CHANNEL_COUNT) {
396+
if (dma->channel[0] >= NUM_DMA_CHANNELS) {
385397
return false;
386398
}
387399
uint32_t control = dma_hw->ch[dma->channel[0]].ctrl_trig;
@@ -408,12 +420,7 @@ bool audio_dma_get_playing(audio_dma_t *dma) {
408420
if (dma->channel[0] == NUM_DMA_CHANNELS) {
409421
return false;
410422
}
411-
if (!dma_channel_is_busy(dma->channel[0]) &&
412-
!dma_channel_is_busy(dma->channel[1])) {
413-
return false;
414-
}
415-
416-
return true;
423+
return dma->playing_in_progress;
417424
}
418425

419426
// WARN(tannewt): DO NOT print from here, or anything it calls. Printing calls

ports/raspberrypi/audio_dma.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ typedef struct {
4545
bool output_signed;
4646
bool first_channel_free;
4747
bool first_buffer_free;
48+
bool playing_in_progress;
4849
uint8_t output_resolution; // in bits
4950
uint8_t sample_resolution; // in bits
5051
uint8_t *first_buffer;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include "shared-bindings/microcontroller/__init__.h"
3939
#include "shared-bindings/microcontroller/Pin.h"
4040
#include "shared-bindings/microcontroller/Processor.h"
41-
#include "supervisor/shared/tick.h"
4241
#include "supervisor/shared/translate.h"
4342

4443
#include "src/rp2040/hardware_structs/include/hardware/structs/dma.h"

shared-module/keypad/KeyMatrix.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint
7777

7878
// Add self to the list of active keypad scanners.
7979
keypad_register_scanner((keypad_scanner_obj_t *)self);
80-
81-
supervisor_enable_tick();
8280
}
8381

8482
void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self) {

shared-module/keypad/Keys.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pin
6363

6464
// Add self to the list of active keypad scanners.
6565
keypad_register_scanner((keypad_scanner_obj_t *)self);
66-
67-
supervisor_enable_tick();
6866
}
6967

7068
void common_hal_keypad_keys_deinit(keypad_keys_obj_t *self) {

shared-module/keypad/ShiftRegisterKeys.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ void common_hal_keypad_shiftregisterkeys_construct(keypad_shiftregisterkeys_obj_
7171

7272
// Add self to the list of active keypad scanners.
7373
keypad_register_scanner((keypad_scanner_obj_t *)self);
74-
75-
supervisor_enable_tick();
7674
}
7775

7876
void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *self) {

shared-module/keypad/__init__.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@ void keypad_tick(void) {
5757
}
5858

5959
void keypad_reset(void) {
60-
if (MP_STATE_VM(keypad_scanners_linked_list)) {
61-
supervisor_disable_tick();
60+
while (MP_STATE_VM(keypad_scanners_linked_list)) {
61+
keypad_deregister_scanner(MP_STATE_VM(keypad_scanners_linked_list));
6262
}
63-
64-
MP_STATE_VM(keypad_scanners_linked_list) = NULL;
65-
keypad_scanners_linked_list_lock = false;
6663
}
6764

6865
// Register a Keys, KeyMatrix, etc. that will be scanned in the background
@@ -71,10 +68,16 @@ void keypad_register_scanner(keypad_scanner_obj_t *scanner) {
7168
scanner->next = MP_STATE_VM(keypad_scanners_linked_list);
7269
MP_STATE_VM(keypad_scanners_linked_list) = scanner;
7370
supervisor_release_lock(&keypad_scanners_linked_list_lock);
71+
72+
// One more request for ticks.
73+
supervisor_enable_tick();
7474
}
7575

7676
// Remove scanner from the list of active scanners.
7777
void keypad_deregister_scanner(keypad_scanner_obj_t *scanner) {
78+
// One less request for ticks.
79+
supervisor_disable_tick();
80+
7881
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
7982
if (MP_STATE_VM(keypad_scanners_linked_list) == scanner) {
8083
// Scanner is at the front; splice it out.

0 commit comments

Comments
 (0)