-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathATetherBOOT.c
executable file
·948 lines (775 loc) · 24.8 KB
/
ATetherBOOT.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
/**********************************************************/
/* Ethernet Bootloader */
/* for atmega328p in conjuction with WIZNET W5100 */
/* */
/* Based on Ardunio bootloader (ATmegaBOOT.c) */
/* Serial Bootloader for Atmel megaAVR Controllers */
/* */
/* ATetherBOOT.c */
/* */
/* 20100404: Hacked to work with W5100 by Severin Smith */
/* http://www.smallbutdigital.com */
/* */
/* ------------------------------------------------------ */
/* */
/* 20090201: hacked for 1k bootloader for ATmega168/328 */
/* swapped bulk of assembler for examples at: */
/* http://www.stderr.org/doc/avr-libc/avr-libc-user-manual/group__avr__boot.html */
/* by D. Brink, dcb AT opengauge.org */
/* with updated compiler options by S. Abeyasinghe spoink AT gmail.com */
/* 20070626: hacked for Arduino Diecimila (which auto- */
/* resets when a USB connection is made to it) */
/* by D. Mellis */
/* 20060802: hacked for Arduino by D. Cuartielles */
/* based on a previous hack by D. Mellis */
/* and D. Cuartielles */
/* */
/* Monitor and debug functions were added to the original */
/* code by Dr. Erik Lins, chip45.com. */
/* */
/* Thanks to Karl Pitrich for fixing a bootloader pin */
/* problem and more informative LED blinking! */
/* */
/* ------------------------------------------------------ */
/* */
/* based on stk500boot.c */
/* Copyright (c) 2003, Jason P. Kyle */
/* All rights reserved. */
/* */
/* This program 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. */
/* */
/* This program 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 this program; if not, */
/* see <http://www.gnu.org/licenses/>. */
/* */
/**********************************************************/
/* Includes */
#include <inttypes.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/boot.h>
#include <util/delay.h>
#include "W5100.h"
/* the current avr-libc eeprom functions do not support the ATmega168 */
/* own eeprom write/read functions are used instead */
#if !defined(__AVR_ATmega168__) && !defined(__AVR_ATmega328P__)
#include <avr/eeprom.h>
#endif
/* Defines */
/* 20100404: Severin Smith */
#define Sn_RX_MASK (0x0800-1)
#define S0_RX_BASE (RX_BASE+(Sn_RX_MASK+1)*0)
#define S1_RX_BASE (RX_BASE+(Sn_RX_MASK+1)*1)
#define S2_RX_BASE (RX_BASE+(Sn_RX_MASK+1)*2)
#define S3_RX_BASE (RX_BASE+(Sn_RX_MASK+1)*3)
#define Sn_TX_MASK (0x0800-1)
#define S0_TX_BASE (TX_BASE+(Sn_TX_MASK+1)*0)
#define S1_TX_BASE (TX_BASE+(Sn_TX_MASK+1)*1)
#define S2_TX_BASE (TX_BASE+(Sn_TX_MASK+1)*2)
#define S3_TX_BASE (TX_BASE+(Sn_TX_MASK+1)*3)
/* Use the F_CPU defined in Makefile */
/* 20060803: hacked by DojoCorp */
/* 20070626: hacked by David A. Mellis to decrease waiting time for auto-reset */
/* set the waiting time for the bootloader */
/* get this from the Makefile instead */
/* #define MAX_TIME_COUNT (F_CPU>>4) */
/* 20070707: hacked by David A. Mellis - after this many errors give up and launch application */
#define MAX_ERROR_COUNT 5
/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
/* never allow AVR Studio to do an update !!!! */
#define HW_VER 0x02
#define SW_MAJOR 0x01
#define SW_MINOR 0x10
/* onboard ` is used to indicate, that the bootloader was entered (3x flashing) */
/* if monitor functions are included, LED goes on after monitor was entered */
#ifdef __AVR_ATmega128__
/* Onboard LED is connected to pin PB7 (e.g. Crumb128, PROBOmega128, Savvy128) */
#define LED_DDR DDRC
#define LED_PORT PORTC
#define LED_PIN PINC
#define LED PINC0
#else
/* Onboard LED is connected to pin PB2 (e.g. Crumb8, Crumb168) */
#define LED_DDR DDRC
#define LED_PORT PORTC
#define LED_PIN PINC
/* 20100404: hacked by Severin Smith, LED pin is C0 on Anatidae */
/* 20060803: hacked by DojoCorp, LED pin is B5 in Arduino */
/* #define LED PINB2 */
#define LED PINC0
#endif
/* monitor functions will only be compiled when using ATmega128, due to bootblock size constraints */
#ifdef __AVR_ATmega128__
#define MONITOR
#endif
/* define various device id's */
/* manufacturer byte is always the same */
#define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
#if defined __AVR_ATmega128__
#define SIG2 0x97
#define SIG3 0x02
#define PAGE_SIZE 0x80U //128 words
#elif defined __AVR_ATmega64__
#define SIG2 0x96
#define SIG3 0x02
#define PAGE_SIZE 0x80U //128 words
#elif defined __AVR_ATmega32__
#define SIG2 0x95
#define SIG3 0x02
#define PAGE_SIZE 0x40U //64 words
#elif defined __AVR_ATmega16__
#define SIG2 0x94
#define SIG3 0x03
#define PAGE_SIZE 0x40U //64 words
#elif defined __AVR_ATmega8__
#define SIG2 0x93
#define SIG3 0x07
#define PAGE_SIZE 0x20U //32 words
#elif defined __AVR_ATmega88__
#define SIG2 0x93
#define SIG3 0x0a
#define PAGE_SIZE 0x20U //32 words
#elif defined __AVR_ATmega168__
#define SIG2 0x94
#define SIG3 0x06
#define PAGE_SIZE 0x40U //64 words
#elif defined __AVR_ATmega328P__
#define SIG2 0x95
#define SIG3 0x0F
#define PAGE_SIZE 0x40U //64 words
#elif defined __AVR_ATmega162__
#define SIG2 0x94
#define SIG3 0x04
#define PAGE_SIZE 0x40U //64 words
#elif defined __AVR_ATmega163__
#define SIG2 0x94
#define SIG3 0x02
#define PAGE_SIZE 0x40U //64 words
#elif defined __AVR_ATmega169__
#define SIG2 0x94
#define SIG3 0x05
#define PAGE_SIZE 0x40U //64 words
#elif defined __AVR_ATmega8515__
#define SIG2 0x93
#define SIG3 0x06
#define PAGE_SIZE 0x20U //32 words
#elif defined __AVR_ATmega8535__
#define SIG2 0x93
#define SIG3 0x08
#define PAGE_SIZE 0x20U //32 words
#endif
/* define null response */
#define NULL_RESPONSE 0xFA
// ladyada mods - start after programming done & on power cycle!
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
#define QUICKSTART_MODS 1
#endif
/* function prototypes */
void putch(char);
char getch(void);
static inline void getNch(uint8_t); // static inline saves a few bytes
void byte_response(uint8_t);
static inline void nothing_response(void);
char gethex(void);
void puthex(char);
void flash_led(uint8_t);
static inline void boot_program_page(uint32_t, uint8_t*);
int main (void) __attribute__ ((naked,section (".init9")));
uint8_t spi(uint8_t data); //Basic 8-Bit SPI exchange
uint8_t spi_W5100(uint8_t opcode, uint16_t address, uint8_t data); //Send a full 32-Bit Command to the W5100 chip
void sock_close();
void clean_app_start();
/* some variables */
union address_union {
uint16_t word;
uint8_t byte[2];
} address;
union length_union {
uint16_t word;
uint8_t byte[2];
} length;
struct flags_struct { // changed from a packed struct to save some bytes
uint8_t eeprom;
uint8_t rampz;
} flags;
uint8_t buff[256];
uint8_t address_high;
uint8_t pagesz=0x80;
uint8_t i;
uint8_t error_count = 0;
uint8_t firstchar = 0;
unsigned int pagenumber = 0;
void (*app_start)(void) = 0x0000;
void boot_program_page (uint32_t page, uint8_t *buf)
{
uint16_t i;
eeprom_busy_wait ();
boot_page_erase (page);
boot_spm_busy_wait (); // Wait until the memory is erased.
for (i=0; i<SPM_PAGESIZE; i+=2)
{
// Set up little-endian word.
uint16_t w = *buf++;
w += (*buf++) << 8;
boot_page_fill (page + i, w);
}
boot_page_write (page); // Store buffer in flash page.
boot_spm_busy_wait(); // Wait until the memory is written.
// Reenable RWW-section again. We need this if we want to jump back
// to the application after bootloading.
boot_rww_enable ();
}
/* main program starts here */
int main(void)
{
asm volatile ( "clr __zero_reg__" );
SP=RAMEND;
uint8_t ch,ch2;
uint16_t w;
#ifdef QUICKSTART_MODS
//ch = MCUSR;
//MCUSR = 0;
WDTCSR |= _BV(WDCE) | _BV(WDE);
WDTCSR = 0;
#endif
/* set pin direction for bootloader pin and enable pullup */
/* for ATmega128, two pins need to be initialized */
#ifdef __AVR_ATmega128__
BL_DDR &= ~_BV(BL0);
BL_DDR &= ~_BV(BL1);
BL_PORT |= _BV(BL0);
BL_PORT |= _BV(BL1);
#else
/* We run the bootloader regardless of the state of this pin. Thus, don't
put it in a different state than the other pins. --DAM, 070709
BL_DDR &= ~_BV(BL);
BL_PORT |= _BV(BL);
*/
#endif
/* set LED pin as output */
LED_DDR |= _BV(LED);
LED_PORT |= _BV(LED);
/* 20100404: Severin Smith, ethernet setup */
/* Ethernet stuff */
DDRB |= (1 << DDB2) | //SS
(1 << DDB3) | //MOSI
(1 << DDB5); //SCK
//Initial Pin States
PORTB |= (1 << PINB2); //SS High
/* SPI Control Register */
SPCR = (0 << SPIE) | //SPI Interrupt Enable (active high)
(1 << SPE) | //SPI Enable (active high)
(0 << DORD) | //Data Order (MSB first low; LSB first high)
(1 << MSTR) | //Master/Slave Select (Master high; Slave low)
(0 << CPOL) | //Clock Polarity
(0 << CPHA) | //Clock Phase
(0 << SPR1) | //SPI Clock Rate Select 1
(0 << SPR0); //SPI Clock Rate Select 0
/* SPI Status Register */
SPSR = (0 << SPIF) | //SPI Interrupt Flag (active high)
(0 << WCOL) | //Write COLlision Flag (active high)
(0 << SPI2X); //Double SPI Speed Bit (active high)
//Set Gateway Address: 169.254.183.177
spi_W5100(1, GAR2, 0xA9);
spi_W5100(1, GAR1, 0xFE);
spi_W5100(1, GAR2, 0xB7);
spi_W5100(1, GAR3, 0x00);
//Set Subnet Mask: 255.255.255.0
spi_W5100(1, SUBR0, 0xFF);
spi_W5100(1, SUBR1, 0xFF);
spi_W5100(1, SUBR2, 0xFF);
spi_W5100(1, SUBR3, 0x00);
//Set Device MAC: 00:08:DC:01:02:03
spi_W5100(1, SHAR0, 0x00);
spi_W5100(1, SHAR1, 0x08);
spi_W5100(1, SHAR2, 0xDC);
spi_W5100(1, SHAR3, 0x01);
spi_W5100(1, SHAR4, 0x02);
spi_W5100(1, SHAR5, 0x03);
//Set Device IP: 169.254.183.177
spi_W5100(1, SIPR0, 0xA9);
spi_W5100(1, SIPR1, 0xFE);
spi_W5100(1, SIPR2, 0XB7);
spi_W5100(1, SIPR3, 0xB1);
//Assign 2K Memory per socket for RX and TX
spi_W5100(1, RMSR, 0x55);
spi_W5100(1, TMSR, 0x55);
//Set up sockets for TCP connections, if SPI connection is faulty increment error count/bail out
while(1) {
spi_W5100(1, S0_MR, 0x01); //Set TCP Mode on Socket 0
//Use port 23
spi_W5100(1, S0_PORT0, 0x00);
spi_W5100(1, S0_PORT1, 0x17);
//Set Socket Open
spi_W5100(1, S0_CR, Sn_CR_OPEN);
//Check if Socket has opened if not try again
if(spi_W5100(0, S0_SR, 0x00) != Sn_SR_SOCK_INIT) {
spi_W5100(1, S0_CR, Sn_CR_CLOSE);
if (++error_count == MAX_ERROR_COUNT) clean_app_start();
continue;
}
//Set Socket to Listen
spi_W5100(1, S0_CR, Sn_CR_LISTEN);
//Check if Socket is listening if not try again
if(spi_W5100(0, S0_SR, 0x00) != Sn_SR_SOCK_LISTEN) {
spi_W5100(1, S0_CR, Sn_CR_CLOSE);
if (++error_count == MAX_ERROR_COUNT) clean_app_start();
continue;
}
break;
}
flash_led(NUM_LED_FLASHES * 2); //Flash to indicate setup complete, ready to recieve connection
uint32_t count = 0;
//Wait for connection
while(1) {
if(spi_W5100(0, S0_SR, 0x00) == Sn_SR_SOCK_ESTABLISHED) {
//Clear Rx pointer or else we get bogus data
uint16_t rx_size = (spi_W5100(0, S0_RX_RSR0, 0x00) << 8) | spi_W5100(0, S0_RX_RSR1, 0x00);
uint16_t rx_pointer = (spi_W5100(0, S0_RX_RD0, 0x00) << 8) | spi_W5100(0, S0_RX_RD1, 0x00);
spi_W5100(1, S0_RX_RD0, ((rx_pointer+rx_size) & 0xFF00) >> 8);
spi_W5100(1, S0_RX_RD1, (rx_pointer+rx_size) & 0x00FF);
//Ready to receive
spi_W5100(1, S0_CR, Sn_CR_RECV);
break;
}
count++;
if (count > MAX_TIME_COUNT) clean_app_start();
}
/* 20050803: by DojoCorp, this is one of the parts provoking the
system to stop listening, cancelled from the original */
//putch('\0');
/* forever loop */
for (;;) {
/* get character from UART */
ch = getch();
/* A bunch of if...else if... gives smaller code than switch...case ! */
/* Hello is anyone home ? */
if(ch=='0') {
firstchar = 1; // we got an appropriate bootloader instr.
nothing_response();
} else if (firstchar == 0) {
clean_app_start();
}
/* Request programmer ID */
/* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
/* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
else if(ch=='1') {
if (getch() == ' ') {
putch(0x14);
putch('A');
putch('V');
putch('R');
putch(' ');
putch('I');
putch('S');
putch('P');
putch(0x10);
} else {
if (++error_count == MAX_ERROR_COUNT)
clean_app_start();
}
}
/* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
else if(ch=='@') {
ch2 = getch();
if (ch2>0x85) getch();
nothing_response();
}
/* AVR ISP/STK500 board requests */
else if(ch=='A') {
ch2 = getch();
if(ch2==0x80) byte_response(HW_VER); // Hardware version
else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
else byte_response(0x00); // Covers various unnecessary responses we don't care about
}
/* Device Parameters DON'T CARE, DEVICE IS FIXED */
else if(ch=='B') {
getNch(20);
nothing_response();
}
/* Parallel programming stuff DON'T CARE */
else if(ch=='E') {
getNch(5);
nothing_response();
}
/* P: Enter programming mode */
/* R: Erase device, don't care as we will erase one page at a time anyway. */
else if(ch=='P' || ch=='R') {
nothing_response();
}
/* Leave programming mode */
else if(ch=='Q') {
nothing_response();
#ifdef QUICKSTART_MODS
flash_led(2);
// start immediately -ada
clean_app_start();
#endif
}
/* Set address, little endian. EEPROM in bytes, FLASH in words */
/* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
/* This might explain why little endian was used here, big endian used everywhere else. */
else if(ch=='U') {
address.byte[0] = getch();
address.byte[1] = getch();
#if defined __AVR_ATmega128__
if (address.word>0x7FFF) flags.rampz = 1; // No go with m256, FIXME
else flags.rampz = 0;
#endif
address.word = address.word << 1; // address * 2 -> byte location
nothing_response();
}
/* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
else if(ch=='V') {
if (getch() == 0x30) {
getch();
ch = getch();
getch();
if (ch == 0) {
byte_response(SIG1);
} else if (ch == 1) {
byte_response(SIG2);
} else {
byte_response(SIG3);
}
} else {
getNch(3);
byte_response(0x00);
}
}
/* Write memory, length is big endian and is in bytes */
else if(ch=='d') {
length.byte[1] = getch();
length.byte[0] = getch();
flags.eeprom = 0;
if (getch() == 'E') flags.eeprom = 1;
for (w=0;w<length.word;w++) {
buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
}
if (getch() == ' ') {
if (flags.eeprom) { //Write to EEPROM one byte at a time
for(w=0;w<length.word;w++) {
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
while(EECR & (1<<EEPE));
EEAR = (uint16_t)(void *)address.word;
EEDR = buff[w];
EECR |= (1<<EEMPE);
EECR |= (1<<EEPE);
#else
eeprom_write_byte((void *)address.word,buff[w]);
#endif
address.word++;
}
}
else { //Write to FLASH one page at a time
boot_program_page(pagenumber,(uint8_t *)&buff);
pagenumber+=SPM_PAGESIZE;
}
putch(0x14);
putch(0x10);
} else {
if (++error_count == MAX_ERROR_COUNT)
clean_app_start();
}
}
/* Read memory block mode, length is big endian. */
else if(ch=='t') {
length.byte[1] = getch();
length.byte[0] = getch();
flags.eeprom = 0;
if (getch() == 'E') flags.eeprom = 1;
if (getch() == ' ') { // Command terminator
putch(0x14);
for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
if (flags.eeprom) { // Byte access EEPROM read
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
while(EECR & (1<<EEPE));
EEAR = (uint16_t)(void *)address.word;
EECR |= (1<<EERE);
putch(EEDR);
#else
putch(eeprom_read_byte((void *)address.word));
#endif
address.word++;
}
else {
#if defined __AVR_ATmega128__
if (!flags.rampz) putch(pgm_read_byte_near(address.word));
else putch(pgm_read_byte_far(address.word + 0x10000));
// Hmmmm, yuck FIXME when m256 arrvies
#else
putch(pgm_read_byte_near(address.word));
#endif
address.word++;
}
}
putch(0x10);
}
}
/* Get device signature bytes */
else if(ch=='u') {
if (getch() == ' ') {
putch(0x14);
putch(SIG1);
putch(SIG2);
putch(SIG3);
putch(0x10);
} else {
if (++error_count == MAX_ERROR_COUNT)
clean_app_start();
}
}
/* Read oscillator calibration byte */
else if(ch=='v') {
byte_response(0x00);
}
#ifdef MONITOR
/* here come the extended monitor commands by Erik Lins */
/* check for three times exclamation mark pressed */
else if(ch=='!') {
ch = getch();
if(ch=='!') {
ch = getch();
if(ch=='!') {
#ifdef __AVR_ATmega128__
uint16_t extaddr;
#endif
uint8_t addrl, addrh;
#ifdef CRUMB128
PGM_P welcome = {"ATmegaBOOT / Crumb128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
#elif defined PROBOMEGA128
PGM_P welcome = {"ATmegaBOOT / PROBOmega128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
#elif defined SAVVY128
PGM_P welcome = {"ATmegaBOOT / Savvy128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
#endif
/* turn on LED */
LED_DDR |= _BV(LED);
LED_PORT &= ~_BV(LED);
/* print a welcome message and command overview */
for(i=0; welcome[i] != '\0'; ++i) {
putch(welcome[i]);
}
/* test for valid commands */
for(;;) {
putch('\n');
putch('\r');
putch(':');
putch(' ');
ch = getch();
putch(ch);
/* toggle LED */
if(ch == 't') {
if(bit_is_set(LED_PIN,LED)) {
LED_PORT &= ~_BV(LED);
putch('1');
} else {
LED_PORT |= _BV(LED);
putch('0');
}
}
/* read byte from address */
else if(ch == 'r') {
ch = getch(); putch(ch);
addrh = gethex();
addrl = gethex();
putch('=');
ch = *(uint8_t *)((addrh << 8) + addrl);
puthex(ch);
}
/* write a byte to address */
else if(ch == 'w') {
ch = getch(); putch(ch);
addrh = gethex();
addrl = gethex();
ch = getch(); putch(ch);
ch = gethex();
*(uint8_t *)((addrh << 8) + addrl) = ch;
}
/* read from uart and echo back */
else if(ch == 'u') {
for(;;) {
putch(getch());
}
}
#ifdef __AVR_ATmega128__
/* external bus loop */
else if(ch == 'b') {
putch('b');
putch('u');
putch('s');
MCUCR = 0x80;
XMCRA = 0;
XMCRB = 0;
extaddr = 0x1100;
for(;;) {
ch = *(volatile uint8_t *)extaddr;
if(++extaddr == 0) {
extaddr = 0x1100;
}
}
}
#endif
else if(ch == 'j') {
clean_app_start();
}
} /* end of monitor functions */
}
}
}
/* end of monitor */
#endif
else if (++error_count == MAX_ERROR_COUNT) {
clean_app_start();
}
} /* end of forever loop */
}
char gethexnib(void) {
char a;
a = getch(); putch(a);
if(a >= 'a') {
return (a - 'a' + 0x0a);
} else if(a >= '0') {
return(a - '0');
}
return a;
}
char gethex(void) {
return (gethexnib() << 4) + gethexnib();
}
void puthex(char ch) {
char ah;
ah = ch >> 4;
if(ah >= 0x0a) {
ah = ah - 0x0a + 'a';
} else {
ah += '0';
}
ch &= 0x0f;
if(ch >= 0x0a) {
ch = ch - 0x0a + 'a';
} else {
ch += '0';
}
putch(ah);
putch(ch);
}
/* 20100404 Severin Smith, hacked to work with WIZNET W5100 instead of UART */
void putch(char ch)
{
sock_close(); //Check if connection was dropped
//Compute TX address
uint16_t tx_write_pointer = spi_W5100(0, S0_TX_WR0, 0x00);
tx_write_pointer = ((tx_write_pointer & 0xFF) << 8) + spi_W5100(0, S0_TX_WR1, 0x00);
uint16_t tx_offset = tx_write_pointer & Sn_TX_MASK;
uint16_t tx_address = S0_TX_BASE + tx_offset;
if((tx_offset+1) > Sn_TX_MASK) tx_address = S0_TX_BASE;
//Load char in W5100 memory
spi_W5100(1, tx_address, ch);
//Update Pointers
spi_W5100(1, S0_TX_WR0, ((tx_write_pointer+1) & 0xFF00) >> 8);
spi_W5100(1, S0_TX_WR1, (tx_write_pointer+1) & 0xFF);
//Send
spi_W5100(1, S0_CR, Sn_CR_SEND);
}
/* 20100404 Severin Smith, hacked to work with WIZNET W5100 instead of UART */
char getch(void)
{
//Wait for the next char within reason, else cleanup/bailout
uint16_t rx_size = 0x00;
uint32_t count = 0;
while(rx_size < 0x01) {
sock_close(); //Check for dropped connection
rx_size = (spi_W5100(0, S0_RX_RSR0, 0x00) << 8) | spi_W5100(0, S0_RX_RSR1, 0x00);
count++;
if (count > MAX_TIME_COUNT) clean_app_start();
}
uint8_t ch = 0;
//Compute RX address
uint16_t rx_pointer = (spi_W5100(0, S0_RX_RD0, 0x00) << 8) | spi_W5100(0, S0_RX_RD1, 0x00);
uint16_t rx_offset = rx_pointer & Sn_RX_MASK; //Calculate offset
uint16_t rx_address = S0_RX_BASE+rx_offset; //Calculate physical start address
if((rx_offset+1) > Sn_RX_MASK) rx_address = S0_RX_BASE;
//Get char
ch = spi_W5100(0, rx_address, 0x00);
//Update RX pointers
spi_W5100(1, S0_RX_RD0, ((rx_pointer+1) & 0xFF00) >> 8);
spi_W5100(1, S0_RX_RD1, (rx_pointer+1) & 0x00FF);
spi_W5100(1, S0_CR, Sn_CR_RECV);
return ch;
}
void getNch(uint8_t count)
{
while(count--) {
getch();
}
}
void byte_response(uint8_t val)
{
if (getch() == ' ') {
putch(0x14);
if (val != NULL_RESPONSE)
putch(val);
putch(0x10);
} else {
if (++error_count == MAX_ERROR_COUNT)
clean_app_start();
}
}
void nothing_response(void)
{
byte_response(NULL_RESPONSE);
}
void flash_led(uint8_t count)
{
while (count--) {
LED_PORT ^= _BV(LED); // toggle!
_delay_ms(100);
}
}
/* 20100404 Severin Smith, hacked to work with WIZNET W5100 instead of UART */
uint8_t spi_W5100(uint8_t opcode, uint16_t address, uint8_t data) {
PORTB = PORTB & ~(1 << PINB2); //Make SS Low
if(opcode) spi(0xF0); //write
else spi(0x0F); //read
spi((address&0xFF00) >> 8); //Address 1
spi(address&0xFF); //Address 2
uint8_t response = spi(data); //Send data
PORTB |= (1 << PINB2); //Make SS High
return response;
}
uint8_t spi(uint8_t data) {
/* Start transmission */
SPDR = data;
/* Wait for transmission complete */
while(!(SPSR & (1 << SPIF)));
/* Return received data */
return SPDR;
}
void sock_close() {
//Check if we were dropped and clean up/bail out if needed
if(spi_W5100(0, S0_SR, 0x00) == Sn_SR_SOCK_CLOSE_WAIT) {
clean_app_start();
}
if(spi_W5100(0, S0_SR, 0x00) == Sn_SR_SOCK_CLOSED) {
clean_app_start();
}
}
void clean_app_start() {
spi_W5100(1, S0_CR, Sn_CR_CLOSE);
app_start();
}
/* end of file ATetherBOOT.c */