Skip to content

The SDU library for Uno R4 #119

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 1 commit into from
Sep 4, 2023
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
Empty file added libraries/SDU/.unor4_only
Empty file.
71 changes: 71 additions & 0 deletions libraries/SDU/examples/Usage/Usage.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Usage
This example demonstrates how to use the UNO R4 SDU library to update a
sketch on an Arduino UNO R4 (WiFi, Minima) board using an
SD card. It prints out the date and time the sketch was compiled at
to both Serial and Serial1.

Circuit:
* Arduino UNO R4 Minima or WiFi board
* SD shield or breakout connected with CS pin of 4
* SD card

Non-Arduino UNO R4 board are NOT supported.
Steps to update sketch via SD card:

1) Upload this sketch or another sketch that includes the SDU library
via #include <SDU.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) Copy the .bin file from the sketch's folder to the SD card and rename
the file to UPDATE.bin. Eject the SD card from your PC.

5) Insert the SD card into the board, shield or breakout and press the
reset button or power cycle the board. The SDU library will then update
the sketch on the board with the contents of UPDATE.bin

created 23 March 2017
by Sandeep Mistry
*/

/*
Include the SDU library

This will add some code to the sketch before setup() is called
to check if an SD card is present and UPDATE.bin exists on the
SD card.

If UPDATE.bin is present, the file is used to update the sketch
running on the board. After this UPDATE.bin is deleted from the
SD card.
*/
#include <SDU.h>

String message;

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

// Wait for Serial Monitor connection
while (!Serial.available()) {
Serial.println("Send any key.");
delay(1000);
}

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
}

123 changes: 123 additions & 0 deletions libraries/SDU/extras/SDUBoot/SDUBoot.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* This sketch is a modified version of the SDUBoot example from the SDU
* library in the Arduino SAMD core.
*
* SDUBoot.ino is converted with build.sh into HEX values in a .h file
* in 'boot' folder of the SDU library. The SDU library with support of
* the linker script inserts the binary representation of this sketch
* before the actual sketch. This way this sketch acts as a second stage
* bootloader always uploaded with the sketch's binary.
*
* To make this second stage bootloader as small as possible while it is
* still an Arduino sketch, it can't be built with standard boards
* configurations of UNO R4 Minima or Wifi.
* Additionally the libfsp.a is built without code flash writing support
* and this sketch needs it.
*
* I created a custom board definition to build this sketch to a binary
* size less than 0x6000 bytes.
* https://github.com/JAndrassy/my_boards/tree/master/renesas-uno
* The custom unor4_sdu variant has peripherals support removed with many
* 'HOWMANY' settings in pins_arduino.h set to 0 to make the SDUBoot sketch
* bin even smaller.
* The bin size reduction for libfsp.a and the sketch is described here
* https://github.com/arduino/ArduinoCore-renesas/discussions/118
*/

#include <SD.h>
#include <r_flash_lp.h>

#define SDU_START 0x4000
#define SDU_SIZE 0x6000

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

#ifndef SDCARD_SS_PIN
#define SDCARD_SS_PIN 4
#endif

#define UPDATE_FILE "UPDATE.BIN"

flash_lp_instance_ctrl_t ctrl;
flash_cfg_t cfg;

void stopAgt(); //add to core/time.cpp as { main_timer.close();}

void setup() {

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;

cfg.data_flash_bgo = false;
cfg.p_callback = nullptr;
cfg.p_context = nullptr;
cfg.ipl = (BSP_IRQ_DISABLED);
cfg.irq = FSP_INVALID_VECTOR;

fsp_err_t rv = R_FLASH_LP_Open(&ctrl, &cfg);
if (rv == FSP_SUCCESS) {

uint32_t flashAddress = (uint32_t) SKETCH_START;

// erase the pages
__disable_irq();
rv = R_FLASH_LP_Erase(&ctrl, flashAddress, (updateSize / BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE) + 1);
__enable_irq();
if (rv == FSP_SUCCESS) {

uint8_t buffer[32 * BSP_FEATURE_FLASH_LP_CF_WRITE_SIZE];

// write the pages
for (uint32_t i = 0; i < updateSize; i += sizeof(buffer)) {
updateFile.read(buffer, sizeof(buffer));
__disable_irq();
R_FLASH_LP_Write(&ctrl, (uint32_t) &buffer, flashAddress, sizeof(buffer));
__enable_irq();
flashAddress += sizeof(buffer);
}

updateFlashed = true;
}

R_FLASH_LP_Close(&ctrl);
}

updateFile.close();

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

stopAgt();

SysTick->CTRL = 0;

// Disable MSP monitoring.
R_MPU_SPMON->SP[0].CTL = 0;

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

void loop() {
}
21 changes: 21 additions & 0 deletions libraries/SDU/extras/SDUBoot/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh -x

ARDUINO=arduino-cli
SKETCH_NAME="SDUBoot.ino"
SKETCH="$PWD/$SKETCH_NAME"
BUILD_PATH="$PWD/build"
OUTPUT_PATH="../../src/boot"

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

$ARDUINO compile -b $BOARD --output-dir="$BUILD_PATH" "$SKETCH"
cat "$BUILD_PATH/$SKETCH_NAME.bin" | xxd -i > $DESTINATION
rm -rf "$BUILD_PATH"
}

mkdir -p "$OUTPUT_PATH"

buildSDUBootSketch "my_boards:renesas_uno:unor4_sdu" "$OUTPUT_PATH/unor4.h"

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=renesas,renesas_uno
30 changes: 30 additions & 0 deletions libraries/SDU/src/SDU.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
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[0x6000] = {
#if defined(ARDUINO_ARCH_RENESAS_UNO)
#include "boot/unor4.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