1
2
3
6
7
8
9
10
11
19
20
21
22
23
24
25
26
27
28
29
30
31
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
52
53
54
57
58
59
62
63
64
65
66
67
68
69
70
89
90
91
92
93
94
95
96
97
98
111
112
113
114
115
116
117
118
119
120
130
134
143
144
145
146
147
148
149
150
151
152
154
156
158
160
162
164
166
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
306
307
312
313
314
319
320
321
322
323
324
325
326
327
328
329
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
385
386
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
444
445
446
447
448
449
450
451
457
458
459
460
464
465
466
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
496
497
498
499
500
501
502
503
506
507
508
509
513
514
515
516
517
518
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
549
550
551
552
553
554
555
556
557
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
614
615
616
620
621
622
623
624
625
626
627
628
629
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
658
659
660
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
692
693
699
700
701
702
703
704
705
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
747
748
749
750
754
755
756
757
758
759
760
761
762
763
764
765
766
771
772
773
774
775
779
780
781
785
786
787
788
789
790
791
792
793
794
795
796
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
898
909
910
911
912
913
918
919
920
921
922
923
924
925
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
999
1003
1007
1010
1013
1018
1019
1020
1025
1026
1027
1028
1029
1030
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include "msp432.h"
#include <helper/binarybuffer.h>
#include <helper/time_support.h>
#include <target/algorithm.h>
#include <target/armv7m.h>
#include <target/image.h>
7 includes
#define P4_FLASH_MAIN_SIZE_REG 0xE0043020
#define P4_FLASH_INFO_SIZE_REG 0xE0043024
#define P4_DEVICE_ID_REG 0x0020100C
#define P4_HARDWARE_REV_REG 0x00201010
#define E4_DID0_REG 0x400FE000
#define E4_DID1_REG 0x400FE004
#define FLASH_TIMEOUT 8000
#define SUPPORT_MESSAGE \
"Your pre-production MSP432P401x silicon is not fully supported\n" \
"You can find more information at www.ti.com/product/MSP432P401R"...
8 defines
struct msp432_bank {
uint32_t device_id;
uint32_t hardware_rev;
int family_type;
int device_type;
uint32_t sector_length;
bool probed_main;
bool probed_info;
bool unlock_bsl;
struct working_area *working_area;
struct armv7m_algorithm armv7m_info;
...};
static const uint8_t msp432p401x_algo[] = {
#include "../../../contrib/loaders/flash/msp432/msp432p401x_algo.inc"
...};
static const uint8_t msp432p411x_algo[] = {
#include "../../../contrib/loaders/flash/msp432/msp432p411x_algo.inc"
...};
static const uint8_t msp432e4x_algo[] = {
#include "../../../contrib/loaders/flash/msp432/msp432e4x_algo.inc"
...};
static int msp432_auto_probe(struct flash_bank *bank);
static int msp432_device_type(uint32_t family_type, uint32_t device_id,
uint32_t hardware_rev)
{
int device_type = MSP432_NO_TYPE;
if (family_type == MSP432E4) {
if (device_id == 0x180C0002) {
if (hardware_rev == 0x102DC06E) {
device_type = MSP432E401Y;
}if (hardware_rev == 0x102DC06E) { ... } else if (hardware_rev == 0x1032E076) {
device_type = MSP432E411Y;
}else if (hardware_rev == 0x1032E076) { ... } else {
device_type = MSP432E4X_GUESS;
}else { ... }
}if (device_id == 0x180C0002) { ... } else {
device_type = MSP432E4X_GUESS;
}else { ... }
}if (family_type == MSP432E4) { ... } else {
switch (device_id) {
case 0xA000:
case 0xA001:
case 0xA002:
case 0xA003:
case 0xA004:
case 0xA005:
if (hardware_rev == 0x41 || hardware_rev == 0x42) {
device_type = MSP432P401X_DEPR;
}if (hardware_rev == 0x41 || hardware_rev == 0x42) { ... } else if (hardware_rev >= 0x43 && hardware_rev <= 0x49) {
device_type = MSP432P401X;
}else if (hardware_rev >= 0x43 && hardware_rev <= 0x49) { ... } else {
device_type = MSP432P401X_GUESS;
}else { ... }
break;case 0xA005:
case 0xA010:
case 0xA012:
case 0xA016:
case 0xA019:
case 0xA01F:
case 0xA020:
case 0xA022:
case 0xA026:
case 0xA029:
case 0xA02F:
if (hardware_rev >= 0x41 && hardware_rev <= 0x49) {
device_type = MSP432P411X;
}if (hardware_rev >= 0x41 && hardware_rev <= 0x49) { ... } else {
device_type = MSP432P411X_GUESS;
}else { ... }
break;case 0xA02F:
case 0xFFFF:
device_type = MSP432P401X_DEPR;
break;case 0xFFFF:
default:
if (device_id < 0xA010) {
device_type = MSP432P401X_GUESS;
}if (device_id < 0xA010) { ... } else {
device_type = MSP432P411X_GUESS;
}else { ... }
break;default
}switch (device_id) { ... }
}else { ... }
return device_type;
}{ ... }
static const char *msp432_return_text(uint32_t return_code)
{
switch (return_code) {
case FLASH_BUSY:
return "FLASH_BUSY";case FLASH_BUSY:
case FLASH_SUCCESS:
return "FLASH_SUCCESS";case FLASH_SUCCESS:
case FLASH_ERROR:
return "FLASH_ERROR";case FLASH_ERROR:
case FLASH_TIMEOUT_ERROR:
return "FLASH_TIMEOUT_ERROR";case FLASH_TIMEOUT_ERROR:
case FLASH_VERIFY_ERROR:
return "FLASH_VERIFY_WRONG";case FLASH_VERIFY_ERROR:
case FLASH_WRONG_COMMAND:
return "FLASH_WRONG_COMMAND";case FLASH_WRONG_COMMAND:
case FLASH_POWER_ERROR:
return "FLASH_POWER_ERROR";case FLASH_POWER_ERROR:
default:
return "UNDEFINED_RETURN_CODE";default
}switch (return_code) { ... }
}{ ... }
static void msp432_init_params(struct msp432_algo_params *algo_params)
{
buf_set_u32(algo_params->flash_command, 0, 32, FLASH_NO_COMMAND);
buf_set_u32(algo_params->return_code, 0, 32, 0);
buf_set_u32(algo_params->_reserved0, 0, 32, 0);
buf_set_u32(algo_params->address, 0, 32, 0);
buf_set_u32(algo_params->length, 0, 32, 0);
buf_set_u32(algo_params->buffer1_status, 0, 32, BUFFER_INACTIVE);
buf_set_u32(algo_params->buffer2_status, 0, 32, BUFFER_INACTIVE);
buf_set_u32(algo_params->erase_param, 0, 32, FLASH_ERASE_MAIN);
buf_set_u32(algo_params->unlock_bsl, 0, 32, FLASH_LOCK_BSL);
}{ ... }
static int msp432_exec_cmd(struct target *target, struct msp432_algo_params
*algo_params, uint32_t command)
{
int retval;
buf_set_u32(algo_params->flash_command, 0, 32, FLASH_NO_COMMAND);
buf_set_u32(algo_params->return_code, 0, 32, 0);
buf_set_u32(algo_params->buffer1_status, 0, 32, BUFFER_INACTIVE);
buf_set_u32(algo_params->buffer2_status, 0, 32, BUFFER_INACTIVE);
retval = target_write_buffer(target, ALGO_PARAMS_BASE_ADDR,
sizeof(struct msp432_algo_params), (uint8_t *)algo_params);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, ALGO_FLASH_COMMAND_ADDR, command);
return retval;
}{ ... }
static int msp432_wait_return_code(struct target *target)
{
uint32_t return_code = 0;
long long start_ms;
long long elapsed_ms;
int retval = ERROR_OK;
start_ms = timeval_ms();
while ((return_code == 0) || (return_code == FLASH_BUSY)) {
retval = target_read_u32(target, ALGO_RETURN_CODE_ADDR, &return_code);
if (retval != ERROR_OK)
return retval;
elapsed_ms = timeval_ms() - start_ms;
if (elapsed_ms > 500)
keep_alive();
if (elapsed_ms > FLASH_TIMEOUT)
break;
}while ((return_code == 0) || (return_code == FLASH_BUSY)) { ... };
if (return_code != FLASH_SUCCESS) {
LOG_ERROR("msp432: Flash operation failed: %s",
msp432_return_text(return_code));
return ERROR_FAIL;
}if (return_code != FLASH_SUCCESS) { ... }
return ERROR_OK;
}{ ... }
static int msp432_wait_inactive(struct target *target, uint32_t buffer)
{
uint32_t status_code = BUFFER_ACTIVE;
uint32_t status_addr;
long long start_ms;
long long elapsed_ms;
int retval;
switch (buffer) {
case 1:
status_addr = ALGO_BUFFER1_STATUS_ADDR;
break;case 1:
case 2:
status_addr = ALGO_BUFFER2_STATUS_ADDR;
break;case 2:
default:
return ERROR_FAIL;default
}switch (buffer) { ... }
start_ms = timeval_ms();
while (status_code != BUFFER_INACTIVE) {
retval = target_read_u32(target, status_addr, &status_code);
if (retval != ERROR_OK)
return retval;
elapsed_ms = timeval_ms() - start_ms;
if (elapsed_ms > 500)
keep_alive();
if (elapsed_ms > FLASH_TIMEOUT)
break;
}while (status_code != BUFFER_INACTIVE) { ... };
if (status_code != BUFFER_INACTIVE) {
LOG_ERROR(
"msp432: Flash operation failed: buffer not written to flash");
return ERROR_FAIL;
}if (status_code != BUFFER_INACTIVE) { ... }
return ERROR_OK;
}{ ... }
static int msp432_init(struct flash_bank *bank)
{
struct target *target = bank->target;
struct msp432_bank *msp432_bank = bank->driver_priv;
struct msp432_algo_params algo_params;
struct reg_param reg_params[1];
const uint8_t *loader_code;
uint32_t loader_size;
uint32_t algo_entry_addr;
int retval;
retval = msp432_auto_probe(bank);
if (retval != ERROR_OK)
return retval;
switch (msp432_bank->device_type) {
case MSP432P401X:
case MSP432P401X_DEPR:
case MSP432P401X_GUESS:
default:
loader_code = msp432p401x_algo;
loader_size = sizeof(msp432p401x_algo);
algo_entry_addr = P4_ALGO_ENTRY_ADDR;
break;default
case MSP432P411X:
case MSP432P411X_GUESS:
loader_code = msp432p411x_algo;
loader_size = sizeof(msp432p411x_algo);
algo_entry_addr = P4_ALGO_ENTRY_ADDR;
break;case MSP432P411X_GUESS:
case MSP432E401Y:
case MSP432E411Y:
case MSP432E4X_GUESS:
loader_code = msp432e4x_algo;
loader_size = sizeof(msp432e4x_algo);
algo_entry_addr = E4_ALGO_ENTRY_ADDR;
break;case MSP432E4X_GUESS:
}switch (msp432_bank->device_type) { ... }
if (msp432_bank->device_type == MSP432P401X_GUESS ||
msp432_bank->device_type == MSP432P411X_GUESS) {
LOG_WARNING(
"msp432: Unrecognized MSP432P4 Device ID and Hardware "
"Rev (%04" PRIX32 ", %02" PRIX32 ")", msp432_bank->device_id,
msp432_bank->hardware_rev);
}if (msp432_bank->device_type == MSP432P401X_GUESS || msp432_bank->device_type == MSP432P411X_GUESS) { ... } else if (msp432_bank->device_type == MSP432P401X_DEPR) {
LOG_WARNING(
"msp432: MSP432P401x pre-production device (deprecated "
"silicon)\n" SUPPORT_MESSAGE);
}else if (msp432_bank->device_type == MSP432P401X_DEPR) { ... } else if (msp432_bank->device_type == MSP432E4X_GUESS) {
LOG_WARNING(
"msp432: Unrecognized MSP432E4 DID0 and DID1 values "
"(%08" PRIX32 ", %08" PRIX32 ")", msp432_bank->device_id,
msp432_bank->hardware_rev);
}else if (msp432_bank->device_type == MSP432E4X_GUESS) { ... }
target_free_working_area(target, msp432_bank->working_area);
msp432_bank->working_area = NULL;
retval = target_alloc_working_area(target, ALGO_WORKING_SIZE,
&msp432_bank->working_area);
if (retval != ERROR_OK)
return retval;
if (msp432_bank->working_area->address != ALGO_BASE_ADDR)
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
retval = target_write_buffer(target, ALGO_BASE_ADDR, loader_size,
loader_code);
if (retval != ERROR_OK)
return retval;
msp432_bank->armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
msp432_bank->armv7m_info.core_mode = ARM_MODE_THREAD;
msp432_init_params(&algo_params);
retval = target_write_buffer(target, ALGO_PARAMS_BASE_ADDR,
sizeof(algo_params), (uint8_t *)&algo_params);
if (retval != ERROR_OK)
return retval;
init_reg_param(®_params[0], "sp", 32, PARAM_OUT);
buf_set_u32(reg_params[0].value, 0, 32, ALGO_STACK_POINTER_ADDR);
retval = target_start_algorithm(target, 0, NULL, 1, reg_params,
algo_entry_addr, 0, &msp432_bank->armv7m_info);
destroy_reg_param(®_params[0]);
if (retval != ERROR_OK) {
LOG_ERROR("msp432: Failed to start flash helper algorithm");
return retval;
}if (retval != ERROR_OK) { ... }
/* ... */
retval = msp432_exec_cmd(target, &algo_params, FLASH_INIT);
if (retval != ERROR_OK)
return retval;
retval = msp432_wait_return_code(target);
return retval;
}{ ... }
static int msp432_quit(struct flash_bank *bank)
{
struct target *target = bank->target;
struct msp432_bank *msp432_bank = bank->driver_priv;
struct msp432_algo_params algo_params;
int retval;
msp432_init_params(&algo_params);
retval = msp432_exec_cmd(target, &algo_params, FLASH_EXIT);
if (retval != ERROR_OK)
return retval;
(void)msp432_wait_return_code(target);
(void)target_halt(target);
retval = target_wait_algorithm(target, 0, NULL, 0, NULL, 0, FLASH_TIMEOUT,
&msp432_bank->armv7m_info);
target_free_working_area(target, msp432_bank->working_area);
msp432_bank->working_area = NULL;
return retval;
}{ ... }
static int msp432_mass_erase(struct flash_bank *bank, bool all)
{
struct target *target = bank->target;
struct msp432_bank *msp432_bank = bank->driver_priv;
struct msp432_algo_params algo_params;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
retval = msp432_init(bank);
if (retval != ERROR_OK)
return retval;
msp432_init_params(&algo_params);
if (all) {
buf_set_u32(algo_params.erase_param, 0, 32,
FLASH_ERASE_MAIN | FLASH_ERASE_INFO);
if (msp432_bank->unlock_bsl)
buf_set_u32(algo_params.unlock_bsl, 0, 32, FLASH_UNLOCK_BSL);
}if (all) { ... }
retval = msp432_exec_cmd(target, &algo_params, FLASH_MASS_ERASE);
if (retval != ERROR_OK) {
(void)msp432_quit(bank);
return retval;
}if (retval != ERROR_OK) { ... }
retval = msp432_wait_return_code(target);
if (retval != ERROR_OK) {
(void)msp432_quit(bank);
return retval;
}if (retval != ERROR_OK) { ... }
retval = msp432_quit(bank);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
COMMAND_HANDLER(msp432_mass_erase_command)
{
struct flash_bank *bank;
struct msp432_bank *msp432_bank;
bool all;
int retval;
if (1 > CMD_ARGC)
return ERROR_COMMAND_SYNTAX_ERROR;
retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
if (1 == CMD_ARGC) {
all = false;
}if (1 == CMD_ARGC) { ... } else if (2 == CMD_ARGC) {
if (strcmp(CMD_ARGV[1], "main") == 0)
all = false;
else if (strcmp(CMD_ARGV[1], "all") == 0)
all = true;
else
return ERROR_COMMAND_SYNTAX_ERROR;
}else if (2 == CMD_ARGC) { ... } else {
return ERROR_COMMAND_SYNTAX_ERROR;
}else { ... }
msp432_bank = bank->driver_priv;
if (msp432_bank->family_type == MSP432E4) {
all = false;
}if (msp432_bank->family_type == MSP432E4) { ... }
retval = msp432_mass_erase(bank, all);
if (retval != ERROR_OK)
return retval;
if (msp432_bank->family_type == MSP432E4) {
LOG_INFO("msp432: Mass erase of flash is complete");
}if (msp432_bank->family_type == MSP432E4) { ... } else {
LOG_INFO("msp432: Mass erase of %s is complete",
all ? "main + information flash" : "main flash");
}else { ... }
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(msp432_bsl_command)
{
struct flash_bank *bank;
struct msp432_bank *msp432_bank;
int retval;
if (1 > CMD_ARGC)
return ERROR_COMMAND_SYNTAX_ERROR;
retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
msp432_bank = bank->driver_priv;
if (msp432_bank->family_type == MSP432E4) {
LOG_WARNING("msp432: MSP432E4 does not have a BSL region");
return ERROR_OK;
}if (msp432_bank->family_type == MSP432E4) { ... }
if (2 == CMD_ARGC) {
if (strcmp(CMD_ARGV[1], "lock") == 0)
msp432_bank->unlock_bsl = false;
else if (strcmp(CMD_ARGV[1], "unlock") == 0)
msp432_bank->unlock_bsl = true;
else
return ERROR_COMMAND_SYNTAX_ERROR;
}if (2 == CMD_ARGC) { ... } else if (1 != CMD_ARGC) {
return ERROR_COMMAND_SYNTAX_ERROR;
}else if (1 != CMD_ARGC) { ... }
LOG_INFO("msp432: BSL flash region is currently %slocked",
msp432_bank->unlock_bsl ? "un" : "");
return ERROR_OK;
}{ ... }
FLASH_BANK_COMMAND_HANDLER(msp432_flash_bank_command)
{
struct msp432_bank *msp432_bank;
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
msp432_bank = malloc(sizeof(struct msp432_bank));
if (!msp432_bank)
return ERROR_FAIL;
msp432_bank->device_id = 0;
msp432_bank->hardware_rev = 0;
msp432_bank->family_type = MSP432_NO_FAMILY;
msp432_bank->device_type = MSP432_NO_TYPE;
msp432_bank->sector_length = 0x1000;
msp432_bank->probed_main = false;
msp432_bank->probed_info = false;
msp432_bank->unlock_bsl = false;
msp432_bank->working_area = NULL;
bank->driver_priv = msp432_bank;
bank->base = FLASH_BASE;
return ERROR_OK;
}{ ... }
static int msp432_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
struct target *target = bank->target;
struct msp432_bank *msp432_bank = bank->driver_priv;
struct msp432_algo_params algo_params;
bool is_main = bank->base == FLASH_BASE;
bool is_info = bank->base == P4_FLASH_INFO_BASE;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if (is_main && (first == 0) && (last == (bank->num_sectors - 1))) {
return msp432_mass_erase(bank, false);
}if (is_main && (first == 0) && (last == (bank->num_sectors - 1))) { ... }
retval = msp432_init(bank);
if (retval != ERROR_OK)
return retval;
msp432_init_params(&algo_params);
if (is_info) {
buf_set_u32(algo_params.erase_param, 0, 32, FLASH_ERASE_INFO);
if (msp432_bank->unlock_bsl)
buf_set_u32(algo_params.unlock_bsl, 0, 32, FLASH_UNLOCK_BSL);
}if (is_info) { ... }
for (unsigned int i = first; i <= last; i++) {
if (is_info && 1 == i)
continue;
if (is_info && (2 == i || 3 == i) &&
!msp432_bank->unlock_bsl)
continue;
buf_set_u32(algo_params.address, 0, 32, bank->base +
(i * msp432_bank->sector_length));
retval = msp432_exec_cmd(target, &algo_params, FLASH_SECTOR_ERASE);
if (retval != ERROR_OK) {
(void)msp432_quit(bank);
return retval;
}if (retval != ERROR_OK) { ... }
retval = msp432_wait_return_code(target);
if (retval != ERROR_OK) {
(void)msp432_quit(bank);
return retval;
}if (retval != ERROR_OK) { ... }
}for (unsigned int i = first; i <= last; i++) { ... }
retval = msp432_quit(bank);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
static int msp432_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
struct target *target = bank->target;
struct msp432_bank *msp432_bank = bank->driver_priv;
struct msp432_algo_params algo_params;
uint32_t size;
uint32_t data_ready = BUFFER_DATA_READY;
long long start_ms;
long long elapsed_ms;
bool is_info = bank->base == P4_FLASH_INFO_BASE;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
/* ... */
if (is_info) {
uint32_t start = 0x1000;
uint32_t end = (msp432_bank->unlock_bsl) ? 0x2000 : 0x4000;
if ((offset + count - 1) < start || offset >= end) {
}if ((offset + count - 1) < start || offset >= end) { ... } else {
if (offset < start) {
uint32_t start_count = MIN(start - offset, count);
retval = msp432_write(bank, buffer, offset, start_count);
if (retval != ERROR_OK)
return retval;
}if (offset < start) { ... }
if ((offset + count - 1) >= end) {
uint32_t skip = end - offset;
count -= skip;
offset += skip;
buffer += skip;
return msp432_write(bank, buffer, offset, count);
}if ((offset + count - 1) >= end) { ... } else {
return ERROR_OK;
}else { ... }
}else { ... }
}if (is_info) { ... }
retval = msp432_init(bank);
if (retval != ERROR_OK)
return retval;
msp432_init_params(&algo_params);
buf_set_u32(algo_params.address, 0, 32, bank->base + offset);
buf_set_u32(algo_params.length, 0, 32, count);
if (is_info) {
if (msp432_bank->unlock_bsl)
buf_set_u32(algo_params.unlock_bsl, 0, 32, FLASH_UNLOCK_BSL);
}if (is_info) { ... }
retval = msp432_exec_cmd(target, &algo_params, FLASH_CONTINUOUS);
if (retval != ERROR_OK) {
(void)msp432_quit(bank);
return retval;
}if (retval != ERROR_OK) { ... }
start_ms = timeval_ms();
while (count > 0) {
if (count > ALGO_BUFFER_SIZE)
size = ALGO_BUFFER_SIZE;
else
size = count;
retval = target_write_buffer(target, ALGO_BUFFER1_ADDR, size, buffer);
if (retval != ERROR_OK) {
LOG_ERROR("Unable to write data to target memory");
(void)msp432_quit(bank);
return ERROR_FLASH_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, ALGO_BUFFER1_STATUS_ADDR,
data_ready);
if (retval != ERROR_OK) {
(void)msp432_quit(bank);
return ERROR_FLASH_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = msp432_wait_inactive(target, 1);
if (retval != ERROR_OK) {
(void)msp432_quit(bank);
return retval;
}if (retval != ERROR_OK) { ... }
count -= size;
buffer += size;
elapsed_ms = timeval_ms() - start_ms;
if (elapsed_ms > 500)
keep_alive();
}while (count > 0) { ... }
retval = msp432_wait_return_code(target);
if (retval != ERROR_OK) {
(void)msp432_quit(bank);
return retval;
}if (retval != ERROR_OK) { ... }
retval = msp432_quit(bank);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
static int msp432_probe(struct flash_bank *bank)
{
struct target *target = bank->target;
struct msp432_bank *msp432_bank = bank->driver_priv;
uint32_t device_id;
uint32_t hardware_rev;
uint32_t sector_length;
uint32_t size;
unsigned int num_sectors;
bool is_main = bank->base == FLASH_BASE;
bool is_info = bank->base == P4_FLASH_INFO_BASE;
int retval;
if (is_main && msp432_bank->probed_main)
return ERROR_OK;
if (is_info && msp432_bank->probed_info)
return ERROR_OK;
retval = target_read_u32(target, P4_FLASH_MAIN_SIZE_REG, &size);
if (retval != ERROR_OK)
return retval;
if (size == 0) {
msp432_bank->family_type = MSP432E4;
retval = target_read_u32(target, E4_DID0_REG, &device_id);
if (retval != ERROR_OK)
return retval;
msp432_bank->device_id = device_id;
retval = target_read_u32(target, E4_DID1_REG, &hardware_rev);
if (retval != ERROR_OK)
return retval;
msp432_bank->hardware_rev = hardware_rev;
}if (size == 0) { ... } else {
msp432_bank->family_type = MSP432P4;
retval = target_read_u32(target, P4_DEVICE_ID_REG, &device_id);
if (retval != ERROR_OK)
return retval;
msp432_bank->device_id = device_id & 0xFFFF;
retval = target_read_u32(target, P4_HARDWARE_REV_REG, &hardware_rev);
if (retval != ERROR_OK)
return retval;
msp432_bank->hardware_rev = hardware_rev & 0xFF;
}else { ... }
msp432_bank->device_type = msp432_device_type(msp432_bank->family_type,
msp432_bank->device_id, msp432_bank->hardware_rev);
if (msp432_bank->family_type == MSP432P4) {
if (is_main) {
retval = target_read_u32(target, P4_FLASH_MAIN_SIZE_REG, &size);
if (retval != ERROR_OK)
return retval;
sector_length = P4_SECTOR_LENGTH;
num_sectors = size / sector_length;
}if (is_main) { ... } else if (is_info) {
if (msp432_bank->device_type == MSP432P411X ||
msp432_bank->device_type == MSP432P411X_GUESS) {
retval = target_read_u32(target, P4_FLASH_INFO_SIZE_REG, &size);
if (retval != ERROR_OK)
return retval;
}if (msp432_bank->device_type == MSP432P411X || msp432_bank->device_type == MSP432P411X_GUESS) { ... } else {
size = 0x4000;
}else { ... }
sector_length = P4_SECTOR_LENGTH;
num_sectors = size / sector_length;
}else if (is_info) { ... } else {
return ERROR_FAIL;
}else { ... }
}if (msp432_bank->family_type == MSP432P4) { ... } else {
if (is_main) {
size = E4_FLASH_SIZE;
sector_length = E4_SECTOR_LENGTH;
num_sectors = size / sector_length;
}if (is_main) { ... } else {
return ERROR_FAIL;
}else { ... }
}else { ... }
free(bank->sectors);
bank->sectors = NULL;
if (num_sectors > 0) {
bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
if (!bank->sectors)
return ERROR_FAIL;
}if (num_sectors > 0) { ... }
bank->size = size;
bank->write_start_alignment = 0;
bank->write_end_alignment = 0;
bank->num_sectors = num_sectors;
msp432_bank->sector_length = sector_length;
for (unsigned int i = 0; i < num_sectors; i++) {
bank->sectors[i].offset = i * sector_length;
bank->sectors[i].size = sector_length;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = 0;
}for (unsigned int i = 0; i < num_sectors; i++) { ... }
if (is_main)
msp432_bank->probed_main = true;
if (is_info)
msp432_bank->probed_info = true;
if (is_main && MSP432P4 == msp432_bank->family_type) {
struct flash_bank *info = calloc(1, sizeof(struct flash_bank));
if (!info)
return ERROR_FAIL;
char *name = malloc(strlen(bank->name) + 3);
strcpy(name, bank->name);
strcat(name, "_1");
info->name = name;
info->target = bank->target;
info->driver = bank->driver;
info->driver_priv = msp432_bank;
info->base = P4_FLASH_INFO_BASE;
flash_bank_add(info);
}if (is_main && MSP432P4 == msp432_bank->family_type) { ... }
return ERROR_OK;
}{ ... }
static int msp432_auto_probe(struct flash_bank *bank)
{
struct msp432_bank *msp432_bank = bank->driver_priv;
bool is_main = bank->base == FLASH_BASE;
bool is_info = bank->base == P4_FLASH_INFO_BASE;
int retval = ERROR_OK;
if (is_main)
if (!msp432_bank->probed_main)
retval = msp432_probe(bank);
if (is_info)
if (!msp432_bank->probed_info)
retval = msp432_probe(bank);
return retval;
}{ ... }
static int msp432_info(struct flash_bank *bank, struct command_invocation *cmd)
{
struct msp432_bank *msp432_bank = bank->driver_priv;
switch (msp432_bank->device_type) {
case MSP432P401X_DEPR:
if (msp432_bank->device_id == 0xFFFF) {
command_print_sameline(cmd, "MSP432P401x pre-production device (deprecated silicon)\n"
SUPPORT_MESSAGE);
}if (msp432_bank->device_id == 0xFFFF) { ... } else {
command_print_sameline(cmd, "MSP432P401x Device Rev %c (deprecated silicon)\n"
SUPPORT_MESSAGE, (char)msp432_bank->hardware_rev);
}else { ... }
break;case MSP432P401X_DEPR:
case MSP432P401X:
command_print_sameline(cmd, "MSP432P401x Device Rev %c\n",
(char)msp432_bank->hardware_rev);
break;case MSP432P401X:
case MSP432P411X:
command_print_sameline(cmd, "MSP432P411x Device Rev %c\n",
(char)msp432_bank->hardware_rev);
break;case MSP432P411X:
case MSP432E401Y:
command_print_sameline(cmd, "MSP432E401Y Device\n");
break;case MSP432E401Y:
case MSP432E411Y:
command_print_sameline(cmd, "MSP432E411Y Device\n");
break;case MSP432E411Y:
case MSP432E4X_GUESS:
command_print_sameline(cmd,
"Unrecognized MSP432E4 DID0 and DID1 IDs (%08" PRIX32 ", %08" PRIX32 ")",
msp432_bank->device_id, msp432_bank->hardware_rev);
break;case MSP432E4X_GUESS:
case MSP432P401X_GUESS:
case MSP432P411X_GUESS:
default:
command_print_sameline(cmd,
"Unrecognized MSP432P4 Device ID and Hardware Rev (%04" PRIX32 ", %02" PRIX32 ")",
msp432_bank->device_id, msp432_bank->hardware_rev);
break;default
}switch (msp432_bank->device_type) { ... }
return ERROR_OK;
}{ ... }
static int msp432_protect_check(struct flash_bank *bank)
{
return ERROR_OK;
}{ ... }
static void msp432_flash_free_driver_priv(struct flash_bank *bank)
{
bool is_main = bank->base == FLASH_BASE;
if (is_main)
free(bank->driver_priv);
bank->driver_priv = NULL;
}{ ... }
static const struct command_registration msp432_exec_command_handlers[] = {
{
.name = "mass_erase",
.handler = msp432_mass_erase_command,
.mode = COMMAND_EXEC,
.help = "Erase entire flash memory on device.",
.usage = "bank_id ['main' | 'all']",
...},
{
.name = "bsl",
.handler = msp432_bsl_command,
.mode = COMMAND_EXEC,
.help = "Allow BSL to be erased or written by flash commands.",
.usage = "bank_id ['unlock' | 'lock']",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration msp432_command_handlers[] = {
{
.name = "msp432",
.mode = COMMAND_EXEC,
.help = "MSP432 flash command group",
.usage = "",
.chain = msp432_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
const struct flash_driver msp432_flash = {
.name = "msp432",
.commands = msp432_command_handlers,
.flash_bank_command = msp432_flash_bank_command,
.erase = msp432_erase,
.write = msp432_write,
.read = default_flash_read,
.probe = msp432_probe,
.auto_probe = msp432_auto_probe,
.erase_check = default_flash_blank_check,
.protect_check = msp432_protect_check,
.info = msp432_info,
.free_driver_priv = msp432_flash_free_driver_priv,
...};