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
81
82
86
87
88
89
90
98
99
109
110
113
114
115
116
117
118
119
120
121
124
125
126
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
174
179
184
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
236
241
246
251
252
253
254
255
259
260
261
262
263
264
265
267
268
270
271
272
278
283
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
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
390
391
392
393
394
395
396
397
398
399
407
408
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
431
432
433
434
435
436
437
438
439
440
441
442
446
447
448
449
450
451
455
462
463
467
482
483
487
494
495
499
500
501
502
503
504
505
506
507
508
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
540
541
544
545
550
551
552
553
554
555
556
557
560
561
562
563
573
574
575
580
581
583
584
586
587
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
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
711
712
713
720
721
728
729
736
737
744
745
746
747
748
749
750
751
752
753
760
761
762
765
766
767
768
771
772
775
/* ... */
#include "stm32f4xx_hal.h"
/* ... */
/* ... */
#ifdef HAL_FLASH_MODULE_ENABLED
/* ... */
#define FLASH_TIMEOUT_VALUE 50000U
/* ... */ Private define
/* ... */
FLASH_ProcessTypeDef pFlash;
/* ... */
Private variables
/* ... */
static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
static void FLASH_Program_Byte(uint32_t Address, uint8_t Data);
static void FLASH_SetErrorCode(void);
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
/* ... */
Private function prototypes
/* ... */
/* ... */
/* ... */
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
{
HAL_StatusTypeDef status = HAL_ERROR;
__HAL_LOCK(&pFlash);
assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
if(status == HAL_OK)
{
if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
{
FLASH_Program_Byte(Address, (uint8_t) Data);
}if (TypeProgram == FLASH_TYPEPROGRAM_BYTE) { ... }
else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
{
FLASH_Program_HalfWord(Address, (uint16_t) Data);
}else if (TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) { ... }
else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
{
FLASH_Program_Word(Address, (uint32_t) Data);
}else if (TypeProgram == FLASH_TYPEPROGRAM_WORD) { ... }
else
{
FLASH_Program_DoubleWord(Address, Data);
}else { ... }
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
FLASH->CR &= (~FLASH_CR_PG);
}if (status == HAL_OK) { ... }
__HAL_UNLOCK(&pFlash);
return status;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
{
HAL_StatusTypeDef status = HAL_OK;
__HAL_LOCK(&pFlash);
assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
__HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
__HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
pFlash.Address = Address;
if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
{
FLASH_Program_Byte(Address, (uint8_t) Data);
}if (TypeProgram == FLASH_TYPEPROGRAM_BYTE) { ... }
else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
{
FLASH_Program_HalfWord(Address, (uint16_t) Data);
}else if (TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) { ... }
else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
{
FLASH_Program_Word(Address, (uint32_t) Data);
}else if (TypeProgram == FLASH_TYPEPROGRAM_WORD) { ... }
else
{
FLASH_Program_DoubleWord(Address, Data);
}else { ... }
return status;
}{ ... }
/* ... */
void HAL_FLASH_IRQHandler(void)
{
uint32_t addresstmp = 0U;
#if defined(FLASH_SR_RDERR)
if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)/* ... */
#else
if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)/* ... */
#endif
{
if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
{
addresstmp = pFlash.Sector;
pFlash.Sector = 0xFFFFFFFFU;
}if (pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE) { ... }
else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
{
addresstmp = pFlash.Bank;
}else if (pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) { ... }
else
{
addresstmp = pFlash.Address;
}else { ... }
FLASH_SetErrorCode();
HAL_FLASH_OperationErrorCallback(addresstmp);
pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
...}
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
{
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
{
pFlash.NbSectorsToErase--;
if(pFlash.NbSectorsToErase != 0U)
{
addresstmp = pFlash.Sector;
HAL_FLASH_EndOfOperationCallback(addresstmp);
pFlash.Sector++;
addresstmp = pFlash.Sector;
FLASH_Erase_Sector(addresstmp, pFlash.VoltageForErase);
}if (pFlash.NbSectorsToErase != 0U) { ... }
else
{
pFlash.Sector = addresstmp = 0xFFFFFFFFU;
pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
FLASH_FlushCaches() ;
HAL_FLASH_EndOfOperationCallback(addresstmp);
}else { ... }
}if (pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE) { ... }
else
{
if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
{
FLASH_FlushCaches() ;
HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
}if (pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) { ... }
else
{
HAL_FLASH_EndOfOperationCallback(pFlash.Address);
}else { ... }
pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
}else { ... }
}if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET) { ... }
if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
{
CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_SER | FLASH_CR_SNB | FLASH_MER_BIT));
__HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
__HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
__HAL_UNLOCK(&pFlash);
}if (pFlash.ProcedureOnGoing == FLASH_PROC_NONE) { ... }
}{ ... }
/* ... */
__weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
{
UNUSED(ReturnValue);
/* ... */
}{ ... }
/* ... */
__weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
{
UNUSED(ReturnValue);
/* ... */
}{ ... }
/* ... */
/* ... */
/* ... */
HAL_StatusTypeDef HAL_FLASH_Unlock(void)
{
HAL_StatusTypeDef status = HAL_OK;
if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
{
WRITE_REG(FLASH->KEYR, FLASH_KEY1);
WRITE_REG(FLASH->KEYR, FLASH_KEY2);
if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
{
status = HAL_ERROR;
}if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) { ... }
}if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) { ... }
return status;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_FLASH_Lock(void)
{
FLASH->CR |= FLASH_CR_LOCK;
return HAL_OK;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
{
if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
{
FLASH->OPTKEYR = FLASH_OPT_KEY1;
FLASH->OPTKEYR = FLASH_OPT_KEY2;
}if ((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET) { ... }
else
{
return HAL_ERROR;
}else { ... }
return HAL_OK;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
{
FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
return HAL_OK;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
{
*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
}{ ... }
/* ... */
/* ... */
/* ... */
uint32_t HAL_FLASH_GetError(void)
{
return pFlash.ErrorCode;
}{ ... }
/* ... */
/* ... */
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
{
uint32_t tickstart = 0U;
pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
/* ... */
tickstart = HAL_GetTick();
while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
{
if(Timeout != HAL_MAX_DELAY)
{
if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
{
return HAL_TIMEOUT;
}if ((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) { ... }
}if (Timeout != HAL_MAX_DELAY) { ... }
}while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET) { ... }
if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
{
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
}if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET) { ... }
#if defined(FLASH_SR_RDERR)
if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)/* ... */
#else
if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)/* ... */
#endif
{
FLASH_SetErrorCode();
return HAL_ERROR;
...}
return HAL_OK;
}{ ... }
/* ... */
static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
{
assert_param(IS_FLASH_ADDRESS(Address));
CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
FLASH->CR |= FLASH_CR_PG;
*(__IO uint32_t*)Address = (uint32_t)Data;
/* ... */
__ISB();
*(__IO uint32_t*)(Address+4) = (uint32_t)(Data >> 32);
}{ ... }
/* ... */
static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
{
assert_param(IS_FLASH_ADDRESS(Address));
CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
FLASH->CR |= FLASH_PSIZE_WORD;
FLASH->CR |= FLASH_CR_PG;
*(__IO uint32_t*)Address = Data;
}{ ... }
/* ... */
static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
{
assert_param(IS_FLASH_ADDRESS(Address));
CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
FLASH->CR |= FLASH_PSIZE_HALF_WORD;
FLASH->CR |= FLASH_CR_PG;
*(__IO uint16_t*)Address = Data;
}{ ... }
/* ... */
static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
{
assert_param(IS_FLASH_ADDRESS(Address));
CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
FLASH->CR |= FLASH_PSIZE_BYTE;
FLASH->CR |= FLASH_CR_PG;
*(__IO uint8_t*)Address = Data;
}{ ... }
/* ... */
static void FLASH_SetErrorCode(void)
{
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR);
}if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET) { ... }
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
}if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET) { ... }
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR);
}if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET) { ... }
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR);
}if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR) != RESET) { ... }
#if defined(FLASH_SR_RDERR)
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_RDERR);
}if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET) { ... }
/* ... */#endif
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR);
}if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET) { ... }
}{ ... }
/* ... */
/* ... */
#endif
/* ... */
/* ... */