-static void close_trace_channel(struct armv7m_common *armv7m)
-{
- switch (armv7m->trace_config.internal_channel) {
- case TRACE_INTERNAL_CHANNEL_FILE:
- if (armv7m->trace_config.trace_file)
- fclose(armv7m->trace_config.trace_file);
- armv7m->trace_config.trace_file = NULL;
- break;
- case TRACE_INTERNAL_CHANNEL_TCP:
- if (armv7m->trace_config.trace_service)
- remove_service(armv7m->trace_config.trace_service->name, armv7m->trace_config.trace_service->port);
- armv7m->trace_config.trace_service = NULL;
- break;
- case TRACE_INTERNAL_CHANNEL_TCL_ONLY:
- /* nothing to do:
- * the trace polling is disabled in the beginning of armv7m_trace_tpiu_config
- **/
- break;
- default:
- LOG_ERROR("unsupported trace internal channel");
- }
-}
-
-static int trace_new_connection(struct connection *connection)
-{
- /* nothing to do */
- return ERROR_OK;
-}
-
-static int trace_input(struct connection *connection)
-{
- /* create a dummy buffer to check if the connection is still active */
- const int buf_len = 100;
- unsigned char buf[buf_len];
- int bytes_read = connection_read(connection, buf, buf_len);
-
- if (bytes_read == 0)
- return ERROR_SERVER_REMOTE_CLOSED;
- else if (bytes_read == -1) {
- LOG_ERROR("error during read: %s", strerror(errno));
- return ERROR_SERVER_REMOTE_CLOSED;
- }
-
- return ERROR_OK;
-}
-
-static int trace_connection_closed(struct connection *connection)
-{
- /* nothing to do, no connection->priv to free */
- return ERROR_OK;
-}
-
-extern struct command_context *global_cmd_ctx;
-
-int armv7m_trace_tpiu_exit(struct target *target)
-{
- struct armv7m_common *armv7m = target_to_armv7m(target);
-
- if (global_cmd_ctx->mode == COMMAND_CONFIG ||
- armv7m->trace_config.config_type == TRACE_CONFIG_TYPE_DISABLED)
- return ERROR_OK;
-
- close_trace_channel(armv7m);
- armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_DISABLED;
- return armv7m_trace_tpiu_config(target);
-}
-
-COMMAND_HANDLER(handle_tpiu_config_command)
-{
- struct target *target = get_current_target(CMD_CTX);
- struct armv7m_common *armv7m = target_to_armv7m(target);
-
- unsigned int cmd_idx = 0;
-
- if (CMD_ARGC == cmd_idx)
- return ERROR_COMMAND_SYNTAX_ERROR;
- if (!strcmp(CMD_ARGV[cmd_idx], "disable")) {
- if (CMD_ARGC == cmd_idx + 1) {
- close_trace_channel(armv7m);
-
- armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_DISABLED;
- if (CMD_CTX->mode == COMMAND_EXEC)
- return armv7m_trace_tpiu_config(target);
- else
- return ERROR_OK;
- }
- } else if (!strcmp(CMD_ARGV[cmd_idx], "external") ||
- !strcmp(CMD_ARGV[cmd_idx], "internal")) {
- close_trace_channel(armv7m);
-
- armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_EXTERNAL;
- if (!strcmp(CMD_ARGV[cmd_idx], "internal")) {
- cmd_idx++;
- if (CMD_ARGC == cmd_idx)
- return ERROR_COMMAND_SYNTAX_ERROR;
-
- armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_INTERNAL;
- armv7m->trace_config.internal_channel = TRACE_INTERNAL_CHANNEL_TCL_ONLY;
-
- if (strcmp(CMD_ARGV[cmd_idx], "-") != 0) {
- if (CMD_ARGV[cmd_idx][0] == ':') {
- armv7m->trace_config.internal_channel = TRACE_INTERNAL_CHANNEL_TCP;
-
- int ret = add_service("armv7m_trace", &(CMD_ARGV[cmd_idx][1]),
- CONNECTION_LIMIT_UNLIMITED, trace_new_connection, trace_input,
- trace_connection_closed, NULL, &armv7m->trace_config.trace_service);
- if (ret != ERROR_OK) {
- LOG_ERROR("Can't configure trace TCP port");
- return ERROR_FAIL;
- }
- } else {
- armv7m->trace_config.internal_channel = TRACE_INTERNAL_CHANNEL_FILE;
- armv7m->trace_config.trace_file = fopen(CMD_ARGV[cmd_idx], "ab");
- if (!armv7m->trace_config.trace_file) {
- LOG_ERROR("Can't open trace destination file");
- return ERROR_FAIL;
- }
- }
- }
- }
- cmd_idx++;
- if (CMD_ARGC == cmd_idx)
- return ERROR_COMMAND_SYNTAX_ERROR;
-
- if (!strcmp(CMD_ARGV[cmd_idx], "sync")) {
- armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_SYNC;
-
- cmd_idx++;
- if (CMD_ARGC == cmd_idx)
- return ERROR_COMMAND_SYNTAX_ERROR;
-
- COMMAND_PARSE_NUMBER(u32, CMD_ARGV[cmd_idx], armv7m->trace_config.port_size);
- } else {
- if (!strcmp(CMD_ARGV[cmd_idx], "manchester"))
- armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_ASYNC_MANCHESTER;
- else if (!strcmp(CMD_ARGV[cmd_idx], "uart"))
- armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_ASYNC_UART;
- else
- return ERROR_COMMAND_SYNTAX_ERROR;
-
- cmd_idx++;
- if (CMD_ARGC == cmd_idx)
- return ERROR_COMMAND_SYNTAX_ERROR;
-
- COMMAND_PARSE_ON_OFF(CMD_ARGV[cmd_idx], armv7m->trace_config.formatter);
- }
-
- cmd_idx++;
- if (CMD_ARGC == cmd_idx)
- return ERROR_COMMAND_SYNTAX_ERROR;
-
- COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.traceclkin_freq);
-
- cmd_idx++;
- if (CMD_ARGC != cmd_idx) {
- COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.trace_freq);
- cmd_idx++;
- } else {
- if (armv7m->trace_config.config_type != TRACE_CONFIG_TYPE_INTERNAL) {
- LOG_ERROR("Trace port frequency can't be omitted in external capture mode");
- return ERROR_COMMAND_SYNTAX_ERROR;
- }
- armv7m->trace_config.trace_freq = 0;
- }
-
- if (CMD_ARGC == cmd_idx) {
- if (CMD_CTX->mode == COMMAND_EXEC)
- return armv7m_trace_tpiu_config(target);
- else
- return ERROR_OK;
- }
- }
-
- return ERROR_COMMAND_SYNTAX_ERROR;
-}
-