Skip to content

Commit

Permalink
Add internal storage support
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepmistry committed Feb 27, 2017
1 parent 1b88b81 commit 4ab512f
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 3 deletions.
78 changes: 78 additions & 0 deletions examples/WiFi101_OTA/WiFi101_OTA.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
This example connects to an WPA encrypted WiFi network.
Then it prints the MAC address of the Wifi shield,
the IP address obtained, and other network details.
It then polls for sketch updates over WiFi, sketches
can be updated by selecting a network port from within
the Arduino IDE: Tools -> Port -> Network Ports ...
Circuit:
* WiFi shield attached
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 16 January 2017
by Sandeep Mistry
*/

#include <SPI.h>
#include <WiFi101.h>
#include <WiFi101OTA.h>

char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password

int status = WL_IDLE_STATUS;

void setup() {
//Initialize serial:
Serial.begin(9600);

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
}

// start the WiFi OTA library with internal (flash) based storage
WiFiOTA.begin("Arduino", "password", InternalStorage);

// you're connected now, so print out the status:
printWifiStatus();
}

void loop() {
// check for WiFi OTA updates
WiFiOTA.poll();

// add your normal loop code below ...
}

void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

WiFi101OTA KEYWORD1
WiFiOTA KEYWORD1

InternalStorage KEYWORD1
SDStorage KEYWORD1

#######################################
Expand Down
115 changes: 115 additions & 0 deletions src/InternalStorage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
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 "InternalStorage.h"

#define PAGE_SIZE (64)
#define PAGES (4096)
#define MAX_FLASH (PAGE_SIZE * PAGES)
#define ROW_SIZE (PAGE_SIZE * 4)

#define SKETCH_START_ADDRESS (0x2000)
#define MAX_PARTIONED_SKETCH_SIZE ((MAX_FLASH - SKETCH_START_ADDRESS) / 2)
#define STORAGE_START_ADDRESS (SKETCH_START_ADDRESS + MAX_PARTIONED_SKETCH_SIZE)

extern "C" {
// these functions must be in RAM (.data) and NOT inlined
// as they erase and copy the sketch data in flash

__attribute__ ((long_call, noinline, section (".data")))
static void eraseFlash(int address, int length)
{
for (int i = 0; i < length; i += ROW_SIZE) {
NVMCTRL->ADDR.reg = ((uint32_t)(address + i)) / 2;
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_ER;

while (!NVMCTRL->INTFLAG.bit.READY);
}
}

__attribute__ ((long_call, noinline, section (".data")))
static void copyFlashAndReset(int dest, int src, int length)
{
uint32_t* d = (uint32_t*)dest;
uint32_t* s = (uint32_t*)src;

eraseFlash(dest, length);

for (int i = 0; i < length; i += 4) {
*d++ = *s++;

while (!NVMCTRL->INTFLAG.bit.READY);
}

NVIC_SystemReset();
}
}

int InternalStorageClass::open()
{
_writeIndex = 0;
_writeAddress = (uint32_t*)STORAGE_START_ADDRESS;

// enable auto page writes
NVMCTRL->CTRLB.bit.MANW = 0;

eraseFlash(STORAGE_START_ADDRESS, MAX_PARTIONED_SKETCH_SIZE);

return 1;
}

size_t InternalStorageClass::write(uint8_t b)
{
_addressData.u8[_writeIndex] = b;
_writeIndex++;

if (_writeIndex == 4) {
_writeIndex = 0;

*_writeAddress = _addressData.u32;

_writeAddress++;

while (!NVMCTRL->INTFLAG.bit.READY);
}

return 1;
}

void InternalStorageClass::close()
{
while ((int)_writeAddress % PAGE_SIZE) {
write(0xff);
}
}

void InternalStorageClass::clear()
{
}

void InternalStorageClass::apply()
{
// disable interrupts, as vector table will be erase during flash sequence
noInterrupts();

copyFlashAndReset(SKETCH_START_ADDRESS, STORAGE_START_ADDRESS, MAX_PARTIONED_SKETCH_SIZE);
}

InternalStorageClass InternalStorage;
44 changes: 44 additions & 0 deletions src/InternalStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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 _INTERNAL_STORAGE_H_INCLUDED
#define _INTERNAL_STORAGE_H_INCLUDED

#include "OTAStorage.h"

class InternalStorageClass : public OTAStorage {
public:
virtual int open();
virtual size_t write(uint8_t);
virtual void close();
virtual void clear();
virtual void apply();

private:
union {
uint32_t u32;
uint8_t u8[4];
} _addressData;

int _writeIndex;
uint32_t* _writeAddress;
};

extern InternalStorageClass InternalStorage;

#endif
1 change: 1 addition & 0 deletions src/OTAStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class OTAStorage {
virtual size_t write(uint8_t) = 0;
virtual void close() = 0;
virtual void clear() = 0;
virtual void apply() = 0;
};

#endif
8 changes: 7 additions & 1 deletion src/SDStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <SDStorage.h>
#include "SDStorage.h"

#define UPDATE_FILE "UPDATE.BIN"

Expand Down Expand Up @@ -46,4 +46,10 @@ void SDStorageClass::clear()
SD.remove(UPDATE_FILE);
}

void SDStorageClass::apply()
{
// just reset, SDU copies the data to flash
NVIC_SystemReset();
}

SDStorageClass SDStorage;
1 change: 1 addition & 0 deletions src/SDStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SDStorageClass : public OTAStorage {
virtual size_t write(uint8_t);
virtual void close();
virtual void clear();
virtual void apply();

private:
File _file;
Expand Down
5 changes: 3 additions & 2 deletions src/WiFi101OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ void WiFiOTAClass::pollServer()

delay(250);

// Reset the device
NVIC_SystemReset() ;
// apply the update
_storage->apply();

while (true);
} else {
_storage->clear();
Expand Down
1 change: 1 addition & 0 deletions src/WiFi101OTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "OTAStorage.h"
#include "SDStorage.h"
#include "InternalStorage.h"

class WiFiOTAClass {
public:
Expand Down

0 comments on commit 4ab512f

Please sign in to comment.