-
Notifications
You must be signed in to change notification settings - Fork 5
/
flash.c
48 lines (38 loc) · 985 Bytes
/
flash.c
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
#include "flash.h"
void FLASH_unlockProg(void) {
FLASH->PUKR = FLASH_KEY1;
FLASH->PUKR = FLASH_KEY2;
while(!(FLASH->IAPSR & FLASH_IAPSR_PUL));
}
void FLASH_lockProg(void) {
FLASH->IAPSR &= FLASH_IAPSR_PUL;
}
void FLASH_unlockData(void) {
FLASH->DUKR = FLASH_KEY2;
FLASH->DUKR = FLASH_KEY1;
while(!(FLASH->IAPSR & FLASH_IAPSR_DUL));
// FIXME without this dummy delay first 4 bytes of EEPROM are written as zero sometimes
{
volatile uint16_t i;
for(i = 0; i < 10000; ++i) {}
}
}
void FLASH_waitData(void) {
while(!(FLASH->IAPSR & FLASH_IAPSR_EOP));
}
void FLASH_lockData(void) {
FLASH->IAPSR &= FLASH_IAPSR_DUL;
}
void FLASH_waitOpt(void) {
FLASH_waitData();
}
void FLASH_unlockOpt(void) {
FLASH_unlockData();
FLASH->CR2 |= FLASH_CR2_OPT;
FLASH->NCR2 &= ~FLASH_NCR2_NOPT;
}
void FLASH_lockOpt(void) {
FLASH->CR2 &= ~FLASH_CR2_OPT;
FLASH->NCR2 |= FLASH_NCR2_NOPT;
FLASH_lockData();
}