Skip to content

Commit 08570b2

Browse files
committed
loader: redirect logs to uart
In DEBUG mode if SHELL is disabled and board has a logging uart, loader will use it to print logs
1 parent d9d7a9e commit 08570b2

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

loader/main.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LOG_MODULE_REGISTER(sketch);
1414
#include <zephyr/llext/buf_loader.h>
1515
#include <zephyr/shell/shell.h>
1616
#include <zephyr/shell/shell_uart.h>
17+
#include <zephyr/logging/log_ctrl.h>
1718

1819
#include <stdlib.h>
1920
#include <zephyr/drivers/gpio.h>
@@ -35,11 +36,11 @@ struct sketch_header_v1 {
3536
#define SKETCH_FLAG_LINKED 0x02
3637
#define SKETCH_FLAG_IMMEDIATE 0x04
3738

38-
#define TARGET_HAS_USB_CDC_SHELL \
39-
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_SHELL && \
39+
#define TARGET_HAS_USB_CDC \
40+
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && \
4041
(CONFIG_USB_DEVICE_STACK || CONFIG_USB_DEVICE_STACK_NEXT)
4142

42-
#if TARGET_HAS_USB_CDC_SHELL
43+
#if TARGET_HAS_USB_CDC
4344
const struct device *const usb_dev =
4445
DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm, 0));
4546

@@ -63,6 +64,7 @@ int usb_enable(usb_dc_status_callback status_cb) {
6364
}
6465
#endif
6566

67+
#if CONFIG_SHELL
6668
static int enable_shell_usb(void) {
6769
bool log_backend = CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > 0;
6870
uint32_t level = (CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > LOG_LEVEL_DBG) ?
@@ -75,6 +77,7 @@ static int enable_shell_usb(void) {
7577
return 0;
7678
}
7779
#endif
80+
#endif
7881

7982
#ifdef CONFIG_USERSPACE
8083
K_THREAD_STACK_DEFINE(llext_stack, CONFIG_MAIN_STACK_SIZE);
@@ -198,8 +201,9 @@ static int loader(const struct shell *sh) {
198201

199202
size_t sketch_buf_len = sketch_hdr->len;
200203

201-
#if TARGET_HAS_USB_CDC_SHELL
204+
#if TARGET_HAS_USB_CDC
202205
int debug = (!sketch_valid) || (sketch_hdr->flags & SKETCH_FLAG_DEBUG);
206+
#if CONFIG_SHELL
203207
if (debug && strcmp(k_thread_name_get(k_current_get()), "main") == 0) {
204208
// disables default shell on UART
205209
shell_uninit(shell_backend_uart_get_ptr(), NULL);
@@ -214,6 +218,20 @@ static int loader(const struct shell *sh) {
214218
enable_shell_usb();
215219
return 0;
216220
}
221+
#elif CONFIG_LOG
222+
#if !CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT
223+
usb_enable(NULL);
224+
#endif
225+
for (int i = 0; i < log_backend_count_get(); i++) {
226+
const struct log_backend *backend;
227+
backend = log_backend_get(i);
228+
log_backend_init(backend);
229+
log_backend_enable(backend, backend->cb->ctx, CONFIG_LOG_DEFAULT_LEVEL);
230+
if (!debug) {
231+
break;
232+
}
233+
}
234+
#endif
217235
#endif
218236

219237
if (sketch_hdr->flags & SKETCH_FLAG_LINKED) {

0 commit comments

Comments
 (0)