Skip to content

Commit 2656df1

Browse files
facchinmsoburi
authored andcommitted
USB: fix corner case when cdc_acm is in dt and CONFIG_CDC is disabled
1 parent b15608f commit 2656df1

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

cores/arduino/SerialUSB.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ class SerialUSB_ : public ZephyrSerial {
3131
};
3232
} // namespace arduino
3333

34-
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)
34+
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_USB_CDC_ACM)
3535
extern arduino::SerialUSB_ Serial;
3636
#endif

cores/arduino/USB.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,13 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
// Make PluggableUSB link happy
8-
#include "api/PluggableUSB.h"
9-
10-
static uint8_t _epBuffer[1];
11-
void* epBuffer(unsigned int n) {
12-
return &_epBuffer[n];
13-
};
14-
15-
arduino::PluggableUSB_::PluggableUSB_() {}
16-
177
#include <zephyr/devicetree.h>
188
#include <zephyr/drivers/uart.h>
199
#include <zephyr/drivers/uart/cdc_acm.h>
2010
#include <zephyr/usb/usb_device.h>
2111
#include <SerialUSB.h>
2212

23-
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)
13+
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_USB_CDC_ACM)
2414
const struct device *const usb_dev = DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm, 0));
2515

2616
void usb_status_cb(enum usb_dc_status_code cb_status, const uint8_t *param) {

cores/arduino/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
#endif
1111

1212
int main(void) {
13-
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) || DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials)
13+
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_USB_CDC_ACM)
1414
Serial.begin(115200);
1515
#endif
1616
setup();
1717

1818
for (;;) {
1919
loop();
20+
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_USB_CDC_ACM) || DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials)
2021
if (arduino::serialEventRun) arduino::serialEventRun();
22+
#endif
2123
}
2224

2325
return 0;

cores/arduino/zephyrSerial.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,25 @@ size_t arduino::ZephyrSerial::write(const uint8_t *buffer, size_t size)
164164
return ret;
165165
}
166166

167+
void arduino::ZephyrSerial::flush() {
168+
while (ring_buf_size_get(&tx.ringbuf) > 0) {
169+
k_yield();
170+
}
171+
while (uart_irq_tx_complete(uart) == 0){
172+
k_yield();
173+
}
174+
}
175+
176+
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm))
177+
#define FIRST_UART_INDEX 1
178+
#else
179+
#define FIRST_UART_INDEX 0
180+
#endif
181+
167182
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials)
168-
#if !DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)
183+
#if !(DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_USB_CDC_ACM)
169184
// If CDC USB, use that object as Serial (and SerialUSB)
170-
arduino::ZephyrSerial Serial(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), serials, 0)));
185+
arduino::ZephyrSerial Serial(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), serials, FIRST_UART_INDEX)));
171186
#endif
172187
#if (DT_PROP_LEN(DT_PATH(zephyr_user), serials) > 1)
173188
#define ARDUINO_SERIAL_DEFINED_0 1

cores/arduino/zephyrSerial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class ZephyrSerial : public HardwareSerial
8484
} // namespace arduino
8585

8686
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials)
87-
#if !DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)
87+
#if !(DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_USB_CDC_ACM)
8888
// If CDC USB, use that object as Serial (and SerialUSB)
8989
extern arduino::ZephyrSerial Serial;
9090
#endif

0 commit comments

Comments
 (0)