Skip to content

Commit 975e135

Browse files
committed
app: Enable UART application log by default
- Application logs are by default going to UART1, when the AT#XLOG=1 is given. - RTT application logs are disabled by default, but can be enabled in prj.conf if required. - UART1 is enabled by default, but suspended in startup. - overlay-trace-backend-uart.conf only includes AT#XTRACE. - overlay-trace-backend-uart.overlay is removed. Signed-off-by: Markus Lassila <markus.lassila@nordicsemi.no>
1 parent 1efc17e commit 975e135

14 files changed

Lines changed: 265 additions & 147 deletions

app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ target_sources(app PRIVATE src/sm_at_icmp.c)
2121
target_sources(app PRIVATE src/sm_at_fota.c)
2222
target_sources(app PRIVATE src/sm_at_dfu.c)
2323
target_sources(app PRIVATE src/sm_uart_handler.c)
24+
target_sources(app PRIVATE src/sm_log_uart.c)
2425
# NORDIC SDK APP END
2526
target_sources_ifdef(CONFIG_SM_SMS app PRIVATE src/sm_at_sms.c)
2627
target_sources_ifdef(CONFIG_SM_PPP app PRIVATE src/sm_ppp.c)

app/boards/nrf9151dk_nrf9151_ns.overlay

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
chosen {
99
ncs,sm-uart = &dtr_uart0;
1010
nordic,pm-ext-flash = &gd25wb256;
11+
zephyr,console = &uart1;
1112
};
1213
};
1314

@@ -69,3 +70,11 @@
6970
&i2c2 {
7071
status = "disabled";
7172
};
73+
74+
/* Enable UART1 as the shared backend for Zephyr logs and modem traces (VCOM1 on the DK).
75+
* Modem traces require 1 000 000 baud. Hardware flow control is not used.
76+
*/
77+
&uart1 {
78+
status = "okay";
79+
current-speed = <1000000>;
80+
};

app/boards/thingy91x_nrf9151_ns.overlay

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/ {
88
chosen {
99
ncs,sm-uart = &dtr_uart0;
10+
zephyr,console = &uart1;
1011
};
1112
};
1213

@@ -27,3 +28,11 @@
2728
/* Use PORT event for DTR (26) pin to save power */
2829
sense-edge-mask = <BIT(26)>;
2930
};
31+
32+
/* Enable UART1 as the shared backend for Zephyr logs and modem traces.
33+
* Modem traces require 1 000 000 baud. Hardware flow control is not used.
34+
*/
35+
&uart1 {
36+
status = "okay";
37+
current-speed = <1000000>;
38+
};

app/overlay-trace-backend-uart.conf

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
66

7-
# Enable UART logging backend - shared with nRF modem traces.
8-
CONFIG_UART_CONSOLE=y
9-
CONFIG_RTT_CONSOLE=n
10-
CONFIG_LOG_BACKEND_RTT=n
11-
# Log backend starts disabled; the UART is off until activated via AT#XLOG=1.
12-
CONFIG_LOG_BACKEND_UART=y
13-
CONFIG_LOG_BACKEND_UART_AUTOSTART=n
14-
157
# Enable UART modem trace backend - shared with the UART log backend.
168
CONFIG_SM_MODEM_TRACE_BACKEND_UART=y
179
CONFIG_NRF_MODEM_LIB_TRACE=y

app/overlay-trace-backend-uart.overlay

Lines changed: 0 additions & 14 deletions
This file was deleted.

app/prj.conf

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@ CONFIG_RING_BUFFER=y
1414
CONFIG_REBOOT=y
1515
CONFIG_EVENTFD=y
1616

17-
# Segger RTT
18-
CONFIG_USE_SEGGER_RTT=y
19-
# Where console messages (printk) are output.
20-
# By itself, SM does not output any.
21-
CONFIG_RTT_CONSOLE=y
22-
CONFIG_UART_CONSOLE=n
23-
# Where logs are output.
24-
CONFIG_LOG_BACKEND_RTT=y
25-
CONFIG_LOG_BACKEND_UART=n
17+
# Enable UART logging by default.
18+
CONFIG_UART_CONSOLE=y
19+
CONFIG_LOG_BACKEND_UART=y
20+
# Logging does not start until activated via AT#XLOG=1 and is deactivated with AT#XLOG=0.
21+
CONFIG_LOG_BACKEND_UART_AUTOSTART=n
22+
23+
# Disable Segger RTT logging by default.
24+
CONFIG_USE_SEGGER_RTT=n
25+
CONFIG_RTT_CONSOLE=n
26+
CONFIG_LOG_BACKEND_RTT=n
2627
# Increase the buffer so that all Serial Modem boot logs fit into the buffer.
27-
# Because J-Link RTT Viewer has to be connected after the bootup,
28-
# only logs that fit into the buffer are shown. This is especially useful in showing
29-
# errors if Serial Modem does not start properly.
30-
CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=2048
28+
#CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=2048
3129

3230
# Network
3331
CONFIG_NETWORKING=y

app/sample.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tests:
66
build_only: true
77
extra_args:
88
- EXTRA_CONF_FILE="overlay-cmux.conf;overlay-ppp.conf;overlay-trace-backend-uart.conf;overlay-nrf-device-provisioning.conf"
9-
- EXTRA_DTC_OVERLAY_FILE="overlay-nrf91m1.overlay;overlay-trace-backend-uart.overlay"
9+
- EXTRA_DTC_OVERLAY_FILE="overlay-nrf91m1.overlay"
1010
extra_configs:
1111
- CONFIG_SM_LOG_LEVEL_DBG=y
1212
platform_allow:

app/src/sm_log_uart.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (c) 2026 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
/*
8+
* AT#XLOG command — enable/disable the Zephyr UART log backend at runtime.
9+
*
10+
* The UART (zephyr,console) is shared with the modem-trace backend
11+
* (sm_trace_backend_uart.c). AT#XLOG and AT#XTRACE are mutually exclusive:
12+
* each refuses to activate while the other is in use.
13+
*
14+
*/
15+
16+
#include <zephyr/devicetree.h>
17+
18+
/* Only compiled if zephyr,console is present and active in the devicetree. */
19+
#if DT_HAS_CHOSEN(zephyr_console) && DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_console), okay)
20+
21+
#include <zephyr/drivers/uart.h>
22+
#include <zephyr/kernel.h>
23+
#include <zephyr/logging/log.h>
24+
#include <zephyr/logging/log_ctrl.h>
25+
#include <zephyr/pm/device.h>
26+
27+
#include "sm_at_host.h"
28+
29+
LOG_MODULE_REGISTER(sm_log_uart, CONFIG_SM_LOG_LEVEL);
30+
31+
/* Zephyr console UART is used both for application logs and modem traces. */
32+
#define UART_DEVICE_NODE DT_CHOSEN(zephyr_console)
33+
static const struct device *const uart_dev = DEVICE_DT_GET(UART_DEVICE_NODE);
34+
35+
static bool log_active;
36+
37+
static int uart_suspend(void)
38+
{
39+
int ret = pm_device_action_run(uart_dev, PM_DEVICE_ACTION_SUSPEND);
40+
41+
if (ret && ret != -EALREADY) {
42+
LOG_ERR("Failed to %s UART device: %d", "suspend", ret);
43+
return ret;
44+
}
45+
46+
return 0;
47+
}
48+
49+
static int uart_resume(void)
50+
{
51+
int ret = pm_device_action_run(uart_dev, PM_DEVICE_ACTION_RESUME);
52+
53+
if (ret && ret != -EALREADY) {
54+
LOG_ERR("Failed to %s UART device: %d", "resume", ret);
55+
return ret;
56+
}
57+
return 0;
58+
}
59+
60+
static bool uart_is_active(void)
61+
{
62+
enum pm_device_state state = PM_DEVICE_STATE_OFF;
63+
int err = pm_device_state_get(uart_dev, &state);
64+
65+
if (err) {
66+
LOG_ERR("Failed to get UART device state (%d).", err);
67+
return false;
68+
}
69+
return state == PM_DEVICE_STATE_ACTIVE;
70+
}
71+
72+
SM_AT_CMD_CUSTOM(xlog, "AT#XLOG", handle_at_log);
73+
STATIC int handle_at_log(enum at_parser_cmd_type cmd_type, struct at_parser *parser, uint32_t)
74+
{
75+
const struct log_backend *log_be = log_backend_get_by_name("log_backend_uart");
76+
77+
if (!log_be) {
78+
return -ENODEV;
79+
}
80+
81+
if (cmd_type == AT_PARSER_CMD_TYPE_SET) {
82+
int mode;
83+
int ret = at_parser_num_get(parser, 1, &mode);
84+
85+
if (ret || (mode < 0) || (mode > 1)) {
86+
return -EINVAL;
87+
}
88+
89+
if (mode == (int)log_active) {
90+
return 0;
91+
}
92+
93+
if (mode == 1 && uart_is_active()) {
94+
return -EBUSY;
95+
}
96+
97+
if (mode == 1) {
98+
ret = uart_resume();
99+
if (ret) {
100+
return ret;
101+
}
102+
if (!log_be->cb->initialized) {
103+
log_backend_init(log_be);
104+
}
105+
log_backend_enable(log_be, log_be->cb->ctx, CONFIG_LOG_DEFAULT_LEVEL);
106+
log_active = true;
107+
} else {
108+
log_backend_disable(log_be);
109+
ret = uart_suspend();
110+
if (ret) {
111+
return ret;
112+
}
113+
log_active = false;
114+
}
115+
return 0;
116+
} else if (cmd_type == AT_PARSER_CMD_TYPE_READ) {
117+
rsp_send("\r\n#XLOG: %d\r\n", (int)log_active);
118+
return 0;
119+
} else if (cmd_type == AT_PARSER_CMD_TYPE_TEST) {
120+
rsp_send("\r\n#XLOG: (0,1)\r\n");
121+
return 0;
122+
}
123+
124+
return -EINVAL;
125+
}
126+
127+
static int sm_log_uart_init(void)
128+
{
129+
if (!IS_ENABLED(CONFIG_LOG_BACKEND_UART) ||
130+
!IS_ENABLED(CONFIG_LOG_BACKEND_UART_AUTOSTART)) {
131+
/* Start with UART log backend disabled. */
132+
if (uart_suspend()) {
133+
LOG_ERR("Failed to suspend UART log backend");
134+
sm_init_failed = true;
135+
return -EFAULT;
136+
}
137+
} else {
138+
log_active = true;
139+
}
140+
141+
return 0;
142+
}
143+
144+
SYS_INIT(sm_log_uart_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY);
145+
146+
#endif /* DT_HAS_CHOSEN(zephyr_console) */

0 commit comments

Comments
 (0)