-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathA0RET.ino
258 lines (209 loc) · 8.39 KB
/
A0RET.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
// December 2024 updated for arduino-esp32 core 2.0.x (Tested on 2.0.16 & 2.0.17)
// Use partition scheme: Minimal SPIFFS (1.9MB APP with OTA/190KB SPIFFS)
// Set CAN TX/RX pins in config.h, lines 39 & 40
/*
A0RET.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.
*/
/*
This code is now all specialized for the A0 and its ESP32 WRover-B which has 4MB flash and
8MB of PSRAM. It also only has a single CAN port.
*/
#include "config.h"
#include <esp32_can.h>
#include <SPI.h>
#include <Preferences.h>
#include "ELM327_Emulator.h"
#include "SerialConsole.h"
#include "wifi_manager.h"
#include "gvret_comm.h"
#include "can_manager.h"
#include "lawicel.h"
byte i = 0;
uint32_t lastFlushMicros = 0;
bool markToggle[6];
uint32_t lastMarkTrigger = 0;
EEPROMSettings settings;
SystemSettings SysSettings;
Preferences nvPrefs;
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;
//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....");
nvPrefs.begin(PREF_NAME, false);
settings.CAN0Speed = nvPrefs.getUInt("can0speed", 500000);
settings.CAN0_Enabled = nvPrefs.getBool("can0_en", true);
settings.CAN0ListenOnly = nvPrefs.getBool("can0-listenonly", 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);
settings.systemType = nvPrefs.getUChar("systype", 0);
settings.CAN1Speed = nvPrefs.getUInt("can1speed", 500000);
settings.CAN1_Enabled = nvPrefs.getBool("can1_en", false);
settings.CAN1ListenOnly = nvPrefs.getBool("can1-listenonly", false);
if (nvPrefs.getString("SSID", settings.SSID, 32) == 0)
{
strcpy(settings.SSID, "ESP32SSID");
}
if (nvPrefs.getString("wpa2Key", settings.WPA2Key, 64) == 0)
{
strcpy(settings.WPA2Key, "aBigSecret");
}
if (nvPrefs.getString("btname", settings.btName, 32) == 0)
{
strcpy(settings.btName, "ELM327-A0");
}
nvPrefs.end();
Logger::setLoglevel((Logger::LogLevel)settings.logLevel);
if (settings.systemType == 0)
{
Logger::console("Running on Macchina A0");
SysSettings.LED_CANTX = 255;
SysSettings.LED_CANRX = 13;
SysSettings.LED_LOGGING = 255;
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;
}
if (settings.systemType == 1)
{
Logger::console("Running on EVTV ESP32 Board");
SysSettings.LED_CANTX = 255;
SysSettings.LED_CANRX = 255;
SysSettings.LED_LOGGING = 255;
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;
}
for (int rx = 0; rx < NUM_BUSES; rx++) SysSettings.lawicelBusReception[rx] = true; //default to showing messages on RX
//set pin mode for all LEDS
}
void setup()
{
//delay(5000); //just for testing. Don't use in production
Serial.begin(1000000); //for production
//Serial.begin(115200); //for testing
SysSettings.isWifiConnected = false;
loadSettings();
//If you enable PSRAM then BluetoothSerial will kill everything via a heap error. These calls
//try to debug that. But, no dice yet. :(
//heap_caps_print_heap_info(MALLOC_CAP_8BIT);
wifiManager.setup();
//heap_caps_print_heap_info(MALLOC_CAP_8BIT);
if (settings.enableBT)
{
Serial.println("Starting bluetooth");
elmEmulator.setup();
}
//heap_caps_print_heap_info(MALLOC_CAP_8BIT);
Serial.print("Build number: ");
Serial.println(CFG_BUILD_NUM);
canManager.setup();
SysSettings.lawicelMode = false;
SysSettings.lawicelAutoPoll = false;
SysSettings.lawicelTimestamping = false;
SysSettings.lawicelPollCounter = 0;
//elmEmulator.setup();
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();
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);
}
if (settings.enableBT) elmEmulator.loop();
}