summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicko van Someren <nicko@nicko.org>2024-01-06 16:33:51 -0700
committerDamien George <damien@micropython.org>2024-01-07 18:27:01 +1100
commit1da45e887af1b5e6e608a13e292aa343f5b9f976 (patch)
treedeabce01b19235da2d4f180c811be2eb77e43f22
parentf8cabe82f71add182702f32e1b1444a44c83eb74 (diff)
rp2: Provide direct memory access to PIO and SPI FIFOs via proxy arrays.
Signed-off-by: Nicko van Someren <nicko@nicko.org>
-rw-r--r--ports/rp2/machine_spi.c13
-rw-r--r--ports/rp2/rp2_pio.c18
2 files changed, 31 insertions, 0 deletions
diff --git a/ports/rp2/machine_spi.c b/ports/rp2/machine_spi.c
index fb8e9f56c..a4f4243b7 100644
--- a/ports/rp2/machine_spi.c
+++ b/ports/rp2/machine_spi.c
@@ -302,6 +302,18 @@ STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8
}
}
+// Buffer protocol implementation for SPI.
+// The buffer represents the SPI data FIFO.
+STATIC mp_int_t machine_spi_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
+ machine_spi_obj_t *self = MP_OBJ_TO_PTR(o_in);
+
+ bufinfo->len = 4;
+ bufinfo->typecode = 'I';
+ bufinfo->buf = (void *)&spi_get_hw(self->spi_inst)->dr;
+
+ return 0;
+}
+
STATIC const mp_machine_spi_p_t machine_spi_p = {
.init = machine_spi_init,
.transfer = machine_spi_transfer,
@@ -314,6 +326,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
make_new, machine_spi_make_new,
print, machine_spi_print,
protocol, &machine_spi_p,
+ buffer, machine_spi_get_buffer,
locals_dict, &mp_machine_spi_locals_dict
);
diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c
index 3a0ab844e..42e61684b 100644
--- a/ports/rp2/rp2_pio.c
+++ b/ports/rp2/rp2_pio.c
@@ -809,6 +809,23 @@ STATIC mp_obj_t rp2_state_machine_tx_fifo(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(rp2_state_machine_tx_fifo_obj, rp2_state_machine_tx_fifo);
+// Buffer protocol implementation for StateMachine.
+// The buffer represents one of the FIFO ports of the state machine. Note that a different
+// pointer is returned depending on if this is for reading or writing.
+STATIC mp_int_t rp2_state_machine_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
+ rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(o_in);
+
+ bufinfo->len = 4;
+ bufinfo->typecode = 'I';
+
+ if (flags & MP_BUFFER_WRITE) {
+ bufinfo->buf = (void *)&self->pio->txf[self->sm];
+ } else {
+ bufinfo->buf = (void *)&self->pio->rxf[self->sm];
+ }
+ return 0;
+}
+
// StateMachine.irq(handler=None, trigger=0|1, hard=False)
STATIC mp_obj_t rp2_state_machine_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_handler, ARG_trigger, ARG_hard };
@@ -884,6 +901,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
MP_TYPE_FLAG_NONE,
make_new, rp2_state_machine_make_new,
print, rp2_state_machine_print,
+ buffer, rp2_state_machine_get_buffer,
locals_dict, &rp2_state_machine_locals_dict
);