Skip to content

Commit db83eb4

Browse files
committed
SFU support for OTA on SaraU210 module
Added new core librares to manage Sara flash support, by useing new MKRGSM's API to write and read from sara module for retrive file during OTA's procedure
1 parent 6e049f7 commit db83eb4

File tree

10 files changed

+323
-0
lines changed

10 files changed

+323
-0
lines changed

libraries/SSU/examples/SSU_LoadBinary/Binary.h

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**************************************************************************************
2+
* INCLUDE
3+
**************************************************************************************/
4+
5+
#include <Arduino_MKRMEM.h>
6+
7+
/**************************************************************************************
8+
* CONSTANTS
9+
**************************************************************************************/
10+
11+
static uint8_t const BINARY[] =
12+
{
13+
#include "Binary.h"
14+
};
15+
16+
/**************************************************************************************
17+
* SETUP/LOOP
18+
**************************************************************************************/
19+
20+
void setup() {
21+
Serial.begin(9600);
22+
23+
unsigned long const start = millis();
24+
for(unsigned long now = millis(); !Serial && ((now - start) < 5000); now = millis()) { };
25+
26+
flash.begin();
27+
28+
Serial.print("Mounting ... ");
29+
if(SPIFFS_OK != filesystem.mount()) {
30+
Serial.println("mount() failed with error code "); Serial.println(filesystem.err()); return;
31+
}
32+
Serial.println("OK");
33+
34+
35+
Serial.print("Checking ... ");
36+
if(SPIFFS_OK != filesystem.check()) {
37+
Serial.println("check() failed with error code "); Serial.println(filesystem.err()); return;
38+
}
39+
Serial.println("OK");
40+
41+
42+
Serial.print("Writing \"UPDATE.BIN\" ... ");
43+
File file = filesystem.open("UPDATE.BIN", CREATE | READ_WRITE| TRUNCATE);
44+
45+
int const bytes_to_write = sizeof(BINARY);
46+
int const bytes_written = file.write((void *)BINARY, bytes_to_write);
47+
48+
if(bytes_written != bytes_to_write) {
49+
Serial.println("write() failed with error code "); Serial.println(filesystem.err()); return;
50+
} else {
51+
Serial.print("OK (");
52+
Serial.print(bytes_written);
53+
Serial.println(" bytes written)");
54+
}
55+
56+
Serial.print("Unmounting ... ");
57+
filesystem.unmount();
58+
Serial.println("OK");
59+
}
60+
61+
void loop() {
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Usage
3+
This example demonstrates how to use the SAMD SFU library to update a
4+
sketch on any Arduino MKR board connected to a MKRMEM Shield. This sketch
5+
prints out the date and time the sketch was compiled.
6+
7+
Steps to update sketch via MKRMEM shield:
8+
9+
1) Upload this sketch or another sketch that includes the SFU library
10+
via #include <SFU.h>
11+
12+
2) Update the sketch as desired. For this example the sketch prints out
13+
the compiled date and time so no updates are needed.
14+
15+
3) In the IDE select: Sketch -> Export compiled Binary
16+
17+
4) Open the location of the sketch and convert the .bin file to a C byte array.
18+
cat SKETCH.bin | xxd --include > Binary.h
19+
20+
5) Copy Binary.h file from the sketch's folder to the SFU_LoadBinary sketch
21+
and load it to the MKRMEM via SFU_LoadBinary sketch.
22+
*/
23+
24+
/*
25+
Include the SFU library
26+
27+
This will add some code to the sketch before setup() is called
28+
to check if UPDATE.bin is present on the flash chip of the MKRMEM
29+
shield. If this theck is positive the file is used to update the sketch
30+
running on the board. After this UPDATE.BIN is deleted from the flash.
31+
*/
32+
33+
#include <SFU.h>
34+
35+
void setup() {
36+
Serial.begin(9600);
37+
while(!Serial) { }
38+
39+
// wait a bit
40+
delay(1000);
41+
42+
String message;
43+
message += "Sketch compile date and time: ";
44+
message += __DATE__;
45+
message += " ";
46+
message += __TIME__;
47+
48+
// print out the sketch compile date and time on the serial port
49+
Serial.println(message);
50+
}
51+
52+
void loop() {
53+
// add you own code here
54+
}
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
/**************************************************************************************
20+
INCLUDE
21+
**************************************************************************************/
22+
23+
#include <FlashStorage.h>
24+
#include "MKRGSM.h"
25+
26+
/**************************************************************************************
27+
DEFINE
28+
**************************************************************************************/
29+
30+
#define SSU_START 0x2000
31+
#define SSU_SIZE 0x8000
32+
33+
#define SKETCH_START (uint32_t*)(SSU_START + SSU_SIZE)
34+
35+
/**************************************************************************************
36+
GLOBAL CONSTANTS
37+
**************************************************************************************/
38+
39+
static char const UPDATE_FILE_NAME[] = "UPDATE.BIN";
40+
41+
/**************************************************************************************
42+
GLOBAL VARIABLES
43+
**************************************************************************************/
44+
45+
FlashClass mcu_flash;
46+
GSMFileUtils fileUtils(false);
47+
/**************************************************************************************
48+
FUNCTION DECLARATION
49+
**************************************************************************************/
50+
51+
extern "C" void __libc_init_array(void);
52+
53+
/**************************************************************************************
54+
MAIN
55+
**************************************************************************************/
56+
57+
int main()
58+
{
59+
init();
60+
61+
__libc_init_array();
62+
63+
delay(1);
64+
65+
String filename = UPDATE_FILE_NAME;
66+
67+
const size_t blockSize = 512;
68+
69+
MODEM.begin();
70+
fileUtils.begin();
71+
bool update_success = false;
72+
auto size = fileUtils.listFile(filename);
73+
auto cycles = (size / blockSize) + 1;
74+
if (size > SSU_SIZE) {
75+
size -= SSU_SIZE;
76+
77+
/* Erase the MCU flash */
78+
uint32_t flash_address = (uint32_t)SKETCH_START;
79+
mcu_flash.erase((void*)flash_address, size);
80+
81+
for (auto i = 0; i < cycles; i++) {
82+
uint8_t block[blockSize] { 0 };
83+
digitalWrite(LED_BUILTIN,LOW);
84+
auto read = fileUtils.readBlock(filename, (i * blockSize) + SSU_SIZE, blockSize, block);
85+
digitalWrite(LED_BUILTIN,HIGH);
86+
mcu_flash.write((void*)flash_address, block, read);
87+
flash_address += read;
88+
}
89+
update_success = true;
90+
}
91+
if (update_success) { fileUtils.deleteFile(filename); }
92+
93+
/* Jump to the sketch */
94+
__set_MSP(*SKETCH_START);
95+
96+
/* Reset vector table address */
97+
SCB->VTOR = ((uint32_t)(SKETCH_START) & SCB_VTOR_TBLOFF_Msk);
98+
99+
/* Address of Reset_Handler is written by the linker at the beginning of the .text section (see linker script) */
100+
uint32_t resetHandlerAddress = (uint32_t) * (SKETCH_START + 1);
101+
/* Jump to reset handler */
102+
asm("bx %0"::"r"(resetHandlerAddress));
103+
}

libraries/SSU/extras/SSUBoot/build.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/sh -x
2+
3+
#Sara U201
4+
ARDUINO=arduino
5+
SKETCH_NAME="SSUBoot.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+
buildSSUBootSketch() {
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+
buildSSUBootSketch "arduino:samd:mkrgsm1400" "$OUTPUT_PATH/mkrgsm1400.h"

libraries/SSU/keywords.txt

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

libraries/SSU/library.properties

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

libraries/SSU/src/SSU.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 "SSU.h"
22+
23+
__attribute__ ((section(".sketch_boot")))
24+
unsigned char SSU_BOOT[0x8000] = {
25+
#if defined(ARDUINO_SAMD_MKRGSM1400)
26+
#include "boot/mkrgsm1400.h"
27+
#else
28+
#error "Unsupported board!"
29+
#endif
30+
};

libraries/SSU/src/SSU.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 _SSU_H_INCLUDED
20+
#define _SSU_H_INCLUDED
21+
22+
/* Nothing to do */
23+
24+
#endif /* _SSU_H_INCLUDED */

libraries/SSU/src/boot/mkrgsm1400.h

Whitespace-only changes.

0 commit comments

Comments
 (0)