-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp8266-arm-swd.ino
311 lines (257 loc) · 8.76 KB
/
esp8266-arm-swd.ino
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
////////////////////////////////////////////////////////////////
//
// Proof of Concept ESP8266 Wifi + HTTP web interface
// for remote control of an ARM microcontroller
// via its Serial Wire Debug port.
//
// Tested with the FRDM-KE04Z dev kit (KE04 Cortex M0+)
// and the Fadecandy board (K20 Cortex M4)
//
////////////////////////////////////////////////////////////////
// Copyright (c) 2015 Micah Elizabeth Scott
// Released with an MIT-style license; see LICENSE file
// Default pins:
// ESP-01 GPIO0 = swdclk, GPIO2 = swdio
// NodeMCU devkit D3 = swdclk, D4 = swdio
//
// And for reference:
// SWD header pin1 = 3.3v, pin2 = swdio, pin3 = gnd, pin4 = swdclk
// SWD over JTAG TCLK = swdclk, TMS = swdio
const int swd_clock_pin = 0;
const int swd_data_pin = 2;
// Edit these in the YOUR-WIFI-CONFIG tab
extern const char *host, *ssid, *password;
////////////////////////////////////////////////////////////////
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <FS.h>
#include "arm_debug.h"
#include "arm_kinetis_debug.h"
#include "arm_kinetis_reg.h"
ESP8266WebServer server(80);
// Turn the log level back up for debugging; but by default, we have it
// completely off so that even failures happen quickly, to keep the web app responsive.
ARMKinetisDebug target(swd_clock_pin, swd_data_pin, ARMDebug::LOG_NONE);
uint32_t intArg(const char *name)
{
// Like server.arg(name).toInt(), but it handles integer bases other than 10
// with C-style prefixes (0xNUMBER for hex, or 0NUMBER for octal)
uint8_t tmp[64];
server.arg(name).getBytes(tmp, sizeof tmp, 0);
return strtoul((char*) tmp, 0, 0);
}
const char *boolStr(bool x)
{
return x ? "true" : "false";
}
void handleMemRead()
{
uint32_t addr = intArg("addr");
uint32_t count = constrain(intArg("count"), 1, 1024);
uint32_t value;
String output = "[";
while (count) {
if (target.memLoad(addr, value)) {
output += value;
} else {
output += "null";
}
addr += 4;
count--;
if (count) {
output += ",";
}
}
output += "]\n";
server.send(200, "application/json", output);
}
void handleRegRead()
{
uint32_t addr = intArg("addr");
uint32_t count = constrain(intArg("count"), 1, 1024);
uint32_t value;
String output = "[";
while (count) {
if (target.regRead(addr >> 2, value)) {
output += value;
} else {
output += "null";
}
addr += 4;
count--;
if (count) {
output += ",";
}
}
output += "]\n";
server.send(200, "application/json", output);
}
void handleMemWrite()
{
// Interprets the argument list as a list of stores to make in order.
// The key in the key=value pair consists of an address with an optional
// width prefix ('b' = byte wide, 'h' = half width, default = word)
// The address can be a '.' to auto-increment after the previous store.
//
// Returns a confirmation and result for each store, as JSON.
uint32_t addr = -1;
String output = "[\n";
for (int i = 0; server.argName(i).length() > 0; i++) {
uint8_t arg[64];
server.argName(i).getBytes(arg, sizeof arg, 0);
uint8_t *addrString = &arg[arg[0] == 'b' || arg[0] == 'h'];
if (addrString[0] != '.') {
addr = strtoul((char*) addrString, 0, 0);
}
uint8_t valueString[64];
server.arg(i).getBytes(valueString, sizeof valueString, 0);
uint32_t value = strtoul((char*) valueString, 0, 0);
bool result;
const char *storeType = "word";
switch (arg[0]) {
case 'b':
value &= 0xff;
storeType = "byte";
result = target.memStoreByte(addr, value);
addr++;
break;
case 'h':
storeType = "half";
value &= 0xffff;
result = target.memStoreHalf(addr, value);
addr += 2;
break;
default:
result = target.memStore(addr, value);
addr += 4;
break;
}
char buf[128];
snprintf(buf, sizeof buf,
"%s{\"store\": \"%s\", \"addr\": %lu, \"value\": %lu, \"result\": %s}",
i ? "," : "", storeType, addr, value, boolStr(result));
output += buf;
}
output += "\n]";
server.send(200, "application/json", output);
}
void handleRegWrite()
{
String output = "[\n";
for (int i = 0; server.argName(i).length() > 0; i++) {
uint8_t addrString[64];
server.argName(i).getBytes(addrString, sizeof addrString, 0);
uint32_t addr = strtoul((char*) addrString, 0, 0);
uint8_t valueString[64];
server.arg(i).getBytes(valueString, sizeof valueString, 0);
uint32_t value = strtoul((char*) valueString, 0, 0);
bool result = target.regWrite(addr >> 2, value);
char buf[128];
snprintf(buf, sizeof buf,
"%s{\"addr\": %lu, \"value\": %lu, \"result\": %s}",
i ? "," : "", addr, value, boolStr(result));
output += buf;
}
output += "\n]";
server.send(200, "application/json", output);
}
void handleBegin()
{
// See if we can communicate. If so, return information about the target.
// This shouldn't reset the target, but it does need to communicate,
// and the debug port itself will be reset.
//
// If all is well, this returns some identifying info about the target.
uint32_t idcode;
if (target.begin() && target.getIDCODE(idcode)) {
char result[128];
// Note the room left in the API for future platforms detected,
// even though it requires refactoring a bit.
snprintf(result, sizeof result,
"{\"connected\": true, \"idcode\": %lu, \"detected\": %s}",
idcode, target.detect() ? "\"kinetis\"" : "false");
server.send(200, "application/json", result);
} else {
server.send(200, "application/json", "{\"connected\": false}");
}
}
String getContentType(String filename)
{
if (filename.endsWith(".html")) return "text/html";
else if (filename.endsWith(".css")) return "text/css";
else if (filename.endsWith(".js")) return "application/javascript";
else if (filename.endsWith(".png")) return "image/png";
else if (filename.endsWith(".gif")) return "image/gif";
else if (filename.endsWith(".jpg")) return "image/jpeg";
else if (filename.endsWith(".ico")) return "image/x-icon";
else if (filename.endsWith(".xml")) return "text/xml";
else if (filename.endsWith(".pdf")) return "application/x-pdf";
else if (filename.endsWith(".zip")) return "application/x-zip";
else if (filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
bool streamFileIfExists(String path)
{
if (SPIFFS.exists(path)) {
String contentType = getContentType(path);
File f = SPIFFS.open(path, "r");
server.sendHeader("Cache-Control", "max-age=36000");
server.streamFile(f, contentType);
f.close();
return true;
}
return false;
}
void handleStaticFile()
{
String path = server.uri();
if (path.endsWith("/")) {
path += "index";
}
if (!streamFileIfExists(path) &&
!streamFileIfExists(path + ".html")) {
server.send(404, "text/plain",
"File not found,\nin ESP8266FS flash.\n\n"
"Did you run Tools -> ESP8266 Sketch Data Upload?");
}
}
void setup()
{
Serial.begin(115200);
Serial.println("\n\n~ Starting up ~\n");
SPIFFS.begin();
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
String fileName = dir.fileName();
Serial.printf("FS File: %s\n", fileName.c_str());
}
Serial.println();
do {
Serial.println("\nGetting the wifi going...");
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
} while (WiFi.waitForConnectResult() != WL_CONNECTED);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/api/reset", [](){
server.send(200, "application/json", boolStr(target.reset()));});
server.on("/api/halt", [](){
server.send(200, "application/json", boolStr(target.debugHalt()));});
server.on("/api/begin", handleBegin);
server.on("/api/mem/read", handleMemRead);
server.on("/api/mem/write", handleMemWrite);
server.on("/api/reg/read", handleRegRead);
server.on("/api/reg/write", handleRegWrite);
server.onNotFound(handleStaticFile);
server.begin();
MDNS.begin(host);
MDNS.addService("http", "tcp", 80);
Serial.printf("Server is running at http://%s.local/\n", host);
}
void loop()
{
server.handleClient();
delay(1);
}