-
-
Notifications
You must be signed in to change notification settings - Fork 726
/
Copy pathSDUBoot.ino
80 lines (58 loc) · 2.19 KB
/
SDUBoot.ino
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
/*
Copyright (c) 2017 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Board.h"
#include <SD.h>
#include <FlashStorage.h>
FlashClass flash;
// Initialize C library
extern "C" void __libc_init_array(void);
int main() {
init();
__libc_init_array();
delay(1);
if (SD.begin(SDCARD_SS_PIN) && SD.exists(UPDATE_FILE)) {
File updateFile = SD.open(UPDATE_FILE);
uint32_t updateSize = updateFile.size();
bool updateFlashed = false;
if (updateSize > SDU_SIZE) {
// skip the SDU section
updateFile.seek(SDU_SIZE);
updateSize -= SDU_SIZE;
uint32_t flashAddress = (uint32_t)SKETCH_START;
// erase the pages
flash.erase((void*)flashAddress, updateSize);
uint8_t buffer[512];
// write the pages
for (uint32_t i = 0; i < updateSize; i += sizeof(buffer)) {
updateFile.read(buffer, sizeof(buffer));
flash.write((void*)flashAddress, buffer, sizeof(buffer));
flashAddress += sizeof(buffer);
}
updateFlashed = true;
}
updateFile.close();
if (updateFlashed) {
SD.remove(UPDATE_FILE);
}
}
// jump to the sketch
__set_MSP(*SKETCH_START);
//Reset vector table address
SCB->VTOR = ((uint32_t)(SKETCH_START) & SCB_VTOR_TBLOFF_Msk);
// address of Reset_Handler is written by the linker at the beginning of the .text section (see linker script)
uint32_t resetHandlerAddress = (uint32_t) * (SKETCH_START + 1);
// jump to reset handler
asm("bx %0"::"r"(resetHandlerAddress));
}