Skip to content

Commit a15d190

Browse files
committed
Add support for updating sketches from an SD card
Via 2nd stage boot code that checks SD card for UPDATE.bin
1 parent 96745cd commit a15d190

File tree

16 files changed

+3776
-3
lines changed

16 files changed

+3776
-3
lines changed

cores/arduino/SERCOM.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,13 @@ void SERCOM::enableSPI()
240240

241241
void SERCOM::disableSPI()
242242
{
243-
//Setting the enable bit to 0
244-
sercom->SPI.CTRLA.bit.ENABLE = 0;
245-
246243
while(sercom->SPI.SYNCBUSY.bit.ENABLE)
247244
{
248245
//Waiting then enable bit from SYNCBUSY is equal to 0;
249246
}
247+
248+
//Setting the enable bit to 0
249+
sercom->SPI.CTRLA.bit.ENABLE = 0;
250250
}
251251

252252
void SERCOM::setDataOrderSPI(SercomDataOrder dataOrder)

extras/package_index.json.Hourly.template

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
"packager": "arduino",
4949
"name": "CMSIS-Atmel",
5050
"version": "1.1.0"
51+
},
52+
{
53+
"packager": "arduino",
54+
"name": "arduinoOTA",
55+
"version": "1.1.1"
5156
}
5257
]
5358
}

extras/package_index.json.PR.template

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
"packager": "arduino",
4949
"name": "CMSIS-Atmel",
5050
"version": "1.1.0"
51+
},
52+
{
53+
"packager": "arduino",
54+
"name": "arduinoOTA",
55+
"version": "1.1.1"
5156
}
5257
]
5358
}
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright (c) 2017 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include <SD.h>
20+
#include <FlashStorage.h>
21+
22+
#define SDU_START 0x2000
23+
#define SDU_SIZE 0x4000
24+
25+
#define SKETCH_START (uint32_t*)(SDU_START + SDU_SIZE)
26+
27+
#ifndef SDCARD_SS_PIN
28+
#define SDCARD_SS_PIN 4
29+
#endif
30+
31+
#define UPDATE_FILE "UPDATE.BIN"
32+
33+
FlashClass flash;
34+
35+
// Initialize C library
36+
extern "C" void __libc_init_array(void);
37+
38+
int main() {
39+
init();
40+
41+
__libc_init_array();
42+
43+
delay(1);
44+
45+
if (SD.begin(SDCARD_SS_PIN) && SD.exists(UPDATE_FILE)) {
46+
File updateFile = SD.open(UPDATE_FILE);
47+
uint32_t updateSize = updateFile.size();
48+
bool updateFlashed = false;
49+
50+
if (updateSize > SDU_SIZE) {
51+
// skip the SDU section
52+
updateFile.seek(SDU_SIZE);
53+
updateSize -= SDU_SIZE;
54+
55+
uint32_t flashAddress = (uint32_t)SKETCH_START;
56+
57+
// erase the pages
58+
flash.erase((void*)flashAddress, updateSize);
59+
60+
uint8_t buffer[512];
61+
62+
// write the pages
63+
for (uint32_t i = 0; i < updateSize; i += sizeof(buffer)) {
64+
updateFile.read(buffer, sizeof(buffer));
65+
66+
flash.write((void*)flashAddress, buffer, sizeof(buffer));
67+
68+
flashAddress += sizeof(buffer);
69+
}
70+
71+
updateFlashed = true;
72+
}
73+
74+
updateFile.close();
75+
76+
if (updateFlashed) {
77+
SD.remove(UPDATE_FILE);
78+
}
79+
}
80+
81+
// jump to the sketch
82+
__set_MSP(*SKETCH_START);
83+
84+
//Reset vector table address
85+
SCB->VTOR = ((uint32_t)(SKETCH_START) & SCB_VTOR_TBLOFF_Msk);
86+
87+
// address of Reset_Handler is written by the linker at the beginning of the .text section (see linker script)
88+
uint32_t resetHandlerAddress = (uint32_t) * (SKETCH_START + 1);
89+
// jump to reset handler
90+
asm("bx %0"::"r"(resetHandlerAddress));
91+
}
92+

libraries/SDU/extras/SDUBoot/build.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh -x
2+
3+
ARDUINO=arduino
4+
SKETCH_NAME="SDUBoot.ino"
5+
SKETCH="$PWD/$SKETCH_NAME"
6+
BUILD_PATH="$PWD/build"
7+
OUTPUT_PATH="../../src/boot"
8+
9+
if [[ "$OSTYPE" == "darwin"* ]]; then
10+
ARDUINO="/Applications/Arduino.app/Contents/MacOS/Arduino"
11+
fi
12+
13+
buildSDUBootSketch() {
14+
BOARD=$1
15+
DESTINATION=$2
16+
17+
$ARDUINO --verify --board $BOARD --preserve-temp-files --pref build.path="$BUILD_PATH" $SKETCH
18+
cat "$BUILD_PATH/$SKETCH_NAME.bin" | xxd -i > $DESTINATION
19+
rm -rf "$BUILD_PATH"
20+
}
21+
22+
mkdir -p "$OUTPUT_PATH"
23+
24+
buildSDUBootSketch "arduino:samd:arduino_zero_edbg" "$OUTPUT_PATH/zero.h"
25+
buildSDUBootSketch "arduino:samd:mkr1000" "$OUTPUT_PATH/mkr1000.h"
26+
buildSDUBootSketch "arduino:samd:mkrzero" "$OUTPUT_PATH/mkrzero.h"

libraries/SDU/keywords.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#######################################
2+
# Syntax Coloring Map For SDU
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
SDU KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
reset KEYWORD2
16+
17+
#######################################
18+
# Constants (LITERAL1)
19+
#######################################

libraries/SDU/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SDU
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=Update the sketch on your board from an SD card
6+
paragraph=Requires an SD card
7+
category=Other
8+
url=http://www.arduino.cc/en/Reference/SDU
9+
architectures=samd

libraries/SDU/src/SDU.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright (c) 2017 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include <Arduino.h>
20+
21+
#include "SDU.h"
22+
23+
__attribute__ ((section(".sketch_boot")))
24+
unsigned char sduBoot[0x4000] = {
25+
#if defined(ARDUINO_SAMD_ZERO)
26+
#include "boot/zero.h"
27+
#elif defined(ARDUINO_SAMD_MKR1000)
28+
#include "boot/mkr1000.h"
29+
#elif defined(ARDUINO_SAMD_MKRZERO)
30+
#include "boot/mkrzero.h"
31+
#else
32+
#error "Unsupported board!"
33+
#endif
34+
};
35+
36+
SDUClass::SDUClass() {
37+
}
38+
39+
void SDUClass::reset() {
40+
// Reset the device
41+
NVIC_SystemReset() ;
42+
43+
while (true);
44+
}
45+
46+
SDUClass SDU;

libraries/SDU/src/SDU.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright (c) 2017 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef _SDU_H_INCLUDED
20+
#define _SDU_H_INCLUDED
21+
22+
class SDUClass {
23+
public:
24+
SDUClass();
25+
26+
void reset();
27+
};
28+
29+
extern SDUClass SDU;
30+
31+
#endif

0 commit comments

Comments
 (0)