forked from alexforencich/xboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xboot.c
1587 lines (1380 loc) · 56.7 KB
/
xboot.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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************/
/* XBoot Extensible AVR Bootloader */
/* */
/* tested with ATXMEGA64A3, ATXMEGA128A1, ATXMEGA256A1, ATXMEGA32A4 */
/* */
/* xboot.c */
/* */
/* Alex Forencich <[email protected]> */
/* */
/* Copyright (c) 2010 Alex Forencich */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files(the "Software"), to deal in the Software without restriction, */
/* including without limitation the rights to use, copy, modify, merge, */
/* publish, distribute, sublicense, and/or sell copies of the Software, */
/* and to permit persons to whom the Software is furnished to do so, */
/* subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */
/* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */
/* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */
/* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/* */
/************************************************************************/
#include "xboot.h"
#ifdef USE_INTERRUPTS
volatile unsigned char comm_mode;
volatile unsigned char rx_buff0;
volatile unsigned char rx_buff1;
volatile unsigned char rx_char_cnt;
volatile unsigned char tx_buff0;
volatile unsigned char tx_char_cnt;
#else
#ifdef __AVR_XMEGA__
unsigned char comm_mode;
#else // __AVR_XMEGA__
// Force data section on atmega
// Seems to be a bug in newer versions of gcc
// this ensures .bss is placed after .data
unsigned char comm_mode = 1;
#endif // __AVR_XMEGA__
#endif // USE_INTERRUPTS
unsigned char buffer[SPM_PAGESIZE];
#ifdef NEED_CODE_PROTECTION
unsigned char protected;
#endif // NEED_CODE_PROTECTION
// Main code
int main(void)
{
ADDR_T address = 0;
unsigned char in_bootloader = 0;
unsigned char val = 0;
int i = 0;
uint32_t j;
uint8_t k;
#ifdef NEED_CODE_PROTECTION
protected = 1;
#endif // NEED_CODE_PROTECTION
#ifdef USE_I2C_ADDRESS_NEGOTIATION
unsigned short devid_bit;
#endif // USE_I2C_ADDRESS_NEGOTIATION
comm_mode = MODE_UNDEF;
#ifdef USE_INTERRUPTS
rx_char_cnt = 0;
tx_char_cnt = 0;
#endif // USE_INTERRUPTS
// Initialization section
// Entry point and communication methods are initialized here
// --------------------------------------------------
#ifdef __AVR_XMEGA__
#ifdef USE_32MHZ_RC
#if (F_CPU != 32000000L)
#error F_CPU must match oscillator setting!
#endif // F_CPU
OSC.CTRL |= OSC_RC32MEN_bm; // turn on 32 MHz oscillator
while (!(OSC.STATUS & OSC_RC32MRDY_bm)) { }; // wait for it to start
CCP = CCP_IOREG_gc;
CLK.CTRL = CLK_SCLKSEL_RC32M_gc;
#ifdef USE_DFLL
OSC.CTRL |= OSC_RC32KEN_bm;
while(!(OSC.STATUS & OSC_RC32KRDY_bm));
DFLLRC32M.CTRL = DFLL_ENABLE_bm;
#endif // USE_DFLL
#else // USE_32MHZ_RC
#if (F_CPU != 2000000L)
#error F_CPU must match oscillator setting!
#endif // F_CPU
#ifdef USE_DFLL
OSC.CTRL |= OSC_RC32KEN_bm;
while(!(OSC.STATUS & OSC_RC32KRDY_bm));
DFLLRC2M.CTRL = DFLL_ENABLE_bm;
#endif // USE_DFLL
#endif // USE_32MHZ_RC
#else // __AVR_XMEGA__
// nothing special for ATmega
#endif // __AVR_XMEGA__
// interrupts
#ifdef __AVR_XMEGA__
#ifdef NEED_INTERRUPTS
// remap interrupts to boot section
CCP = CCP_IOREG_gc;
#ifdef USE_INTERRUPTS
PMIC.CTRL = PMIC_IVSEL_bm | PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm;
#else
PMIC.CTRL = PMIC_IVSEL_bm;
#endif // USE_INTERRUPTS
#endif // NEED_INTERRUPTS
#else // __AVR_XMEGA__
// nothing special for ATmega
#endif // __AVR_XMEGA__
// LED
#ifdef __AVR_XMEGA__
#ifdef USE_LED
// Initialize LED pin
LED_PORT.DIRSET = (1 << LED_PIN);
#if LED_PIN_INV
LED_PORT.OUTCLR = (1 << LED_PIN);
#else
LED_PORT.OUTSET = (1 << LED_PIN);
#endif // LED_PIN_INV
#endif // USE_LED
#else // __AVR_XMEGA__
#ifdef USE_LED
// Initialize LED pin
LED_PORT_DDR |= (1 << LED_PIN);
#if LED_PIN_INV
LED_PORT &= ~(1 << LED_PIN);
#else
LED_PORT |= (1 << LED_PIN);
#endif // LED_PIN_INV
#endif // USE_LED
#endif // __AVR_XMEGA__
// I2C Attach LED_PIN
#ifdef __AVR_XMEGA__
#ifdef USE_I2C_ADDRESS_NEGOTIATION
#ifdef USE_ATTACH_LED
// Initialize ATTACH_LED
ATTACH_LED_PORT.DIRSET = (1 << ATTACH_LED_PIN);
#if ATTACH_LED_INV
ATTACH_LED_PORT.OUTSET = (1 << ATTACH_LED_PIN);
#else
ATTACH_LED_PORT.OUTCLR = (1 << ATTACH_LED_PIN);
#endif // ATTACH_LED_INV
#endif // USE_ATTACH_LED
#endif // USE_I2C_ADDRESS_NEGOTIATION
#else // __AVR_XMEGA__
#ifdef USE_I2C_ADDRESS_NEGOTIATION
#ifdef USE_ATTACH_LED
// Initialize ATTACH_LED
ATTACH_LED_PORT_DDR |= (1 << ATTACH_LED_PIN);
#if ATTACH_LED_INV
ATTACH_LED_PORT |= (1 << ATTACH_LED_PIN);
#else
ATTACH_LED_PORT &= ~(1 << ATTACH_LED_PIN);
#endif // ATTACH_LED_INV
#endif // USE_ATTACH_LED
#endif // USE_I2C_ADDRESS_NEGOTIATION
#endif // __AVR_XMEGA__
// Enter pin
#ifdef __AVR_XMEGA__
#ifdef USE_ENTER_PIN
// Make sure it's an input
ENTER_PORT.DIRCLR = (1 << ENTER_PIN);
#if ENTER_PIN_PUEN
// Enable bootloader entry pin pullup
ENTER_PIN_CTRL = 0x18;
#endif // ENTER_PIN_PUEN
#endif // USE_ENTER_PIN
#else // __AVR_XMEGA__
#ifdef USE_ENTER_PIN
// Make sure it's an input
ENTER_PORT_DDR &= ~(1 << ENTER_PIN);
#if ENTER_PIN_PUEN
// Enable bootloader entry pin pullup
ENTER_PORT |= (1 << ENTER_PIN);
#else // ENER_PIN_PUEN
// Disable bootloader entry pin pullup
ENTER_PORT &= ~(1 << ENTER_PIN);
#endif // ENTER_PIN_PUEN
#endif // USE_ENTER_PIN
#endif // __AVR_XMEGA__
#ifdef USE_UART
// Initialize UART
uart_init();
// Initialize RX pin pull-up
#ifdef __AVR_XMEGA__
#ifdef UART_RX_PUEN
// Enable RX pin pullup
UART_RX_PIN_CTRL = 0x18;
#endif // UART_RX_PUEN
#else // __AVR_XMEGA__
#ifdef UART_RX_PUEN
// Enable RX pin pullup
UART_PORT |= (1 << UART_RX_PIN);
#endif // UART_RX_PUEN
#endif // __AVR_XMEGA__
// Initialize UART EN pin
#ifdef __AVR_XMEGA__
#ifdef USE_UART_EN_PIN
UART_EN_PORT.DIRSET = (1 << UART_EN_PIN);
#if UART_EN_INV
UART_EN_PORT.OUTSET = (1 << UART_EN_PIN);
#else // UART_PIN_INV
UART_EN_PORT.OUTCLR = (1 << UART_EN_PIN);
#endif // UART_PIN_INV
#endif // USE_UART_EN_PIN
#else // __AVR_XMEGA__
#ifdef USE_UART_EN_PIN
UART_EN_PORT_DDR |= (1 << UART_EN_PIN);
#if UART_EN_INV
UART_EN_PORT |= (1 << UART_EN_PIN);
#else // UART_PIN_INV
UART_EN_PORT &= ~(1 << UART_EN_PIN);
#endif // UART_PIN_INV
#endif // USE_UART_EN_PIN
#endif // __AVR_XMEGA__
#endif // USE_UART
#ifdef USE_I2C
// Initialize I2C interface
i2c_init();
#ifdef __AVR_XMEGA__
#ifdef USE_I2C_ADDRESS_NEGOTIATION
I2C_AUTONEG_PORT.DIRCLR = (1 << I2C_AUTONEG_PIN);
I2C_AUTONEG_PORT.OUTCLR = (1 << I2C_AUTONEG_PIN);
#endif // USE_I2C_ADDRESS_NEGOTIATION
#else // __AVR_XMEGA__
#ifdef USE_I2C_ADDRESS_NEGOTIATION
I2C_AUTONEG_PORT_DDR &= ~(1 << I2C_AUTONEG_PIN);
I2C_AUTONEG_PORT &= ~(1 << I2C_AUTONEG_PIN);
#endif // USE_I2C_ADDRESS_NEGOTIATION
#endif // __AVR_XMEGA__
#endif // USE_I2C
#ifdef USE_FIFO
// Initialize FIFO
fifo_init();
#endif // USE_FIFO
#ifndef __AVR_XMEGA__
// ATMEGA must reset via watchdog, so turn it off
MCUSR = 0;
wdt_disable();
#endif
// --------------------------------------------------
// End initialization section
// One time trigger section
// Triggers that are checked once, regardless of
// whether or not USE_ENTER_DELAY is selected
// --------------------------------------------------
// --------------------------------------------------
// End one time trigger section
#ifdef USE_ENTER_DELAY
k = ENTER_BLINK_COUNT*2;
j = ENTER_BLINK_WAIT;
while (!in_bootloader && k > 0)
{
if (j-- <= 0)
{
#ifdef USE_LED
#ifdef __AVR_XMEGA__
LED_PORT.OUTTGL = (1 << LED_PIN);
#else // __AVR_XMEGA__
LED_PORT ^= (1 << LED_PIN);
#endif // __AVR_XMEGA__
#endif // USE_LED
j = ENTER_BLINK_WAIT;
k--;
}
#else // USE_ENTER_DELAY
// Need a small delay when not running loop
// so we don't accidentally enter the bootloader
// on power-up with USE_ENTER_PIN selected
asm("nop");
asm("nop");
asm("nop");
asm("nop");
#endif // USE_ENTER_DELAY
// Main trigger section
// Set in_bootloader here to enter the bootloader
// Checked when USE_ENTER_DELAY is selected
// --------------------------------------------------
#ifdef USE_ENTER_PIN
// Check entry pin state
#ifdef __AVR_XMEGA__
if ((ENTER_PORT.IN & (1 << ENTER_PIN)) == (ENTER_PIN_STATE ? (1 << ENTER_PIN) : 0))
in_bootloader = 1;
#else // __AVR_XMEGA__
if ((ENTER_PORT_PIN & (1 << ENTER_PIN)) == (ENTER_PIN_STATE ? (1 << ENTER_PIN) : 0))
in_bootloader = 1;
#endif // __AVR_XMEGA__
#endif // USE_ENTER_PIN
#ifdef USE_ENTER_UART
// Check for received character
#ifdef ENTER_UART_NEED_SYNC
if (uart_char_received() && (uart_cur_char() == CMD_SYNC))
#else // ENTER_UART_NEED_SYNC
if (uart_char_received())
#endif // ENTER_UART_NEED_SYNC
{
in_bootloader = 1;
comm_mode = MODE_UART;
}
#endif // USE_ENTER_UART
#ifdef USE_ENTER_I2C
// Check for address match condition
if (i2c_address_match())
{
in_bootloader = 1;
comm_mode = MODE_I2C;
}
#endif // USE_ENTER_I2C
#ifdef USE_ENTER_FIFO
// Check for received character
#ifdef ENTER_FIFO_NEED_SYNC
if (fifo_char_received() && (fifo_cur_char() == CMD_SYNC))
#else // ENTER_FIFO_NEED_SYNC
if (fifo_char_received())
#endif // ENTER_FIFO_NEED_SYNC
{
in_bootloader = 1;
comm_mode = MODE_FIFO;
}
#endif // USE_ENTER_FIFO
#ifdef USE_ENTER_EEPROM
if (enter_eeprom_check())
{
enter_eeprom_reset();
in_bootloader = 1;
}
#endif // USE_ENTER_EEPROM
// --------------------------------------------------
// End main trigger section
#ifdef __AVR_XMEGA__
WDT_Reset();
#else // __AVR_XMEGA__
wdt_reset();
#endif // __AVR_XMEGA__
#ifdef USE_ENTER_DELAY
}
#endif // USE_ENTER_DELAY
#ifdef USE_INTERRUPTS
// Enable interrupts
sei();
#endif // USE_INTERRUPTS
#ifdef USE_WATCHDOG
WDT_EnableAndSetTimeout();
#endif // USE_WATCHDOG
// Main bootloader
while (in_bootloader) {
#ifdef USE_LED
#ifdef __AVR_XMEGA__
LED_PORT.OUTTGL = (1 << LED_PIN);
#else // __AVR_XMEGA__
LED_PORT ^= (1 << LED_PIN);
#endif // __AVR_XMEGA__
#endif // USE_LED
val = get_char();
#ifdef USE_WATCHDOG
WDT_Reset();
#endif // USE_WATCHDOG
// Main bootloader parser
// check autoincrement status
if (val == CMD_CHECK_AUTOINCREMENT)
{
// yes, it is supported
send_char(REPLY_YES);
}
// Set address
else if (val == CMD_SET_ADDRESS)
{
// Read address high then low
address = get_2bytes();
// acknowledge
send_char(REPLY_ACK);
}
// Extended address
else if (val == CMD_SET_EXT_ADDRESS)
{
// Read address high then low
//address = ((ADDR_T)get_char() << 16) | get_2bytes();
asm volatile (
"call get_char" "\n\t"
"mov %C0,r24" "\n\t"
"call get_2bytes" "\n\t"
"clr %D0" "\n\t"
: "=r" (address)
:
);
// acknowledge
send_char(REPLY_ACK);
}
// Chip erase
else if (val == CMD_CHIP_ERASE)
{
// Erase the application section
// XMEGA E5: ERASE_APP NVM command (0x20) erases the entire flash - as a workaround, we erase page-by-page.
// From Atmel Support: "The NVM controller design is such that the entire flash will get erased always when application/bootloader erase is called."
#if defined(__AVR_ATxmega8E5__) || defined(__AVR_ATxmega16E5__) || defined(__AVR_ATxmega32E5__)
for(uint32_t addr = APP_SECTION_START; addr < APP_SECTION_END; addr += SPM_PAGESIZE)
{
Flash_EraseWriteApplicationPage(addr);
// Wait for completion
#ifdef __AVR_XMEGA__
#ifdef USE_WATCHDOG
while (NVM_STATUS & NVM_NVMBUSY_bp)
{
// reset watchdog while waiting for erase completion
WDT_Reset();
}
#else // USE_WATCHDOG
SP_WaitForSPM();
#endif // USE_WATCHDOG
#endif // __AVR_XMEGA__
}
#else
Flash_EraseApplicationSection();
// Wait for completion
#ifdef __AVR_XMEGA__
#ifdef USE_WATCHDOG
while (NVM_STATUS & NVM_NVMBUSY_bp)
{
// reset watchdog while waiting for erase completion
WDT_Reset();
}
#else // USE_WATCHDOG
SP_WaitForSPM();
#endif // USE_WATCHDOG
#endif // __AVR_XMEGA__
#endif
// Erase EEPROM
EEPROM_erase_all();
// turn off read protection
#ifdef NEED_CODE_PROTECTION
protected = 0;
#endif // NEED_CODE_PROTECTION
// acknowledge
send_char(REPLY_ACK);
}
#ifdef ENABLE_BLOCK_SUPPORT
// Check block load support
else if (val == CMD_CHECK_BLOCK_SUPPORT )
{
// yes, it is supported
send_char(REPLY_YES);
// Send block size (page size)
send_char((SPM_PAGESIZE >> 8) & 0xFF);
send_char(SPM_PAGESIZE & 0xFF);
}
// Block load
else if (val == CMD_BLOCK_LOAD)
{
// Block size
i = get_2bytes();
// Memory type
val = get_char();
// Load it
send_char(BlockLoad(i, val, &address));
}
// Block read
else if (val == CMD_BLOCK_READ)
{
// Block size
i = get_2bytes();
// Memory type
val = get_char();
// Read it
BlockRead(i, val, &address);
}
#endif // ENABLE_BLOCK_SUPPORT
#ifdef ENABLE_FLASH_BYTE_SUPPORT
// Read program memory byte
else if (val == CMD_READ_BYTE)
{
unsigned int w = Flash_ReadWord((address << 1));
#ifdef ENABLE_CODE_PROTECTION
if (protected)
w = 0xffff;
#endif // ENABLE_CODE_PROTECTION
send_char(w >> 8);
send_char(w);
address++;
}
// Write program memory low byte
else if (val == CMD_WRITE_LOW_BYTE)
{
// get low byte
i = get_char();
send_char(REPLY_ACK);
}
// Write program memory high byte
else if (val == CMD_WRITE_HIGH_BYTE)
{
// get high byte; combine
i |= (get_char() << 8);
Flash_LoadFlashWord((address << 1), i);
address++;
send_char(REPLY_ACK);
}
// Write page
else if (val == CMD_WRITE_PAGE)
{
if (address >= (APP_SECTION_SIZE>>1))
{
// don't allow bootloader overwrite
send_char(REPLY_ERROR);
}
else
{
Flash_WriteApplicationPage( address << 1);
send_char(REPLY_ACK);
}
}
#endif // ENABLE_FLASH_BYTE_SUPPORT
#ifdef ENABLE_EEPROM_BYTE_SUPPORT
// Write EEPROM memory
else if (val == CMD_WRITE_EEPROM_BYTE)
{
EEPROM_write_byte(address, get_char());
address++;
send_char(REPLY_ACK);
}
// Read EEPROM memory
else if (val == CMD_READ_EEPROM_BYTE)
{
char c = EEPROM_read_byte(address);
#ifdef ENABLE_EEPROM_PROTECTION
if (protected)
c = 0xff;
#endif // ENABLE_EEPROM_PROTECTION
send_char(c);
address++;
}
#endif // ENABLE_EEPROM_BYTE_SUPPORT
#ifdef ENABLE_LOCK_BITS
#ifdef __AVR_XMEGA__
// Write lockbits
else if (val == CMD_WRITE_LOCK_BITS)
{
SP_WriteLockBits( get_char() );
send_char(REPLY_ACK);
}
// Read lockbits
else if (val == CMD_READ_LOCK_BITS)
{
send_char(SP_ReadLockBits());
}
#endif // __AVR_XMEGA__
#endif // ENABLE_LOCK_BITS
#ifdef ENABLE_FUSE_BITS
#ifdef __AVR_XMEGA__
// Read low fuse bits
else if (val == CMD_READ_LOW_FUSE_BITS)
{
send_char(SP_ReadFuseByte(0));
}
// Read high fuse bits
else if (val == CMD_READ_HIGH_FUSE_BITS)
{
send_char(SP_ReadFuseByte(1));
}
// Read extended fuse bits
else if (val == CMD_READ_EXT_FUSE_BITS)
{
send_char(SP_ReadFuseByte(2));
}
#endif // __AVR_XMEGA__
#endif // ENABLE_FUSE_BITS
// Enter and leave programming mode
else if ((val == CMD_ENTER_PROG_MODE) || (val == CMD_LEAVE_PROG_MODE))
{
// just acknowledge
send_char(REPLY_ACK);
}
// Exit bootloader
else if (val == CMD_EXIT_BOOTLOADER)
{
in_bootloader = 0;
send_char(REPLY_ACK);
}
// Get programmer type
else if (val == CMD_PROGRAMMER_TYPE)
{
// serial
send_char('S');
}
// Return supported device codes
else if (val == CMD_DEVICE_CODE)
{
// send only this device
send_char(123); // TODO
// terminator
send_char(0);
}
// Set LED, clear LED, and set device type
else if ((val == CMD_SET_LED) || (val == CMD_CLEAR_LED) || (val == CMD_SET_TYPE))
{
// discard parameter
get_char();
send_char(REPLY_ACK);
}
// Return program identifier
else if (val == CMD_PROGRAM_ID)
{
send_char('X');
send_char('B');
send_char('o');
send_char('o');
send_char('t');
send_char('+');
send_char('+');
}
// Read software version
else if (val == CMD_VERSION)
{
send_char('0' + XBOOT_VERSION_MAJOR);
send_char('0' + XBOOT_VERSION_MINOR);
}
// Read signature bytes
else if (val == CMD_READ_SIGNATURE)
{
send_char(SIGNATURE_2);
send_char(SIGNATURE_1);
send_char(SIGNATURE_0);
}
#ifdef ENABLE_CRC_SUPPORT
else if (val == CMD_CRC)
{
uint32_t start = 0;
uint32_t length = 0;
uint16_t crc;
val = get_char();
switch (val)
{
case SECTION_FLASH:
length = PROGMEM_SIZE;
break;
case SECTION_APPLICATION:
length = APP_SECTION_SIZE;
break;
case SECTION_BOOT:
start = BOOT_SECTION_START;
length = BOOT_SECTION_SIZE;
break;
#ifdef ENABLE_API
case SECTION_APP:
length = XB_APP_SIZE;
break;
case SECTION_APP_TEMP:
start = XB_APP_TEMP_START;
length = XB_APP_TEMP_SIZE;
break;
#endif // ENABLE_API
default:
send_char(REPLY_ERROR);
continue;
}
crc = crc16_block(start, length);
send_char((crc >> 8) & 0xff);
send_char(crc & 0xff);
}
#endif // ENABLE_CRC_SUPPORT
#ifdef USE_I2C
#ifdef USE_I2C_ADDRESS_NEGOTIATION
// Enter autonegotiate mode
else if (val == CMD_AUTONEG_START)
{
// The address autonegotiation protocol is borrowed from the
// OneWire address detection method. The algorthim Utilizes
// one extra shared wire, pulled up by resistors just like the
// main I2C bus, a OneWire bus, or a wired-AND IRQ line.
// The protocol involves intelligently guessing all of the
// connected devices' 88 bit unique hardware ID numbers, stored
// permanently in the production signature row during manufacture
// (see XMega series datasheet for details)
#ifdef __AVR_XMEGA__
// k is temp
// devid is pointer to current bit, init to first bit
// of the hardware ID in the production signature row
devid_bit = 0x08 << 3;
// read first byte of hardware ID into temporary location
k = SP_ReadCalibrationByte(0x08);
// main negotiation loop
while (1)
{
// wait for incoming data
while (1)
{
// check for bit read command
if (!(I2C_AUTONEG_PORT.IN & (1 << I2C_AUTONEG_PIN)))
{
// write current bit of hardware ID
ow_slave_write_bit(k & 1); // write bit
break;
}
// check for I2C bus activity
else if (I2C_DEVICE.SLAVE.STATUS & (TWI_SLAVE_APIF_bm | TWI_SLAVE_DIF_bm))
{
// grab a byte
// (there will be no I2C bus activity while
// the autonegotiation is taking place,
// so it's OK to block)
val = get_char();
// Is this an address byte for me?
if (val == CMD_AUTONEG_DONE)
{
// If so, we're now attached, so light
// the LED and update the I2C bus
// controller accordingly
// turn on attach LED
#ifdef USE_ATTACH_LED
#if ATTACH_LED_INV
ATTACH_LED_PORT.OUTCLR = (1 << ATTACH_LED_PIN);
#else
ATTACH_LED_PORT.OUTSET = (1 << ATTACH_LED_PIN);
#endif // ATTACH_LED_INV
#endif // USE_ATTACH_LED
// get new address
#if I2C_AUTONEG_DIS_GC
I2C_DEVICE.SLAVE.ADDR = get_char() << 1;
#else
I2C_DEVICE.SLAVE.ADDR = (get_char() << 1) | 1;
#endif // I2C_AUTONEG_DIS_GC
#if I2C_AUTONEG_DIS_PROMISC
// turn off promiscuous mode
I2C_DEVICE.SLAVE.CTRLA = TWI_SLAVE_ENABLE_bm;
#endif // I2C_AUTONEG_DIS_PROMISC
// we're done here
goto autoneg_done;
}
// Check for sync command
else if (val == CMD_SYNC)
{
// break out to main bootloader on sync
goto autoneg_done;
}
}
}
// Already wrote normal bit, so write the inverted one
ow_slave_write_bit(~k & 1); // write inverted bit
// Now read master's guess
i = ow_slave_read_bit();
// Does the guess agree with the current bit?
if ((k & 1 && i) || (~k & 1 && !i))
{
// look at next bit
devid_bit++;
k >>= 1;
// time for next byte?
if (!(devid_bit & 7))
{
// Out of bits?
if (devid_bit > (0x15 << 3))
{
// Can't break here (need to wait
// to see if the master sends along
// an address) so wrap around instead
devid_bit = 0x08 << 3;
}
// there are some holes in the signature row,
// so skip over them
if (devid_bit == (0x0E << 3))
devid_bit += 0x02 << 3;
if (devid_bit == (0x11 << 3))
devid_bit += 0x01 << 3;
// Read next byte
k = SP_ReadCalibrationByte(devid_bit >> 3);
}
}
else
{
// No match, we're done here
break;
}
}
autoneg_done:
// dummy to avoid error message
// this actually produces code 4 bytes smaller than either
// an asm nop, a continue, or a bare semicolon
i = 0;
#endif // __AVR_XMEGA__
}
// out-of-order autonegotiate address message
else if (val == CMD_AUTONEG_DONE)
{
// ignore it
// (blocking to send a ? will cause trouble)
}
#endif // USE_I2C_ADDRESS_NEGOTIATION
#endif // USE_I2C
// ESC (0x1b) to sync
// otherwise, error
else if (val != CMD_SYNC)
{
send_char(REPLY_ERROR);
}
// Wait for any lingering SPM instructions to finish
Flash_WaitForSPM();
// End of bootloader main loop
}
#ifdef NEED_INTERRUPTS
// Disable interrupts
cli();
#endif // NEED_INTERRUPTS
// Bootloader exit section
// Code here runs after the bootloader has exited,
// but before the application code has started
// --------------------------------------------------
#ifdef ENABLE_API
#ifdef ENABLE_API_FIRMWARE_UPDATE
// Update firmware if needed
install_firmware();
#endif // ENABLE_API_FIRMWARE_UPDATE
#endif // ENABLE_API
#ifdef USE_FIFO
// Shut down FIFO
fifo_deinit();
#endif // USE_FIFO
#ifdef USE_I2C
// Shut down I2C interface
i2c_deinit();
#endif // USE_I2C
#ifdef USE_UART
// Shut down UART
uart_deinit();
// Disable RX pin pull-up
#ifdef __AVR_XMEGA__
#ifdef UART_RX_PUEN
// Disable RX pin pullup
UART_RX_PIN_CTRL = 0;
#endif // UART_RX_PUEN
#else // __AVR_XMEGA__
#ifdef UART_RX_PUEN
// Disable RX pin pullup
UART_PORT &= ~(1 << UART_RX_PIN);
#endif // UART_RX_PUEN
#endif // __AVR_XMEGA__
// Shut down UART EN pin
#ifdef USE_UART_EN_PIN
#ifdef __AVR_XMEGA__
UART_EN_PORT.DIRCLR = (1 << UART_EN_PIN);
UART_EN_PORT.OUTCLR = (1 << UART_EN_PIN);
#else // __AVR_XMEGA__
UART_EN_PORT_DDR &= ~(1 << UART_EN_PIN);
UART_EN_PORT &= ~(1 << UART_EN_PIN);
#endif // __AVR_XMEGA__
#endif // USE_UART_EN_PIN
#endif // USE_UART
#ifdef __AVR_XMEGA__
#ifdef LOCK_SPM_ON_EXIT
// Lock SPM writes
SP_LockSPM();
#endif // LOCK_SPM_ON_EXIT
#endif // __AVR_XMEGA__
// Disable bootloader entry pin
#ifdef __AVR_XMEGA__
#ifdef USE_ENTER_PIN
#if ENTER_PIN_PUEN
// Disable bootloader entry pin pullup
ENTER_PIN_CTRL = 0;
#endif // ENTER_PIN_PUEN
#endif // USE_ENTER_PIN
#else // __AVR_XMEGA__
#ifdef USE_ENTER_PIN
#if ENTER_PIN_PUEN
// Disable bootloader entry pin pullup
ENTER_PORT &= ~(1 << ENTER_PIN);
#endif // ENTER_PIN_PUEN
#endif // USE_ENTER_PIN
#endif // __AVR_XMEGA__
// LED
#ifdef __AVR_XMEGA__
#ifdef USE_LED
// Turn off LED on exit