Skip to content

Commit 0aa1c92

Browse files
committed
Some Serial object fixes...
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
1 parent 0bf6118 commit 0aa1c92

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

Diff for: cores/arduino/zephyrSerial.cpp

+19-4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ void arduino::ZephyrSerial::begin(unsigned long baud, uint16_t conf)
6666

6767
uart_configure(uart, &config);
6868
uart_irq_callback_user_data_set(uart, arduino::ZephyrSerial::IrqDispatch, this);
69+
k_sem_take(&rx.sem, K_FOREVER);
70+
ring_buf_reset(&rx.ringbuf);
71+
k_sem_give(&rx.sem);
72+
6973
uart_irq_rx_enable(uart);
7074
}
7175

@@ -126,26 +130,37 @@ int arduino::ZephyrSerial::available()
126130
return ret;
127131
}
128132

133+
int arduino::ZephyrSerial::availableForWrite()
134+
{
135+
int ret;
136+
137+
k_sem_take(&rx.sem, K_FOREVER);
138+
ret = ring_buf_space_get(&rx.ringbuf);
139+
k_sem_give(&rx.sem);
140+
141+
return ret;
142+
}
143+
129144
int arduino::ZephyrSerial::peek()
130145
{
131146
uint8_t data;
132147

133148
k_sem_take(&rx.sem, K_FOREVER);
134-
ring_buf_peek(&rx.ringbuf, &data, 1);
149+
uint32_t cb_ret = ring_buf_peek(&rx.ringbuf, &data, 1);
135150
k_sem_give(&rx.sem);
136151

137-
return data;
152+
return cb_ret? data : -1;
138153
}
139154

140155
int arduino::ZephyrSerial::read()
141156
{
142157
uint8_t data;
143158

144159
k_sem_take(&rx.sem, K_FOREVER);
145-
ring_buf_get(&rx.ringbuf, &data, 1);
160+
uint32_t cb_ret = ring_buf_get(&rx.ringbuf, &data, 1);
146161
k_sem_give(&rx.sem);
147162

148-
return data;
163+
return cb_ret? data : -1;
149164
}
150165

151166
size_t arduino::ZephyrSerial::write(const uint8_t *buffer, size_t size)

Diff for: cores/arduino/zephyrSerial.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ZephyrSerial : public HardwareSerial
6161
size_t write(const uint8_t data) { return write(&data, 1); }
6262
using Print::write; // pull in write(str) and write(buf, size) from Print
6363
int available();
64+
int availableForWrite();
6465
int peek();
6566
int read();
6667

0 commit comments

Comments
 (0)