target.c 110 KB
Newer Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
			Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
			return JIM_ERR;
	}
	if (len == 0) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
		return JIM_ERR;
	}
	if ((addr + (len * width)) < addr) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
		return JIM_ERR;
	}
	/* absurd transfer size? */
	if (len > 65536) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
		return JIM_ERR;
oharboe's avatar
oharboe committed
3019
3020
	}

3021
3022
3023
3024
3025
3026
3027
	if ((width == 1) ||
		((width == 2) && ((addr & 1) == 0)) ||
		((width == 4) && ((addr & 3) == 0))) {
		/* all is well */
	} else {
		char buf[100];
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
oharboe's avatar
oharboe committed
3028
		sprintf(buf, "mem2array address: 0x%08x is not aligned for %d byte reads", addr, width);
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
		Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
		return JIM_ERR;
	}

	/* Transfer loop */

	/* index counter */
	n = 0;
	/* assume ok */
	e = JIM_OK;
	while (len) {
		/* Slurp... in buffer size chunks */
oharboe's avatar
oharboe committed
3041

3042
3043
3044
3045
		count = len; /* in objects.. */
		if (count > (sizeof(buffer)/width)) {
			count = (sizeof(buffer)/width);
		}
oharboe's avatar
oharboe committed
3046

zwelch's avatar
zwelch committed
3047
		retval = target_read_memory( target, addr, width, count, buffer );
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
		if (retval != ERROR_OK) {
			/* BOO !*/
			LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
			Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
			e = JIM_ERR;
			len = 0;
		} else {
			v = 0; /* shut up gcc */
			for (i = 0 ;i < count ;i++, n++) {
				switch (width) {
					case 4:
						v = target_buffer_get_u32(target, &buffer[i*width]);
						break;
					case 2:
						v = target_buffer_get_u16(target, &buffer[i*width]);
						break;
					case 1:
						v = buffer[i] & 0x0ff;
						break;
				}
				new_int_array_element(interp, varname, n, v);
			}
			len -= count;
		}
	}
oharboe's avatar
oharboe committed
3074

3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
	Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));

	return JIM_OK;
}

static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
{
	char *namebuf;
	Jim_Obj *nameObjPtr, *valObjPtr;
	int result;
	long l;

	namebuf = alloc_printf("%s(%d)", varname, idx);
	if (!namebuf)
		return JIM_ERR;

	nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
	if (!nameObjPtr)
	{
		free(namebuf);
		return JIM_ERR;
	}

	Jim_IncrRefCount(nameObjPtr);
	valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
	Jim_DecrRefCount(interp, nameObjPtr);
	free(namebuf);
	if (valObjPtr == NULL)
		return JIM_ERR;

	result = Jim_GetLong(interp, valObjPtr, &l);
	/* printf("%s(%d) => 0%08x\n", varname, idx, val); */
	*val = l;
	return result;
}

static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	command_context_t *context;
3114
	target_t *target;
3115

3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
	context = Jim_GetAssocData(interp, "context");
	if (context == NULL){
		LOG_ERROR("array2mem: no command context");
		return JIM_ERR;
	}
	target = get_current_target(context);
	if (target == NULL){
		LOG_ERROR("array2mem: no current target");
		return JIM_ERR;
	}
3126

3127
	return target_array2mem( interp,target, argc-1, argv+1 );
3128
}
3129

3130
3131
static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
{
3132
3133
	long l;
	u32 width;
3134
	int len;
3135
3136
3137
3138
3139
	u32 addr;
	u32 count;
	u32 v;
	const char *varname;
	u8 buffer[4096];
3140
3141
	int  n, e, retval;
	u32 i;
3142
3143
3144

	/* argv[1] = name of array to get the data
	 * argv[2] = desired width
oharboe's avatar
oharboe committed
3145
	 * argv[3] = memory address
3146
3147
	 * argv[4] = count to write
	 */
3148
	if (argc != 4) {
3149
3150
3151
		Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
		return JIM_ERR;
	}
3152
	varname = Jim_GetString(argv[0], &len);
3153
3154
	/* given "foo" get space for worse case "foo(%d)" .. add 20 */

3155
	e = Jim_GetLong(interp, argv[1], &l);
3156
3157
3158
3159
	width = l;
	if (e != JIM_OK) {
		return e;
	}
oharboe's avatar
oharboe committed
3160

3161
	e = Jim_GetLong(interp, argv[2], &l);
3162
3163
3164
3165
	addr = l;
	if (e != JIM_OK) {
		return e;
	}
3166
	e = Jim_GetLong(interp, argv[3], &l);
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
	len = l;
	if (e != JIM_OK) {
		return e;
	}
	switch (width) {
		case 8:
			width = 1;
			break;
		case 16:
			width = 2;
			break;
		case 32:
			width = 4;
			break;
		default:
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
			Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
			return JIM_ERR;
	}
	if (len == 0) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
		return JIM_ERR;
	}
	if ((addr + (len * width)) < addr) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
		return JIM_ERR;
	}
	/* absurd transfer size? */
	if (len > 65536) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
		return JIM_ERR;
oharboe's avatar
oharboe committed
3201
3202
	}

3203
3204
3205
3206
3207
3208
3209
	if ((width == 1) ||
		((width == 2) && ((addr & 1) == 0)) ||
		((width == 4) && ((addr & 3) == 0))) {
		/* all is well */
	} else {
		char buf[100];
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
oharboe's avatar
oharboe committed
3210
		sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
		Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
		return JIM_ERR;
	}

	/* Transfer loop */

	/* index counter */
	n = 0;
	/* assume ok */
	e = JIM_OK;
	while (len) {
		/* Slurp... in buffer size chunks */
oharboe's avatar
oharboe committed
3223

3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
		count = len; /* in objects.. */
		if (count > (sizeof(buffer)/width)) {
			count = (sizeof(buffer)/width);
		}

		v = 0; /* shut up gcc */
		for (i = 0 ;i < count ;i++, n++) {
			get_int_array_element(interp, varname, n, &v);
			switch (width) {
			case 4:
				target_buffer_set_u32(target, &buffer[i*width], v);
				break;
			case 2:
				target_buffer_set_u16(target, &buffer[i*width], v);
				break;
			case 1:
				buffer[i] = v & 0x0ff;
				break;
			}
		}
		len -= count;

zwelch's avatar
zwelch committed
3246
		retval = target_write_memory(target, addr, width, count, buffer);
3247
3248
3249
3250
		if (retval != ERROR_OK) {
			/* BOO !*/
			LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3251
			Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3252
3253
3254
3255
			e = JIM_ERR;
			len = 0;
		}
	}
oharboe's avatar
oharboe committed
3256

3257
3258
3259
3260
	Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));

	return JIM_OK;
}
3261

3262
void target_all_handle_event( enum target_event e )
3263
3264
3265
{
	target_t *target;

3266
	LOG_DEBUG( "**all*targets: event: %d, %s",
3267
3268
			e,
			Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3269
3270
3271
3272
3273
3274
3275
3276

	target = all_targets;
	while (target){
		target_handle_event( target, e );
		target = target->next;
	}
}

3277
void target_handle_event( target_t *target, enum target_event e )
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
{
	target_event_action_t *teap;
	int done;

	teap = target->event_action;

	done = 0;
	while( teap ){
		if( teap->event == e ){
			done = 1;
			LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
					   target->target_number,
					   target->cmd_name,
zwelch's avatar
zwelch committed
3291
					   target_get_name(target),
3292
3293
3294
					   e,
					   Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
					   Jim_GetString( teap->body, NULL ) );
3295
3296
3297
3298
			if (Jim_EvalObj( interp, teap->body )!=JIM_OK)
			{
				Jim_PrintErrorMessage(interp);
			}
3299
3300
3301
3302
		}
		teap = teap->next;
	}
	if( !done ){
3303
		LOG_DEBUG( "event: %d %s - no action",
3304
3305
3306
3307
3308
3309
3310
				   e,
				   Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
	}
}

enum target_cfg_param {
	TCFG_TYPE,
3311
	TCFG_EVENT,
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
	TCFG_WORK_AREA_VIRT,
	TCFG_WORK_AREA_PHYS,
	TCFG_WORK_AREA_SIZE,
	TCFG_WORK_AREA_BACKUP,
	TCFG_ENDIAN,
	TCFG_VARIANT,
	TCFG_CHAIN_POSITION,
};

static Jim_Nvp nvp_config_opts[] = {
	{ .name = "-type",             .value = TCFG_TYPE },
	{ .name = "-event",            .value = TCFG_EVENT },
	{ .name = "-work-area-virt",   .value = TCFG_WORK_AREA_VIRT },
	{ .name = "-work-area-phys",   .value = TCFG_WORK_AREA_PHYS },
	{ .name = "-work-area-size",   .value = TCFG_WORK_AREA_SIZE },
	{ .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
	{ .name = "-endian" ,          .value = TCFG_ENDIAN },
	{ .name = "-variant",          .value = TCFG_VARIANT },
	{ .name = "-chain-position",   .value = TCFG_CHAIN_POSITION },
3331

3332
3333
	{ .name = NULL, .value = -1 }
};
3334

3335
static int target_configure( Jim_GetOptInfo *goi, target_t *target )
3336
3337
3338
3339
3340
3341
3342
3343
{
	Jim_Nvp *n;
	Jim_Obj *o;
	jim_wide w;
	char *cp;
	int e;

	/* parse config or cget options ... */
3344
	while( goi->argc > 0 ){
3345
		Jim_SetEmptyResult( goi->interp );
3346
		/* Jim_GetOpt_Debug( goi ); */
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379

		if( target->type->target_jim_configure ){
			/* target defines a configure function */
			/* target gets first dibs on parameters */
			e = (*(target->type->target_jim_configure))( target, goi );
			if( e == JIM_OK ){
				/* more? */
				continue;
			}
			if( e == JIM_ERR ){
				/* An error */
				return e;
			}
			/* otherwise we 'continue' below */
		}
		e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
		if( e != JIM_OK ){
			Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
			return e;
		}
		switch( n->value ){
		case TCFG_TYPE:
			/* not setable */
			if( goi->isconfigure ){
				Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
				return JIM_ERR;
			} else {
			no_params:
				if( goi->argc != 0 ){
					Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
					return JIM_ERR;
				}
			}
zwelch's avatar
zwelch committed
3380
			Jim_SetResultString( goi->interp, target_get_name(target), -1 );
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
			/* loop for more */
			break;
		case TCFG_EVENT:
			if( goi->argc == 0 ){
				Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
				return JIM_ERR;
			}

			e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
			if( e != JIM_OK ){
				Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
				return e;
			}

			if( goi->isconfigure ){
3396
				if( goi->argc != 1 ){
3397
3398
					Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
					return JIM_ERR;
3399
				}
3400
3401
3402
3403
3404
3405
3406
			} else {
				if( goi->argc != 0 ){
					Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
					return JIM_ERR;
				}
			}

3407
			{
3408
				target_event_action_t *teap;
3409

3410
3411
3412
				teap = target->event_action;
				/* replace existing? */
				while( teap ){
3413
					if( teap->event == (enum target_event)n->value ){
3414
3415
3416
3417
						break;
					}
					teap = teap->next;
				}
3418

3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
				if( goi->isconfigure ){
					if( teap == NULL ){
						/* create new */
						teap = calloc( 1, sizeof(*teap) );
					}
					teap->event = n->value;
					Jim_GetOpt_Obj( goi, &o );
					if( teap->body ){
						Jim_DecrRefCount( interp, teap->body );
					}
					teap->body  = Jim_DuplicateObj( goi->interp, o );
					/*
3431
					 * FIXME:
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
					 *     Tcl/TK - "tk events" have a nice feature.
					 *     See the "BIND" command.
					 *    We should support that here.
					 *     You can specify %X and %Y in the event code.
					 *     The idea is: %T - target name.
					 *     The idea is: %N - target number
					 *     The idea is: %E - event name.
					 */
					Jim_IncrRefCount( teap->body );

					/* add to head of event list */
					teap->next = target->event_action;
					target->event_action = teap;
					Jim_SetEmptyResult(goi->interp);
				} else {
					/* get */
					if( teap == NULL ){
						Jim_SetEmptyResult( goi->interp );
					} else {
						Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
					}
				}
			}
			/* loop for more */
			break;

		case TCFG_WORK_AREA_VIRT:
			if( goi->isconfigure ){
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
				if( e != JIM_OK ){
					return e;
				}
				target->working_area_virt = w;
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3470
			}
3471
3472
3473
			Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
			/* loop for more */
			break;
3474

3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
		case TCFG_WORK_AREA_PHYS:
			if( goi->isconfigure ){
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
				if( e != JIM_OK ){
					return e;
				}
				target->working_area_phys = w;
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3487
			}
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
			Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
			/* loop for more */
			break;

		case TCFG_WORK_AREA_SIZE:
			if( goi->isconfigure ){
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
				if( e != JIM_OK ){
					return e;
				}
				target->working_area_size = w;
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3504
			}
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
			Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
			/* loop for more */
			break;

		case TCFG_WORK_AREA_BACKUP:
			if( goi->isconfigure ){
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
				if( e != JIM_OK ){
					return e;
				}
				/* make this exactly 1 or 0 */
				target->backup_working_area = (!!w);
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3522
			}
3523
			Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
			/* loop for more e*/
			break;

		case TCFG_ENDIAN:
			if( goi->isconfigure ){
				e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
				if( e != JIM_OK ){
					Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
					return e;
				}
				target->endianness = n->value;
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
			}
			n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
			if( n->name == NULL ){
				target->endianness = TARGET_LITTLE_ENDIAN;
				n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3544
			}
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
			Jim_SetResultString( goi->interp, n->name, -1 );
			/* loop for more */
			break;

		case TCFG_VARIANT:
			if( goi->isconfigure ){
				if( goi->argc < 1 ){
					Jim_SetResult_sprintf( goi->interp,
										   "%s ?STRING?",
										   n->name );
					return JIM_ERR;
				}
				if( target->variant ){
					free((void *)(target->variant));
				}
				e = Jim_GetOpt_String( goi, &cp, NULL );
				target->variant = strdup(cp);
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3566
			}
3567
3568
3569
3570
3571
			Jim_SetResultString( goi->interp, target->variant,-1 );
			/* loop for more */
			break;
		case TCFG_CHAIN_POSITION:
			if( goi->isconfigure ){
3572
3573
				Jim_Obj *o;
				jtag_tap_t *tap;
3574
				target_free_all_working_areas(target);
3575
				e = Jim_GetOpt_Obj( goi, &o );
3576
3577
3578
				if( e != JIM_OK ){
					return e;
				}
3579
				tap = jtag_tap_by_jim_obj( goi->interp, o );
3580
				if( tap == NULL ){
3581
					return JIM_ERR;
3582
				}
3583
				/* make this exactly 1 or 0 */
3584
				target->tap = tap;
3585
3586
3587
3588
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3589
			}
3590
			Jim_SetResultString( interp, target->tap->dotted_name, -1 );
3591
3592
3593
			/* loop for more e*/
			break;
		}
3594
	} /* while( goi->argc ) */
3595
3596


3597
		/* done - we return */
3598
3599
3600
3601
	return JIM_OK;
}

/** this is the 'tcl' handler for the target specific command */
3602
static int tcl_target_func( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
{
	Jim_GetOptInfo goi;
	jim_wide a,b,c;
	int x,y,z;
	u8  target_buf[32];
	Jim_Nvp *n;
	target_t *target;
	struct command_context_s *cmd_ctx;
	int e;

	enum {
		TS_CMD_CONFIGURE,
		TS_CMD_CGET,
3616

3617
3618
3619
3620
		TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
		TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
		TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
		TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3621
		TS_CMD_EXAMINE,
3622
3623
3624
3625
3626
3627
		TS_CMD_POLL,
		TS_CMD_RESET,
		TS_CMD_HALT,
		TS_CMD_WAITSTATE,
		TS_CMD_EVENTLIST,
		TS_CMD_CURSTATE,
3628
		TS_CMD_INVOKE_EVENT,
3629
	};
3630

3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
	static const Jim_Nvp target_options[] = {
		{ .name = "configure", .value = TS_CMD_CONFIGURE },
		{ .name = "cget", .value = TS_CMD_CGET },
		{ .name = "mww", .value = TS_CMD_MWW },
		{ .name = "mwh", .value = TS_CMD_MWH },
		{ .name = "mwb", .value = TS_CMD_MWB },
		{ .name = "mdw", .value = TS_CMD_MDW },
		{ .name = "mdh", .value = TS_CMD_MDH },
		{ .name = "mdb", .value = TS_CMD_MDB },
		{ .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
		{ .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
		{ .name = "eventlist", .value = TS_CMD_EVENTLIST },
		{ .name = "curstate",  .value = TS_CMD_CURSTATE },

		{ .name = "arp_examine", .value = TS_CMD_EXAMINE },
		{ .name = "arp_poll", .value = TS_CMD_POLL },
		{ .name = "arp_reset", .value = TS_CMD_RESET },
		{ .name = "arp_halt", .value = TS_CMD_HALT },
		{ .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3650
		{ .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666

		{ .name = NULL, .value = -1 },
	};

	/* go past the "command" */
	Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );

	target = Jim_CmdPrivData( goi.interp );
	cmd_ctx = Jim_GetAssocData(goi.interp, "context");

	/* commands here are in an NVP table */
	e = Jim_GetOpt_Nvp( &goi, target_options, &n );
	if( e != JIM_OK ){
		Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
		return e;
	}
3667
	/* Assume blank result */
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
	Jim_SetEmptyResult( goi.interp );

	switch( n->value ){
	case TS_CMD_CONFIGURE:
		if( goi.argc < 2 ){
			Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
			return JIM_ERR;
		}
		goi.isconfigure = 1;
		return target_configure( &goi, target );
	case TS_CMD_CGET:
		// some things take params
		if( goi.argc < 1 ){
			Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
			return JIM_ERR;
		}
		goi.isconfigure = 0;
		return target_configure( &goi, target );
		break;
	case TS_CMD_MWW:
	case TS_CMD_MWH:
	case TS_CMD_MWB:
		/* argv[0] = cmd
		 * argv[1] = address
		 * argv[2] = data
		 * argv[3] = optional count.
		 */
3695

3696
3697
3698
3699
3700
3701
3702
		if( (goi.argc == 3) || (goi.argc == 4) ){
			/* all is well */
		} else {
		mwx_error:
			Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
			return JIM_ERR;
		}
3703

3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
		e = Jim_GetOpt_Wide( &goi, &a );
		if( e != JIM_OK ){
			goto mwx_error;
		}

		e = Jim_GetOpt_Wide( &goi, &b );
		if( e != JIM_OK ){
			goto mwx_error;
		}
		if( goi.argc ){
			e = Jim_GetOpt_Wide( &goi, &c );
			if( e != JIM_OK ){
				goto mwx_error;
			}
		} else {
			c = 1;
		}
3721

3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
		switch( n->value ){
		case TS_CMD_MWW:
			target_buffer_set_u32( target, target_buf, b );
			b = 4;
			break;
		case TS_CMD_MWH:
			target_buffer_set_u16( target, target_buf, b );
			b = 2;
			break;
		case TS_CMD_MWB:
			target_buffer_set_u8( target, target_buf, b );
			b = 1;
			break;
		}
		for( x = 0 ; x < c ; x++ ){
zwelch's avatar
zwelch committed
3737
			e = target_write_memory( target, a, b, 1, target_buf );
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
			if( e != ERROR_OK ){
				Jim_SetResult_sprintf( interp, "Error writing @ 0x%08x: %d\n", (int)(a), e );
				return JIM_ERR;
			}
			/* b = width */
			a = a + b;
		}
		return JIM_OK;
		break;

		/* display */
	case TS_CMD_MDW:
	case TS_CMD_MDH:
	case TS_CMD_MDB:
		/* argv[0] = command
		 * argv[1] = address
		 * argv[2] = optional count
		 */
		if( (goi.argc == 2) || (goi.argc == 3) ){
			Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
			return JIM_ERR;
		}
		e = Jim_GetOpt_Wide( &goi, &a );
		if( e != JIM_OK ){
			return JIM_ERR;
		}
		if( goi.argc ){
			e = Jim_GetOpt_Wide( &goi, &c );
			if( e != JIM_OK ){
				return JIM_ERR;
			}
		} else {
			c = 1;
		}
		b = 1; /* shut up gcc */
		switch( n->value ){
		case TS_CMD_MDW:
			b =  4;
			break;
		case TS_CMD_MDH:
			b = 2;
			break;
		case TS_CMD_MDB:
			b = 1;
			break;
		}

		/* convert to "bytes" */
		c = c * b;
		/* count is now in 'BYTES' */
		while( c > 0 ){
			y = c;
			if( y > 16 ){
				y = 16;
			}
zwelch's avatar
zwelch committed
3793
			e = target_read_memory( target, a, b, y / b, target_buf );
3794
3795
3796
3797
			if( e != ERROR_OK ){
				Jim_SetResult_sprintf( interp, "error reading target @ 0x%08lx", (int)(a) );
				return JIM_ERR;
			}
3798

3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
			Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
			switch( b ){
			case 4:
				for( x = 0 ; (x < 16) && (x < y) ; x += 4 ){
					z = target_buffer_get_u32( target, &(target_buf[ x * 4 ]) );
					Jim_fprintf( interp, interp->cookie_stdout, "%08x ", (int)(z) );
				}
				for( ; (x < 16) ; x += 4 ){
					Jim_fprintf( interp, interp->cookie_stdout, "         " );
				}
				break;
			case 2:
				for( x = 0 ; (x < 16) && (x < y) ; x += 2 ){
					z = target_buffer_get_u16( target, &(target_buf[ x * 2 ]) );
					Jim_fprintf( interp, interp->cookie_stdout, "%04x ", (int)(z) );
				}
				for( ; (x < 16) ; x += 2 ){
					Jim_fprintf( interp, interp->cookie_stdout, "     " );
				}
				break;
			case 1:
			default:
				for( x = 0 ; (x < 16) && (x < y) ; x += 1 ){
					z = target_buffer_get_u8( target, &(target_buf[ x * 4 ]) );
					Jim_fprintf( interp, interp->cookie_stdout, "%02x ", (int)(z) );
				}
				for( ; (x < 16) ; x += 1 ){
					Jim_fprintf( interp, interp->cookie_stdout, "   " );
				}
				break;
			}
			/* ascii-ify the bytes */
			for( x = 0 ; x < y ; x++ ){
3832
				if( (target_buf[x] >= 0x20) &&
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
					(target_buf[x] <= 0x7e) ){
					/* good */
				} else {
					/* smack it */
					target_buf[x] = '.';
				}
			}
			/* space pad  */
			while( x < 16 ){
				target_buf[x] = ' ';
				x++;
			}
			/* terminate */
			target_buf[16] = 0;
			/* print - with a newline */
			Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
			/* NEXT... */
			c -= 16;
			a += 16;
		}
		return JIM_OK;
	case TS_CMD_MEM2ARRAY:
		return target_mem2array( goi.interp, target, goi.argc, goi.argv );
		break;
	case TS_CMD_ARRAY2MEM:
		return target_array2mem( goi.interp, target, goi.argc, goi.argv );
		break;
	case TS_CMD_EXAMINE:
		if( goi.argc ){
3862
			Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3863
3864
			return JIM_ERR;
		}
zwelch's avatar
zwelch committed
3865
3866
		if (!target->tap->enabled)
			goto err_tap_disabled;
3867
3868
3869
3870
3871
3872
3873
3874
		e = target->type->examine( target );
		if( e != ERROR_OK ){
			Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
			return JIM_ERR;
		}
		return JIM_OK;
	case TS_CMD_POLL:
		if( goi.argc ){
3875
			Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3876
3877
			return JIM_ERR;
		}
zwelch's avatar
zwelch committed
3878
3879
		if (!target->tap->enabled)
			goto err_tap_disabled;
3880
		if( !(target_was_examined(target)) ){
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
			e = ERROR_TARGET_NOT_EXAMINED;
		} else {
			e = target->type->poll( target );
		}
		if( e != ERROR_OK ){
			Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
			return JIM_ERR;
		} else {
			return JIM_OK;
		}
		break;
	case TS_CMD_RESET:
3893
3894
		if( goi.argc != 2 ){
			Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL");
3895
3896
3897
3898
3899
3900
3901
			return JIM_ERR;
		}
		e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
		if( e != JIM_OK ){
			Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
			return e;
		}
3902
		/* the halt or not param */
3903
3904
3905
3906
		e = Jim_GetOpt_Wide( &goi, &a);
		if( e != JIM_OK ){
			return e;
		}
zwelch's avatar
zwelch committed
3907
3908
		if (!target->tap->enabled)
			goto err_tap_disabled;
3909
		/* determine if we should halt or not. */
3910
		target->reset_halt = !!a;
3911
		/* When this happens - all workareas are invalid. */
3912
3913
		target_free_all_working_areas_restore(target, 0);

3914
		/* do the assert */
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
		if( n->value == NVP_ASSERT ){
			target->type->assert_reset( target );
		} else {
			target->type->deassert_reset( target );
		}
		return JIM_OK;
	case TS_CMD_HALT:
		if( goi.argc ){
			Jim_WrongNumArgs( goi.interp, 0, argv, "halt [no parameters]");
			return JIM_ERR;
		}
zwelch's avatar
zwelch committed
3926
3927
		if (!target->tap->enabled)
			goto err_tap_disabled;
3928
3929
3930
		target->type->halt( target );
		return JIM_OK;
	case TS_CMD_WAITSTATE:
3931
		/* params:  <name>  statename timeoutmsecs */
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
		if( goi.argc != 2 ){
			Jim_SetResult_sprintf( goi.interp, "%s STATENAME TIMEOUTMSECS", n->name );
			return JIM_ERR;
		}
		e = Jim_GetOpt_Nvp( &goi, nvp_target_state, &n );
		if( e != JIM_OK ){
			Jim_GetOpt_NvpUnknown( &goi, nvp_target_state,1 );
			return e;
		}
		e = Jim_GetOpt_Wide( &goi, &a );
		if( e != JIM_OK ){
			return e;
		}
zwelch's avatar
zwelch committed
3945
3946
		if (!target->tap->enabled)
			goto err_tap_disabled;
3947
		e = target_wait_state( target, n->value, a );
3948
		if( e != ERROR_OK ){
3949
			Jim_SetResult_sprintf( goi.interp,
3950
								   "target: %s wait %s fails (%d) %s",
3951
3952
								   target->cmd_name,
								   n->name,
3953
								   e, target_strerror_safe(e) );
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
			return JIM_ERR;
		} else {
			return JIM_OK;
		}
	case TS_CMD_EVENTLIST:
		/* List for human, Events defined for this target.
		 * scripts/programs should use 'name cget -event NAME'
		 */
		{
			target_event_action_t *teap;
			teap = target->event_action;
			command_print( cmd_ctx, "Event actions for target (%d) %s\n",
3966
						   target->target_number,
3967
3968
3969
3970
						   target->cmd_name );
			command_print( cmd_ctx, "%-25s | Body", "Event");
			command_print( cmd_ctx, "------------------------- | ----------------------------------------");
			while( teap ){
3971
				command_print( cmd_ctx,
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
							   "%-25s | %s",
							   Jim_Nvp_value2name_simple( nvp_target_event, teap->event )->name,
							   Jim_GetString( teap->body, NULL ) );
				teap = teap->next;
			}
			command_print( cmd_ctx, "***END***");
			return JIM_OK;
		}
	case TS_CMD_CURSTATE:
		if( goi.argc != 0 ){
			Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]");
			return JIM_ERR;
		}
3985
		Jim_SetResultString( goi.interp,
3986
3987
							 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
		return JIM_OK;
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
	case TS_CMD_INVOKE_EVENT:
		if( goi.argc != 1 ){
			Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name);
			return JIM_ERR;
		}
		e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n );
		if( e != JIM_OK ){
			Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 );
			return e;
		}
		target_handle_event( target, n->value );
		return JIM_OK;
4000
	}
For faster browsing, not all history is shown. View entire blame