1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 * Copyright (C) 2016-2020 by Marc Schink <dev@zapb.de>
7 #ifndef OPENOCD_RTT_RTT_H
8 #define OPENOCD_RTT_RTT_H
13 #include <helper/command.h>
14 #include <target/target.h>
17 * Control block ID length in bytes, including the trailing null-terminator.
19 #define RTT_CB_MAX_ID_LENGTH 16
21 /* Control block size in bytes. */
22 #define RTT_CB_SIZE (RTT_CB_MAX_ID_LENGTH + 2 * sizeof(uint32_t))
24 /* Channel structure size in bytes. */
25 #define RTT_CHANNEL_SIZE 24
27 /* Minimal channel buffer size in bytes. */
28 #define RTT_CHANNEL_BUFFER_MIN_SIZE 2
30 /** RTT control block. */
32 /** Control block address on the target. */
33 target_addr_t address
;
34 /** Control block identifier, including trailing null-terminator. */
35 char id
[RTT_CB_MAX_ID_LENGTH
];
36 /** Maximum number of up-channels. */
37 uint32_t num_up_channels
;
38 /** Maximum number of down-channels. */
39 uint32_t num_down_channels
;
44 /** Channel structure address on the target. */
45 target_addr_t address
;
46 /** Channel name address on the target. */
48 /** Buffer address on the target. */
50 /** Channel buffer size in bytes. */
52 /** Write position within the buffer in bytes. */
54 /** Read position within the buffer in bytes. */
59 * @note: Not used at the moment.
64 /** RTT channel information. */
65 struct rtt_channel_info
{
68 /** Length of the name in bytes, including the trailing null-terminator. */
70 /** Buffer size in bytes. */
75 * @note: Not used at the moment.
80 typedef int (*rtt_sink_read
)(unsigned int channel
, const uint8_t *buffer
,
81 size_t length
, void *user_data
);
83 struct rtt_sink_list
{
87 struct rtt_sink_list
*next
;
91 enum rtt_channel_type
{
92 /** Up channel (target to host). */
94 /** Down channel (host to target). */
98 typedef int (*rtt_source_find_ctrl_block
)(struct target
*target
,
99 target_addr_t
*address
, size_t size
, const char *id
, bool *found
,
101 typedef int (*rtt_source_read_ctrl_block
)(struct target
*target
,
102 target_addr_t address
, struct rtt_control
*ctrl_block
,
104 typedef int (*rtt_source_read_channel_info
)(struct target
*target
,
105 const struct rtt_control
*ctrl
, unsigned int channel
,
106 enum rtt_channel_type type
, struct rtt_channel_info
*info
,
108 typedef int (*rtt_source_start
)(struct target
*target
,
109 const struct rtt_control
*ctrl
, void *user_data
);
110 typedef int (*rtt_source_stop
)(struct target
*target
, void *user_data
);
111 typedef int (*rtt_source_read
)(struct target
*target
,
112 const struct rtt_control
*ctrl
, struct rtt_sink_list
**sinks
,
113 size_t num_channels
, void *user_data
);
114 typedef int (*rtt_source_write
)(struct target
*target
,
115 struct rtt_control
*ctrl
, unsigned int channel
,
116 const uint8_t *buffer
, size_t *length
, void *user_data
);
120 rtt_source_find_ctrl_block find_cb
;
121 rtt_source_read_ctrl_block read_cb
;
122 rtt_source_read_channel_info read_channel_info
;
123 rtt_source_start start
;
124 rtt_source_stop stop
;
125 rtt_source_read read
;
126 rtt_source_write write
;
130 * Initialize Real-Time Transfer (RTT).
132 * @returns ERROR_OK on success, an error code on failure.
137 * Shutdown Real-Time Transfer (RTT).
139 * @returns ERROR_OK on success, an error code on failure.
144 * Register an RTT source for a target.
146 * @param[in] source RTT source.
147 * @param[in,out] target Target.
149 * @returns ERROR_OK on success, an error code on failure.
151 int rtt_register_source(const struct rtt_source source
,
152 struct target
*target
);
157 * @param[in] address Start address to search for the control block.
158 * @param[in] size Size of the control block search area.
159 * @param[in] id Identifier of the control block. Must be null-terminated.
161 * @returns ERROR_OK on success, an error code on failure.
163 int rtt_setup(target_addr_t address
, size_t size
, const char *id
);
166 * Start Real-Time Transfer (RTT).
168 * @returns ERROR_OK on success, an error code on failure.
173 * Stop Real-Time Transfer (RTT).
175 * @returns ERROR_OK on success, an error code on failure.
180 * Get the polling interval.
182 * @param[out] interval Polling interval in milliseconds.
184 * @returns ERROR_OK on success, an error code on failure.
186 int rtt_get_polling_interval(unsigned int *interval
);
189 * Set the polling interval.
191 * @param[in] interval Polling interval in milliseconds.
193 * @returns ERROR_OK on success, an error code on failure.
195 int rtt_set_polling_interval(unsigned int interval
);
198 * Get whether RTT is started.
200 * @returns Whether RTT is started.
202 bool rtt_started(void);
205 * Get whether RTT is configured.
207 * @returns Whether RTT is configured.
209 bool rtt_configured(void);
212 * Get whether RTT control block was found.
214 * @returns Whether RTT was found.
216 bool rtt_found_cb(void);
219 * Get the RTT control block.
221 * @returns The RTT control block.
223 const struct rtt_control
*rtt_get_control(void);
226 * Read channel information.
228 * @param[in] channel_index Channel index.
229 * @param[in] type Channel type.
230 * @param[out] info Channel information.
232 * @returns ERROR_OK on success, an error code on failure.
234 int rtt_read_channel_info(unsigned int channel_index
,
235 enum rtt_channel_type type
, struct rtt_channel_info
*info
);
238 * Register an RTT sink.
240 * @param[in] channel_index Channel index.
241 * @param[in] read Read callback function.
242 * @param[in,out] user_data User data to be passed to the callback function.
244 * @returns ERROR_OK on success, an error code on failure.
246 int rtt_register_sink(unsigned int channel_index
, rtt_sink_read read
,
250 * Unregister an RTT sink.
252 * @param[in] channel_index Channel index.
253 * @param[in] read Read callback function.
254 * @param[in,out] user_data User data to be passed to the callback function.
256 * @returns ERROR_OK on success, an error code on failure.
258 int rtt_unregister_sink(unsigned int channel_index
, rtt_sink_read read
,
262 * Write to an RTT channel.
264 * @param[in] channel_index Channel index.
265 * @param[in] buffer Buffer with data that should be written to the channel.
266 * @param[in,out] length Number of bytes to write. On success, the argument gets
267 * updated with the actual number of written bytes.
269 * @returns ERROR_OK on success, an error code on failure.
271 int rtt_write_channel(unsigned int channel_index
, const uint8_t *buffer
,
274 extern const struct command_registration rtt_target_command_handlers
[];
276 #endif /* OPENOCD_RTT_RTT_H */