-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnuvorom.ino
More file actions
148 lines (126 loc) · 3.6 KB
/
Copy pathnuvorom.ino
File metadata and controls
148 lines (126 loc) · 3.6 KB
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
//
// Nuvoton LDROM (bootloader) init
// (for N76E003 or similar)
//
#include "nuvoicp.h"
#include "ldrom.h"
constexpr bool may_unlock = true;
constexpr uint8_t DAT = 5, CLK = 6, RST = 9;
inline void print_begin() { Serial.begin(9600); }
inline void print_end() { Serial.end(); }
inline void print(const char str[]) { Serial.print(str); }
inline void print(char ch) { Serial.write(ch); }
inline void print(unsigned long num, int base) { Serial.print(num, base); }
inline void println(const char str[]) { Serial.println(str); }
inline void println(unsigned long num, int base) { Serial.println(num, base); }
bool enter_icp();
bool update_config(const uint8_t config[5]);
bool update_ldrom(const uint8_t rom[] PROGMEM, size_t length);
void loop() { /* nothing */ }
NuvotonICP icp(DAT, CLK, RST);
void setup()
{
print_begin();
icp.pin_init(LED_BUILTIN, HIGH);
print("Nuvoton device ");
if (enter_icp()) {
println(icp.device_id(), HEX); // Device ID
print("Flash ROM ");
print(icp.size() / 1024, DEC);
println("K");
print("LDROM "); // LDROM update
if (update_ldrom(ldrom, sizeof(ldrom))) {
println("OK");
static const uint8_t config[5] = {
icp.config0(false),
icp.config1(sizeof(ldrom)),
UINT8_MAX, UINT8_MAX, UINT8_MAX,
};
print("CONFIG "); // CONFIG update
if (update_config(config)) {
println("OK");
icp.pin_float(LED_BUILTIN);
} else
println("Fail");
} else
println("Fail");
} else
println("None");
icp.exit();
print_end();
}
bool enter_icp()
{
uint8_t company_id = icp.enter();
if (company_id == UINT8_MAX && icp.locked()) {
print("locked ");
if (!may_unlock)
return false;
// try unlock
icp.mass_erase();
print("erased ");
icp.exit();
delay(10);
company_id = icp.enter();
}
return (company_id == NuvotonICP::NUVOTON);
}
bool update_config(const uint8_t config[5])
{
uint8_t result = icp.config_compare(config);
if (icp.equal(result))
return true; // nothing to do
// erase
if (icp.need_erase(result)) {
icp.config_erase();
print('x');
}
// write
if (icp.need_write(result)) {
icp.config_write(config);
print('.');
}
// verify
return icp.config_verify(config);
}
bool update_ldrom(const uint8_t rom[] PROGMEM, size_t length)
{
// check ldrom size
const uint16_t ldsize = icp.ldsize(length);
if (ldsize == 0 || ldsize > 4096)
return false;
const uint16_t psize = icp.psize();
const uint8_t incomplete_page = length / psize;
const uint8_t empty_page = incomplete_page + !!(length % psize);
uint8_t data[icp.psize_max()], page = 0;
for (uint16_t offset = 0; offset < ldsize; offset += psize, ++page) {
if (page < incomplete_page) {
// complete page
memcpy_P(data, &rom[offset], psize);
} else if (page < empty_page) {
// incomplete non-empty page
uint16_t leftover = length - offset;
memcpy_P(data, &rom[offset], leftover);
memset(&data[leftover], UINT8_MAX, psize - leftover);
}
uint16_t address = icp.size() - ldsize + offset;
uint8_t* p_data = (page < empty_page) ? data : nullptr;
uint8_t result = icp.flash_compare(address, p_data, psize);
if (icp.equal(result))
continue; // nothing to do
// erase
if (icp.need_erase(result)) {
icp.flash_erase(address);
print('x');
}
// write
if (icp.need_write(result)) {
icp.flash_write(address, p_data, psize);
print('.');
}
// verify
if (!icp.flash_verify(address, p_data, psize))
return false;
}
return true;
}