Skip to content

Commit e71cc81

Browse files
committed
Added arduino sketch
1 parent 9a89350 commit e71cc81

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Endianess.ino - Network byte order conversion functions.
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+
21+
bool isBigEndian() {
22+
uint32_t test = 0x11223344;
23+
uint8_t *pTest = reinterpret_cast<uint8_t *>(&test);
24+
return pTest[0] == 0x11;
25+
}
26+
27+
uint32_t fromNetwork32(uint32_t from) {
28+
static const bool be = isBigEndian();
29+
if (be) {
30+
return from;
31+
} else {
32+
uint8_t *pFrom = reinterpret_cast<uint8_t *>(&from);
33+
uint32_t to;
34+
to = pFrom[0]; to <<= 8;
35+
to |= pFrom[1]; to <<= 8;
36+
to |= pFrom[2]; to <<= 8;
37+
to |= pFrom[3];
38+
return to;
39+
}
40+
}
41+
42+
uint16_t fromNetwork16(uint16_t from) {
43+
static bool be = isBigEndian();
44+
if (be) {
45+
return from;
46+
} else {
47+
uint8_t *pFrom = reinterpret_cast<uint8_t *>(&from);
48+
uint16_t to;
49+
to = pFrom[0]; to <<= 8;
50+
to |= pFrom[1];
51+
return to;
52+
}
53+
}
54+
55+
uint32_t toNetwork32(uint32_t to) {
56+
return fromNetwork32(to);
57+
}
58+
59+
uint16_t toNetwork16(uint16_t to) {
60+
return fromNetwork16(to);
61+
}
62+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)