blackmagic-sys 0.1.0

Bindings to Blackmagic-debug host functions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
 * This file is part of the Black Magic Debug project.
 *
 * Copyright (C) 2020-2021 Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de)
 * Copyright (C) 2022-2023 1BitSquared <info@1bitsquared.com>
 * Modified by Rachel Mant <git@dragonmux.network>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* Implements core platform-specific functionality for BMDA */

#if defined(_WIN32) || defined(__CYGWIN__)
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

#include "general.h"
#include "platform.h"
#include "jtagtap.h"
#include "swd.h"
#include "target.h"
#include "target_internal.h"
#include "adiv5.h"
#include "timing.h"
#include "cli.h"
#include "gdb_if.h"
#include "gdb_packet.h"
#include <signal.h>

#ifdef ENABLE_RTT
#include "rtt_if.h"
#endif

#include "bmp_remote.h"
#include "bmp_hosted.h"
#if HOSTED_BMP_ONLY == 0
#include "stlinkv2.h"
#include "ftdi_bmp.h"
#include "jlink.h"
#include "cmsis_dap.h"
#endif

bmda_probe_s bmda_probe_info;

jtag_proc_s jtag_proc;
swd_proc_s swd_proc;

static bmda_cli_options_s cl_opts;

void gdb_ident(char *p, int count)
{
	snprintf(p, count, "%s (%s), %s", bmda_probe_info.manufacturer, bmda_probe_info.product, bmda_probe_info.version);
}

static void exit_function(void)
{
#if HOSTED_BMP_ONLY == 0
	if (bmda_probe_info.type == PROBE_TYPE_STLINK_V2)
		stlink_deinit();
#endif

	libusb_exit_function(&bmda_probe_info);

	switch (bmda_probe_info.type) {
#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_CMSIS_DAP:
		dap_exit_function();
		break;
#endif

	default:
		break;
	}

#ifdef ENABLE_RTT
	rtt_if_exit();
#endif
#if HOSTED_BMP_ONLY == 0
	if (bmda_probe_info.libusb_ctx)
		libusb_exit(bmda_probe_info.libusb_ctx);
#endif
	fflush(stdout);
}

/* SIGTERM handler. */
static void sigterm_handler(int sig)
{
	(void)sig;
	exit(0);
}

void platform_init(int argc, char **argv)
{
#if defined(_WIN32) || defined(__CYGWIN__)
	SetConsoleOutputCP(CP_UTF8);
#endif
	cl_init(&cl_opts, argc, argv);
	atexit(exit_function);
	signal(SIGTERM, sigterm_handler);
	signal(SIGINT, sigterm_handler);

	if (cl_opts.opt_device)
		bmda_probe_info.type = PROBE_TYPE_BMP;
	else if (find_debuggers(&cl_opts, &bmda_probe_info))
		exit(1);

	if (cl_opts.opt_list_only)
		exit(0);

	bmp_ident(&bmda_probe_info);

	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		if (!serial_open(&cl_opts, bmda_probe_info.serial) || !remote_init(cl_opts.opt_tpwr))
			exit(1);
		break;

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_STLINK_V2:
		if (!stlink_init())
			exit(1);
		break;

	case PROBE_TYPE_CMSIS_DAP:
		if (!dap_init())
			exit(1);
		break;

	case PROBE_TYPE_FTDI:
		if (!ftdi_bmp_init(&cl_opts))
			exit(1);
		break;

	case PROBE_TYPE_JLINK:
		if (!jlink_init())
			exit(1);
		break;
#endif

	default:
		exit(1);
	}

	if (cl_opts.opt_mode != BMP_MODE_DEBUG)
		exit(cl_execute(&cl_opts));
	else {
		gdb_if_init();

#ifdef ENABLE_RTT
		rtt_if_init();
#endif
	}
}

bool bmda_swd_scan(const uint32_t targetid)
{
	bmda_probe_info.is_jtag = false;
	platform_max_frequency_set(cl_opts.opt_max_swj_frequency);

	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
	case PROBE_TYPE_FTDI:
	case PROBE_TYPE_CMSIS_DAP:
	case PROBE_TYPE_JLINK:
		return adiv5_swd_scan(targetid);

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_STLINK_V2:
		return stlink_swd_scan();
#endif

	default:
		return false;
	}
}

bool bmda_swd_dp_init(adiv5_debug_port_s *dp)
{
#if HOSTED_BMP_ONLY == 1
	(void)dp;
#endif
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		return remote_swd_init();

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_CMSIS_DAP:
		return dap_swd_init(dp);

	case PROBE_TYPE_STLINK_V2:
		return false;

	case PROBE_TYPE_JLINK:
		return jlink_swd_init(dp);

	case PROBE_TYPE_FTDI:
		return ftdi_swd_init();
#endif

	default:
		return false;
	}
}

void bmda_add_jtag_dev(const uint32_t dev_index, const jtag_dev_s *const jtag_dev)
{
	if (bmda_probe_info.type == PROBE_TYPE_BMP)
		remote_add_jtag_dev(dev_index, jtag_dev);
}

bool bmda_jtag_scan(void)
{
	bmda_probe_info.is_jtag = true;

	platform_max_frequency_set(cl_opts.opt_max_swj_frequency);

	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
	case PROBE_TYPE_FTDI:
	case PROBE_TYPE_JLINK:
	case PROBE_TYPE_CMSIS_DAP:
		return jtag_scan();

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_STLINK_V2:
		return stlink_jtag_scan();
#endif

	default:
		return false;
	}
}

bool bmda_jtag_init(void)
{
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		return remote_jtag_init();

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_STLINK_V2:
		return false;

	case PROBE_TYPE_FTDI:
		return ftdi_jtag_init();

	case PROBE_TYPE_JLINK:
		return jlink_jtag_init();

	case PROBE_TYPE_CMSIS_DAP:
		return dap_jtag_init();
#endif

	default:
		return false;
	}
}

void bmda_adiv5_dp_init(adiv5_debug_port_s *const dp)
{
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		if (cl_opts.opt_no_hl) {
			DEBUG_WARN("Not using HL commands\n");
			break;
		}
		remote_adiv5_dp_init(dp);
		break;

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_STLINK_V2:
		stlink_adiv5_dp_init(dp);
		break;

	case PROBE_TYPE_CMSIS_DAP:
		dap_adiv5_dp_init(dp);
		break;
#endif

	default:
		break;
	}
}

void bmda_jtag_dp_init(adiv5_debug_port_s *dp)
{
#if HOSTED_BMP_ONLY == 0
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_STLINK_V2:
		stlink_jtag_dp_init(dp);
		break;
	case PROBE_TYPE_CMSIS_DAP:
		dap_jtag_dp_init(dp);
		break;
	default:
		break;
	}
#else
	(void)dp;
#endif
}

char *bmda_adaptor_ident(void)
{
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_NONE:
		return "None";

	case PROBE_TYPE_BMP:
		return "BMP";

	case PROBE_TYPE_STLINK_V2:
		return "ST-Link v2";

	case PROBE_TYPE_FTDI:
		return "FTDI";

	case PROBE_TYPE_CMSIS_DAP:
		return "CMSIS-DAP";

	case PROBE_TYPE_JLINK:
		return "J-Link";

	default:
		return NULL;
	}
}

const char *platform_target_voltage(void)
{
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		return remote_target_voltage();

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_STLINK_V2:
		return stlink_target_voltage();

	case PROBE_TYPE_FTDI:
		return ftdi_target_voltage();

	case PROBE_TYPE_JLINK:
		return jlink_target_voltage_string();
#endif

	default:
		return NULL;
	}
}

void platform_nrst_set_val(bool assert)
{
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		remote_nrst_set_val(assert);
		break;

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_STLINK_V2:
		stlink_nrst_set_val(assert);
		break;

	case PROBE_TYPE_JLINK:
		jlink_nrst_set_val(assert);
		break;

	case PROBE_TYPE_FTDI:
		libftdi_nrst_set_val(assert);
		break;

	case PROBE_TYPE_CMSIS_DAP:
		dap_nrst_set_val(assert);
		break;
#endif

	default:
		break;
	}
}

bool platform_nrst_get_val(void)
{
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		return remote_nrst_get_val();

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_STLINK_V2:
		return stlink_nrst_get_val();

	case PROBE_TYPE_JLINK:
		return jlink_nrst_get_val();

	case PROBE_TYPE_FTDI:
		return ftdi_nrst_get_val();
#endif

	default:
		return false;
	}
}

void platform_max_frequency_set(uint32_t freq)
{
	if (!freq)
		return;

	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		remote_max_frequency_set(freq);
		break;

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_CMSIS_DAP:
		dap_max_frequency(freq);
		break;

	case PROBE_TYPE_FTDI:
		ftdi_max_frequency_set(freq);
		break;

	case PROBE_TYPE_STLINK_V2:
		stlink_max_frequency_set(freq);
		break;

	case PROBE_TYPE_JLINK:
		jlink_max_frequency_set(freq);
		break;
#endif

	default:
		DEBUG_WARN("Setting max SWD/JTAG frequency not yet implemented\n");
		break;
	}

	const uint32_t actual_freq = platform_max_frequency_get();
	if (actual_freq == FREQ_FIXED)
		DEBUG_INFO("Device has fixed frequency for %s\n", bmda_probe_info.is_jtag ? "JTAG" : "SWD");
	else {
		const uint16_t freq_mhz = actual_freq / 1000000U;
		const uint16_t freq_khz = (actual_freq / 1000U) - (freq_mhz * 1000U);
		DEBUG_INFO("Speed set to %u.%03uMHz for %s\n", freq_mhz, freq_khz, bmda_probe_info.is_jtag ? "JTAG" : "SWD");
	}
}

uint32_t platform_max_frequency_get(void)
{
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		return remote_max_frequency_get();

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_CMSIS_DAP:
		return dap_max_frequency(0);

	case PROBE_TYPE_FTDI:
		return libftdi_max_frequency_get();

	case PROBE_TYPE_STLINK_V2:
		return stlink_max_frequency_get();

	case PROBE_TYPE_JLINK:
		return jlink_max_frequency_get();
#endif

	default:
		DEBUG_WARN("Reading max SWJ frequency not yet implemented\n");
		return 0;
	}
}

bool platform_target_set_power(const bool power)
{
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		return remote_target_set_power(power);

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_JLINK:
		return jlink_target_set_power(power);
#endif

	default:
		DEBUG_ERROR("Target power not available or not yet implemented\n");
		return false;
	}
}

bool platform_target_get_power(void)
{
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		return remote_target_get_power();

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_JLINK:
		return jlink_target_get_power();
#endif

	default:
		return false;
	}
}

uint32_t platform_target_voltage_sense(void)
{
	uint32_t target_voltage = 0;

	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP: {
		const char *const result = remote_target_voltage();
		if (result != NULL) {
			uint32_t units = 0;
			uint32_t tenths = 0;
			sscanf(result, "%" PRIu32 ".%" PRIu32, &units, &tenths);
			target_voltage = (units * 10U) + tenths;
		}
		break;
	}

#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_JLINK:
		target_voltage = jlink_target_voltage_sense();
		break;
#endif

	default:
		break;
	}

	return target_voltage;
}

void platform_buffer_flush(void)
{
	switch (bmda_probe_info.type) {
#if HOSTED_BMP_ONLY == 0
	case PROBE_TYPE_FTDI:
		ftdi_buffer_flush();
		break;
#endif

	default:
		break;
	}
}

void platform_pace_poll(void)
{
	if (!cl_opts.fast_poll)
		platform_delay(8);
}

void platform_target_clk_output_enable(const bool enable)
{
	switch (bmda_probe_info.type) {
	case PROBE_TYPE_BMP:
		remote_target_clk_output_enable(enable);
		break;

	default:
		break;
	}
}

static void decode_dp_access(const uint8_t addr, const uint8_t rnw, const uint32_t value)
{
	/* How a DP address should be decoded depends on the bank that's presently selected, so make a note of that */
	static uint8_t dp_bank = 0;
	const char *reg = NULL;

	/* Try to decode the requested address */
	switch (addr) {
	case 0x00U:
		reg = rnw ? "DPIDR" : "ABORT";
		break;
	case 0x04U:
		switch (dp_bank) {
		case 0:
			reg = rnw ? "STATUS" : "CTRL";
			break;
		case 1:
			reg = "DLCR";
			break;
		case 2:
			reg = "TARGETID";
			break;
		case 3:
			reg = "DLPIDR";
			break;
		case 4:
			reg = "EVENTSTAT";
			break;
		}
		break;
	case 0x08U:
		if (!rnw)
			dp_bank = value & 15U;
		reg = rnw ? "RESEND" : "SELECT";
		break;
	case 0x0cU:
		reg = rnw ? "RDBUFF" : "TARGETSEL";
		break;
	}

	if (reg)
		DEBUG_PROTO("%s: ", reg);
	else
		DEBUG_PROTO("Unknown DP register %02x: ", addr);
}

static void decode_ap_access(const uint8_t ap, const uint8_t addr)
{
	DEBUG_PROTO("AP %u ", ap);

	const char *reg = NULL;
	switch (addr) {
	case 0x00U:
		reg = "CSW";
		break;
	case 0x04U:
		reg = "TAR";
		break;
	case 0x0cU:
		reg = "DRW";
		break;
	case 0x10U:
		reg = "DB0";
		break;
	case 0x14U:
		reg = "DB1";
		break;
	case 0x18U:
		reg = "DB2";
		break;
	case 0x1cU:
		reg = "DB3";
		break;
	case 0xf8U:
		reg = "BASE";
		break;
	case 0xf4U:
		reg = "CFG";
		break;
	case 0xfcU:
		reg = "IDR";
		break;
	}

	if (reg)
		DEBUG_PROTO("%s: ", reg);
	else
		DEBUG_PROTO("Reserved(%02x): ", addr);
}

static void decode_access(const uint16_t addr, const uint8_t rnw, const uint32_t value)
{
	if (rnw)
		DEBUG_PROTO("Read ");
	else
		DEBUG_PROTO("Write ");

	if (addr < 0x100U)
		decode_dp_access(addr & 0xffU, rnw, value);
	else
		decode_ap_access(addr >> 8U, addr & 0xffU);
}

void adiv5_dp_write(adiv5_debug_port_s *dp, uint16_t addr, uint32_t value)
{
	decode_access(addr, ADIV5_LOW_WRITE, value);
	DEBUG_PROTO("0x%08" PRIx32 "\n", value);
	dp->low_access(dp, ADIV5_LOW_WRITE, addr, value);
}

uint32_t adiv5_dp_read(adiv5_debug_port_s *dp, uint16_t addr)
{
	uint32_t ret = dp->dp_read(dp, addr);
	decode_access(addr, ADIV5_LOW_READ, 0U);
	DEBUG_PROTO("0x%08" PRIx32 "\n", ret);
	return ret;
}

uint32_t adiv5_dp_error(adiv5_debug_port_s *dp)
{
	uint32_t ret = dp->error(dp, false);
	DEBUG_PROTO("DP Error 0x%08" PRIx32 "\n", ret);
	return ret;
}

uint32_t adiv5_dp_low_access(adiv5_debug_port_s *dp, uint8_t rnw, uint16_t addr, uint32_t value)
{
	uint32_t ret = dp->low_access(dp, rnw, addr, value);
	decode_access(addr, rnw, value);
	DEBUG_PROTO("0x%08" PRIx32 "\n", rnw ? ret : value);
	return ret;
}

uint32_t adiv5_ap_read(adiv5_access_port_s *ap, uint16_t addr)
{
	uint32_t ret = ap->dp->ap_read(ap, addr);
	decode_access(addr, ADIV5_LOW_READ, 0U);
	DEBUG_PROTO("0x%08" PRIx32 "\n", ret);
	return ret;
}

void adiv5_ap_write(adiv5_access_port_s *ap, uint16_t addr, uint32_t value)
{
	decode_access(addr, ADIV5_LOW_WRITE, value);
	DEBUG_PROTO("0x%08" PRIx32 "\n", value);
	ap->dp->ap_write(ap, addr, value);
}

void adiv5_mem_read(adiv5_access_port_s *ap, void *dest, uint32_t src, size_t len)
{
	ap->dp->mem_read(ap, dest, src, len);
	DEBUG_PROTO("ap_memread @ %" PRIx32 " len %zu:", src, len);
	const uint8_t *const data = (const uint8_t *)dest;
	for (size_t offset = 0; offset < len; ++offset) {
		if (offset == 16U)
			break;
		DEBUG_PROTO(" %02x", data[offset]);
	}
	if (len > 16U)
		DEBUG_PROTO(" ...");
	DEBUG_PROTO("\n");
}

void adiv5_mem_write_sized(adiv5_access_port_s *ap, uint32_t dest, const void *src, size_t len, align_e align)
{
	DEBUG_PROTO("ap_mem_write_sized @ %" PRIx32 " len %zu, align %d:", dest, len, 1 << align);
	const uint8_t *const data = (const uint8_t *)src;
	for (size_t offset = 0; offset < len; ++offset) {
		if (offset == 16U)
			break;
		DEBUG_PROTO(" %02x", data[offset]);
	}
	if (len > 16U)
		DEBUG_PROTO(" ...");
	DEBUG_PROTO("\n");
	ap->dp->mem_write(ap, dest, src, len, align);
}

void adiv5_dp_abort(adiv5_debug_port_s *dp, uint32_t abort)
{
	DEBUG_PROTO("Abort: %08" PRIx32 "\n", abort);
	dp->abort(dp, abort);
}