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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
142
144
146
148
150
152
154
155
156
157
158
174
175
176
177
178
179
180
181
182
183
184
189
190
191
192
193
194
195
196
197
198
199
204
205
210
211
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
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
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
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
385
386
396
397
398
399
400
401
402
403
404
405
406
410
411
414
415
420
421
422
423
424
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
449
450
451
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
501
502
503
504
505
506
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
544
545
546
547
548
549
550
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
578
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include "msp432p4.h"
#include <helper/binarybuffer.h>
#include <target/algorithm.h>
#include <target/armv7m.h>
#include <target/image.h>
6 includes
#define FLASH_FUNCTION 0x20000150
#define FLASH_MASS_ERASE 0x01
#define FLASH_SECTOR_ERASE 0x02
#define FLASH_PROGRAM 0x04
#define FLASH_INIT 0x08
#define RETURN_CODE 0x20000154
#define DEFAULT_CODE 0x00000DEF
#define ENTRY_POINT 0x01000180
#define SRC_ADDRESS 0x20000160
#define DST_ADDRESS 0x20000164
#define SRC_LENGTH 0x20000168
#define ERASE_PARAMETER 0x2000016C
#define ERASE_MAIN 0x01
#define ERASE_INFO 0x02
#define ERASE_MAIN_INFO 0x03
#define SRC_START 0x20002000
#define STC_MAX_LENGTH 4096
#define FLASH_BUSY 0x00000001
#define FLASH_SUCCESS 0x00000ACE
#define FLASH_ERROR 0x0000DEAD
#define FLASH_TIMEOUT_ERROR 0xDEAD0000
#define FLASH_VERIFY_ERROR 0xDEADDEAD
#define FLASH_WRONG_CMD 0x00000BAD
#define FLASH_PWR_ERR 0x00DEAD00
#define FLASH_SIZE_REG 0xE0043020
#define CPU_TYPE 0xE000ED00
#define DDDS_DEV_ID 0x00203008
#define TIMEOUT 2000
28 defines
struct msp432p4_flash_bank {
uint32_t register_base;
uint32_t user_bank_size;
int probed, initialized;
...};
static int msp432p4_init(struct flash_bank *bank);
void hexDump (char *desc, const uint8_t *addr, int len) {
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char*)addr;
if (desc != NULL)
printf ("%s:\n", desc);
for (i = 0; i < len; i++) {
if ((i % 16) == 0) {
if (i != 0)
printf (" %s\n", buff);
printf (" %04x ", i);
}if ((i % 16) == 0) { ... }
printf (" %02x", pc[i]);
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0';
}for (i = 0; i < len; i++) { ... }
while ((i % 16) != 0) {
printf (" ");
i++;
}while ((i % 16) != 0) { ... }
printf (" %s\n", buff);
}{ ... }
FLASH_BANK_COMMAND_HANDLER(msp432p4_flash_bank_command)
{
struct msp432p4_flash_bank *msp432p4_info;
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
msp432p4_info = malloc(sizeof(struct msp432p4_flash_bank));
bank->driver_priv = msp432p4_info;
msp432p4_info->register_base = bank->base;
msp432p4_info->user_bank_size = bank->size;
msp432p4_info->probed = 0;
msp432p4_info->initialized = 0;
return ERROR_OK;
}{ ... }
static char* msp432p4_decode_error(uint32_t error_code)
{
switch (error_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_CMD:
return "FLASH_WRONG_CMD";case FLASH_WRONG_CMD:
case FLASH_PWR_ERR:
return "FLASH_PWR_ERR";case FLASH_PWR_ERR:
}switch (error_code) { ... }
return "UNDEFINED";
}{ ... }
static int msp432p4_init_if_needed(struct flash_bank *bank)
{
struct msp432p4_flash_bank *msp432p4_info = bank->driver_priv;
if (!msp432p4_info->initialized)
{
int retval = msp432p4_init(bank);
if (retval != ERROR_OK)
{
msp432p4_info->initialized = retval;
return retval;
}if (retval != ERROR_OK) { ... }
msp432p4_info->initialized = 1;
}if (!msp432p4_info->initialized) { ... }
return ERROR_OK;
}{ ... }
static int msp432p4_run_algo(struct flash_bank *bank, uint32_t function,
uint32_t src_address, uint32_t dst_address,
uint32_t src_len)
{
struct target *target = bank->target;
struct armv7m_algorithm armv7m_info;
struct working_area *write_algorithm;
int retval;
if (target_alloc_working_area(target, sizeof(msp432p4_flash_write_code),
&write_algorithm) != ERROR_OK) {
LOG_WARNING("no working area available, can't do block memory writes");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (target_alloc_working_area(target, sizeof(msp432p4_flash_write_code), &write_algorithm) != ERROR_OK) { ... }
retval = target_write_buffer(target, write_algorithm->address,
sizeof(msp432p4_flash_write_code), msp432p4_flash_write_code);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, FLASH_FUNCTION, function);
if (retval != ERROR_OK)
return retval;
if (src_address > 0) {
retval = target_write_u32(target, SRC_ADDRESS, src_address);
if (retval != ERROR_OK)
return retval;
}if (src_address > 0) { ... }
if (function == FLASH_PROGRAM || function == FLASH_SECTOR_ERASE) {
retval = target_write_u32(target, DST_ADDRESS, dst_address);
if (retval != ERROR_OK)
return retval;
}if (function == FLASH_PROGRAM || function == FLASH_SECTOR_ERASE) { ... }
if (src_len > 0) {
retval = target_write_u32(target, SRC_LENGTH, src_len);
if (retval != ERROR_OK)
return retval;
}if (src_len > 0) { ... }
armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
armv7m_info.core_mode = ARM_MODE_THREAD;
retval = target_run_algorithm(target, 0, NULL, 0, NULL,
ENTRY_POINT, 0,
TIMEOUT, &armv7m_info);
target_free_working_area(target, write_algorithm);
return retval;
}{ ... }
static int msp432p4_init(struct flash_bank *bank)
{
struct target *target = bank->target;
uint32_t reg_value;
char *error_name;
int retval;
retval = target_read_u32(target, DDDS_DEV_ID, ®_value);
if (retval != ERROR_OK)
return retval;
retval = msp432p4_run_algo(bank, FLASH_INIT, 0, 0, 0);
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, RETURN_CODE, ®_value);
if (retval != ERROR_OK)
return retval;
if (reg_value != FLASH_SUCCESS) {
error_name = msp432p4_decode_error(reg_value);
LOG_ERROR("Cannot init flash controller: %s.", error_name);
}if (reg_value != FLASH_SUCCESS) { ... }
retval = target_write_u32(target, RETURN_CODE, DEFAULT_CODE);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
static int msp432p4_mass_erase(struct flash_bank *bank)
{
struct target *target = bank->target;
uint32_t reg_value;
char *error_name;
int retval;
retval = msp432p4_init_if_needed(bank);
if (retval != ERROR_OK)
return retval;
retval = msp432p4_run_algo(bank, FLASH_MASS_ERASE, 0, 0, 0);
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, RETURN_CODE, ®_value);
if (retval != ERROR_OK)
return retval;
if (reg_value != FLASH_SUCCESS) {
error_name = msp432p4_decode_error(reg_value);
LOG_ERROR("Cannot mass erase: %s.", error_name);
}if (reg_value != FLASH_SUCCESS) { ... }
retval = target_write_u32(target, RETURN_CODE, DEFAULT_CODE);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
static int msp432p4_sector_erase(struct flash_bank *bank, uint32_t dst_address)
{
struct target *target = bank->target;
uint32_t reg_value;
char *error_name;
int retval;
retval = msp432p4_init_if_needed(bank);
if (retval != ERROR_OK)
return retval;
retval = msp432p4_run_algo(bank, FLASH_SECTOR_ERASE, 0, dst_address, 0);
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, RETURN_CODE, ®_value);
if (retval != ERROR_OK)
return retval;
if (reg_value != FLASH_SUCCESS) {
error_name = msp432p4_decode_error(reg_value);
LOG_ERROR("Cannot erase sector at address: %s.", error_name);
}if (reg_value != FLASH_SUCCESS) { ... }
retval = target_write_u32(target, RETURN_CODE, DEFAULT_CODE);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
COMMAND_HANDLER(msp432p4_handle_init_command)
{
struct flash_bank *bank;
int retval;
retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
return msp432p4_init(bank);
}{ ... }
COMMAND_HANDLER(msp432p4_handle_mass_erase_command)
{
struct flash_bank *bank;
int retval;
retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
return msp432p4_mass_erase(bank);
}{ ... }
COMMAND_HANDLER(msp432p4_handle_erase_sector)
{
struct flash_bank *bank;
int retval;
uint32_t dst_address;
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], dst_address);
return msp432p4_sector_erase(bank, dst_address);
}{ ... }
static const struct command_registration msp432p4_exec_command_handlers[] = {
{
.name = "mass_erase",
.handler = msp432p4_handle_mass_erase_command,
.mode = COMMAND_EXEC,
.help = "Erase entire flash device.",
...},
{
.name = "init",
.handler = msp432p4_handle_init_command,
.mode = COMMAND_EXEC,
.help = "Init flash for operations.",
...},
{
.name = "erase_sector",
.handler = msp432p4_handle_erase_sector,
.mode = COMMAND_EXEC,
.help = "Erase a single sector from flash.",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration msp432p4_command_handlers[] = {
{
.name = "msp432p4",
.mode = COMMAND_ANY,
.help = "msp432p4 flash command group",
.usage = "Set of flashing commands for a msp432p4 target",
.chain = msp432p4_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
static int msp432p4_erase(struct flash_bank *bank, unsigned first, unsigned last)
{
struct target *target = bank->target;
int retval = ERROR_OK, i;
retval = msp432p4_init_if_needed(bank);
if (retval != ERROR_OK)
return retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if ((first == 0) && (last == (bank->num_sectors - 1))) {
return msp432p4_mass_erase(bank);
}if ((first == 0) && (last == (bank->num_sectors - 1))) { ... }
for (i = first; i <= last; ++i) {
retval = msp432p4_sector_erase(bank, bank->sectors[i].offset);
if (retval != ERROR_OK)
return retval;
}for (i = first; i <= last; ++i) { ... }
return retval;
}{ ... }
static int msp432p4_protect(struct flash_bank *bank, int set, unsigned first, unsigned last)
{
/* ... */
return ERROR_OK;
}{ ... }
static int msp432p4_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
struct target *target = bank->target;
struct working_area *write_algorithm;
uint32_t reg_value;
char *error_name;
struct armv7m_algorithm armv7m_info;
int retval = ERROR_OK, size = 0, ofs = 0;
retval = msp432p4_init_if_needed(bank);
if (retval != ERROR_OK)
return retval;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (target_alloc_working_area(target, sizeof(msp432p4_flash_write_code),
&write_algorithm) != ERROR_OK) {
LOG_WARNING("no working area available, can't do block memory writes");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (target_alloc_working_area(target, sizeof(msp432p4_flash_write_code), &write_algorithm) != ERROR_OK) { ... };
retval = target_write_buffer(target, write_algorithm->address,
sizeof(msp432p4_flash_write_code), msp432p4_flash_write_code);
if (retval != ERROR_OK)
return retval;
armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
armv7m_info.core_mode = ARM_MODE_THREAD;
while (count) {
if (count > 4096)
size = 4096;
else
size = count;
retval = target_write_buffer(target, SRC_START, size, buffer + ofs);
if (retval != ERROR_OK) {
LOG_ERROR("Unable to write block write code to target");
return ERROR_FLASH_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, FLASH_FUNCTION, FLASH_PROGRAM);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, SRC_ADDRESS, SRC_START);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, DST_ADDRESS, ofs);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, SRC_LENGTH, size);
if (retval != ERROR_OK)
return retval;
count -= size;
ofs += size;
retval = target_run_algorithm(target, 0, NULL, 0, NULL,
ENTRY_POINT, 0,
TIMEOUT, &armv7m_info);
if (retval == ERROR_FLASH_OPERATION_FAILED) {
LOG_ERROR("Cannot flash!");
}if (retval == ERROR_FLASH_OPERATION_FAILED) { ... }
retval = target_read_u32(target, RETURN_CODE, ®_value);
if (retval != ERROR_OK)
return retval;
if (reg_value != FLASH_SUCCESS) {
error_name = msp432p4_decode_error(reg_value);
LOG_ERROR("Cannot write flash program: %s.", error_name);
}if (reg_value != FLASH_SUCCESS) { ... }
}while (count) { ... }
target_free_working_area(target, write_algorithm);
return retval;
}{ ... }
static int msp432p4_probe(struct flash_bank *bank)
{
struct target *target = bank->target;
struct msp432p4_flash_bank *msp432p4_info = bank->driver_priv;
uint32_t reg_value, cpu_id;
uint32_t base_address = 0x00;
uint32_t flash_size;
int retval, i, number_sectors;
retval = target_read_u32(target, CPU_TYPE, &cpu_id);
if (retval != ERROR_OK)
return retval;
if (((cpu_id >> 4) & 0xFFF) != 0xC24)
return ERROR_FAIL;
retval = target_read_u32(target, FLASH_SIZE_REG, ®_value);
if (retval != ERROR_OK)
return retval;
number_sectors = reg_value / 4096;
flash_size = reg_value;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}if (bank->sectors) { ... }
bank->base = base_address;
bank->size = flash_size;
bank->num_sectors = number_sectors;
bank->sectors = malloc(sizeof(struct flash_sector) * number_sectors);
for (i = 0; i < number_sectors; i++) {
bank->sectors[i].offset = i * 4096;
bank->sectors[i].size = 4096;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = 1;
}for (i = 0; i < number_sectors; i++) { ... }
msp432p4_info->probed = 1;
return ERROR_OK;
}{ ... }
static int msp432p4_auto_probe(struct flash_bank *bank)
{
int probed = ((struct msp432p4_flash_bank *)bank->driver_priv)->probed;
if (probed < 0)
return probed;
else if (probed)
return ERROR_OK;
else
return msp432p4_probe(bank);
}{ ... }
static int msp432p4_protect_check(struct flash_bank *bank)
{
return ERROR_OK;
}{ ... }
static int get_msp432p4_info(struct flash_bank *bank, struct command_invocation *cmd)
{
return ERROR_OK;
}{ ... }
struct flash_driver msp432p4_flash = {
.name = "msp432p4",
.commands = msp432p4_command_handlers,
.flash_bank_command = msp432p4_flash_bank_command,
.erase = msp432p4_erase,
.protect = msp432p4_protect,
.write = msp432p4_write,
.read = default_flash_read,
.probe = msp432p4_probe,
.auto_probe = msp432p4_auto_probe,
.erase_check = default_flash_blank_check,
.protect_check = msp432p4_protect_check,
.info = get_msp432p4_info,
...};