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 if (fwrite(buf, 1, size, armv7m->trace_config.trace_file) == size)
39 fflush(armv7m->trace_config.trace_file);
41 LOG_ERROR("Error writing to the trace destination file");
48 int armv7m_trace_tpiu_config(struct target *target)
50 struct armv7m_common *armv7m = target_to_armv7m(target);
51 struct armv7m_trace_config *trace_config = &armv7m->trace_config;
55 target_unregister_timer_callback(armv7m_poll_trace, target);
58 retval = adapter_config_trace(trace_config->config_type == INTERNAL,
59 trace_config->pin_protocol,
60 trace_config->port_size,
61 &trace_config->trace_freq);
62 if (retval != ERROR_OK)
65 if (!trace_config->trace_freq) {
66 LOG_ERROR("Trace port frequency is 0, can't enable TPIU");
70 prescaler = trace_config->traceclkin_freq / trace_config->trace_freq;
72 if (trace_config->traceclkin_freq % trace_config->trace_freq) {
74 int trace_freq = trace_config->traceclkin_freq / prescaler;
75 LOG_INFO("Can not obtain %u trace port frequency from %u TRACECLKIN frequency, using %u instead",
76 trace_config->trace_freq, trace_config->traceclkin_freq,
78 trace_config->trace_freq = trace_freq;
79 retval = adapter_config_trace(trace_config->config_type == INTERNAL,
80 trace_config->pin_protocol,
81 trace_config->port_size,
82 &trace_config->trace_freq);
83 if (retval != ERROR_OK)
87 retval = target_write_u32(target, TPIU_CSPSR, 1 << trace_config->port_size);
88 if (retval != ERROR_OK)
91 retval = target_write_u32(target, TPIU_ACPR, prescaler - 1);
92 if (retval != ERROR_OK)
95 retval = target_write_u32(target, TPIU_SPPR, trace_config->pin_protocol);
96 if (retval != ERROR_OK)
100 retval = target_read_u32(target, TPIU_FFCR, &ffcr);
101 if (retval != ERROR_OK)
103 if (trace_config->formatter)
107 retval = target_write_u32(target, TPIU_FFCR, ffcr);
108 if (retval != ERROR_OK)
111 if (trace_config->config_type == INTERNAL)
112 target_register_timer_callback(armv7m_poll_trace, 1, 1, target);
114 target_call_event_callbacks(target, TARGET_EVENT_TRACE_CONFIG);
119 int armv7m_trace_itm_config(struct target *target)
121 struct armv7m_common *armv7m = target_to_armv7m(target);
122 struct armv7m_trace_config *trace_config = &armv7m->trace_config;
125 retval = target_write_u32(target, ITM_LAR, ITM_LAR_KEY);
126 if (retval != ERROR_OK)
129 /* Enable ITM, TXENA, set TraceBusID and other parameters */
130 retval = target_write_u32(target, ITM_TCR, (1 << 0) | (1 << 3) |
131 (trace_config->itm_diff_timestamps << 1) |
132 (trace_config->itm_synchro_packets << 2) |
133 (trace_config->itm_async_timestamps << 4) |
134 (trace_config->itm_ts_prescale << 8) |
135 (trace_config->trace_bus_id << 16));
136 if (retval != ERROR_OK)
139 for (unsigned int i = 0; i < 8; i++) {
140 retval = target_write_u32(target, ITM_TER0 + i * 4,
141 trace_config->itm_ter[i]);
142 if (retval != ERROR_OK)
149 static void close_trace_file(struct armv7m_common *armv7m)
151 if (armv7m->trace_config.trace_file)
152 fclose(armv7m->trace_config.trace_file);
153 armv7m->trace_config.trace_file = NULL;
156 COMMAND_HANDLER(handle_tpiu_config_command)
158 struct target *target = get_current_target(CMD_CTX);
159 struct armv7m_common *armv7m = target_to_armv7m(target);
161 unsigned int cmd_idx = 0;
163 if (CMD_ARGC == cmd_idx)
164 return ERROR_COMMAND_SYNTAX_ERROR;
165 if (!strcmp(CMD_ARGV[cmd_idx], "disable")) {
166 if (CMD_ARGC == cmd_idx + 1) {
167 close_trace_file(armv7m);
169 armv7m->trace_config.config_type = DISABLED;
170 if (CMD_CTX->mode == COMMAND_EXEC)
171 return armv7m_trace_tpiu_config(target);
175 } else if (!strcmp(CMD_ARGV[cmd_idx], "external") ||
176 !strcmp(CMD_ARGV[cmd_idx], "internal")) {
177 close_trace_file(armv7m);
179 armv7m->trace_config.config_type = EXTERNAL;
180 if (!strcmp(CMD_ARGV[cmd_idx], "internal")) {
182 if (CMD_ARGC == cmd_idx)
183 return ERROR_COMMAND_SYNTAX_ERROR;
185 armv7m->trace_config.config_type = INTERNAL;
186 armv7m->trace_config.trace_file = fopen(CMD_ARGV[cmd_idx], "ab");
187 if (!armv7m->trace_config.trace_file) {
188 LOG_ERROR("Can't open trace destination file");
193 if (CMD_ARGC == cmd_idx)
194 return ERROR_COMMAND_SYNTAX_ERROR;
196 if (!strcmp(CMD_ARGV[cmd_idx], "sync")) {
197 armv7m->trace_config.pin_protocol = SYNC;
200 if (CMD_ARGC == cmd_idx)
201 return ERROR_COMMAND_SYNTAX_ERROR;
203 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[cmd_idx], armv7m->trace_config.port_size);
205 if (!strcmp(CMD_ARGV[cmd_idx], "manchester"))
206 armv7m->trace_config.pin_protocol = ASYNC_MANCHESTER;
207 else if (!strcmp(CMD_ARGV[cmd_idx], "uart"))
208 armv7m->trace_config.pin_protocol = ASYNC_UART;
210 return ERROR_COMMAND_SYNTAX_ERROR;
213 if (CMD_ARGC == cmd_idx)
214 return ERROR_COMMAND_SYNTAX_ERROR;
216 COMMAND_PARSE_ON_OFF(CMD_ARGV[cmd_idx], armv7m->trace_config.formatter);
220 if (CMD_ARGC == cmd_idx)
221 return ERROR_COMMAND_SYNTAX_ERROR;
223 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.traceclkin_freq);
226 if (CMD_ARGC != cmd_idx) {
227 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.trace_freq);
230 if (armv7m->trace_config.config_type != INTERNAL) {
231 LOG_ERROR("Trace port frequency can't be omitted in external capture mode");
232 return ERROR_COMMAND_SYNTAX_ERROR;
234 armv7m->trace_config.trace_freq = 0;
237 if (CMD_ARGC == cmd_idx) {
238 if (CMD_CTX->mode == COMMAND_EXEC)
239 return armv7m_trace_tpiu_config(target);
245 return ERROR_COMMAND_SYNTAX_ERROR;
248 COMMAND_HANDLER(handle_itm_port_command)
250 struct target *target = get_current_target(CMD_CTX);
251 struct armv7m_common *armv7m = target_to_armv7m(target);
252 unsigned int reg_idx;
257 return ERROR_COMMAND_SYNTAX_ERROR;
259 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], port);
260 COMMAND_PARSE_ON_OFF(CMD_ARGV[1], enable);
264 armv7m->trace_config.itm_ter[reg_idx] |= (1 << port);
266 armv7m->trace_config.itm_ter[reg_idx] &= ~(1 << port);
268 if (CMD_CTX->mode == COMMAND_EXEC)
269 return armv7m_trace_itm_config(target);
274 COMMAND_HANDLER(handle_itm_ports_command)
276 struct target *target = get_current_target(CMD_CTX);
277 struct armv7m_common *armv7m = target_to_armv7m(target);
281 return ERROR_COMMAND_SYNTAX_ERROR;
283 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
284 memset(armv7m->trace_config.itm_ter, enable ? 0xff : 0,
285 sizeof(armv7m->trace_config.itm_ter));
287 if (CMD_CTX->mode == COMMAND_EXEC)
288 return armv7m_trace_itm_config(target);
293 static const struct command_registration tpiu_command_handlers[] = {
296 .handler = handle_tpiu_config_command,
298 .help = "Configure TPIU features",
299 .usage = "(disable | "
300 "((external | internal <filename>) "
301 "(sync <port width> | ((manchester | uart) <formatter enable>)) "
302 "<TRACECLKIN freq> [<trace freq>]))",
304 COMMAND_REGISTRATION_DONE
307 static const struct command_registration itm_command_handlers[] = {
310 .handler = handle_itm_port_command,
312 .help = "Enable or disable ITM stimulus port",
313 .usage = "<port> (0|1|on|off)",
317 .handler = handle_itm_ports_command,
319 .help = "Enable or disable all ITM stimulus ports",
320 .usage = "(0|1|on|off)",
322 COMMAND_REGISTRATION_DONE
325 const struct command_registration armv7m_trace_command_handlers[] = {
329 .help = "tpiu command group",
331 .chain = tpiu_command_handlers,
336 .help = "itm command group",
338 .chain = itm_command_handlers,
340 COMMAND_REGISTRATION_DONE