From ea7cea6c8a7e7184f275a9a56cfa6873b1716e1c Mon Sep 17 00:00:00 2001
From: Andrei Gherzan <andrei.gherzan@huawei.com>
Date: Thu, 27 Jan 2022 12:43:52 +0100
Subject: [PATCH] zephyr-kernel-src.inc: Include patch to enable console by
 default

This is a reimplementation of a similar patch dropped before. It is done
based on upstream feedback. In oniro, we rely on it for the CI testing
infrastructure.

Signed-off-by: Andrei Gherzan <andrei.gherzan@huawei.com>
---
 ...able-the-USB-ACM0-console-by-default.patch | 114 ++++++++++++++++++
 ...-USB-ACM0-console-by-default.patch.license |   3 +
 .../zephyr-kernel/zephyr-kernel-src.inc       |   1 +
 3 files changed, 118 insertions(+)
 create mode 100644 meta-zephyr-core/recipes-kernel/zephyr-kernel/files/0001-console-enable-the-USB-ACM0-console-by-default.patch
 create mode 100644 meta-zephyr-core/recipes-kernel/zephyr-kernel/files/0001-console-enable-the-USB-ACM0-console-by-default.patch.license

diff --git a/meta-zephyr-core/recipes-kernel/zephyr-kernel/files/0001-console-enable-the-USB-ACM0-console-by-default.patch b/meta-zephyr-core/recipes-kernel/zephyr-kernel/files/0001-console-enable-the-USB-ACM0-console-by-default.patch
new file mode 100644
index 0000000..21a3d93
--- /dev/null
+++ b/meta-zephyr-core/recipes-kernel/zephyr-kernel/files/0001-console-enable-the-USB-ACM0-console-by-default.patch
@@ -0,0 +1,114 @@
+From e7a751f8003e85c93317bc0edfdc5e0cf589d17c Mon Sep 17 00:00:00 2001
+From: Bartosz Golaszewski <bartosz.golaszewski@huawei.com>
+Date: Thu, 27 Jan 2022 10:26:41 +0100
+Subject: [PATCH] console: enable the USB ACM0 console by default
+
+Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@huawei.com>
+---
+ boards/arm/arduino_nano_33_ble/CMakeLists.txt |  3 +++
+ .../arduino_nano_33_ble.dts                   |  9 +++++++
+ .../arduino_nano_33_ble_defconfig             |  6 +++++
+ .../src/init_usb_cdc_acm.c                    | 25 +++++++++++++++++++
+ subsys/shell/shell_uart.c                     |  2 +-
+ 5 files changed, 44 insertions(+), 1 deletion(-)
+ create mode 100644 boards/arm/arduino_nano_33_ble/src/init_usb_cdc_acm.c
+
+diff --git a/boards/arm/arduino_nano_33_ble/CMakeLists.txt b/boards/arm/arduino_nano_33_ble/CMakeLists.txt
+index 9e885f42ab..37495cb885 100644
+--- a/boards/arm/arduino_nano_33_ble/CMakeLists.txt
++++ b/boards/arm/arduino_nano_33_ble/CMakeLists.txt
+@@ -11,3 +11,6 @@ if(CONFIG_BOARD_ARDUINO_NANO_33_BLE_INIT_SENSORS)
+   zephyr_library_sources("${CMAKE_CURRENT_SOURCE_DIR}/src/init_sensors.c")
+   target_link_libraries(${ZEPHYR_CURRENT_LIBRARY} PRIVATE arduino_nano_33_ble_pins)
+ endif()
++
++zephyr_library()
++zephyr_library_sources("${CMAKE_CURRENT_SOURCE_DIR}/src/init_usb_cdc_acm.c")
+diff --git a/boards/arm/arduino_nano_33_ble/arduino_nano_33_ble.dts b/boards/arm/arduino_nano_33_ble/arduino_nano_33_ble.dts
+index 7e509086d2..aa769b87f0 100644
+--- a/boards/arm/arduino_nano_33_ble/arduino_nano_33_ble.dts
++++ b/boards/arm/arduino_nano_33_ble/arduino_nano_33_ble.dts
+@@ -34,6 +34,10 @@
+ 		led0 = &led0;
+ 		spi = &spi2;
+ 	};
++
++	chosen {
++		zephyr,console = &cdc_acm_uart0;
++	};
+ };
+ 
+ &flash0 {
+@@ -113,6 +117,11 @@
+ zephyr_udc0: &usbd {
+ 	compatible = "nordic,nrf-usbd";
+ 	status = "okay";
++
++	cdc_acm_uart0: cdc_acm_uart0 {
++		compatible = "zephyr,cdc-acm-uart";
++		label = "CDC_ACM_0";
++	};
+ };
+ // All PWM's should be enaled
+ &pwm0 {
+diff --git a/boards/arm/arduino_nano_33_ble/arduino_nano_33_ble_defconfig b/boards/arm/arduino_nano_33_ble/arduino_nano_33_ble_defconfig
+index ba8eeee931..652d79e6cd 100644
+--- a/boards/arm/arduino_nano_33_ble/arduino_nano_33_ble_defconfig
++++ b/boards/arm/arduino_nano_33_ble/arduino_nano_33_ble_defconfig
+@@ -19,3 +19,9 @@ CONFIG_BOOTLOADER_BOSSA_LEGACY=y
+ 
+ # additional board options
+ CONFIG_GPIO_AS_PINRESET=y
++
++CONFIG_USB_DEVICE_STACK=y
++CONFIG_USB_DEVICE_PRODUCT="Zephyr USB console sample"
++
++CONFIG_UART_LINE_CTRL=y
++CONFIG_USB_UART_CONSOLE=y
+diff --git a/boards/arm/arduino_nano_33_ble/src/init_usb_cdc_acm.c b/boards/arm/arduino_nano_33_ble/src/init_usb_cdc_acm.c
+new file mode 100644
+index 0000000000..820dcc2b7a
+--- /dev/null
++++ b/boards/arm/arduino_nano_33_ble/src/init_usb_cdc_acm.c
+@@ -0,0 +1,25 @@
++#include <zephyr.h>
++#include <usb/usb_device.h>
++#include <drivers/uart.h>
++
++BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart),
++	     "Console device is not ACM CDC UART device");
++
++void board_internal_usb_cdc_acm_init(const struct device *unused)
++{
++	ARG_UNUSED(unused);
++	const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
++	uint32_t dtr = 0;
++
++	if (usb_enable(NULL)) {
++		return;
++	}
++
++	/* Poll if the DTR flag was set */
++	while (!dtr) {
++		uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
++		/* Give CPU resources to low priority threads. */
++		k_sleep(K_MSEC(100));
++	}
++}
++SYS_INIT(board_internal_usb_cdc_acm_init, APPLICATION, 32);
+diff --git a/subsys/shell/shell_uart.c b/subsys/shell/shell_uart.c
+index 64b53e29fc..24d647ac58 100644
+--- a/subsys/shell/shell_uart.c
++++ b/subsys/shell/shell_uart.c
+@@ -332,7 +332,7 @@ static int enable_shell_uart(const struct device *arg)
+ 
+ 	return 0;
+ }
+-SYS_INIT(enable_shell_uart, POST_KERNEL,
++SYS_INIT(enable_shell_uart, APPLICATION,
+ 	 CONFIG_SHELL_BACKEND_SERIAL_INIT_PRIORITY);
+ 
+ const struct shell *shell_backend_uart_get_ptr(void)
+-- 
+2.30.1
+
diff --git a/meta-zephyr-core/recipes-kernel/zephyr-kernel/files/0001-console-enable-the-USB-ACM0-console-by-default.patch.license b/meta-zephyr-core/recipes-kernel/zephyr-kernel/files/0001-console-enable-the-USB-ACM0-console-by-default.patch.license
new file mode 100644
index 0000000..98a0b3f
--- /dev/null
+++ b/meta-zephyr-core/recipes-kernel/zephyr-kernel/files/0001-console-enable-the-USB-ACM0-console-by-default.patch.license
@@ -0,0 +1,3 @@
+# SPDX-FileCopyrightText: Huawei Inc.
+#
+# SPDX-License-Identifier: Apache-2.0
diff --git a/meta-zephyr-core/recipes-kernel/zephyr-kernel/zephyr-kernel-src.inc b/meta-zephyr-core/recipes-kernel/zephyr-kernel/zephyr-kernel-src.inc
index 60291ed..e060a95 100644
--- a/meta-zephyr-core/recipes-kernel/zephyr-kernel/zephyr-kernel-src.inc
+++ b/meta-zephyr-core/recipes-kernel/zephyr-kernel/zephyr-kernel-src.inc
@@ -50,6 +50,7 @@ SRC_URI = "\
     git://github.com/zephyrproject-rtos/tinycrypt.git;protocol=https;nobranch=1;destsuffix=git/modules/crypto/tinycrypt;name=tinycrypt \
     git://github.com/zephyrproject-rtos/TraceRecorderSource.git;protocol=https;nobranch=1;destsuffix=git/modules/debug/TraceRecorder;name=TraceRecorder \
     git://github.com/zephyrproject-rtos/trusted-firmware-m.git;protocol=https;nobranch=1;destsuffix=git/modules/tee/tfm;name=tfm \
+    file://0001-console-enable-the-USB-ACM0-console-by-default.patch \
     "
 S = "${WORKDIR}/git"
 
-- 
GitLab