Skip to content

Commit 761e1e6

Browse files
authored
Merge pull request #512 from arduino/SNU
SNU - Second Stage Bootloader for WiFi NINA enabled boards
2 parents 44d354b + 509cb77 commit 761e1e6

13 files changed

+3739
-2
lines changed

boards.txt

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ mkrwifi1010.build.vid=0x2341
175175
mkrwifi1010.build.pid=0x8054
176176
mkrwifi1010.bootloader.tool=openocd
177177
mkrwifi1010.bootloader.file=mkrwifi1010/samd21_sam_ba_arduino_mkrwifi1010.bin
178+
mkrwifi1010.arduinoota.extraflags=-d
178179

179180
# Arduino NANO 33 IoT
180181
# --------------------
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Usage
3+
This example demonstrates how to use the SAMD SNU library to update a
4+
sketch on an Arduino MKRWiFi1010 board using the board and nothing else.
5+
It prints out the date and time the sketch was compiled at
6+
to both Serial and Serial1.
7+
8+
Arduino MKRWiFi1010 board
9+
10+
Steps to update sketch via NINA WiFi/BT module:
11+
12+
1) Upload this sketch or another sketch that includes the SNU library
13+
via #include <SNU.h>
14+
15+
2) Update the sketch as desired. For this example the sketch prints out
16+
the compiled date and time so no updates are needed.
17+
18+
3) In the IDE select: Sketch -> Export compiled Binary
19+
20+
4) Use WiFiStorage.download(url, "UPDATE.BIN") function to download the
21+
new binary from a remote webserver.
22+
23+
5) Reboot the board; the update will be applied seamlessly
24+
25+
created 14 December 2018
26+
by Martino Facchin
27+
*/
28+
29+
/*
30+
Include the SNU library
31+
32+
This will add some code to the sketch before setup() is called
33+
to check if the WiFi module is present and UPDATE.bin exists.
34+
35+
If UPDATE.bin is present, the file is used to update the sketch
36+
running on the board. After this UPDATE.bin is deleted from NINA memory.
37+
*/
38+
#include <SNU.h>
39+
#include <WiFiNINA.h>
40+
41+
#include "arduino_secrets.h"
42+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
43+
/////// Wifi Settings ///////
44+
char ssid[] = SECRET_SSID; // your network SSID (name)
45+
char pass[] = SECRET_PASS; // your network password
46+
char url[] = SECRET_OTA_URL;
47+
48+
int status = WL_IDLE_STATUS;
49+
50+
String message;
51+
52+
void setup() {
53+
Serial.begin(9600);
54+
Serial1.begin(9600);
55+
56+
// check for the presence of the shield:
57+
if (WiFi.status() == WL_NO_SHIELD) {
58+
Serial.println("WiFi shield not present");
59+
// don't continue:
60+
while (true);
61+
}
62+
63+
// attempt to connect to Wifi network:
64+
while ( status != WL_CONNECTED) {
65+
Serial.print("Attempting to connect to SSID: ");
66+
Serial.println(ssid);
67+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
68+
status = WiFi.begin(ssid, pass);
69+
}
70+
71+
// wait a bit
72+
delay(1000);
73+
74+
message += "Sketch compile date and time: ";
75+
message += __DATE__;
76+
message += " ";
77+
message += __TIME__;
78+
79+
// print out the sketch compile date and time on the serial port
80+
Serial.println(message);
81+
Serial1.println(message);
82+
83+
Serial.println("Type \"download\" in the Serial Monitor to start downloading the update");
84+
}
85+
86+
void loop() {
87+
String command = Serial.readStringUntil('\n');
88+
89+
if (command.indexOf("download") >= 0) {
90+
91+
Serial.println("Downloading update file");
92+
WiFiStorage.download(url, "UPDATE.BIN");
93+
94+
WiFiStorageFile update = WiFiStorage.open("/fs/UPDATE.BIN");
95+
if (update.available()) {
96+
Serial.println("Download complete, please restart or type \"restart\" to apply the update");
97+
Serial.println("Filesize: " + String(update.available()));
98+
} else {
99+
Serial.println("Download failed, please retry :(");
100+
}
101+
}
102+
103+
if (command.indexOf("restart") >= 0) {
104+
NVIC_SystemReset();
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
3+
#define SECRET_OTA_URL "http://downloads.arduino.cc/misc/WiFi1010_blinkRBG.bin"
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 <WiFiNINA.h>
20+
#include <FlashStorage.h>
21+
22+
#ifdef ARDUINO_SAMD_MKRVIDOR4000
23+
#include <VidorPeripherals.h>
24+
#endif /* ARDUINO_SAMD_MKRVIDOR4000 */
25+
26+
#ifdef ARDUINO_SAMD_MKRVIDOR4000
27+
#define NINA_GPIO0 FPGA_NINA_GPIO0
28+
#define NINA_RESETN FPGA_SPIWIFI_RESET
29+
#endif /* ARDUINO_SAMD_MKRVIDOR4000 */
30+
31+
#define SDU_START 0x2000
32+
#define SDU_SIZE 0x4000
33+
34+
#define SKETCH_START (uint32_t*)(SDU_START + SDU_SIZE)
35+
36+
#define UPDATE_FILE "/fs/UPDATE.BIN"
37+
38+
FlashClass flash;
39+
40+
// Initialize C library
41+
extern "C" void __libc_init_array(void);
42+
43+
int main() {
44+
init();
45+
46+
__libc_init_array();
47+
48+
delay(1);
49+
50+
#if defined(ARDUINO_SAMD_MKRVIDOR4000)
51+
FPGA.begin();
52+
/* NINA select SPI mode and enable (by setting RESETN = '1') */
53+
FPGA.pinMode (NINA_GPIO0, OUTPUT);
54+
FPGA.digitalWrite(NINA_GPIO0, HIGH);
55+
FPGA.pinMode (NINA_RESETN, OUTPUT);
56+
FPGA.digitalWrite(NINA_RESETN, HIGH);
57+
#elif defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT)
58+
/* NINA select SPI mode and enable (by setting RESETN = '1') */
59+
pinMode (NINA_GPIO0, OUTPUT);
60+
digitalWrite(NINA_GPIO0, HIGH);
61+
pinMode (NINA_RESETN, OUTPUT);
62+
digitalWrite(NINA_RESETN, HIGH);
63+
#endif
64+
65+
if (WiFi.status() == WL_NO_SHIELD) {
66+
goto boot;
67+
}
68+
69+
if (WiFiStorage.exists(UPDATE_FILE)) {
70+
71+
WiFiStorageFile updateFile = WiFiStorage.open(UPDATE_FILE);
72+
uint32_t updateSize = updateFile.size();
73+
bool updateFlashed = false;
74+
75+
if (updateSize > SDU_SIZE) {
76+
// skip the SDU section
77+
updateFile.seek(SDU_SIZE);
78+
updateSize -= SDU_SIZE;
79+
80+
uint32_t flashAddress = (uint32_t)SKETCH_START;
81+
82+
// erase the pages
83+
flash.erase((void*)flashAddress, updateSize);
84+
85+
uint8_t buffer[128];
86+
87+
// write the pages
88+
for (uint32_t i = 0; i < updateSize; i += sizeof(buffer)) {
89+
updateFile.read(buffer, sizeof(buffer));
90+
91+
flash.write((void*)flashAddress, buffer, sizeof(buffer));
92+
93+
flashAddress += sizeof(buffer);
94+
}
95+
96+
updateFlashed = true;
97+
}
98+
99+
updateFile.close();
100+
101+
if (updateFlashed) {
102+
updateFile.erase();
103+
}
104+
}
105+
106+
boot:
107+
// jump to the sketch
108+
__set_MSP(*SKETCH_START);
109+
110+
//Reset vector table address
111+
SCB->VTOR = ((uint32_t)(SKETCH_START) & SCB_VTOR_TBLOFF_Msk);
112+
113+
// address of Reset_Handler is written by the linker at the beginning of the .text section (see linker script)
114+
uint32_t resetHandlerAddress = (uint32_t) * (SKETCH_START + 1);
115+
// jump to reset handler
116+
asm("bx %0"::"r"(resetHandlerAddress));
117+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh -x
2+
3+
ARDUINO=arduino
4+
SKETCH_NAME="NiNaBoot.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 -include -len 0x4000 > $DESTINATION
19+
rm -rf "$BUILD_PATH"
20+
}
21+
22+
mkdir -p "$OUTPUT_PATH"
23+
24+
buildSDUBootSketch "arduino:samd:mkrwifi1010" "$OUTPUT_PATH/mkrwifi1010.h"
25+
buildSDUBootSketch "arduino:samd:mkrvidor4000" "$OUTPUT_PATH/mkrvidor4000.h"
26+
buildSDUBootSketch "arduino:samd:nano_33_iot" "$OUTPUT_PATH/nano33iot.h"

libraries/SNU/keywords.txt

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

libraries/SNU/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SNU
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Update the sketch on your board with NiNa W10 wifi module
6+
paragraph=Requires a board with NiNa W10 module onboard
7+
category=Other
8+
url=http://www.arduino.cc/en/Reference/SNU
9+
architectures=megaAVR, samd

libraries/SNU/src/SNU.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 "SNU.h"
22+
23+
__attribute__ ((section(".sketch_boot")))
24+
unsigned char sduBoot[0x4000] = {
25+
#if defined(ARDUINO_SAMD_MKRWIFI1010)
26+
#include "boot/mkrwifi1010.h"
27+
#elif defined(ARDUINO_SAMD_MKRVIDOR4000)
28+
#include "boot/mkrvidor4000.h"
29+
#elif defined(ARDUINO_SAMD_NANO_33_IOT)
30+
#include "boot/nano33iot.h"
31+
#else
32+
#error "Unsupported board!"
33+
#endif
34+
};

libraries/SNU/src/SNU.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 _SFU_H_INCLUDED
20+
#define _SFU_H_INCLUDED
21+
22+
// nothing for now
23+
24+
#endif

0 commit comments

Comments
 (0)