Skip to content

Commit

Permalink
nrf_modem_lib: trace_backends: rtt: early alloc buf
Browse files Browse the repository at this point in the history
Allocate the modem trace buffer for the RTT backend on boot, instead of
when `nrf_modem_lib_init` is called. This allows the user to delay the
call to `nrf_modem_lib_init`, giving them a chance to attach to the RTT
channel before the modem starts.

Without this change the start of trace logs from the RTT backend are
invariably lost, as the user cannot attach until after data generation
has started.

Update tests to work with the move of `SEGGER_RTT_AllocUpBuffer` into
a `SYS_INIT` function.

Signed-off-by: Jordan Yates <[email protected]>
  • Loading branch information
JordanYates authored and nordicjm committed Aug 22, 2024
1 parent f869800 commit 9c52e3d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ Modem libraries

* :ref:`nrf_modem_lib_readme` library:

* Updated the RTT trace backend to allocate the RTT channel at boot, instead of when the modem is activated.
* Removed support for deprecated RAI socket options ``SO_RAI_LAST``, ``SO_RAI_NO_DATA``, ``SO_RAI_ONE_RESP``, ``SO_RAI_ONGOING``, and ``SO_RAI_WAIT_MORE``.

Multiprotocol Service Layer libraries
Expand Down
26 changes: 18 additions & 8 deletions lib/nrf_modem_lib/trace_backends/rtt/rtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <stdio.h>
#include <zephyr/init.h>
#include <zephyr/logging/log.h>
#include <modem/trace_backend.h>
#include <SEGGER_RTT.h>
Expand All @@ -18,21 +19,15 @@ static trace_backend_processed_cb trace_processed_callback;

int trace_backend_init(trace_backend_processed_cb trace_processed_cb)
{
const int segger_rtt_mode = SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL;

if (trace_processed_cb == NULL) {
return -EFAULT;
}

trace_processed_callback = trace_processed_cb;

trace_rtt_channel = SEGGER_RTT_AllocUpBuffer("modem_trace", rtt_buffer, sizeof(rtt_buffer),
segger_rtt_mode);

if (trace_rtt_channel <= 0) {
return -EBUSY;
}

trace_processed_callback = trace_processed_cb;

return 0;
}

Expand Down Expand Up @@ -72,3 +67,18 @@ struct nrf_modem_lib_trace_backend trace_backend = {
.deinit = trace_backend_deinit,
.write = trace_backend_write,
};

/* Allocate RTT buffer ASAP so external tooling can attach before `nrf_modem_init` */
IF_DISABLED(CONFIG_UNITY, (static))
int nrf_modem_lib_trace_rtt_channel_alloc(void)
{
const int segger_rtt_mode = SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL;

trace_rtt_channel = SEGGER_RTT_AllocUpBuffer("modem_trace", rtt_buffer, sizeof(rtt_buffer),
segger_rtt_mode);

return trace_rtt_channel < 0 ? -EBUSY : 0;
}

/* The RTT core initialises in PRE_KERNEL_1 */
SYS_INIT(nrf_modem_lib_trace_rtt_channel_alloc, POST_KERNEL, 0);
10 changes: 9 additions & 1 deletion tests/lib/nrf_modem_lib/trace_backends/rtt/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#include "trace_backend.h"

/* SYS_INIT function */
int nrf_modem_lib_trace_rtt_channel_alloc(void);

extern struct nrf_modem_lib_trace_backend trace_backend;

#include "cmock_SEGGER_RTT.h"
Expand Down Expand Up @@ -51,8 +54,10 @@ void test_trace_backend_init_rtt(void)
__cmock_SEGGER_RTT_AllocUpBuffer_ExpectAnyArgsAndReturn(trace_rtt_channel);
__cmock_SEGGER_RTT_AllocUpBuffer_AddCallback(&rtt_allocupbuffer_callback);

ret = trace_backend.init(callback);
ret = nrf_modem_lib_trace_rtt_channel_alloc();
TEST_ASSERT_EQUAL(0, ret);

ret = trace_backend.init(callback);
TEST_ASSERT_EQUAL(0, ret);
}

Expand All @@ -63,6 +68,9 @@ void test_trace_backend_init_rtt_ebusy(void)
/* Simulate failure by returning negative RTT channel. */
__cmock_SEGGER_RTT_AllocUpBuffer_ExpectAnyArgsAndReturn(-1);

ret = nrf_modem_lib_trace_rtt_channel_alloc();
TEST_ASSERT_EQUAL(-EBUSY, ret);

ret = trace_backend.init(callback);
TEST_ASSERT_EQUAL(-EBUSY, ret);
}
Expand Down

0 comments on commit 9c52e3d

Please sign in to comment.