Skip to content

Commit 9be0d75

Browse files
committed
loader: redirect shell to USB if sketch is in Debug mode
1 parent 6823c28 commit 9be0d75

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

loader/llext_exports.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ FORCE_EXPORT_SYM(cdc_acm_dte_rate_callback_set);
9191
#endif
9292

9393
FORCE_EXPORT_SYM(k_timer_init);
94+
FORCE_EXPORT_SYM(k_fatal_halt);
9495
//FORCE_EXPORT_SYM(k_timer_user_data_set);
9596
//FORCE_EXPORT_SYM(k_timer_start);
9697

loader/main.c

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@ LOG_MODULE_REGISTER(app);
1212
#include <zephyr/llext/llext.h>
1313
#include <zephyr/llext/buf_loader.h>
1414
#include <zephyr/shell/shell.h>
15+
#include <zephyr/shell/shell_uart.h>
1516

1617
#include <stdlib.h>
18+
#include <zephyr/drivers/uart/cdc_acm.h>
19+
#include <zephyr/drivers/uart.h>
20+
#include <zephyr/usb/usb_device.h>
21+
22+
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)
23+
const struct device *const usb_dev = DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm, 0));
24+
#endif
25+
26+
static int enable_shell_usb(void);
1727

1828
#ifdef CONFIG_USERSPACE
1929
K_THREAD_STACK_DEFINE(llext_stack, CONFIG_MAIN_STACK_SIZE);
@@ -58,11 +68,23 @@ static int loader(const struct shell *sh)
5868
return -EINVAL;
5969
}
6070

71+
#if CONFIG_SHELL
6172
uint8_t debug = endptr[1];
6273
if (debug != 0 && strcmp(k_thread_name_get(k_current_get()), "main") == 0) {
63-
// starts the shell
74+
// disables default shell on UART
75+
shell_uninit(shell_backend_uart_get_ptr(), NULL);
76+
// enables USB and starts the shell
77+
usb_enable(NULL);
78+
int dtr;
79+
do {
80+
// wait for the serial port to open
81+
uart_line_ctrl_get(usb_dev, UART_LINE_CTRL_DTR, &dtr);
82+
k_sleep(K_MSEC(100));
83+
} while (!dtr);
84+
enable_shell_usb();
6485
return 0;
6586
}
87+
#endif
6688

6789
int header_len = 16;
6890

@@ -150,11 +172,24 @@ static int loader(const struct shell *sh)
150172

151173
#if CONFIG_SHELL
152174
SHELL_CMD_REGISTER(sketch, NULL, "Run sketch", loader);
175+
176+
static int enable_shell_usb(void)
177+
{
178+
bool log_backend = CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > 0;
179+
uint32_t level =
180+
(CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > LOG_LEVEL_DBG) ?
181+
CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL;
182+
static const struct shell_backend_config_flags cfg_flags =
183+
SHELL_DEFAULT_BACKEND_CONFIG_FLAGS;
184+
185+
shell_init(shell_backend_uart_get_ptr(), usb_dev, cfg_flags, log_backend, level);
186+
187+
return 0;
188+
}
153189
#endif
154190

155191
int main(void)
156192
{
157193
loader(NULL);
158194
return 0;
159-
}
160-
195+
}

0 commit comments

Comments
 (0)