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>
24 int armv7m_trace_tpiu_config(struct target *target)
26 struct armv7m_common *armv7m = target_to_armv7m(target);
27 struct armv7m_trace_config *trace_config = &armv7m->trace_config;
31 if (!trace_config->trace_freq) {
32 LOG_ERROR("Trace port frequency is 0, can't enable TPIU");
36 if (trace_config->traceclkin_freq % trace_config->trace_freq) {
37 LOG_ERROR("Can not calculate an integer divisor to get %u trace port frequency from %u TRACECLKIN frequency",
38 trace_config->trace_freq, trace_config->traceclkin_freq);
42 prescaler = trace_config->traceclkin_freq / trace_config->trace_freq;
44 retval = target_write_u32(target, TPIU_CSPSR, 1 << trace_config->port_size);
45 if (retval != ERROR_OK)
48 retval = target_write_u32(target, TPIU_ACPR, prescaler - 1);
49 if (retval != ERROR_OK)
52 retval = target_write_u32(target, TPIU_SPPR, trace_config->pin_protocol);
53 if (retval != ERROR_OK)
57 retval = target_read_u32(target, TPIU_FFCR, &ffcr);
58 if (retval != ERROR_OK)
60 if (trace_config->formatter)
64 retval = target_write_u32(target, TPIU_FFCR, ffcr);
65 if (retval != ERROR_OK)
68 target_call_event_callbacks(target, TARGET_EVENT_TRACE_CONFIG);
73 int armv7m_trace_itm_config(struct target *target)
75 struct armv7m_common *armv7m = target_to_armv7m(target);
76 struct armv7m_trace_config *trace_config = &armv7m->trace_config;
79 retval = target_write_u32(target, ITM_LAR, ITM_LAR_KEY);
80 if (retval != ERROR_OK)
83 /* Enable ITM, TXENA, set TraceBusID and other parameters */
84 retval = target_write_u32(target, ITM_TCR, (1 << 0) | (1 << 3) |
85 (trace_config->itm_diff_timestamps << 1) |
86 (trace_config->itm_synchro_packets << 2) |
87 (trace_config->itm_async_timestamps << 4) |
88 (trace_config->itm_ts_prescale << 8) |
89 (trace_config->trace_bus_id << 16));
90 if (retval != ERROR_OK)
93 for (unsigned int i = 0; i < 8; i++) {
94 retval = target_write_u32(target, ITM_TER0 + i * 4,
95 trace_config->itm_ter[i]);
96 if (retval != ERROR_OK)
103 static void close_trace_file(struct armv7m_common *armv7m)
105 if (armv7m->trace_config.trace_file)
106 fclose(armv7m->trace_config.trace_file);
107 armv7m->trace_config.trace_file = NULL;
110 COMMAND_HANDLER(handle_tpiu_config_command)
112 struct target *target = get_current_target(CMD_CTX);
113 struct armv7m_common *armv7m = target_to_armv7m(target);
115 unsigned int cmd_idx = 0;
117 if (CMD_ARGC == cmd_idx)
118 return ERROR_COMMAND_SYNTAX_ERROR;
119 if (!strcmp(CMD_ARGV[cmd_idx], "disable")) {
120 if (CMD_ARGC == cmd_idx + 1) {
121 close_trace_file(armv7m);
123 armv7m->trace_config.config_type = DISABLED;
124 if (CMD_CTX->mode == COMMAND_EXEC)
125 return armv7m_trace_tpiu_config(target);
129 } else if (!strcmp(CMD_ARGV[cmd_idx], "external") ||
130 !strcmp(CMD_ARGV[cmd_idx], "internal")) {
131 close_trace_file(armv7m);
133 armv7m->trace_config.config_type = EXTERNAL;
134 if (!strcmp(CMD_ARGV[cmd_idx], "internal")) {
136 if (CMD_ARGC == cmd_idx)
137 return ERROR_COMMAND_SYNTAX_ERROR;
139 armv7m->trace_config.config_type = INTERNAL;
140 armv7m->trace_config.trace_file = fopen(CMD_ARGV[cmd_idx], "ab");
141 if (!armv7m->trace_config.trace_file) {
142 LOG_ERROR("Can't open trace destination file");
147 if (CMD_ARGC == cmd_idx)
148 return ERROR_COMMAND_SYNTAX_ERROR;
150 if (!strcmp(CMD_ARGV[cmd_idx], "sync")) {
151 armv7m->trace_config.pin_protocol = SYNC;
154 if (CMD_ARGC == cmd_idx)
155 return ERROR_COMMAND_SYNTAX_ERROR;
157 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[cmd_idx], armv7m->trace_config.port_size);
159 if (!strcmp(CMD_ARGV[cmd_idx], "manchester"))
160 armv7m->trace_config.pin_protocol = ASYNC_MANCHESTER;
161 else if (!strcmp(CMD_ARGV[cmd_idx], "uart"))
162 armv7m->trace_config.pin_protocol = ASYNC_UART;
164 return ERROR_COMMAND_SYNTAX_ERROR;
167 if (CMD_ARGC == cmd_idx)
168 return ERROR_COMMAND_SYNTAX_ERROR;
170 COMMAND_PARSE_ON_OFF(CMD_ARGV[cmd_idx], armv7m->trace_config.formatter);
174 if (CMD_ARGC == cmd_idx)
175 return ERROR_COMMAND_SYNTAX_ERROR;
177 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.traceclkin_freq);
180 if (CMD_ARGC != cmd_idx) {
181 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.trace_freq);
184 if (armv7m->trace_config.config_type != INTERNAL) {
185 LOG_ERROR("Trace port frequency can't be omitted in external capture mode");
186 return ERROR_COMMAND_SYNTAX_ERROR;
188 armv7m->trace_config.trace_freq = 0;
191 if (CMD_ARGC == cmd_idx) {
192 if (CMD_CTX->mode == COMMAND_EXEC)
193 return armv7m_trace_tpiu_config(target);
199 return ERROR_COMMAND_SYNTAX_ERROR;
202 COMMAND_HANDLER(handle_itm_port_command)
204 struct target *target = get_current_target(CMD_CTX);
205 struct armv7m_common *armv7m = target_to_armv7m(target);
206 unsigned int reg_idx;
211 return ERROR_COMMAND_SYNTAX_ERROR;
213 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], port);
214 COMMAND_PARSE_ON_OFF(CMD_ARGV[1], enable);
218 armv7m->trace_config.itm_ter[reg_idx] |= (1 << port);
220 armv7m->trace_config.itm_ter[reg_idx] &= ~(1 << port);
222 if (CMD_CTX->mode == COMMAND_EXEC)
223 return armv7m_trace_itm_config(target);
228 COMMAND_HANDLER(handle_itm_ports_command)
230 struct target *target = get_current_target(CMD_CTX);
231 struct armv7m_common *armv7m = target_to_armv7m(target);
235 return ERROR_COMMAND_SYNTAX_ERROR;
237 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
238 memset(armv7m->trace_config.itm_ter, enable ? 0xff : 0,
239 sizeof(armv7m->trace_config.itm_ter));
241 if (CMD_CTX->mode == COMMAND_EXEC)
242 return armv7m_trace_itm_config(target);
247 static const struct command_registration tpiu_command_handlers[] = {
250 .handler = handle_tpiu_config_command,
252 .help = "Configure TPIU features",
253 .usage = "(disable | "
254 "((external | internal <filename>) "
255 "(sync <port width> | ((manchester | uart) <formatter enable>)) "
256 "<TRACECLKIN freq> [<trace freq>]))",
258 COMMAND_REGISTRATION_DONE
261 static const struct command_registration itm_command_handlers[] = {
264 .handler = handle_itm_port_command,
266 .help = "Enable or disable ITM stimulus port",
267 .usage = "<port> (0|1|on|off)",
271 .handler = handle_itm_ports_command,
273 .help = "Enable or disable all ITM stimulus ports",
274 .usage = "(0|1|on|off)",
276 COMMAND_REGISTRATION_DONE
279 const struct command_registration armv7m_trace_command_handlers[] = {
283 .help = "tpiu command group",
285 .chain = tpiu_command_handlers,
290 .help = "itm command group",
292 .chain = itm_command_handlers,
294 COMMAND_REGISTRATION_DONE