1
2
3
6
7
8
9
10
11
18
19
20
24
25
26
29
30
31
32
33
34
35
36
37
38
39
40
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
74
75
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
119
120
121
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
161
162
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
193
194
195
196
197
198
199
200
201
202
203
207
208
209
210
211
212
213
214
215
216
217
218
219
220
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
353
354
355
356
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
479
480
481
482
483
484
485
486
487
488
489
490
491
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include "cc3220sf.h"
#include <helper/binarybuffer.h>
#include <helper/time_support.h>
#include <target/algorithm.h>
#include <target/armv7m.h>
6 includes
#define FLASH_TIMEOUT 5000
struct cc3220sf_bank {
bool probed;
struct armv7m_algorithm armv7m_info;
...};
static const uint8_t cc3220sf_algo[] = {
#include "../../../contrib/loaders/flash/cc3220sf/cc3220sf.inc"
...};
static int cc3220sf_mass_erase(struct flash_bank *bank)
{
struct target *target = bank->target;
bool done;
long long start_ms;
long long elapsed_ms;
uint32_t value;
int retval = ERROR_OK;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
retval = target_write_u32(target, FMA_REGISTER_ADDR, 0);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, FMC_REGISTER_ADDR, FMC_MERASE_VALUE);
if (retval != ERROR_OK)
return retval;
done = false;
start_ms = timeval_ms();
while (!done) {
retval = target_read_u32(target, FMC_REGISTER_ADDR, &value);
if (retval != ERROR_OK)
return retval;
if ((value & FMC_MERASE_BIT) == 0) {
done = true;
}if ((value & FMC_MERASE_BIT) == 0) { ... } else {
elapsed_ms = timeval_ms() - start_ms;
if (elapsed_ms > 500)
keep_alive();
if (elapsed_ms > FLASH_TIMEOUT)
break;
}else { ... }
}while (!done) { ... }
if (!done) {
return ERROR_FAIL;
}if (!done) { ... }
return retval;
}{ ... }
FLASH_BANK_COMMAND_HANDLER(cc3220sf_flash_bank_command)
{
struct cc3220sf_bank *cc3220sf_bank;
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
cc3220sf_bank = malloc(sizeof(struct cc3220sf_bank));
if (!cc3220sf_bank)
return ERROR_FAIL;
cc3220sf_bank->probed = false;
bank->driver_priv = cc3220sf_bank;
return ERROR_OK;
}{ ... }
static int cc3220sf_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
struct target *target = bank->target;
bool done;
long long start_ms;
long long elapsed_ms;
uint32_t address;
uint32_t value;
int retval = ERROR_OK;
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 cc3220sf_mass_erase(bank);
}if ((first == 0) && (last == (bank->num_sectors - 1))) { ... }
for (unsigned int i = first; i <= last; i++) {
address = FLASH_BASE_ADDR + i * FLASH_SECTOR_SIZE;
retval = target_write_u32(target, FMA_REGISTER_ADDR, address);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, FMC_REGISTER_ADDR, FMC_ERASE_VALUE);
if (retval != ERROR_OK)
return retval;
done = false;
start_ms = timeval_ms();
while (!done) {
retval = target_read_u32(target, FMC_REGISTER_ADDR, &value);
if (retval != ERROR_OK)
return retval;
if ((value & FMC_ERASE_BIT) == 0) {
done = true;
}if ((value & FMC_ERASE_BIT) == 0) { ... } else {
elapsed_ms = timeval_ms() - start_ms;
if (elapsed_ms > 500)
keep_alive();
if (elapsed_ms > FLASH_TIMEOUT)
break;
}else { ... }
}while (!done) { ... }
if (!done) {
return ERROR_FAIL;
}if (!done) { ... }
}for (unsigned int i = first; i <= last; i++) { ... }
return retval;
}{ ... }
static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
struct target *target = bank->target;
struct cc3220sf_bank *cc3220sf_bank = bank->driver_priv;
struct working_area *algo_working_area;
struct working_area *buffer_working_area;
struct reg_param reg_params[3];
uint32_t algo_base_address;
uint32_t algo_buffer_address;
uint32_t algo_buffer_size;
uint32_t address;
uint32_t remaining;
uint32_t words;
uint32_t result;
int retval = ERROR_OK;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
retval = target_alloc_working_area(target, sizeof(cc3220sf_algo),
&algo_working_area);
if (retval != ERROR_OK)
return retval;
retval = target_alloc_working_area(target,
target_get_working_area_avail(target), &buffer_working_area);
if (retval != ERROR_OK) {
target_free_working_area(target, algo_working_area);
return retval;
}if (retval != ERROR_OK) { ... }
algo_base_address = algo_working_area->address;
algo_buffer_address = buffer_working_area->address;
algo_buffer_size = buffer_working_area->size;
if (algo_buffer_size > 0x80)
algo_buffer_size &= ~0x7f;
retval = target_write_buffer(target, algo_base_address,
sizeof(cc3220sf_algo), cc3220sf_algo);
if (retval != ERROR_OK) {
target_free_working_area(target, algo_working_area);
target_free_working_area(target, buffer_working_area);
return retval;
}if (retval != ERROR_OK) { ... }
cc3220sf_bank->armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
cc3220sf_bank->armv7m_info.core_mode = ARM_MODE_THREAD;
init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
init_reg_param(®_params[2], "r2", 32, PARAM_IN_OUT);
address = FLASH_BASE_ADDR + offset;
remaining = count;
/* ... */
if (0 != (address & 0x3)) {
uint8_t head[4];
uint32_t head_offset = address & 0x03;
uint32_t head_address = address & 0xfffffffc;
retval = target_read_buffer(target, head_address, sizeof(head), head);
if (retval == ERROR_OK) {
while ((remaining > 0) && (head_offset < 4)) {
head[head_offset] = *buffer;
head_offset++;
address++;
buffer++;
remaining--;
}while ((remaining > 0) && (head_offset < 4)) { ... }
}if (retval == ERROR_OK) { ... }
if (retval == ERROR_OK) {
buf_set_u32(reg_params[0].value, 0, 32, algo_buffer_address);
buf_set_u32(reg_params[1].value, 0, 32, head_address);
buf_set_u32(reg_params[2].value, 0, 32, 1);
retval = target_write_buffer(target, algo_buffer_address,
sizeof(head), head);
}if (retval == ERROR_OK) { ... }
if (retval == ERROR_OK) {
retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
algo_base_address, 0, FLASH_TIMEOUT,
&cc3220sf_bank->armv7m_info);
if (retval != ERROR_OK)
LOG_ERROR("cc3220sf: Flash algorithm failed to run");
result = buf_get_u32(reg_params[2].value, 0, 32);
if (result != 0) {
retval = ERROR_FAIL;
LOG_ERROR("cc3220sf: Flash operation failed");
}if (result != 0) { ... }
}if (retval == ERROR_OK) { ... }
}if (0 != (address & 0x3)) { ... }
uint32_t tail_count = remaining & 0x03;
remaining -= tail_count;
while ((retval == ERROR_OK) && (remaining > 0)) {
buf_set_u32(reg_params[0].value, 0, 32, algo_buffer_address);
buf_set_u32(reg_params[1].value, 0, 32, address);
if (remaining >= algo_buffer_size) {
retval = target_write_buffer(target, algo_buffer_address,
algo_buffer_size, buffer);
if (retval != ERROR_OK)
break;
words = algo_buffer_size / 4;
address += algo_buffer_size;
buffer += algo_buffer_size;
remaining -= algo_buffer_size;
}if (remaining >= algo_buffer_size) { ... } else {
retval = target_write_buffer(target, algo_buffer_address,
remaining, buffer);
if (retval != ERROR_OK)
break;
words = remaining / 4;
if (0 != (remaining % 4))
words++;
address += remaining;
buffer += remaining;
remaining = 0;
}else { ... }
buf_set_u32(reg_params[2].value, 0, 32, words);
retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
algo_base_address, 0, FLASH_TIMEOUT,
&cc3220sf_bank->armv7m_info);
if (retval != ERROR_OK) {
LOG_ERROR("cc3220sf: Flash algorithm failed to run");
break;
}if (retval != ERROR_OK) { ... }
result = buf_get_u32(reg_params[2].value, 0, 32);
if (result != 0) {
retval = ERROR_FAIL;
LOG_ERROR("cc3220sf: Flash operation failed");
break;
}if (result != 0) { ... }
keep_alive();
}while ((retval == ERROR_OK) && (remaining > 0)) { ... }
if ((retval == ERROR_OK) && (tail_count != 0)) {
uint8_t tail[4];
uint32_t tail_offset = 0;
retval = target_read_buffer(target, address, sizeof(tail), tail);
if (retval == ERROR_OK) {
while (tail_count > 0) {
tail[tail_offset] = *buffer;
tail_offset++;
buffer++;
tail_count--;
}while (tail_count > 0) { ... }
}if (retval == ERROR_OK) { ... }
if (retval == ERROR_OK) {
buf_set_u32(reg_params[0].value, 0, 32, algo_buffer_address);
buf_set_u32(reg_params[1].value, 0, 32, address);
buf_set_u32(reg_params[2].value, 0, 32, 1);
retval = target_write_buffer(target, algo_buffer_address,
sizeof(tail), tail);
}if (retval == ERROR_OK) { ... }
if (retval == ERROR_OK) {
retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
algo_base_address, 0, FLASH_TIMEOUT,
&cc3220sf_bank->armv7m_info);
if (retval != ERROR_OK)
LOG_ERROR("cc3220sf: Flash algorithm failed to run");
result = buf_get_u32(reg_params[2].value, 0, 32);
if (result != 0) {
retval = ERROR_FAIL;
LOG_ERROR("cc3220sf: Flash operation failed");
}if (result != 0) { ... }
}if (retval == ERROR_OK) { ... }
}if ((retval == ERROR_OK) && (tail_count != 0)) { ... }
destroy_reg_param(®_params[0]);
destroy_reg_param(®_params[1]);
destroy_reg_param(®_params[2]);
target_free_working_area(target, algo_working_area);
target_free_working_area(target, buffer_working_area);
return retval;
}{ ... }
static int cc3220sf_probe(struct flash_bank *bank)
{
struct cc3220sf_bank *cc3220sf_bank = bank->driver_priv;
uint32_t base;
uint32_t size;
unsigned int num_sectors;
base = FLASH_BASE_ADDR;
size = FLASH_NUM_SECTORS * FLASH_SECTOR_SIZE;
num_sectors = FLASH_NUM_SECTORS;
free(bank->sectors);
bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
if (!bank->sectors)
return ERROR_FAIL;
bank->base = base;
bank->size = size;
bank->write_start_alignment = 0;
bank->write_end_alignment = 0;
bank->num_sectors = num_sectors;
for (unsigned int i = 0; i < num_sectors; i++) {
bank->sectors[i].offset = i * FLASH_SECTOR_SIZE;
bank->sectors[i].size = FLASH_SECTOR_SIZE;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = 0;
}for (unsigned int i = 0; i < num_sectors; i++) { ... }
cc3220sf_bank->probed = true;
return ERROR_OK;
}{ ... }
static int cc3220sf_auto_probe(struct flash_bank *bank)
{
struct cc3220sf_bank *cc3220sf_bank = bank->driver_priv;
int retval = ERROR_OK;
if (!cc3220sf_bank->probed)
retval = cc3220sf_probe(bank);
return retval;
}{ ... }
static int cc3220sf_info(struct flash_bank *bank, struct command_invocation *cmd)
{
command_print_sameline(cmd, "CC3220SF with 1MB internal flash\n");
return ERROR_OK;
}{ ... }
const struct flash_driver cc3220sf_flash = {
.name = "cc3220sf",
.flash_bank_command = cc3220sf_flash_bank_command,
.erase = cc3220sf_erase,
.write = cc3220sf_write,
.read = default_flash_read,
.probe = cc3220sf_probe,
.auto_probe = cc3220sf_auto_probe,
.erase_check = default_flash_blank_check,
.info = cc3220sf_info,
.free_driver_priv = default_flash_free_driver_priv,
...};