tcl.c 40.7 KB
Newer Older
1001
		unsigned delay;
1002
1003
		COMMAND_PARSE_NUMBER(uint, args[0], delay);

1004
1005
		jtag_set_ntrst_delay(delay);
	}
1006
	command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1007
1008
1009
	return ERROR_OK;
}

oharboe's avatar
oharboe committed
1010
1011
1012
1013
1014
1015
1016
1017
static int handle_jtag_nsrst_assert_width_command(struct command_context_s *cmd_ctx,
		char *cmd, char **args, int argc)
{
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;
	if (argc == 1)
	{
		unsigned delay;
1018
1019
		COMMAND_PARSE_NUMBER(uint, args[0], delay);

oharboe's avatar
oharboe committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
		jtag_set_nsrst_assert_width(delay);
	}
	command_print(cmd_ctx, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
	return ERROR_OK;
}

static int handle_jtag_ntrst_assert_width_command(struct command_context_s *cmd_ctx,
		char *cmd, char **args, int argc)
{
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;
	if (argc == 1)
	{
		unsigned delay;
1034
1035
		COMMAND_PARSE_NUMBER(uint, args[0], delay);

oharboe's avatar
oharboe committed
1036
1037
1038
1039
1040
1041
		jtag_set_ntrst_assert_width(delay);
	}
	command_print(cmd_ctx, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
	return ERROR_OK;
}

1042
static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1043
{
1044
1045
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;
1046

1047
1048
	int retval = ERROR_OK;
	if (argc == 1)
oharboe's avatar
   
oharboe committed
1049
	{
1050
		unsigned khz = 0;
1051
1052
		COMMAND_PARSE_NUMBER(uint, args[0], khz);

1053
		retval = jtag_config_khz(khz);
1054
		if (ERROR_OK != retval)
1055
			return retval;
oharboe's avatar
   
oharboe committed
1056
	}
1057

1058
	int cur_speed = jtag_get_speed_khz();
1059
1060
1061
1062
	retval = jtag_get_speed_readable(&cur_speed);
	if (ERROR_OK != retval)
		return retval;

1063
1064
1065
	if (cur_speed)
		command_print(cmd_ctx, "%d kHz", cur_speed);
	else
1066
		command_print(cmd_ctx, "RCLK - adaptive");
oharboe's avatar
   
oharboe committed
1067

1068
	return retval;
1069
1070
}

1071
1072
1073
1074
1075
1076
1077
1078
1079
static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;

	int retval = ERROR_OK;
	if (argc == 1)
	{
		unsigned khz = 0;
1080
1081
		COMMAND_PARSE_NUMBER(uint, args[0], khz);

1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
		retval = jtag_config_rclk(khz);
		if (ERROR_OK != retval)
			return retval;
	}

	int cur_khz = jtag_get_speed_khz();
	retval = jtag_get_speed_readable(&cur_khz);
	if (ERROR_OK != retval)
		return retval;

	if (cur_khz)
		command_print(cmd_ctx, "RCLK not supported - fallback to %d kHz", cur_khz);
	else
		command_print(cmd_ctx, "RCLK - adaptive");

	return retval;
}

zwelch's avatar
zwelch committed
1100
1101
static int handle_jtag_reset_command(struct command_context_s *cmd_ctx,
		char *cmd, char **args, int argc)
1102
{
zwelch's avatar
zwelch committed
1103
	if (argc != 2)
1104
1105
		return ERROR_COMMAND_SYNTAX_ERROR;

zwelch's avatar
zwelch committed
1106
	int trst = -1;
1107
1108
1109
1110
1111
1112
1113
	if (args[0][0] == '1')
		trst = 1;
	else if (args[0][0] == '0')
		trst = 0;
	else
		return ERROR_COMMAND_SYNTAX_ERROR;

zwelch's avatar
zwelch committed
1114
	int srst = -1;
1115
1116
1117
1118
1119
1120
1121
	if (args[1][0] == '1')
		srst = 1;
	else if (args[1][0] == '0')
		srst = 0;
	else
		return ERROR_COMMAND_SYNTAX_ERROR;

1122
	if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1123
1124
		return ERROR_JTAG_INIT_FAILED;

1125
	jtag_add_reset(trst, srst);
1126
	return jtag_execute_queue();
1127
1128
}

zwelch's avatar
zwelch committed
1129
1130
static int handle_runtest_command(struct command_context_s *cmd_ctx,
		char *cmd, char **args, int argc)
1131
{
zwelch's avatar
zwelch committed
1132
	if (argc != 1)
1133
1134
		return ERROR_COMMAND_SYNTAX_ERROR;

zwelch's avatar
zwelch committed
1135
	unsigned num_clocks;
1136
	COMMAND_PARSE_NUMBER(uint, args[0], num_clocks);
zwelch's avatar
zwelch committed
1137

zwelch's avatar
zwelch committed
1138
	jtag_add_runtest(num_clocks, TAP_IDLE);
1139
	return jtag_execute_queue();
1140
1141
}

zwelch's avatar
zwelch committed
1142
1143
1144
1145
/*
 * For "irscan" or "drscan" commands, the "end" (really, "next") state
 * should be stable ... and *NOT* a shift state, otherwise free-running
 * jtag clocks could change the values latched by the update state.
1146
1147
 * Not surprisingly, this is the same constraint as SVF; the "irscan"
 * and "drscan" commands are a write-only subset of what SVF provides.
zwelch's avatar
zwelch committed
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
 */
static bool scan_is_safe(tap_state_t state)
{
	switch (state)
	{
	case TAP_RESET:
	case TAP_IDLE:
	case TAP_DRPAUSE:
	case TAP_IRPAUSE:
		return true;
	default:
		return false;
	}
}


1164
static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1165
1166
1167
{
	int i;
	scan_field_t *fields;
1168
	jtag_tap_t *tap;
1169
	tap_state_t endstate;
1170

1171
1172
1173
1174
1175
	if ((argc < 2) || (argc % 2))
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

zwelch's avatar
zwelch committed
1176
1177
1178
1179
1180
1181
	/* optional "-endstate" "statename" at the end of the arguments,
	 * so that e.g. IRPAUSE can let us load the data register before
	 * entering RUN/IDLE to execute the instruction we load here.
	 */
	endstate = TAP_IDLE;

zwelch's avatar
zwelch committed
1182
	if (argc >= 4) {
ntfreak's avatar
ntfreak committed
1183
1184
		/* have at least one pair of numbers. */
		/* is last pair the magic text? */
1185
1186
1187
		if (strcmp("-endstate", args[argc - 2]) == 0) {
			endstate = tap_state_by_name(args[argc - 1]);
			if (endstate == TAP_INVALID)
1188
				return ERROR_COMMAND_SYNTAX_ERROR;
1189
1190
1191
1192
			if (!scan_is_safe(endstate))
				LOG_WARNING("unstable irscan endstate \"%s\"",
						args[argc - 1]);
			argc -= 2;
1193
1194
1195
		}
	}

1196
	int num_fields = argc / 2;
zwelch's avatar
zwelch committed
1197
1198
1199
	size_t fields_len = sizeof(scan_field_t) * num_fields;
	fields = malloc(fields_len);
	memset(fields, 0, fields_len);
1200

zwelch's avatar
zwelch committed
1201
	int retval;
oharboe's avatar
oharboe committed
1202
	for (i = 0; i < num_fields; i++)
1203
	{
1204
		tap = jtag_tap_by_string(args[i*2]);
zwelch's avatar
zwelch committed
1205
		if (tap == NULL)
1206
		{
1207
1208
1209
1210
			int j;
			for (j = 0; j < i; j++)
				free(fields[j].out_value);
                        free(fields);
1211
			command_print(cmd_ctx, "Tap: %s unknown", args[i*2]);
1212

1213
1214
			return ERROR_FAIL;
		}
1215
1216
		int field_size = tap->ir_length;
		fields[i].tap = tap;
oharboe's avatar
oharboe committed
1217
		fields[i].num_bits = field_size;
1218
		fields[i].out_value = malloc(CEIL(field_size, 8));
zwelch's avatar
zwelch committed
1219

1220
		uint32_t value;
zwelch's avatar
zwelch committed
1221
1222
1223
1224
		retval = parse_u32(args[i * 2 + 1], &value);
		if (ERROR_OK != retval)
			goto error_return;
		buf_set_u32(fields[i].out_value, 0, field_size, value);
1225
1226
1227
		fields[i].in_value = NULL;
	}

ntfreak's avatar
ntfreak committed
1228
	/* did we have an endstate? */
1229
	jtag_add_ir_scan(num_fields, fields, endstate);
mifi's avatar
mifi committed
1230

zwelch's avatar
zwelch committed
1231
	retval = jtag_execute_queue();
1232

zwelch's avatar
zwelch committed
1233
error_return:
oharboe's avatar
oharboe committed
1234
	for (i = 0; i < num_fields; i++)
zwelch's avatar
zwelch committed
1235
1236
1237
1238
	{
		if (NULL != fields[i].out_value)
			free(fields[i].out_value);
	}
1239
1240
1241

	free (fields);

1242
	return retval;
1243
1244
}

1245
static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1246
{
oharboe's avatar
oharboe committed
1247
	int retval;
1248
	scan_field_t *fields;
oharboe's avatar
oharboe committed
1249
	int num_fields;
1250
	int field_count = 0;
1251
	int i, e;
1252
	jtag_tap_t *tap;
1253
	tap_state_t endstate;
1254
1255
1256
1257
1258

	/* args[1] = device
	 * args[2] = num_bits
	 * args[3] = hex string
	 * ... repeat num bits and hex string ...
ntfreak's avatar
ntfreak committed
1259
	 *
1260
	 * .. optionally:
ntfreak's avatar
ntfreak committed
1261
	*     args[N-2] = "-endstate"
1262
	 *     args[N-1] = statename
1263
	 */
zwelch's avatar
zwelch committed
1264
	if ((argc < 4) || ((argc % 2) != 0))
1265
	{
1266
		Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
oharboe's avatar
oharboe committed
1267
		return JIM_ERR;
1268
1269
	}

zwelch's avatar
zwelch committed
1270
1271
	endstate = TAP_IDLE;

zwelch's avatar
zwelch committed
1272
1273
	script_debug(interp, "drscan", argc, args);

ntfreak's avatar
ntfreak committed
1274
	/* validate arguments as numbers */
1275
	e = JIM_OK;
zwelch's avatar
zwelch committed
1276
	for (i = 2; i < argc; i += 2)
1277
	{
oharboe's avatar
oharboe committed
1278
		long bits;
1279
		const char *cp;
1280
1281

		e = Jim_GetLong(interp, args[i], &bits);
1282
		/* If valid - try next arg */
zwelch's avatar
zwelch committed
1283
		if (e == JIM_OK) {
1284
1285
1286
1287
			continue;
		}

		/* Not valid.. are we at the end? */
zwelch's avatar
zwelch committed
1288
		if (((i + 2) != argc)) {
1289
			/* nope, then error */
1290
			return e;
1291
1292
		}

zwelch's avatar
zwelch committed
1293
1294
1295
1296
		/* it could be: "-endstate FOO"
		 * e.g. DRPAUSE so we can issue more instructions
		 * before entering RUN/IDLE and executing them.
		 */
1297
1298

		/* get arg as a string. */
1299
		cp = Jim_GetString(args[i], NULL);
1300
		/* is it the magic? */
zwelch's avatar
zwelch committed
1301
		if (0 == strcmp("-endstate", cp)) {
1302
			/* is the statename valid? */
1303
			cp = Jim_GetString(args[i + 1], NULL);
1304
1305
1306

			/* see if it is a valid state name */
			endstate = tap_state_by_name(cp);
zwelch's avatar
zwelch committed
1307
			if (endstate < 0) {
1308
				/* update the error message */
1309
				Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1310
			} else {
zwelch's avatar
zwelch committed
1311
1312
1313
1314
				if (!scan_is_safe(endstate))
					LOG_WARNING("drscan with unsafe "
							"endstate \"%s\"", cp);

1315
1316
1317
1318
1319
1320
1321
1322
				/* valid - so clear the error */
				e = JIM_OK;
				/* and remove the last 2 args */
				argc -= 2;
			}
		}

		/* Still an error? */
zwelch's avatar
zwelch committed
1323
		if (e != JIM_OK) {
1324
			return e; /* too bad */
ntfreak's avatar
ntfreak committed
1325
		}
1326
	} /* validate args */
1327

1328
	tap = jtag_tap_by_jim_obj(interp, args[1]);
zwelch's avatar
zwelch committed
1329
	if (tap == NULL) {
1330
1331
		return JIM_ERR;
	}
1332

zwelch's avatar
zwelch committed
1333
	num_fields = (argc-2)/2;
1334
	fields = malloc(sizeof(scan_field_t) * num_fields);
zwelch's avatar
zwelch committed
1335
	for (i = 2; i < argc; i += 2)
1336
	{
oharboe's avatar
oharboe committed
1337
		long bits;
1338
1339
1340
		int len;
		const char *str;

oharboe's avatar
oharboe committed
1341
		Jim_GetLong(interp, args[i], &bits);
zwelch's avatar
zwelch committed
1342
		str = Jim_GetString(args[i + 1], &len);
1343

1344
		fields[field_count].tap = tap;
oharboe's avatar
oharboe committed
1345
1346
		fields[field_count].num_bits = bits;
		fields[field_count].out_value = malloc(CEIL(bits, 8));
1347
		str_to_buf(str, len, fields[field_count].out_value, bits, 0);
oharboe's avatar
oharboe committed
1348
		fields[field_count].in_value = fields[field_count].out_value;
1349
		field_count++;
1350
	}
ntfreak's avatar
ntfreak committed
1351

1352
	jtag_add_dr_scan(num_fields, fields, endstate);
mifi's avatar
mifi committed
1353

1354
1355
1356
	retval = jtag_execute_queue();
	if (retval != ERROR_OK)
	{
1357
		Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
1358
1359
1360
		return JIM_ERR;
	}

zwelch's avatar
zwelch committed
1361
	field_count = 0;
1362
	Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
zwelch's avatar
zwelch committed
1363
	for (i = 2; i < argc; i += 2)
oharboe's avatar
oharboe committed
1364
1365
	{
		long bits;
1366
		char *str;
1367

1368
1369
		Jim_GetLong(interp, args[i], &bits);
		str = buf_to_str(fields[field_count].in_value, bits, 16);
oharboe's avatar
oharboe committed
1370
		free(fields[field_count].out_value);
1371
1372
1373

		Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
		free(str);
oharboe's avatar
oharboe committed
1374
1375
		field_count++;
	}
1376

oharboe's avatar
oharboe committed
1377
	Jim_SetResult(interp, list);
1378

1379
	free(fields);
1380

1381
	return JIM_OK;
1382
1383
}

1384

oharboe's avatar
oharboe committed
1385
1386
1387
1388
static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
{
	tap_state_t states[8];

zwelch's avatar
zwelch committed
1389
	if ((argc < 2) || ((size_t)argc > (sizeof(states)/sizeof(*states) + 1)))
oharboe's avatar
oharboe committed
1390
1391
1392
1393
1394
	{
		Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
		return JIM_ERR;
	}

zwelch's avatar
zwelch committed
1395
1396
	script_debug(interp, "pathmove", argc, args);

oharboe's avatar
oharboe committed
1397
	int i;
zwelch's avatar
zwelch committed
1398
	for (i = 0; i < argc-1; i++)
oharboe's avatar
oharboe committed
1399
1400
	{
		const char *cp;
1401
		cp = Jim_GetString(args[i + 1], NULL);
oharboe's avatar
oharboe committed
1402
		states[i] = tap_state_by_name(cp);
1403
		if (states[i] < 0)
oharboe's avatar
oharboe committed
1404
1405
		{
			/* update the error message */
1406
			Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
oharboe's avatar
oharboe committed
1407
1408
1409
1410
			return JIM_ERR;
		}
	}

1411
	if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
oharboe's avatar
oharboe committed
1412
1413
1414
1415
1416
	{
		Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
		return JIM_ERR;
	}

zwelch's avatar
zwelch committed
1417
	jtag_add_pathmove(argc-2, states + 1);
oharboe's avatar
oharboe committed
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428

	if (jtag_execute_queue()!= ERROR_OK)
	{
		Jim_SetResultString(interp, "pathmove: failed",-1);
		return JIM_ERR;
	}

	return JIM_OK;
}


1429
1430
static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
{
zwelch's avatar
zwelch committed
1431
1432
	script_debug(interp, "flush_count", argc, args);

1433
	Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
1434
1435
1436
1437
1438

	return JIM_OK;
}


1439
static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1440
{
1441
1442
1443
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;

1444
1445
1446
	if (argc == 1)
	{
		if (strcmp(args[0], "enable") == 0)
1447
			jtag_set_verify_capture_ir(true);
1448
		else if (strcmp(args[0], "disable") == 0)
1449
1450
			jtag_set_verify_capture_ir(false);
		else
1451
1452
			return ERROR_COMMAND_SYNTAX_ERROR;
	}
1453

1454
1455
	const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
	command_print(cmd_ctx, "verify Capture-IR is %s", status);
1456

1457
1458
	return ERROR_OK;
}
1459

oharboe's avatar
oharboe committed
1460
1461
static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
1462
1463
1464
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;

oharboe's avatar
oharboe committed
1465
1466
1467
	if (argc == 1)
	{
		if (strcmp(args[0], "enable") == 0)
1468
			jtag_set_verify(true);
oharboe's avatar
oharboe committed
1469
		else if (strcmp(args[0], "disable") == 0)
1470
1471
			jtag_set_verify(false);
		else
oharboe's avatar
oharboe committed
1472
			return ERROR_COMMAND_SYNTAX_ERROR;
1473
	}
oharboe's avatar
oharboe committed
1474

1475
1476
	const char *status = jtag_will_verify() ? "enabled": "disabled";
	command_print(cmd_ctx, "verify jtag capture is %s", status);
oharboe's avatar
oharboe committed
1477
1478
1479
1480

	return ERROR_OK;
}

1481
1482
static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
1483
1484
1485
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;

1486
1487
	if (argc == 1)
	{
1488
		bool use_new_table;
1489
		if (strcmp(args[0], "short") == 0)
1490
			use_new_table = true;
1491
		else if (strcmp(args[0], "long") == 0)
1492
1493
			use_new_table = false;
		else
1494
			return ERROR_COMMAND_SYNTAX_ERROR;
1495
1496

		tap_use_new_tms_table(use_new_table);
1497
1498
	}

1499
1500
	command_print(cmd_ctx, "tms sequence is  %s",
			tap_uses_new_tms_table() ? "short": "long");
1501
1502
1503

	return ERROR_OK;
}
For faster browsing, not all history is shown. View entire blame