-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootloader.c
More file actions
247 lines (210 loc) · 6.57 KB
/
bootloader.c
File metadata and controls
247 lines (210 loc) · 6.57 KB
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
#include <string.h>
#include <avr/boot.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include "drv/assert.h"
#include "drv/tlog.h"
#include "drv/watchdog.h"
#include "atmega328p/rtu_impl.h"
#include "rtu.h"
#include "rtu_cmd.h"
#include "fixed.h"
#ifndef EEPROM_ADDR_RTU_ADDR
#error "Please define EEPROM_ADDR_RTU_ADDR"
#endif
/*-----------------------------------------------------------------------------*/
static
void flash_page_erase(rtu_memory_fields_t *rtu_memory_fields)
{
boot_page_erase_safe(rtu_memory_fields->flash_page_addr);
}
static
void flash_page_fill(rtu_memory_fields_t *rtu_memory_fields)
{
uint16_t addr = rtu_memory_fields->flash_page_addr;
for(uint8_t i = 0; i < FLASH_PAGE_SIZE_IN_WORDS; ++i)
{
boot_page_fill_safe(addr, rtu_memory_fields->flash_page.words[i]);
addr += sizeof(uint16_t);
}
}
static
void flash_page_write(rtu_memory_fields_t *rtu_memory_fields)
{
boot_page_write_safe(rtu_memory_fields->flash_page_addr);
boot_spm_busy_wait();
}
/*-----------------------------------------------------------------------------*/
static
void handle_reboot(rtu_memory_fields_t *rtu_memory_fields)
{
if(!rtu_memory_fields->reboot) return;
rtu_memory_fields->reboot = 0;
fixed__.bootloader_reset_code.curr = RESET_CODE_REBOOT;
watchdog_enable(WATCHDOG_TIMEOUT_250ms);
sei(); /* USART0 async transmission in progress - Modbus reply */
for(;;) {/* wait until reset */}
}
static
void handle_watchdog(rtu_memory_fields_t *rtu_memory_fields)
{
if(rtu_memory_fields->watchdog_reset)
{
rtu_memory_fields->watchdog_reset = 0;
watchdog_reset();
}
if(rtu_memory_fields->watchdog_disable)
{
rtu_memory_fields->watchdog_disable = 0;
if(!rtu_memory_fields->watchdog_disabled)
{
watchdog_disable();
rtu_memory_fields->watchdog_disabled = 1;
}
}
}
static
void handle_flash(rtu_memory_fields_t *rtu_memory_fields)
{
if(!rtu_memory_fields->flash_page_update) return;
rtu_memory_fields->flash_page_update = 0;
if(rtu_memory_fields->flash_page_rnw)
{
memcpy_P(
rtu_memory_fields->flash_page.bytes,
(uint16_t *)rtu_memory_fields->flash_page_addr,
FLASH_PAGE_SIZE_IN_BYTES);
++rtu_memory_fields->flash_page_rd_num;
}
else
{
flash_page_fill(rtu_memory_fields);
flash_page_erase(rtu_memory_fields);
flash_page_write(rtu_memory_fields);
++rtu_memory_fields->flash_page_wr_num;
}
}
static
void handle_eeprom(rtu_memory_fields_t *rtu_memory_fields)
{
if(!rtu_memory_fields->eeprom_update) return;
rtu_memory_fields->eeprom_update = 0;
if(rtu_memory_fields->eeprom_rnw)
{
rtu_memory_fields->eeprom_data =
eeprom_read_byte(
(const uint8_t *)rtu_memory_fields->eeprom_addr);
++rtu_memory_fields->eeprom_rd_num;
}
else
{
eeprom_write_byte(
(uint8_t *)rtu_memory_fields->eeprom_addr,
rtu_memory_fields->eeprom_data);
++rtu_memory_fields->eeprom_wr_num;
}
}
static
void handle_rtu_state(modbus_rtu_state_t *state)
{
if(RTU_ERR_REBOOT_THREASHOLD > state->stats.err_cntr) return;
fixed__.bootloader_reset_code.curr = RESET_CODE_RTU_ERROR;
watchdog_enable(WATCHDOG_TIMEOUT_16ms);
for(;;) {/* wait until reset */}
}
static
void dispatch(
modbus_rtu_state_t *state,
rtu_memory_fields_t *rtu_memory_fields)
{
handle_rtu_state(state);
handle_reboot(rtu_memory_fields);
handle_watchdog(rtu_memory_fields);
handle_flash(rtu_memory_fields);
handle_eeprom(rtu_memory_fields);
}
/*-----------------------------------------------------------------------------*/
__attribute__((noreturn))
void exec_bootloader_code(void)
{
rtu_memory_fields_t rtu_memory_fields;
modbus_rtu_state_t state;
rtu_memory_fields_clear(&rtu_memory_fields);
rtu_memory_fields_init(&rtu_memory_fields);
rtu_memory_fields.mcusr = fixed__.mcusr;
TLOG_INIT(rtu_memory_fields.tlog, TLOG_SIZE);
TLOG_XPRINT2x8("MCUSR|RSTC", fixed__.mcusr, fixed__.reset_counter);
TLOG_XPRINT2x8("PNC|APPCNT", fixed__.panic_counter, fixed__.app_counter);
rtu_memory_fields.priv.self_addr =
eeprom_read_byte((const uint8_t *)EEPROM_ADDR_RTU_ADDR);
modbus_rtu_impl(
&state,
NULL /* suspend */, NULL /* resume */,
rtu_pdu_cb,
(uintptr_t)&rtu_memory_fields);
/* set SMCR SE (Sleep Enable bit) */
sleep_enable();
fixed__.bootloader_reset_code.curr = RESET_CODE_BOOTLOADER_IDLE;
watchdog_enable(WATCHDOG_TIMEOUT_8000ms);
for(;;)
{
cli(); // disable interrupts
modbus_rtu_event(&state);
const bool is_idle = modbus_rtu_idle(&state);
if(is_idle) dispatch(&state, &rtu_memory_fields);
sei(); // enabled interrupts
sleep_cpu();
}
}
__attribute__((noreturn))
void exec_app_code(void)
{
++fixed__.app_counter;
fixed__.app_reset_code.last = fixed__.app_reset_code.curr;
fixed__.app_reset_code.curr = RESET_CODE_APP_EXEC_FAILED;
watchdog_enable(WATCHDOG_TIMEOUT_16ms);
asm("jmp 0000");
for(;;) {}
}
__attribute__((noreturn))
void main(void)
{
fixed__.mcusr = MCUSR;
MCUSR &= ~M4(WDRF, BORF, EXTRF, PORF);
/* if System Reset was caused by watchdog - WDRE bit in MCUSR
* will re-enable watchdog - so watchdog must be disabled to
* avoid endless watchdog System Reset loops
* (because AVR does not have dedicated System Reset instruction
* watchdog is used for reset) */
watchdog_disable();
if(fixed__.mcusr & M1(PORF))
{
/* if power was lost SRAM state is undefined
* (memset/bzero not used because of volatile)*/
for(uint8_t i = 0; i < FIXED_SIZE; ++i) fixed__.bytes[i] = 0;
}
++fixed__.reset_counter;
fixed__.bootloader_reset_code.last = fixed__.bootloader_reset_code.curr;
/* jump to app code ONLY if reset was caused by watchdog and signature is
* matching */
if(
(fixed__.mcusr & M1(WDRF))
&& RESET_SIGNATURE_BOOT_APP == fixed__.reset_signature)
{
fixed__.reset_signature = (uint8_t)~RESET_SIGNATURE_BOOT_APP;
exec_app_code();
}
else
{
fixed__.reset_signature = RESET_SIGNATURE_BOOT_APP;
/* map interrupt vector table to bootloader flash */
{
MCUCR = M1(IVCE);
MCUCR = M1(IVSEL);
}
exec_bootloader_code();
}
}
/*-----------------------------------------------------------------------------*/