-
Notifications
You must be signed in to change notification settings - Fork 34
/
eeprom.c
88 lines (72 loc) · 2.1 KB
/
eeprom.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
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
/* Copyright (C) 2015 Baruch Even
*
* This file is part of the B3603 alternative firmware.
*
* B3603 alternative firmware is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* B3603 alternative firmware is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with B3603 alternative firmware. If not, see <http://www.gnu.org/licenses/>.
*/
#include "eeprom.h"
#include "stm8s.h"
static uint8_t eeprom_unlock_data(void)
{
FLASH_DUKR = 0xAE; // Enable the flash data writing
FLASH_DUKR = 0x56;
return (FLASH_IAPSR & FLASH_IAPSR_DUL); // True if device unlocked
}
inline void eeprom_lock(void)
{
FLASH_IAPSR &= ~FLASH_IAPSR_DUL;
}
uint8_t eeprom_set_afr0(void)
{
uint8_t sr;
uint16_t timeout = 0xFFFF;
if (!eeprom_unlock_data())
return 0;
FLASH_CR2 = FLASH_CR2_OPT;// Set the OPT bit
FLASH_NCR2 = ~FLASH_NCR2_NOPT; // Remove the NOPT bit
OPT2 = 1;
NOPT2 = 0xFE;
for (timeout = 0xFFFF; timeout > 0; timeout--) {
sr = FLASH_IAPSR;
if (sr & FLASH_IAPSR_EOP)
break;
}
if (sr & FLASH_IAPSR_WR_PG_DIS)
timeout = 0; // This will report failure
FLASH_CR2 &= FLASH_CR2_OPT;
FLASH_NCR2 |= FLASH_NCR2_NOPT;
eeprom_lock();
return (timeout > 0) && (OPT2 & 1);
}
uint8_t eeprom_save_data(uint8_t *dst, uint8_t *src, uint8_t len)
{
uint16_t timeout;
uint8_t sr;
if (!eeprom_unlock_data())
return 0;
for (; len > 0; len--, dst++, src++) {
*dst = *src;
IWDG_KR = 0xAA; // Reset the counter
}
for (timeout = 0xFFFF; timeout > 0; timeout--) {
IWDG_KR = 0xAA; // Reset the counter
sr = FLASH_IAPSR;
if (sr & FLASH_IAPSR_EOP)
break;
}
if (sr & FLASH_IAPSR_WR_PG_DIS)
timeout = 0; // This will report failure
eeprom_lock();
return (timeout > 0);
}