-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootutils.pas
295 lines (266 loc) · 7.29 KB
/
bootutils.pas
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
unit bootutils;
{$inline on}
interface
const
SIGNATURE_0 = $1E;
{$if defined(FPC_MCU_ATmega8) or defined(FPC_MCU_ATmega8A) or defined(FPC_MCU_ATmega88) or defined(FPC_MCU_ATmega88A) or defined(FPC_MCU_ATmega88P) or defined(FPC_MCU_ATmega88PA) or defined(FPC_MCU_ATmega88PB)}
RAMSTART = $100;
NRWWSTART = $1800;
SIGNATURE_1 = $93;
{$if defined(FPC_MCU_ATmega8) or defined(FPC_MCU_ATmega8A)}
SIGNATURE_2 = $07;
{$elseif defined(FPC_MCU_ATmega88) or defined(FPC_MCU_ATmega88A)}
SIGNATURE_2 = $0A;
{$elseif defined(FPC_MCU_ATmega88PA)}
SIGNATURE_2 = $0F;
{$elseif defined(FPC_MCU_ATmega88PB)}
SIGNATURE_2 = $16;
{$endif}
{$elseif defined(FPC_MCU_ATmega168) or defined(FPC_MCU_ATmega168A) or defined(FPC_MCU_ATmega168P) or defined(FPC_MCU_ATmega168PA) or defined(FPC_MCU_ATmega168PB)}
RAMSTART = $100;
NRWWSTART = $3800;
SIGNATURE_1 = $94;
{$if defined(FPC_MCU_ATmega168) or defined(FPC_MCU_ATmega168A)}
SIGNATURE_2 = $06;
{$elseif defined(FPC_MCU_ATmega168PA)}
SIGNATURE_2 = $0B;
{$elseif defined(FPC_MCU_ATmega168PB)}
SIGNATURE_2 = $15;
{$endif}
{$elseif defined(FPC_MCU_ATmega328) or defined(FPC_MCU_ATmega328P) or defined(FPC_MCU_ATmega328PB)}
RAMSTART = $100;
NRWWSTART = $7000;
SIGNATURE_1 = $95;
{$if defined(FPC_MCU_ATmega328)}
SIGNATURE_2 = $14;
{$elseif defined(FPC_MCU_ATmega328P)}
SIGNATURE_2 = $0F;
{$elseif defined(FPC_MCU_ATmega328PB)}
SIGNATURE_2 = $16;
{$endif}
{$elseif defined (FPC_MCU_ATmega644) or defined (FPC_MCU_ATmega644P) or defined (FPC_MCU_ATmega644PA) or defined (FPC_MCU_ATmega644PB)}
RAMSTART = $100;
NRWWSTART = $E000;
SIGNATURE_1 = $96;
{$if defined(FPC_MCU_ATmega644) or defined(FPC_MCU_ATmega644A)}
SIGNATURE_2 = $09;
{$elseif defined(FPC_MCU_ATmega644PA)}
SIGNATURE_2 = $0A;
{$endif}
{$elseif defined(FPC_MCU_ATmega1280) or defined(FPC_MCU_ATmega1281)}
RAMSTART = $200;
NRWWSTART = $1E000;
SIGNATURE_1 = $97;
{$if defined(FPC_MCU_ATmega1280)}
SIGNATURE_2 = $03;
{$elseif defined(FPC_MCU_ATmega1281)}
SIGNATURE_2 = $04;
{$endif}
{$elseif defined(FPC_MCU_ATmega2560) or defined(FPC_MCU_ATmega1561)}
RAMSTART = $200;
NRWWSTART = $3E000;
SIGNATURE_1 = $98;
{$if defined(FPC_MCU_ATmega2560)}
SIGNATURE_2 = $01;
{$elseif defined(FPC_MCU_ATmega2561)}
SIGNATURE_2 = $02;
{$endif}
{$elseif defined(FPC_MCU_ATtiny84) or defined(FPC_MCU_ATtiny84A)}
RAMSTART = $100;
NRWWSTART = $0000;
{$endif}
// Z register values to read signature/calibration info
deviceSignature1_Z = 0;
deviceSignature2_Z = 2;
deviceSignature3_Z = 4;
deviceOscCal_Z = 1;
// Z register values to read fuse/lockbits info
deviceFuseLow_Z = 0;
deviceFuseHigh_Z = 3;
deviceFuseExt_Z = 2;
deviceLockbits_Z = 1;
{$if defined(CPUAVR6) or defined(CPUAVR51)}
flashPageSize = 256;
{$elseif defined(CPUAVR5)}
{$if (FPC_FLASHSIZE > 32768) or defined(FPC_MCU_AT90CAN32) or defined(FPC_MCU_AT90CAN64)}
flashPageSize = 256;
{$else}
flashPageSize = 128;
{$endif}
{$elseif defined(CPUAVR4)}
{$if defined(FPC_MCU_ATA6285) or defined(FPC_MCU_ATA6286)}
flashPageSize = 128;
{$else}
flashPageSize = 64;
{$endif}
{$elseif defined(CPUAVR35)}
{$if defined(FPC_MCU_ATTINY1634)}
flashPageSize = 32;
{$else}
flashPageSize = 128;
{$endif}
{$elseif defined(CPUAVR25)}
{$if defined(FPC_MCU_ATTINY87)}
flashPageSize = 128;
{$elseif FPC_FLASHSIZE >= 4096}
flashPageSize = 64;
{$else}
flashPageSize = 32;
{$endif}
{$endif}
type
TEEPROMAddress = {$if FPC_EEPROMSIZE > 256}uint16{$else}byte{$endif};
procedure spm_busy_wait; inline;
procedure eeprom_busy_wait; inline;
{$if not declared(SIGNATURE_2) or not defined(arduino)}
// Only read signatures if not predefined
function readSignatureCalibrationByte(const index: byte): byte;
{$endif}
function readFuseLockBits(const index: byte): byte;
procedure writeLockBits(const lockBits: byte);
{$if declared(RWWSRE)}
procedure enableRWW;
{$endif}
// Byte sized address
// For >64K flash size RAMPZ should be preconfigured,
// thus address is the low 16 bits of the address
function flashReadByte(const addr: uint16): byte;
procedure flashPageErase(const address: uint16);
procedure flashPageFill(const address, data: uint16);
procedure flashPageWrite(const address: uint16);
{$ifndef arduino}
function EEPROMReadByte(const addr: TEEPROMAddress): byte;
procedure EEPROMWriteByte(const addr: TEEPROMAddress; const data: byte);
{$endif}
implementation
const
// Mapping between different naming conventions
SPMenable = {$if declared(SPMEN)}SPMEN{$elseif declared(SELFPRGEN)}SELFPRGEN{$else}0{$endif};
{$I bootutilsconsts.inc}
procedure spm_busy_wait; inline;
begin
repeat
until (xSPMCSR and (1 shl SPMenable)) = 0;
end;
procedure eeprom_busy_wait; inline;
begin
repeat
until (EECR and (1 shl xEEPE)) = 0;
end;
{$if not declared(SIGNATURE_2) or not defined(arduino)}
function readSignatureCalibrationByte(const index: byte): byte; assembler; nostackframe;
const
SIGRD = 5;
SigReadSPM = (1 shl SIGRD) or (1 shl SPMenable);
asm
ldi r31, 0
mov r30, r24
ldi r24, SigReadSPM
out xSPMCSR+(-32), r24
lpm r24, Z
end;
{$endif}
function readFuseLockBits(const index: byte): byte; assembler; nostackframe;
const
{$if declared(RFLB)}
{$define BLBSET:=RFLB}
{$endif}
BLBReadSPM = (1 shl BLBSET) or (1 shl SPMenable);
asm
mov r30, r24
ldi r31, 0
ldi r24, BLBReadSPM
out xSPMCSR+(-32), r24
lpm r24, Z
end;
procedure writeLockBits(const lockBits: byte); assembler; nostackframe;
const
BLBWriteSPM = (1 shl BLBSET) or (1 shl SPMenable);
asm
mov r0, r24
ldi r24, BLBWriteSPM
out xSPMCSR+(-32), r24
spm
end;
{$if declared(RWWSRE)}
procedure enableRWW; assembler; nostackframe;
const
RWWEnableSPM = (1 shl RWWSRE) or (1 shl SPMenable);
asm
ldi r24, RWWEnableSPM
out xSPMCSR+(-32), r24
spm
end;
{$endif}
function flashReadByte(const addr: uint16): byte; assembler; nostackframe;
asm
movw r30, r24
{$ifdef CPUAVR_HAS_ELPM}
elpm r24, Z
{$else}
lpm r24, Z
{$endif CPUAVR_HAS_ELPM}
end;
procedure flashPageErase(const address: uint16); assembler; nostackframe;
const
{$if declared(CTPB)}
{$define PGERS:=CTPB}
{$endif}
pageEraseSPM = (1 shl PGERS) or (1 shl SPMenable);
asm
movw r30, r24
ldi r24, pageEraseSPM
out xSPMCSR+(-32), r24
spm
end;
procedure flashPageFill(const address, data: uint16); assembler; nostackframe;
const
pageFillSPM = (1 shl SPMenable);
asm
movw r0, r22
movw r30, r24
ldi r24, pageFillSPM
out xSPMCSR+(-32), r24
spm
clr r1
end;
procedure flashPageWrite(const address: uint16); assembler; nostackframe;
const
pageWriteSPM = (1 shl PGWRT) or (1 shl SPMenable);
asm
movw r30, r24
ldi r24, pageWriteSPM
out xSPMCSR+(-32), r24
spm
end;
{$ifndef arduino}
// TODO: Perhaps also change addr parameter to byte size if EEAR is not declared.
function EEPROMReadByte(const addr: TEEPROMAddress): byte;
begin
eeprom_busy_wait;
{$if FPC_EEPROMSIZE > 256}
EEAR := addr;
{$elseif declared(EEARL)}
EEARL := addr;
{$else}
EEAR := addr;
{$endif}
EECR := (1 shl EERE);
Result := EEDR;
end;
procedure EEPROMWriteByte(const addr: TEEPROMAddress; const data: byte);
begin
eeprom_busy_wait;
{$if FPC_EEPROMSIZE > 256}
EEAR := addr;
{$elseif declared(EEARL)}
EEARL := addr;
{$else}
EEAR := addr;
{$endif}
EEDR := data;
EECR := (1 shl xEEMPE);
EECR := (1 shl xEEPE);
end;
{$endif}
end.