1 /*
2 * Copyright (c) 2018 Makaio GmbH
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/shell/shell_rtt.h>
8 #include <zephyr/init.h>
9 #include <SEGGER_RTT.h>
10 #include <zephyr/logging/log.h>
11
12 #define RTT_LOCK() \
13 COND_CODE_0(CONFIG_SHELL_BACKEND_RTT_BUFFER, (SEGGER_RTT_LOCK()), ())
14
15 #define RTT_UNLOCK() \
16 COND_CODE_0(CONFIG_SHELL_BACKEND_RTT_BUFFER, (SEGGER_RTT_UNLOCK()), ())
17
18 #if defined(CONFIG_LOG_BACKEND_RTT)
19 BUILD_ASSERT(!(CONFIG_SHELL_BACKEND_RTT_BUFFER == CONFIG_LOG_BACKEND_RTT_BUFFER),
20 "Conflicting log RTT backend enabled on the same channel");
21 #endif
22
23 static uint8_t shell_rtt_up_buf[CONFIG_SEGGER_RTT_BUFFER_SIZE_UP];
24 static uint8_t shell_rtt_down_buf[CONFIG_SEGGER_RTT_BUFFER_SIZE_DOWN];
25
26 SHELL_RTT_DEFINE(shell_transport_rtt);
27 SHELL_DEFINE(shell_rtt, CONFIG_SHELL_PROMPT_RTT, &shell_transport_rtt,
28 CONFIG_SHELL_BACKEND_RTT_LOG_MESSAGE_QUEUE_SIZE,
29 CONFIG_SHELL_BACKEND_RTT_LOG_MESSAGE_QUEUE_TIMEOUT,
30 SHELL_FLAG_OLF_CRLF);
31
32 LOG_MODULE_REGISTER(shell_rtt, CONFIG_SHELL_RTT_LOG_LEVEL);
33
34 static bool panic_mode;
35 static bool host_present;
36
timer_handler(struct k_timer * timer)37 static void timer_handler(struct k_timer *timer)
38 {
39 const struct shell_rtt *sh_rtt = k_timer_user_data_get(timer);
40
41 if (SEGGER_RTT_HasData(CONFIG_SHELL_BACKEND_RTT_BUFFER)) {
42 sh_rtt->handler(SHELL_TRANSPORT_EVT_RX_RDY, sh_rtt->context);
43 }
44 }
45
init(const struct shell_transport * transport,const void * config,shell_transport_handler_t evt_handler,void * context)46 static int init(const struct shell_transport *transport,
47 const void *config,
48 shell_transport_handler_t evt_handler,
49 void *context)
50 {
51 struct shell_rtt *sh_rtt = (struct shell_rtt *)transport->ctx;
52
53 sh_rtt->handler = evt_handler;
54 sh_rtt->context = context;
55
56 k_timer_init(&sh_rtt->timer, timer_handler, NULL);
57 k_timer_user_data_set(&sh_rtt->timer, (void *)sh_rtt);
58 k_timer_start(&sh_rtt->timer, K_MSEC(CONFIG_SHELL_RTT_RX_POLL_PERIOD),
59 K_MSEC(CONFIG_SHELL_RTT_RX_POLL_PERIOD));
60
61 if (CONFIG_SHELL_BACKEND_RTT_BUFFER > 0) {
62 SEGGER_RTT_ConfigUpBuffer(CONFIG_SHELL_BACKEND_RTT_BUFFER, "Shell",
63 shell_rtt_up_buf, sizeof(shell_rtt_up_buf),
64 SEGGER_RTT_MODE_NO_BLOCK_SKIP);
65
66 SEGGER_RTT_ConfigDownBuffer(CONFIG_SHELL_BACKEND_RTT_BUFFER, "Shell",
67 shell_rtt_down_buf, sizeof(shell_rtt_down_buf),
68 SEGGER_RTT_MODE_NO_BLOCK_SKIP);
69 }
70
71 return 0;
72 }
73
uninit(const struct shell_transport * transport)74 static int uninit(const struct shell_transport *transport)
75 {
76 struct shell_rtt *sh_rtt = (struct shell_rtt *)transport->ctx;
77
78 k_timer_stop(&sh_rtt->timer);
79
80 return 0;
81 }
82
enable(const struct shell_transport * transport,bool blocking)83 static int enable(const struct shell_transport *transport, bool blocking)
84 {
85 struct shell_rtt *sh_rtt = (struct shell_rtt *)transport->ctx;
86
87 if (blocking) {
88 if (IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) && IS_ENABLED(CONFIG_SHELL_LOG_BACKEND)) {
89 panic_mode = true;
90 }
91 k_timer_stop(&sh_rtt->timer);
92 }
93
94 return 0;
95 }
96
is_panic_mode(void)97 static inline bool is_panic_mode(void)
98 {
99 return panic_mode;
100 }
101
is_sync_mode(void)102 static inline bool is_sync_mode(void)
103 {
104 return (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) && IS_ENABLED(CONFIG_SHELL_LOG_BACKEND)) ||
105 is_panic_mode();
106 }
107
on_failed_write(int retry_cnt)108 static void on_failed_write(int retry_cnt)
109 {
110 if (retry_cnt == 0) {
111 host_present = false;
112 } else if (is_sync_mode()) {
113 k_busy_wait(USEC_PER_MSEC *
114 CONFIG_SHELL_BACKEND_RTT_RETRY_DELAY_MS);
115 } else {
116 k_msleep(CONFIG_SHELL_BACKEND_RTT_RETRY_DELAY_MS);
117 }
118 }
119
on_write(int retry_cnt)120 static void on_write(int retry_cnt)
121 {
122 host_present = true;
123 if (is_panic_mode()) {
124 /* In panic mode block on each write until host reads it. This
125 * way it is ensured that if system resets all messages are read
126 * by the host. While pending on data being read by the host we
127 * must also detect situation where host is disconnected.
128 */
129 while (SEGGER_RTT_HasDataUp(CONFIG_SHELL_BACKEND_RTT_BUFFER) &&
130 host_present) {
131 on_failed_write(retry_cnt--);
132 }
133 }
134
135 }
136
write(const struct shell_transport * transport,const void * data,size_t length,size_t * cnt)137 static int write(const struct shell_transport *transport,
138 const void *data, size_t length, size_t *cnt)
139 {
140 struct shell_rtt *sh_rtt = (struct shell_rtt *)transport->ctx;
141 int ret = 0;
142 int retry_cnt = CONFIG_SHELL_BACKEND_RTT_RETRY_CNT;
143
144 do {
145 if (!is_sync_mode()) {
146 RTT_LOCK();
147 ret = SEGGER_RTT_WriteSkipNoLock(CONFIG_SHELL_BACKEND_RTT_BUFFER,
148 data, length);
149 RTT_UNLOCK();
150 } else {
151 ret = SEGGER_RTT_WriteSkipNoLock(CONFIG_SHELL_BACKEND_RTT_BUFFER,
152 data, length);
153 }
154
155 if (ret) {
156 on_write(retry_cnt);
157 } else if (host_present) {
158 retry_cnt--;
159 on_failed_write(retry_cnt);
160 } else {
161 }
162 } while ((ret == 0) && host_present);
163
164 sh_rtt->handler(SHELL_TRANSPORT_EVT_TX_RDY, sh_rtt->context);
165 *cnt = length;
166
167 return 0;
168 }
169
read(const struct shell_transport * transport,void * data,size_t length,size_t * cnt)170 static int read(const struct shell_transport *transport,
171 void *data, size_t length, size_t *cnt)
172 {
173 *cnt = SEGGER_RTT_Read(CONFIG_SHELL_BACKEND_RTT_BUFFER, data, length);
174
175 return 0;
176 }
177
178 const struct shell_transport_api shell_rtt_transport_api = {
179 .init = init,
180 .uninit = uninit,
181 .enable = enable,
182 .write = write,
183 .read = read
184 };
185
enable_shell_rtt(void)186 static int enable_shell_rtt(void)
187 {
188 bool log_backend = CONFIG_SHELL_RTT_INIT_LOG_LEVEL > 0;
189 uint32_t level = (CONFIG_SHELL_RTT_INIT_LOG_LEVEL > LOG_LEVEL_DBG) ?
190 CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_RTT_INIT_LOG_LEVEL;
191 static const struct shell_backend_config_flags cfg_flags =
192 SHELL_DEFAULT_BACKEND_CONFIG_FLAGS;
193
194 shell_init(&shell_rtt, NULL, cfg_flags, log_backend, level);
195
196 return 0;
197 }
198
199 /* Function is used for testing purposes */
shell_backend_rtt_get_ptr(void)200 const struct shell *shell_backend_rtt_get_ptr(void)
201 {
202 return &shell_rtt;
203 }
204 SYS_INIT(enable_shell_rtt, POST_KERNEL, 0);
205