-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathESP32RET.ino
402 lines (341 loc) · 13.5 KB
/
ESP32RET.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
ESP32RET.ino
Created: June 1, 2020
Author: Collin Kidder
Copyright (c) 2014-2020 Collin Kidder, Michael Neuweiler
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), 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.
*/
#include "config.h"
#include <esp32_can.h>
#include <SPI.h>
#include <esp32_mcp2517fd.h>
#include <Preferences.h>
#include <FastLED.h>
#include "ELM327_Emulator.h"
#include "SerialConsole.h"
#include "wifi_manager.h"
#include "gvret_comm.h"
#include "can_manager.h"
#include "lawicel.h"
//on the S3 we want the default pins to be different
#ifdef CONFIG_IDF_TARGET_ESP32S3
MCP2517FD CAN1(10, 3);
#endif
byte i = 0;
uint32_t lastFlushMicros = 0;
bool markToggle[6];
uint32_t lastMarkTrigger = 0;
EEPROMSettings settings;
SystemSettings SysSettings;
Preferences nvPrefs;
char deviceName[20];
char otaHost[40];
char otaFilename[100];
uint8_t espChipRevision;
ELM327Emu elmEmulator;
WiFiManager wifiManager;
GVRET_Comm_Handler serialGVRET; //gvret protocol over the serial to USB connection
GVRET_Comm_Handler wifiGVRET; //GVRET over the wifi telnet port
CANManager canManager; //keeps track of bus load and abstracts away some details of how things are done
LAWICELHandler lawicel;
SerialConsole console;
CRGB leds[A5_NUM_LEDS]; //A5 has the largest # of LEDs so use that one even for A0 or EVTV
CAN_COMMON *canBuses[NUM_BUSES];
//initializes all the system EEPROM values. Chances are this should be broken out a bit but
//there is only one checksum check for all of them so it's simple to do it all here.
void loadSettings()
{
Logger::console("Loading settings....");
//Logger::console("%i\n", espChipRevision);
for (int i = 0; i < NUM_BUSES; i++) canBuses[i] = nullptr;
nvPrefs.begin(PREF_NAME, false);
settings.useBinarySerialComm = nvPrefs.getBool("binarycomm", false);
settings.logLevel = nvPrefs.getUChar("loglevel", 1); //info
settings.wifiMode = nvPrefs.getUChar("wifiMode", 2); //Wifi defaults to creating an AP
settings.enableBT = nvPrefs.getBool("enable-bt", false);
settings.enableLawicel = nvPrefs.getBool("enableLawicel", true);
uint8_t defaultVal = (espChipRevision > 2) ? 0 : 1; //0 = A0, 1 = EVTV ESP32
#ifdef CONFIG_IDF_TARGET_ESP32S3
defaultVal = 3;
#endif
settings.systemType = nvPrefs.getUChar("systype", defaultVal);
if (settings.systemType == 0)
{
Logger::console("Running on Macchina A0");
canBuses[0] = &CAN0;
SysSettings.LED_CANTX = 255;
SysSettings.LED_CANRX = 255;
SysSettings.LED_LOGGING = 255;
SysSettings.LED_CONNECTION_STATUS = 0;
SysSettings.fancyLED = true;
SysSettings.logToggle = false;
SysSettings.txToggle = true;
SysSettings.rxToggle = true;
SysSettings.lawicelAutoPoll = false;
SysSettings.lawicelMode = false;
SysSettings.lawicellExtendedMode = false;
SysSettings.lawicelTimestamping = false;
SysSettings.numBuses = 1; //Currently we support CAN0
SysSettings.isWifiActive = false;
SysSettings.isWifiConnected = false;
strcpy(deviceName, MACC_NAME);
strcpy(otaHost, "macchina.cc");
strcpy(otaFilename, "/a0/files/a0ret.bin");
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
delay(100);
FastLED.addLeds<LED_TYPE, A0_LED_PIN, COLOR_ORDER>(leds, A0_NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
leds[0] = CRGB::Red;
FastLED.show();
pinMode(21, OUTPUT);
digitalWrite(21, LOW);
CAN0.setCANPins(GPIO_NUM_4, GPIO_NUM_5);
}
if (settings.systemType == 1)
{
Logger::console("Running on EVTV ESP32 Board");
canBuses[0] = &CAN0;
canBuses[1] = &CAN1;
SysSettings.LED_CANTX = 255;
SysSettings.LED_CANRX = 255;
SysSettings.LED_LOGGING = 255;
SysSettings.LED_CONNECTION_STATUS = 255;
SysSettings.fancyLED = false;
SysSettings.logToggle = false;
SysSettings.txToggle = true;
SysSettings.rxToggle = true;
SysSettings.lawicelAutoPoll = false;
SysSettings.lawicelMode = false;
SysSettings.lawicellExtendedMode = false;
SysSettings.lawicelTimestamping = false;
SysSettings.numBuses = 2;
SysSettings.isWifiActive = false;
SysSettings.isWifiConnected = false;
strcpy(deviceName, EVTV_NAME);
strcpy(otaHost, "media3.evtv.me");
strcpy(otaFilename, "/esp32ret.bin");
}
if (settings.systemType == 2)
{
Logger::console("Running on Macchina 5-CAN");
canBuses[0] = &CAN0; //SWCAN on this hardware - DLC pin 1
canBuses[1] = &CAN1; //DLC pins 1 and 9. Overlaps with SWCAN
canBuses[2] = new MCP2517FD(33, 39); //DLC pins 3/11
canBuses[3] = new MCP2517FD(25, 34); //DLC pins 6/14
canBuses[4] = new MCP2517FD(14, 13); //DLC pins 12/13
//reconfigure the two already defined CAN buses to use the actual pins for this board.
CAN0.setCANPins(GPIO_NUM_4, GPIO_NUM_5); //rx, tx - This is the SWCAN interface
CAN1.setINTPin(36);
CAN1.setCSPin(32);
SysSettings.LED_CANTX = 0;
SysSettings.LED_CANRX = 1;
SysSettings.LED_LOGGING = 2;
SysSettings.LED_CONNECTION_STATUS = 3;
SysSettings.fancyLED = true;
SysSettings.logToggle = false;
SysSettings.txToggle = true;
SysSettings.rxToggle = true;
SysSettings.lawicelAutoPoll = false;
SysSettings.lawicelMode = false;
SysSettings.lawicellExtendedMode = false;
SysSettings.lawicelTimestamping = false;
SysSettings.numBuses = 5;
SysSettings.isWifiActive = false;
SysSettings.isWifiConnected = false;
FastLED.addLeds<LED_TYPE, A5_LED_PIN, COLOR_ORDER>(leds, A5_NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
//With the board facing up and looking at the USB end the LEDs are 0 1 2 (USB) 3
//can test LEDs here for debugging but normally leave first three off and set connection to RED.
//leds[0] = CRGB::White;
//leds[1] = CRGB::Blue;
//leds[2] = CRGB::Green;
leds[3] = CRGB::Red;
FastLED.show();
strcpy(deviceName, MACC_NAME);
strcpy(otaHost, "macchina.cc");
strcpy(otaFilename, "/a0/files/a0ret.bin");
//Single wire interface
pinMode(SW_EN, OUTPUT);
pinMode(SW_MODE0, OUTPUT);
pinMode(SW_MODE1, OUTPUT);
digitalWrite(SW_EN, LOW); //MUST be LOW to use CAN1 channel
//HH = Normal Mode
digitalWrite(SW_MODE0, HIGH);
digitalWrite(SW_MODE1, HIGH);
}
if (settings.systemType == 3)
{
Logger::console("Running on EVTV ESP32-S3 Board");
canBuses[0] = &CAN0;
canBuses[1] = &CAN1;
//CAN1.setINTPin(3);
//CAN1.setCSPin(10);
SysSettings.LED_CANTX = 255;//18;
SysSettings.LED_CANRX = 255;//18;
SysSettings.LED_LOGGING = 255;
SysSettings.LED_CONNECTION_STATUS = 255;
SysSettings.fancyLED = false;
SysSettings.logToggle = false;
SysSettings.txToggle = true;
SysSettings.rxToggle = true;
SysSettings.lawicelAutoPoll = false;
SysSettings.lawicelMode = false;
SysSettings.lawicellExtendedMode = false;
SysSettings.lawicelTimestamping = false;
SysSettings.numBuses = 2;
SysSettings.isWifiActive = false;
SysSettings.isWifiConnected = false;
strcpy(deviceName, EVTV_NAME);
strcpy(otaHost, "media3.evtv.me");
strcpy(otaFilename, "/esp32s3ret.bin");
}
if (nvPrefs.getString("SSID", settings.SSID, 32) == 0)
{
strcpy(settings.SSID, deviceName);
strcat(settings.SSID, "SSID");
}
if (nvPrefs.getString("wpa2Key", settings.WPA2Key, 64) == 0)
{
strcpy(settings.WPA2Key, "aBigSecret");
}
if (nvPrefs.getString("btname", settings.btName, 32) == 0)
{
strcpy(settings.btName, "ELM327-");
strcat(settings.btName, deviceName);
}
char buff[80];
for (int i = 0; i < SysSettings.numBuses; i++)
{
sprintf(buff, "can%ispeed", i);
settings.canSettings[i].nomSpeed = nvPrefs.getUInt(buff, 500000);
sprintf(buff, "can%i_en", i);
settings.canSettings[i].enabled = nvPrefs.getBool(buff, (i < 2)?true:false);
sprintf(buff, "can%i-listenonly", i);
settings.canSettings[i].listenOnly = nvPrefs.getBool(buff, false);
sprintf(buff, "can%i-fdspeed", i);
settings.canSettings[i].fdSpeed = nvPrefs.getUInt(buff, 5000000);
sprintf(buff, "can%i-fdmode", i);
settings.canSettings[i].fdMode = nvPrefs.getBool(buff, false);
}
nvPrefs.end();
Logger::setLoglevel((Logger::LogLevel)settings.logLevel);
for (int rx = 0; rx < NUM_BUSES; rx++) SysSettings.lawicelBusReception[rx] = true; //default to showing messages on RX
}
void setup()
{
#ifdef CONFIG_IDF_TARGET_ESP32S3
//for the ESP32S3 it will block if nothing is connected to USB and that can slow down the program
//if nothing is connected. But, you can't set 0 or writing rapidly to USB will lose data. It needs
//some sort of timeout but I'm not sure exactly how much is needed or if there is a better way
//to deal with this issue.
Serial.setTxTimeoutMs(2);
#endif
Serial.begin(1000000); //for production
//Serial.begin(115200); //for testing
//delay(2000); //just for testing. Don't use in production
espChipRevision = ESP.getChipRevision();
Serial.print("Build number: ");
Serial.println(CFG_BUILD_NUM);
SysSettings.isWifiConnected = false;
loadSettings();
//CAN0.setDebuggingMode(true);
//CAN1.setDebuggingMode(true);
canManager.setup();
if (settings.enableBT)
{
Serial.println("Starting bluetooth");
elmEmulator.setup();
if (SysSettings.fancyLED && (settings.wifiMode == 0) )
{
leds[0] = CRGB::Green;
FastLED.show();
}
}
/*else*/ wifiManager.setup();
SysSettings.lawicelMode = false;
SysSettings.lawicelAutoPoll = false;
SysSettings.lawicelTimestamping = false;
SysSettings.lawicelPollCounter = 0;
elmEmulator.setup();
Serial.print("Free heap after setup: ");
Serial.println(esp_get_free_heap_size());
Serial.print("Done with init\n");
}
/*
Send a fake frame out USB and maybe to file to show where the mark was triggered at. The fake frame has bits 31 through 3
set which can never happen in reality since frames are either 11 or 29 bit IDs. So, this is a sign that it is a mark frame
and not a real frame. The bottom three bits specify which mark triggered.
*/
void sendMarkTriggered(int which)
{
CAN_FRAME frame;
frame.id = 0xFFFFFFF8ull + which;
frame.extended = true;
frame.length = 0;
frame.rtr = 0;
canManager.displayFrame(frame, 0);
}
/*
Loop executes as often as possible all the while interrupts fire in the background.
The serial comm protocol is as follows:
All commands start with 0xF1 this helps to synchronize if there were comm issues
Then the next byte specifies which command this is.
Then the command data bytes which are specific to the command
Lastly, there is a checksum byte just to be sure there are no missed or duped bytes
Any bytes between checksum and 0xF1 are thrown away
Yes, this should probably have been done more neatly but this way is likely to be the
fastest and safest with limited function calls
*/
void loop()
{
//uint32_t temp32;
bool isConnected = false;
int serialCnt;
uint8_t in_byte;
/*if (Serial)*/ isConnected = true;
if (SysSettings.lawicelPollCounter > 0) SysSettings.lawicelPollCounter--;
//}
canManager.loop();
/*if (!settings.enableBT)*/ wifiManager.loop();
size_t wifiLength = wifiGVRET.numAvailableBytes();
size_t serialLength = serialGVRET.numAvailableBytes();
size_t maxLength = (wifiLength>serialLength) ? wifiLength : serialLength;
//If the max time has passed or the buffer is almost filled then send buffered data out
if ((micros() - lastFlushMicros > SER_BUFF_FLUSH_INTERVAL) || (maxLength > (WIFI_BUFF_SIZE - 40)) )
{
lastFlushMicros = micros();
if (serialLength > 0)
{
Serial.write(serialGVRET.getBufferedBytes(), serialLength);
serialGVRET.clearBufferedBytes();
}
if (wifiLength > 0)
{
wifiManager.sendBufferedData();
}
}
serialCnt = 0;
while ( (Serial.available() > 0) && serialCnt < 128 )
{
serialCnt++;
in_byte = Serial.read();
serialGVRET.processIncomingByte(in_byte);
}
elmEmulator.loop();
}