uint64_t->target_addr_t for stack pointers.
[openocd.git] / src / rtt / rtt.h
blobbc21bd012890710753ff2ede1f45eaac5e056536
1 /*
2 * Copyright (C) 2016-2020 by Marc Schink <dev@zapb.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef OPENOCD_RTT_RTT_H
19 #define OPENOCD_RTT_RTT_H
21 #include <stdint.h>
22 #include <stdbool.h>
24 #include <helper/command.h>
25 #include <target/target.h>
27 /**
28 * Control block ID length in bytes, including the trailing null-terminator.
30 #define RTT_CB_MAX_ID_LENGTH 16
32 /* Control block size in bytes. */
33 #define RTT_CB_SIZE (RTT_CB_MAX_ID_LENGTH + 2 * sizeof(uint32_t))
35 /* Channel structure size in bytes. */
36 #define RTT_CHANNEL_SIZE 24
38 /* Minimal channel buffer size in bytes. */
39 #define RTT_CHANNEL_BUFFER_MIN_SIZE 2
41 /** RTT control block. */
42 struct rtt_control {
43 /** Control block address on the target. */
44 target_addr_t address;
45 /** Control block identifier, including trailing null-terminator. */
46 char id[RTT_CB_MAX_ID_LENGTH];
47 /** Maximum number of up-channels. */
48 uint32_t num_up_channels;
49 /** Maximum number of down-channels. */
50 uint32_t num_down_channels;
53 /** RTT channel. */
54 struct rtt_channel {
55 /** Channel structure address on the target. */
56 target_addr_t address;
57 /** Channel name address on the target. */
58 uint32_t name_addr;
59 /** Buffer address on the target. */
60 uint32_t buffer_addr;
61 /** Channel buffer size in bytes. */
62 uint32_t size;
63 /** Write position within the buffer in bytes. */
64 uint32_t write_pos;
65 /** Read position within the buffer in bytes. */
66 uint32_t read_pos;
67 /**
68 * Buffer flags.
70 * @note: Not used at the moment.
72 uint32_t flags;
75 /** RTT channel information. */
76 struct rtt_channel_info {
77 /** Channel name. */
78 char *name;
79 /** Length of the name in bytes, including the trailing null-terminator. */
80 size_t name_length;
81 /** Buffer size in bytes. */
82 uint32_t size;
83 /**
84 * Buffer flags.
86 * @note: Not used at the moment.
88 uint32_t flags;
91 typedef int (*rtt_sink_read)(unsigned int channel, const uint8_t *buffer,
92 size_t length, void *user_data);
94 struct rtt_sink_list {
95 rtt_sink_read read;
96 void *user_data;
98 struct rtt_sink_list *next;
101 /** Channel type. */
102 enum rtt_channel_type {
103 /** Up channel (target to host). */
104 RTT_CHANNEL_TYPE_UP,
105 /** Down channel (host to target). */
106 RTT_CHANNEL_TYPE_DOWN
109 typedef int (*rtt_source_find_ctrl_block)(struct target *target,
110 target_addr_t *address, size_t size, const char *id, bool *found,
111 void *user_data);
112 typedef int (*rtt_source_read_ctrl_block)(struct target *target,
113 target_addr_t address, struct rtt_control *ctrl_block,
114 void *user_data);
115 typedef int (*rtt_source_read_channel_info)(struct target *target,
116 const struct rtt_control *ctrl, unsigned int channel,
117 enum rtt_channel_type type, struct rtt_channel_info *info,
118 void *user_data);
119 typedef int (*rtt_source_start)(struct target *target,
120 const struct rtt_control *ctrl, void *user_data);
121 typedef int (*rtt_source_stop)(struct target *target, void *user_data);
122 typedef int (*rtt_source_read)(struct target *target,
123 const struct rtt_control *ctrl, struct rtt_sink_list **sinks,
124 size_t num_channels, void *user_data);
125 typedef int (*rtt_source_write)(struct target *target,
126 struct rtt_control *ctrl, unsigned int channel,
127 const uint8_t *buffer, size_t *length, void *user_data);
129 /** RTT source. */
130 struct rtt_source {
131 rtt_source_find_ctrl_block find_cb;
132 rtt_source_read_ctrl_block read_cb;
133 rtt_source_read_channel_info read_channel_info;
134 rtt_source_start start;
135 rtt_source_stop stop;
136 rtt_source_read read;
137 rtt_source_write write;
141 * Initialize Real-Time Transfer (RTT).
143 * @returns ERROR_OK on success, an error code on failure.
145 int rtt_init(void);
148 * Shutdown Real-Time Transfer (RTT).
150 * @returns ERROR_OK on success, an error code on failure.
152 int rtt_exit(void);
155 * Register an RTT source for a target.
157 * @param[in] source RTT source.
158 * @param[in,out] target Target.
160 * @returns ERROR_OK on success, an error code on failure.
162 int rtt_register_source(const struct rtt_source source,
163 struct target *target);
166 * Setup RTT.
168 * @param[in] address Start address to search for the control block.
169 * @param[in] size Size of the control block search area.
170 * @param[in] id Identifier of the control block. Must be null-terminated.
172 * @returns ERROR_OK on success, an error code on failure.
174 int rtt_setup(target_addr_t address, size_t size, const char *id);
177 * Start Real-Time Transfer (RTT).
179 * @returns ERROR_OK on success, an error code on failure.
181 int rtt_start(void);
184 * Stop Real-Time Transfer (RTT).
186 * @returns ERROR_OK on success, an error code on failure.
188 int rtt_stop(void);
191 * Get the polling interval.
193 * @param[out] interval Polling interval in milliseconds.
195 * @returns ERROR_OK on success, an error code on failure.
197 int rtt_get_polling_interval(unsigned int *interval);
200 * Set the polling interval.
202 * @param[in] interval Polling interval in milliseconds.
204 * @returns ERROR_OK on success, an error code on failure.
206 int rtt_set_polling_interval(unsigned int interval);
209 * Get whether RTT is started.
211 * @returns Whether RTT is started.
213 bool rtt_started(void);
216 * Get whether RTT is configured.
218 * @returns Whether RTT is configured.
220 bool rtt_configured(void);
223 * Get whether RTT control block was found.
225 * @returns Whether RTT was found.
227 bool rtt_found_cb(void);
230 * Get the RTT control block.
232 * @returns The RTT control block.
234 const struct rtt_control *rtt_get_control(void);
237 * Read channel information.
239 * @param[in] channel_index Channel index.
240 * @param[in] type Channel type.
241 * @param[out] info Channel information.
243 * @returns ERROR_OK on success, an error code on failure.
245 int rtt_read_channel_info(unsigned int channel_index,
246 enum rtt_channel_type type, struct rtt_channel_info *info);
249 * Register an RTT sink.
251 * @param[in] channel_index Channel index.
252 * @param[in] read Read callback function.
253 * @param[in,out] user_data User data to be passed to the callback function.
255 * @returns ERROR_OK on success, an error code on failure.
257 int rtt_register_sink(unsigned int channel_index, rtt_sink_read read,
258 void *user_data);
261 * Unregister an RTT sink.
263 * @param[in] channel_index Channel index.
264 * @param[in] read Read callback function.
265 * @param[in,out] user_data User data to be passed to the callback function.
267 * @returns ERROR_OK on success, an error code on failure.
269 int rtt_unregister_sink(unsigned int channel_index, rtt_sink_read read,
270 void *user_data);
273 * Write to an RTT channel.
275 * @param[in] channel_index Channel index.
276 * @param[in] buffer Buffer with data that should be written to the channel.
277 * @param[in,out] length Number of bytes to write. On success, the argument gets
278 * updated with the actual number of written bytes.
280 * @returns ERROR_OK on success, an error code on failure.
282 int rtt_write_channel(unsigned int channel_index, const uint8_t *buffer,
283 size_t *length);
285 extern const struct command_registration rtt_target_command_handlers[];
287 #endif /* OPENOCD_RTT_RTT_H */