Thanks to visit codestin.com
Credit goes to github.com

Skip to content

rp2: Change machine.I2S and rp2.DMA to use shared DMA IRQ handlers. #13555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2024
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
1 change: 1 addition & 0 deletions ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ target_compile_definitions(${MICROPY_TARGET} PRIVATE
PICO_FLOAT_PROPAGATE_NANS=1
PICO_STACK_SIZE=0x2000
PICO_CORE1_STACK_SIZE=0
PICO_MAX_SHARED_IRQ_HANDLERS=8 # we need more than the default
PICO_PROGRAM_NAME="MicroPython"
PICO_NO_PROGRAM_VERSION_STRING=1 # do it ourselves in main.c
MICROPY_BUILD_TYPE="${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION} ${CMAKE_BUILD_TYPE}"
Expand Down
6 changes: 2 additions & 4 deletions ports/rp2/machine_i2s.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,18 @@ STATIC void feed_dma(machine_i2s_obj_t *self, uint8_t *dma_buffer_p) {

STATIC void irq_configure(machine_i2s_obj_t *self) {
if (self->i2s_id == 0) {
irq_set_exclusive_handler(DMA_IRQ_0, dma_irq0_handler);
irq_add_shared_handler(DMA_IRQ_0, dma_irq0_handler, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY);
irq_set_enabled(DMA_IRQ_0, true);
} else {
irq_set_exclusive_handler(DMA_IRQ_1, dma_irq1_handler);
irq_add_shared_handler(DMA_IRQ_1, dma_irq1_handler, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY);
irq_set_enabled(DMA_IRQ_1, true);
}
}

STATIC void irq_deinit(machine_i2s_obj_t *self) {
if (self->i2s_id == 0) {
irq_set_enabled(DMA_IRQ_0, false);
irq_remove_handler(DMA_IRQ_0, dma_irq0_handler);
} else {
irq_set_enabled(DMA_IRQ_1, false);
irq_remove_handler(DMA_IRQ_1, dma_irq1_handler);
}
}
Expand Down
14 changes: 7 additions & 7 deletions ports/rp2/rp2_dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,16 @@ STATIC uint32_t rp2_dma_register_value_from_obj(mp_obj_t o, int reg_type) {
STATIC void rp2_dma_irq_handler(void) {
// Main IRQ handler
uint32_t irq_bits = dma_hw->ints0;
dma_hw->ints0 = 0xffff;

for (int i = 0; i < NUM_DMA_CHANNELS; i++) {
if (irq_bits & (1u << i)) {
mp_irq_obj_t *handler = MP_STATE_PORT(rp2_dma_irq_obj[i]);
if (handler) {
// An rp2.DMA IRQ handler is registered for this channel, so handle it.
dma_channel_acknowledge_irq0(i);
rp2_dma_obj_t *self = (rp2_dma_obj_t *)handler->parent;
self->irq_flag = 1;
mp_irq_handler(handler);
} else {
// We got an interrupt with no handler. Disable the channel
dma_channel_set_irq0_enabled(i, false);
}
}
}
Expand Down Expand Up @@ -389,6 +387,9 @@ STATIC mp_obj_t rp2_dma_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
if (irq == NULL) {
irq = mp_irq_new(&rp2_dma_irq_methods, MP_OBJ_FROM_PTR(self));
MP_STATE_PORT(rp2_dma_irq_obj[self->channel]) = irq;

// Clear any existing IRQs on this DMA channel, they are not for us.
dma_channel_acknowledge_irq0(self->channel);
}

if (n_args > 1 || kw_args->used != 0) {
Expand Down Expand Up @@ -457,12 +458,11 @@ MP_DEFINE_CONST_OBJ_TYPE(
void rp2_dma_init(void) {
// Set up interrupts.
memset(MP_STATE_PORT(rp2_dma_irq_obj), 0, sizeof(MP_STATE_PORT(rp2_dma_irq_obj)));
irq_set_exclusive_handler(DMA_IRQ_0, rp2_dma_irq_handler);
irq_add_shared_handler(DMA_IRQ_0, rp2_dma_irq_handler, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY);
}

void rp2_dma_deinit(void) {
// Disable and clear interrupts.
irq_set_mask_enabled(1u << DMA_IRQ_0, false);
// Remove our interrupt handler.
irq_remove_handler(DMA_IRQ_0, rp2_dma_irq_handler);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ports/rp2/rp2_dma.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def run_and_time_dma(dma):
dma.count = len(dest) // 4
dma.ctrl = dma.pack_ctrl()
dt = run_and_time_dma(dma)
print(60 <= dt <= 90)
print(70 <= dt <= 110)
print(dest[:8], dest[-8:])
dma.close()

Expand All @@ -80,7 +80,7 @@ def run_and_time_dma(dma):
print("# test irq")
dest = bytearray(1024)
dma = rp2.DMA()
dma.irq(lambda _: print("irq fired"))
dma.irq(lambda dma: print("irq fired", dma.irq().flags()))
dma.config(
read=src, write=dest, count=len(dest) // 4, ctrl=dma.pack_ctrl(irq_quiet=0), trigger=True
)
Expand Down
2 changes: 1 addition & 1 deletion tests/ports/rp2/rp2_dma.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07') bytearray(b'\xf8\xf9\xfa\xfb\xfc\
# test immediate trigger
bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07') bytearray(b'\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff')
# test irq
irq fired
irq fired 1
bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07') bytearray(b'\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff')