1 /***************************************************************************
2 * Copyright (C) 2015 Paul Fertser <fercerpav@gmail.com> *
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. *
13 ***************************************************************************/
19 #include <target/target.h>
20 #include <target/armv7m.h>
21 #include <target/cortex_m.h>
22 #include <target/armv7m_trace.h>
23 #include <jtag/interface.h>
25 #define TRACE_BUF_SIZE 4096
27 static int armv7m_poll_trace(void *target)
29 struct armv7m_common *armv7m = target_to_armv7m(target);
30 uint8_t buf[TRACE_BUF_SIZE];
31 size_t size = sizeof(buf);
34 retval = adapter_poll_trace(buf, &size);
35 if (retval != ERROR_OK || !size)
38 target_call_trace_callbacks(target, size, buf);
40 if (armv7m->trace_config.trace_file != NULL) {
41 if (fwrite(buf, 1, size, armv7m->trace_config.trace_file) == size)
42 fflush(armv7m->trace_config.trace_file);
44 LOG_ERROR("Error writing to the trace destination file");
52 int armv7m_trace_tpiu_config(struct target *target)
54 struct armv7m_common *armv7m = target_to_armv7m(target);
55 struct armv7m_trace_config *trace_config = &armv7m->trace_config;
59 target_unregister_timer_callback(armv7m_poll_trace, target);
62 retval = adapter_config_trace(trace_config->config_type == INTERNAL,
63 trace_config->pin_protocol,
64 trace_config->port_size,
65 &trace_config->trace_freq);
66 if (retval != ERROR_OK)
69 if (!trace_config->trace_freq) {
70 LOG_ERROR("Trace port frequency is 0, can't enable TPIU");
74 prescaler = trace_config->traceclkin_freq / trace_config->trace_freq;
76 if (trace_config->traceclkin_freq % trace_config->trace_freq) {
78 int trace_freq = trace_config->traceclkin_freq / prescaler;
79 LOG_INFO("Can not obtain %u trace port frequency from %u TRACECLKIN frequency, using %u instead",
80 trace_config->trace_freq, trace_config->traceclkin_freq,
82 trace_config->trace_freq = trace_freq;
83 retval = adapter_config_trace(trace_config->config_type == INTERNAL,
84 trace_config->pin_protocol,
85 trace_config->port_size,
86 &trace_config->trace_freq);
87 if (retval != ERROR_OK)
91 retval = target_write_u32(target, TPIU_CSPSR, 1 << trace_config->port_size);
92 if (retval != ERROR_OK)
95 retval = target_write_u32(target, TPIU_ACPR, prescaler - 1);
96 if (retval != ERROR_OK)
99 retval = target_write_u32(target, TPIU_SPPR, trace_config->pin_protocol);
100 if (retval != ERROR_OK)
104 retval = target_read_u32(target, TPIU_FFCR, &ffcr);
105 if (retval != ERROR_OK)
107 if (trace_config->formatter)
111 retval = target_write_u32(target, TPIU_FFCR, ffcr);
112 if (retval != ERROR_OK)
115 if (trace_config->config_type == INTERNAL)
116 target_register_timer_callback(armv7m_poll_trace, 1, 1, target);
118 target_call_event_callbacks(target, TARGET_EVENT_TRACE_CONFIG);
123 int armv7m_trace_itm_config(struct target *target)
125 struct armv7m_common *armv7m = target_to_armv7m(target);
126 struct armv7m_trace_config *trace_config = &armv7m->trace_config;
129 retval = target_write_u32(target, ITM_LAR, ITM_LAR_KEY);
130 if (retval != ERROR_OK)
133 /* Enable ITM, TXENA, set TraceBusID and other parameters */
134 retval = target_write_u32(target, ITM_TCR, (1 << 0) | (1 << 3) |
135 (trace_config->itm_diff_timestamps << 1) |
136 (trace_config->itm_synchro_packets << 2) |
137 (trace_config->itm_async_timestamps << 4) |
138 (trace_config->itm_ts_prescale << 8) |
139 (trace_config->trace_bus_id << 16));
140 if (retval != ERROR_OK)
143 for (unsigned int i = 0; i < 8; i++) {
144 retval = target_write_u32(target, ITM_TER0 + i * 4,
145 trace_config->itm_ter[i]);
146 if (retval != ERROR_OK)
153 static void close_trace_file(struct armv7m_common *armv7m)
155 if (armv7m->trace_config.trace_file)
156 fclose(armv7m->trace_config.trace_file);
157 armv7m->trace_config.trace_file = NULL;
160 COMMAND_HANDLER(handle_tpiu_config_command)
162 struct target *target = get_current_target(CMD_CTX);
163 struct armv7m_common *armv7m = target_to_armv7m(target);
165 unsigned int cmd_idx = 0;
167 if (CMD_ARGC == cmd_idx)
168 return ERROR_COMMAND_SYNTAX_ERROR;
169 if (!strcmp(CMD_ARGV[cmd_idx], "disable")) {
170 if (CMD_ARGC == cmd_idx + 1) {
171 close_trace_file(armv7m);
173 armv7m->trace_config.config_type = DISABLED;
174 if (CMD_CTX->mode == COMMAND_EXEC)
175 return armv7m_trace_tpiu_config(target);
179 } else if (!strcmp(CMD_ARGV[cmd_idx], "external") ||
180 !strcmp(CMD_ARGV[cmd_idx], "internal")) {
181 close_trace_file(armv7m);
183 armv7m->trace_config.config_type = EXTERNAL;
184 if (!strcmp(CMD_ARGV[cmd_idx], "internal")) {
186 if (CMD_ARGC == cmd_idx)
187 return ERROR_COMMAND_SYNTAX_ERROR;
189 armv7m->trace_config.config_type = INTERNAL;
191 if (strcmp(CMD_ARGV[cmd_idx], "-") != 0) {
192 armv7m->trace_config.trace_file = fopen(CMD_ARGV[cmd_idx], "ab");
193 if (!armv7m->trace_config.trace_file) {
194 LOG_ERROR("Can't open trace destination file");
200 if (CMD_ARGC == cmd_idx)
201 return ERROR_COMMAND_SYNTAX_ERROR;
203 if (!strcmp(CMD_ARGV[cmd_idx], "sync")) {
204 armv7m->trace_config.pin_protocol = SYNC;
207 if (CMD_ARGC == cmd_idx)
208 return ERROR_COMMAND_SYNTAX_ERROR;
210 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[cmd_idx], armv7m->trace_config.port_size);
212 if (!strcmp(CMD_ARGV[cmd_idx], "manchester"))
213 armv7m->trace_config.pin_protocol = ASYNC_MANCHESTER;
214 else if (!strcmp(CMD_ARGV[cmd_idx], "uart"))
215 armv7m->trace_config.pin_protocol = ASYNC_UART;
217 return ERROR_COMMAND_SYNTAX_ERROR;
220 if (CMD_ARGC == cmd_idx)
221 return ERROR_COMMAND_SYNTAX_ERROR;
223 COMMAND_PARSE_ON_OFF(CMD_ARGV[cmd_idx], armv7m->trace_config.formatter);
227 if (CMD_ARGC == cmd_idx)
228 return ERROR_COMMAND_SYNTAX_ERROR;
230 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.traceclkin_freq);
233 if (CMD_ARGC != cmd_idx) {
234 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.trace_freq);
237 if (armv7m->trace_config.config_type != INTERNAL) {
238 LOG_ERROR("Trace port frequency can't be omitted in external capture mode");
239 return ERROR_COMMAND_SYNTAX_ERROR;
241 armv7m->trace_config.trace_freq = 0;
244 if (CMD_ARGC == cmd_idx) {
245 if (CMD_CTX->mode == COMMAND_EXEC)
246 return armv7m_trace_tpiu_config(target);
252 return ERROR_COMMAND_SYNTAX_ERROR;
255 COMMAND_HANDLER(handle_itm_port_command)
257 struct target *target = get_current_target(CMD_CTX);
258 struct armv7m_common *armv7m = target_to_armv7m(target);
259 unsigned int reg_idx;
264 return ERROR_COMMAND_SYNTAX_ERROR;
266 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], port);
267 COMMAND_PARSE_ON_OFF(CMD_ARGV[1], enable);
271 armv7m->trace_config.itm_ter[reg_idx] |= (1 << port);
273 armv7m->trace_config.itm_ter[reg_idx] &= ~(1 << port);
275 if (CMD_CTX->mode == COMMAND_EXEC)
276 return armv7m_trace_itm_config(target);
281 COMMAND_HANDLER(handle_itm_ports_command)
283 struct target *target = get_current_target(CMD_CTX);
284 struct armv7m_common *armv7m = target_to_armv7m(target);
288 return ERROR_COMMAND_SYNTAX_ERROR;
290 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
291 memset(armv7m->trace_config.itm_ter, enable ? 0xff : 0,
292 sizeof(armv7m->trace_config.itm_ter));
294 if (CMD_CTX->mode == COMMAND_EXEC)
295 return armv7m_trace_itm_config(target);
300 static const struct command_registration tpiu_command_handlers[] = {
303 .handler = handle_tpiu_config_command,
305 .help = "Configure TPIU features",
306 .usage = "(disable | "
307 "((external | internal <filename>) "
308 "(sync <port width> | ((manchester | uart) <formatter enable>)) "
309 "<TRACECLKIN freq> [<trace freq>]))",
311 COMMAND_REGISTRATION_DONE
314 static const struct command_registration itm_command_handlers[] = {
317 .handler = handle_itm_port_command,
319 .help = "Enable or disable ITM stimulus port",
320 .usage = "<port> (0|1|on|off)",
324 .handler = handle_itm_ports_command,
326 .help = "Enable or disable all ITM stimulus ports",
327 .usage = "(0|1|on|off)",
329 COMMAND_REGISTRATION_DONE
332 const struct command_registration armv7m_trace_command_handlers[] = {
336 .help = "tpiu command group",
338 .chain = tpiu_command_handlers,
343 .help = "itm command group",
345 .chain = itm_command_handlers,
347 COMMAND_REGISTRATION_DONE