Skip to content

SSU support for OTA on SaraU210 module #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
63 changes: 63 additions & 0 deletions libraries/SSU/examples/SSU_LoadBinary/SSU_LoadBinary.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <Arduino_MKRMEM.h>

/**************************************************************************************
* CONSTANTS
**************************************************************************************/

static uint8_t const BINARY[] =
{
#include "Binary.h"
};

/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/

void setup() {
Serial.begin(9600);

unsigned long const start = millis();
for(unsigned long now = millis(); !Serial && ((now - start) < 5000); now = millis()) { };

flash.begin();

Serial.print("Mounting ... ");
if(SPIFFS_OK != filesystem.mount()) {
Serial.println("mount() failed with error code "); Serial.println(filesystem.err()); return;
}
Serial.println("OK");


Serial.print("Checking ... ");
if(SPIFFS_OK != filesystem.check()) {
Serial.println("check() failed with error code "); Serial.println(filesystem.err()); return;
}
Serial.println("OK");


Serial.print("Writing \"UPDATE.BIN\" ... ");
File file = filesystem.open("UPDATE.BIN", CREATE | READ_WRITE| TRUNCATE);

int const bytes_to_write = sizeof(BINARY);
int const bytes_written = file.write((void *)BINARY, bytes_to_write);

if(bytes_written != bytes_to_write) {
Serial.println("write() failed with error code "); Serial.println(filesystem.err()); return;
} else {
Serial.print("OK (");
Serial.print(bytes_written);
Serial.println(" bytes written)");
}

Serial.print("Unmounting ... ");
filesystem.unmount();
Serial.println("OK");
}

void loop() {

}
54 changes: 54 additions & 0 deletions libraries/SSU/examples/SSU_Usage/SSU_Usage.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Usage
This example demonstrates how to use the SAMD SFU library to update a
sketch on any Arduino MKR board connected to a MKRMEM Shield. This sketch
prints out the date and time the sketch was compiled.

Steps to update sketch via MKRMEM shield:

1) Upload this sketch or another sketch that includes the SFU library
via #include <SFU.h>

2) Update the sketch as desired. For this example the sketch prints out
the compiled date and time so no updates are needed.

3) In the IDE select: Sketch -> Export compiled Binary

4) Open the location of the sketch and convert the .bin file to a C byte array.
cat SKETCH.bin | xxd --include > Binary.h

5) Copy Binary.h file from the sketch's folder to the SFU_LoadBinary sketch
and load it to the MKRMEM via SFU_LoadBinary sketch.
*/

/*
Include the SFU library

This will add some code to the sketch before setup() is called
to check if UPDATE.bin is present on the flash chip of the MKRMEM
shield. If this theck is positive the file is used to update the sketch
running on the board. After this UPDATE.BIN is deleted from the flash.
*/

#include <SFU.h>

void setup() {
Serial.begin(9600);
while(!Serial) { }

// wait a bit
delay(1000);

String message;
message += "Sketch compile date and time: ";
message += __DATE__;
message += " ";
message += __TIME__;

// print out the sketch compile date and time on the serial port
Serial.println(message);
}

void loop() {
// add you own code here
}
107 changes: 107 additions & 0 deletions libraries/SSU/extras/SSUBoot/SSUBoot.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright (c) 2020 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
**************************************************************************************/

#include <FlashStorage.h>
#include "MKRGSM.h"

/**************************************************************************************
DEFINE
**************************************************************************************/

#define SSU_START 0x2000
#define SSU_SIZE 0x8000

#define SKETCH_START (uint32_t*)(SSU_START + SSU_SIZE)

/**************************************************************************************
GLOBAL CONSTANTS
**************************************************************************************/

static char const UPDATE_FILE_NAME[] = "UPDATE.BIN";

/**************************************************************************************
GLOBAL VARIABLES
**************************************************************************************/

FlashClass mcu_flash;
GSMFileUtils fileUtils(false);
/**************************************************************************************
FUNCTION DECLARATION
**************************************************************************************/

extern "C" void __libc_init_array(void);

/**************************************************************************************
MAIN
**************************************************************************************/

int main()
{
init();

__libc_init_array();

delay(1);

String filename = UPDATE_FILE_NAME;

const size_t blockSize = 512;

MODEM.begin();
fileUtils.begin();
bool update_success = false;
if (fileUtils.listFile("UPDATE.OK") == 1) {
auto size = fileUtils.listFile(filename);
auto cycles = (size / blockSize) + 1;
if (size > SSU_SIZE) {
size -= SSU_SIZE;

/* Erase the MCU flash */
uint32_t flash_address = (uint32_t)SKETCH_START;
mcu_flash.erase((void*)flash_address, size);

for (auto i = 0; i < cycles; i++) {
uint8_t block[blockSize] { 0 };
digitalWrite(LED_BUILTIN,LOW);
auto read = fileUtils.readBlock(filename, (i * blockSize) + SSU_SIZE, blockSize, block);
digitalWrite(LED_BUILTIN,HIGH);
mcu_flash.write((void*)flash_address, block, read);
flash_address += read;
}
update_success = true;
}
if (update_success) {
fileUtils.deleteFile(filename);
fileUtils.deleteFile("UPDATE.OK");
}
}
/* 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));
}
23 changes: 23 additions & 0 deletions libraries/SSU/extras/SSUBoot/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh -x

#Sara U201
ARDUINO=arduino
SKETCH_NAME="SSUBoot.ino"
SKETCH="$PWD/$SKETCH_NAME"
BUILD_PATH="$PWD/build"
OUTPUT_PATH="../../src/boot"

if [[ "$OSTYPE" == "darwin"* ]]; then
ARDUINO="/Applications/Arduino.app/Contents/MacOS/Arduino"
fi

buildSSUBootSketch() {
BOARD=$1
DESTINATION=$2

$ARDUINO --verify --board $BOARD --preserve-temp-files --pref build.path="$BUILD_PATH" $SKETCH
cat "$BUILD_PATH/$SKETCH_NAME.bin" | xxd -include > $DESTINATION
rm -rf "$BUILD_PATH"
}

buildSSUBootSketch "arduino:samd:mkrgsm1400" "$OUTPUT_PATH/mkrgsm1400.h"
17 changes: 17 additions & 0 deletions libraries/SSU/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#######################################
# Syntax Coloring Map For SDU
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

SSU KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

#######################################
# Constants (LITERAL1)
#######################################
9 changes: 9 additions & 0 deletions libraries/SSU/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=SSU
version=1.0.0
author=Arduino
maintainer=Arduino <[email protected]>
sentence=Update the sketch on your Arduino MKRGSM1400 from SARAU201 flash.
paragraph=
category=Other
url=https://www.arduino.cc/en/Reference/SSU
architectures=samd
30 changes: 30 additions & 0 deletions libraries/SSU/src/SSU.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright (c) 2020 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 <Arduino.h>

#include "SSU.h"

__attribute__ ((section(".sketch_boot")))
unsigned char SSU_BOOT[0x8000] = {
#if defined(ARDUINO_SAMD_MKRGSM1400)
#include "boot/mkrgsm1400.h"
#else
#error "Unsupported board!"
#endif
};
24 changes: 24 additions & 0 deletions libraries/SSU/src/SSU.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright (c) 2020 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
*/

#ifndef _SSU_H_INCLUDED
#define _SSU_H_INCLUDED

/* Nothing to do */

#endif /* _SSU_H_INCLUDED */
Loading