Skip to content

Commit 6a50e54

Browse files
committed
The SDU library
1 parent 2e56526 commit 6a50e54

File tree

10 files changed

+2290
-0
lines changed

10 files changed

+2290
-0
lines changed

libraries/SDU/.unor4_only

Whitespace-only changes.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Usage
3+
This example demonstrates how to use the UNO R4 SDU library to update a
4+
sketch on an Arduino UNO R4 (WiFi, Minima) board using an
5+
SD card. It prints out the date and time the sketch was compiled at
6+
to both Serial and Serial1.
7+
8+
Circuit:
9+
* Arduino UNO R4 Minima or WiFi board
10+
* SD shield or breakout connected with CS pin of 4
11+
* SD card
12+
13+
Non-Arduino UNO R4 board are NOT supported.
14+
Steps to update sketch via SD card:
15+
16+
1) Upload this sketch or another sketch that includes the SDU library
17+
via #include <SDU.h>
18+
19+
2) Update the sketch as desired. For this example the sketch prints out
20+
the compiled date and time so no updates are needed.
21+
22+
3) In the IDE select: Sketch -> Export Compiled Binary
23+
24+
4) Copy the .bin file from the sketch's folder to the SD card and rename
25+
the file to UPDATE.bin. Eject the SD card from your PC.
26+
27+
5) Insert the SD card into the board, shield or breakout and press the
28+
reset button or power cycle the board. The SDU library will then update
29+
the sketch on the board with the contents of UPDATE.bin
30+
31+
created 23 March 2017
32+
by Sandeep Mistry
33+
*/
34+
35+
/*
36+
Include the SDU library
37+
38+
This will add some code to the sketch before setup() is called
39+
to check if an SD card is present and UPDATE.bin exists on the
40+
SD card.
41+
42+
If UPDATE.bin is present, the file is used to update the sketch
43+
running on the board. After this UPDATE.bin is deleted from the
44+
SD card.
45+
*/
46+
#include <SDU.h>
47+
48+
String message;
49+
50+
void setup() {
51+
Serial.begin(115200);
52+
53+
// Wait for Serial Monitor connection
54+
while (!Serial.available()) {
55+
Serial.println("Send any key.");
56+
delay(1000);
57+
}
58+
59+
message += "Sketch compile date and time: ";
60+
message += __DATE__;
61+
message += " ";
62+
message += __TIME__;
63+
64+
// print out the sketch compile date and time on the serial port
65+
Serial.println(message);
66+
}
67+
68+
void loop() {
69+
// add you own code here
70+
}
71+
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* This sketch is a modified version of the SDUBoot example from the SDU
3+
* library in the Arduino SAMD core.
4+
*
5+
* SDUBoot.ino is converted with build.sh into HEX values in a .h file
6+
* in 'boot' folder of the SDU library. The SDU library with support of
7+
* the linker script inserts the binary representation of this sketch
8+
* before the actual sketch. This way this sketch acts as a second stage
9+
* bootloader always uploaded with the sketch's binary.
10+
*
11+
* To make this second stage bootloader as small as possible while it is
12+
* still an Arduino sketch, it can't be built with standard boards
13+
* configurations of UNO R4 Minima or Wifi.
14+
* Additionally the libfsp.a is built without code flash writing support
15+
* and this sketch needs it.
16+
*
17+
* I created a custom board definition to build this sketch to a binary
18+
* size less than 0x6000 bytes.
19+
* https://github.com/JAndrassy/my_boards/tree/master/renesas-uno
20+
* The custom unor4_sdu variant has peripherals support removed with many
21+
* 'HOWMANY' settings in pins_arduino.h set to 0 to make the SDUBoot sketch
22+
* bin even smaller.
23+
* The bin size reduction for libfsp.a and the sketch is described here
24+
* https://github.com/arduino/ArduinoCore-renesas/discussions/118
25+
*/
26+
27+
#include <SD.h>
28+
#include <r_flash_lp.h>
29+
30+
#define SDU_START 0x4000
31+
#define SDU_SIZE 0x6000
32+
33+
#define SKETCH_START (uint32_t*)(SDU_START + SDU_SIZE)
34+
35+
#ifndef SDCARD_SS_PIN
36+
#define SDCARD_SS_PIN 4
37+
#endif
38+
39+
#define UPDATE_FILE "UPDATE.BIN"
40+
41+
flash_lp_instance_ctrl_t ctrl;
42+
flash_cfg_t cfg;
43+
44+
void stopAgt(); //add to core/time.cpp as { main_timer.close();}
45+
46+
void setup() {
47+
48+
delay(1);
49+
50+
if (SD.begin(SDCARD_SS_PIN) && SD.exists(UPDATE_FILE)) {
51+
52+
File updateFile = SD.open(UPDATE_FILE);
53+
uint32_t updateSize = updateFile.size();
54+
bool updateFlashed = false;
55+
56+
if (updateSize > SDU_SIZE) {
57+
// skip the SDU section
58+
updateFile.seek(SDU_SIZE);
59+
updateSize -= SDU_SIZE;
60+
61+
cfg.data_flash_bgo = false;
62+
cfg.p_callback = nullptr;
63+
cfg.p_context = nullptr;
64+
cfg.ipl = (BSP_IRQ_DISABLED);
65+
cfg.irq = FSP_INVALID_VECTOR;
66+
67+
fsp_err_t rv = R_FLASH_LP_Open(&ctrl, &cfg);
68+
if (rv == FSP_SUCCESS) {
69+
70+
uint32_t flashAddress = (uint32_t) SKETCH_START;
71+
72+
// erase the pages
73+
__disable_irq();
74+
rv = R_FLASH_LP_Erase(&ctrl, flashAddress, (updateSize / BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE) + 1);
75+
__enable_irq();
76+
if (rv == FSP_SUCCESS) {
77+
78+
uint8_t buffer[32 * BSP_FEATURE_FLASH_LP_CF_WRITE_SIZE];
79+
80+
// write the pages
81+
for (uint32_t i = 0; i < updateSize; i += sizeof(buffer)) {
82+
updateFile.read(buffer, sizeof(buffer));
83+
__disable_irq();
84+
R_FLASH_LP_Write(&ctrl, (uint32_t) &buffer, flashAddress, sizeof(buffer));
85+
__enable_irq();
86+
flashAddress += sizeof(buffer);
87+
}
88+
89+
updateFlashed = true;
90+
}
91+
92+
R_FLASH_LP_Close(&ctrl);
93+
}
94+
95+
updateFile.close();
96+
97+
if (updateFlashed) {
98+
SD.remove(UPDATE_FILE);
99+
}
100+
}
101+
}
102+
103+
stopAgt();
104+
105+
SysTick->CTRL = 0;
106+
107+
// Disable MSP monitoring.
108+
R_MPU_SPMON->SP[0].CTL = 0;
109+
110+
// jump to the sketch
111+
__set_MSP(*SKETCH_START);
112+
113+
//Reset vector table address
114+
SCB->VTOR = ((uint32_t) (SKETCH_START ) & SCB_VTOR_TBLOFF_Msk);
115+
116+
// address of Reset_Handler is written by the linker at the beginning of the .text section (see linker script)
117+
uint32_t resetHandlerAddress = (uint32_t) *(SKETCH_START + 1);
118+
// jump to reset handler
119+
asm("bx %0"::"r"(resetHandlerAddress));
120+
}
121+
122+
void loop() {
123+
}

libraries/SDU/extras/SDUBoot/build.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh -x
2+
3+
ARDUINO=arduino-cli
4+
SKETCH_NAME="SDUBoot.ino"
5+
SKETCH="$PWD/$SKETCH_NAME"
6+
BUILD_PATH="$PWD/build"
7+
OUTPUT_PATH="../../src/boot"
8+
9+
buildSDUBootSketch() {
10+
BOARD=$1
11+
DESTINATION=$2
12+
13+
$ARDUINO compile -b $BOARD --output-dir="$BUILD_PATH" "$SKETCH"
14+
cat "$BUILD_PATH/$SKETCH_NAME.bin" | xxd -i > $DESTINATION
15+
rm -rf "$BUILD_PATH"
16+
}
17+
18+
mkdir -p "$OUTPUT_PATH"
19+
20+
buildSDUBootSketch "my_boards:renesas_uno:unor4_sdu" "$OUTPUT_PATH/unor4.h"
21+

libraries/SDU/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SDU
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=Update the sketch on your board from an SD card
6+
paragraph=Requires an SD card
7+
category=Other
8+
url=http://www.arduino.cc/en/Reference/SDU
9+
architectures=renesas,renesas_uno

libraries/SDU/src/SDU.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 "SDU.h"
22+
23+
__attribute__ ((section(".sketch_boot")))
24+
unsigned char sduBoot[0x6000] = {
25+
#if defined(ARDUINO_ARCH_RENESAS_UNO)
26+
#include "boot/unor4.h"
27+
#else
28+
#error "Unsupported board!"
29+
#endif
30+
};

libraries/SDU/src/SDU.h

Lines changed: 24 additions & 0 deletions
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 _SDU_H_INCLUDED
20+
#define _SDU_H_INCLUDED
21+
22+
// nothing for now
23+
24+
#endif

0 commit comments

Comments
 (0)