-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
CCDebugger.c
807 lines (669 loc) · 15.1 KB
/
CCDebugger.c
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/***********************************************************************
* Copyright (c) 2014-2016 Ioannis Charalampidis
* Copyright (c) 2015 Simon Schulz - github.com/fishpepper
Copyright © 2019 Jean Michault.
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 <https://www.gnu.org/licenses/>.
*************************************************************************/
#include <wiringPi.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include "CCDebugger.h"
/**
* Switch reset pin
*/
void cc_setDDDirection( uint8_t direction );
/**
* Software-overridable instruction table that can be used
* for supporting other CCDebug-Compatible chips purely by software
*/
uint8_t instr[16];
/**
* Local properties
*/
int pinRST= PIN_RST;
int pinDC= PIN_DC;
int pinDD= PIN_DD;
uint8_t errorFlag=0;
uint8_t ddIsOutput=false;
uint8_t inDebugMode=false;
uint8_t cc_active=false;
/**
* Instruction table indices
*/
#define INSTR_VERSION 0
#define I_HALT 1
#define I_RESUME 2
#define I_RD_CONFIG 3
#define I_WR_CONFIG 4
#define I_DEBUG_INSTR_1 5
#define I_DEBUG_INSTR_2 6
#define I_DEBUG_INSTR_3 7
#define I_GET_CHIP_ID 8
#define I_GET_PC 9
#define I_READ_STATUS 10
#define I_STEP_INSTR 11
#define I_CHIP_ERASE 12
#define I_SET_HW_BRKPNT 13
#define I_GET_BM 14
#define I_BURST_WRITE 15
void cc_delay_calibrate( );
int cc_init( int pRST, int pDC, int pDD )
{
if(pRST>=0) pinRST=pRST;
if(pDC>=0) pinDC=pDC;
if(pDD>=0) pinDD=pDD;
if(wiringPiSetup() == -1){
printf("no wiring pi detected\n");
return 0;
}
cc_delay_calibrate();
// Prepare CC Pins
pinMode(pinDC, OUTPUT);
pinMode(pinDD, OUTPUT);
pinMode(pinRST, OUTPUT);
digitalWrite(pinDC, LOW);
digitalWrite(pinDD, LOW);
digitalWrite(pinRST, LOW);
// Prepare default direction
cc_setDDDirection(INPUT);
// Default CCDebug instruction set for CC254x
instr[INSTR_VERSION] = 1;
instr[I_HALT] = 0x40;
instr[I_RESUME] = 0x48;
instr[I_RD_CONFIG] = 0x20;
instr[I_WR_CONFIG] = 0x18;
instr[I_DEBUG_INSTR_1] = 0x51;
instr[I_DEBUG_INSTR_2] = 0x52;
instr[I_DEBUG_INSTR_3] = 0x53;
instr[I_GET_CHIP_ID] = 0x68;
instr[I_GET_PC] = 0x28;
instr[I_READ_STATUS] = 0x30;
instr[I_STEP_INSTR] = 0x58;
instr[I_CHIP_ERASE] = 0x10;
// We are active by default
cc_active = true;
};
/**
* Activate/Deactivate debugger
*/
void cc_setActive( uint8_t on )
{
// Reset error flag
errorFlag = CC_ERROR_NONE;
// Continue only if active
if (on == cc_active) return;
cc_active = on;
if (on) {
// Prepare CC Pins
pinMode(pinDC, OUTPUT);
pinMode(pinDD, OUTPUT);
pinMode(pinRST, OUTPUT);
digitalWrite(pinDC, LOW);
digitalWrite(pinDD, LOW);
digitalWrite(pinRST, LOW);
// Default direction is INPUT
cc_setDDDirection(INPUT);
} else {
// Before deactivating, exit debug mode
if (inDebugMode)
cc_exit();
// Put everything in inactive mode
pinMode(pinDC, INPUT);
pinMode(pinDD, INPUT);
pinMode(pinRST, INPUT);
digitalWrite(pinDC, LOW);
digitalWrite(pinDD, LOW);
digitalWrite(pinRST, LOW);
}
}
/**
* Return the error flag
*/
uint8_t cc_error()
{
return errorFlag;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//// LOW LEVEL FUNCTIONS ////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/**
* Delay a particular number of cycles
*/
struct timespec tp={0,0};
static int cc_delay_mult=80;
void cc_delay( unsigned int d )
{
volatile unsigned int i = cc_delay_mult*d;
while( i-- );
//tp.tv_nsec=40*d;
//nanosleep(&tp,NULL);
}
void cc_setmult(int mult)
{
if(mult>0)
cc_delay_mult=mult;
else
cc_delay_mult=cc_delay_mult+cc_delay_mult/10+1;
}
int cc_getmult()
{
return cc_delay_mult;
}
/* provas konsideri la rapidecon de la procesoro */
void cc_delay_calibrate( )
{
long time0=micros();
cc_delay(200);
cc_delay(200);
cc_delay(200);
cc_delay(200);
cc_delay(200);
long time1=micros();
cc_delay_mult=cc_delay_mult*600/(time1-time0);
}
/**
* Enter debug mode
*/
uint8_t cc_enter()
{
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
// =============
// Reset error flag
errorFlag = CC_ERROR_NONE;
// Enter debug mode
digitalWrite(pinRST, LOW);
cc_delay(200);
digitalWrite(pinDC, HIGH);
cc_delay(3);
digitalWrite(pinDC, LOW);
cc_delay(3);
digitalWrite(pinDC, HIGH);
cc_delay(3);
digitalWrite(pinDC, LOW);
cc_delay(4);
digitalWrite(pinRST, HIGH);
cc_delay(200);
// We are now in debug mode
inDebugMode = 1;
// =============
// Success
return 0;
};
/**
* Write a uint8_t to the debugger
*/
uint8_t cc_write( uint8_t data )
{
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
};
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
// =============
uint8_t cnt;
// Make sure DD is on output
cc_setDDDirection(OUTPUT);
// Sent uint8_ts
for (cnt = 8; cnt; cnt--) {
// First put data bit on bus
if (data & 0x80)
digitalWrite(pinDD, HIGH);
else
digitalWrite(pinDD, LOW);
// Place clock on high (other end reads data)
digitalWrite(pinDC, HIGH);
// Shift & Delay
data <<= 1;
cc_delay(2);
// Place clock down
digitalWrite(pinDC, LOW);
cc_delay(2);
}
// =============
return 0;
}
/**
* Wait until input is ready for reading
*/
uint8_t cc_switchRead(uint8_t maxWaitCycles)
{
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
// =============
uint8_t cnt;
uint8_t didWait = 0;
// Switch to input
cc_setDDDirection(INPUT);
// Wait at least 83 ns before checking state t(dir_change)
cc_delay(2);
// Wait for DD to go LOW (Chip is READY)
while (digitalRead(pinDD) == HIGH) {
// Do 8 clock cycles
for (cnt = 8; cnt; cnt--) {
digitalWrite(pinDC, HIGH);
cc_delay(2);
digitalWrite(pinDC, LOW);
cc_delay(2);
}
// Let next function know that we did wait
didWait = 1;
// Check if we ran out if wait cycles
if (!--maxWaitCycles) {
// If we are waiting for too long, we have lost the chip,
// so also assume we are out of debugging mode
errorFlag = CC_ERROR_NOT_WIRED;
inDebugMode = 0;
return 0;
}
}
// Wait t(sample_wait)
if (didWait) cc_delay(2);
// =============
return 0;
}
/**
* Switch to output
*/
uint8_t cc_switchWrite()
{
cc_setDDDirection(OUTPUT);
return 0;
}
/**
* Read an input uint8_t
*/
uint8_t cc_read()
{
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
// =============
uint8_t cnt;
uint8_t data = 0;
// Switch to input
cc_setDDDirection(INPUT);
// Send 8 clock pulses if we are HIGH
for (cnt = 8; cnt; cnt--) {
digitalWrite(pinDC, HIGH);
cc_delay(2);
// Shift and read
data <<= 1;
if (digitalRead(pinDD) == HIGH)
data |= 0x01;
digitalWrite(pinDC, LOW);
cc_delay(2);
}
// =============
return data;
}
/**
* Switch reset pin
*/
void cc_setDDDirection( uint8_t direction )
{
// Switch direction if changed
if (direction == ddIsOutput) return;
ddIsOutput = direction;
// Handle new direction
if (ddIsOutput) {
digitalWrite(pinDD, LOW); // Disable pull-up
pinMode(pinDD, OUTPUT); // Enable output
digitalWrite(pinDD, LOW); // Switch to low
} else {
digitalWrite(pinDD, LOW); // Disable pull-up
pinMode(pinDD, INPUT); // Disable output
digitalWrite(pinDD, LOW); // Don't use output pull-up
}
}
void cc_reset()
{
pinMode(pinDC, INPUT);
pinMode(pinDD, INPUT);
pinMode(pinRST, OUTPUT);
cc_delay(200);
pinMode(pinRST, LOW);
cc_delay(500);
pinMode(pinRST, INPUT);
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//// HIGH LEVEL FUNCTIONS ////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/**
* Exit from debug mode
*/
uint8_t cc_exit()
{
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_RESUME] ); // RESUME
cc_switchRead(250);
bAns = cc_read(); // debug status
cc_switchWrite();
inDebugMode = 0;
return 0;
}
/**
* Get debug configuration
*/
uint8_t cc_getConfig() {
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_RD_CONFIG] ); // RD_CONFIG
cc_switchRead(250);
bAns = cc_read(); // Config
cc_switchWrite();
return bAns;
}
/**
* Set debug configuration
*/
uint8_t cc_setConfig( uint8_t config ) {
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_WR_CONFIG] ); // WR_CONFIG
cc_write( config );
cc_switchRead(250);
bAns = cc_read(); // Config
cc_switchWrite();
return bAns;
}
/**
* Invoke a debug instruction with 1 opcode
*/
uint8_t cc_exec( uint8_t oc0 )
{
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_DEBUG_INSTR_1] ); // DEBUG_INSTR + 1b
cc_write( oc0 );
cc_switchRead(250);
bAns = cc_read(); // Accumulator
cc_switchWrite();
return bAns;
}
/**
* Invoke a debug instruction with 2 opcodes
*/
uint8_t cc_exec2( uint8_t oc0, uint8_t oc1 )
{
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_DEBUG_INSTR_2] ); // DEBUG_INSTR + 2b
cc_write( oc0 );
cc_write( oc1 );
cc_switchRead(250);
bAns = cc_read(); // Accumulator
cc_switchWrite();
return bAns;
}
/**
* Invoke a debug instruction with 3 opcodes
*/
uint8_t cc_exec3( uint8_t oc0, uint8_t oc1, uint8_t oc2 )
{
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_DEBUG_INSTR_3] ); // DEBUG_INSTR + 3b
cc_write( oc0 );
cc_write( oc1 );
cc_write( oc2 );
cc_switchRead(250);
bAns = cc_read(); // Accumulator
cc_switchWrite();
return bAns;
}
/**
* Invoke a debug instruction with 1 opcode + 16-bit immediate
*/
uint8_t cc_execi( uint8_t oc0, unsigned short c0 )
{
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_DEBUG_INSTR_3] ); // DEBUG_INSTR + 3b
cc_write( oc0 );
cc_write( (c0 >> 8) & 0xFF );
cc_write( c0 & 0xFF );
cc_switchRead(250);
bAns = cc_read(); // Accumulator
cc_switchWrite();
return bAns;
}
/**
* Return chip ID
*/
unsigned short cc_getChipID() {
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
unsigned short bAns;
uint8_t bRes;
cc_write( instr[I_GET_CHIP_ID] ); // GET_CHIP_ID
cc_switchRead(250);
bRes = cc_read(); // High order
bAns = bRes << 8;
bRes = cc_read(); // Low order
bAns |= bRes;
cc_switchWrite();
return bAns;
}
/**
* Return PC
*/
unsigned short cc_getPC() {
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
unsigned short bAns;
uint8_t bRes;
cc_write( instr[I_GET_PC] ); // GET_PC
cc_switchRead(250);
bRes = cc_read(); // High order
bAns = bRes << 8;
bRes = cc_read(); // Low order
bAns |= bRes;
cc_switchWrite();
return bAns;
}
/**
* Return debug status
*/
uint8_t cc_getStatus() {
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_READ_STATUS] ); // READ_STATUS
cc_switchRead(250);
bAns = cc_read(); // debug status
cc_switchWrite();
return bAns;
}
/**
* Step instruction
*/
uint8_t cc_step() {
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_STEP_INSTR] ); // STEP_INSTR
cc_switchRead(250);
bAns = cc_read(); // Accumulator
cc_switchWrite();
return bAns;
}
/**
* resume instruction
*/
uint8_t cc_resume() {
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_RESUME] ); //RESUME
cc_switchRead(250);
bAns = cc_read(); // Accumulator
cc_switchWrite();
return bAns;
}
/**
* halt instruction
*/
uint8_t cc_halt() {
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
}
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_HALT] ); //HALT
cc_switchRead(250);
bAns = cc_read(); // Accumulator
cc_switchWrite();
return bAns;
}
/**
* Mass-erase all chip configuration & Lock Bits
*/
uint8_t cc_chipErase()
{
if (!cc_active) {
errorFlag = CC_ERROR_NOT_ACTIVE;
return 0;
};
if (!inDebugMode) {
errorFlag = CC_ERROR_NOT_DEBUGGING;
return 0;
}
uint8_t bAns;
cc_write( instr[I_CHIP_ERASE] ); // CHIP_ERASE
cc_switchRead(250);
bAns = cc_read(); // Debug status
cc_switchWrite();
return bAns;
}
/**
* Update the debug instruction table
*/
uint8_t cc_updateInstructionTable( uint8_t newTable[16] )
{
// Copy table entries
for (uint8_t i=0; i<16; i++)
instr[i] = newTable[i];
// Return the new version
return instr[INSTR_VERSION];
}
/**
* Get the instruction table version
*/
uint8_t cc_getInstructionTableVersion()
{
// Return version of instruction table
return instr[INSTR_VERSION];
}