├── src ├── Internal_Storage_OTA.h ├── OTAStorage.h ├── InternalStorage.h └── InternalStorage.cpp ├── library.properties ├── library.json └── README.md /src/Internal_Storage_OTA.h: -------------------------------------------------------------------------------- 1 | #ifndef INTERNAL_STORAGE_OTA_H 2 | #define INTERNAL_STORAGE_OTA_H 3 | #include 4 | #include "InternalStorage.h" 5 | 6 | 7 | class Internal_Storage_OTA 8 | { 9 | public: 10 | Internal_Storage_OTA(){} 11 | ~Internal_Storage_OTA(){} 12 | }; 13 | 14 | #endif -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Internal_Storage_OTA 2 | version=0.0.1 3 | author=Mobizt 4 | maintainer=Mobizt 5 | sentence=This library contains modified version of InternalStorageClass and OTAStorage classes from http://www.arduino.cc/en/Reference/WiFi101OTA. 6 | paragraph=Requires an Arduino/Genuino SAMD board 7 | category=Other 8 | url=https://github.com/mobizt/Internal_Storage_OTA 9 | architectures=samd 10 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Internal_Storage_OTA", 3 | "version": "0.0.1", 4 | "keywords": "arduino", 5 | "description": "This library contains modified version of InternalStorageClass and OTAStorage classes from http://www.arduino.cc/en/Reference/WiFi101OTA.", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/mobizt/Internal_Storage_OTA.git" 9 | }, 10 | "authors": [{ 11 | "name": "Mobizt", 12 | "email": "k_suwatchai@hotmail.com" 13 | }], 14 | "frameworks": "arduino", 15 | "platforms": "atmelsam" 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Internal_Storage_OTA 2 | 3 | This library contains modified version of InternalStorageClass and OTAStorage classes from http://www.arduino.cc/en/Reference/WiFi101OTA. 4 | 5 | Copyright (c) Arduino LLC. All right reserved. 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library; if not, write to the Free Software 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -------------------------------------------------------------------------------- /src/OTAStorage.h: -------------------------------------------------------------------------------- 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 _OTA_STORAGE_H_INCLUDED 20 | #define _OTA_STORAGE_H_INCLUDED 21 | 22 | class OTAStorage { 23 | public: 24 | virtual int open(int size) = 0; 25 | virtual size_t write(uint8_t) = 0; 26 | virtual void close() = 0; 27 | virtual void clear() = 0; 28 | virtual void apply() = 0; 29 | 30 | virtual long maxSize() { 31 | return ((256 * 1024) - 0x2000); 32 | } 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /src/InternalStorage.h: -------------------------------------------------------------------------------- 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 _INTERNAL_STORAGE_H_INCLUDED 20 | #define _INTERNAL_STORAGE_H_INCLUDED 21 | 22 | #include "OTAStorage.h" 23 | 24 | class InternalStorageClass : public OTAStorage { 25 | public: 26 | virtual int open(int size); 27 | virtual size_t write(uint8_t); 28 | virtual void close(); 29 | virtual void clear(); 30 | virtual void apply(); 31 | virtual long maxSize(); 32 | 33 | private: 34 | union { 35 | uint32_t u32; 36 | uint8_t u8[4]; 37 | } _addressData; 38 | 39 | int _writeIndex; 40 | uint32_t* _writeAddress; 41 | }; 42 | 43 | extern InternalStorageClass InternalStorage; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /src/InternalStorage.cpp: -------------------------------------------------------------------------------- 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 20 | 21 | #include "InternalStorage.h" 22 | 23 | #define PAGE_SIZE (64) 24 | #define PAGES (4096) 25 | #define MAX_FLASH (PAGE_SIZE * PAGES) 26 | #define ROW_SIZE (PAGE_SIZE * 4) 27 | 28 | #define SKETCH_START_ADDRESS (0x2000) 29 | #define MAX_PARTIONED_SKETCH_SIZE ((MAX_FLASH - SKETCH_START_ADDRESS) / 2) 30 | #define STORAGE_START_ADDRESS (SKETCH_START_ADDRESS + MAX_PARTIONED_SKETCH_SIZE) 31 | 32 | extern "C" 33 | { 34 | // these functions must be in RAM (.data) and NOT inlined 35 | // as they erase and copy the sketch data in flash 36 | 37 | __attribute__((long_call, noinline, section(".data#"))) static void eraseFlash(int address, int length) 38 | { 39 | for (int i = 0; i < length; i += ROW_SIZE) 40 | { 41 | NVMCTRL->ADDR.reg = ((uint32_t)(address + i)) / 2; 42 | NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_ER; 43 | 44 | while (!NVMCTRL->INTFLAG.bit.READY) 45 | ; 46 | } 47 | } 48 | 49 | __attribute__((long_call, noinline, section(".data#"))) static void copyFlashAndReset(int dest, int src, int length) 50 | { 51 | uint32_t *d = (uint32_t *)dest; 52 | uint32_t *s = (uint32_t *)src; 53 | 54 | eraseFlash(dest, length); 55 | 56 | for (int i = 0; i < length; i += 4) 57 | { 58 | *d++ = *s++; 59 | 60 | while (!NVMCTRL->INTFLAG.bit.READY) 61 | ; 62 | } 63 | 64 | NVIC_SystemReset(); 65 | } 66 | } 67 | 68 | int InternalStorageClass::open(int size) 69 | { 70 | (void)size; 71 | _writeIndex = 0; 72 | _writeAddress = (uint32_t *)STORAGE_START_ADDRESS; 73 | 74 | // enable auto page writes 75 | NVMCTRL->CTRLB.bit.MANW = 0; 76 | 77 | eraseFlash(STORAGE_START_ADDRESS, MAX_PARTIONED_SKETCH_SIZE); 78 | 79 | return 1; 80 | } 81 | 82 | size_t InternalStorageClass::write(uint8_t b) 83 | { 84 | _addressData.u8[_writeIndex] = b; 85 | _writeIndex++; 86 | 87 | if (_writeIndex == 4) 88 | { 89 | _writeIndex = 0; 90 | 91 | *_writeAddress = _addressData.u32; 92 | 93 | _writeAddress++; 94 | 95 | while (!NVMCTRL->INTFLAG.bit.READY) 96 | ; 97 | } 98 | 99 | return 1; 100 | } 101 | 102 | void InternalStorageClass::close() 103 | { 104 | while ((int)_writeAddress % PAGE_SIZE) 105 | { 106 | write(0xff); 107 | } 108 | } 109 | 110 | void InternalStorageClass::clear() 111 | { 112 | } 113 | 114 | void InternalStorageClass::apply() 115 | { 116 | // disable interrupts, as vector table will be erase during flash sequence 117 | noInterrupts(); 118 | 119 | copyFlashAndReset(SKETCH_START_ADDRESS, STORAGE_START_ADDRESS, MAX_PARTIONED_SKETCH_SIZE); 120 | } 121 | 122 | long InternalStorageClass::maxSize() 123 | { 124 | return MAX_PARTIONED_SKETCH_SIZE; 125 | } 126 | 127 | InternalStorageClass InternalStorage; 128 | --------------------------------------------------------------------------------