-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathimpeeduino.device.nut
258 lines (216 loc) · 8.46 KB
/
impeeduino.device.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (c) 2014 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
server.log("Device started, impee_id " + hardware.getimpeeid() + " and mac = " + imp.getmacaddress() );
//------------------------------------------------------------------------------------------------------------------------------
// Uart57 for TX/RX
SERIAL <- hardware.uart57;
SERIAL.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS);
// Drive pin1 high for reset
RESET <- hardware.pin1;
RESET.configure(DIGITAL_OUT);
RESET.write(0);
// Pin 2 is the red LED
ACTIVITY <- hardware.pin2;
ACTIVITY.configure(DIGITAL_OUT);
ACTIVITY.write(1);
// Pin 8 is the orange LED
LINK <- hardware.pin8;
LINK.configure(DIGITAL_OUT);
LINK.write(1);
// Sequence number
__seq <- 0x28;
//------------------------------------------------------------------------------------------------------------------------------
/* STK500 constants list, from AVRDUDE */
const MESSAGE_START = 0x1B;
const TOKEN = 0x0E;
const STK_OK = 0x10;
const STK_FAILED = 0x11; // Not used
const STK_UNKNOWN = 0x12; // Not used
const STK_NODEVICE = 0x13; // Not used
const STK_INSYNC = 0x14; // ' '
const STK_NOSYNC = 0x15; // Not used
const ADC_CHANNEL_ERROR = 0x16; // Not used
const ADC_MEASURE_OK = 0x17; // Not used
const PWM_CHANNEL_ERROR = 0x18; // Not used
const PWM_ADJUST_OK = 0x19; // Not used
const CRC_EOP = 0x20; // 'SPACE'
const STK_GET_SYNC = 0x30; // '0'
const STK_GET_SIGN_ON = 0x31; // '1'
const STK_SET_PARAMETER = 0x40; // '@'
const STK_GET_PARAMETER = 0x41; // 'A'
const STK_SET_DEVICE = 0x42; // 'B'
const STK_SET_DEVICE_EXT = 0x45; // 'E'
const STK_ENTER_PROGMODE = 0x50; // 'P'
const STK_LEAVE_PROGMODE = 0x51; // 'Q'
const STK_CHIP_ERASE = 0x52; // 'R'
const STK_CHECK_AUTOINC = 0x53; // 'S'
const STK_LOAD_ADDRESS = 0x55; // 'U'
const STK_UNIVERSAL = 0x56; // 'V'
const STK_PROG_FLASH = 0x60; // '`'
const STK_PROG_DATA = 0x61; // 'a'
const STK_PROG_FUSE = 0x62; // 'b'
const STK_PROG_LOCK = 0x63; // 'c'
const STK_PROG_PAGE = 0x64; // 'd'
const STK_PROG_FUSE_EXT = 0x65; // 'e'
const STK_READ_FLASH = 0x70; // 'p'
const STK_READ_DATA = 0x71; // 'q'
const STK_READ_FUSE = 0x72; // 'r'
const STK_READ_LOCK = 0x73; // 's'
const STK_READ_PAGE = 0x74; // 't'
const STK_READ_SIGN = 0x75; // 'u'
const STK_READ_OSCCAL = 0x76; // 'v'
const STK_READ_FUSE_EXT = 0x77; // 'w'
const STK_READ_OSCCAL_EXT = 0x78; // 'x'
//------------------------------------------------------------------------------------------------------------------------------
function HEXDUMP(buf, len = null) {
if (buf == null) return "null";
if (len == null) {
len = (typeof buf == "blob") ? buf.tell() : buf.len();
}
local dbg = "";
for (local i = 0; i < len; i++) {
local ch = buf[i];
dbg += format("0x%02X ", ch);
}
return format("%s (%d bytes)", dbg, len)
}
//------------------------------------------------------------------------------------------------------------------------------
function SERIAL_READ(len = 100, timeout = 300) {
local rxbuf = blob(len);
local write = rxbuf.writen.bindenv(rxbuf);
local read = SERIAL.read.bindenv(SERIAL);
local hw = hardware;
local ms = hw.millis.bindenv(hw);
local started = ms();
local charsRead = 0;
LINK.write(0); //Turn LED on
do {
local ch = read();
if (ch != -1) {
write(ch, 'b')
charsRead++;
if(charsRead == len) break;
}
} while (ms() - started < timeout);
LINK.write(1); //Turn LED off
// Clean up any extra bytes
while (SERIAL.read() != -1);
if (rxbuf.tell() == 0) {
return null;
} else {
return rxbuf;
}
}
//------------------------------------------------------------------------------------------------------------------------------
function execute(command = null, param = null, response_length = 100, response_timeout = 300) {
local send_buffer = null;
if (command == null) {
send_buffer = format("%c", CRC_EOP);
} else if (param == null) {
send_buffer = format("%c%c", command, CRC_EOP);
} else if (typeof param == "array") {
send_buffer = format("%c", command);
foreach (datum in param) {
switch (typeof datum) {
case "string":
case "blob":
case "array":
case "table":
foreach (adat in datum) {
send_buffer += format("%c", adat);
}
break;
default:
send_buffer += format("%c", datum);
}
}
send_buffer += format("%c", CRC_EOP);
} else {
send_buffer = format("%c%c%c", command, param, CRC_EOP);
}
// server.log("Sending: " + HEXDUMP(send_buffer));
SERIAL.write(send_buffer);
local resp_buffer = SERIAL_READ(response_length+2, response_timeout);
// server.log("Received: " + HEXDUMP(resp_buffer));
assert(resp_buffer != null);
assert(resp_buffer.tell() >= 2);
assert(resp_buffer[0] == STK_INSYNC);
assert(resp_buffer[resp_buffer.tell()-1] == STK_OK);
local tell = resp_buffer.tell();
if (tell == 2) return blob(0);
resp_buffer.seek(1);
return resp_buffer.readblob(tell-2);
}
//------------------------------------------------------------------------------------------------------------------------------
function check_duino() {
// Clear the read buffer
SERIAL_READ();
// Check everything we can check to ensure we are speaking to the correct boot loader
local major = execute(STK_GET_PARAMETER, 0x81, 1);
local minor = execute(STK_GET_PARAMETER, 0x82, 1);
local invalid = execute(STK_GET_PARAMETER, 0x83, 1);
local signature = execute(STK_READ_SIGN);
assert(major.len() == 1 && minor.len() == 1);
assert((major[0] >= 5) || (major[0] == 4 && minor[0] >= 4));
assert(invalid.len() == 1 && invalid[0] == 0x03);
assert(signature.len() == 3 && signature[0] == 0x1E && signature[1] == 0x95 && signature[2] == 0x0F);
}
//------------------------------------------------------------------------------------------------------------------------------
function program_duino(address16, data) {
local addr8_hi = (address16 >> 8) & 0xFF;
local addr8_lo = address16 & 0xFF;
local data_len = data.len();
execute(STK_LOAD_ADDRESS, [addr8_lo, addr8_hi], 0);
execute(STK_PROG_PAGE, [0x00, data_len, 0x46, data], 0)
local data_check = execute(STK_READ_PAGE, [0x00, data_len, 0x46], data_len)
assert(data_check.len() == data_len);
for (local i = 0; i < data_len; i++) {
assert(data_check[i] == data[i]);
}
}
//------------------------------------------------------------------------------------------------------------------------------
function bounce() {
// Bounce the reset pin
server.log("Bouncing the Arduino reset pin");
imp.sleep(0.5);
ACTIVITY.write(0);
RESET.write(1);
imp.sleep(0.2);
RESET.write(0);
imp.sleep(0.3);
check_duino();
ACTIVITY.write(1);
}
//------------------------------------------------------------------------------------------------------------------------------
function scan_serial() {
local ch = null;
local str = "";
do {
ch = SERIAL.read();
if (ch != -1 && ch != 0) {
str += format("%c", ch);
}
} while (ch != -1);
if (str.len() > 0) {
server.log("Serial: " + str);
}
}
//------------------------------------------------------------------------------------------------------------------------------
function burn(pline) {
if ("first" in pline) {
server.log("Starting to burn");
SERIAL.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS);
bounce();
} else if ("last" in pline) {
server.log("Done!")
agent.send("done", true);
SERIAL.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS, scan_serial);
} else {
program_duino(pline.addr, pline.data);
}
}
//------------------------------------------------------------------------------------------------------------------------------
agent.on("burn", burn);
agent.send("ready", true);
SERIAL.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS, scan_serial);