Skip to content
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

Make library compatible with NINA WiFi module #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 0 additions & 1 deletion examples/WiFi101_OTA/WiFi101_OTA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

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

#include "arduino_secrets.h"
Expand Down
1 change: 0 additions & 1 deletion examples/WiFi101_SD_OTA/WiFi101_SD_OTA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

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

Expand Down
4 changes: 4 additions & 0 deletions src/InternalStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef __AVR__

#include <Arduino.h>

#include "InternalStorage.h"
Expand Down Expand Up @@ -119,3 +121,5 @@ long InternalStorageClass::maxSize()
}

InternalStorageClass InternalStorage;

#endif
85 changes: 85 additions & 0 deletions src/NINAStorage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright (c) 2018 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 "NINAStorage.h"

#define UPDATE_FILE "/fs/UPDATE.BIN"

static inline void reboot() {
#ifdef __AVR__
/* Write boot request */
USERROW.USERROW31 = 0xEB;
_PROTECTED_WRITE_SPM(NVMCTRL.CTRLA, NVMCTRL_CMD_PAGEERASEWRITE_gc);
while(NVMCTRL.STATUS & NVMCTRL_EEBUSY_bm);

_PROTECTED_WRITE(RSTCTRL.SWRR, RSTCTRL_SWRE_bm);
#else
NVIC_SystemReset();
#endif
}

int NINAStorageClass::open(int contentLength)
{
if (WiFiStorage.exists(UPDATE_FILE)) {
WiFiStorage.remove(UPDATE_FILE);
}

WiFiStorageFile file = WiFiStorage.open(UPDATE_FILE);

_file = &file;

return 1;
}

size_t NINAStorageClass::write(uint8_t b)
{
int ret = _file->write(&b, 1);
return ret;
}

void NINAStorageClass::close()
{
_file->close();
}

void NINAStorageClass::clear()
{
WiFiStorage.remove(UPDATE_FILE);
}

void NINAStorageClass::apply()
{
WiFiDrv::applyOTA();
reboot();
}

void NINAStorageClass::download(String url)
{
WiFiStorage.download(url, "UPDATE.BIN");
}

long NINAStorageClass::maxSize()
{
#ifdef __AVR__
return (0xFFFF - 0x3FFF - 0x100);
#else
return ((256 * 1024) - 0x2000);
#endif
}

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

#include <WiFiStorage.h>

#include "OTAStorage.h"

class NINAStorageClass : public OTAStorage {
public:
virtual int open(int length);
virtual size_t write(uint8_t);
virtual void close();
virtual void clear();
virtual void apply();
virtual long maxSize();
virtual void download(String url);
virtual bool hasDownloadAPI() {
return true;
}

private:
WiFiStorageFile* _file;
};

extern NINAStorageClass NINAStorage;

#endif
6 changes: 6 additions & 0 deletions src/OTAStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class OTAStorage {
virtual long maxSize() {
return ((256 * 1024) - 0x2000);
}
virtual void download(String url) {

}
virtual bool hasDownloadAPI() {
return false;
}
};

#endif
10 changes: 9 additions & 1 deletion src/SDStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@

#define UPDATE_FILE "UPDATE.BIN"

static inline void reboot() {
#ifdef __AVR__

#else
NVIC_SystemReset();
#endif
}

int SDStorageClass::open(int length)
{
(void)length;
Expand Down Expand Up @@ -50,7 +58,7 @@ void SDStorageClass::clear()
void SDStorageClass::apply()
{
// just reset, SDU copies the data to flash
NVIC_SystemReset();
reboot();
}

SDStorageClass SDStorage;
10 changes: 9 additions & 1 deletion src/SerialFlashStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@

#define UPDATE_FILE "UPDATE.BIN"

static inline void reboot() {
#ifdef __AVR__

#else
NVIC_SystemReset();
#endif
}

int SerialFlashStorageClass::open(int contentLength)
{
if (!SerialFlash.begin(SERIAL_FLASH_CS)) {
Expand Down Expand Up @@ -63,7 +71,7 @@ void SerialFlashStorageClass::clear()
void SerialFlashStorageClass::apply()
{
// just reset, SDU copies the data to flash
NVIC_SystemReset();
reboot();
}

SerialFlashStorageClass SerialFlashStorage;
25 changes: 12 additions & 13 deletions src/WiFi101OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@

#include "WiFi101OTA.h"

#if defined(ARDUINO_SAMD_ZERO)
#define BOARD "arduino_zero_edbg"
#elif defined(ARDUINO_SAMD_MKR1000)
#define BOARD "mkr1000"
#elif defined(ARDUINO_SAMD_MKRZERO)
#define BOARD "mkrzero"
#else
#error "Unsupported board!"
#endif

#define BOARD_LENGTH (sizeof(BOARD) - 1)

static String base64Encode(const String& in)
Expand Down Expand Up @@ -82,7 +72,7 @@ void WiFiOTAClass::begin(const char* name, const char* password, OTAStorage& sto

_server.begin();

_mdnsSocket.beginMulti(IPAddress(224, 0, 0, 251), 5353);
_mdnsSocket.beginMulticast(IPAddress(224, 0, 0, 251), 5353);
}

void WiFiOTAClass::poll()
Expand Down Expand Up @@ -297,15 +287,24 @@ void WiFiOTAClass::pollServer()
}

long read = 0;
String url;

while (client.connected() && read < contentLength) {
while (client.available()) {
read++;

_storage->write((char)client.read());
char c = (char)client.read();
if (_storage->hasDownloadAPI()) {
url += c;
} else {
_storage->write(c);
}
}
}

if (_storage->hasDownloadAPI()) {
_storage->download(url);
}

_storage->close();

if (read == contentLength) {
Expand Down
27 changes: 26 additions & 1 deletion src/WiFi101OTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,38 @@

#include <Arduino.h>

#include "WiFi101.h"
#if defined(ARDUINO_SAMD_ZERO)
#include "WiFi101.h"
#define BOARD "arduino_zero_edbg"
#elif defined(ARDUINO_SAMD_MKR1000)
#include "WiFi101.h"
#define BOARD "mkr1000"
#elif defined(ARDUINO_SAMD_MKRZERO)
#include "WiFi101.h"
#define BOARD "mkrzero"
#elif defined(ARDUINO_SAMD_MKRWIFI1010)
facchinm marked this conversation as resolved.
Show resolved Hide resolved
#include "WiFiNINA.h"
#define BOARD "mkrwifi1010"
#elif defined(ARDUINO_SAMD_NANO_33_IOT)
#include "WiFiNINA.h"
#define BOARD "nano_33_iot"
#elif defined(ARDUINO_SAMD_MKRVIDOR4000)
#include "WiFiNINA.h"
#define BOARD "mkrvidor4000"
#elif defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_MEGAAVR_UNO_WIFI_REV2)
#include "WiFiNINA.h"
#define BOARD "uno2018"
#else
#error "Unsupported board!"
#endif

#include "WiFiUdp.h"

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

class WiFiOTAClass {
public:
Expand Down