diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 9ba7a0dd151e..93f332adcfd1 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -218,7 +218,7 @@ msgstr "" #: ports/raspberrypi/bindings/cyw43/__init__.c py/argcheck.c py/objexcept.c #: shared-bindings/bitmapfilter/__init__.c shared-bindings/canio/CAN.c #: shared-bindings/digitalio/Pull.c shared-bindings/supervisor/__init__.c -#: shared-module/synthio/Synthesizer.c +#: shared-module/synthio/Biquad.c shared-module/synthio/Synthesizer.c msgid "%q must be of type %q or %q, not %q" msgstr "" @@ -795,6 +795,10 @@ msgstr "" msgid "Cannot subclass slice" msgstr "" +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +msgid "Cannot use GPIO0..15 together with GPIO32..47" +msgstr "" + #: ports/nordic/common-hal/alarm/pin/PinAlarm.c msgid "Cannot wake on pin edge, only level" msgstr "" @@ -3383,10 +3387,6 @@ msgstr "" msgid "label redefined" msgstr "" -#: shared-bindings/audiomixer/MixerVoice.c -msgid "level must be between 0 and 1" -msgstr "" - #: py/objarray.c msgid "lhs and rhs should be compatible" msgstr "" diff --git a/ports/raspberrypi/bindings/rp2pio/StateMachine.c b/ports/raspberrypi/bindings/rp2pio/StateMachine.c index f8a2081332c1..cd35f2fadb4b 100644 --- a/ports/raspberrypi/bindings/rp2pio/StateMachine.c +++ b/ports/raspberrypi/bindings/rp2pio/StateMachine.c @@ -208,8 +208,8 @@ static mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n { MP_QSTR_first_in_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_in_pin_count, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, - { MP_QSTR_pull_in_pin_up, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_pull_in_pin_down, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_pull_in_pin_up, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_obj = MP_ROM_INT(0)} }, + { MP_QSTR_pull_in_pin_down, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_obj = MP_ROM_INT(0) } }, { MP_QSTR_first_set_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_set_pin_count, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, @@ -334,14 +334,14 @@ static mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n args[ARG_frequency].u_int, init_bufinfo.buf, init_bufinfo.len / 2, may_exec_bufinfo.buf, may_exec_bufinfo.len / 2, - first_out_pin, out_pin_count, args[ARG_initial_out_pin_state].u_int, args[ARG_initial_out_pin_direction].u_int, - first_in_pin, in_pin_count, args[ARG_pull_in_pin_up].u_int, args[ARG_pull_in_pin_down].u_int, - first_set_pin, set_pin_count, args[ARG_initial_set_pin_state].u_int, args[ARG_initial_set_pin_direction].u_int, + first_out_pin, out_pin_count, PIO_PINMASK32_FROM_VALUE(args[ARG_initial_out_pin_state].u_int), PIO_PINMASK32_FROM_VALUE(args[ARG_initial_out_pin_direction].u_int), + first_in_pin, in_pin_count, PIO_PINMASK32_FROM_VALUE(args[ARG_pull_in_pin_up].u_int), PIO_PINMASK32_FROM_VALUE(args[ARG_pull_in_pin_down].u_int), + first_set_pin, set_pin_count, PIO_PINMASK32_FROM_VALUE(args[ARG_initial_set_pin_state].u_int), PIO_PINMASK32_FROM_VALUE(args[ARG_initial_set_pin_direction].u_int), first_sideset_pin, sideset_pin_count, args[ARG_sideset_pindirs].u_bool, - args[ARG_initial_sideset_pin_state].u_int, args[ARG_initial_sideset_pin_direction].u_int, + PIO_PINMASK32_FROM_VALUE(args[ARG_initial_sideset_pin_state].u_int), PIO_PINMASK32_FROM_VALUE(args[ARG_initial_sideset_pin_direction].u_int), args[ARG_sideset_enable].u_bool, jmp_pin, jmp_pin_pull, - 0, + PIO_PINMASK_FROM_VALUE(0), // wait_gpio_mask args[ARG_exclusive_pin_use].u_bool, args[ARG_auto_pull].u_bool, pull_threshold, args[ARG_out_shift_right].u_bool, args[ARG_wait_for_txstall].u_bool, diff --git a/ports/raspberrypi/bindings/rp2pio/StateMachine.h b/ports/raspberrypi/bindings/rp2pio/StateMachine.h index 90fb24cbf804..afdffd1eccd3 100644 --- a/ports/raspberrypi/bindings/rp2pio/StateMachine.h +++ b/ports/raspberrypi/bindings/rp2pio/StateMachine.h @@ -21,14 +21,14 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, size_t frequency, const uint16_t *init, size_t init_len, const uint16_t *may_exec, size_t may_exec_len, - const mcu_pin_obj_t *first_out_pin, uint8_t out_pin_count, uint32_t initial_out_pin_state, uint32_t initial_out_pin_direction, - const mcu_pin_obj_t *first_in_pin, uint8_t in_pin_count, uint32_t pull_pin_up, uint32_t pull_pin_down, - const mcu_pin_obj_t *first_set_pin, uint8_t set_pin_count, uint32_t initial_set_pin_state, uint32_t initial_set_pin_direction, + const mcu_pin_obj_t *first_out_pin, uint8_t out_pin_count, pio_pinmask32_t initial_out_pin_state, pio_pinmask32_t initial_out_pin_direction, + const mcu_pin_obj_t *first_in_pin, uint8_t in_pin_count, pio_pinmask32_t in_pull_pin_up, pio_pinmask32_t in_pull_pin_down, + const mcu_pin_obj_t *first_set_pin, uint8_t set_pin_count, pio_pinmask32_t initial_set_pin_state, pio_pinmask32_t initial_set_pin_direction, const mcu_pin_obj_t *first_sideset_pin, uint8_t sideset_pin_count, bool sideset_pindirs, - uint32_t initial_sideset_pin_state, uint32_t initial_sideset_pin_direction, + pio_pinmask32_t initial_sideset_pin_state, pio_pinmask32_t initial_sideset_pin_direction, bool sideset_enable, const mcu_pin_obj_t *jmp_pin, digitalio_pull_t jmp_pin_pull, - uint32_t wait_gpio_mask, + pio_pinmask_t wait_gpio_mask, bool exclusive_pin_use, bool auto_pull, uint8_t pull_threshold, bool out_shift_right, bool wait_for_txstall, diff --git a/ports/raspberrypi/common-hal/audiobusio/I2SOut.c b/ports/raspberrypi/common-hal/audiobusio/I2SOut.c index 90e92a426658..f9484f546a89 100644 --- a/ports/raspberrypi/common-hal/audiobusio/I2SOut.c +++ b/ports/raspberrypi/common-hal/audiobusio/I2SOut.c @@ -184,14 +184,14 @@ void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t *self, 44100 * 32 * 6, // Clock at 44.1 khz to warm the DAC up. NULL, 0, // init NULL, 0, // may_exec - data, 1, 0, 0xffffffff, // out pin + data, 1, PIO_PINMASK32_NONE, PIO_PINMASK32_ALL, // out pin NULL, 0, // in pins - 0, 0, // in pulls - NULL, 0, 0, 0x1f, // set pins - sideset_pin, 2, false, 0, 0x1f, // sideset pins + PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // in pulls + NULL, 0, PIO_PINMASK32_NONE, PIO_PINMASK32_FROM_VALUE(0x1f), // set pins + sideset_pin, 2, false, PIO_PINMASK32_NONE, PIO_PINMASK32_FROM_VALUE(0x1f), // sideset pins false, // No sideset enable NULL, PULL_NONE, // jump pin - 0, // wait gpio pins + PIO_PINMASK_NONE, // wait gpio pins true, // exclusive pin use false, 32, false, // shift out left to start with MSB false, // Wait for txstall diff --git a/ports/raspberrypi/common-hal/audiobusio/PDMIn.c b/ports/raspberrypi/common-hal/audiobusio/PDMIn.c index 6045aae99869..327bec8de728 100644 --- a/ports/raspberrypi/common-hal/audiobusio/PDMIn.c +++ b/ports/raspberrypi/common-hal/audiobusio/PDMIn.c @@ -46,14 +46,14 @@ void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t *self, sample_rate * 32 * 2, // Frequency based on sample rate NULL, 0, NULL, 0, // may_exec - NULL, 1, 0, 0xffffffff, // out pin + NULL, 1, PIO_PINMASK32_NONE, PIO_PINMASK32_ALL, // out pin data_pin, 1, // in pins - 0, 0, // in pulls - NULL, 0, 0, 0x1f, // set pins - clock_pin, 1, false, 0, 0x1f, // sideset pins + PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // in pulls + NULL, 0, PIO_PINMASK32_NONE, PIO_PINMASK32_FROM_VALUE(0x1f), // set pins + clock_pin, 1, false, PIO_PINMASK32_NONE, PIO_PINMASK32_FROM_VALUE(0x1f), // sideset pins false, // No sideset enable NULL, PULL_NONE, // jump pin - 0, // wait gpio pins + PIO_PINMASK_NONE, // wait gpio pins true, // exclusive pin use false, 32, false, // out settings false, // Wait for txstall diff --git a/ports/raspberrypi/common-hal/floppyio/__init__.c b/ports/raspberrypi/common-hal/floppyio/__init__.c index 06da4781e210..3f76e892da8d 100644 --- a/ports/raspberrypi/common-hal/floppyio/__init__.c +++ b/ports/raspberrypi/common-hal/floppyio/__init__.c @@ -77,7 +77,8 @@ int common_hal_floppyio_flux_readinto(void *buf, size_t len, digitalio_digitalin memset(buf, 0, len); - uint32_t pins_we_use = 1 << data->pin->number; + + pio_pinmask_t pins_we_use = PIO_PINMASK_FROM_PIN(data->pin->number); rp2pio_statemachine_obj_t state_machine; bool ok = rp2pio_statemachine_construct(&state_machine, @@ -86,10 +87,10 @@ int common_hal_floppyio_flux_readinto(void *buf, size_t len, digitalio_digitalin NULL, 0, // init program NULL, 0, // out index->pin, 1, // in - 1, 0, // in pull up/down + PIO_PINMASK_FROM_PIN(index->pin->number), PIO_PINMASK_FROM_VALUE(0), // pull up/down NULL, 0, // set NULL, 0, false, // sideset - 0, 0, // initial pin state + PIO_PINMASK_FROM_VALUE(0), PIO_PINMASK_FROM_VALUE(0), // initial pin state data->pin, // jump pin pins_we_use, false, true, true, 32, false, // TX setting we don't use diff --git a/ports/raspberrypi/common-hal/imagecapture/ParallelImageCapture.c b/ports/raspberrypi/common-hal/imagecapture/ParallelImageCapture.c index 835171bef3e6..7be1d5581fcb 100644 --- a/ports/raspberrypi/common-hal/imagecapture/ParallelImageCapture.c +++ b/ports/raspberrypi/common-hal/imagecapture/ParallelImageCapture.c @@ -83,18 +83,19 @@ void common_hal_imagecapture_parallelimagecapture_construct(imagecapture_paralle common_hal_mcu_processor_get_frequency(), // full speed (4 instructions per loop -> max pclk 30MHz @ 120MHz) 0, 0, // init NULL, 0, // may_exec - NULL, 0, 0, 0, // out pins + NULL, 0, PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // out pins pin_from_number(data_pins[0]), data_count, // in pins - 0, 0, // in pulls - NULL, 0, 0, 0, // set pins + PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // in pulls + NULL, 0, PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // set pins #if DEBUG_STATE_MACHINE - &pin_GPIO26, 3, 7, 7, // sideset pins + &pin_GPIO26, 3, PIO_PINMASK32_FROM_VALUE(7), PIO_PINMASK32_FROM_VALUE(7), // sideset pins #else - NULL, 0, false, 0, 0, // sideset pins + NULL, 0, false, PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // sideset pins #endif false, // No sideset enable NULL, PULL_NONE, // jump pin - (1 << vertical_sync->number) | (1 << horizontal_reference->number) | (1 << data_clock->number), // wait gpio pins + PIO_PINMASK_OR3(PIO_PINMASK_FROM_PIN(vertical_sync->number), PIO_PINMASK_FROM_PIN(horizontal_reference->number), PIO_PINMASK_FROM_PIN(data_clock->number)), + // wait gpio pins true, // exclusive pin use false, 32, false, // out settings false, // wait for txstall diff --git a/ports/raspberrypi/common-hal/neopixel_write/__init__.c b/ports/raspberrypi/common-hal/neopixel_write/__init__.c index add8d1f8ceed..76db28a41e12 100644 --- a/ports/raspberrypi/common-hal/neopixel_write/__init__.c +++ b/ports/raspberrypi/common-hal/neopixel_write/__init__.c @@ -40,17 +40,17 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t *digitalinout, // TODO: Cache the state machine after we create it once. We'll need a way to // change the pins then though. - uint32_t pins_we_use = 1 << digitalinout->pin->number; + pio_pinmask_t pins_we_use = PIO_PINMASK_FROM_PIN(digitalinout->pin->number); bool ok = rp2pio_statemachine_construct(&state_machine, neopixel_program, MP_ARRAY_SIZE(neopixel_program), 12800000, // 12.8MHz, to get appropriate sub-bit times in PIO program. NULL, 0, // init program NULL, 1, // out NULL, 1, // in - 0, 0, // in pulls + PIO_PINMASK_NONE, PIO_PINMASK_NONE, // gpio pulls NULL, 1, // set digitalinout->pin, 1, false, // sideset - 0, pins_we_use, // initial pin state + PIO_PINMASK_NONE, pins_we_use, // initial pin state NULL, // jump pin pins_we_use, true, false, true, 8, false, // TX, auto pull every 8 bits. shift left to output msb first diff --git a/ports/raspberrypi/common-hal/paralleldisplaybus/ParallelBus.c b/ports/raspberrypi/common-hal/paralleldisplaybus/ParallelBus.c index b4b61ef88c88..517d960b7650 100644 --- a/ports/raspberrypi/common-hal/paralleldisplaybus/ParallelBus.c +++ b/ports/raspberrypi/common-hal/paralleldisplaybus/ParallelBus.c @@ -79,13 +79,13 @@ void common_hal_paralleldisplaybus_parallelbus_construct(paralleldisplaybus_para frequency * 2, // frequency multiplied by 2 as 2 PIO instructions NULL, 0, // init NULL, 0, // may_exec - data0, 8, 0, 255, // first out pin, # out pins - NULL, 0, 0, 0, // first in pin, # in pins - NULL, 0, 0, 0, // first set pin - write, 1, false, 0, 1, // first sideset pin + data0, 8, PIO_PINMASK32_NONE, PIO_PINMASK32_FROM_VALUE(255), // first out pin, # out pins + NULL, 0, PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // first in pin, # in pins + NULL, 0, PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // first set pin + write, 1, false, PIO_PINMASK32_NONE, PIO_PINMASK32_FROM_VALUE(1), // first sideset pin false, // No sideset enable NULL, PULL_NONE, // jump pin - 0, // wait gpio pins + PIO_PINMASK_NONE, // wait gpio pins true, // exclusive pin usage true, 8, true, // TX, auto pull every 8 bits. shift left to output msb first false, // wait for TX stall diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 18622dcb4ff2..a1d281a24754 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -41,13 +41,13 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, 1000000, // frequency NULL, 0, // init, init_len NULL, 0, // may_exec - NULL, 0, 0, 0, // first out pin, # out pins, initial_out_pin_state - pin, 1, 0, 0, // first in pin, # in pins - NULL, 0, 0, 0, // first set pin - NULL, 0, false, 0, 0, // first sideset pin + NULL, 0, PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // first out pin, # out pins, initial_out_pin_state + pin, 1, PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // first in pin, # in pins + NULL, 0, PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // first set pin + NULL, 0, false, PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // first sideset pin false, // No sideset enable NULL, PULL_NONE, // jump pin, jmp_pull - 0, // wait gpio pins + PIO_PINMASK_NONE, // wait gpio pins true, // exclusive pin usage false, 8, false, // TX, setting we don't use false, // wait for TX stall diff --git a/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c b/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c index ef885512dfe1..113bd1900dd8 100644 --- a/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c @@ -62,14 +62,14 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode 1000000, encoder_init, MP_ARRAY_SIZE(encoder_init), // init NULL, 0, // may_exec - NULL, 0, 0, 0, // out pin + NULL, 0, PIO_PINMASK32_NONE, PIO_PINMASK32_NONE, // out pin pins[0], 2, // in pins - 3, 0, // in pulls - NULL, 0, 0, 0x1f, // set pins - NULL, 0, false, 0, 0x1f, // sideset pins + PIO_PINMASK32_FROM_VALUE(3), PIO_PINMASK32_NONE, // in pulls + NULL, 0, PIO_PINMASK32_NONE, PIO_PINMASK32_FROM_VALUE(0x1f), // set pins + NULL, 0, false, PIO_PINMASK32_NONE, PIO_PINMASK32_FROM_VALUE(0x1f), // sideset pins false, // No sideset enable NULL, PULL_NONE, // jump pin - 0, // wait gpio pins + PIO_PINMASK_NONE, // wait gpio pins true, // exclusive pin use false, 32, false, // out settings false, // Wait for txstall diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c index c41e43855e4e..4d1386ceaddc 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c @@ -35,8 +35,8 @@ static uint8_t _current_program_offset[NUM_PIOS][NUM_PIO_STATE_MACHINES]; static uint8_t _current_program_len[NUM_PIOS][NUM_PIO_STATE_MACHINES]; static bool _never_reset[NUM_PIOS][NUM_PIO_STATE_MACHINES]; -static uint32_t _current_pins[NUM_PIOS]; -static uint32_t _current_sm_pins[NUM_PIOS][NUM_PIO_STATE_MACHINES]; +static pio_pinmask_t _current_pins[NUM_PIOS]; +static pio_pinmask_t _current_sm_pins[NUM_PIOS][NUM_PIO_STATE_MACHINES]; static int8_t _sm_dma_plus_one_write[NUM_PIOS][NUM_PIO_STATE_MACHINES]; static int8_t _sm_dma_plus_one_read[NUM_PIOS][NUM_PIO_STATE_MACHINES]; @@ -77,12 +77,12 @@ static inline void sm_config_set_in_pin_count_issue1878(pio_sm_config *c, uint i ((in_count & 0x1fu) << PIO_SM0_SHIFTCTRL_IN_COUNT_LSB); #endif } -static void rp2pio_statemachine_set_pull(uint32_t pull_pin_up, uint32_t pull_pin_down, uint32_t pins_we_use) { +static void rp2pio_statemachine_set_pull(pio_pinmask_t pull_pin_up, pio_pinmask_t pull_pin_down, pio_pinmask_t pins_we_use) { for (size_t i = 0; i < NUM_BANK0_GPIOS; i++) { - bool used = pins_we_use & (1 << i); + bool used = PIO_PINMASK_IS_SET(pins_we_use, i); if (used) { - bool pull_up = pull_pin_up & (1 << i); - bool pull_down = pull_pin_down & (1 << i); + bool pull_up = PIO_PINMASK_IS_SET(pull_pin_up, i); + bool pull_down = PIO_PINMASK_IS_SET(pull_pin_down, i); gpio_set_pulls(i, pull_up, pull_down); } } @@ -142,9 +142,9 @@ static void _reset_statemachine(PIO pio, uint8_t sm, bool leave_pins) { pio_remove_program(pio, &program_struct, offset); } - uint32_t pins = _current_sm_pins[pio_index][sm]; + pio_pinmask_t pins = _current_sm_pins[pio_index][sm]; for (size_t pin_number = 0; pin_number < NUM_BANK0_GPIOS; pin_number++) { - if ((pins & (1 << pin_number)) == 0) { + if (PIO_PINMASK_IS_SET(pins, pin_number)) { continue; } _pin_reference_count[pin_number]--; @@ -152,10 +152,10 @@ static void _reset_statemachine(PIO pio, uint8_t sm, bool leave_pins) { if (!leave_pins) { reset_pin_number(pin_number); } - _current_pins[pio_index] &= ~(1 << pin_number); + PIO_PINMASK_CLEAR(_current_pins[pio_index], pin_number); } } - _current_sm_pins[pio_index][sm] = 0; + _current_sm_pins[pio_index][sm] = PIO_PINMASK_NONE; pio->inte0 &= ~((PIO_IRQ0_INTF_SM0_RXNEMPTY_BITS | PIO_IRQ0_INTF_SM0_TXNFULL_BITS | PIO_IRQ0_INTF_SM0_BITS) << sm); pio_sm_unclaim(pio, sm); } @@ -179,8 +179,8 @@ void reset_rp2pio_statemachine(void) { } } -static uint32_t _check_pins_free(const mcu_pin_obj_t *first_pin, uint8_t pin_count, bool exclusive_pin_use) { - uint32_t pins_we_use = 0; +static pio_pinmask_t _check_pins_free(const mcu_pin_obj_t *first_pin, uint8_t pin_count, bool exclusive_pin_use) { + pio_pinmask_t pins_we_use = PIO_PINMASK_NONE; if (first_pin != NULL) { for (size_t i = 0; i < pin_count; i++) { uint8_t pin_number = first_pin->number + i; @@ -195,28 +195,12 @@ static uint32_t _check_pins_free(const mcu_pin_obj_t *first_pin, uint8_t pin_cou if (exclusive_pin_use || _pin_reference_count[pin_number] == 0) { assert_pin_free(pin); } - pins_we_use |= 1 << pin_number; + PIO_PINMASK_SET(pins_we_use, pin_number); } } return pins_we_use; } -static bool can_add_program(PIO pio, const pio_program_t *program, int offset) { - if (offset == -1) { - return pio_can_add_program(pio, program); - } - return pio_can_add_program_at_offset(pio, program, offset); -} - -static uint add_program(PIO pio, const pio_program_t *program, int offset) { - if (offset == -1) { - return pio_add_program(pio, program); - } else { - pio_add_program_at_offset(pio, program, offset); - return offset; - } -} - static enum pio_fifo_join compute_fifo_type(int fifo_type_in, bool rx_fifo, bool tx_fifo) { if (fifo_type_in != PIO_FIFO_JOIN_AUTO) { return fifo_type_in; @@ -244,18 +228,60 @@ static int compute_fifo_depth(enum pio_fifo_join join) { return 4; } + +// from pico-sdk/src/rp2_common/hardware_pio/pio.c +static bool is_gpio_compatible(PIO pio, uint32_t used_gpio_ranges) { + #if PICO_PIO_VERSION > 0 + bool gpio_base = pio_get_gpio_base(pio); + return !((gpio_base && (used_gpio_ranges & 1)) || + (!gpio_base && (used_gpio_ranges & 4))); + #else + ((void)pio); + ((void)used_gpio_ranges); + return true; + #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) { + uint32_t required_gpio_ranges; + if (gpio_count) { + required_gpio_ranges = (1u << (gpio_base >> 4)) | + (1u << ((gpio_base + gpio_count - 1) >> 4)); + } else { + required_gpio_ranges = 0; + } + + for (size_t i = 0; i < NUM_PIOS; i++) { + PIO pio = pio_instances[i]; + if (!is_gpio_compatible(pio, required_gpio_ranges)) { + continue; + } + for (size_t j = 0; j < NUM_PIO_STATE_MACHINES; j++) { + if (_current_program_id[i][j] == program_id && + _current_program_len[i][j] == program_len && + (*offset_inout == -1 || *offset_inout == _current_program_offset[i][j])) { + *pio_out = pio; + *sm_out = j; + *offset_inout = _current_program_offset[i][j]; + return true; + } + } + } + return false; +} + bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, const uint16_t *program, size_t program_len, size_t frequency, const uint16_t *init, size_t init_len, const mcu_pin_obj_t *first_out_pin, uint8_t out_pin_count, const mcu_pin_obj_t *first_in_pin, uint8_t in_pin_count, - uint32_t pull_pin_up, uint32_t pull_pin_down, + pio_pinmask_t pull_pin_up, pio_pinmask_t pull_pin_down, // GPIO numbering const mcu_pin_obj_t *first_set_pin, uint8_t set_pin_count, const mcu_pin_obj_t *first_sideset_pin, uint8_t sideset_pin_count, bool sideset_pindirs, - uint32_t initial_pin_state, uint32_t initial_pin_direction, + pio_pinmask_t initial_pin_state, pio_pinmask_t initial_pin_direction, const mcu_pin_obj_t *jmp_pin, - uint32_t pins_we_use, bool tx_fifo, bool rx_fifo, + pio_pinmask_t pins_we_use, bool tx_fifo, bool rx_fifo, bool auto_pull, uint8_t pull_threshold, bool out_shift_right, bool wait_for_txstall, bool auto_push, uint8_t push_threshold, bool in_shift_right, @@ -270,70 +296,75 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, // Create a program id that isn't the pointer so we can store it without storing the original object. uint32_t program_id = ~((uint32_t)program); + uint gpio_base = 0, gpio_count = 0; + #if NUM_BANK0_GPIOS > 32 + if (PIO_PINMASK_VALUE(pins_we_use) >> 32) { + if (PIO_PINMASK_VALUE(pins_we_use) & 0xffff) { + // Uses pins from 0-15 and 32-47. not possible + return false; + } + } + + pio_pinmask_value_t v = PIO_PINMASK_VALUE(pins_we_use); + if (v) { + while (!(v & 1)) { + gpio_base++; + v >>= 1; + } + while (v) { + gpio_count++; + v >>= 1; + } + } + #endif + // Next, find a PIO and state machine to use. - size_t pio_index = NUM_PIOS; - uint8_t program_offset = 32; pio_program_t program_struct = { .instructions = (uint16_t *)program, .length = program_len, .origin = -1 }; - for (size_t i = 0; i < NUM_PIOS; i++) { - PIO pio = pio_instances[i]; - uint8_t free_count = 0; - for (size_t j = 0; j < NUM_PIO_STATE_MACHINES; j++) { - if (_current_program_id[i][j] == program_id && - _current_program_len[i][j] == program_len && - (offset == -1 || offset == _current_program_offset[i][j])) { - program_offset = _current_program_offset[i][j]; - } - if (!pio_sm_is_claimed(pio, j)) { - free_count++; - } - } - if (free_count > 0 && (program_offset < 32 || can_add_program(pio, &program_struct, offset))) { - pio_index = i; - if (program_offset < 32) { - break; - } + PIO pio; + uint 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); + if (!r) { + return false; } - // Reset program offset if we weren't able to find a free state machine - // on that PIO. (We would have broken the loop otherwise.) - program_offset = 32; + offset = program_offset; + added = true; } - size_t state_machine = NUM_PIO_STATE_MACHINES; - if (pio_index < NUM_PIOS) { - PIO pio = pio_instances[pio_index]; - for (size_t i = 0; i < NUM_PIOS; i++) { - if (i == pio_index) { - continue; - } - if ((_current_pins[i] & pins_we_use) != 0) { - // Pin in use by another PIO already. - return false; + size_t pio_index = pio_get_index(pio); + for (size_t i = 0; i < NUM_PIOS; i++) { + if (i == pio_index) { + continue; + } + pio_pinmask_t intersection = PIO_PINMASK_AND(_current_pins[i], pins_we_use); + if (PIO_PINMASK_VALUE(intersection) != 0) { + if (added) { + pio_remove_program(pio, &program_struct, offset); } + pio_sm_unclaim(pio, state_machine); + // Pin in use by another PIO already. + return false; } - state_machine = pio_claim_unused_sm(pio, false); - } - if (pio_index == NUM_PIOS || state_machine < 0 || state_machine >= NUM_PIO_STATE_MACHINES) { - return false; } - self->pio = pio_instances[pio_index]; + self->pio = pio; self->state_machine = state_machine; - if (program_offset == 32) { - program_offset = add_program(self->pio, &program_struct, offset); - } - self->offset = program_offset; + self->offset = offset; _current_program_id[pio_index][state_machine] = program_id; _current_program_len[pio_index][state_machine] = program_len; - _current_program_offset[pio_index][state_machine] = program_offset; + _current_program_offset[pio_index][state_machine] = offset; _current_sm_pins[pio_index][state_machine] = pins_we_use; - _current_pins[pio_index] |= pins_we_use; + PIO_PINMASK_MERGE(_current_pins[pio_index], pins_we_use); - pio_sm_set_pins_with_mask(self->pio, state_machine, initial_pin_state, pins_we_use); - pio_sm_set_pindirs_with_mask(self->pio, state_machine, initial_pin_direction, pins_we_use); + pio_sm_set_pins_with_mask64(self->pio, state_machine, PIO_PINMASK_VALUE(initial_pin_state), PIO_PINMASK_VALUE(pins_we_use)); + pio_sm_set_pindirs_with_mask64(self->pio, state_machine, PIO_PINMASK_VALUE(initial_pin_direction), PIO_PINMASK_VALUE(pins_we_use)); rp2pio_statemachine_set_pull(pull_pin_up, pull_pin_down, pins_we_use); self->initial_pin_state = initial_pin_state; self->initial_pin_direction = initial_pin_direction; @@ -341,7 +372,7 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, self->pull_pin_down = pull_pin_down; for (size_t pin_number = 0; pin_number < NUM_BANK0_GPIOS; pin_number++) { - if ((pins_we_use & (1 << pin_number)) == 0) { + if (PIO_PINMASK_IS_SET(pins_we_use, pin_number)) { continue; } const mcu_pin_obj_t *pin = mcu_get_pin_by_number(pin_number); @@ -404,8 +435,8 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, mp_arg_validate_int_range(wrap_target, 0, program_len - 1, MP_QSTR_wrap_target); - wrap += program_offset; - wrap_target += program_offset; + wrap += offset; + wrap_target += offset; sm_config_set_wrap(&c, wrap_target, wrap); sm_config_set_in_shift(&c, in_shift_right, auto_push, push_threshold); @@ -459,7 +490,7 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, SM_DMA_CLEAR_CHANNEL_READ(pio_index, state_machine); SM_DMA_CLEAR_CHANNEL_WRITE(pio_index, state_machine); - pio_sm_init(self->pio, self->state_machine, program_offset, &c); + pio_sm_init(self->pio, self->state_machine, offset, &c); common_hal_rp2pio_statemachine_run(self, init, init_len); common_hal_rp2pio_statemachine_set_frequency(self, frequency); @@ -467,18 +498,20 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, return true; } -static uint32_t mask_and_rotate(const mcu_pin_obj_t *first_pin, uint32_t bit_count, uint32_t value) { +static pio_pinmask_t mask_and_shift(const mcu_pin_obj_t *first_pin, uint32_t bit_count, pio_pinmask32_t value_in) { if (!first_pin) { - return 0; + return PIO_PINMASK_NONE; } - value = value & ((1 << bit_count) - 1); - uint32_t shift = first_pin->number; - return value << shift | value >> (32 - shift); + pio_pinmask_value_t mask = (PIO_PINMASK_C(1) << bit_count) - 1; + pio_pinmask_value_t value = (pio_pinmask_value_t)PIO_PINMASK32_VALUE(value_in); + int shift = first_pin->number; + return PIO_PINMASK_FROM_VALUE((value & mask) << shift); } typedef struct { struct { - uint32_t pins_we_use, in_pin_count, out_pin_count; + pio_pinmask_t pins_we_use; + uint8_t in_pin_count, out_pin_count, pio_gpio_offset; bool has_jmp_pin, auto_push, auto_pull, has_in_pin, has_out_pin, has_set_pin; } inputs; struct { @@ -505,11 +538,10 @@ static void consider_instruction(introspect_t *state, uint16_t full_instruction, } if (instruction == pio_instr_bits_wait) { uint16_t wait_source = (full_instruction & 0x0060) >> 5; - uint16_t wait_index = full_instruction & 0x001f; - if (wait_source == 0 && (state->inputs.pins_we_use & (1 << wait_index)) == 0) { // GPIO + uint16_t wait_index = (full_instruction & 0x001f) + state->inputs.pio_gpio_offset; + if (wait_source == 0 && !PIO_PINMASK_IS_SET(state->inputs.pins_we_use, wait_index)) { // GPIO mp_raise_ValueError_varg(MP_ERROR_TEXT("%q[%u] uses extra pin"), what_program, i); - } - if (wait_source == 1) { // Input pin + } else if (wait_source == 1) { // Input pin if (!state->inputs.has_in_pin) { mp_raise_ValueError_varg(MP_ERROR_TEXT("Missing first_in_pin. %q[%u] waits based on pin"), what_program, i); } @@ -589,15 +621,15 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, size_t frequency, const uint16_t *init, size_t init_len, const uint16_t *may_exec, size_t may_exec_len, - const mcu_pin_obj_t *first_out_pin, uint8_t out_pin_count, uint32_t initial_out_pin_state, uint32_t initial_out_pin_direction, + const mcu_pin_obj_t *first_out_pin, uint8_t out_pin_count, pio_pinmask32_t initial_out_pin_state32, pio_pinmask32_t initial_out_pin_direction32, const mcu_pin_obj_t *first_in_pin, uint8_t in_pin_count, - uint32_t pull_pin_up, uint32_t pull_pin_down, - const mcu_pin_obj_t *first_set_pin, uint8_t set_pin_count, uint32_t initial_set_pin_state, uint32_t initial_set_pin_direction, + pio_pinmask32_t in_pull_pin_up32, pio_pinmask32_t in_pull_pin_down32, // relative to first_in_pin + const mcu_pin_obj_t *first_set_pin, uint8_t set_pin_count, pio_pinmask32_t initial_set_pin_state32, pio_pinmask32_t initial_set_pin_direction32, const mcu_pin_obj_t *first_sideset_pin, uint8_t sideset_pin_count, bool sideset_pindirs, - uint32_t initial_sideset_pin_state, uint32_t initial_sideset_pin_direction, + pio_pinmask32_t initial_sideset_pin_state32, pio_pinmask32_t initial_sideset_pin_direction32, bool sideset_enable, const mcu_pin_obj_t *jmp_pin, digitalio_pull_t jmp_pull, - uint32_t wait_gpio_mask, + pio_pinmask_t wait_gpio_mask, bool exclusive_pin_use, bool auto_pull, uint8_t pull_threshold, bool out_shift_right, bool wait_for_txstall, @@ -610,17 +642,28 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, int mov_status_n) { // First, check that all pins are free OR already in use by any PIO if exclusive_pin_use is false. - uint32_t pins_we_use = wait_gpio_mask; - pins_we_use |= _check_pins_free(first_out_pin, out_pin_count, exclusive_pin_use); - pins_we_use |= _check_pins_free(first_in_pin, in_pin_count, exclusive_pin_use); - pins_we_use |= _check_pins_free(first_set_pin, set_pin_count, exclusive_pin_use); - pins_we_use |= _check_pins_free(first_sideset_pin, sideset_pin_count, exclusive_pin_use); - pins_we_use |= _check_pins_free(jmp_pin, 1, exclusive_pin_use); + pio_pinmask_t pins_we_use = wait_gpio_mask; + PIO_PINMASK_MERGE(pins_we_use, _check_pins_free(first_out_pin, out_pin_count, exclusive_pin_use)); + PIO_PINMASK_MERGE(pins_we_use, _check_pins_free(first_in_pin, in_pin_count, exclusive_pin_use)); + PIO_PINMASK_MERGE(pins_we_use, _check_pins_free(first_set_pin, set_pin_count, exclusive_pin_use)); + PIO_PINMASK_MERGE(pins_we_use, _check_pins_free(first_sideset_pin, sideset_pin_count, exclusive_pin_use)); + PIO_PINMASK_MERGE(pins_we_use, _check_pins_free(jmp_pin, 1, exclusive_pin_use)); + + int pio_gpio_offset = 0; + #if NUM_BANK0_GPIOS > 32 + if (PIO_PINMASK_VALUE(pins_we_use) >> 32) { + pio_gpio_offset = 16; + if (PIO_PINMASK_VALUE(pins_we_use) & 0xffff) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("Cannot use GPIO0..15 together with GPIO32..47")); + } + } + #endif // Look through the program to see what we reference and make sure it was provided. introspect_t state = { .inputs = { .pins_we_use = pins_we_use, + .pio_gpio_offset = pio_gpio_offset, .has_jmp_pin = jmp_pin != NULL, .has_in_pin = first_in_pin != NULL, .has_out_pin = first_out_pin != NULL, @@ -642,43 +685,54 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, mp_raise_ValueError_varg(MP_ERROR_TEXT("Program does OUT without loading OSR")); } - uint32_t initial_pin_state = mask_and_rotate(first_out_pin, out_pin_count, initial_out_pin_state); - uint32_t initial_pin_direction = mask_and_rotate(first_out_pin, out_pin_count, initial_out_pin_direction); - initial_set_pin_state = mask_and_rotate(first_set_pin, set_pin_count, initial_set_pin_state); - initial_set_pin_direction = mask_and_rotate(first_set_pin, set_pin_count, initial_set_pin_direction); - uint32_t set_out_overlap = mask_and_rotate(first_out_pin, out_pin_count, 0xffffffff) & - mask_and_rotate(first_set_pin, set_pin_count, 0xffffffff); + pio_pinmask_t initial_pin_state = mask_and_shift(first_out_pin, out_pin_count, initial_out_pin_state32); + pio_pinmask_t initial_pin_direction = mask_and_shift(first_out_pin, out_pin_count, initial_out_pin_direction32); + pio_pinmask_t initial_set_pin_state = mask_and_shift(first_set_pin, set_pin_count, initial_set_pin_state32); + pio_pinmask_t initial_set_pin_direction = mask_and_shift(first_set_pin, set_pin_count, initial_set_pin_direction32); + pio_pinmask_t set_out_overlap = PIO_PINMASK_AND(mask_and_shift(first_out_pin, out_pin_count, PIO_PINMASK32_ALL), + mask_and_shift(first_set_pin, set_pin_count, PIO_PINMASK32_ALL)); // Check that OUT and SET settings agree because we don't have a way of picking one over the other. - if ((initial_pin_state & set_out_overlap) != (initial_set_pin_state & set_out_overlap)) { + if (!PIO_PINMASK_EQUAL( + PIO_PINMASK_AND(initial_pin_state, set_out_overlap), + PIO_PINMASK_AND(initial_set_pin_state, set_out_overlap))) { mp_raise_ValueError(MP_ERROR_TEXT("Initial set pin state conflicts with initial out pin state")); } - if ((initial_pin_direction & set_out_overlap) != (initial_set_pin_direction & set_out_overlap)) { + if (!PIO_PINMASK_EQUAL( + PIO_PINMASK_AND(initial_pin_direction, set_out_overlap), + PIO_PINMASK_AND(initial_set_pin_direction, set_out_overlap))) { mp_raise_ValueError(MP_ERROR_TEXT("Initial set pin direction conflicts with initial out pin direction")); } - initial_pin_state |= initial_set_pin_state; - initial_pin_direction |= initial_set_pin_direction; + PIO_PINMASK_MERGE(initial_pin_state, initial_set_pin_state); + PIO_PINMASK_MERGE(initial_pin_direction, initial_set_pin_direction); // Sideset overrides OUT or SET so we always use its values. - uint32_t sideset_mask = mask_and_rotate(first_sideset_pin, sideset_pin_count, 0x1f); - initial_pin_state = (initial_pin_state & ~sideset_mask) | mask_and_rotate(first_sideset_pin, sideset_pin_count, initial_sideset_pin_state); - initial_pin_direction = (initial_pin_direction & ~sideset_mask) | mask_and_rotate(first_sideset_pin, sideset_pin_count, initial_sideset_pin_direction); + pio_pinmask_t sideset_mask = mask_and_shift(first_sideset_pin, sideset_pin_count, PIO_PINMASK32_FROM_VALUE(0x1f)); + initial_pin_state = PIO_PINMASK_OR( + PIO_PINMASK_AND_NOT(initial_pin_state, sideset_mask), + mask_and_shift(first_sideset_pin, sideset_pin_count, initial_sideset_pin_state32)); + initial_pin_direction = PIO_PINMASK_OR( + PIO_PINMASK_AND_NOT(initial_pin_direction, sideset_mask), + mask_and_shift(first_sideset_pin, sideset_pin_count, initial_sideset_pin_direction32)); // Deal with pull up/downs - uint32_t pull_up = mask_and_rotate(first_in_pin, in_pin_count, pull_pin_up); - uint32_t pull_down = mask_and_rotate(first_in_pin, in_pin_count, pull_pin_down); + pio_pinmask_t pull_up = mask_and_shift(first_in_pin, in_pin_count, in_pull_pin_up32); + pio_pinmask_t pull_down = mask_and_shift(first_in_pin, in_pin_count, in_pull_pin_down32); if (jmp_pin) { - uint32_t jmp_mask = mask_and_rotate(jmp_pin, 1, 0x1f); + pio_pinmask_t jmp_mask = mask_and_shift(jmp_pin, 1, PIO_PINMASK32_FROM_VALUE(0x1f)); if (jmp_pull == PULL_UP) { - pull_up |= jmp_mask; + PIO_PINMASK_MERGE(pull_up, jmp_mask); } if (jmp_pull == PULL_DOWN) { - pull_down |= jmp_mask; + PIO_PINMASK_MERGE(pull_down, jmp_mask); } } - if (initial_pin_direction & (pull_up | pull_down)) { + if (PIO_PINMASK_VALUE( + PIO_PINMASK_AND(initial_pin_direction, + PIO_PINMASK_OR(pull_up, pull_down)))) { mp_raise_ValueError(MP_ERROR_TEXT("pull masks conflict with direction masks")); } + bool ok = rp2pio_statemachine_construct( self, program, program_len, @@ -713,9 +767,9 @@ void common_hal_rp2pio_statemachine_restart(rp2pio_statemachine_obj_t *self) { pio_sm_exec(self->pio, self->state_machine, self->offset); pio_sm_restart(self->pio, self->state_machine); uint8_t pio_index = pio_get_index(self->pio); - uint32_t pins_we_use = _current_sm_pins[pio_index][self->state_machine]; - pio_sm_set_pins_with_mask(self->pio, self->state_machine, self->initial_pin_state, pins_we_use); - pio_sm_set_pindirs_with_mask(self->pio, self->state_machine, self->initial_pin_direction, pins_we_use); + pio_pinmask_t pins_we_use = _current_sm_pins[pio_index][self->state_machine]; + pio_sm_set_pins_with_mask64(self->pio, self->state_machine, PIO_PINMASK_VALUE(self->initial_pin_state), PIO_PINMASK_VALUE(pins_we_use)); + pio_sm_set_pindirs_with_mask64(self->pio, self->state_machine, PIO_PINMASK_VALUE(self->initial_pin_direction), PIO_PINMASK_VALUE(pins_we_use)); rp2pio_statemachine_set_pull(self->pull_pin_up, self->pull_pin_down, pins_we_use); common_hal_rp2pio_statemachine_run(self, self->init, self->init_len); pio_sm_set_enabled(self->pio, self->state_machine, true); diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.h b/ports/raspberrypi/common-hal/rp2pio/StateMachine.h index 8a597544b401..9b741bd41c6e 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.h +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.h @@ -12,6 +12,75 @@ #include "common-hal/memorymap/AddressRange.h" #include "src/rp2_common/hardware_pio/include/hardware/pio.h" +// pio_pinmask_t can hold ANY pin masks, so it is used before selection of gpiobase +#if NUM_BANK0_GPIOS > 32 +typedef struct { uint64_t value; +} pio_pinmask_t; +typedef uint64_t pio_pinmask_value_t; +#define PIO_PINMASK_C(c) UINT64_C(c) +#define PIO_PINMASK_BIT (64) +#define PIO_PINMASK(i) (UINT64_C(1) << (i)) +#define PIO_PINMASK_PRINT(p) mp_printf(&mp_plat_print, \ + "%s:%d: %s = %08x %08x\n", \ + __FILE__, __LINE__, #p, \ + (uint32_t)(PIO_PINMASK_VALUE(p) >> 32), \ + (uint32_t)PIO_PINMASK_VALUE(p)); +#define PIO_PINMASK_ALL PIO_PINMASK_FROM_VALUE(~UINT64_C(0)) +#else +typedef struct { uint32_t value; +} pio_pinmask_t; +typedef uint32_t pio_pinmask_value_t; +#define PIO_PINMASK_C(c) UINT32_C(c) +#define PIO_PINMASK_BIT (32) +#define PIO_PINMASK(i) (UINT32_C(1) << (i)) +#define PIO_PINMASK_PRINT(p) mp_printf(&mp_plat_print, "%s:%d: %s = %08x\n", \ + __FILE__, __LINE__, #p, \ + (uint32_t)(PIO_PINMASK_VALUE(p))); +#define PIO_PINMASK_ALL PIO_PINMASK_FROM_VALUE(~UINT32_C(0)) +#endif +#define PIO_PINMASK_VALUE(p) ((p).value) +#define PIO_PINMASK_FROM_VALUE(v) ((pio_pinmask_t) {(v)}) +#define PIO_PINMASK_FROM_PIN(i) ((pio_pinmask_t) {(PIO_PINMASK(i))}) +#define PIO_PINMASK_NONE PIO_PINMASK_FROM_VALUE(0) +#define PIO_PINMASK_SET(p, i) ((p).value |= PIO_PINMASK(i)) +#define PIO_PINMASK_CLEAR(p, i) ((p).value &= ~PIO_PINMASK(i)) +#define PIO_PINMASK_IS_SET(p, i) (((p).value & ~PIO_PINMASK(i)) != 0) +#define PIO_PINMASK_BINOP(op, p, q) PIO_PINMASK_FROM_VALUE((p).value op(q).value) +#define PIO_PINMASK_BINOP_ASSIGN(op, p, q) ((p).value op(q).value) +#define PIO_PINMASK_EQUAL(p, q) ((p).value == (q).value) +#define PIO_PINMASK_AND(p, q) PIO_PINMASK_BINOP(&, (p), (q)) +#define PIO_PINMASK_AND_NOT(p, q) PIO_PINMASK_BINOP(&~, (p), (q)) +#define PIO_PINMASK_OR(p, q) PIO_PINMASK_BINOP(|, (p), (q)) +#define PIO_PINMASK_OR3(p, q, r) PIO_PINMASK_OR((p), PIO_PINMASK_OR((q), (r))) +#define PIO_PINMASK_INTERSECT(p, q) PIO_PINMASK_BINOP_ASSIGN( &=, (p), (q)) +#define PIO_PINMASK_DIFFERENCE(p, q) PIO_PINMASK_BINOP_ASSIGN( &= ~, (p), (q)) +#define PIO_PINMASK_MERGE(p, q) PIO_PINMASK_BINOP_ASSIGN( |=, (p), (q)) + +// pio peripheral registers only work 32 bits at a time and depend on the selection of base +// (0 only on RP2040 & RP2350A; 0 or 16 on RP2350B) +typedef struct { uint32_t value32; +} pio_pinmask32_t; +#define PIO_PINMASK32(i) (1u << (i)) +#define PIO_PINMASK32_C(c) UINT32_C(c) +#define PIO_PINMASK32_NONE PIO_PINMASK32_FROM_VALUE(0) +#define PIO_PINMASK32_ALL PIO_PINMASK32_FROM_VALUE(~UINT32_C(0)) +#define PIO_PINMASK32_BASE(i, base) PIO_PINMASK32((i) - (base)) +#define PIO_PINMASK32_VALUE(p) ((p).value32) +#define PIO_PINMASK32_FROM_VALUE(v) ((pio_pinmask32_t) {(v)}) +#define PIO_PINMASK32_SET(p, i) ((p).value32 |= PIO_PINMASK32_VALUE(i)) +#define PIO_PINMASK32_CLEAR(p, i) ((p).value32 &= ~PIO_PINMASK32_VALUE(i)) +#define PIO_PINMASK32_IS_SET(p, i) (((p).value32 & ~PIO_PINMASK32_VALUE(i)) != 0) +#define PIO_PINMASK32_BINOP(op, p, q) PIO_PINMASK32_FROM_VALUE((p).value32 op(q).value32) +#define PIO_PINMASK32_AND(p, q) PIO_PINMASK32_BINOP(&, (p), (q)) +#define PIO_PINMASK32_AND_NOT(p, q) PIO_PINMASK32_BINOP(&~, (p), (q)) +#define PIO_PINMASK32_OR(p, q) PIO_PINMASK32_BINOP(|, (p), (q)) +#define PIO_PINMASK32_OR3(p, q, r) PIO_PINMASK32_OR((p), PIO_PINMASK32_OR((q), (r))) +#define PIO_PINMASK32_INTERSECT(p, q) PIO_PINMASK32_BINOP( &=, (p), (q)) +#define PIO_PINMASK32_DIFFERENCE(p, q) PIO_PINMASK32_BINOP( &= ~, (p), (q)) +#define PIO_PINMASK32_MERGE(p, q) PIO_PINMASK32_BINOP( |=, (p), (q)) +#define PIO_PINMASK32_FROM_PINMASK_WITH_OFFSET(p, gpio_offset) PIO_PINMASK32_FROM_VALUE(PIO_PINMASK_VALUE((p)) >> (gpio_offset)) +#define PIO_PINMASK_FROM_PINMASK32_WITH_OFFSET(p, gpio_offset) PIO_PINMASK_FROM_VALUE(PIO_PINMASK32_VALUE((p)) << (gpio_offset)) + enum { PIO_ANY_OFFSET = -1 }; enum { PIO_FIFO_JOIN_AUTO = -1, PIO_FIFO_TYPE_DEFAULT = PIO_FIFO_JOIN_AUTO }; enum { PIO_MOV_STATUS_DEFAULT = STATUS_TX_LESSTHAN }; @@ -26,15 +95,15 @@ typedef struct sm_buf_info { typedef struct { mp_obj_base_t base; - uint32_t pins; // Bitmask of what pins this state machine uses. + pio_pinmask32_t pins; // Bitmask of what pins this state machine uses. int state_machine; PIO pio; const uint16_t *init; size_t init_len; - uint32_t initial_pin_state; - uint32_t initial_pin_direction; - uint32_t pull_pin_up; - uint32_t pull_pin_down; + pio_pinmask_t initial_pin_state; + pio_pinmask_t initial_pin_direction; + pio_pinmask_t pull_pin_up; + pio_pinmask_t pull_pin_down; uint tx_dreq; uint rx_dreq; uint32_t actual_frequency; @@ -45,6 +114,9 @@ typedef struct { bool out_shift_right; bool in_shift_right; bool user_interruptible; + #if NUM_BANK0_GPIOS > 32 + uint8_t pio_gpio_offset; + #endif uint8_t offset; uint8_t fifo_depth; // Either 4 if FIFOs are not joined, or 8 if they are. @@ -79,12 +151,12 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, const uint16_t *init, size_t init_len, const mcu_pin_obj_t *first_out_pin, uint8_t out_pin_count, const mcu_pin_obj_t *first_in_pin, uint8_t in_pin_count, - uint32_t pull_pin_up, uint32_t pull_pin_down, + pio_pinmask_t pull_pin_up, pio_pinmask_t pull_pin_down, const mcu_pin_obj_t *first_set_pin, uint8_t set_pin_count, const mcu_pin_obj_t *first_sideset_pin, uint8_t sideset_pin_count, bool sideset_pindirs, - uint32_t initial_pin_state, uint32_t initial_pin_direction, + pio_pinmask_t initial_pin_state, pio_pinmask_t initial_pin_direction, const mcu_pin_obj_t *jmp_pin, - uint32_t pins_we_use, bool tx_fifo, bool rx_fifo, + pio_pinmask_t pins_we_use, bool tx_fifo, bool rx_fifo, bool auto_pull, uint8_t pull_threshold, bool out_shift_right, bool wait_for_txstall, bool auto_push, uint8_t push_threshold, bool in_shift_right, diff --git a/ports/raspberrypi/common-hal/usb_host/Port.c b/ports/raspberrypi/common-hal/usb_host/Port.c index 5fb4506a145e..cf242dc80926 100644 --- a/ports/raspberrypi/common-hal/usb_host/Port.c +++ b/ports/raspberrypi/common-hal/usb_host/Port.c @@ -96,6 +96,20 @@ static bool _has_program_room(uint8_t pio_index, uint8_t program_size) { return pio_can_add_program(pio, &program_struct); } +// from pico-sdk/src/rp2_common/hardware_pio/pio.c +static bool is_gpio_compatible(PIO pio, uint32_t used_gpio_ranges) { + #if PICO_PIO_VERSION > 0 + bool gpio_base = pio_get_gpio_base(pio); + return !((gpio_base && (used_gpio_ranges & 1)) || + (!gpio_base && (used_gpio_ranges & 4))); + #else + ((void)pio); + ((void)used_gpio_ranges); + return true; + #endif +} + + usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm) { if (dp->number + 1 != dm->number) { raise_ValueError_invalid_pins(); @@ -113,6 +127,15 @@ usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, assert_pin_free(dp); assert_pin_free(dm); + #if PICO_PIO_VERSION == 0 + uint32_t used_gpio_ranges = 0; + #else + uint gpio_base = dm->number; + uint gpio_count = 2; + uint32_t used_gpio_ranges = (1u << (gpio_base >> 4)) | + (1u << ((gpio_base + gpio_count - 1) >> 4)); + #endif + pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; pio_cfg.skip_alarm_pool = true; pio_cfg.pin_dp = dp->number; @@ -120,8 +143,15 @@ usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, // "preferred PIO" for the cyw43 wifi chip is PIO 1. pio_cfg.pio_tx_num = 1; // uses 22 instructions and 1 SM pio_cfg.pio_rx_num = 0; // uses 31 instructions and 2 SM. - if (!_has_program_room(pio_cfg.pio_tx_num, 22) || _sm_free_count(pio_cfg.pio_tx_num) < 1 || - !_has_program_room(pio_cfg.pio_rx_num, 31) || _sm_free_count(pio_cfg.pio_rx_num) < 2) { + uint8_t tx_sm_free = _sm_free_count(pio_cfg.pio_tx_num); + uint8_t rx_sm_free = _sm_free_count(pio_cfg.pio_rx_num); + PIO pio_tx = pio_instances[pio_cfg.pio_tx_num]; + PIO pio_rx = pio_instances[pio_cfg.pio_rx_num]; + + if (!_has_program_room(pio_cfg.pio_tx_num, 22) || tx_sm_free < 1 || + !(tx_sm_free == 4 || is_gpio_compatible(pio_tx, used_gpio_ranges)) || + !_has_program_room(pio_cfg.pio_rx_num, 31) || rx_sm_free < 2 || + !(rx_sm_free == 4 || is_gpio_compatible(pio_rx, used_gpio_ranges))) { mp_raise_RuntimeError(MP_ERROR_TEXT("All state machines in use")); } pio_cfg.tx_ch = dma_claim_unused_channel(false); // DMA channel diff --git a/ports/raspberrypi/lib/Pico-PIO-USB b/ports/raspberrypi/lib/Pico-PIO-USB index fe9133fc513b..445ca6403643 160000 --- a/ports/raspberrypi/lib/Pico-PIO-USB +++ b/ports/raspberrypi/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit fe9133fc513b82cc3dc62c67cb51f2339cf29ef7 +Subproject commit 445ca6403643f19ae8a70a3632b6924943a93189 diff --git a/ports/raspberrypi/sdk_config/pico/version.h b/ports/raspberrypi/sdk_config/pico/version.h index 0596c75dd073..b5803bb98c9d 100644 --- a/ports/raspberrypi/sdk_config/pico/version.h +++ b/ports/raspberrypi/sdk_config/pico/version.h @@ -12,8 +12,8 @@ #define _PICO_VERSION_H #define PICO_SDK_VERSION_MAJOR 2 -#define PICO_SDK_VERSION_MINOR 0 +#define PICO_SDK_VERSION_MINOR 1 #define PICO_SDK_VERSION_REVISION 0 -#define PICO_SDK_VERSION_STRING "2.0.0" +#define PICO_SDK_VERSION_STRING "2.1.0" #endif