Skip to content

Commit 85a2cfe

Browse files
authored
Merge pull request #370 from pillo79/router-serial
zephyrSerial: provide ARDUINO_ROUTER_SERIAL from DTS
2 parents 0a69a87 + b75efe7 commit 85a2cfe

19 files changed

Lines changed: 199 additions & 89 deletions

File tree

cores/arduino/Arduino.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,8 @@ void analogWriteResolution(int bits);
160160
// Allow namespace-less operations if Arduino.h is included
161161
using namespace arduino;
162162

163+
#if __has_include("postvariant.h")
164+
#include "postvariant.h"
165+
#endif
166+
163167
#endif // __cplusplus

cores/arduino/DebugLog.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) Arduino s.r.l. and/or its affiliated companies
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
/*
10+
* This header is used to forcibly disable logging and assertions in the
11+
* MsgPack library. It is here to shadow the real DebugLog library available in
12+
* the index, which is currently not compatible with the latest boards.
13+
*/
14+
15+
#define LOG_ERROR(...) ((void)0)
16+
#define LOG_WARN(...) ((void)0)
17+
#define LOG_INFO(...) ((void)0)
18+
#define LOG_DEBUG(...) ((void)0)
19+
#define LOG_TRACE(...) ((void)0)
20+
#define ASSERT(...) ((void)0)
21+
#define ASSERTM(...) ((void)0)
22+
#define PRINT(...) ((void)0)
23+
#define PRINTLN(...) ((void)0)

cores/arduino/SerialUSB.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class SerialUSB_ : public ZephyrSerial {
5757
};
5858
} // namespace arduino
5959

60-
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && \
61-
(CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
60+
#if ZARD_FIRST_SERIAL_IS_SERIALUSB
6261
extern arduino::SerialUSB_ Serial;
6362
#endif

cores/arduino/USB.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
#include <zephyr/usb/usb_device.h>
1111
#include <SerialUSB.h>
1212

13-
#if ((DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)) && \
14-
(CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
13+
#if ZARD_FIRST_SERIAL_IS_SERIALUSB
1514
const struct device *const usb_dev =
16-
DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm, 0));
15+
DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm_serial, 0));
1716

1817
void __attribute__((weak)) _on_1200_bps() {
1918
NVIC_SystemReset();

cores/arduino/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ void __attribute__((weak)) __loopHook(void) {
2424
}
2525

2626
int main(void) {
27-
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && \
28-
(CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
27+
#if ZARD_FIRST_SERIAL_IS_SERIALUSB
2928
Serial.begin(115200);
3029
#endif
3130

cores/arduino/zephyrSerial.cpp

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2022 Dhruva Gole
3+
* Copyright (c) Arduino s.r.l. and/or its affiliated companies
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -189,62 +190,29 @@ void arduino::ZephyrSerial::flush() {
189190
}
190191
}
191192

192-
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm))
193-
#define FIRST_UART_INDEX 1
194-
#else
195-
#define FIRST_UART_INDEX 0
196-
#endif
197-
198193
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials)
199-
#if !(DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && \
200-
(CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
201-
// If CDC USB, use that object as Serial (and SerialUSB)
202-
arduino::ZephyrSerial
203-
Serial(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), serials, FIRST_UART_INDEX)));
194+
#define DEFINE_SERIAL_N(n, p, i) \
195+
arduino::ZephyrSerial ZARD_SERIAL_NAME(i)(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(n, p, i)));
196+
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), serials, DEFINE_SERIAL_N)
197+
198+
#elif DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(arduino_serial))
199+
arduino::ZephyrSerial ZARD_SERIAL_NAME(0)(DEVICE_DT_GET(DT_NODELABEL(arduino_serial)));
204200
#endif
205-
#if (DT_PROP_LEN(DT_PATH(zephyr_user), serials) > 1)
206-
#define ARDUINO_SERIAL_DEFINED_0 1
207-
208-
#define DECL_SERIAL_0(n, p, i)
209-
#define DECL_SERIAL_N(n, p, i) \
210-
arduino::ZephyrSerial Serial##i(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(n, p, i)));
211-
#define DECLARE_SERIAL_N(n, p, i) \
212-
COND_CODE_1(ARDUINO_SERIAL_DEFINED_##i, (DECL_SERIAL_0(n, p, i)), (DECL_SERIAL_N(n, p, i)))
213-
214-
#define CALL_EVENT_0(n, p, i)
215-
#define CALL_EVENT_N(n, p, i) \
216-
if (_CONCAT(Serial, i).available()) \
217-
_CONCAT(_CONCAT(serial, i), Event)();
218-
#define CALL_SERIALEVENT_N(n, p, i) \
219-
COND_CODE_1(ARDUINO_SERIAL_DEFINED_##i, (CALL_EVENT_0(n, p, i)), (CALL_EVENT_N(n, p, i)));
220-
221-
#define DECL_EVENT_0(n, p, i)
222-
#define DECL_EVENT_N(n, p, i) \
223-
__attribute__((weak)) void serial##i##Event() { \
224-
}
225-
#define DECLARE_SERIALEVENT_N(n, p, i) \
226-
COND_CODE_1(ARDUINO_SERIAL_DEFINED_##i, (DECL_EVENT_0(n, p, i)), (DECL_EVENT_N(n, p, i)));
227-
228-
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), serials, DECLARE_SERIAL_N)
229-
#endif // PROP_LEN(serials) > 1
230-
#elif DT_NODE_HAS_STATUS(DT_NODELABEL(arduino_serial), okay)
231-
/* If serials node is not defined, tries to use arduino_serial */
232-
arduino::ZephyrSerial Serial(DEVICE_DT_GET(DT_NODELABEL(arduino_serial)));
233-
#else
201+
202+
#if ZARD_FIRST_SERIAL_IS_STUB
234203
arduino::ZephyrSerialStub Serial;
235204
#endif
236205

237-
__attribute__((weak)) void serialEvent() {
238-
}
239-
#if (DT_PROP_LEN(DT_PATH(zephyr_user), serials) > 1)
240-
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), serials, DECLARE_SERIALEVENT_N)
241-
#endif
206+
#define DEFINE_WEAK_SERIALEVENT_N(n, s) \
207+
__attribute__((weak)) void CONCAT(serial, ZARD_SERIAL_STEM(n), Event)() { \
208+
}
209+
210+
#define CALL_SERIALEVENT_N(n, s) \
211+
if (ZARD_SERIAL_NAME(n).available()) \
212+
CONCAT(serial, ZARD_SERIAL_STEM(n), Event)();
213+
214+
LISTIFY(ZARD_GENERIC_SERIAL_COUNT, DEFINE_WEAK_SERIALEVENT_N, ())
242215

243216
void arduino::serialEventRun(void) {
244-
if (Serial.available()) {
245-
serialEvent();
246-
}
247-
#if (DT_PROP_LEN(DT_PATH(zephyr_user), serials) > 1)
248-
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), serials, CALL_SERIALEVENT_N)
249-
#endif
217+
LISTIFY(ZARD_GENERIC_SERIAL_COUNT, CALL_SERIALEVENT_N, ())
250218
}

cores/arduino/zephyrSerial.h

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2022 Dhruva Gole
3+
* Copyright (c) Arduino s.r.l. and/or its affiliated companies
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -115,26 +116,90 @@ class ZephyrSerial : public HardwareSerial {
115116

116117
} // namespace arduino
117118

118-
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials)
119-
#if !(DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && \
120-
(CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
121-
// If CDC USB, use that object as Serial (and SerialUSB)
122-
extern arduino::ZephyrSerial Serial;
119+
/* Return the index of it if matched, oterwise return an empty string. */
120+
#define ZARD_SERIAL_MATCH(n, p, i, node) \
121+
COND_CODE_1(DT_SAME_NODE(DT_PHANDLE_BY_IDX(n, p, i), node), (i), ())
122+
123+
/* Only matched device returns non-empty value, so the overall expansion is
124+
* matched device's index.
125+
*/
126+
#define ZARD_SERIAL_INDEXOF(node) \
127+
DT_FOREACH_PROP_ELEM_VARGS(DT_PATH(zephyr_user), serials, ZARD_SERIAL_MATCH, node)
128+
129+
/* Serial object associated with the Zephyr console. */
130+
#define ARDUINO_CONSOLE_SERIAL ZARD_SERIAL_NAME(ZARD_SERIAL_INDEXOF(DT_CHOSEN(zephyr_console)))
131+
132+
/* Serial object associated with the first HW serial (usually on D0/D1). */
133+
#define ARDUINO_HARDWARE_SERIAL ZARD_SERIAL_NAME(0)
134+
135+
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm_serial)
136+
/* Devicetree requires a SerialUSB object for 'Serial'. */
137+
#define ZARD_SKIP_FIRST_SERIAL 1
138+
#if (CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS)
139+
/* SerialUSB can be compiled in the project. */
140+
#define ZARD_FIRST_SERIAL_IS_SERIALUSB 1
141+
#else
142+
/* SerialUSB is required but no driver was enabled for the USB CDC ACM device.
143+
* Define a stub Serial object to avoid build errors.
144+
*/
145+
#define ZARD_FIRST_SERIAL_IS_STUB 1
146+
#endif
147+
#endif
148+
149+
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), arduino_router_serial)
150+
/* If the board has an arduino,router-serial node, and the currently used
151+
* library has the support, then the 'Serial' object is actually the same as
152+
* the Monitor object in the Arduino_RouterBridge library.
153+
*/
154+
#define ZARD_SKIP_FIRST_SERIAL 1
155+
#define ZARD_FIRST_SERIAL_IS_ARDUINO_ROUTER 1
156+
#define ARDUINO_ROUTER_PHANDLE DT_PROP(DT_PATH(zephyr_user), arduino_router_serial)
157+
#define ARDUINO_ROUTER_SERIAL ZARD_SERIAL_NAME(ZARD_SERIAL_INDEXOF(ARDUINO_ROUTER_PHANDLE))
123158
#endif
124-
#if (DT_PROP_LEN(DT_PATH(zephyr_user), serials) > 1)
125-
#define SERIAL_DEFINED_0 1
126-
#define EXTERN_SERIAL_N(i) extern arduino::ZephyrSerial Serial##i;
127-
#define DECLARE_EXTERN_SERIAL_N(n, p, i) COND_CODE_1(SERIAL_DEFINED_##i, (), (EXTERN_SERIAL_N(i)))
128159

129-
/* Declare Serial1, Serial2, ... */
130-
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), serials, DECLARE_EXTERN_SERIAL_N)
160+
/* Name of a Serial object for a given index. */
161+
#define ZARD_SERIAL_NAME(n) CONCAT(Serial, ZARD_SERIAL_STEM(n))
162+
163+
/*
164+
* Determine the number of generic ZephyrSerial objects to instantiate.
165+
*/
166+
167+
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials)
168+
/* The array property; the total number is the length of the array. */
169+
#define ZARD_GENERIC_SERIAL_COUNT DT_PROP_LEN(DT_PATH(zephyr_user), serials)
131170

132-
#undef DECLARE_EXTERN_SERIAL_N
133-
#undef EXTERN_SERIAL_N
134-
#undef SERIAL_DEFINED_0
171+
#elif DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(arduino_serial))
172+
/* A single node with compatible "arduino,serial" is present. */
173+
#define ZARD_GENERIC_SERIAL_COUNT 1
174+
175+
#elif ZARD_SKIP_FIRST_SERIAL
176+
/* Only a special Serial is present. */
177+
#define ZARD_GENERIC_SERIAL_COUNT 0
178+
179+
#else
180+
/* Neither 'serials' property, nor 'arduino,serial' node, nor a special Serial
181+
* is present. Define a stub Serial object to avoid build errors.
182+
*/
183+
#define ZARD_GENERIC_SERIAL_COUNT 0
184+
#define ZARD_SKIP_FIRST_SERIAL 1
185+
#define ZARD_FIRST_SERIAL_IS_STUB 1
135186
#endif
136-
#elif DT_NODE_HAS_STATUS(DT_NODELABEL(arduino_serial), okay)
137-
extern arduino::ZephyrSerial Serial;
187+
188+
#if ZARD_SKIP_FIRST_SERIAL
189+
// Map index 0 to 'Serial1', index 1 to 'Serial2', etc.
190+
#define ZARD_SERIAL_STEM(i) UTIL_INC(i)
138191
#else
192+
// Map index 0 to 'Serial', index 1 to 'Serial1', etc.
193+
#define ZARD_SERIAL_STEM(i) COND_CODE_0(i, (), (i))
194+
#endif
195+
196+
#if ZARD_FIRST_SERIAL_IS_STUB
139197
extern arduino::ZephyrSerialStub Serial;
140198
#endif
199+
200+
#if ZARD_GENERIC_SERIAL_COUNT > 0
201+
/* Declare all generic ZephyrSerial objects */
202+
#define DECLARE_SERIAL_N(n, s) extern arduino::ZephyrSerial ZARD_SERIAL_NAME(n);
203+
LISTIFY(ZARD_GENERIC_SERIAL_COUNT, DECLARE_SERIAL_N, ())
204+
#undef DECLARE_SERIAL_N
205+
#endif

extra/artifacts/zephyr_unoq.only

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
# Files listed here are included by this package only, and automatically
55
# excluded by all other artifacts.
66

7+
# Shadow the actual DebugLog library which does not work on the UNO Q
8+
cores/arduino/DebugLog.h
9+
710
libraries/Arduino_LED_Matrix/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) Arduino s.r.l. and/or its affiliated companies
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
/*
10+
* This is just a placeholder to handle the case of the Arduino_RouterBridge
11+
* library NOT being installed. If this file is chosen, the actual library is
12+
* missing and the user needs to be warned about it.
13+
*/
14+
15+
#error \
16+
"Please install the Arduino_RouterBridge library from the Library Manager for proper Serial support on this board."

variants/arduino_giga_r1_stm32h747xx_m7/arduino_giga_r1_stm32h747xx_m7.overlay

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@
522522
<&gpioa 4 0>,
523523
<&gpioa 5 0>;
524524

525-
serials = <&board_cdc_acm_uart>, <&usart1>, <&usart2>, <&uart4>, <&usart6>;
526-
cdc-acm = <&board_cdc_acm_uart>;
525+
cdc-acm-serial = <&board_cdc_acm_uart>; /* 'Serial' object is managed by SerialUSB */
526+
serials = <&usart1>, <&usart2>, <&uart4>, <&usart6>; /* 'Serial1' onwards */
527527
cans = <&fdcan2>;
528528
i2cs = <&i2c2>, <&i2c4>, <&i2c1>;
529529
spis = <&spi1>, <&spi5>;

0 commit comments

Comments
 (0)