-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
executable file
·137 lines (124 loc) · 3.54 KB
/
main.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
#include <avr/eeprom.h>
#include <util/delay.h>
#include "gsm.h"
#include "eeprom_addr.h"
#include "watchdog.h"
#include "debug.h"
int main(void) __attribute__ ((section (".init9")));
void appStart(void) __attribute__ ((naked));
int main(void){
watchdogDisable();
uint8_t ch;
DDRD &= ~(_BV(PD7));
PORTD |= (1 << PD7);
/* This code makes the following assumptions:
* No interrupts will execute
* SP points to RAMEND
* r1 contains zero
* If not, uncomment the following instructions. */
// cli();
asm volatile("clr __zero_reg__");
#if defined(__AVR_ATmega8__)
SP = RAMEND; // This is done by hardware reset
#endif
#if !defined(__AVR_ATmega16__)
ch = MCUSR;
MCUSR = 0;
#else
ch = MCUCSR;
MCUCSR = 0;
#endif
uart_init();
DEBUG(uart_puts("\r\n-- Bootloader Starts --\r\n"));
if(ch & _BV(WDRF)){
watchdogDisable();
DEBUG(uart_puts("Watchdog Reset\r\n"));
if(eeprom_read_byte(OTA_INIT_SIG_ADDR) == OTA_START_SIG){
DEBUG(uart_puts("OTA Signal Found\r\n"));
if(eeprom_read_byte(OTA_ATTEMPTED_ADDR) == OTA_ATTEMPTED_SIG){
DEBUG(uart_puts("Optiboot Attempted Before\r\n"));
eeprom_write_byte(OTA_ATTEMPTED_ADDR, 0xFF);
DEBUG(uart_puts("Starting Application\r\n"));
appStart();
}
else{
eeprom_write_byte(OTA_ATTEMPTED_ADDR, OTA_ATTEMPTED_SIG);
if(eeprom_read_byte(OTA_STAUS_ADDR) == OTA_COMPLETED){
eeprom_write_byte(OTA_STAUS_ADDR, 0xFF);
}
DEBUG(uart_puts("Attempting Optiboot\r\n"));
DEBUG(uart_puts("GSM Loop\r\n"));
gsm_loop();
watchdogConfig(WATCHDOG_1S);
}
}
else{ // on wdt
DEBUG(uart_puts("OTA Signal Not Found\r\n"));
DEBUG(uart_puts("Starting Application\r\n"));
appStart();
}
}
else if(ch & _BV(BORF)){
watchdogConfig(WATCHDOG_1S);
DEBUG(uart_puts("Brown Out Reset\r\n"));
}
else if(ch & _BV(EXTRF)){
watchdogConfig(WATCHDOG_1S);
DEBUG(uart_puts("External Reset\r\n"));
DEBUG(uart_puts("Starting OTA\r\n"));
if(eeprom_read_byte(OTA_INIT_SIG_ADDR) == OTA_START_SIG){
DEBUG(uart_puts("OTA Signal Found\r\n"));
if(bit_is_clear(PIND, PD7)) {
while(bit_is_clear(PIND, PD7));
uart_puts("-- Clearing GSM OTA Flag --");
eeprom_write_byte(OTA_INIT_SIG_ADDR, 0xFF);
watchdogConfig(WATCHDOG_16MS);
_delay_ms(1000);
}
if(eeprom_read_byte(OTA_ATTEMPTED_ADDR) == OTA_ATTEMPTED_SIG){
DEBUG(uart_puts("Optiboot Attempted Before\r\n"));
eeprom_write_byte(OTA_ATTEMPTED_ADDR, 0xFF);
DEBUG(uart_puts("Starting Application\r\n"));
appStart();
}
else{
eeprom_write_byte(OTA_ATTEMPTED_ADDR, OTA_ATTEMPTED_SIG);
if(eeprom_read_byte(OTA_STAUS_ADDR) == OTA_COMPLETED){
eeprom_write_byte(OTA_STAUS_ADDR, 0xFF);
}
DEBUG(uart_puts("Attempting Optiboot\r\n"));
DEBUG(uart_puts("GSM Loop\r\n"));
gsm_loop();
watchdogConfig(WATCHDOG_1S);
}
}
else{ // on wdt
DEBUG(uart_puts("OTA Signal Not Found\r\n"));
DEBUG(uart_puts("Starting Application\r\n"));
appStart();
}
}
else if(ch & _BV(PORF)){
watchdogConfig(WATCHDOG_1S);
DEBUG(uart_puts("Power Reset\r\n"));
}
else{
// watchdogConfig(WATCHDOG_1S);
DEBUG(uart_puts("Invalid Firmware\r\n"));
}
// DEBUG(uart_puts("-- Optiboot Starts Now --\r\n"));
optiboot();
DEBUG(uart_puts("Bootloader Ends Here. Starting App\r\n"));
/* Exit to user application */
appStart();
DEBUG(uart_puts("Main Exited\r\n"));
return(0); //never reached
}
void appStart(void) {
watchdogDisable();
asm volatile(
"clr r30 \n\t"
"clr r31 \n\t"
"ijmp \n\t"
);
}