forked from Oitzu/mysensors-Arduino-attiny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMYSBootloader.c
338 lines (267 loc) · 9.12 KB
/
MYSBootloader.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
/*
MYSBootloader: OTA bootloader for Mysensor nodes (www.mysensors.org)
Original OTA bootloader code by ToSa
Optimized and extended by tekka
Version 1.1 / 20150314
Size: 2002 bytes
Tested with MYSController 0.1.2.276 (goo.gl/9DCWNo)
MCU: Atmega328p
bootsz: 1024W
fuses for ISP:
EX = 0xFE (use 0x06 for Arduino IDE, boards.txt)
HI = 0xDA
LO = 0xF7
nRF24L01+ connected to pins:
CE = 9
CSN = 10
Successfully tested with:
16Mhz extXTAL, 3.3V & 5V
8Mhz intRC, 3.3V & 5V
1Mhz intRC, 3.3 & 5V
128kHz intRC, 3.3V & 5V
* Version 1.1
- use eeprom_update instead of eeprom_write to reduce wear out
- bootloader commands: erase eeprom, set node id
- verify incoming FW blocks for type & address
- communicate over static parent (if set and found) else broadcast to find nearest node
- adjusted timings
* Version 1.0
Initial release
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include "MYSBootloader.h"
#define MYSBOOTLOADER_MAJVER 1
#define MYSBOOTLOADER_MINVER 1
#define MYSBOOTLOADER_VERSION (MYSBOOTLOADER_MINVER * 256 + MYSBOOTLOADER_MAJVER)
#define MAX_RESEND 5
// procedures and functions
static void programPage(uint16_t page, uint8_t *buf);
static uint16_t calcCRCrom (const void* ptr, uint16_t len);
static uint8_t IsFirmwareValid();
static void reboot();
static void startup();
static boolean sendWrite(MyMessage message);
static bool sendAndWait(uint8_t reqType, uint8_t resType);
int main(void) __attribute__ ((OS_main)) __attribute__ ((section (".init9")));
static void programPage(uint16_t page, uint8_t *buf) {
// these function calls use "out" commands: save some bytes and cycles :)
__boot_page_erase_short(page);
boot_spm_busy_wait();
for (uint16_t i = 0; i < SPM_PAGESIZE; i += 2) {
uint16_t data_word = *buf++;
data_word += (*buf++) << 8;
__boot_page_fill_short(page + i, data_word);
}
__boot_page_write_short(page);
boot_spm_busy_wait();
__boot_rww_enable_short();
}
static uint16_t calcCRCrom (const void* ptr, uint16_t len) {
// init 0xFFFF
uint16_t crc = ~0;
for (uint16_t i = 0; i < len; i++) {
crc = _crc16_update(crc, pgm_read_byte((uint16_t) ptr + i));
}
return crc;
}
static uint8_t IsFirmwareValid () {
return calcCRCrom(0, fc.blocks * FIRMWARE_BLOCK_SIZE) == fc.crc;
}
static void reboot() {
// wait for pending eeprom activities
eeprom_busy_wait();
// trigger watchdog ASAP
watchdogConfig(WATCHDOG_16MS);
// endless loop
while (1);
}
static void startup() {
if (IsFirmwareValid()) {
// WD off
watchdogConfig(WATCHDOG_OFF);
// run sketch
((void(*)()) 0)();
} else {
reboot();
}
}
static boolean sendWrite(MyMessage message) {
return write(nc.parentNodeId, message.array, HEADER_SIZE + mGetLength(message), (message.destination == BROADCAST_ADDRESS));
}
static bool sendAndWait(uint8_t reqType, uint8_t resType) {
outMsg.type = reqType;
// outer loop, retries
for (uint8_t i = 0; i < MAX_RESEND; i++) {
sendWrite(outMsg);
// loop 20 times, wait time 0.1s if no/wrong data => 2s
for (uint8_t j = 0; j < 20; j++) {
// loop 100 times, wait 1ms if no/wrong data => 0.1s
for (uint8_t k = 0; k < 100; k++) {
watchdogReset();
// Tx FIFO data available? (we don't care about the pipe here)
if (available(NULL)) {
// read message from FIFO, skip if size = 0
if (readMessage(inMsg.array) > 0) {
// protocol compatible? if not ignore msg
if ((mGetVersion(inMsg) != PROTOCOL_VERSION)) {
continue;
}
// msg for us?
if (inMsg.destination == nc.nodeId) {
// internal command: find parent
if ((mGetCommand(inMsg) == C_INTERNAL) && (inMsg.type == I_FIND_PARENT_RESPONSE)) {
// static parent found?
if (configuredParentID == inMsg.sender) {
configuredParentFound = true;
}
if ( ((inMsg.bValue < nc.distance - 1) && ( !configuredParentFound) ) || (configuredParentID == inMsg.sender)) {
// got new routing info, update settings
nc.distance = inMsg.bValue + 1;
nc.parentNodeId = inMsg.sender;
}
}
// did we receive expected reply?
if ((mGetCommand(inMsg) == mGetCommand(outMsg)) && (inMsg.type == resType)) {
return true;
}
}
}
} else {
// wait 1ms if no data available
_delay_ms(1);
}
}
}
}
return false;
}
// main start
int main(void) {
asm volatile ("clr __zero_reg__");
// reset MCU status register
MCUSR = 0;
// enable watchdog to avoid deadlock
watchdogConfig(WATCHDOG_8S);
// initialize SPI
SPIinit();
// initialize RF module
RFinit();
// Read node config from EEPROM, i.e. nodeId, parent nodeId, distance
eeprom_read_block((void*)&nc, (void*)EEPROM_NODE_ID_ADDRESS, sizeof(struct NodeConfig));
// Read firmware config from EEPROM, i.e. type, version, CRC, blocks
eeprom_read_block((void*)&fc, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS, sizeof(NodeFirmwareConfig));
// find nearest node during reboot: invalidate parent node settings, since we have to re-discover them for every single reboot
configuredParentID = nc.parentNodeId;
// nc.parentNodeId = 0xFF;
nc.distance = 0xFF;
// prepare for I_FIND_PARENTS
outMsg.sender = nc.nodeId;
outMsg.last = nc.nodeId;
outMsg.sensor = 0xFF;
outMsg.destination = BROADCAST_ADDRESS;
// set header
mSetVersion(outMsg, PROTOCOL_VERSION);
mSetLength(outMsg, 0);
mSetCommand(outMsg, C_INTERNAL);
mSetAck(outMsg,false);
mSetPayloadType(outMsg, P_STRING);
// set reading & writing pipe address
setAddress(nc.nodeId);
// network up? get neighbors, else startup
if (!sendAndWait(I_FIND_PARENT, I_FIND_PARENT_RESPONSE)) {
startup();
}
// all messages to gateway
outMsg.destination = GATEWAY_ADDRESS;
// if no node id assigned, request new id
if (nc.nodeId == AUTO) {
// listen to broadcast
openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(BROADCAST_ADDRESS));
if (sendAndWait(I_ID_REQUEST, I_ID_RESPONSE)) {
// save id to eeprom
eeprom_update_byte((uint8_t*)EEPROM_NODE_ID_ADDRESS, atoi(inMsg.data));
}
// we could go on and set everything right here, but rebooting will take care of that - and saves some bytes :)
reboot();
}
// wuff
watchdogReset();
// prepare for FW config request
RequestFirmwareConfig *reqFWConfig = (RequestFirmwareConfig *)outMsg.data;
mSetLength(outMsg, sizeof(RequestFirmwareConfig));
mSetCommand(outMsg, C_STREAM);
mSetPayloadType(outMsg,P_CUSTOM);
// copy node settings to reqFWConfig
memcpy(reqFWConfig,&fc,sizeof(NodeFirmwareConfig));
// add bootloader information
reqFWConfig->BLVersion = MYSBOOTLOADER_VERSION;
// send node config and request FW config from controller
if (!sendAndWait(ST_FIRMWARE_CONFIG_REQUEST, ST_FIRMWARE_CONFIG_RESPONSE)) {
startup();
}
NodeFirmwareConfig *firmwareConfigResponse = (NodeFirmwareConfig *)inMsg.data;
// bootloader commands
if (firmwareConfigResponse->blocks == 0) {
// verify flag
if (firmwareConfigResponse->crc == 0xDA7A){
// cmd 0x01 clear eeprom
if(firmwareConfigResponse->bl_command == 0x01) {
for(uint16_t i = 0; i < EEPROM_SIZE; i++) eeprom_update_byte((uint8_t *)i,0xFF);
} else
// cmd 0x02 set id
if(firmwareConfigResponse->bl_command == 0x02) {
eeprom_update_byte((uint8_t*)EEPROM_NODE_ID_ADDRESS, (uint8_t)firmwareConfigResponse->bl_data);
}
}
// final step
reboot();
}
// compare with current node configuration, if equal startup
if (!memcmp(&fc,firmwareConfigResponse,sizeof(NodeFirmwareConfig))) {
startup();
}
// *********** from here on we will fetch new FW
// invalidate current CRC
fc.crc = 0xFFFF;
// write fetched type and version in case OTA fails (BL will reboot and re-request FW with stored settings)
eeprom_update_block(&fc, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS,sizeof(NodeFirmwareConfig));
// copy new FW config
memcpy(&fc,firmwareConfigResponse,sizeof(NodeFirmwareConfig));
RequestFWBlock *firmwareRequest = (RequestFWBlock *)outMsg.data;
mSetLength(outMsg, sizeof(RequestFWBlock));
firmwareRequest->type = fc.type;
firmwareRequest->version = fc.version;
// request FW from controller, load FW counting backwards
uint16_t block = fc.blocks;
do {
firmwareRequest->block = block - 1;
// request FW block
if (!sendAndWait(ST_FIRMWARE_REQUEST, ST_FIRMWARE_RESPONSE)) {
reboot();
}
ReplyFWBlock *firmwareResponse = (ReplyFWBlock *)inMsg.data;
// did we receive requested block?
if (!memcmp(firmwareRequest,firmwareResponse,sizeof(RequestFWBlock))) {
// calculate page offset
uint8_t offset = ((block - 1) * FIRMWARE_BLOCK_SIZE) % SPM_PAGESIZE;
// write to buffer
memcpy(progBuf + offset, firmwareResponse->data, FIRMWARE_BLOCK_SIZE);
// program if page full
if (offset == 0) {
programPage(((block - 1) * FIRMWARE_BLOCK_SIZE), progBuf);
}
block--;
}
} while (block);
// wuff
watchdogReset();
// all blocks transmitted, calc CRC and write to eeprom if valid
if (IsFirmwareValid()) {
// if FW is valid, write settings to eeprom
eeprom_update_block(&fc, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS, sizeof(NodeFirmwareConfig));
}
// final step
reboot();
}