|
| 1 | +/* |
| 2 | + FirmwareUpdate.h - Firmware Updater for WiFi101 / WINC1500. |
| 3 | + Copyright (c) 2015 Arduino LLC. All right reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <WiFi101.h> |
| 21 | +#include <driver/include/spi_flash.h> |
| 22 | + |
| 23 | +typedef struct __attribute__((__packed__)) { |
| 24 | + uint8_t command; |
| 25 | + uint32_t address; |
| 26 | + uint32_t arg1; |
| 27 | + uint16_t payloadLength; |
| 28 | + |
| 29 | + // payloadLenght bytes of data follows... |
| 30 | +} UartPacket; |
| 31 | + |
| 32 | +static const int MAX_PAYLOAD_SIZE = 1024; |
| 33 | + |
| 34 | +#define CMD_READ_FLASH 0x01 |
| 35 | +#define CMD_WRITE_FLASH 0x02 |
| 36 | +#define CMD_ERASE_FLASH 0x03 |
| 37 | +#define CMD_MAX_PAYLOAD_SIZE 0x50 |
| 38 | +#define CMD_HELLO 0x99 |
| 39 | + |
| 40 | +void setup() { |
| 41 | + Serial.begin(115200); |
| 42 | + |
| 43 | + nm_bsp_init(); |
| 44 | + if (m2m_wifi_download_mode() != M2M_SUCCESS) { |
| 45 | + Serial.println(F("Failed to put the WiFi module in download mode")); |
| 46 | + while (true) |
| 47 | + ; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void receivePacket(UartPacket *pkt, uint8_t *payload) { |
| 52 | + // Read command |
| 53 | + uint8_t *p = reinterpret_cast<uint8_t *>(pkt); |
| 54 | + uint16_t l = sizeof(UartPacket); |
| 55 | + while (l > 0) { |
| 56 | + int c = Serial.read(); |
| 57 | + if (c == -1) |
| 58 | + continue; |
| 59 | + *p++ = c; |
| 60 | + l--; |
| 61 | + } |
| 62 | + |
| 63 | + // Convert parameters from network byte order to cpu byte order |
| 64 | + pkt->address = fromNetwork32(pkt->address); |
| 65 | + pkt->arg1 = fromNetwork32(pkt->arg1); |
| 66 | + pkt->payloadLength = fromNetwork16(pkt->payloadLength); |
| 67 | + |
| 68 | + // Read payload |
| 69 | + l = pkt->payloadLength; |
| 70 | + while (l > 0) { |
| 71 | + int c = Serial.read(); |
| 72 | + if (c == -1) |
| 73 | + continue; |
| 74 | + *payload++ = c; |
| 75 | + l--; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Allocated statically so the compiler can tell us |
| 80 | +// about the amount of used RAM |
| 81 | +static UartPacket pkt; |
| 82 | +static uint8_t payload[MAX_PAYLOAD_SIZE]; |
| 83 | + |
| 84 | +void loop() { |
| 85 | + receivePacket(&pkt, payload); |
| 86 | + |
| 87 | + if (pkt.command == CMD_HELLO) { |
| 88 | + if (pkt.address == 0x11223344 && pkt.arg1 == 0x55667788) |
| 89 | + Serial.print("v10000"); |
| 90 | + } |
| 91 | + |
| 92 | + if (pkt.command == CMD_MAX_PAYLOAD_SIZE) { |
| 93 | + uint16_t res = toNetwork16(MAX_PAYLOAD_SIZE); |
| 94 | + Serial.write(reinterpret_cast<uint8_t *>(&res), sizeof(res)); |
| 95 | + } |
| 96 | + |
| 97 | + if (pkt.command == CMD_READ_FLASH) { |
| 98 | + uint32_t address = pkt.address; |
| 99 | + uint32_t len = pkt.arg1; |
| 100 | + if (spi_flash_read(payload, address, len) != M2M_SUCCESS) { |
| 101 | + Serial.println("ER"); |
| 102 | + } else { |
| 103 | + Serial.write(payload, len); |
| 104 | + Serial.print("OK"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (pkt.command == CMD_WRITE_FLASH) { |
| 109 | + uint32_t address = pkt.address; |
| 110 | + uint32_t len = pkt.payloadLength; |
| 111 | + if (spi_flash_write(payload, address, len) != M2M_SUCCESS) { |
| 112 | + Serial.print("ER"); |
| 113 | + } else { |
| 114 | + Serial.print("OK"); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (pkt.command == CMD_ERASE_FLASH) { |
| 119 | + uint32_t address = pkt.address; |
| 120 | + uint32_t len = pkt.arg1; |
| 121 | + if (spi_flash_erase(address, len) != M2M_SUCCESS) { |
| 122 | + Serial.print("ER"); |
| 123 | + } else { |
| 124 | + Serial.print("OK"); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | + |
0 commit comments