Skip to content

Commit

Permalink
Merge pull request #5766 from tannewt/rp2040_sideset_enable
Browse files Browse the repository at this point in the history
Add sideset_enable support to PIO
  • Loading branch information
jepler authored Dec 23, 2021
2 parents 674c845 + 827ad32 commit c1236fb
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 10 deletions.
8 changes: 7 additions & 1 deletion ports/raspberrypi/bindings/rp2pio/StateMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
//| sideset_pin_count: int = 1,
//| initial_sideset_pin_state: int = 0,
//| initial_sideset_pin_direction: int = 0x1f,
//| sideset_enable: bool = False,
//| exclusive_pin_use: bool = True,
//| auto_pull: bool = False,
//| pull_threshold: int = 32,
Expand Down Expand Up @@ -107,9 +108,10 @@
//| :param int initial_set_pin_state: the initial output value for set pins starting at first_set_pin
//| :param int initial_set_pin_direction: the initial output direction for set pins starting at first_set_pin
//| :param ~microcontroller.Pin first_sideset_pin: the first pin to use with a side set
//| :param int sideset_pin_count: the count of consecutive pins to use with a side set starting at first_sideset_pin
//| :param int sideset_pin_count: the count of consecutive pins to use with a side set starting at first_sideset_pin. Does not include sideset enable
//| :param int initial_sideset_pin_state: the initial output value for sideset pins starting at first_sideset_pin
//| :param int initial_sideset_pin_direction: the initial output direction for sideset pins starting at first_sideset_pin
//| :param bool sideset_enable: True when the top sideset bit is to enable. This should be used with the ".side_set # opt" directive
//| :param ~microcontroller.Pin jmp_pin: the pin which determines the branch taken by JMP PIN instructions
//| :param bool exclusive_pin_use: When True, do not share any pins with other state machines. Pins are never shared with other peripherals
//| :param bool auto_pull: When True, automatically load data from the tx FIFO into the
Expand Down Expand Up @@ -147,6 +149,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
ARG_pull_in_pin_up, ARG_pull_in_pin_down,
ARG_first_set_pin, ARG_set_pin_count, ARG_initial_set_pin_state, ARG_initial_set_pin_direction,
ARG_first_sideset_pin, ARG_sideset_pin_count, ARG_initial_sideset_pin_state, ARG_initial_sideset_pin_direction,
ARG_sideset_enable,
ARG_jmp_pin,
ARG_exclusive_pin_use,
ARG_auto_pull, ARG_pull_threshold, ARG_out_shift_right,
Expand Down Expand Up @@ -178,6 +181,8 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
{ MP_QSTR_initial_sideset_pin_state, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_initial_sideset_pin_direction, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0x1f} },

{ MP_QSTR_sideset_enable, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },

{ MP_QSTR_jmp_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },

{ MP_QSTR_exclusive_pin_use, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
Expand Down Expand Up @@ -257,6 +262,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
first_in_pin, args[ARG_in_pin_count].u_int, args[ARG_pull_in_pin_up].u_int, args[ARG_pull_in_pin_down].u_int,
first_set_pin, args[ARG_set_pin_count].u_int, args[ARG_initial_set_pin_state].u_int, args[ARG_initial_set_pin_direction].u_int,
first_sideset_pin, args[ARG_sideset_pin_count].u_int, args[ARG_initial_sideset_pin_state].u_int, args[ARG_initial_sideset_pin_direction].u_int,
args[ARG_sideset_enable].u_bool,
jmp_pin,
0,
args[ARG_exclusive_pin_use].u_bool,
Expand Down
1 change: 1 addition & 0 deletions ports/raspberrypi/bindings/rp2pio/StateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
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_sideset_pin, uint8_t sideset_pin_count, uint32_t initial_sideset_pin_state, uint32_t initial_sideset_pin_direction,
bool sideset_enable,
const mcu_pin_obj_t *jmp_pin,
uint32_t wait_gpio_mask,
bool exclusive_pin_use,
Expand Down
3 changes: 2 additions & 1 deletion ports/raspberrypi/common-hal/audiobusio/I2SOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t *self,
false, 32, false, // shift out left to start with MSB
false, // Wait for txstall
false, 32, false, // in settings
false); // Not user-interruptible.
false, // Not user-interruptible.
false); // No sideset enable

self->playing = false;
audio_dma_init(&self->dma);
Expand Down
3 changes: 2 additions & 1 deletion ports/raspberrypi/common-hal/audiobusio/PDMIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t *self,
false, 32, false, // out settings
false, // Wait for txstall
false, 32, true, // in settings
false); // Not user-interruptible
false, // Not user-interruptible.
false); // No sideset enable

uint32_t actual_frequency = common_hal_rp2pio_statemachine_get_frequency(&self->state_machine);
if (actual_frequency < MIN_MIC_CLOCK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void common_hal_imagecapture_parallelimagecapture_construct(imagecapture_paralle
#else
NULL, 0, 0, 0, // sideset pins
#endif
false, // No sideset enable
NULL, // jump pin
(1 << vertical_sync->number) | (1 << horizontal_reference->number) | (1 << data_clock->number), // wait gpio pins
true, // exclusive pin use
Expand Down
3 changes: 2 additions & 1 deletion ports/raspberrypi/common-hal/neopixel_write/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t *digitalinout,
true, // Wait for txstall. If we don't, then we'll deinit too quickly.
false, 32, true, // RX setting we don't use
false, // claim pins
false); // Not user-interruptible.
false, // Not user-interruptible.
false); // No sideset enable
if (!ok) {
// Do nothing. Maybe bitbang?
return;
Expand Down
8 changes: 8 additions & 0 deletions ports/raspberrypi/common-hal/nvm/ByteArray.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ static void erase_and_write_sector(uint32_t address, uint32_t len, uint8_t *byte
// Write a whole sector to flash, buffering it first and then erasing and rewriting it
// since we can only erase a whole sector at a time.
uint8_t buffer[FLASH_SECTOR_SIZE];
#pragma GCC diagnostic push
#if __GNUC__ >= 11
// TODO: Update this to a better workaround for GCC 11 when one is provided.
// See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578#c20
#pragma GCC diagnostic ignored "-Warray-bounds"
#pragma GCC diagnostic ignored "-Wstringop-overread"
#endif
memcpy(buffer, (uint8_t *)CIRCUITPY_INTERNAL_NVM_START_ADDR, FLASH_SECTOR_SIZE);
#pragma GCC diagnostic pop
memcpy(buffer + address, bytes, len);
// disable interrupts to prevent core hang on rp2040
common_hal_mcu_disable_interrupts();
Expand Down
1 change: 1 addition & 0 deletions ports/raspberrypi/common-hal/paralleldisplay/ParallelBus.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ void common_hal_paralleldisplay_parallelbus_construct(paralleldisplay_parallelbu
NULL, 0, 0, 0, // first in pin, # in pins
NULL, 0, 0, 0, // first set pin
write, 1, 0, 1, // first sideset pin
false, // No sideset enable
NULL, // jump pin
0, // wait gpio pins
true, // exclusive pin usage
Expand Down
3 changes: 2 additions & 1 deletion ports/raspberrypi/common-hal/pulseio/PulseIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
false,
true, 32, true, // RX auto-push every 32 bits
false, // claim pins
false); // Not user-interruptible.
false, // Not user-interruptible.
false); // No sideset enable

if (!ok) {
mp_raise_RuntimeError(translate("All state machines in use"));
Expand Down
1 change: 1 addition & 0 deletions ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
3, 0, // in pulls
NULL, 0, 0, 0x1f, // set pins
NULL, 0, 0, 0x1f, // sideset pins
false, // No sideset enable
NULL, // jump pin
0, // wait gpio pins
true, // exclusive pin use
Expand Down
15 changes: 11 additions & 4 deletions ports/raspberrypi/common-hal/rp2pio/StateMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
bool wait_for_txstall,
bool auto_push, uint8_t push_threshold, bool in_shift_right,
bool claim_pins,
bool user_interruptible
bool user_interruptible,
bool sideset_enable
) {
// 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);
Expand Down Expand Up @@ -278,7 +279,11 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
sm_config_set_set_pins(&c, first_set_pin->number, set_pin_count);
}
if (first_sideset_pin != NULL) {
sm_config_set_sideset(&c, sideset_pin_count, false /* optional */, false /* pin direction */);
size_t total_sideset_bits = sideset_pin_count;
if (sideset_enable) {
total_sideset_bits += 1;
}
sm_config_set_sideset(&c, total_sideset_bits, sideset_enable, false /* pin direction */);
sm_config_set_sideset_pins(&c, first_sideset_pin->number);
}
if (jmp_pin != NULL) {
Expand Down Expand Up @@ -336,6 +341,7 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
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_sideset_pin, uint8_t sideset_pin_count, uint32_t initial_sideset_pin_state, uint32_t initial_sideset_pin_direction,
bool sideset_enable,
const mcu_pin_obj_t *jmp_pin,
uint32_t wait_gpio_mask,
bool exclusive_pin_use,
Expand Down Expand Up @@ -503,7 +509,8 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
wait_for_txstall,
auto_push, push_threshold, in_shift_right,
true /* claim pins */,
user_interruptible);
user_interruptible,
sideset_enable);
if (!ok) {
mp_raise_RuntimeError(translate("All state machines in use"));
}
Expand All @@ -513,7 +520,7 @@ void common_hal_rp2pio_statemachine_restart(rp2pio_statemachine_obj_t *self) {
common_hal_rp2pio_statemachine_stop(self);
// Reset program counter to the original offset. A JMP is 0x0000 plus
// the desired offset, so we can just use self->offset.
pio_sm_exec(self->pio, self->state_machine,self->offset);
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];
Expand Down
3 changes: 2 additions & 1 deletion ports/raspberrypi/common-hal/rp2pio/StateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
bool wait_for_txstall,
bool auto_push, uint8_t push_threshold, bool in_shift_right,
bool claim_pins,
bool interruptible);
bool interruptible,
bool sideset_enable);

uint8_t rp2pio_statemachine_program_offset(rp2pio_statemachine_obj_t *self);
void rp2pio_statemachine_set_wrap(rp2pio_statemachine_obj_t *self, uint wrap_target, uint wrap);
Expand Down

0 comments on commit c1236fb

Please sign in to comment.