Skip to content

Commit 7d8892e

Browse files
committed
ports/rp2: Provide direct memory access to PIO and SPI FIFOs via buffer protocol.
Signed-off-by: Nicko van Someren <nicko@nicko.org>
1 parent 9feb068 commit 7d8892e

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

ports/rp2/machine_spi.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,18 @@ STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8
302302
}
303303
}
304304

305+
// Buffer protocol implementation for StateMachine
306+
// The buffer represents the SPI data FIFO.
307+
STATIC mp_int_t machine_spi_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
308+
machine_spi_obj_t *self = MP_OBJ_TO_PTR(o_in);
309+
310+
bufinfo->len = 1;
311+
bufinfo->typecode = 'I';
312+
bufinfo->buf = (void *)&spi_get_hw(self->spi_inst)->dr;
313+
314+
return 0;
315+
}
316+
305317
STATIC const mp_machine_spi_p_t machine_spi_p = {
306318
.init = machine_spi_init,
307319
.transfer = machine_spi_transfer,
@@ -314,6 +326,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
314326
make_new, machine_spi_make_new,
315327
print, machine_spi_print,
316328
protocol, &machine_spi_p,
329+
buffer, machine_spi_get_buffer,
317330
locals_dict, &mp_machine_spi_locals_dict
318331
);
319332

ports/rp2/rp2_dma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ STATIC const uint32_t rp2_dma_ctrl_field_count = MP_ARRAY_SIZE(rp2_dma_ctrl_fiel
8686
STATIC uint32_t rp2_dma_register_value_from_obj(mp_obj_t o, int reg_type) {
8787
if (reg_type == REG_TYPE_ADDR_READ || reg_type == REG_TYPE_ADDR_WRITE) {
8888
mp_buffer_info_t buf_info;
89-
mp_uint_t flags = MP_BUFFER_READ;
89+
mp_uint_t flags = (reg_type == REG_TYPE_ADDR_READ) ? MP_BUFFER_READ : MP_BUFFER_WRITE;
9090
if (mp_get_buffer(o, &buf_info, flags)) {
9191
return (uint32_t)buf_info.buf;
9292
}

ports/rp2/rp2_pio.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,23 @@ STATIC mp_obj_t rp2_state_machine_tx_fifo(mp_obj_t self_in) {
809809
}
810810
STATIC MP_DEFINE_CONST_FUN_OBJ_1(rp2_state_machine_tx_fifo_obj, rp2_state_machine_tx_fifo);
811811

812+
// Buffer protocol implementation for StateMachine
813+
// The buffer represents one of the FIFO ports of the state machine. Note that a different
814+
// pointer is returned depending on if this is for reading or writing.
815+
STATIC mp_int_t rp2_state_machine_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
816+
rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(o_in);
817+
818+
bufinfo->len = 1;
819+
bufinfo->typecode = 'I';
820+
821+
if (flags & MP_BUFFER_WRITE) {
822+
bufinfo->buf = (void *)&self->pio->txf[self->sm];
823+
} else {
824+
bufinfo->buf = (void *)&self->pio->rxf[self->sm];
825+
}
826+
return 0;
827+
}
828+
812829
// StateMachine.irq(handler=None, trigger=0|1, hard=False)
813830
STATIC mp_obj_t rp2_state_machine_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
814831
enum { ARG_handler, ARG_trigger, ARG_hard };
@@ -884,6 +901,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
884901
MP_TYPE_FLAG_NONE,
885902
make_new, rp2_state_machine_make_new,
886903
print, rp2_state_machine_print,
904+
buffer, rp2_state_machine_get_buffer,
887905
locals_dict, &rp2_state_machine_locals_dict
888906
);
889907

0 commit comments

Comments
 (0)