Skip to content

Commit 13b6e68

Browse files
committed
Add SBU for MKRNB1500 - initial commit
1 parent 3f464a0 commit 13b6e68

File tree

9 files changed

+2947
-0
lines changed

9 files changed

+2947
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <MKRNB.h>
2+
#include <SBU.h>
3+
4+
static char const BINARY[] =
5+
{
6+
#include "Binary.h"
7+
};
8+
9+
static char const CHECK_FILE[] =
10+
{
11+
"OK"
12+
};
13+
14+
static constexpr char CHECK_FILE_NAME[] = "UPDATE.OK";
15+
16+
NBFileUtils fileUtils;
17+
bool update_available = false;
18+
19+
void setup() {
20+
Serial.begin(9600);
21+
22+
unsigned long const start = millis();
23+
for(unsigned long now = millis(); !Serial && ((now - start) < 5000); now = millis()) { };
24+
25+
Serial.print("Accessing SARA Filesystem... ");
26+
if(!fileUtils.begin()) {
27+
Serial.println("failed.");
28+
return;
29+
30+
}
31+
Serial.println("OK");
32+
Serial.print("Writing \"UPDATE.BIN\" ... ");
33+
34+
uint32_t bytes_to_write = sizeof(BINARY);
35+
Serial.print("Size of BINARY: ");
36+
Serial.println(bytes_to_write);
37+
int index = 0;
38+
bool append = false;
39+
int new_bytes = 0;
40+
//int bytes_written = 0;
41+
42+
for (int i=0; i<(bytes_to_write/512); i++) {
43+
auto new_bytes = fileUtils.downloadFile("UPDATE.BIN", BINARY+index, 512, append);
44+
if (new_bytes != 512) {
45+
Serial.print("New_bytes = ");
46+
Serial.print(new_bytes);
47+
Serial.println(" != 512");
48+
}
49+
index = index + new_bytes;
50+
append = true;
51+
}
52+
if ((bytes_to_write%512)!=0) {
53+
auto new_bytes = fileUtils.downloadFile("UPDATE.BIN", BINARY+index, bytes_to_write%512, append);
54+
if (new_bytes != bytes_to_write%512) {
55+
Serial.print("Last bytes read = ");
56+
Serial.print(new_bytes);
57+
Serial.print(". They should have been ");
58+
Serial.println(bytes_to_write%512);
59+
}
60+
index = index + new_bytes;
61+
}
62+
63+
if(index != bytes_to_write) {
64+
Serial.print("Written only ");
65+
Serial.println(index); //bytes_written
66+
Serial.print(bytes_to_write);
67+
Serial.println(" should have been written. System is restarting...");
68+
delay(100);
69+
NVIC_SystemReset();
70+
71+
} else {
72+
Serial.print("Download complete! ");
73+
Serial.print(index);
74+
Serial.println(" bytes written");
75+
76+
auto status = 0;
77+
while (status != 2) {
78+
status = fileUtils.createFile(CHECK_FILE_NAME, CHECK_FILE, 2);
79+
delay(100);
80+
}
81+
82+
Serial.println("Please type \"restart\" to apply the update");
83+
update_available = true;
84+
}
85+
86+
}
87+
88+
void loop() {
89+
if (update_available == true) {
90+
String command = Serial.readStringUntil('\n');
91+
if (command.indexOf("restart") >= 0) {
92+
NVIC_SystemReset();
93+
}
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Usage
3+
This example demonstrates how to use the SAMD SBU library to update a
4+
sketch on any Arduino MKR board via the storage on the SARA-R410M module.
5+
This sketch prints out the current date and time.
6+
Steps to update sketch:
7+
1) Upload this sketch or another sketch that includes the SBU library
8+
2) Update the sketch as desired. For this example the sketch prints out
9+
the compiled date and time.
10+
3) In the IDE select: Sketch -> Export compiled Binary
11+
4) Open the location of the sketch and convert the .bin file to a C byte array.
12+
cat SKETCH.bin | xxd --include > Binary.h
13+
5) Copy Binary.h file from the sketch's folder to the SBU_LoadBinary sketch
14+
and load it to the SARA-R410M via SBU_LoadBinary sketch.
15+
*/
16+
17+
/*
18+
Include the SBU library
19+
20+
This will add some code to the sketch before setup() is called
21+
to check if UPDATE.BIN and UPDATE.OK are present on the storage of
22+
the SARA-R410M module. If this check is positive UPDATE.BIN is used to update
23+
the sketch running on the board.
24+
After this UPDATE.BIN and UPDATE.OK are deleted from the flash.
25+
*/
26+
27+
28+
#include <SBU.h>
29+
30+
void setup()
31+
{
32+
Serial.begin(9600);
33+
while (!Serial) { }
34+
// wait a bit
35+
delay(1000);
36+
String message;
37+
message += "Sketch compile date and time: ";
38+
message += __DATE__;
39+
message += " ";
40+
message += __TIME__;
41+
// print out the sketch compile date and time on the serial port
42+
Serial.println(message);
43+
}
44+
45+
void loop()
46+
{
47+
// add you own code here
48+
}
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright (c) 2020 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 <FlashStorage.h>
20+
#include <MKRNB.h>
21+
22+
#define SBU_START 0x2000
23+
#define SBU_SIZE 0x8000
24+
25+
#define SKETCH_START (uint32_t*)(SBU_START + SBU_SIZE)
26+
27+
static constexpr char UPDATE_FILE_NAME[] = "UPDATE.BIN";
28+
static constexpr char CHECK_FILE_NAME[] = "UPDATE.OK";
29+
30+
FlashClass mcu_flash;
31+
32+
NBFileUtils fileUtils;
33+
34+
extern "C" void __libc_init_array(void);
35+
36+
int main()
37+
{
38+
init();
39+
40+
__libc_init_array();
41+
42+
delay(1);
43+
44+
constexpr size_t blockSize = 512;
45+
fileUtils.begin();
46+
Serial1.begin(115200);
47+
Serial1.print("SBU start. ");
48+
49+
bool update_success = false;
50+
51+
// Try to update only if update file
52+
// has been download successfully.
53+
54+
if (fileUtils.listFile(CHECK_FILE_NAME)) {
55+
Serial1.println("Update file exists");
56+
uint32_t updateSize = fileUtils.listFile(UPDATE_FILE_NAME);
57+
size_t cycles = (updateSize / blockSize) + 1;
58+
59+
if (updateSize > SBU_SIZE) {
60+
updateSize = updateSize - SBU_SIZE - SBU_START;
61+
/* Erase the MCU flash */
62+
uint32_t flash_address = (uint32_t)SKETCH_START;
63+
mcu_flash.erase((void*)flash_address, updateSize);
64+
65+
for (auto i = 0; i < cycles; i++) {
66+
uint8_t block[blockSize] { 0 };
67+
digitalWrite(LED_BUILTIN, LOW);
68+
uint32_t read_bytes = fileUtils.readBlock(UPDATE_FILE_NAME, (i * blockSize) + SBU_SIZE + SBU_START, blockSize, block);
69+
digitalWrite(LED_BUILTIN, HIGH);
70+
mcu_flash.write((void*)flash_address, block, read_bytes);
71+
flash_address += read_bytes;
72+
}
73+
update_success = true;
74+
}
75+
if (update_success) {
76+
fileUtils.deleteFile(UPDATE_FILE_NAME);
77+
fileUtils.deleteFile(CHECK_FILE_NAME);
78+
}
79+
}
80+
else {
81+
Serial1.println("Update file does not exist");
82+
delay(100);
83+
}
84+
85+
boot:
86+
/* Jump to the sketch */
87+
__set_MSP(*SKETCH_START);
88+
89+
/* Reset vector table address */
90+
SCB->VTOR = ((uint32_t)(SKETCH_START) & SCB_VTOR_TBLOFF_Msk);
91+
92+
/* Address of Reset_Handler is written by the linker at the beginning of the .text section (see linker script) */
93+
uint32_t resetHandlerAddress = (uint32_t) * (SKETCH_START + 1);
94+
/* Jump to reset handler */
95+
asm("bx %0"::"r"(resetHandlerAddress));
96+
}

libraries/SBU/extras/SBUBoot/build.sh

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

libraries/SBU/keywords.txt

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

libraries/SBU/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SBU
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=Update the sketch on your Arduino MKRNB1500 from SARA-R410M flash.
6+
paragraph=
7+
category=Other
8+
url=
9+
architectures=samd

libraries/SBU/src/SBU.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright (c) 2020 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 "SBU.h"
22+
23+
__attribute__ ((section(".sketch_boot")))
24+
unsigned char SBU_BOOT[0x8000] = {
25+
#if defined(ARDUINO_SAMD_MKRNB1500)
26+
#include "boot/mkrnb1500.h"
27+
#else
28+
#error "Unsupported board!"
29+
#endif
30+
};

libraries/SBU/src/SBU.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright (c) 2020 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 _SBU_H_INCLUDED
20+
#define _SBU_H_INCLUDED
21+
22+
/* Nothing to do */
23+
24+
#endif /* _SBU_H_INCLUDED */

0 commit comments

Comments
 (0)