aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.c')
-rw-r--r--lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.c b/lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.c
new file mode 100644
index 000000000..253a2f1fc
--- /dev/null
+++ b/lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.c
@@ -0,0 +1,116 @@
1/*
2 ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio
3 Copyright (C) 2019 Diego Ismirlian, (dismirlian(at)google's mail)
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16*/
17
18#include "hal.h"
19#include "SEGGER_RTT_streams.h"
20
21RTTDriver RTTD0;
22static bool rtt_global_init;
23
24static size_t _write(RTTDriver *rttdp, const uint8_t *bp, size_t n) {
25 return SEGGER_RTT_Write(rttdp->up_buffer_index, bp, n);
26}
27
28static size_t _read(RTTDriver *rttdp, uint8_t *bp, size_t n) {
29 (void)rttdp, (void)bp, (void)n;
30 /* TODO: implement */
31 return 0;
32}
33
34static msg_t _put(RTTDriver *rttdp, uint8_t b) {
35 if (SEGGER_RTT_PutChar(rttdp->up_buffer_index, b) == 1) {
36 return MSG_OK;
37 }
38 return MSG_TIMEOUT;
39}
40
41static msg_t _get(RTTDriver *rttdp) {
42 (void)rttdp;
43 /* TODO: implement */
44 return MSG_TIMEOUT;
45}
46
47static const struct RTTDriverVMT vmt = {
48 (size_t)0,
49 (size_t (*)(void *, const uint8_t *, size_t))_write,
50 (size_t (*)(void *, uint8_t *, size_t))_read,
51 (msg_t (*)(void *, uint8_t))_put,
52 (msg_t (*)(void *))_get,
53};
54
55static inline void _object_init(RTTDriver *rttdp) {
56 rttdp->state = RTT_STATE_READY;
57 rttdp->vmt = &vmt;
58}
59
60void rttInit(void) {
61 osalDbgAssert(rtt_global_init == false, "double init");
62 SEGGER_RTT_LOCK();
63 SEGGER_RTT_Init();
64 RTTD0.up_buffer_index = 0;
65 RTTD0.down_buffer_index = 0;
66 _object_init(&RTTD0);
67 rtt_global_init = true;
68 RTTD0.state = RTT_STATE_READY;
69 SEGGER_RTT_UNLOCK();
70}
71
72void rttObjectInit(RTTDriver *rttdp, const RTTConfig *cfg) {
73 osalDbgCheck(rttdp);
74 osalDbgAssert(rtt_global_init, "uninitialized");
75 osalDbgAssert(rttdp != &RTTD0, "RTTD0 is automatically initialized on rttInit");
76
77 int idx;
78
79 SEGGER_RTT_LOCK();
80 if (cfg->down.size) {
81 idx = SEGGER_RTT_AllocDownBuffer(cfg->name, cfg->down.buff, cfg->down.size, cfg->down.flags);
82 osalDbgAssert(idx > 0, "can't alloc down buffer");
83 rttdp->down_buffer_index = (unsigned)idx;
84 } else {
85 rttdp->down_buffer_index = 0;
86 }
87
88 if (cfg->up.size) {
89 idx = SEGGER_RTT_AllocUpBuffer(cfg->name, cfg->up.buff, cfg->up.size, cfg->up.flags);
90 osalDbgAssert(idx > 0, "can't alloc up buffer");
91 rttdp->up_buffer_index = (unsigned)idx;
92 } else {
93 rttdp->up_buffer_index = 0;
94 }
95
96 _object_init(rttdp);
97 SEGGER_RTT_UNLOCK();
98}
99
100void rttSetUpFlags(RTTDriver *rttdp, rtt_mode_flags_t flags) {
101 int ret = SEGGER_RTT_SetFlagsUpBuffer(rttdp->up_buffer_index, (unsigned)flags);
102 osalDbgAssert(ret >= 0, "error");
103}
104
105void rttSetDownFlags(RTTDriver *rttdp, rtt_mode_flags_t flags) {
106 int ret = SEGGER_RTT_SetFlagsDownBuffer(rttdp->down_buffer_index, (unsigned)flags);
107 osalDbgAssert(ret >= 0, "error");
108}
109
110void rttStart(RTTDriver *rttdp) {
111 osalDbgCheck(rttdp);
112 osalDbgAssert(rtt_global_init, "uninitialized");
113 osalDbgAssert((rttdp->state == RTT_STATE_ACTIVE)
114 || (rttdp->state == RTT_STATE_READY), "wrong state");
115 rttdp->state = RTT_STATE_READY;
116}