Repository navigation
core: Improve Serial implements #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
soburi
wants to merge
1
commit into
zephyrproject-rtos:next
Choose a base branch
from
soburi:improve_serial
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| /* | ||
| * Copyright (c) 2022 Dhruva Gole | ||
| * Copyright (c) Arduino s.r.l. and/or its affiliated companies | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
@@ -9,6 +10,7 @@ | |
|
|
||
| #include <api/HardwareSerial.h> | ||
| #include <zephyrSerial.h> | ||
| #include <Arduino.h> | ||
|
|
||
| namespace { | ||
|
|
||
|
|
@@ -58,10 +60,14 @@ void arduino::ZephyrSerial::begin(unsigned long baud, uint16_t conf) { | |
| .flow_ctrl = UART_CFG_FLOW_CTRL_NONE, | ||
| }; | ||
|
|
||
| uart->ops.init(uart); | ||
| _reinit_if_needed(); | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
|
|
@@ -72,10 +78,6 @@ void arduino::ZephyrSerial::IrqHandler() { | |
|
|
||
| (void)uart_irq_update(uart); | ||
|
|
||
| if (ring_buf_size_get(&tx.ringbuf) == 0) { | ||
| uart_irq_tx_disable(uart); | ||
| } | ||
|
|
||
| k_sem_take(&rx.sem, K_NO_WAIT); | ||
| while (uart_irq_rx_ready(uart) && ((length = uart_fifo_read(uart, buf, sizeof(buf))) > 0)) { | ||
| length = min(sizeof(buf), static_cast<size_t>(length)); | ||
|
|
@@ -88,6 +90,11 @@ void arduino::ZephyrSerial::IrqHandler() { | |
| k_sem_give(&rx.sem); | ||
|
|
||
| k_sem_take(&tx.sem, K_NO_WAIT); | ||
|
|
||
| if (ring_buf_size_get(&tx.ringbuf) == 0) { | ||
| uart_irq_tx_disable(uart); | ||
| } | ||
|
Comment on lines
92
to
+96
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Considering addressing this in arduino/ArduinoCore-zephyr. |
||
|
|
||
| while (uart_irq_tx_ready(uart) && ((length = ring_buf_size_get(&tx.ringbuf)) > 0)) { | ||
| length = min(sizeof(buf), static_cast<size_t>(length)); | ||
| ring_buf_peek(&tx.ringbuf, &buf[0], length); | ||
|
|
@@ -103,6 +110,7 @@ void arduino::ZephyrSerial::IrqHandler() { | |
| } | ||
|
|
||
| void arduino::ZephyrSerial::IrqDispatch(const struct device *dev, void *data) { | ||
| (void)dev; // unused | ||
| reinterpret_cast<ZephyrSerial *>(data)->IrqHandler(); | ||
| } | ||
|
|
||
|
|
@@ -116,87 +124,93 @@ int arduino::ZephyrSerial::available() { | |
| return ret; | ||
| } | ||
|
|
||
| int arduino::ZephyrSerial::availableForWrite() { | ||
| int ret; | ||
|
|
||
| k_sem_take(&tx.sem, K_FOREVER); | ||
| ret = ring_buf_space_get(&tx.ringbuf); | ||
| k_sem_give(&tx.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) { | ||
| int ret; | ||
| size_t idx = 0; | ||
|
|
||
| k_sem_take(&tx.sem, K_FOREVER); | ||
| ret = ring_buf_put(&tx.ringbuf, buffer, size); | ||
| k_sem_give(&tx.sem); | ||
|
|
||
| if (ret < 0) { | ||
| return 0; | ||
| while (1) { | ||
|
soburi marked this conversation as resolved.
|
||
| k_sem_take(&tx.sem, K_FOREVER); | ||
| auto ret = ring_buf_put(&tx.ringbuf, &buffer[idx], size - idx); | ||
| k_sem_give(&tx.sem); | ||
| if (ret < 0) { | ||
| return 0; | ||
| } | ||
| idx += ret; | ||
| if (ret == 0) { | ||
| uart_irq_tx_enable(uart); | ||
| yield(); | ||
| } | ||
| if (idx == size) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| uart_irq_tx_enable(uart); | ||
|
|
||
| return ret; | ||
| return size; | ||
| } | ||
|
|
||
| #if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials) | ||
| arduino::ZephyrSerial Serial(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), serials, 0))); | ||
| #if (DT_PROP_LEN(DT_PATH(zephyr_user), serials) > 1) | ||
| #define ARDUINO_SERIAL_DEFINED_0 1 | ||
|
|
||
| #define DECL_SERIAL_0(n, p, i) | ||
| #define DECL_SERIAL_N(n, p, i) \ | ||
| arduino::ZephyrSerial Serial##i(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(n, p, i))); | ||
| #define DECLARE_SERIAL_N(n, p, i) \ | ||
| COND_CODE_1(ARDUINO_SERIAL_DEFINED_##i, (DECL_SERIAL_0(n, p, i)), (DECL_SERIAL_N(n, p, i))) | ||
|
|
||
| #define CALL_EVENT_0(n, p, i) | ||
| #define CALL_EVENT_N(n, p, i) \ | ||
| if (_CONCAT(Serial, i).available()) \ | ||
| _CONCAT(_CONCAT(serial, i), Event)(); | ||
| #define CALL_SERIALEVENT_N(n, p, i) \ | ||
| COND_CODE_1(ARDUINO_SERIAL_DEFINED_##i, (CALL_EVENT_0(n, p, i)), (CALL_EVENT_N(n, p, i))); | ||
|
|
||
| #define DECL_EVENT_0(n, p, i) | ||
| #define DECL_EVENT_N(n, p, i) \ | ||
| __attribute__((weak)) void serial##i##Event() { \ | ||
| void arduino::ZephyrSerial::flush() { | ||
| while (ring_buf_size_get(&tx.ringbuf) > 0) { | ||
| k_yield(); | ||
| } | ||
| #define DECLARE_SERIALEVENT_N(n, p, i) \ | ||
| COND_CODE_1(ARDUINO_SERIAL_DEFINED_##i, (DECL_EVENT_0(n, p, i)), (DECL_EVENT_N(n, p, i))); | ||
|
|
||
| DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), serials, DECLARE_SERIAL_N) | ||
| #endif // PROP_LEN(serials) > 1 | ||
| #elif DT_NODE_HAS_STATUS(DT_NODELABEL(arduino_serial), okay) | ||
| /* If serials node is not defined, tries to use arduino_serial */ | ||
| arduino::ZephyrSerial Serial(DEVICE_DT_GET(DT_NODELABEL(arduino_serial))); | ||
| #else | ||
| arduino::ZephyrSerialStub Serial; | ||
| while (uart_irq_tx_complete(uart) == 0) { | ||
| k_yield(); | ||
| } | ||
| } | ||
|
|
||
| #if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials) | ||
| #define DEFINE_SERIAL_N(n, p, i) \ | ||
| arduino::ZephyrSerial ZARD_SERIAL_NAME(i)(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(n, p, i))); | ||
| DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), serials, DEFINE_SERIAL_N) | ||
|
|
||
| #elif DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(arduino_serial)) | ||
| arduino::ZephyrSerial ZARD_SERIAL_NAME(0)(DEVICE_DT_GET(DT_NODELABEL(arduino_serial))); | ||
| #endif | ||
|
|
||
| __attribute__((weak)) void serialEvent() { | ||
| } | ||
| #if (DT_PROP_LEN(DT_PATH(zephyr_user), serials) > 1) | ||
| DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), serials, DECLARE_SERIALEVENT_N) | ||
| #if ZARD_FIRST_SERIAL_IS_STUB | ||
| arduino::ZephyrSerialStub Serial; | ||
| #endif | ||
|
|
||
| void arduino::serialEventRun(void) { | ||
| if (Serial.available()) { | ||
| serialEvent(); | ||
| #define DEFINE_WEAK_SERIALEVENT_N(n, s) \ | ||
| __attribute__((weak)) void CONCAT(serial, ZARD_SERIAL_STEM(n), Event)() { \ | ||
| } | ||
| #if (DT_PROP_LEN(DT_PATH(zephyr_user), serials) > 1) | ||
| DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), serials, CALL_SERIALEVENT_N) | ||
| #endif | ||
|
|
||
| #define CALL_SERIALEVENT_N(n, s) \ | ||
| if (ZARD_SERIAL_NAME(n).available()) \ | ||
| CONCAT(serial, ZARD_SERIAL_STEM(n), Event)(); | ||
|
|
||
| LISTIFY(ZARD_GENERIC_SERIAL_COUNT, DEFINE_WEAK_SERIALEVENT_N, ()) | ||
|
|
||
| void arduino::serialEventRun(void) { | ||
| LISTIFY(ZARD_GENERIC_SERIAL_COUNT, CALL_SERIALEVENT_N, ()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.