-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
M5Stack-ESP12-Shield.ino
311 lines (261 loc) · 8.34 KB
/
M5Stack-ESP12-Shield.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
/*
ESP12 Flasher for M5Stack-ESP12-Shield
Project Page: https://github.com/tobozo/M5Stack-ESP12-Shield
Copyright 2018 tobozo http://github.com/tobozo
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files ("M5Stack ESP12 Shield"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
PIN Mapping:
M5Stack | ESP12
------------+------------
16 (Rx2) | D1 (TX0)
17 (Tx2) | D3 (RX0)
12 | D0 (FLASH)
13 | RESET
3.3v | VCC
3.3v | D2
3.3v | CH_PD (EN)
GND | GND
GND | D15
Using the Arduino IDE:
- Compile & Upload this sketch on the M5Stack
- Change board to ESP8266
- Set Baudrate to "115200"
- Set Flash Mode to "DOUT"
- Set Reset Method to "no dtr (aka ck)"
- Open your ESP8266 sketch
- Hit the Button B on the M5 so the ESP8266 waits for flashing
- Hit the Upload button
Also acts as a serial bridge (115200) after flash timeout.
*/
#include <ESP32-Chimera-Core.h> // available from the Arduino Library Manager or https://github.com/tobozo/ESP32-Chimera-Core
#define tft M5.Lcd
#include <M5StackUpdater.h> // available from the Arduino Library Manager or https://github.com/tobozo/M5Stack-SD-Updater
#include <Preferences.h>
#include "esp12.h"
#include "scrollpanel.h"
// For some reason GPIO12 and GPIO13 pins aren't exposed by the battery shield and the
// other available pins wouldn't work as expected (boot HIGH, no pullup or input only).
// I had to get a M5 Proto Board to be able to access those pins properly, but they're
// available directly on the socket http://forum.m5stack.com/topic/188/bottom-io-expander-pinout
#define ESP8266_RESET_PIN 13
#define ESP8266_FLASH_PIN 12
#define FLASH_TIMEOUT 10000 // timeout delay (ms) before acting as a serial bridge
HardwareSerial ESP12Serial(2); // ESP32 UART2 GPIO-16 ( RXD2 )
Preferences prefs;
unsigned long last_activity = 0;
unsigned long start_time = 0;
bool intro_done = false;
bool ignore_timeout = false; // UI logic: wait for timeout before showing UI menu
bool bg_rendered = false;
bool scrollPanelIsActive = false;
bool isflashon = false;
static uint16_t laststatus = 0;
static int baudRate = 115200;
static uint32_t rxbytes = 0;
static uint32_t txbytes = 0;
void displayMsg(const char* title= "", const char* msg = "")
{
tft.fillRect(0, 196, 319, 48, TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextDatum( TC_DATUM );
tft.setFont( &Font4 );
tft.setTextSize( 1 );
tft.drawString(title, tft.width() / 2, 196);
tft.setTextSize( 0.75 );
tft.drawString(msg, tft.width() / 2, 222);
}
void displayIntro()
{
tft.fillScreen(BLACK);
tft.fillRect(0, 0, 319, 184, TFT_WHITE);
tft.drawJpg( doc_intrologo_jpg, doc_intrologo_jpg_len );
tft.setTextColor(TFT_BLACK, TFT_WHITE);
tft.setTextDatum( TC_DATUM );
tft.setFont( &Font4 );
tft.setTextSize( 1 );
tft.drawString("M5Stack-ESP12 Flasher", tft.width() / 2, 105);
tft.setFont( &Font2 );
tft.drawString(F("Copyright 2018-2020 tobozo"), tft.width() / 2, 145);
}
void displayComStatus( uint16_t color )
{
if( laststatus != color ) {
laststatus = color;
tft.fillCircle( 6, 6, 5, color );
}
if( ! bg_rendered ) {
displayScrollTitle( baudRate, rxbytes, txbytes, TFT_WHITE );
}
}
void resetOn()
{
digitalWrite(ESP8266_RESET_PIN, LOW);
displayMsg("PIN STATE CHANGE", "Reset Pin: ON");
}
void resetOff()
{
digitalWrite(ESP8266_RESET_PIN, HIGH);
displayMsg("PIN STATE CHANGE", "Reset Pin: OFF");
}
void flashOn()
{
digitalWrite(ESP8266_FLASH_PIN, LOW);
displayMsg("PIN STATE CHANGE", "Flash Pin: ON");
}
void flashOff()
{
digitalWrite(ESP8266_FLASH_PIN, HIGH);
displayMsg("PIN STATE CHANGE", "Flash Pin: OFF");
}
void doResetESP()
{
resetOn();
delay(300);
resetOff();
displayMsg("BRIDGE MODE", "Reset done!");
isflashon = false;
}
void enableFlashMode()
{
flashOn();
resetOn();
delay(100);
resetOff();
delay(100);
flashOff();
displayMsg("FLASH MODE", "ESP8266 awaits flashing..");
isflashon = true;
}
void setup()
{
M5.begin(); // this starts serial at 115200
// retrieve last baud rate if any
prefs.begin("ESP12-Shield", true);
baudRate = prefs.getUInt("baudRate", 115200 );
prefs.end();
if( baudRate != 115200 ) {
Serial.end();
Serial.begin( baudRate );
}
ESP12Serial.begin( baudRate );
ESP12Serial.setRxBufferSize(8192); // big buffer to avoid crashes
scrollSetup();
displayIntro();
pinMode(BUTTON_A_PIN, INPUT_PULLUP); // Set A button
pinMode(BUTTON_B_PIN, INPUT_PULLUP); // Set B button
pinMode(BUTTON_C_PIN, INPUT_PULLUP); // Set C button
pinMode(ESP8266_RESET_PIN, OUTPUT); // Set Reset Pin on the ESP12
pinMode(ESP8266_FLASH_PIN, OUTPUT); // Set Flash Pin on the ESP12
if (digitalRead(BUTTON_A_PIN) == 0) {
updateFromFS(SD);
ESP.restart();
}
last_activity = millis();
start_time = millis();
enableFlashMode();
}
void loop()
{
// ESP12 talking to the M5Stack (or forwarding to Arduino IDE)
while (ESP12Serial.available()) {
char c = ESP12Serial.read();
Serial.print(c);
rxbytes++;
if( !isflashon ) {
// do scrolly animation only when not flashing
if(! scrollPanelIsActive || bg_rendered ) {
// render panel title/borders once
scrollPanelIsActive = true;
scrollReset();
bg_rendered = false;
}
scrollLinePushChar( c );
}
displayComStatus( TFT_GREEN );
last_activity = millis();
}
// M5Stack (or Arduino IDE forwarded) talking to the ESP12
while (Serial.available()) {
char c = Serial.read();
ESP12Serial.print(c);
txbytes++;
last_activity = millis();
displayComStatus( TFT_RED );
}
displayComStatus( TFT_DARKGREY );
M5.update();
if ( M5.BtnA.wasPressed()) {
ignore_timeout = false;
last_activity = millis();
doResetESP();
bg_rendered = false;
} else if ( M5.BtnB.wasPressed()) { // toggle flash mode
if( isflashon ) {
doResetESP();
isflashon = false;
bg_rendered = false;
} else {
enableFlashMode();
isflashon = true;
}
ignore_timeout = true;
} else if ( M5.BtnC.wasPressed()) {
// TODO: toggle Serial1 speed
Serial.printf("old baudrate: %d ", baudRate);
switch( baudRate ) {
case 921600: baudRate = 230400; break;
case 230400: baudRate = 115200; break;
case 115200: baudRate = 57600 ; break;
case 57600 : baudRate = 38400 ; break;
case 38400 : baudRate = 28800 ; break;
case 28800 : baudRate = 14400 ; break;
case 14400 : baudRate = 9600 ; break;
case 9600 : baudRate = 921600; break;
default : baudRate = 9600; break;
}
Serial.printf("new baudrate: %d ", baudRate);
ESP12Serial.end();
delay( 100 );
ESP12Serial.begin( baudRate );
displayMsg("Baud Rate", String( baudRate ).c_str() );
// save baud rate to NVS
prefs.begin("ESP12-Shield", false);
prefs.putUInt("baudRate", baudRate );
prefs.end();
ignore_timeout = true;
}
if (!ignore_timeout) {
if (last_activity + FLASH_TIMEOUT < millis()) {
ignore_timeout = true;
scrollReset();
scrollPanelIsActive = false;
flashOff();
displayMsg("IDLE MODE", "Reset Flash Toggle");
if(!bg_rendered) {
tft.drawJpg(esp12_jpg, esp12_jpg_len);
bg_rendered = true;
}
}
}
if (! intro_done ) {
if( millis()-start_time > 2000 ) {
intro_done = true;
}
}
}