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
|
/**
* @file flc.h
* @brief Flash Controler driver.
* @details This driver can be used to operate on the embedded flash memory.
*/
/* ****************************************************************************
* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*
* $Date: 2019-06-05 16:53:29 -0500 (Wed, 05 Jun 2019) $
* $Revision: 43696 $
*
*************************************************************************** */
/* **** Includes **** */
#include <string.h>
#include "mxc_config.h"
#include "mxc_sys.h"
#include "flc.h"
#include "flc_regs.h"
/* **** Definitions **** */
/* **** Globals **** */
/* **** Functions **** */
// *****************************************************************************
#if defined (__ICCARM__)
#pragma section=".flashprog"
#endif
#if defined ( __GNUC__ )
__attribute__ ((section(".flashprog")))
#endif
static int prepare_flc(void)
{
// Set flash clock divider to generate a 1MHz clock from the APB clock
MXC_FLC->clkdiv = SystemCoreClock / 1000000;
/* Check if the flash controller is busy */
if (FLC_Busy()) {
return E_BUSY;
}
/* Clear stale errors */
if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
}
/* Unlock flash */
MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_UNLOCK) | MXC_S_FLC_CN_UNLOCK_UNLOCKED;
return E_NO_ERROR;
}
// *****************************************************************************
#if defined (__ICCARM__)
// IAR memory section declaration for the in-system flash programming functions to be loaded in RAM.
#pragma section=".flashprog"
#endif
#if defined ( __GNUC__ )
__attribute__ ((section(".flashprog")))
#endif
int FLC_Init(const sys_cfg_flc_t *sys_cfg)
{
SYS_FLC_Init(sys_cfg);
return E_NO_ERROR;
}
// *****************************************************************************
#if defined (__ICCARM__)
// IAR memory section declaration for the in-system flash programming functions to be loaded in RAM.
#pragma section=".flashprog"
#endif
#if defined ( __GNUC__ )
__attribute__ ((section(".flashprog")))
#endif
int FLC_Busy(void)
{
return (MXC_FLC->cn & (MXC_F_FLC_CN_WR | MXC_F_FLC_CN_ME | MXC_F_FLC_CN_PGE));
}
// *****************************************************************************
#if defined (__ICCARM__)
#pragma section=".flashprog"
#endif
#if defined ( __GNUC__ )
__attribute__ ((section(".flashprog")))
#endif
int FLC_MassErase(void)
{
int err;
if ((err = prepare_flc()) != E_NO_ERROR)
return err;
/* Write mass erase code */
MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_ERASE_CODE) | MXC_S_FLC_CN_ERASE_CODE_ERASEALL;
/* Issue mass erase command */
MXC_FLC->cn |= MXC_F_FLC_CN_ME;
/* Wait until flash operation is complete */
while (FLC_Busy());
/* Lock flash */
MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
/* Check access violations */
if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
return E_BAD_STATE;
}
SYS_Flash_Operation();
return E_NO_ERROR;
}
// *****************************************************************************
#if defined (__ICCARM__)
#pragma section=".flashprog"
#endif
#if defined ( __GNUC__ )
__attribute__ ((section(".flashprog")))
#endif
int FLC_PageErase(uint32_t address)
{
int err;
if ((err = prepare_flc()) != E_NO_ERROR)
return err;
// Align address on page boundary
address = address - (address % MXC_FLASH_PAGE_SIZE);
/* Write page erase code */
MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_ERASE_CODE) | MXC_S_FLC_CN_ERASE_CODE_ERASEPAGE;
/* Issue page erase command */
MXC_FLC->addr = address;
MXC_FLC->cn |= MXC_F_FLC_CN_PGE;
/* Wait until flash operation is complete */
while (FLC_Busy());
/* Lock flash */
MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
/* Check access violations */
if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
return E_BAD_STATE;
}
SYS_Flash_Operation();
return E_NO_ERROR;
}
// *****************************************************************************
#if defined (__ICCARM__)
#pragma section=".flashprog"
#endif
#if defined ( __GNUC__ )
__attribute__ ((section(".flashprog")))
#endif
int FLC_Erase(uint32_t start, uint32_t end)
{
int retval;
uint32_t addr;
// Align start and end on page boundaries
start = start - (start % MXC_FLASH_PAGE_SIZE);
end = end - (end % MXC_FLASH_PAGE_SIZE);
for (addr = start; addr <= end; addr += MXC_FLASH_PAGE_SIZE) {
retval = FLC_PageErase(addr);
if (retval != E_NO_ERROR) {
return retval;
}
}
return E_NO_ERROR;
}
// *****************************************************************************
#if defined (__ICCARM__)
#pragma section=".flashprog"
#endif
#if defined ( __GNUC__ )
__attribute__ ((section(".flashprog")))
#endif
int FLC_BufferErase(uint32_t start, uint32_t end, uint8_t *buffer, unsigned length)
{
int retval;
uint32_t start_align, start_len, end_align, end_len;
// Align start and end on page boundaries, calculate length of data to buffer
start_align = start - (start % MXC_FLASH_PAGE_SIZE);
start_len = (start % MXC_FLASH_PAGE_SIZE);
end_align = end - (end % MXC_FLASH_PAGE_SIZE);
end_len = ((MXC_FLASH_PAGE_SIZE - (end % MXC_FLASH_PAGE_SIZE)) % MXC_FLASH_PAGE_SIZE);
// Make sure the length of buffer is sufficient
if ((length < start_len) || (length < end_len)) {
return E_BAD_PARAM;
}
// Start and end address are in the same page
if (start_align == end_align) {
if (length < (start_len + end_len)) {
return E_BAD_PARAM;
}
// Buffer first page data and last page data, erase and write
memcpy(buffer, (void*)start_align, start_len);
memcpy(&buffer[start_len], (void*)end, end_len);
retval = FLC_PageErase(start_align);
if (retval != E_NO_ERROR) {
return retval;
}
retval = FLC_Write(start_align, start_len, buffer);
if (retval != E_NO_ERROR) {
return retval;
}
retval = FLC_Write(end, end_len, &buffer[start_len]);
if (retval != E_NO_ERROR) {
return retval;
}
return E_NO_ERROR;
}
// Buffer, erase, and write the data in the first page
memcpy(buffer, (void*)start_align, start_len);
retval = FLC_PageErase(start_align);
if (retval != E_NO_ERROR) {
return retval;
}
retval = FLC_Write(start_align, start_len, buffer);
if (retval != E_NO_ERROR) {
return retval;
}
// Buffer, erase, and write the data in the last page
memcpy(buffer, (void*)end, end_len);
retval = FLC_PageErase(end_align);
if (retval != E_NO_ERROR) {
return retval;
}
retval = FLC_Write(end, end_len, buffer);
if (retval != E_NO_ERROR) {
return retval;
}
// Erase the remaining pages
if (start_align != end_align) {
return FLC_Erase((start_align + MXC_FLASH_PAGE_SIZE), (end_align - MXC_FLASH_PAGE_SIZE));
}
return E_NO_ERROR;
}
// *****************************************************************************
#if defined (__ICCARM__)
#pragma section=".flashprog"
#endif
#if defined ( __GNUC__ )
__attribute__ ((section(".flashprog")))
#endif
int FLC_Write32(uint32_t address, uint32_t data)
{
int err;
// Address checked if it is byte addressable
if (address & 0x3) {
return E_BAD_PARAM;
}
if ((err = prepare_flc()) != E_NO_ERROR)
return err;
// write in 32-bit units
MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
MXC_FLC->cn &= ~MXC_F_FLC_CN_BRST;
// write the data
MXC_FLC->addr = address;
MXC_FLC->data[0] = data;
MXC_FLC->cn |= MXC_F_FLC_CN_WR;
/* Wait until flash operation is complete */
while (FLC_Busy()) {}
/* Lock flash */
MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
/* Check access violations */
if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
return E_BAD_STATE;
}
SYS_Flash_Operation();
return E_NO_ERROR;
}
// *****************************************************************************
#if defined (__ICCARM__)
#pragma section=".flashprog"
#endif
#if defined ( __GNUC__ )
__attribute__ ((section(".flashprog")))
#endif
int FLC_Write128(uint32_t address, uint32_t *data)
{
int err;
// Address checked if it is word addressable
if (address & 0xF) {
return E_BAD_PARAM;
}
if ((err = prepare_flc()) != E_NO_ERROR)
return err;
// write 128-bits
MXC_FLC->cn &= ~MXC_F_FLC_CN_WDTH;
// write the data
MXC_FLC->addr = address;
memcpy((void*)&MXC_FLC->data[0], data, 16);
MXC_FLC->cn |= MXC_F_FLC_CN_WR;
/* Wait until flash operation is complete */
while (FLC_Busy());
/* Lock flash */
MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
/* Check access violations */
if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
return E_BAD_STATE;
}
SYS_Flash_Operation();
return E_NO_ERROR;
}
// *****************************************************************************
#if defined (__ICCARM__)
#pragma section=".flashprog"
#endif
#if defined ( __GNUC__ )
__attribute__ ((section(".flashprog")))
#endif
int FLC_Write(uint32_t address, uint32_t length, uint8_t *buffer)
{
int err;
uint32_t bytes_written;
uint8_t current_data[4];
if ((err = prepare_flc()) != E_NO_ERROR)
return err;
// write in 32-bit units until we are 128-bit aligned
MXC_FLC->cn &= ~MXC_F_FLC_CN_BRST;
MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
// Align the address and read/write if we have to
if (address & 0x3) {
// Figure out how many bytes we have to write to round up the address
bytes_written = 4 - (address & 0x3);
// Save the data currently in the flash
memcpy(current_data, (void*)(address & (~0x3)), 4);
// Modify current_data to insert the data from buffer
memcpy(¤t_data[4-bytes_written], buffer, bytes_written);
// Write the modified data
MXC_FLC->addr = address - (address % 4);
memcpy((void*)&MXC_FLC->data[0], ¤t_data, 4);
MXC_FLC->cn |= MXC_F_FLC_CN_WR;
/* Wait until flash operation is complete */
while (FLC_Busy());
address += bytes_written;
length -= bytes_written;
buffer += bytes_written;
}
while ( (length >= 4) && ((address & 0xF) != 0) ) {
MXC_FLC->addr = address;
memcpy((void*)&MXC_FLC->data[0], buffer, 4);
MXC_FLC->cn |= MXC_F_FLC_CN_WR;
/* Wait until flash operation is complete */
while (FLC_Busy());
address += 4;
length -= 4;
buffer += 4;
}
if (length >= 16) {
// write in 128-bit bursts while we can
MXC_FLC->cn &= ~MXC_F_FLC_CN_WDTH;
while (length >= 16) {
MXC_FLC->addr = address;
memcpy((void*)&MXC_FLC->data[0], buffer, 16);
MXC_FLC->cn |= MXC_F_FLC_CN_WR;
/* Wait until flash operation is complete */
while (FLC_Busy());
address += 16;
length -= 16;
buffer += 16;
}
// Return to 32-bit writes.
MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
}
while (length >= 4) {
MXC_FLC->addr = address;
memcpy((void*)&MXC_FLC->data[0], buffer, 4);
MXC_FLC->cn |= MXC_F_FLC_CN_WR;
/* Wait until flash operation is complete */
while (FLC_Busy());
address += 4;
length -= 4;
buffer += 4;
}
if (length > 0) {
// Save the data currently in the flash
memcpy(current_data, (void*)(address), 4);
// Modify current_data to insert the data from buffer
memcpy(current_data, buffer, length);
MXC_FLC->addr = address;
memcpy((void*)&MXC_FLC->data[0], current_data, 4);
MXC_FLC->cn |= MXC_F_FLC_CN_WR;
/* Wait until flash operation is complete */
while (FLC_Busy());
}
/* Lock flash */
MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
/* Check access violations */
if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
return E_BAD_STATE;
}
SYS_Flash_Operation();
return E_NO_ERROR;
}
int FLC_EnableInt(uint32_t mask)
{
uint32_t tmp;
mask &= (MXC_F_FLC_INTR_DONEIE | MXC_F_FLC_INTR_AFIE);
if (!mask) {
/* No bits set? Wasn't something we can enable. */
return E_BAD_PARAM;
}
/* Careful with access_fail bit, as it is W0C */
tmp = MXC_FLC->intr | MXC_F_FLC_INTR_AF;
/* Don't lose done flag */
tmp &= ~(MXC_F_FLC_INTR_DONE);
/* Apply enables and write back */
MXC_FLC->intr = (tmp | mask);
return E_NO_ERROR;
}
int FLC_DisableInt(uint32_t mask)
{
uint32_t tmp;
mask &= (MXC_F_FLC_INTR_DONEIE | MXC_F_FLC_INTR_AFIE);
if (!mask) {
/* No bits set? Wasn't something we can disable. */
return E_BAD_PARAM;
}
/* Careful with access_fail bit, as it is W0C */
tmp = MXC_FLC->intr | MXC_F_FLC_INTR_AF;
/* Don't lose done flag */
tmp &= ~(MXC_F_FLC_INTR_DONE);
/* Apply disables and write back */
MXC_FLC->intr = (tmp & ~mask);
return E_NO_ERROR;
}
int FLC_GetFlags(void)
{
return (MXC_FLC->intr & (MXC_F_FLC_INTR_DONE | MXC_F_FLC_INTR_AF));
}
int FLC_ClearFlags(uint32_t mask)
{
mask &= (MXC_F_FLC_INTR_DONE | MXC_F_FLC_INTR_AF);
if (!mask) {
/* No bits set? Wasn't something we can clear. */
return E_BAD_PARAM;
}
// Both bits are write zero clear
MXC_FLC->intr ^= mask;
return E_NO_ERROR;
}
int FLC_UnlockInfoBlock()
{
MXC_FLC->acntl = 0x3a7f5ca3;
MXC_FLC->acntl = 0xa1e34f20;
MXC_FLC->acntl = 0x9608b2c1;
return E_NO_ERROR;
}
int FLC_LockInfoBlock()
{
MXC_FLC->acntl = 0xDEADBEEF;
return E_NO_ERROR;
}
|