From 0d11333175b56cd5429336908fe89d57763feee7 Mon Sep 17 00:00:00 2001 From: bkleiner Date: Sat, 30 Nov 2024 21:55:42 +0100 Subject: [PATCH] spi: drop delayed TX --- src/driver/rx/sx128x.c | 79 ++++++++++++++++++------------------------ src/driver/spi.c | 18 ---------- src/driver/spi.h | 4 --- 3 files changed, 33 insertions(+), 68 deletions(-) diff --git a/src/driver/rx/sx128x.c b/src/driver/rx/sx128x.c index fedaca4dc..7efd04661 100644 --- a/src/driver/rx/sx128x.c +++ b/src/driver/rx/sx128x.c @@ -23,6 +23,7 @@ volatile uint16_t irq_status = 0; static uint32_t busy_timeout = 1000; static uint8_t payload_len = 8; +static uint8_t buffer_status[2] = {0, 0}; extern volatile uint32_t packet_time_us; extern volatile uint8_t packet_status[2]; @@ -107,31 +108,29 @@ static void sx128x_set_dio0_active(void *arg) { dio0_active = 1; } +static void sx128x_read_buffer(void *arg) { + { + const spi_txn_segment_t segs[] = { + spi_make_seg_const(SX1280_RADIO_READ_BUFFER, buffer_status[1], 0x00), + spi_make_seg_buffer((uint8_t *)rx_spi_packet, NULL, payload_len), + }; + spi_seg_submit(&bus, segs); + } + { + const spi_txn_segment_t segs[] = { + read_command_txn(SX1280_RADIO_GET_PACKETSTATUS, (uint8_t *)packet_status, 2), + }; + spi_seg_submit(&bus, segs, .done_fn = sx128x_set_dio0_active); + } +} + static void sx128x_handle_irq_status(void *arg) { const uint16_t irq = ((irq_status & 0xFF) << 8 | ((irq_status >> 8) & 0xFF)); if ((irq & SX1280_IRQ_RX_DONE)) { - static uint8_t buffer_status[2] = {0, 0}; - { - static const spi_txn_segment_t segs[] = { - read_command_txn(SX1280_RADIO_GET_RXBUFFERSTATUS, buffer_status, 2), - }; - spi_seg_submit(&bus, segs); - } - { - const spi_txn_segment_t segs[] = { - spi_make_seg_const(SX1280_RADIO_READ_BUFFER), - spi_make_seg_delay(NULL, buffer_status + 1, 1), - spi_make_seg_const(0x00), - spi_make_seg_buffer((uint8_t *)rx_spi_packet, NULL, payload_len), - }; - spi_seg_submit(&bus, segs); - } - { - static const spi_txn_segment_t segs[] = { - read_command_txn(SX1280_RADIO_GET_PACKETSTATUS, (uint8_t *)packet_status, 2), - }; - spi_seg_submit(&bus, segs, .done_fn = sx128x_set_dio0_active); - } + const spi_txn_segment_t segs[] = { + read_command_txn(SX1280_RADIO_GET_RXBUFFERSTATUS, buffer_status, 2), + }; + spi_seg_submit(&bus, segs, .done_fn = sx128x_read_buffer); } else if ((irq & SX1280_IRQ_TX_DONE)) { sx128x_set_mode_async(SX1280_MODE_RX); } @@ -156,17 +155,17 @@ void sx128x_wait() { } void sx128x_handle_dio0_exti(bool level) { - if (!level) { + if (!level) return; - } + { - static const spi_txn_segment_t segs[] = { + const spi_txn_segment_t segs[] = { read_command_txn(SX1280_RADIO_GET_IRQSTATUS, (uint8_t *)&irq_status, 2), }; spi_seg_submit(&bus, segs); } { - static const spi_txn_segment_t segs[] = { + const spi_txn_segment_t segs[] = { spi_make_seg_const( SX1280_RADIO_CLR_IRQSTATUS, (uint8_t)(((uint16_t)SX1280_IRQ_RADIO_ALL >> 8) & 0x00FF), @@ -199,15 +198,11 @@ uint16_t sx128x_read_dio0() { } void sx128x_read_register_burst(const uint16_t reg, uint8_t *data, const uint8_t size) { - const uint8_t buf[4] = { - (SX1280_RADIO_READ_REGISTER), - ((reg & 0xFF00) >> 8), - (reg & 0x00FF), - 0x00, - }; - const spi_txn_segment_t segs[] = { - spi_make_seg_buffer(NULL, buf, 4), + spi_make_seg_const((SX1280_RADIO_READ_REGISTER), + ((reg & 0xFF00) >> 8), + (reg & 0x00FF), + 0x00), spi_make_seg_buffer(data, NULL, size), }; spi_seg_submit(&bus, segs); @@ -221,14 +216,10 @@ uint8_t sx128x_read_register(const uint16_t reg) { } void sx128x_write_register_burst(const uint16_t reg, const uint8_t *data, const uint8_t size) { - uint8_t buf[3] = { - (uint8_t)SX1280_RADIO_WRITE_REGISTER, - ((reg & 0xFF00) >> 8), - (reg & 0x00FF), - }; - const spi_txn_segment_t segs[] = { - spi_make_seg_buffer(NULL, buf, 3), + spi_make_seg_const((uint8_t)SX1280_RADIO_WRITE_REGISTER, + ((reg & 0xFF00) >> 8), + (reg & 0x00FF)), spi_make_seg_buffer(NULL, data, size), }; spi_seg_submit(&bus, segs); @@ -258,12 +249,8 @@ void sx128x_write_command(const sx128x_commands_t cmd, const uint8_t val) { } void sx128x_write_tx_buffer(const uint8_t offset, const volatile uint8_t *data, const uint8_t size) { - const uint8_t buf[2] = { - (uint8_t)SX1280_RADIO_WRITE_BUFFER, - offset, - }; const spi_txn_segment_t segs[] = { - spi_make_seg_buffer(NULL, buf, 2), + spi_make_seg_const(SX1280_RADIO_WRITE_BUFFER, offset), spi_make_seg_buffer(NULL, (uint8_t *)data, size), }; spi_seg_submit(&bus, segs); diff --git a/src/driver/spi.c b/src/driver/spi.c index 64914525b..655cae506 100644 --- a/src/driver/spi.c +++ b/src/driver/spi.c @@ -78,18 +78,6 @@ bool spi_txn_continue_port(spi_ports_t port) { } txn->status = TXN_IN_PROGRESS; - - if (txn->flags & TXN_DELAYED_TX) { - uint32_t txn_size = 0; - for (uint32_t i = 0; i < txn->segment_count; ++i) { - spi_txn_segment_t *seg = &txn->segments[i]; - if (seg->tx_data) { - memcpy((uint8_t *)txn->buffer + txn_size, seg->tx_data, seg->size); - } - txn_size += seg->size; - } - } - dev->dma_done = false; } @@ -134,12 +122,6 @@ void spi_seg_submit_ex(spi_bus_device_t *bus, const spi_txn_opts_t opts) { memset(txn->buffer + txn->size, 0xFF, seg->size); } break; - - case TXN_DELAY: - txn_seg->tx_data = seg->tx_data; - txn_seg->rx_data = seg->rx_data; - txn->flags |= TXN_DELAYED_TX; - break; } if (txn_seg->rx_data) { diff --git a/src/driver/spi.h b/src/driver/spi.h index e93a8f023..b35ea6a79 100644 --- a/src/driver/spi.h +++ b/src/driver/spi.h @@ -28,7 +28,6 @@ typedef struct { typedef enum { TXN_CONST, TXN_BUFFER, - TXN_DELAY, } spi_txn_segment_type_t; typedef struct { @@ -49,8 +48,6 @@ typedef struct { (spi_txn_segment_t) { .type = TXN_CONST, .bytes = {_bytes}, .size = sizeof((uint8_t[]){_bytes}) } #define spi_make_seg_buffer(_rx_data, _tx_data, _size) \ (spi_txn_segment_t) { .type = TXN_BUFFER, .rx_data = (_rx_data), .tx_data = (_tx_data), .size = (_size) } -#define spi_make_seg_delay(_rx_data, _tx_data, _size) \ - (spi_txn_segment_t) { .type = TXN_DELAY, .rx_data = (_rx_data), .tx_data = (_tx_data), .size = (_size) } struct spi_bus_device; @@ -62,7 +59,6 @@ typedef enum { } spi_txn_status_t; typedef enum { - TXN_DELAYED_TX = (1 << 0), TXN_DELAYED_RX = (1 << 1), } spi_txn_flags_t;