From 0aa1c927f23ed67b0737d933044626a9acb47b83 Mon Sep 17 00:00:00 2001 From: Kurt Eckhardt Date: Fri, 29 Nov 2024 15:10:04 -0800 Subject: [PATCH 1/4] 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 --- cores/arduino/zephyrSerial.cpp | 23 +++++++++++++++++++---- cores/arduino/zephyrSerial.h | 1 + 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cores/arduino/zephyrSerial.cpp b/cores/arduino/zephyrSerial.cpp index ba419bd8..50b1e9de 100644 --- a/cores/arduino/zephyrSerial.cpp +++ b/cores/arduino/zephyrSerial.cpp @@ -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); } @@ -126,15 +130,26 @@ 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() @@ -142,10 +157,10 @@ 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) diff --git a/cores/arduino/zephyrSerial.h b/cores/arduino/zephyrSerial.h index d280d251..ff3e8206 100644 --- a/cores/arduino/zephyrSerial.h +++ b/cores/arduino/zephyrSerial.h @@ -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(); From a64ced3b7752d235f234417c1044b2ce4d742f4e Mon Sep 17 00:00:00 2001 From: Kurt Eckhardt Date: Fri, 29 Nov 2024 15:48:06 -0800 Subject: [PATCH 2/4] Update arduino_giga_r1_m7.overlay Added the other serial ports --- loader/boards/arduino_giga_r1_m7.overlay | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/loader/boards/arduino_giga_r1_m7.overlay b/loader/boards/arduino_giga_r1_m7.overlay index 5ebea60f..143376f1 100644 --- a/loader/boards/arduino_giga_r1_m7.overlay +++ b/loader/boards/arduino_giga_r1_m7.overlay @@ -9,6 +9,20 @@ status = "okay"; }; +&usart2 { + status = "okay"; + pinctrl-0 = <&usart2_tx_pd5 &usart2_rx_pd6>; + pinctrl-names = "default"; + current-speed = <115200>; +}; + +&uart4 { + status = "okay"; + pinctrl-0 = <&uart4_tx_ph13 &uart4_rx_pi9>; + pinctrl-names = "default"; + current-speed = <115200>; +}; + &usart6 { status = "okay"; }; @@ -407,7 +421,7 @@ <&gpioa 4 0>, <&gpioa 5 0>; - serials = <&cdc_acm_uart0>, <&usart1>, <&usart6>; + serials = <&cdc_acm_uart0>, <&usart1>, <&usart2>, <&uart4>, <&usart6>; cdc-acm = <&cdc_acm_uart0>; i2cs = <&i2c4>; spis = <&spi1>, <&spi5>; From 5c4e10e74a3e9b3e7a86f16976b65e68c28468b3 Mon Sep 17 00:00:00 2001 From: Kurt Eckhardt Date: Sat, 30 Nov 2024 10:37:52 -0800 Subject: [PATCH 3/4] SerialX.flush() - now wait until TC Complete SerialX.flush() - now waits until the TX completes... Looks at the TC flag of the uart to know when it has completed the transfer. Moved the flush() function to .cpp file as it now uses the device header file which is only included in the .cpp file --- cores/arduino/zephyrSerial.cpp | 10 ++++++++++ cores/arduino/zephyrSerial.h | 6 +----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/cores/arduino/zephyrSerial.cpp b/cores/arduino/zephyrSerial.cpp index 50b1e9de..8d838c6b 100644 --- a/cores/arduino/zephyrSerial.cpp +++ b/cores/arduino/zephyrSerial.cpp @@ -189,6 +189,16 @@ size_t arduino::ZephyrSerial::write(const uint8_t *buffer, size_t size) return size; } +void arduino::ZephyrSerial::flush() { + while (ring_buf_size_get(&tx.ringbuf) > 0) { + k_yield(); + } + while (uart_irq_tx_complete(uart) == 0){ + k_yield(); + } +} + + #if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials) #if !DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) // If CDC USB, use that object as Serial (and SerialUSB) diff --git a/cores/arduino/zephyrSerial.h b/cores/arduino/zephyrSerial.h index ff3e8206..2febd369 100644 --- a/cores/arduino/zephyrSerial.h +++ b/cores/arduino/zephyrSerial.h @@ -51,11 +51,7 @@ class ZephyrSerial : public HardwareSerial ZephyrSerial(const struct device *dev) : uart(dev) { } void begin(unsigned long baudrate, uint16_t config); void begin(unsigned long baudrate) { begin(baudrate, SERIAL_8N1); } - void flush() { - while (ring_buf_size_get(&tx.ringbuf) > 0) { - k_yield(); - } - } + void flush(); void end() { } size_t write(const uint8_t *buffer, size_t size); size_t write(const uint8_t data) { return write(&data, 1); } From a565a0aadc7f26beb04b580dc15b04c2dd7c52d8 Mon Sep 17 00:00:00 2001 From: kurte Date: Wed, 4 Dec 2024 09:19:47 -0800 Subject: [PATCH 4/4] zephyrSerial.h - reduce compiler warnings --- cores/arduino/zephyrSerial.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/arduino/zephyrSerial.h b/cores/arduino/zephyrSerial.h index 2febd369..7e7a160a 100644 --- a/cores/arduino/zephyrSerial.h +++ b/cores/arduino/zephyrSerial.h @@ -14,8 +14,8 @@ namespace arduino { class ZephyrSerialStub : public HardwareSerial { public: - void begin(unsigned long baudRate) { } - void begin(unsigned long baudrate, uint16_t config) { } + void begin(__attribute__((unused)) unsigned long baudRate) { } + void begin(__attribute__((unused)) unsigned long baudrate, __attribute__((unused)) uint16_t config) { } void end() { } int available() { return 0; } int peek() { return 0; }