diff options
Diffstat (limited to 'lib/chibios-contrib/os/various/segger_bindings/RTT')
-rw-r--r-- | lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.c | 116 | ||||
-rw-r--r-- | lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.h | 107 |
2 files changed, 223 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 | |||
21 | RTTDriver RTTD0; | ||
22 | static bool rtt_global_init; | ||
23 | |||
24 | static 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 | |||
28 | static 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 | |||
34 | static 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 | |||
41 | static msg_t _get(RTTDriver *rttdp) { | ||
42 | (void)rttdp; | ||
43 | /* TODO: implement */ | ||
44 | return MSG_TIMEOUT; | ||
45 | } | ||
46 | |||
47 | static 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 | |||
55 | static inline void _object_init(RTTDriver *rttdp) { | ||
56 | rttdp->state = RTT_STATE_READY; | ||
57 | rttdp->vmt = &vmt; | ||
58 | } | ||
59 | |||
60 | void 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 | |||
72 | void 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 | |||
100 | void 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 | |||
105 | void 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 | |||
110 | void 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 | } | ||
diff --git a/lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.h b/lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.h new file mode 100644 index 000000000..3ee679cb9 --- /dev/null +++ b/lib/chibios-contrib/os/various/segger_bindings/RTT/SEGGER_RTT_streams.h | |||
@@ -0,0 +1,107 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio | ||
3 | Copyright (C) 2015..2017 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 | #ifndef SEGGER_RTT_streams_H_ | ||
19 | #define SEGGER_RTT_streams_H_ | ||
20 | |||
21 | #include "hal.h" | ||
22 | #include "SEGGER_RTT.h" | ||
23 | |||
24 | /*===========================================================================*/ | ||
25 | /* Driver pre-compile time settings. */ | ||
26 | /*===========================================================================*/ | ||
27 | |||
28 | /*===========================================================================*/ | ||
29 | /* Derived constants and error checks. */ | ||
30 | /*===========================================================================*/ | ||
31 | |||
32 | /*===========================================================================*/ | ||
33 | /* Driver data structures and types. */ | ||
34 | /*===========================================================================*/ | ||
35 | |||
36 | typedef enum { | ||
37 | RTT_STATE_UNINIT = 0, | ||
38 | RTT_STATE_STOP = 1, | ||
39 | RTT_STATE_ACTIVE = 2, | ||
40 | RTT_STATE_READY = 3 | ||
41 | } rtt_state_t; | ||
42 | |||
43 | typedef enum { | ||
44 | RTT_MODE_FLAGS_NO_BLOCK_SKIP = SEGGER_RTT_MODE_NO_BLOCK_SKIP, | ||
45 | RTT_MODE_FLAGS_NO_BLOCK_TRIM = SEGGER_RTT_MODE_NO_BLOCK_TRIM, | ||
46 | RTT_MODE_FLAGS_BLOCK_IF_FIFO_FULL = SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL, | ||
47 | } rtt_mode_flags_t; | ||
48 | |||
49 | #define _rtt_driver_methods \ | ||
50 | _base_sequential_stream_methods | ||
51 | |||
52 | struct RTTDriverVMT { | ||
53 | _rtt_driver_methods | ||
54 | }; | ||
55 | |||
56 | typedef struct RTTDriver RTTDriver; | ||
57 | typedef struct RTTConfig RTTConfig; | ||
58 | typedef struct RTTBufferConfig RTTBufferConfig; | ||
59 | |||
60 | struct RTTDriver { | ||
61 | /* inherited from abstract asyncrhonous channel driver */ | ||
62 | const struct RTTDriverVMT *vmt; | ||
63 | _base_sequential_stream_data | ||
64 | |||
65 | rtt_state_t state; | ||
66 | unsigned int up_buffer_index; | ||
67 | unsigned int down_buffer_index; | ||
68 | }; | ||
69 | |||
70 | struct RTTBufferConfig { | ||
71 | void *buff; | ||
72 | unsigned int size; | ||
73 | rtt_mode_flags_t flags; | ||
74 | }; | ||
75 | |||
76 | struct RTTConfig { | ||
77 | const char *name; | ||
78 | RTTBufferConfig up; | ||
79 | RTTBufferConfig down; | ||
80 | }; | ||
81 | |||
82 | /*===========================================================================*/ | ||
83 | /* Driver macros. */ | ||
84 | /*===========================================================================*/ | ||
85 | #define rttGetState(rttdp) ((rttdp)->state) | ||
86 | #define rttGetUpBufferIndex(rttdp) ((rttdp)->up_buffer_index) | ||
87 | #define rttGetDownBufferIndex(rttdp) ((rttdp)->down_buffer_index) | ||
88 | |||
89 | /*===========================================================================*/ | ||
90 | /* External declarations. */ | ||
91 | /*===========================================================================*/ | ||
92 | extern RTTDriver RTTD0; | ||
93 | |||
94 | #ifdef __cplusplus | ||
95 | extern "C" { | ||
96 | #endif | ||
97 | /* RTT device driver */ | ||
98 | void rttInit(void); | ||
99 | void rttObjectInit(RTTDriver *rttdp, const RTTConfig *cfg); | ||
100 | void rttStart(RTTDriver *rttdp); | ||
101 | void rttSetUpFlags(RTTDriver *rttdp, rtt_mode_flags_t flags); | ||
102 | void rttSetDownFlags(RTTDriver *rttdp, rtt_mode_flags_t flags); | ||
103 | #ifdef __cplusplus | ||
104 | } | ||
105 | #endif | ||
106 | |||
107 | #endif /* SEGGER_RTT_streams_H_ */ | ||