Skip to content

Commit

Permalink
Add experimental support for pre-breaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Jan 25, 2025
1 parent a065f50 commit 385be24
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
24 changes: 12 additions & 12 deletions lib/uart.toit
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ class Port extends Object with io.InMixin implements reader.Reader:
Deprecated. Use $out instead.
*/
write data/io.Data from/int=0 to/int=data.byte-size --break-length=0 --wait=false -> int:
write data/io.Data from/int=0 to/int=data.byte-size --break-length/int=0 --pre-break-length/int=0 --wait=false -> int:
size := to - from
while from < to:
from += try-write_ data from to --break-length=break-length
from += try-write_ data from to --break-length=break-length --pre-break-length=pre-break-length

if wait: flush_

Expand Down Expand Up @@ -263,11 +263,11 @@ class Port extends Object with io.InMixin implements reader.Reader:
if flushed: return
yield

try-write_ data/io.Data from/int=0 to/int=data.byte-size --break-length=0:
try-write_ data/io.Data from/int=0 to/int=data.byte-size --break-length=0 --pre-break-length=0:
while true:
s := state_.wait-for-state WRITE-STATE_ | ERROR-STATE_
if not uart_: throw "CLOSED"
written := uart-write_ uart_ data from to break-length
written := uart-write_ uart_ data from to break-length pre-break-length
if written < to - from:
// Not everything was written, clear write flag and try again.
state_.clear-state WRITE-STATE_
Expand Down Expand Up @@ -360,10 +360,10 @@ class UartWriter extends io.Writer:
Returns the number of bytes written.
*/
write data/io.Data from/int=0 to/int=data.byte-size --break-length/int=0 --flush/bool=false -> int:
write data/io.Data from/int=0 to/int=data.byte-size --break-length/int=0 --pre-break-length/int=0 --flush/bool=false -> int:
data-size := to - from
while not is-closed_:
from += try-write data from to --break-length=break-length --flush=flush
from += try-write data from to --break-length=break-length --pre-break-length=pre-break-length --flush=flush
if from >= to: return data-size
yield
assert: is-closed_
Expand All @@ -379,14 +379,14 @@ class UartWriter extends io.Writer:
the data is written. The duration of the break signal is bit-duration * $break-length,
where bit-duration is the duration it takes to write one bit at the current baud rate.
*/
try-write data/io.Data from/int=0 to/int=data.byte-size --break-length/int=0 --flush/bool=false -> int:
try-write data/io.Data from/int=0 to/int=data.byte-size --break-length/int=0 --pre-break-length/int=0 --flush/bool=false -> int:
if is-closed_: throw "WRITER_CLOSED"
result := port_.try-write_ data from to --break-length=break-length
result := port_.try-write_ data from to --break-length=break-length --pre-break-length=pre-break-length
if flush and (result > 0 or data.byte-size == 0): this.flush
return result

try-write_ data/io.Data from/int to/int --break-length/int=0 -> int:
return port_.try-write_ data from to --break-length=break-length
try-write_ data/io.Data from/int to/int --break-length/int=0 --pre-break-length/int=0 -> int:
return port_.try-write_ data from to --break-length=break-length --pre-break-length=pre-break-length

flush -> none:
port_.flush_
Expand Down Expand Up @@ -419,12 +419,12 @@ uart-close_ group uart:
Writes the $data to the uart.
Returns the amount of bytes that were written.
*/
uart-write_ uart data from to break-length:
uart-write_ uart data from to break-length pre-break-length:
#primitive.uart.write:
// The `uart-write_` function is allowed to consume less than the whole data slice.
// We limit the chunk size to 1024 bytes.
return io.primitive-redo-io-data_ it data from (min to (from + 1024)): | chunk/ByteArray |
uart-write_ uart chunk 0 chunk.size break-length
uart-write_ uart chunk 0 chunk.size break-length pre-break-length

uart-wait-tx_ uart:
#primitive.uart.wait-tx
Expand Down
2 changes: 1 addition & 1 deletion src/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ namespace toit {
PRIMITIVE(close, 2) \
PRIMITIVE(get_baud_rate, 1) \
PRIMITIVE(set_baud_rate, 2) \
PRIMITIVE(write, 5) \
PRIMITIVE(write, 6) \
PRIMITIVE(read, 1) \
PRIMITIVE(wait_tx, 1) \
PRIMITIVE(set_control_flags, 2) \
Expand Down
3 changes: 2 additions & 1 deletion src/resources/uart_esp32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,9 @@ PRIMITIVE(set_baud_rate) {
}

PRIMITIVE(write) {
ARGS(UartResource, uart, Blob, data, int, from, int, to, int, break_length)
ARGS(UartResource, uart, Blob, data, int, from, int, to, int, break_length, int, pre_preak_length)

// TODO: use pre_break_length.
if (from < 0 || from > to || to > data.length()) FAIL(OUT_OF_RANGE);
if (break_length < 0 || break_length >= 256) FAIL(OUT_OF_RANGE);

Expand Down
3 changes: 2 additions & 1 deletion src/resources/uart_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ PRIMITIVE(set_baud_rate) {

// Writes the data to the UART.
PRIMITIVE(write) {
ARGS(IntResource, resource, Blob, data, int, from, int, to, int, break_length);
ARGS(IntResource, resource, Blob, data, int, from, int, to, int, break_length, int, pre_break_length);
USE(pre_break_length);
int fd = resource->id();

if (from < 0 || from > to || to > data.length()) FAIL(OUT_OF_RANGE);
Expand Down
3 changes: 2 additions & 1 deletion src/resources/uart_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ PRIMITIVE(set_baud_rate) {
// Writes the data to the UART.
// Does not support break.
PRIMITIVE(write) {
ARGS(UartResource, uart_resource, Blob, data, int, from, int, to, int, break_length);
ARGS(UartResource, uart_resource, Blob, data, int, from, int, to, int, break_length, int, pre_break_length);
USE(pre_break_length);
if (break_length > 0) FAIL(INVALID_ARGUMENT);

if (from < 0 || from > to || to > data.length()) FAIL(OUT_OF_RANGE);
Expand Down

0 comments on commit 385be24

Please sign in to comment.