Skip to content

Commit

Permalink
Some Serial object fixes...
Browse files Browse the repository at this point in the history
added availableForWrite - overwrite Print virtual - get it from ring buffer
fix peek and Read, that if 0 bytes read from ring buffer return -1

begin method appeared to have initial garbage in it.  so have it call ring_buf_reset on the rx ring buffer
  • Loading branch information
KurtE committed Nov 29, 2024
1 parent 0bf6118 commit 0aa1c92
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
23 changes: 19 additions & 4 deletions cores/arduino/zephyrSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ void arduino::ZephyrSerial::begin(unsigned long baud, uint16_t conf)

uart_configure(uart, &config);
uart_irq_callback_user_data_set(uart, arduino::ZephyrSerial::IrqDispatch, this);
k_sem_take(&rx.sem, K_FOREVER);
ring_buf_reset(&rx.ringbuf);
k_sem_give(&rx.sem);

uart_irq_rx_enable(uart);
}

Expand Down Expand Up @@ -126,26 +130,37 @@ int arduino::ZephyrSerial::available()
return ret;
}

int arduino::ZephyrSerial::availableForWrite()
{
int ret;

k_sem_take(&rx.sem, K_FOREVER);
ret = ring_buf_space_get(&rx.ringbuf);
k_sem_give(&rx.sem);

return ret;
}

int arduino::ZephyrSerial::peek()
{
uint8_t data;

k_sem_take(&rx.sem, K_FOREVER);
ring_buf_peek(&rx.ringbuf, &data, 1);
uint32_t cb_ret = ring_buf_peek(&rx.ringbuf, &data, 1);
k_sem_give(&rx.sem);

return data;
return cb_ret? data : -1;
}

int arduino::ZephyrSerial::read()
{
uint8_t data;

k_sem_take(&rx.sem, K_FOREVER);
ring_buf_get(&rx.ringbuf, &data, 1);
uint32_t cb_ret = ring_buf_get(&rx.ringbuf, &data, 1);
k_sem_give(&rx.sem);

return data;
return cb_ret? data : -1;
}

size_t arduino::ZephyrSerial::write(const uint8_t *buffer, size_t size)
Expand Down
1 change: 1 addition & 0 deletions cores/arduino/zephyrSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ZephyrSerial : public HardwareSerial
size_t write(const uint8_t data) { return write(&data, 1); }
using Print::write; // pull in write(str) and write(buf, size) from Print
int available();
int availableForWrite();
int peek();
int read();

Expand Down

0 comments on commit 0aa1c92

Please sign in to comment.