Skip to content

Commit 6508356

Browse files
committed
rp2pio: Fix fifo_type and mov_status_type handling. add rxfifo property
1 parent 5a12687 commit 6508356

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

ports/raspberrypi/bindings/rp2pio/StateMachine.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "py/objproperty.h"
2222
#include "py/runtime.h"
2323

24-
2524
//| FifoType = Literal["auto", "txrx", "tx", "rx", "txput", "txget", "putget"]
2625
//| FifoType_piov0 = Literal["auto", "txrx", "tx", "rx"]
2726
//| MovStatusType = Literal["txfifo", "rxfifo", "irq"]
@@ -233,8 +232,8 @@ static mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
233232

234233
{ MP_QSTR_offset, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PIO_ANY_OFFSET} },
235234

236-
{ MP_QSTR_fifo_type, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_obj = MP_ROM_QSTR(MP_QSTR_rxtx) } },
237-
{ MP_QSTR_mov_status_type, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_obj = MP_ROM_QSTR(MP_QSTR_txfifo) } },
235+
{ MP_QSTR_fifo_type, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_ROM_QSTR(MP_QSTR_rxtx) } },
236+
{ MP_QSTR_mov_status_type, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_ROM_QSTR(MP_QSTR_txfifo) } },
238237
{ MP_QSTR_mov_status_n, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
239238
};
240239
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -300,9 +299,11 @@ static mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
300299
};
301300
const int fifo_values[] = { PIO_FIFO_JOIN_AUTO, PIO_FIFO_JOIN_NONE, PIO_FIFO_JOIN_TX, PIO_FIFO_JOIN_RX,
302301
#if PICO_PIO_VERSION > 0
303-
PIO_FIFO_JOIN_TXPUT, PIO_FIFO_JOIN_PUTGET
302+
PIO_FIFO_JOIN_TXPUT, PIO_FIFO_JOIN_TXGET, PIO_FIFO_JOIN_PUTGET
304303
#endif
305304
};
305+
MP_STATIC_ASSERT(MP_ARRAY_SIZE(fifo_alternatives) == MP_ARRAY_SIZE(fifo_values));
306+
306307
int fifo_type = one_of(MP_QSTR_fifo_type, args[ARG_fifo_type].u_obj, MP_ARRAY_SIZE(fifo_alternatives), fifo_alternatives, fifo_values);
307308

308309
const qstr_short_t mov_status_alternatives[] = { MP_QSTR_txfifo, MP_QSTR_rxfifo,
@@ -315,6 +316,7 @@ static mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
315316
STATUS_IRQ_SET
316317
#endif
317318
};
319+
MP_STATIC_ASSERT(MP_ARRAY_SIZE(mov_status_alternatives) == MP_ARRAY_SIZE(mov_status_values));
318320
int mov_status_type = one_of(MP_QSTR_mov_status_type, args[ARG_mov_status_type].u_obj, MP_ARRAY_SIZE(mov_status_alternatives), mov_status_alternatives, mov_status_values);
319321

320322
common_hal_rp2pio_statemachine_construct(self,
@@ -907,6 +909,32 @@ MP_DEFINE_CONST_FUN_OBJ_1(rp2pio_statemachine_get_pc_obj, rp2pio_statemachine_ob
907909
MP_PROPERTY_GETTER(rp2pio_statemachine_pc_obj,
908910
(mp_obj_t)&rp2pio_statemachine_get_pc_obj);
909911

912+
//| rxfifo: AddressRange
913+
//| """Accecss the state machine's rxfifo directly
914+
//|
915+
//| If the state machine's fifo mode is ``txput`` then accessing this object
916+
//| reads values stored by the ``mov rxfifo[], isr`` PIO instruction, and the
917+
//| result of modifying it is undefined.
918+
//|
919+
//| If the state machine's fifo mode is ``txget`` then modifying this object
920+
//| writes values accessed by the ``mov osr, rxfifo[]`` PIO instruction, and
921+
//| the result of accessing it is undefined..
922+
//|
923+
//| If this state machine's mode is something else, then the property's value is `None`.
924+
//|
925+
//| Note: Since the ``txput`` and ``txget`` fifo mode does not exist on RP2040, this property will always be `None`."""
926+
//|
927+
928+
static mp_obj_t rp2pio_statemachine_obj_get_rxfifo(mp_obj_t self_in) {
929+
rp2pio_statemachine_obj_t *self = MP_OBJ_TO_PTR(self_in);
930+
check_for_deinit(self);
931+
return common_hal_rp2pio_statemachine_get_rxfifo(self);
932+
}
933+
MP_DEFINE_CONST_FUN_OBJ_1(rp2pio_statemachine_get_rxfifo_obj, rp2pio_statemachine_obj_get_rxfifo);
934+
935+
MP_PROPERTY_GETTER(rp2pio_statemachine_rxfifo_obj,
936+
(mp_obj_t)&rp2pio_statemachine_get_rxfifo_obj);
937+
910938

911939
static const mp_rom_map_elem_t rp2pio_statemachine_locals_dict_table[] = {
912940
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&rp2pio_statemachine_deinit_obj) },
@@ -934,6 +962,8 @@ static const mp_rom_map_elem_t rp2pio_statemachine_locals_dict_table[] = {
934962

935963
{ MP_ROM_QSTR(MP_QSTR_offset), MP_ROM_PTR(&rp2pio_statemachine_offset_obj) },
936964
{ MP_ROM_QSTR(MP_QSTR_pc), MP_ROM_PTR(&rp2pio_statemachine_pc_obj) },
965+
966+
{ MP_ROM_QSTR(MP_QSTR_rxfifo), MP_ROM_PTR(&rp2pio_statemachine_rxfifo_obj) },
937967
};
938968
static MP_DEFINE_CONST_DICT(rp2pio_statemachine_locals_dict, rp2pio_statemachine_locals_dict_table);
939969

ports/raspberrypi/bindings/rp2pio/StateMachine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,5 @@ int common_hal_rp2pio_statemachine_get_offset(rp2pio_statemachine_obj_t *self);
7373
int common_hal_rp2pio_statemachine_get_pc(rp2pio_statemachine_obj_t *self);
7474

7575
void common_hal_rp2pio_statemachine_set_interrupt_handler(rp2pio_statemachine_obj_t *self, void (*handler)(void *), void *arg, int mask);
76+
77+
mp_obj_t common_hal_rp2pio_statemachine_get_rxfifo(rp2pio_statemachine_obj_t *self);

ports/raspberrypi/common-hal/rp2pio/StateMachine.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "shared-bindings/digitalio/Pull.h"
1313
#include "shared-bindings/microcontroller/__init__.h"
1414
#include "shared-bindings/microcontroller/Pin.h"
15+
#include "shared-bindings/memorymap/AddressRange.h"
1516

1617
#include "src/rp2040/hardware_regs/include/hardware/platform_defs.h"
1718
#include "src/rp2_common/hardware_clocks/include/hardware/clocks.h"
@@ -365,6 +366,15 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
365366
#endif
366367
: 4;
367368

369+
#if PICO_PIO_VERSION > 0
370+
if (fifo_type == PIO_FIFO_JOIN_TXPUT || fifo_type == PIO_FIFO_JOIN_TXGET) {
371+
self->rxfifo_obj.base.type = &memorymap_addressrange_type;
372+
common_hal_memorymap_addressrange_construct(&self->rxfifo_obj, (uint8_t *)self->pio->rxf_putget[self->state_machine], 4 * sizeof(uint32_t));
373+
} else {
374+
self->rxfifo_obj.base.type = NULL;
375+
}
376+
#endif
377+
368378
if (rx_fifo) {
369379
self->rx_dreq = pio_get_dreq(self->pio, self->state_machine, false);
370380
}
@@ -631,8 +641,8 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
631641
user_interruptible,
632642
sideset_enable,
633643
wrap_target, wrap, offset,
634-
PIO_FIFO_TYPE_DEFAULT,
635-
PIO_MOV_STATUS_DEFAULT, PIO_MOV_N_DEFAULT);
644+
fifo_type,
645+
mov_status_type, mov_status_n);
636646
if (!ok) {
637647
mp_raise_RuntimeError(MP_ERROR_TEXT("All state machines in use"));
638648
}
@@ -1124,6 +1134,16 @@ int common_hal_rp2pio_statemachine_get_pc(rp2pio_statemachine_obj_t *self) {
11241134
return pio_sm_get_pc(pio, sm);
11251135
}
11261136

1137+
mp_obj_t common_hal_rp2pio_statemachine_get_rxfifo(rp2pio_statemachine_obj_t *self) {
1138+
#if PICO_PIO_VERSION > 0
1139+
if (self->rxfifo_obj.base.type) {
1140+
return MP_OBJ_FROM_PTR(&self->rxfifo_obj);
1141+
}
1142+
#endif
1143+
return mp_const_none;
1144+
}
1145+
1146+
11271147
// Use a compile-time constant for MP_REGISTER_POINTER so the preprocessor will
11281148
// not split the expansion across multiple lines.
11291149
MP_REGISTER_ROOT_POINTER(mp_obj_t background_pio[enum_NUM_DMA_CHANNELS]);

ports/raspberrypi/common-hal/rp2pio/StateMachine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "py/obj.h"
1010

1111
#include "common-hal/microcontroller/Pin.h"
12+
#include "common-hal/memorymap/AddressRange.h"
1213
#include "src/rp2_common/hardware_pio/include/hardware/pio.h"
1314

1415
enum { PIO_ANY_OFFSET = -1 };
@@ -50,6 +51,7 @@ typedef struct {
5051
sm_buf_info current, once, loop;
5152
int background_stride_in_bytes;
5253
bool dma_completed, byteswap;
54+
memorymap_addressrange_obj_t rxfifo_obj;
5355
} rp2pio_statemachine_obj_t;
5456

5557
void reset_rp2pio_statemachine(void);

0 commit comments

Comments
 (0)