STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
rtt_log.c
Go to the documentation of this file.
1#include "rtt_log.h"
2
3#ifdef STAR_BENCH /* the flight build carries no RTT block */
4
5#include <string.h>
6
7/*
8 * A minimal SEGGER-RTT-compatible up buffer: a control block in RAM that
9 * OpenOCD finds by its "SEGGER RTT" tag and drains over SWD while the core
10 * runs (OpenOCD `rtt setup` / `rtt server start`, see scripts/serial.sh).
11 * One up channel, no down channel. Non-blocking: when the host is not
12 * reading, the bytes that do not fit are dropped.
13 */
14
15#define RTT_UP_SIZE 2048U
16
17typedef struct {
18 const char* name;
19 char* buffer;
20 uint32_t size;
21 volatile uint32_t wr; /* written by the target */
22 volatile uint32_t rd; /* written by the host */
23 uint32_t flags; /* 0: skip when full */
24} rtt_buffer_t;
25
26typedef struct {
27 char id[16];
28 int32_t max_up, max_down;
29 rtt_buffer_t up[1];
30 rtt_buffer_t down[1];
31} rtt_cb_t;
32
33static char up_buffer[RTT_UP_SIZE];
34rtt_cb_t _SEGGER_RTT; /* the name OpenOCD's docs and tools look for */
35
36void rtt_log_init(void) {
37 _SEGGER_RTT.max_up = 1;
38 _SEGGER_RTT.max_down = 1;
39 _SEGGER_RTT.up[0] =
40 (rtt_buffer_t){"Terminal", up_buffer, RTT_UP_SIZE, 0, 0, 0};
41 _SEGGER_RTT.down[0] = (rtt_buffer_t){"Terminal", NULL, 0, 0, 0, 0};
42 /* The tag goes in last, so the host never finds a half-built block. */
43 __asm volatile("dmb" ::: "memory");
44 memcpy(_SEGGER_RTT.id, "SEGGER RTT", 11);
45 __asm volatile("dmb" ::: "memory");
46}
47
48uint32_t rtt_log_write(const char* s, uint32_t n) {
49 rtt_buffer_t* b = &_SEGGER_RTT.up[0];
50 uint32_t wr = b->wr, rd = b->rd, done = 0;
51 while (done < n) {
52 uint32_t next = (wr + 1U) % b->size;
53 if (next == rd) {
54 break; /* full: drop the rest */
55 }
56 b->buffer[wr] = s[done++];
57 wr = next;
58 }
59 __asm volatile("dmb" ::: "memory");
60 b->wr = wr;
61 return done;
62}
63#endif /* STAR_BENCH */
#define NULL
Definition usbd_def.h:44
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
Definition json.hpp:13878
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474
void rtt_log_init(void)
uint32_t rtt_log_write(const char *s, uint32_t n)