-
Notifications
You must be signed in to change notification settings - Fork 2
/
reaDIYboot.c
1403 lines (1290 loc) · 41.4 KB
/
reaDIYboot.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
/* reaDIYboot
* Written by Pierre Bouchet
* Copyright (C) 2011-2012 reaDIYmate
*
* 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/>.
*/
#include <inttypes.h>
#include <stdbool.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <util/delay.h>
/* WiFly reset pin */
volatile uint8_t* const RESET_PORT = &PORTL;
volatile uint8_t* const RESET_PORT_INPUT = &PINL;
volatile uint8_t* const RESET_DDR = &DDRL;
uint8_t const RESET_PIN = PINL0;
/* WiFly GPIO4 pin */
volatile uint8_t* const GPIO4_PORT = &PORTJ;
volatile uint8_t* const GPIO4_PORT_INPUT = &PINJ;
volatile uint8_t* const GPIO4_DDR = &DDRJ;
uint8_t const GPIO4_PIN = PINJ5;
/* WiFly GPIO5 pin */
volatile uint8_t* const GPIO5_PORT = &PORTJ;
volatile uint8_t* const GPIO5_PORT_INPUT = &PINJ;
volatile uint8_t* const GPIO5_DDR = &DDRJ;
uint8_t const GPIO5_PIN = PINJ6;
/* WiFly GPIO6 pin */
volatile uint8_t* const GPIO6_PORT = &PORTJ;
volatile uint8_t* const GPIO6_PORT_INPUT = &PINJ;
volatile uint8_t* const GPIO6_DDR = &DDRJ;
uint8_t const GPIO6_PIN = PINJ7;
/* Green LED pin */
volatile uint8_t* const GREEN_LED_PORT = &PORTD;
volatile uint8_t* const GREEN_LED_PORT_INPUT = &PIND;
volatile uint8_t* const GREEN_LED_DDR = &DDRD;
uint8_t const GREEN_LED_PIN = PIND5;
/* Red LED pin */
volatile uint8_t* const RED_LED_PORT = &PORTD;
volatile uint8_t* const RED_LED_PORT_INPUT = &PIND;
volatile uint8_t* const RED_LED_DDR = &DDRD;
uint8_t const RED_LED_PIN = PIND4;
/* Use Timer/Counter3 channel A to detect UART timeouts */
volatile uint16_t* const UART_OCR = &OCR3A;
uint8_t const UART_OCF = OCF3A;
/* Use Timer/Counter3 channel B to detect WLAN timeouts */
volatile uint16_t* const WLAN_OCR = &OCR3B;
uint8_t const WLAN_OCF = OCF3B;
/* Use Timer/Counter3 channel C to detect HTTP timeouts */
volatile uint16_t* const HTTP_OCR = &OCR3C;
uint8_t const HTTP_OCF = OCF3C;
/* Location of the EEPROM flag */
uint16_t* const EEPROM_FLAG_ADDRESS = (uint16_t*)(0xFFF - 1);
/* Value of the EEPROM flag */
uint16_t const EEPROM_FLAG_VALUE = 0x232e;
/* Size of a program page in a HEX file */
#define HEX_BUFFER_SIZE 4096
/* Size of a program page in Flash memory (128 words) */
#define FLASH_PAGE_SIZE 0x80U
/* Size of the buffer used to hold the HEX file location */
#define PATH_BUFFER_SIZE 64
// STK500 protocol
/* Get parameter value */
uint8_t const STK_GET_PARAMETER = 0x41;
/* Leave program mode */
uint8_t const STK_LEAVE_PROGMODE = 0x51;
/* Load address */
uint8_t const STK_LOAD_ADDRESS = 0x55;
/* Program page */
uint8_t const STK_PROG_PAGE = 0x64;
/* Read page */
uint8_t const STK_READ_PAGE = 0x74;
/* Read signature bytes */
uint8_t const STK_READ_SIGN = 0x75;
/* Set device programming parameters */
uint8_t const STK_SET_DEVICE = 0x42;
/* Set extended device programming parameters */
uint8_t const STK_SET_DEVICE_EXT = 0x45;
/* Universal command */
uint8_t const STK_UNIVERSAL = 0x56;
/* Software version major */
uint8_t const STK_SW_MAJOR = 0x81;
/* Software version minor */
uint8_t const STK_SW_MINOR = 0x82;
/* Software version major */
uint8_t const SW_MAJOR = 0x01;
/* Software version minor */
uint8_t const SW_MINOR = 0x10;
/* No top-card detected */
uint8_t const NO_TOPCARD_DETECTED = 0x03;
/* End of packet */
uint8_t const STK_CRC_EOP = 0x20;
/* Sent after a valid command has been executed */
uint8_t const STK_OK = 0x10;
/* Sent after STK_CRC_EOP has been received */
uint8_t const STK_INSYNC = 0x14;
/* Device Signature Byte 1 */
uint8_t const SIG1 = 0x1E;
/* Device Signature Byte 2 */
uint8_t const SIG2 = 0x97;
/* Device Signature Byte 3 */
uint8_t const SIG3 = 0x03;
// Error thresholds for the state machines
/* STK programmer */
uint8_t const MAX_STK_ERROR_COUNT = 5;
/* WiFly module */
uint8_t const MAX_COMMAND_ERRORS = 1;
uint8_t const MAX_WIFLY_CRITICAL_ERRORS = 1;
uint8_t const MAX_WLAN_ERRORS = 3;
/* Web server */
uint8_t const MAX_DOWNLOAD_CRITICAL_ERRORS = 3;
uint8_t const MAX_HTTP_ERRORS = 3;
uint8_t const MAX_SOCKET_ERRORS = 3;
/* HTTP fields sent with each request */
char* const HTTP_FIELDS =
" HTTP/1.1\r\n"
"User-Agent: " USER_AGENT "\r\n"
"Host: " PROGRAM_HOST "\r\n"
"Connection: Keep-Alive\r\n";
/* Pointer to a string representing the HEX file location */
#ifdef USE_URL_INDIRECTION
char* PROGRAM_PATH = 0;
#else
char* PROGRAM_PATH = STATIC_PROGRAM_PATH;
#endif
/* Buffer used to hold the location of the HEX file */
char path_buffer[PATH_BUFFER_SIZE];
/* Buffer used to hold the id of the device */
char device_id[DEVICE_ID_LENGTH + 1];
void main(void) __attribute__((OS_main));
static void bootload_from_internet(void);
static void bootload_from_stk(void);
static bool add_error(uint8_t* count, uint8_t max_count);
/* Download management */
static void download_append_leftover(void);
static bool download_get_chunk(void);
static bool download_get_path(void);
static bool download_get_size(void);
static bool download_get_status(void);
static bool download_parse_chunk(void);
static bool download_parse_path(void);
static bool download_parse_size(void);
static void download_update_status(void);
/* Read device ID from EEPROM */
static void eeprom_read_id(void);
/* Send HTTP requests */
static bool http_await_response(void);
static bool http_send(void (*request)(void), bool (*action)(void));
static void request_get_chunk(void);
static void request_get_size(void);
static void request_get_status(void);
static void request_update_status(void);
/* iHEX data format */
static bool ihex_check_line(void);
static bool ihex_load_byte(void);
static uint8_t ihex_parse_byte(uint8_t* source);
/* STK communication protocol */
static void stk_byte_response(uint8_t);
static uint8_t stk_get_char(void);
static void stk_get_n_char(uint8_t);
static void stk_nothing_response(void);
static void stk_put_char(uint8_t);
/* WiFly EZX Wi-Fi module management */
static bool wifly_check_socket(void);
static bool wifly_check_wlan(void);
static void wifly_close_socket(void);
static bool wifly_connect_to_host(void);
static void wifly_enter_command_mode(void);
static bool wifly_find_string(const char* target);
static uint8_t wifly_get_char(void);
static void wifly_join_wlan(void);
static void wifly_open_socket(void);
static void wifly_put_char(uint8_t ch);
static void wifly_put_string(const char* source);
static void wifly_put_long(uint32_t number);
static void wifly_reset(void);
static bool wifly_set_host(void);
/* Core self-programming function */
static void write_bin_page(void);
/* Possible states for the WiFly state machine */
enum wifly_state {
RESETTING,
SETTING_HOST,
JOINING_WLAN,
OPENING_SOCKET,
WIFLY_CRITICAL_ERROR
};
/* Possible states for the Web downloader state machine */
enum download_state {
CHECKING_SOCKET,
SENDING_REQUEST,
RECEIVING_RESPONSE,
HTTP_ERROR,
DOWNLOAD_CRITICAL_ERROR
};
/* Possible states for the internet bootloader state machine */
enum bootloader_state {
ENTERING,
FILLING_BUFFER,
CHECKING_HEX_LINE,
PARSING_HEX_LINE,
WRITING_BIN_PAGE,
EXITING,
JUMPING_TO_APP
};
struct wifly_struct {
enum wifly_state state;
struct {
uint8_t command;
uint8_t critical;
uint8_t wlan;
uint8_t socket;
} errors;
} wifly = {RESETTING, {0, 0, 0, 0}};
struct download_struct {
enum download_state state;
struct {
uint8_t critical;
uint8_t http;
uint8_t parse;
} errors;
} download = {CHECKING_SOCKET, {0, 0, 0}};
/* Binary program page */
struct hex_chunk_struct {
uint32_t file_start;
uint32_t file_stop;
uint16_t size;
uint16_t index;
} hex_chunk = {0, 0, 0, 0};
/* Binary program page */
struct bin_page_struct {
uint16_t address;
uint16_t index;
} bin_page = {0x0000, 0};
/* Target address in Flash memory */
union address_union {
uint16_t word;
uint8_t byte[2];
} address;
/* Source block byte count */
union length_union {
uint16_t word;
uint8_t byte[2];
} length;
/* State of the internet bootloader state machine */
enum bootloader_state boot_state = ENTERING;
/* UART buffer */
uint8_t bin_buffer[2*FLASH_PAGE_SIZE];
/* HEX page buffer */
uint8_t hex_buffer[HEX_BUFFER_SIZE + 1];
/* UART error counter */
uint8_t stk_errors;
/* STK communication timeout flag */
bool stk_timeout;
/* Size of the HEX file hosted on the remote server in bytes */
uint32_t hex_program_size;
/* Byte count of the current line in the HEX file */
uint8_t line_byte_count;
/* Buffer used for unsigned-to-ASCII conversions */
char utoa_buffer[7];
/* Function pointer to the start of the application */
void (*app_start)(void) = 0x0000;
void main(void)
{
uint8_t status_register;
status_register = MCUSR;
// Clear the Watchdog System Reset Flag
MCUSR = 0x00;
// Disable the Watchdog Timer (see section 11.4 in the ATmega1280 manual)
WDTCSR |= (1 << WDCE) | (1 << WDE);
WDTCSR = 0x00;
// If the last reset was triggered by the watchdog timer, skip the
// bootloader and start the main program.
if (status_register & (1 << WDRF))
app_start();
// Set the LED pins as outputs
*GREEN_LED_DDR |= (1 << GREEN_LED_PIN);
*RED_LED_DDR |= (1 << RED_LED_PIN);
// Set the reset line for the Wi-Fi module as an output
*RESET_DDR |= (1 << RESET_PIN);
// Set the GPIO5 pin as an output
*GPIO5_DDR |= (1 << GPIO5_PIN);
// Set the GPIO4 and GPIO6 pins as inputs
*GPIO4_DDR &= ~(1 << GPIO4_PIN);
*GPIO6_DDR &= ~(1 << GPIO6_PIN);
// Enable internal pull-up resistor
*GPIO4_PORT |= (1 << GPIO4_PIN);
// Initialize UART0 (for the STK programmer)
UBRR0L = (uint8_t)(F_CPU/(STK_BAUD_RATE*16L) - 1);
UBRR0H = (F_CPU/(STK_BAUD_RATE*16L)-1) >> 8;
UCSR0A = 0x00;
UCSR0B = (1 << TXEN0)|(1 << RXEN0);
UCSR0C = (1 << UCSZ01)|(1 << UCSZ00);
// Enable internal pull-up resistor on pin E0 (RX)
DDRE &= ~(1 << PINE0);
PORTE |= (1 << PINE0);
// Initialize UART1 (for the Wi-Fi module)
#if WIFLY_BAUD_RATE >= 115200
UBRR1L = (uint8_t)(F_CPU/(WIFLY_BAUD_RATE*4L) - 1)/2;
UBRR1H = (F_CPU/(WIFLY_BAUD_RATE*4L) - 1)/2 >> 8;
UCSR1A = (1 << U2X1);
#else
UBRR1L = (uint8_t)(F_CPU/(WIFLY_BAUD_RATE*8L) - 1)/2;
UBRR1H = (F_CPU/(WIFLY_BAUD_RATE*8L) - 1)/2 >> 8;
UCSR1A = 0x00;
#endif
UCSR1B = (1 << TXEN1)|(1 << RXEN1);
UCSR1C = (1 << UCSZ11)|(1 << UCSZ10);
// Set Timer3 to normal mode
TCCR3A = 0x00;
// Set the prescaler to 1024
TCCR3B = (1 << CS32) | (1 << CS30);
// Set the WLAN timeout to 1 second
*WLAN_OCR = 0x3d09;
// Set the UART timeout to 4 seconds
*UART_OCR = 0xF424;
// Set the HTTP timeout to 4 seconds
*HTTP_OCR = 0xF424;
// Try to bootload using the STK protocol on UART0.
bootload_from_stk();
// If the EEPROM flag doesn't have the magic value, don't try to bootload
// from the internet.
if (eeprom_read_word(EEPROM_FLAG_ADDRESS) != EEPROM_FLAG_VALUE) {
WDTCSR = (1 << WDE);
while (1);
}
// Try to bootload using the Wi-Fi module on UART1 to fetch a program from
// the internet.
bootload_from_internet();
// If either operation succeeds, the MCU will reset using the Watchdog
// Timer, then it will skip to the main program.
}
static void bootload_from_internet(void)
{
#ifdef USE_DEVICE_ID
eeprom_read_id();
#endif
do {
if (boot_state == ENTERING) {
// Reset the Wi-Fi module in case it was left in a hanging state
wifly_reset();
// Switch led color to red
*RED_LED_PORT |= (1 << RED_LED_PIN);
*GREEN_LED_PORT &= ~(1 << GREEN_LED_PIN);
#ifdef CHECK_STATUS_BEFORE_DOWNLOAD
// Check if an update is available
if (!download_get_status())
boot_state = JUMPING_TO_APP;
else
#endif
#ifdef USE_URL_INDIRECTION
if (!download_get_path())
boot_state = JUMPING_TO_APP;
else
#endif
// Get the size of the HEX file
if (!download_get_size())
boot_state = JUMPING_TO_APP;
else
boot_state = FILLING_BUFFER;
}
else if (boot_state == FILLING_BUFFER) {
// Switch led color to orange
*RED_LED_PORT |= (1 << RED_LED_PIN);
*GREEN_LED_PORT |= (1 << GREEN_LED_PIN);
if (hex_chunk.file_start == hex_program_size) {
boot_state = EXITING;
}
// Fetch the next chunk of HEX data
else if (download_get_chunk()) {
// Reset the HEX buffer indexes
hex_chunk.size += hex_chunk.index;
hex_chunk.index = 0;
boot_state = CHECKING_HEX_LINE;
}
else
boot_state = JUMPING_TO_APP;
}
else if (boot_state == CHECKING_HEX_LINE) {
// Switch led color to orange
*RED_LED_PORT |= (1 << RED_LED_PIN);
*GREEN_LED_PORT |= (1 << GREEN_LED_PIN);
if (ihex_check_line()) {
boot_state = PARSING_HEX_LINE;
}
else {
hex_chunk.file_start = hex_chunk.file_stop + 1;
download_append_leftover();
boot_state = FILLING_BUFFER;
}
}
else if (boot_state == PARSING_HEX_LINE) {
// Switch led color to orange
*RED_LED_PORT |= (1 << RED_LED_PIN);
*GREEN_LED_PORT |= (1 << GREEN_LED_PIN);
if (bin_page.index == 2*FLASH_PAGE_SIZE) {
boot_state = WRITING_BIN_PAGE;
}
else if (!ihex_load_byte())
boot_state = CHECKING_HEX_LINE;
}
else if (boot_state == WRITING_BIN_PAGE) {
// Switch led color to green
*RED_LED_PORT &= ~(1 << RED_LED_PIN);
*GREEN_LED_PORT |= (1 << GREEN_LED_PIN);
// Update page byte count
length.word = bin_page.index;
// Update target Flash location
address.word = bin_page.address;
// Write the binary page to Flash
write_bin_page();
// The address is a word location whereas the size is a byte count,
// so divide it by 2
bin_page.address += length.word >> 1;
// Reset the binary page index
bin_page.index = 0;
boot_state = PARSING_HEX_LINE;
}
else if (boot_state == EXITING) {
// Switch led color to green
*RED_LED_PORT &= ~(1 << RED_LED_PIN);
*GREEN_LED_PORT |= (1 << GREEN_LED_PIN);
// Update page byte count
length.word = bin_page.index;
// Update target Flash location
address.word = bin_page.address;
// Write the binary page to Flash
write_bin_page();
#ifdef CLEAR_STATUS_AFTER_DOWNLOAD
download_update_status();
#endif
boot_state = JUMPING_TO_APP;
}
else if (boot_state == JUMPING_TO_APP) {
*GREEN_LED_PORT &= ~(1 << GREEN_LED_PIN);
*RED_LED_PORT &= ~(1 << RED_LED_PIN);
// Watchdog Timer reset
WDTCSR = (1 << WDE);
while (1);
}
} while (1);
}
static void bootload_from_stk(void)
{
uint16_t b;
uint8_t ch, ch2;
// RAMPZ Flag used to indicate memory writes beyond the 64kB boundary
uint8_t flag_rampz;
while (!stk_timeout && stk_errors < MAX_STK_ERROR_COUNT) {
ch = stk_get_char();
// Get parameter value
if (ch == STK_GET_PARAMETER) {
ch2 = stk_get_char();
// Software major version
if (ch2 == STK_SW_MAJOR) {
stk_byte_response(SW_MAJOR);
}
// Software minor version
else if (ch2 == STK_SW_MINOR) {
stk_byte_response(SW_MINOR);
}
// Required by AVR Studio
else {
stk_byte_response(NO_TOPCARD_DETECTED);
}
}
// Leave program mode
else if (ch == STK_LEAVE_PROGMODE) {
stk_nothing_response();
// Watchdog Timer reset
WDTCSR = (1 << WDE);
while (1);
}
// Load word address
else if (ch == STK_LOAD_ADDRESS) {
// Address is little endian and is in words
address.byte[0] = stk_get_char();
address.byte[1] = stk_get_char();
stk_nothing_response();
}
// Program page
else if (ch == STK_PROG_PAGE) {
// Length is big endian and is in bytes
length.byte[1] = stk_get_char();
length.byte[0] = stk_get_char();
stk_get_char();
// Receive the binary page and store it to the buffer
for (b = 0; b < length.word; b++) {
bin_buffer[b] = stk_get_char();
}
if (stk_get_char() == STK_CRC_EOP) {
// Write the binary page to the Flash memory
write_bin_page();
stk_put_char(STK_INSYNC);
stk_put_char(STK_OK);
}
else {
++stk_errors;
}
}
// Read page
else if (ch == STK_READ_PAGE) {
*GREEN_LED_PORT |= (1 << GREEN_LED_PIN);
// Length is big endian and is in bytes
length.byte[1] = stk_get_char();
length.byte[0] = stk_get_char();
if (address.word > 0x7FFF) {
flag_rampz = 1;
}
else {
flag_rampz = 0;
}
// Since the address sent via STK is the word address, address*2
// yields the byte address
address.word = address.word << 1;
stk_get_char();
if (stk_get_char() == STK_CRC_EOP) {
stk_put_char(STK_INSYNC);
for (b = 0; b < length.word; b++) {
if (!flag_rampz) {
stk_put_char(pgm_read_byte_near(address.word));
}
else {
stk_put_char(pgm_read_byte_far(address.word +
0x10000));
}
address.word++;
}
stk_put_char(STK_OK);
}
*GREEN_LED_PORT &= ~(1 << GREEN_LED_PIN);
}
// Read signature bytes
else if (ch == STK_READ_SIGN) {
if (stk_get_char() == STK_CRC_EOP) {
stk_put_char(STK_INSYNC);
stk_put_char(SIG1);
stk_put_char(SIG2);
stk_put_char(SIG3);
stk_put_char(STK_OK);
}
else {
++stk_errors;
}
}
// Set device programming parameters (ignored)
else if (ch == STK_SET_DEVICE) {
stk_get_n_char(20);
stk_nothing_response();
}
// Set extended device programming parameters (ignored)
else if (ch == STK_SET_DEVICE_EXT) {
stk_get_n_char(5);
stk_nothing_response();
}
// Universal command (ignored)
else if (ch == STK_UNIVERSAL) {
stk_get_n_char(4);
stk_byte_response(0x00);
}
// Other commands are either invalid or ignored
else {
stk_nothing_response();
}
}
}
/* Increment the error counter then wait a moment */
static bool add_error(uint8_t* count, uint8_t max_count) {
if (*count >= max_count)
return false;
else {
*count += 1;
_delay_ms(2000);
return true;
}
}
// Move an incomplete HEX line to the beginning of the buffer
static void download_append_leftover(void) {
uint8_t* data = hex_buffer + hex_chunk.index;
uint8_t length = hex_chunk.size - hex_chunk.index;
uint8_t i;
for (i = 0; i < length; i++) {
hex_buffer[i] = data[i];
}
hex_buffer[i] = 0x00;
hex_chunk.index = i;
}
/* Download the next HEX page and extract its binary content */
static bool download_get_chunk(void)
{
return http_send(&request_get_chunk, &download_parse_chunk);
}
/* Get the location of the HEX file */
static bool download_get_path(void)
{
if (!http_send(&request_get_status, 0))
return false;
if (!wifly_find_string(PATH_JSON_PREFIX))
return false;
if (!download_parse_path())
return false;
else {
PROGRAM_PATH = path_buffer;
return true;
}
}
/* Poll the HTTP server to get the size of the HEX file */
static bool download_get_size(void)
{
return http_send(&request_get_size, &download_parse_size);
}
/* Send a request to check if a new program is available */
static bool download_get_status(void)
{
if (!http_send(&request_get_status, 0))
return false;
else
return wifly_find_string(CHECK_STATUS_EXPECTED_RESPONSE);
}
/* Store the incoming data into the HEX page buffer */
static bool download_parse_chunk(void)
{
uint32_t i;
uint8_t* dest;
// Skip HTTP status and header
if (!wifly_find_string("\r\n\r\n"))
return false;
else {
// Download HTTP response body
dest = hex_buffer + hex_chunk.index;
hex_chunk.size = hex_chunk.file_stop - hex_chunk.file_start + 1;
for (i = 0; i < hex_chunk.size; i++) {
dest[i] = wifly_get_char();
if (dest[i] == 0x00) {
return false;
}
}
// Add a terminating null character
dest[i] = 0x00;
return true;
}
}
/* Parse the value associated to the "path" key in the JSON response */
static bool download_parse_path(void)
{
uint8_t ch;
uint8_t i;
i = 0;
do {
ch = wifly_get_char();
if (i >= PATH_BUFFER_SIZE - 1)
return false;
else if (ch == '\\')
continue;
else if (ch == '\"')
break;
else {
path_buffer[i++] = ch;
}
} while (true);
path_buffer[i] = '\0';
return true;
}
/* Parse the Content-Length field from the incoming HTTP header */
static bool download_parse_size(void)
{
uint8_t i, nb_digits;
uint32_t result;
uint8_t ch;
uint8_t buffer[16];
// Find the digits in the HTTP input stream
if (!wifly_find_string("Content-Length: ")) {
return false;
}
else {
i = 0;
do {
ch = wifly_get_char();
if (ch == 0)
return false;
else {
buffer[i++] = ch;
}
} while (ch != '\r');
buffer[i] = 0x00;
nb_digits = i - 1;
}
// Compute the value represented by these digits
result = 0;
for (i = 0; i < nb_digits; i++) {
ch = buffer[i];
if (ch < '0' || ch > '9')
return false;
else {
result *= 10;
result += (uint32_t)(ch - '0');
}
}
hex_program_size = result;
return (hex_program_size > 0);
}
/* Send a request to confirm that the program was successfully downloaded */
static void download_update_status(void)
{
http_send(&request_update_status, 0);
}
/* Read the device ID from the EEPROM in to the SRAM */
static void eeprom_read_id(void)
{
eeprom_read_block(
(void*)device_id,
(const void*)DEVICE_ID_EEPROM_ADDRESS,
DEVICE_ID_LENGTH
);
}
/* Wait for a response from the server to the last HTTP request sent */
static bool http_await_response(void)
{
// Reset the Timer/Counter
TCNT3 = 0;
// Clear the Compare Match flag by writing a logical one to its location
TIFR3 |= (1 << HTTP_OCF);
// The flag is set again on compare match
while (!(TIFR3 & (1 << HTTP_OCF))) {
if ((UCSR1A & (1 << RXC1))) {
return true;
}
}
return false;
}
/* Send an HTTP request and process the response */
static bool http_send(void (*request)(void), bool (*action)(void)) {
do {
if (download.state == CHECKING_SOCKET) {
if (wifly_connect_to_host())
download.state = SENDING_REQUEST;
else
download.state = DOWNLOAD_CRITICAL_ERROR;
}
else if (download.state == SENDING_REQUEST) {
(*request)();
if (http_await_response())
download.state = RECEIVING_RESPONSE;
else
download.state = HTTP_ERROR;
}
else if (download.state == RECEIVING_RESPONSE) {
if (action == 0) {
download.state = CHECKING_SOCKET;
return true;
}
else if ((*action)()) {
download.state = CHECKING_SOCKET;
return true;
}
else
download.state = HTTP_ERROR;
}
else if (download.state == HTTP_ERROR) {
if (add_error(&download.errors.http, MAX_HTTP_ERRORS))
download.state = SENDING_REQUEST;
else
download.state = DOWNLOAD_CRITICAL_ERROR;
}
else if (download.state == DOWNLOAD_CRITICAL_ERROR) {
if (download.errors.critical >= MAX_DOWNLOAD_CRITICAL_ERRORS)
return false;
else {
++download.errors.critical;
download.errors.parse = 0;
download.errors.http = 0;
download.state = CHECKING_SOCKET;
}
}
} while (1);
}
/* Check if the next HEX line is complete in the HEX buffer */
static bool ihex_check_line(void)
{
// Make sure the HEX buffer contains at least:
// - the start code (1 byte)
// - the byte count (2 bytes)
// - the address (4 bytes)
// - the record type (2 bytes)
if (hex_chunk.size < hex_chunk.index + 9)
return false;
// Check the start code
if (hex_buffer[hex_chunk.index] != ':')
return false;
// Parse the byte count
line_byte_count = ihex_parse_byte(hex_buffer + hex_chunk.index + 1);
// Make sure the HEX buffer contains the rest of the line, including the
// the CRC (2 bytes) and the "\r\n"
if (hex_chunk.size < hex_chunk.index + 9 + (line_byte_count << 1))
return false;
// Parse the record type
uint16_t record_type = ihex_parse_byte(hex_buffer + hex_chunk.index + 7);
// Place the index at the beginning of the data frame
hex_chunk.index += 9;
// Skip the data of Extended Segment Address records
if (record_type == 0x02) {
line_byte_count = 0;
hex_chunk.index += 4;
}
return true;
}
/* Load one byte of binary content to the page buffer */
static bool ihex_load_byte(void)
{
if (line_byte_count == 0) {
// Skip the CRC and the "\r\n"
hex_chunk.index += 4;
return false;
}
else {
// Copy one byte of the HEX line content to the binary page buffer
bin_buffer[bin_page.index] =
ihex_parse_byte(hex_buffer + hex_chunk.index);
bin_page.index += 1;
hex_chunk.index += 2;
line_byte_count -= 1;
return true;
}
}
static uint8_t ihex_parse_byte(uint8_t* source)
{
uint8_t byte_value;
uint8_t* data;
uint8_t ch;
data = source;
// Convert first digit to first nibble
ch = *data++;
if (ch >= '0' && ch <= '9')
byte_value = ch - '0';
else if (ch >= 'A' && ch <= 'F')
byte_value = ch - 'A' + 10;
else {
return false;
}
byte_value <<= 4;
// Convert second digit to second nibble
ch = *data++;
if (ch >= '0' && ch <= '9')
byte_value += ch - '0';
else if (ch >= 'A' && ch <= 'F')
byte_value += ch - 'A' + 10;
else {
return false;
}
return byte_value;
}
/*
* Send a partial GET request to the server in order to receive the next page
* of the HEX file
*/
static void request_get_chunk(void)
{
// Compute the position of the next program page within the HEX file
hex_chunk.file_stop =
hex_chunk.file_start - hex_chunk.index+ HEX_BUFFER_SIZE - 1;
if (hex_chunk.file_stop >= hex_program_size)
hex_chunk.file_stop = hex_program_size - 1;
// Send HTTP GET range request
wifly_put_string("GET ");
wifly_put_string(PROGRAM_PATH);
wifly_put_string(HTTP_FIELDS);
wifly_put_string("Range: bytes=");
wifly_put_long(hex_chunk.file_start);
wifly_put_string("-");
wifly_put_long(hex_chunk.file_stop);
wifly_put_string(
"\r\n"
"\r\n"
);
}
/* Send a HEAD request about the HEX file to the server */
static void request_get_size(void)
{
wifly_put_string("HEAD ");
wifly_put_string(PROGRAM_PATH);
wifly_put_string(HTTP_FIELDS);
wifly_put_string("\r\n");
}
/* Send a request to check if a new program is available */
static void request_get_status(void)
{
wifly_put_string(CHECK_STATUS_REQUEST);
wifly_put_string(device_id);
wifly_put_string(HTTP_FIELDS);
wifly_put_string("\r\n");
}
/* Send a request to confirm that the program was successfully downloaded */
static void request_update_status(void)
{
wifly_put_string(CLEAR_STATUS_REQUEST);
wifly_put_string(device_id);
wifly_put_string(HTTP_FIELDS);
wifly_put_string("\r\n");
}
/* Send a byte response to the programmer */
static void stk_byte_response(uint8_t val)
{
if (stk_get_char() == STK_CRC_EOP) {
stk_put_char(STK_INSYNC);
stk_put_char(val);
stk_put_char(STK_OK);
}