This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
cc2530prog.c
1199 lines (1015 loc) · 24.9 KB
/
cc2530prog.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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* cc2530prog - Texas Instruments CC2530 programming tool
*
* Copyright (C) 2010, Florian Fainelli <[email protected]>
*
* This file is part of "cc2530prog", this file is distributed under
* a 2-clause BSD license, see LICENSE for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include "gpio.h"
#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x[0])))
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
static unsigned int gpios[3] = { RST_GPIO, CCLK_GPIO, DATA_GPIO };
struct cc2530_cmd {
char name[32];
uint8_t id;
uint8_t in;
uint8_t out;
};
static unsigned debug_enabled;
static unsigned verbose, progress;
#define DEFAULT_TIMEOUT 1000
#define CMD_ERASE 0x10
#define CMD_WR_CFG 0x18
#define CMD_RD_CFG 0x20
#define CMD_GET_PC 0x28
#define CMD_RD_ST 0x30
#define CMD_SET_BRK 0x38
#define CMD_HALT 0x40
#define CMD_RESUME 0x48
#define CMD_DBG_INST 0x50
#define CMD_STEP_INST 0x58
#define CMD_GET_BM 0x60
#define CMD_GET_CHIP 0x68
#define CMD_BURST_WR 0x80
/* Various chip statuses */
#define STACK_OVF 0x01
#define OSC_STABLE 0x02
#define DBG_LOCKED 0x04
#define HALT_STATUS 0x08
#define PWR_MODE_0 0x10
#define CPU_HALTED 0x20
#define PCON_IDLE 0x40
#define CHIP_ERASE_BSY 0x80
#define FCTL_BUSY 0x80
/* IDs that we recognize */
#define CC2530_ID 0xA5
/* Buffers */
#define ADDR_BUF0 0x0000 /* 1K */
#define ADDR_BUF1 0x0400 /* 1K */
#define ADDR_DMA_DESC 0x0800 /* 32 bytes */
/* DMA Channels */
#define CH_DBG_TO_BUF0 0x02
#define CH_DBG_TO_BUF1 0x04
#define CH_BUF0_TO_FLASH 0x08
#define CH_BUF1_TO_FLASH 0x10
#define PROG_BLOCK_SIZE 1024
#define LOBYTE(w) ((uint8_t)(w))
#define HIBYTE(w) ((uint8_t)(((uint16_t)(w) >> 8) & 0xFF))
/*
* Register offsets from ioCC2530.h
* make sure that these are always extended registers (16-bits addr)
* otherwise this simply will not work.
*/
#define X_EXT_ADDR_BASE 0x616A
#define DBGDATA 0x6260
#define FCTL 0x6270
#define FADDRL 0x6271
#define FADDRH 0x6272
#define FWDATA 0x6273
#define X_CHIPINFO0 0x6276
#define X_CHIPINFO1 0x6277
#define X_MEMCTR 0x70C7
#define X_DMA1CFGH 0x70D3
#define X_DMA1CFGL 0x70D4
#define X_DMAARM 0x70D6
#define X_CLKCONCMD 0x70C6
#define X_CLKCONSTA 0x709E
const uint8_t dma_desc[32] = {
/* Debug Interface -> Buffer 0 (Channel 1) */
HIBYTE(DBGDATA), /* src[15:8] */
LOBYTE(DBGDATA), /* src[7:0] */
HIBYTE(ADDR_BUF0), /* dest[15:8] */
LOBYTE(ADDR_BUF0), /* dest[7:0] */
HIBYTE(PROG_BLOCK_SIZE),
LOBYTE(PROG_BLOCK_SIZE),
31, /* trigger DBG_BW */
0x11, /* increment destination */
/* Debug Interface -> Buffer 1 (Channel 2) */
HIBYTE(DBGDATA), /* src[15:8] */
LOBYTE(DBGDATA), /* src[7:0] */
HIBYTE(ADDR_BUF1), /* dest[15:8] */
LOBYTE(ADDR_BUF1), /* dest[7:0] */
HIBYTE(PROG_BLOCK_SIZE),
LOBYTE(PROG_BLOCK_SIZE),
31, /* trigger DBG_BW */
0x11, /* increment destination */
/* Buffer 0 -> Flash controller (Channel 3) */
HIBYTE(ADDR_BUF0), /* src[15:8] */
LOBYTE(ADDR_BUF0), /* src[7:0] */
HIBYTE(FWDATA), /* dest[15:8] */
LOBYTE(FWDATA), /* dest[7:0] */
HIBYTE(PROG_BLOCK_SIZE),
LOBYTE(PROG_BLOCK_SIZE),
18, /* trigger FLASH */
0x42, /* increment source */
/* Buffer 1 -> Flash controller (Channel 4) */
HIBYTE(ADDR_BUF1), /* src[15:8] */
LOBYTE(ADDR_BUF1), /* src[7:0] */
HIBYTE(FWDATA), /* dest[15:8] */
LOBYTE(FWDATA), /* dest[7:0] */
HIBYTE(PROG_BLOCK_SIZE),
LOBYTE(PROG_BLOCK_SIZE),
18, /* trigger FLASH */
0x42 /* increment source */
};
static uint16_t flash_ptr = 0;
static void init_flash_ptr(void)
{
flash_ptr = 0;
}
static unsigned char *fwdata;
static inline uint8_t get_next_flash_byte(void)
{
return fwdata[flash_ptr++];
}
static inline void bytes_to_bits(uint8_t byte)
{
int i;
for (i = 0; i < 8; i++) {
if (byte & (1 << i))
printf("1");
else
printf("0");
}
}
static struct cc2530_cmd cc2530_commands[] = {
{
.name = "erase",
.id = CMD_ERASE,
.in = 0,
.out = 1,
}, {
.name = "write_config",
.id = CMD_WR_CFG,
.in = 1,
.out = 1,
}, {
.name = "read_config",
.id = CMD_RD_CFG,
.in = 0,
.out = 1,
}, {
.name = "get_pc",
.id = CMD_GET_PC,
.in = 0,
.out = 2,
}, {
.name = "read_status",
.id = CMD_RD_ST,
.in = 0,
.out = 1,
}, {
.name = "halt",
.id = CMD_HALT,
.in = 0,
.out = 1,
}, {
.name = "resume",
.id = CMD_RESUME,
.in = 0,
.out = 1,
}, {
.name = "debug_inst",
.id = CMD_DBG_INST,
.in = -1, /* variable */
.out = 1,
}, {
.name = "step_inst",
.id = CMD_STEP_INST,
.in = 0,
.out = 1,
}, {
.name = "get_bm",
.id = CMD_GET_BM,
.in = 0,
.out = 1,
}, {
.name = "get_chip_id",
.id = CMD_GET_CHIP,
.in = 0,
.out = 2,
}, {
.name = "burst_write",
.id = CMD_BURST_WR,
.in = -1, /* variable */
.out = 1,
},
};
static inline struct cc2530_cmd *find_cmd_by_name(const char *name)
{
int len;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(cc2530_commands); i++) {
len = strlen(name);
if (!strncmp(cc2530_commands[i].name, name, len))
return &cc2530_commands[i];
}
return NULL;
}
static void cc2530_show_command_list(void)
{
unsigned int i;
printf("Supported commands:\n");
for (i = 0; i < ARRAY_SIZE(cc2530_commands); i++)
printf("\t%s\n", cc2530_commands[i].name);
}
/*
* Perform GPIO initialization
*/
static int cc2530_gpio_init(void)
{
int ret;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(gpios); i++) {
ret = gpio_export(gpios[i]);
if (ret) {
fprintf(stderr, "failed to export %d\n", gpios[i]);
return ret;
}
ret = gpio_set_direction(gpios[i], GPIO_DIRECTION_OUT);
if (ret) {
fprintf(stderr, "failed to set direction on %d\n", gpios[i]);
return ret;
}
}
return 0;
}
/*
* Put back GPIOs in a sane state
*/
static int cc2530_gpio_deinit(void)
{
int ret;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(gpios); i++) {
ret = gpio_set_direction(gpios[i], GPIO_DIRECTION_IN);
if (ret) {
fprintf(stderr, "failed to set direction on %d\n", gpios[i]);
return ret;
}
ret = gpio_unexport(gpios[i]);
if (ret) {
fprintf(stderr, "failed to unexport %d\n", gpios[i]);
return ret;
}
}
return 0;
}
/*
* Hold reset low while raising clock twice
*/
static int cc2530_enter_debug(void)
{
int i;
/* pulse RST low */
gpio_set_value(RST_GPIO, RST_GPIO_POL 0);
for (i = 0; i < 2; i++) {
gpio_set_value(CCLK_GPIO, 0);
gpio_set_value(CCLK_GPIO, 1);
}
/* Keep clock low */
gpio_set_value(CCLK_GPIO, 0);
/* pulse Reset high */
gpio_set_value(RST_GPIO, RST_GPIO_POL 1);
debug_enabled = 1;
return 0;
}
static int cc2530_leave_debug(void)
{
gpio_set_value(RST_GPIO, RST_GPIO_POL 0);
gpio_set_value(RST_GPIO, RST_GPIO_POL 1);
return 0;
}
/*
* Bit-bang a byte on the GPIO data line
*/
static inline void send_byte(unsigned char byte)
{
int i;
/* Data setup on rising clock edge */
for (i = 7; i >= 0; i--) {
if (byte & (1 << i))
gpio_set_value(DATA_GPIO, 1);
else
gpio_set_value(DATA_GPIO, 0);
gpio_set_value(CCLK_GPIO, 1);
gpio_set_value(CCLK_GPIO, 0);
}
}
/*
* Clock in a byte from the GPIO line
*/
static inline void read_byte(unsigned char *byte)
{
int i;
bool val;
*byte = 0;
/* data read on falling clock edge */
for (i = 7; i >= 0; i--) {
gpio_set_value(CCLK_GPIO, 1);
gpio_get_value(DATA_GPIO, &val);
if (val)
*byte |= (1 << i);
gpio_set_value(CCLK_GPIO, 0);
}
}
/*
* Send the command to the chip
*/
static int cc2530_do_cmd(const struct cc2530_cmd *cmd, unsigned char *params, unsigned char *outbuf)
{
int ret;
int bytes;
unsigned int timeout = DEFAULT_TIMEOUT;
bool val;
unsigned char *answer;
if (!cmd) {
fprintf(stderr, "invalid command\n");
return -1;
}
/* allocate as many bytes as we need to store the result */
answer = malloc(cmd->out);
if (!answer) {
perror("malloc");
return -1;
}
memset(answer, 0, cmd->out);
ret = gpio_set_direction(DATA_GPIO, GPIO_DIRECTION_OUT);
if (ret) {
fprintf(stderr, "failed to put gpio in output direction\n");
goto out_exit;
}
/*
* Debug instruction also needs to set the number of bytes
* of the sent instruction.
*/
if (cmd->id == CMD_DBG_INST)
send_byte(cmd->id | cmd->in);
else
send_byte(cmd->id);
/* If there is any command payload also send it */
for (bytes = 0; bytes < cmd->in; bytes++)
send_byte(params[bytes]);
/* Now change the pin direction and wait for the chip to be ready
* and sample the data pin until the chip is ready to answer */
ret = gpio_set_direction(DATA_GPIO, GPIO_DIRECTION_IN);
if (ret) {
fprintf(stderr, "failed to put back gpio in input direction\n");
goto out_exit;
}
/*
* Cope with commands requiring a response delay and those which do
* not. In case the data line was not low right after we wrote data to
* clock out the chip 8 times as per the specification mentions. The
* data line should then be low and we are ready to read out from the
* chip.
*/
gpio_get_value(DATA_GPIO, &val);
while (val && timeout--) {
for (bytes = 0; bytes < 8; bytes++) {
gpio_set_value(CCLK_GPIO, 1);
gpio_set_value(CCLK_GPIO, 0);
}
gpio_get_value(DATA_GPIO, &val);
}
if (!timeout) {
fprintf(stderr, "timed out waiting for chip to be ready again\n");
goto out_exit;
}
/* Now read the answer */
for (bytes = 0; bytes < cmd->out; bytes++)
read_byte(&answer[bytes]);
memcpy(outbuf, answer, cmd->out);
out_exit:
free(answer);
return ret;
}
/*
* Bypass the command sending infrastructure to allow
* direct access to the what is sent to the chip
*/
static int cc2530_burst_write(void)
{
int ret;
uint16_t i;
unsigned char result;
unsigned int timeout = DEFAULT_TIMEOUT;
bool val;
ret = gpio_set_direction(DATA_GPIO, GPIO_DIRECTION_OUT);
if (ret) {
fprintf(stderr, "failed to put gpio in output direction\n");
return ret;
}
send_byte(CMD_BURST_WR | HIBYTE(PROG_BLOCK_SIZE));
send_byte(LOBYTE(PROG_BLOCK_SIZE));
for (i = 0; i < PROG_BLOCK_SIZE; i++)
send_byte(get_next_flash_byte());
ret = gpio_set_direction(DATA_GPIO, GPIO_DIRECTION_IN);
if (ret) {
fprintf(stderr, "failed to put gpio in input direction\n");
return ret;
}
gpio_get_value(DATA_GPIO, &val);
while (val && timeout--) {
for (i = 0; i < 8; i++) {
gpio_set_value(CCLK_GPIO, 1);
gpio_set_value(CCLK_GPIO, 0);
}
gpio_get_value(DATA_GPIO, &val);
}
if (!timeout) {
fprintf(stderr, "timed out waiting for chip to be ready\n");
return -1;
}
read_byte(&result);
return 0;
}
static int cc2530_chip_erase(struct cc2530_cmd *cmd)
{
int ret;
unsigned char result;
unsigned int timeout = DEFAULT_TIMEOUT;
cmd = find_cmd_by_name("erase");
ret = cc2530_do_cmd(cmd, NULL, &result);
if (ret) {
fprintf(stderr, "%s: failed to issue: %s\n", __func__, cmd->name);
return ret;
}
cmd = find_cmd_by_name("read_status");
do {
ret = cc2530_do_cmd(cmd, NULL, &result);
if (ret) {
fprintf(stderr, "%s: failed to issue: %s\n", __func__, cmd->name);
return ret;
}
usleep(10);
} while ((result & CHIP_ERASE_BSY) && timeout--);
if (!timeout) {
fprintf(stderr, "timeout waiting for the chip to be erased\n");
return -1;
}
return 0;
}
static int cc2530_write_xdata_memory(struct cc2530_cmd *cmd, uint16_t addr, uint8_t value)
{
int ret;
unsigned char instr[3];
unsigned char result;
cmd = find_cmd_by_name("debug_inst");
instr[0] = 0x90;
instr[1] = HIBYTE(addr);
instr[2] = LOBYTE(addr);
cmd->in = sizeof(instr);
ret = cc2530_do_cmd(cmd, instr, &result);
if (ret) {
fprintf(stderr, "%s: failed to issue: %s\n", __func__, cmd->name);
return ret;
}
instr[0] = 0x74;
instr[1] = value;
cmd->in = 2;
ret = cc2530_do_cmd(cmd, instr, &result);
if (ret) {
fprintf(stderr, "%s: failed to issue: %s\n", __func__, cmd->name);
return ret;
}
instr[0] = 0xF0;
cmd->in = 1;
ret = cc2530_do_cmd(cmd, instr, &result);
if (ret) {
fprintf(stderr, "%s: failed to issue: %s\n", __func__, cmd->name);
return ret;
}
return 0;
}
static int cc2530_read_xdata_memory(struct cc2530_cmd *cmd, uint16_t addr, unsigned char *result)
{
int ret;
unsigned char instr[3];
unsigned char res;
cmd = find_cmd_by_name("debug_inst");
instr[0] = 0x90;
instr[1] = HIBYTE(addr);
instr[2] = LOBYTE(addr);
cmd->in = sizeof(instr);
ret = cc2530_do_cmd(cmd, instr, result);
if (ret) {
fprintf(stderr, "%s: failed to issue: %s\n", __func__, cmd->name);
return ret;
}
instr[0] = 0xE0;
cmd->in = 1;
ret = cc2530_do_cmd(cmd, instr, &res);
if (ret) {
fprintf(stderr, "%s: failed to issue: %s\n", __func__, cmd->name);
return ret;
}
memcpy(result, &res, sizeof(res));
return 0;
}
static int cc2530_write_xdata_memory_block(struct cc2530_cmd *cmd,
uint16_t addr, const uint8_t *values, uint16_t num_bytes)
{
int ret;
unsigned char instr[3];
unsigned char result;
uint16_t i;
cmd = find_cmd_by_name("debug_inst");
instr[0] = 0x90;
instr[1] = HIBYTE(addr);
instr[2] = LOBYTE(addr);
cmd->in = sizeof(instr);
ret = cc2530_do_cmd(cmd, instr, &result);
if (ret) {
fprintf(stderr, "failed to issue: %s\n", cmd->name);
return ret;
}
for (i = 0; i < num_bytes; i++) {
instr[0] = 0x74;
instr[1] = values[i];
cmd->in = 2;
ret = cc2530_do_cmd(cmd, instr, &result);
if (ret) {
fprintf(stderr, "failed to issue: %s at %i\n", cmd->name, i);
return ret;
}
instr[0] = 0xF0;
cmd->in = 1;
ret = cc2530_do_cmd(cmd, instr, &result);
if (ret) {
fprintf(stderr, "failed to issue: %s at %i\n", cmd->name, i);
return ret;
}
instr[0] = 0xA3;
cmd->in = 1;
ret = cc2530_do_cmd(cmd, instr, &result);
if (ret) {
fprintf(stderr, "failed to issue: %s at %i\n", cmd->name, i);
return ret;
}
}
return 0;
}
static uint32_t cc2530_flash_verify(struct cc2530_cmd *cmd, uint32_t max_addr)
{
uint8_t bank;
unsigned char instr[3];
unsigned char result;
unsigned char expected;
uint16_t i;
uint32_t addr = 0;
int ret;
for (bank = 0; bank < 8; bank++) {
if (verbose)
printf("Reading bank: %d\n", bank);
ret = cc2530_write_xdata_memory(cmd, X_MEMCTR, bank);
if (ret) {
fprintf(stderr, "%s: failed to write to X_MEMCTR\n", __func__);
return ret;
}
cmd = find_cmd_by_name("debug_inst");
instr[0] = 0x90;
instr[1] = 0x80;
instr[2] = 0x00;
cmd->in = sizeof(instr);
ret = cc2530_do_cmd(cmd, instr, &result);
if (ret) {
fprintf(stderr, "%s: command failed: %s\n", __func__, cmd->name);
return ret;
}
for (i = 0; i < 32*1024; i++) {
if (addr == max_addr)
return addr;
instr[0] = 0xE0;
cmd->in = 1;
ret = cc2530_do_cmd(cmd, instr, &result);
if (ret) {
fprintf(stderr, "%s: command failed at %i\n", __func__, i);
return ret;
}
expected = get_next_flash_byte();
if (result != expected) {
printf("[bank%d][%d], result: %02x, expected: %02x\n",
bank, i, result, expected);
}
instr[0] = 0xA3;
cmd->in = 1;
ret = cc2530_do_cmd(cmd, instr, &result);
if (ret) {
fprintf(stderr, "%s: command failed at %i\n", __func__, i);
return ret;
}
addr++;
}
}
return addr;
}
static uint8_t cc2530_program_flash(struct cc2530_cmd *cmd, uint16_t num_buffers)
{
uint8_t dbg_arm, flash_arm;
uint8_t max_speed = 1;
uint16_t i;
uint8_t wait;
unsigned char result;
int ret;
unsigned int timeout = DEFAULT_TIMEOUT;
/* Write the 4 DMA descriptors */
ret = cc2530_write_xdata_memory_block(cmd, ADDR_DMA_DESC, dma_desc, ARRAY_SIZE(dma_desc));
if (ret) {
fprintf(stderr, "%s: failed to write DMA descriptors\n", __func__);
return ret;
}
/* Set the pointer to the DMA descriptors */
ret = cc2530_write_xdata_memory(cmd, X_DMA1CFGH, HIBYTE(ADDR_DMA_DESC));
if (ret) {
fprintf(stderr, "%s: failed to set DMA descriptors (part 1)\n", __func__);
return ret;
}
ret = cc2530_write_xdata_memory(cmd, X_DMA1CFGL, LOBYTE(ADDR_DMA_DESC));
if (ret) {
fprintf(stderr, "%s: failed to set DMA descriptors (part 2)\n", __func__);
return ret;
}
/* Make sure FADDR is 0x0000 */
ret = cc2530_write_xdata_memory(cmd, FADDRH, 0);
if (ret) {
fprintf(stderr, "%s: failed to set FADDRH\n", __func__);
return ret;
}
ret = cc2530_write_xdata_memory(cmd, FADDRL, 0);
if (ret) {
fprintf(stderr, "%s: failed to set FADDRL\n", __func__);
return ret;
}
for (i = 0; i < num_buffers; i++) {
if (progress) {
printf("%d/%d\n", i, num_buffers - 1);
fflush(stdout);
}
/* Set what is to be written to DMAARM, based on loop iteration */
if ((i & 0x0001) == 0) {
dbg_arm = CH_DBG_TO_BUF0;
flash_arm = CH_BUF0_TO_FLASH;
} else {
dbg_arm = CH_DBG_TO_BUF1;
flash_arm = CH_BUF1_TO_FLASH;
}
/* transfer next buffer (first buffer when i == 0) */
ret = cc2530_write_xdata_memory(cmd, X_DMAARM, dbg_arm);
if (ret) {
fprintf(stderr, "%s: failed to arm DMA\n", __func__);
return ret;
}
cc2530_burst_write();
/* wait for write to finish */
wait = 0;
do {
ret = cc2530_read_xdata_memory(cmd, FCTL, &result);
if (ret) {
fprintf(stderr, "%s: failed at %i\n", __func__, i);
return ret;
}
wait = 1;
} while ((result & FCTL_BUSY) && timeout--);
if (!timeout) {
fprintf(stderr, "%s: timeout at %i\n", __func__, i);
return -1;
}
/* no waiting means programming finished before burst write */
if (i > 0 && wait == 0)
max_speed = 0;
/* start programming current buffer */
ret = cc2530_write_xdata_memory(cmd, X_DMAARM, flash_arm);
if (ret) {
fprintf(stderr, "%s: failed programming current buffer: %d\n", __func__, i);
return ret;
}
ret = cc2530_write_xdata_memory(cmd, FCTL, 0x06);
if (ret) {
fprintf(stderr, "%s: failed to set FCTL\n", __func__);
return ret;
}
}
timeout = DEFAULT_TIMEOUT;
/* Programming last buffer in progress, wait until done */
do {
ret = cc2530_read_xdata_memory(cmd, FCTL, &result);
if (ret) {
fprintf(stderr, "%s: failed\n", __func__);
return ret;
}
} while ((result & FCTL_BUSY) && timeout--);
if (!timeout) {
fprintf(stderr, "%s: timeout programming last buffer\n", __func__);
return -1;
}
return max_speed;
}
static int cc2530_chip_identify(struct cc2530_cmd *cmd, int *flash_size)
{
int ret = 0;
unsigned char result[2] = { 0 };
unsigned char ext_addr[8] = { 0 };
int i;
ret = gpio_set_direction(DATA_GPIO, GPIO_DIRECTION_OUT);
if (ret) {
fprintf(stderr, "failed to set data gpio direction\n");
return ret;
}
cmd = find_cmd_by_name("get_chip_id");
ret = cc2530_do_cmd(cmd, NULL, result);
if (ret) {
fprintf(stderr, "%s: failed to issue: %s\n", __func__, cmd->name);
goto out;
}
/* Check that we actually know that chip */
if (result[0] != CC2530_ID) {
fprintf(stderr, "unknown Chip ID: %02x\n", result[0]);
if (result[0] == 0xFF || result[0] == 0)
fprintf(stderr, "someone is holding the CLK/DATA lines against us "
"make sure no debugger is *connected*\n");
ret = -EINVAL;
goto out;
}
if (verbose)
printf("Texas Instruments CC2530 (ID: 0x%02x, rev 0x%02x)\n", result[0], result[1]);
/*
* Do some chip identification
*/
for (i = 0; i < 7; i++) {
ret = cc2530_read_xdata_memory(cmd, X_EXT_ADDR_BASE + i, result);
if (ret) {
fprintf(stderr, "%s: failed to read X_ETXADDR%d\n", __func__, i);
return ret;
}
ext_addr[i] = result[0];
}
if (verbose)
printf("Extended addr: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
ext_addr[7], ext_addr[6], ext_addr[5], ext_addr[4],
ext_addr[3], ext_addr[2], ext_addr[1], ext_addr[0]);
ret = cc2530_read_xdata_memory(cmd, X_CHIPINFO0, result);
if (ret) {
fprintf(stderr, "failed to read X_CHIPINFO0 register\n");
return ret;
}
if (verbose) {
if (result[0] & 8)
printf("USB available\n");
else
printf("USB: not availabe\n");
}
switch ((result[0] & 0x70) >> 4) {
case 1:
*flash_size = 32;
break;
case 2:
*flash_size = 64;
break;
case 3:
*flash_size = 128;
break;
case 4:
*flash_size = 256;
}
if (verbose)
printf("Flash size: %d KB\n", *flash_size);
*flash_size *= 1024;
ret = cc2530_read_xdata_memory(cmd, X_CHIPINFO1, result);
if (ret) {
fprintf(stderr, "failed to read X_CHIPINFO1 register\n");
return ret;
}
out:
return ret;
}
/*
* Perform full CC2530 chip initialization and programming
*/
static int cc2530_do_program(struct cc2530_cmd *cmd, off_t fwsize, unsigned do_readback)
{
int ret = 0;
unsigned char config;
unsigned char result;
uint32_t num_bytes_ok = 0;
uint16_t blocks;
unsigned int timeout = DEFAULT_TIMEOUT;
unsigned int retry_cnt = 3;
while (retry_cnt--) {
/* Enable DMA */
cmd = find_cmd_by_name("write_config");
config = 0x22;
ret = cc2530_do_cmd(cmd, &config, &result);
if (ret) {
fprintf(stderr, "failed to enable DMA\n");
return ret;
}
if (result != config) {
fprintf(stderr, "write config failed (retry count: %d)\n", retry_cnt);
cc2530_enter_debug();
continue;
}
break;
}
ret = cc2530_write_xdata_memory(cmd, X_CLKCONCMD, 0x80);
if (ret) {
fprintf(stderr, "failed to write X_CLKCONCMD\n");
return ret;
}
do {
ret = cc2530_read_xdata_memory(cmd, X_CLKCONSTA, &result);
if (ret) {
fprintf(stderr, "%s: failed to read X_CLKCONSTA\n", __func__);
return ret;
}
} while ((result != 0x80) && timeout--);
if (!timeout) {
fprintf(stderr, "%s: timeout waiting for CLKCONSTA\n", __func__);
return -1;
}
ret = cc2530_chip_erase(cmd);
if (ret) {
fprintf(stderr, "failed to erase chip\n");
return ret;
}
init_flash_ptr();
blocks = DIV_ROUND_UP(fwsize, PROG_BLOCK_SIZE);
ret = cc2530_program_flash(cmd, blocks);
if (ret && verbose)