Skip to content

Add support for updating sketches from an SD card #203

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 4 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions cores/arduino/SERCOM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ void SERCOM::enableSPI()

void SERCOM::disableSPI()
{
//Setting the enable bit to 0
sercom->SPI.CTRLA.bit.ENABLE = 0;

while(sercom->SPI.SYNCBUSY.bit.ENABLE)
{
//Waiting then enable bit from SYNCBUSY is equal to 0;
}

//Setting the enable bit to 0
sercom->SPI.CTRLA.bit.ENABLE = 0;
}

void SERCOM::setDataOrderSPI(SercomDataOrder dataOrder)
Expand Down
5 changes: 5 additions & 0 deletions extras/package_index.json.Hourly.template
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
"packager": "arduino",
"name": "CMSIS-Atmel",
"version": "1.1.0"
},
{
"packager": "arduino",
"name": "arduinoOTA",
"version": "1.2.0"
}
]
}
Expand Down
5 changes: 5 additions & 0 deletions extras/package_index.json.PR.template
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
"packager": "arduino",
"name": "CMSIS-Atmel",
"version": "1.1.0"
},
{
"packager": "arduino",
"name": "arduinoOTA",
"version": "1.2.0"
}
]
}
Expand Down
92 changes: 92 additions & 0 deletions libraries/SDU/extras/SDUBoot/SDUBoot.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright (c) 2017 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 <SD.h>
#include <FlashStorage.h>

#define SDU_START 0x2000
#define SDU_SIZE 0x4000

#define SKETCH_START (uint32_t*)(SDU_START + SDU_SIZE)

#ifndef SDCARD_SS_PIN
#define SDCARD_SS_PIN 4
#endif

#define UPDATE_FILE "UPDATE.BIN"

FlashClass flash;

// Initialize C library
extern "C" void __libc_init_array(void);

int main() {
init();

__libc_init_array();

delay(1);

if (SD.begin(SDCARD_SS_PIN) && SD.exists(UPDATE_FILE)) {
File updateFile = SD.open(UPDATE_FILE);
uint32_t updateSize = updateFile.size();
bool updateFlashed = false;

if (updateSize > SDU_SIZE) {
// skip the SDU section
updateFile.seek(SDU_SIZE);
updateSize -= SDU_SIZE;

uint32_t flashAddress = (uint32_t)SKETCH_START;

// erase the pages
flash.erase((void*)flashAddress, updateSize);

uint8_t buffer[512];

// write the pages
for (uint32_t i = 0; i < updateSize; i += sizeof(buffer)) {
updateFile.read(buffer, sizeof(buffer));

flash.write((void*)flashAddress, buffer, sizeof(buffer));

flashAddress += sizeof(buffer);
}

updateFlashed = true;
}

updateFile.close();

if (updateFlashed) {
SD.remove(UPDATE_FILE);
}
}

// 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));
}

26 changes: 26 additions & 0 deletions libraries/SDU/extras/SDUBoot/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh -x

ARDUINO=arduino
SKETCH_NAME="SDUBoot.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

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

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

mkdir -p "$OUTPUT_PATH"

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

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

SDU KEYWORD1

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

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

__attribute__ ((section(".sketch_boot")))
unsigned char sduBoot[0x4000] = {
#if defined(ARDUINO_SAMD_ZERO)
#include "boot/zero.h"
#elif defined(ARDUINO_SAMD_MKR1000)
#include "boot/mkr1000.h"
#elif defined(ARDUINO_SAMD_MKRZERO)
#include "boot/mkrzero.h"
#else
#error "Unsupported board!"
#endif
};
24 changes: 24 additions & 0 deletions libraries/SDU/src/SDU.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright (c) 2017 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 _SDU_H_INCLUDED
#define _SDU_H_INCLUDED

// nothing for now

#endif
Loading