Skip to content

Commit

Permalink
spi: return state on continue
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Apr 30, 2024
1 parent 3d3955a commit 55cbce9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
10 changes: 6 additions & 4 deletions src/driver/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ void spi_txn_wait(spi_bus_device_t *bus) {
}
}

void spi_txn_continue_port(spi_ports_t port) {
bool spi_txn_continue_port(spi_ports_t port) {
spi_device_t *dev = &spi_dev[port];
spi_txn_t *txn = NULL;

ATOMIC_BLOCK_ALL {
if (dev->txn_head == dev->txn_tail) {
return;
return false;
}

txn = dev->txns[(dev->txn_tail + 1) % SPI_TXN_MAX];
if (txn->status != TXN_READY) {
return;
return false;
}

if (!spi_txn_can_send(txn->bus, true)) {
return;
return false;
}

txn->status = TXN_IN_PROGRESS;
Expand All @@ -98,6 +98,8 @@ void spi_txn_continue_port(spi_ports_t port) {
spi_csn_enable(txn->bus);

spi_dma_transfer_begin(port, txn->buffer, txn->size);

return true;
}

void spi_seg_submit_ex(spi_bus_device_t *bus, const spi_txn_opts_t opts) {
Expand Down
39 changes: 20 additions & 19 deletions src/driver/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ extern const spi_port_def_t spi_port_defs[SPI_PORT_MAX];
void spi_bus_device_init(const spi_bus_device_t *bus);

void spi_txn_wait(spi_bus_device_t *bus);
void spi_txn_continue_port(spi_ports_t port);
bool spi_txn_continue_port(spi_ports_t port);

void spi_seg_submit_ex(spi_bus_device_t *bus, const spi_txn_opts_t opts);
void spi_seg_submit_wait_ex(spi_bus_device_t *bus, const spi_txn_segment_t *segs, const uint32_t count);

static inline void spi_csn_enable(spi_bus_device_t *bus) { gpio_pin_reset(bus->nss); }
static inline void spi_csn_disable(spi_bus_device_t *bus) { gpio_pin_set(bus->nss); }

static inline void spi_txn_continue(spi_bus_device_t *bus) { spi_txn_continue_port(bus->port); }
static inline bool spi_txn_continue(spi_bus_device_t *bus) { return spi_txn_continue_port(bus->port); }

static inline void spi_bus_device_reconfigure(spi_bus_device_t *bus, spi_mode_t mode, uint32_t hz) {
bus->mode = mode;
Expand All @@ -148,32 +148,33 @@ static inline bool spi_txn_ready(spi_bus_device_t *bus) {
return dev->txn_head == dev->txn_tail;
}

#define spi_seg_submit_wait(bus, segs) \
{ \
static_assert(__builtin_types_compatible_p(spi_txn_segment_t[], typeof(segs)), "spi segment not const array"); \
const uint32_t count = sizeof(segs) / sizeof(spi_txn_segment_t); \
static_assert(count < SPI_TXN_SEG_MAX, "spi segment count > max"); \
spi_seg_submit_wait_ex(bus, segs, count); \
#define spi_seg_submit_wait(_bus, _segs) \
{ \
static_assert(__builtin_types_compatible_p(spi_txn_segment_t[], typeof(_segs)), "spi segment not const array"); \
const uint32_t count = sizeof(_segs) / sizeof(spi_txn_segment_t); \
static_assert(count < SPI_TXN_SEG_MAX, "spi segment count > max"); \
spi_seg_submit_wait_ex(_bus, _segs, count); \
}
#define spi_seg_submit(bus, segs, ...) \
{ \
static_assert(__builtin_types_compatible_p(spi_txn_segment_t[], typeof(segs)), "spi segment not const array"); \
const uint32_t count = sizeof(segs) / sizeof(spi_txn_segment_t); \
static_assert(count < SPI_TXN_SEG_MAX, "spi segment count > max"); \
spi_seg_submit_ex(bus, (spi_txn_opts_t){.segs = segs, .seg_count = count, __VA_ARGS__}); \
#define spi_seg_submit(_bus, _segs, ...) \
{ \
static_assert(__builtin_types_compatible_p(spi_txn_segment_t[], typeof(_segs)), "spi segment not const array"); \
const uint32_t count = sizeof(_segs) / sizeof(spi_txn_segment_t); \
static_assert(count < SPI_TXN_SEG_MAX, "spi segment count > max"); \
spi_seg_submit_ex(_bus, (spi_txn_opts_t){.segs = _segs, .seg_count = count, __VA_ARGS__}); \
}
#define spi_seg_submit_continue(bus, segs, ...) \
spi_seg_submit(bus, segs, __VA_ARGS__); \
spi_txn_continue(bus);
#define spi_seg_submit_continue(_bus, _segs, ...) ({ \
spi_seg_submit(_bus, _segs, __VA_ARGS__); \
spi_txn_continue(_bus); \
})

static inline void spi_txn_set_done(void *arg) { *((bool *)arg) = true; }
#define spi_seg_submit_check(bus, segs, ...) \
#define spi_seg_submit_check(_bus, _segs, ...) \
({ \
static volatile bool __did_submit__ = false; \
static volatile bool __is_done__ = false; \
if (!__did_submit__) { \
__VA_ARGS__; \
spi_seg_submit_continue(bus, segs, \
spi_seg_submit_continue(_bus, _segs, \
.done_fn = spi_txn_set_done, \
.done_fn_arg = (void *)&__is_done__); \
__did_submit__ = true; \
Expand Down

0 comments on commit 55cbce9

Please sign in to comment.