Skip to content

Commit

Permalink
spi: drop delayed TX
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Nov 30, 2024
1 parent e2dd09b commit 0d11333
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 68 deletions.
79 changes: 33 additions & 46 deletions src/driver/rx/sx128x.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
Expand All @@ -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),
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 0 additions & 18 deletions src/driver/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions src/driver/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ typedef struct {
typedef enum {
TXN_CONST,
TXN_BUFFER,
TXN_DELAY,
} spi_txn_segment_type_t;

typedef struct {
Expand All @@ -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;

Expand All @@ -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;

Expand Down

0 comments on commit 0d11333

Please sign in to comment.