-
Notifications
You must be signed in to change notification settings - Fork 1
/
pasboot.lpr
477 lines (427 loc) · 12.8 KB
/
pasboot.lpr
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
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
348
349
350
351
352
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
program pasboot;
{ By default the bootloader supports the "stk500v1", "avrisp" and "arduino" programmer types
of avrdude. This supports many of the commands according to document AVR061.
By defining "arduino", a smaller subset of AVR061 commands are supported, similar
to the optiboot project. This results in a smaller bootloader. This mode only
supports the avrdude programmer type "arduino".
}
{$inline on}
uses
stk500, intrinsics, bootutils, uart, delay;
const
lockBitMask = $C0; // Undefined bits should be written as 1 for future compatibility
{$ifdef FPC_MCU_ATMEGA2560}
LEDpin = 7;
{$else}
LEDpin = 5;
{$endif}
flashEnd = FPC_FLASHSIZE;
type
TWordRecord = packed record
case boolean of
false: (lsb, msb: byte);
true : (val: word);
end;
var
// Note: if no RTL startup code is used (compiler version 3.3.1),
// all variables will be uninitialized so ensure all variables are assigned
// before use.
b: byte;
buffer: array [0..flashPageSize-1] of byte;
address, size, i: word;
LEDport: byte absolute PORTB;
LEDDDR: byte absolute DDRB;
// Information not used currently
deviceParams: TDeviceParameters;
deviceParamsEx: TDeviceParametersEx;
{$ifdef VER3_3_1}
pascalmain: record end; external name 'PASCALMAIN';
// Simplified startup code without data initialization
procedure init0; assembler; nostackframe; noreturn; section '.init.0';
const
initSP = FPC_SRAMBASE + FPC_SRAMSIZE-1;
SPL_ = byte(@SPL) - $20;
SPH_ = byte(@SPH) - $20;
asm
// Clear zero register
clr r1
// Set stack pointer
ldi r18, hi8(initSP)
out SPH_, r18
ldi r18, lo8(initSP)
out SPL_, r18
{$ifdef CPUAVR_HAS_JMP_CALL}jmp{$else}rjmp{$endif} pascalmain
end;
{$endif}
procedure uart_transmit_buffer(data: PByte; len: byte);
begin
while len > 0 do
begin
uart_transmit(data^);
inc(data);
dec(len);
end;
end;
procedure uart_receive_buffer(data: PByte; len: byte);
begin
while len > 0 do
begin
data^ := uart_receive;
inc(data);
dec(len);
end;
end;
procedure checkAndReply;
var
c: byte;
begin
c := uart_receive;
if c <> Sync_CRC_EOP then
uart_transmit(c)
else
begin
uart_transmit(Resp_STK_INSYNC);
uart_transmit(Resp_STK_OK);
end;
end;
procedure checkAndReplyByte(const b: byte);
begin
if uart_receive = Sync_CRC_EOP then
begin
uart_transmit(Resp_STK_INSYNC);
uart_transmit(b);
uart_transmit(Resp_STK_OK);
end
else
uart_transmit(Resp_STK_NOSYNC);
end;
{$I bootutilsconsts.inc}
{$if declared(WDTOE)}
const
WDCE = WDTOE; // atmega16 seems to be the exception
{$endif}
{$if not defined(CPUAVR_HAS_JMP_CALL)}
// Remember to define rebootstart symbol:
// -k "--defsym rebootstart=0"
procedure rebootStart; external name 'rebootstart';
{$endif}
begin
b := xMCUSR;
xMCUSR := 0;
// Disable watchdog
avr_cli;
avr_wdr;
// Clear Disable watchdog
xMCUSR := xMCUSR and not(1 shl WDRF);
xWDTCSR := xWDTCSR or ((1 shl WDCE) or (1 shl WDE));
xWDTCSR := 0;
LEDDDR := LEDDDR or (1 shl LEDpin);
{ Read reset cause
If reset cause = 0 it means application code ran into bootloader, so probably
no valid application, start bootloader anyway.
If not external reset, start application.
TODO: Check if there is valid code at application start before jumping there.
Currently bootloader gets started anyway, just a little later...
}
if (b > 0) and ((b and (1 shl EXTRF)) = 0) then
begin
// Brief LED flash
LEDport := LEDport or (1 shl LEDpin);
delay_ms(100);
LEDport := LEDport and not (1 shl LEDpin);
LEDDDR := LEDDDR and not (1 shl LEDpin);
asm
{$ifdef CPUAVR_HAS_JMP_CALL}
jmp 0
{$else}
rjmp rebootstart
{$endif}
end;
end;
avr_cli;
avr_wdr;
// Set watchdog to 2s timeout
// this enables the watchdog interrupt which is used to start the main application
xWDTCSR := (1 shl WDCE) or (1 shl WDE);
xWDTCSR := (1 shl WDE) or 7; // 2s timeout
SREG := 0;
LEDport := LEDport or (1 shl LEDpin);
delay_ms(400);
LEDport := LEDport and not (1 shl LEDpin);
delay_ms(200);
LEDport := LEDport or (1 shl LEDpin);
delay_ms(100);
LEDport := LEDport and not (1 shl LEDpin);
uart_init;
repeat
// Reset watchdog
avr_wdr;
case uart_receive of
Sync_CRC_EOP: uart_transmit(Resp_STK_NOSYNC);
//Cmnd_STK_GET_SYNC:
{$ifndef arduino}
Cmnd_STK_GET_SIGN_ON:
begin
if uart_receive = Sync_CRC_EOP then
begin
uart_transmit(Resp_STK_INSYNC);
uart_transmit(ord('A'));
uart_transmit(ord('V'));
uart_transmit(ord('R'));
uart_transmit(ord(' '));
uart_transmit(ord('S'));
uart_transmit(ord('T'));
uart_transmit(ord('K'));
uart_transmit(Resp_STK_OK);
end
else
uart_transmit(Resp_STK_NOSYNC);
end;
{$endif}
{$ifndef arduino}
Cmnd_STK_SET_PARAMETER:
begin
b := uart_receive;
uart_receive;
if uart_receive = Sync_CRC_EOP then
begin
uart_transmit(Resp_STK_INSYNC);
uart_transmit(b);
uart_transmit(Resp_STK_FAILED); // indicate not supported
end
else
uart_transmit(Resp_STK_NOSYNC);
end;
{$endif}
Cmnd_STK_GET_PARAMETER:
begin
case uart_receive of
Parm_STK_SW_MAJOR: checkAndReplyByte(1);
Parm_STK_SW_MINOR: checkAndReplyByte(18);
else
checkAndReplyByte(3);
end;
end;
Cmnd_STK_SET_DEVICE:
begin
uart_receive_buffer(@deviceParams.bytes[0], SizeOf(deviceParams));
checkAndReply;
end;
Cmnd_STK_SET_DEVICE_EXT:
begin
uart_receive_buffer(@deviceParamsEx.bytes[0], SizeOf(deviceParamsEx));
checkAndReply;
end;
//Cmnd_STK_ENTER_PROGMODE: nothing specific to do
Cmnd_STK_LEAVE_PROGMODE:
begin
// Set watchdog to shortest timeout (16ms)
xWDTCSR := (1 shl WDCE) or (1 shl WDE);
xWDTCSR := (1 shl WDE) or 0;
checkAndReply;
end;
{$ifndef arduino}
Cmnd_STK_CHIP_ERASE:
begin
if uart_receive = Sync_CRC_EOP then
begin
uart_transmit(Resp_STK_INSYNC);
uart_transmit(Resp_STK_FAILED); // indicate not supported
end
else
uart_transmit(Resp_STK_NOSYNC);
end;
{$endif}
//Cmnd_STK_CHECK_AUTOINC
//Cmnd_STK_CHECK_DEVICE
Cmnd_STK_LOAD_ADDRESS:
begin
// Address in LE format
uart_receive_buffer(@address, 2);
{$if declared(RAMPZ)}
if (address and $8000) = 0 then
RAMPZ := RAMPZ and $FE
else
RAMPZ := RAMPZ or 1;
{$endif}
// Convert from word to byte address
address := address shl 1;
checkAndReply;
end;
Cmnd_STK_UNIVERSAL:
begin
uart_receive_buffer(@buffer[0], 5);
if buffer[4] = Sync_CRC_EOP then
begin
uart_transmit(Resp_STK_INSYNC);
{$ifndef arduino}
if buffer[0] = $30 then // Read signature byte
begin
case buffer[2] of
// We can only read the signature with the AVRs that have SIGRD bit in SPMCR.
// For all others we use predefined signaures like AVR-GCC does.
{$if declared(SIGNATURE_2)}
0: uart_transmit(SIGNATURE_0);
1: uart_transmit(SIGNATURE_1);
2: uart_transmit(SIGNATURE_2);
{$else}
0: uart_transmit(readSignatureCalibrationByte(deviceSignature1_Z));
1: uart_transmit(readSignatureCalibrationByte(deviceSignature2_Z));
2: uart_transmit(readSignatureCalibrationByte(deviceSignature3_Z));
{$endif}
otherwise
uart_transmit(0);
end;
end
else
{$endif}
{$if declared(RAMPZ)}
// Handle extended address byte
if buffer[0] = $4D then
begin
RAMPZ := (RAMPZ and 1) or (buffer[2] shl 1); // convert from word address to byte address
uart_transmit(0);
end{$ifndef arduino} else {$else};{$endif}
{$endif declared(RAMPZ)}
{$ifndef arduino}
// Support for fuse bits
if buffer[0] = $50 then
begin
case buffer[1] of
0: uart_transmit(readFuseLockBits(deviceFuseLow_Z));
8: uart_transmit(readFuseLockBits(deviceFuseExt_Z));
otherwise
uart_transmit(0);
end;
end
else if (buffer[0] = $58) and (buffer[1] = 8) then
uart_transmit(readFuseLockBits(deviceFuseHigh_Z))
else if (buffer[0] = $AC) and (buffer[1] = $80) then
begin
//eraseChip; Erasure of all memory not supported. Memory is erased just before being written n page or byte level.
// Note: this implies that old data cannot be deleted unless it is overwritten by new data.
uart_transmit(0);
end
else if (buffer[0] = $58) and (buffer[1] = 0) then
uart_transmit(readFuseLockBits(deviceLockbits_Z))
else if (buffer[0] = $AC) and (buffer[1] = $E0) then
begin
writeLockBits(buffer[3] or lockBitMask);
uart_transmit(buffer[3]);
end
else if (buffer[0] = $38) and (buffer[1] = $00) then
uart_transmit(readSignatureCalibrationByte(deviceOscCal_Z))
else
uart_transmit(0); // dummy reply
{$else arduino}
uart_transmit(0); // dummy reply
{$endif ndef arduino}
uart_transmit(Resp_STK_OK);
end
else
uart_transmit(Resp_STK_NOSYNC);
end;
//Cmnd_STK_PROG_FLASH
//Cmnd_STK_PROG_DATA
//Cmnd_STK_PROG_FUSE
Cmnd_STK_PROG_PAGE:
begin
// Size is transmitted in BE format
TWordRecord(size).msb := uart_receive;
TWordRecord(size).lsb := uart_receive;
b := uart_receive;
// If page size > 128 (i.e. 256),
// break read into 2
{$if flashPageSize > 128}
uart_receive_buffer(@buffer[0], 128);
uart_receive_buffer(@buffer[128], 128);
{$else flashPageSize <= 128}
uart_receive_buffer(@buffer[0], size);
{$endif}
begin
if char(b) = 'F' then
begin
flashPageErase(address);
spm_busy_wait;
i := 0;
while i < size do
begin
flashPageFill(address + i, buffer[i] + (word(buffer[i + 1]) shl 8));
inc(i, 2);
end;
flashPageWrite(address);
spm_busy_wait;
{$if declared(RWWSRE)}
enableRWW;
{$endif}
end
else // program EEPROM
begin
{$ifndef arduino}
for i := 0 to size-1 do
EEPROMWriteByte(address + i, buffer[i]);
{$endif}
end;
checkAndReply;
end
end;
//Cmnd_STK_PROG_FUSE_EXT
//Cmnd_STK_READ_FLASH
//Cmnd_STK_READ_DATA
//Cmnd_STK_READ_FUSE
//Cmnd_STK_READ_LOCK
Cmnd_STK_READ_PAGE:
begin
// Size is transmitted in BE format
TWordRecord(size).msb := uart_receive;
TWordRecord(size).lsb := uart_receive;
b := uart_receive;
if uart_receive = Sync_CRC_EOP then
begin
uart_transmit(Resp_STK_INSYNC);
if char(b) = 'F' then
begin
for i := 0 to size-1 do
uart_transmit(flashReadByte(address + i));
end
else // read EEPROM
begin
{$ifndef arduino}
for i := 0 to size-1 do
uart_transmit(EEPROMReadByte(address + i));
{$endif}
end;
uart_transmit(Resp_STK_OK);
end
else
uart_transmit(Resp_STK_NOSYNC);
end;
Cmnd_STK_READ_SIGN:
begin
if uart_receive = Sync_CRC_EOP then
begin
uart_transmit(Resp_STK_INSYNC);
{$if declared(SIGNATURE_2)}
// Return predefined signatures
uart_transmit(SIGNATURE_0);
uart_transmit(SIGNATURE_1);
uart_transmit(SIGNATURE_2);
{$else}
// Read chip signatures
uart_transmit(readSignatureCalibrationByte(deviceSignature1_Z));
uart_transmit(readSignatureCalibrationByte(deviceSignature2_Z));
uart_transmit(readSignatureCalibrationByte(deviceSignature3_Z));
{$endif}
uart_transmit(Resp_STK_OK);
end
else
uart_transmit(Resp_STK_NOSYNC);
end;
//Cmnd_STK_READ_OSCCAL
//Cmnd_STK_READ_FUSE_EXT
//Cmnd_STK_READ_OSCCAL_EXT
otherwise
checkAndReply;
end;
until false;
end.