Skip to content

Commit

Permalink
fixup! feat(split): Add full-duplex wired split support
Browse files Browse the repository at this point in the history
  • Loading branch information
petejohanson committed Jan 17, 2025
1 parent 95c58b3 commit ffd742c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 89 deletions.
44 changes: 2 additions & 42 deletions app/src/split/wired/central.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,56 +80,16 @@ static void publish_events_work(struct k_work *work);

#if IS_ENABLED(CONFIG_ZMK_SPLIT_WIRED_UART_MODE_DEFAULT_INTERRUPT)

static void recv_data(const struct device *dev, const struct wired_bus *bus) {
uint32_t last_read = 0, len = 0;
struct ring_buf *buf = &bus->state->rx_buf;
do {
uint8_t *buffer;
len = ring_buf_put_claim(buf, &buffer, buf->size);
if (len > 0) {
last_read = uart_fifo_read(dev, buffer, len);

ring_buf_put_finish(buf, last_read);
} else {
LOG_ERR("Dropping incoming RPC byte, insufficient room in the RX buffer. Bump "
"CONFIG_ZMK_STUDIO_RPC_RX_BUF_SIZE.");
uint8_t dummy;
last_read = uart_fifo_read(dev, &dummy, 1);
}
} while (last_read && last_read == len);

k_work_submit(&bus->state->event_publish_work);
}

static void send_data(const struct device *dev, const struct wired_bus *bus) {
struct ring_buf *tx_buf = &bus->state->tx_buf;
uint32_t len;
while ((len = ring_buf_size_get(tx_buf)) > 0) {
uint8_t *buf;
uint32_t claim_len = ring_buf_get_claim(tx_buf, &buf, tx_buf->size);

if (claim_len == 0) {
continue;
}

int sent = uart_fifo_fill(dev, buf, claim_len);

ring_buf_get_finish(tx_buf, MAX(sent, 0));
}

uart_irq_tx_disable(dev);
}

static void serial_cb(const struct device *dev, void *user_data) {
const struct wired_bus *bus = (const struct wired_bus *)user_data;

while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
if (uart_irq_rx_ready(dev)) {
recv_data(dev, bus);
zmk_split_wired_fifo_read(dev, &bus->state->rx_buf, &bus->state->event_publish_work);
}

if (uart_irq_tx_ready(dev)) {
send_data(dev, bus);
zmk_split_wired_fifo_fill(dev, &bus->state->tx_buf);
}
}
}
Expand Down
75 changes: 29 additions & 46 deletions app/src/split/wired/peripheral.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,59 +79,42 @@ static void publish_commands_work(struct k_work *work);
K_WORK_DEFINE(publish_commands, publish_commands_work);

#if IS_ENABLED(CONFIG_ZMK_SPLIT_WIRED_UART_MODE_DEFAULT_INTERRUPT)
static void serial_cb(const struct device *dev, void *user_data) {
while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
if (uart_irq_rx_ready(dev)) {
LOG_DBG("rx is ready");
/* read until FIFO empty */
uint32_t last_read = 0, len = 0;
struct ring_buf *buf = &chosen_rx_buf;
do {
uint8_t *buffer;
len = ring_buf_put_claim(buf, &buffer, buf->size);
if (len > 0) {
last_read = uart_fifo_read(dev, buffer, len);

LOG_DBG("Read %d bytes from the uART", last_read);

ring_buf_put_finish(buf, last_read);
} else {
LOG_ERR("Dropping incoming RPC byte, insufficient room in the RX buffer. Bump "
"CONFIG_ZMK_STUDIO_RPC_RX_BUF_SIZE.");
uint8_t dummy;
last_read = uart_fifo_read(dev, &dummy, 1);
}
} while (last_read && last_read == len);

k_work_submit(&publish_commands);
}

if (uart_irq_tx_ready(dev)) {
LOG_DBG("tx is ready");
struct ring_buf *tx_buf = &chosen_tx_buf;
uint32_t len;
while ((len = ring_buf_size_get(tx_buf)) > 0) {
uint8_t *buf;
uint32_t claim_len = ring_buf_get_claim(tx_buf, &buf, tx_buf->size);
// static void fifo_fill(const struct device *dev) {
// struct ring_buf *tx_buf = &chosen_tx_buf;
// uint32_t len;
// while ((len = ring_buf_size_get(tx_buf)) > 0) {
// uint8_t *buf;
// uint32_t claim_len = ring_buf_get_claim(tx_buf, &buf, tx_buf->size);

// if (claim_len <= 0) {
// break;
// }

if (claim_len <= 0) {
break;
}
// int sent = uart_fifo_fill(dev, buf, claim_len);

int sent = uart_fifo_fill(dev, buf, claim_len);
// LOG_DBG("Sent %d to the UART", sent);

LOG_DBG("Sent %d to the UART", sent);
// ring_buf_get_finish(tx_buf, MAX(sent, 0));

ring_buf_get_finish(tx_buf, MAX(sent, 0));
// if (sent <= 0) {
// break;
// }
// }

if (sent <= 0) {
break;
}
}
// if (ring_buf_size_get(tx_buf) == 0) {
// uart_irq_tx_disable(dev);
// }
// }

if (ring_buf_size_get(tx_buf) == 0) {
uart_irq_tx_disable(dev);
}
static void serial_cb(const struct device *dev, void *user_data) {
while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
if (uart_irq_rx_ready(dev)) {
zmk_split_wired_fifo_read(dev, &chosen_rx_buf, &publish_commands);
}

if (uart_irq_tx_ready(dev)) {
zmk_split_wired_fifo_fill(dev, &chosen_tx_buf);
}
}
}
Expand Down
53 changes: 52 additions & 1 deletion app/src/split/wired/wired.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,55 @@ void zmk_split_wired_poll_in(struct ring_buf *rx_buf, const struct device *uart,
}
}

#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_WIRED_UART_MODE_DEFAULT_POLLING)
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_WIRED_UART_MODE_DEFAULT_POLLING)

#if IS_ENABLED(CONFIG_ZMK_SPLIT_WIRED_UART_MODE_DEFAULT_INTERRUPT)

void zmk_split_wired_fifo_read(const struct device *dev, struct ring_buf *buf,
struct k_work *process_work) {
uint32_t last_read = 0, len = 0;
do {
uint8_t *buffer;
len = ring_buf_put_claim(buf, &buffer, buf->size);
if (len > 0) {
last_read = uart_fifo_read(dev, buffer, len);

ring_buf_put_finish(buf, last_read);
} else {
LOG_ERR("Dropping incoming RPC byte, insufficient room in the RX buffer. Bump "
"CONFIG_ZMK_STUDIO_RPC_RX_BUF_SIZE.");
uint8_t dummy;
last_read = uart_fifo_read(dev, &dummy, 1);
}
} while (last_read && last_read == len);

k_work_submit(process_work);
}

void zmk_split_wired_fifo_fill(const struct device *dev, struct ring_buf *tx_buf) {
uint32_t len;
while ((len = ring_buf_size_get(tx_buf)) > 0) {
uint8_t *buf;
uint32_t claim_len = ring_buf_get_claim(tx_buf, &buf, tx_buf->size);

if (claim_len <= 0) {
break;
}

int sent = uart_fifo_fill(dev, buf, claim_len);

LOG_DBG("Sent %d to the UART", sent);

ring_buf_get_finish(tx_buf, MAX(sent, 0));

if (sent <= 0) {
break;
}
}

if (ring_buf_size_get(tx_buf) == 0) {
uart_irq_tx_disable(dev);
}
}

#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_WIRED_UART_MODE_DEFAULT_INTERRUPT)
8 changes: 8 additions & 0 deletions app/src/split/wired/wired.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ void zmk_split_wired_poll_out(struct ring_buf *tx_buf, const struct device *uart
void zmk_split_wired_poll_in(struct ring_buf *rx_buf, const struct device *uart,
struct k_work *process_data_work, size_t envelope_size);

#endif

#if IS_ENABLED(CONFIG_ZMK_SPLIT_WIRED_UART_MODE_DEFAULT_INTERRUPT)

void zmk_split_wired_fifo_read(const struct device *dev, struct ring_buf *buf,
struct k_work *process_work);
void zmk_split_wired_fifo_fill(const struct device *dev, struct ring_buf *tx_buf);

#endif

0 comments on commit ffd742c

Please sign in to comment.