Skip to content

Commit f14158d

Browse files
authored
Merge pull request #7 from giulcioffi/UpdateSDexample
Update SD example
2 parents 8dd7596 + 29535f7 commit f14158d

File tree

4 files changed

+110
-25
lines changed

4 files changed

+110
-25
lines changed
+95-11
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,128 @@
11
/*
2-
This example demonstrates how to use to update
3-
the firmware of the Arduino Portenta H7 using
4-
a firmware image stored on a SD card.
2+
* This example demonstrates how to use to update the firmware of the Arduino Portenta H7 using
3+
* a firmware image stored on the SD.
4+
*
5+
* Steps:
6+
* 1) Create a sketch for the Portenta H7 and verifiy
7+
* that it both compiles and works on a board.
8+
* 2) In the IDE select: Sketch -> Export compiled Binary.
9+
* 3) Create an OTA update file utilising the tools 'lzss.py' and 'bin2ota.py' stored in
10+
* https://github.com/arduino-libraries/ArduinoIoTCloud/tree/master/extras/tools .
11+
* A) ./lzss.py --encode SKETCH.bin PORTENTA_H7_M7.lzss
12+
* B) ./bin2ota.py PORTENTA_H7_M7.lzss PORTENTA_H7_M7.ota
13+
* 4) Upload the OTA file to a network reachable location, e.g. OTA_Usage_Portenta.ino.PORTENTA_H7_M7.ota
14+
* has been uploaded to: http://downloads.arduino.cc/ota/OTA_Usage_Portenta.ino.PORTENTA_H7_M7.ota
15+
* 5) Perform an OTA update via steps outlined below.
516
*/
617

7-
#include "Arduino_Portenta_OTA.h"
18+
/******************************************************************************
19+
* INCLUDE
20+
******************************************************************************/
21+
22+
#include <Arduino_Portenta_OTA.h>
23+
24+
#include <WiFi.h>
25+
26+
#include "arduino_secrets.h"
27+
28+
/******************************************************************************
29+
* CONSTANT
30+
******************************************************************************/
31+
32+
/* Please enter your sensitive data in the Secret tab/arduino_secrets.h */
33+
static char const SSID[] = SECRET_SSID; /* your network SSID (name) */
34+
static char const PASS[] = SECRET_PASS; /* your network password (use for WPA, or use as key for WEP) */
35+
36+
static char const OTA_FILE_LOCATION[] = "http://downloads.arduino.cc/ota/OTA_Usage_Portenta.ino.PORTENTA_H7_M7.ota";
37+
38+
/******************************************************************************
39+
* SETUP/LOOP
40+
******************************************************************************/
841

942
void setup()
1043
{
1144
Serial.begin(115200);
1245
while (!Serial) {}
1346

14-
Serial.println("*****OTA from SD*****");
15-
Arduino_Portenta_OTA_SD ota(SD_OFFSET, 10240);
47+
if (WiFi.status() == WL_NO_SHIELD)
48+
{
49+
Serial.println("Communication with WiFi module failed!");
50+
return;
51+
}
52+
53+
int status = WL_IDLE_STATUS;
54+
while (status != WL_CONNECTED)
55+
{
56+
Serial.print ("Attempting to connect to '");
57+
Serial.print (SSID);
58+
Serial.println("'");
59+
status = WiFi.begin(SSID, PASS);
60+
delay(10000);
61+
}
62+
Serial.print ("You're connected to '");
63+
Serial.print (WiFi.SSID());
64+
Serial.println("'");
65+
66+
//Arduino_Portenta_OTA_SD ota(SD_FATFS, 0);
67+
Arduino_Portenta_OTA_SD ota(SD_FATFS_MBR, 1);
1668
Arduino_Portenta_OTA::Error ota_err = Arduino_Portenta_OTA::Error::None;
1769

70+
if (!ota.isOtaCapable())
71+
{
72+
Serial.println("Higher version bootloader required to perform OTA.");
73+
Serial.println("Please update the bootloader.");
74+
Serial.println("File -> Examples -> Portenta_System -> PortentaH7_updateBootloader");
75+
return;
76+
}
77+
1878
Serial.println("Initializing OTA storage");
1979
if ((ota_err = ota.begin()) != Arduino_Portenta_OTA::Error::None)
2080
{
21-
Serial.print ("ota.begin() failed with error code ");
81+
Serial.print ("Arduino_Portenta_OTA::begin() failed with error code ");
2282
Serial.println((int)ota_err);
2383
return;
2484
}
2585

26-
/* This function sets the precise length of update binary, in this case of OTA_Usage_Portenta.ino.PORTENTA_H7_M7.bin */
27-
ota.setUpdateLen(131728);
86+
87+
Serial.println("Starting download to SD ...");
88+
int const ota_download = ota.download(OTA_FILE_LOCATION, false /* is_https */);
89+
if (ota_download <= 0)
90+
{
91+
Serial.print ("Arduino_Portenta_OTA_SD::download failed with error code ");
92+
Serial.println(ota_download);
93+
return;
94+
}
95+
Serial.print (ota_download);
96+
Serial.println(" bytes stored.");
97+
98+
99+
Serial.println("Decompressing LZSS compressed file ...");
100+
int const ota_decompress = ota.decompress();
101+
if (ota_decompress < 0)
102+
{
103+
Serial.print("Arduino_Portenta_OTA_SD::decompress() failed with error code");
104+
Serial.println(ota_decompress);
105+
return;
106+
}
107+
Serial.print(ota_decompress);
108+
Serial.println(" bytes decompressed.");
109+
28110

29111
Serial.println("Storing parameters for firmware update in bootloader accessible non-volatile memory");
30112
if ((ota_err = ota.update()) != Arduino_Portenta_OTA::Error::None)
31113
{
32-
Serial.print ("ota.update() failed with error code ");
114+
Serial.print ("Arduino_Portenta_OTA::update() failed with error code ");
33115
Serial.println((int)ota_err);
34116
return;
35117
}
36118

37119
Serial.println("Performing a reset after which the bootloader will update the firmware.");
120+
Serial.println("Hint: Portenta H7 LED will blink Red-Blue-Green.");
121+
delay(1000);
38122
ota.reset();
39123
}
40124

41125
void loop()
42126
{
43127

44-
}
128+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

examples/OTA_Usage_Portenta/OTA_Usage_Portenta.ino

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
/*
2-
Usage
3-
This example demonstrates how to use the Arduino_Portenta_OTA library to update a
4-
sketch on any Portenta via the storage types allowed by the board.
5-
Steps to update sketch:
6-
1) Upload this sketch or any other sketch (this one lights the LED up with different colours).
2+
This sketch can be used to generate an example binary that can be uploaded to Portenta via OTA.
3+
It needs to be used together with
4+
- 'OTA_Qspi_Flash.ino' if you want to use the Qspi Flash as storage system
5+
OR
6+
- 'SD_Qspi_Flash.ino' if you want to use the SD card as storage system
7+
8+
Steps to test OTA on Portenta:
9+
1) Upload this sketch or any other sketch (this one lights up the RGB LED with different colours).
710
2) In the IDE select: Sketch -> Export compiled Binary
8-
3) Open the location of the sketch and choose the next step according to the desired storage type:
9-
- SD: copy the binary to the SD with the name "UPDATE.BIN"
10-
- INTERNAL FLASH:
11-
- QSPI Flash:
12-
5) Upload the sketch OTA_*_Portenta.ino
11+
3) Upload the exported binary to a server
12+
4) Choose a storage mechanism (SD or QSPI), open the related OTA_*_Portenta.ino sketch,
13+
eventually update the OTA_FILE_LOCATION
14+
5) Upload the sketch OTA_*_Portenta.ino to perform OTA via SD or QSPI Flash
1315
*/
1416

1517
void setLed(int blue, int gree, int red) {

src/Arduino_Portenta_OTA_SD.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ Arduino_Portenta_OTA_SD::Arduino_Portenta_OTA_SD(StorageTypePortenta const stora
5353

5454
bool Arduino_Portenta_OTA_SD::init()
5555
{
56-
if (_block_device.init())
57-
return false;
58-
5956
if(_storage_type == SD_FATFS)
6057
{
6158
_fs_sd = new mbed::FATFileSystem("fs");
@@ -70,7 +67,7 @@ bool Arduino_Portenta_OTA_SD::init()
7067

7168
if (_storage_type == SD_FATFS_MBR)
7269
{
73-
_bd = new mbed::MBRBlockDevice(reinterpret_cast<mbed::BlockDevice *>(&_block_device), 1);
70+
_bd = new mbed::MBRBlockDevice(reinterpret_cast<mbed::BlockDevice *>(&_block_device), _data_offset);
7471
_fs_sd = new mbed::FATFileSystem("fs");
7572
int const err = _fs_sd->mount(_bd);
7673
if (err)

0 commit comments

Comments
 (0)