Skip to content

Fix signedness bug causing PIO hang-up. #10186

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 1 commit into from
Mar 26, 2025
Merged
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
18 changes: 14 additions & 4 deletions ports/raspberrypi/common-hal/rp2pio/StateMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/memorymap/AddressRange.h"

#if defined(PICO_RP2040)
#include "src/rp2040/hardware_regs/include/hardware/platform_defs.h"
#include "src/rp2040/hardware_structs/include/hardware/structs/iobank0.h"
#elif defined(PICO_RP2350)
#include "src/rp2350/hardware_regs/include/hardware/platform_defs.h"
#include "src/rp2350/hardware_structs/include/hardware/structs/iobank0.h"
#endif

#include "src/rp2_common/hardware_clocks/include/hardware/clocks.h"
#include "src/rp2_common/hardware_dma/include/hardware/dma.h"
#include "src/rp2_common/hardware_pio/include/hardware/pio_instructions.h"
#include "src/rp2040/hardware_structs/include/hardware/structs/iobank0.h"
#include "src/rp2_common/hardware_irq/include/hardware/irq.h"

#include "shared/runtime/interrupt_char.h"
Expand Down Expand Up @@ -222,7 +228,7 @@ static bool is_gpio_compatible(PIO pio, uint32_t used_gpio_ranges) {
#endif
}

static bool use_existing_program(PIO *pio_out, uint *sm_out, int *offset_inout, uint32_t program_id, size_t program_len, uint gpio_base, uint gpio_count) {
static bool use_existing_program(PIO *pio_out, int *sm_out, int *offset_inout, uint32_t program_id, size_t program_len, uint gpio_base, uint gpio_count) {
uint32_t required_gpio_ranges;
if (gpio_count) {
required_gpio_ranges = (1u << (gpio_base >> 4)) |
Expand Down Expand Up @@ -307,12 +313,12 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
.origin = offset,
};
PIO pio;
uint state_machine;
int state_machine;
bool added = false;

if (!use_existing_program(&pio, &state_machine, &offset, program_id, program_len, gpio_base, gpio_count)) {
uint program_offset;
bool r = pio_claim_free_sm_and_add_program_for_gpio_range(&program_struct, &pio, &state_machine, &program_offset, gpio_base, gpio_count, true);
bool r = pio_claim_free_sm_and_add_program_for_gpio_range(&program_struct, &pio, (uint *)&state_machine, &program_offset, gpio_base, gpio_count, true);
if (!r) {
return false;
}
Expand All @@ -336,6 +342,8 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
}
}

// Sanity check that state_machine number is valid.
assert(state_machine >= 0);
self->pio = pio;
self->state_machine = state_machine;
self->offset = offset;
Expand Down Expand Up @@ -739,6 +747,8 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
fifo_type,
mov_status_type, mov_status_n);
if (!ok) {
// indicate state machine never inited
self->state_machine = NUM_PIO_STATE_MACHINES;
mp_raise_RuntimeError(MP_ERROR_TEXT("All state machines in use"));
}
}
Expand Down