|
| 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 "SerialFlashStorage.h" |
| 20 | + |
| 21 | +#define UPDATE_FILE "UPDATE.BIN" |
| 22 | + |
| 23 | +int SerialFlashStorageClass::open(int contentLength) |
| 24 | +{ |
| 25 | + if (!SerialFlash.begin(SERIAL_FLASH_CS)) { |
| 26 | + return 0; |
| 27 | + } |
| 28 | + |
| 29 | + while (!SerialFlash.ready()) {} |
| 30 | + |
| 31 | + if (SerialFlash.exists(UPDATE_FILE)) { |
| 32 | + SerialFlash.remove(UPDATE_FILE); |
| 33 | + } |
| 34 | + |
| 35 | + if (SerialFlash.create(UPDATE_FILE, contentLength)) { |
| 36 | + _file = SerialFlash.open(UPDATE_FILE); |
| 37 | + } |
| 38 | + |
| 39 | + if (!_file) { |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + return 1; |
| 44 | +} |
| 45 | + |
| 46 | +size_t SerialFlashStorageClass::write(uint8_t *data, size_t size) |
| 47 | +{ |
| 48 | + while (!SerialFlash.ready()) {} |
| 49 | + int ret = _file.write(data, size); |
| 50 | + return ret; |
| 51 | +} |
| 52 | + |
| 53 | +void SerialFlashStorageClass::close() |
| 54 | +{ |
| 55 | + _file.close(); |
| 56 | +} |
| 57 | + |
| 58 | +void SerialFlashStorageClass::clear() |
| 59 | +{ |
| 60 | + SerialFlash.remove(UPDATE_FILE); |
| 61 | +} |
| 62 | + |
| 63 | +void SerialFlashStorageClass::apply() |
| 64 | +{ |
| 65 | + // just reset, SDU copies the data to flash |
| 66 | + NVIC_SystemReset(); |
| 67 | +} |
| 68 | + |
| 69 | +SerialFlashStorageClass SerialFlashStorage; |
0 commit comments