-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoyotaread.h
548 lines (485 loc) · 12 KB
/
toyotaread.h
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//PMV-C210
IRAM_ATTR void ClearTPMSData(int i)
{
if (i < TYRECOUNT_ON_DISPLAY) {
TPMS[i].TPMS_ID = TPMS[i].lastupdated = 0;
TPMS[i].Typename='-';
}
}
IRAM_ATTR void PulseDebugPin(int width_us)
{
digitalWrite(DEBUGPIN, HIGH);
delayMicroseconds(width_us);
digitalWrite(DEBUGPIN, LOW);
}
IRAM_ATTR int GetPreferredIndex(unsigned long ID)
{
for (int i = 0; i < TYRECOUNT_IN_DBASE; i++)
{
if (IDLookup[i][0] == ID)
return (i);
}
return (-1);
}
IRAM_ATTR void PrintTimings(byte StartPoint, byte Count)
{
for (int i = 0; i < Count; i++)
{
Serial.print(Timings[StartPoint + i]);
Serial.print(F(","));
}
Serial.println(F(""));
}
IRAM_ATTR void PrintData(byte Count)
{
byte hexdata;
for (int i = 0; i < Count; i++)
{
Serial.print(IncomingBits[i]);
hexdata = (hexdata << 1) + IncomingBits[i];
if ((i + 1) % 8 == 0)
{
Serial.print(F(" ["));
Serial.print(hexdata, HEX);
Serial.print(F("] "));
hexdata = 0;
}
}
Serial.println(F(""));
}
IRAM_ATTR void InitTPMS()
{
for (int i = 0; i < TYRECOUNT_ON_DISPLAY; i++)
{
ClearTPMSData(i);
}
UpdateDisplay();
}
IRAM_ATTR void UpdateTPMSData(int index, unsigned long ID, unsigned int status, float Temperature, float Pressure, char Typename)
{
if (index < TYRECOUNT_ON_DISPLAY)
{
TPMS[index].TPMS_ID = ID;
TPMS[index].TPMS_Status = status;
TPMS[index].lastupdated = millis();
TPMS[index].TPMS_Temperature = Temperature;
TPMS[index].Typename=Typename;
#ifdef USE_BAR
TPMS[index].TPMS_Pressure = Pressure/PSI2BAR;
#else
TPMS[index].TPMS_Pressure = Pressure;
#endif
}
}
IRAM_ATTR void DisplayStatusInfo()
{
Serial.print (F("FreqOffset: "));
Serial.print (FreqOffset);
Serial.print (F(" DemodLinkQuality: "));
Serial.print (DemodLinkQuality);
Serial.print (F(" RSSI: "));
Serial.println (RSSIvalue);
}
IRAM_ATTR boolean Check_TPMS_Timeouts()
{
boolean ret = false;
//clear any data not updated in the last 5 minutes
for (int i = 0; i < TYRECOUNT_ON_DISPLAY; i++)
{
if ((TPMS[i].TPMS_ID != 0) && (millis() - TPMS[i].lastupdated > TPMS_TIMEOUT))
{
ClearTPMSData(i);
ret = true;
}
}
return(ret);
}
IRAM_ATTR void DecodeTPMS()
{
unsigned long id = 0;
unsigned int status, pressure1, pressure2, temp;
float realpressure;
float realtemp;
bool IDFound = false;
int prefindex;
for (int i = 0; i < 4; i++)
{
id = id << 8;
id = id + RXBytes[i];
}
id = id & 0xFFFFFFF;
// id = (unsigned)RXBytes[0] << 24 | RXBytes[1] << 16 | RXBytes[2] << 8 | RXBytes[3];
status = (RXBytes[4] & 0x80) | (RXBytes[6] & 0x7f); // status bit and 0 filler
pressure1 = (RXBytes[4] & 0x7f) << 1 | RXBytes[5] >> 7;
temp = (RXBytes[5] & 0x7f) << 1 | RXBytes[6] >> 7;
pressure2 = RXBytes[7] ^ 0xff;
if (pressure1 != pressure2)
{
Serial.println(F("Pressure check mis-match"));
return;
}
realpressure = pressure1 * 0.25 - 7.0;
// realpressure = pressure1 * 0.25 - 7.3;
realtemp = temp - 40.0;
#ifdef SHOWVALIDTPMS
Serial.print(F("ID: "));
Serial.print(id, HEX);
Serial.print(F(" Status: "));
Serial.print(status);
Serial.print(F(" Temperature: "));
Serial.print(realtemp);
Serial.print(F(" Tyre Pressure: "));
Serial.print(realpressure);
Serial.print(F(" (psi) "));
Serial.print(realpressure/PSI2BAR);
Serial.print(F(" (bar)"));
Serial.println(F(""));
#endif
DisplayStatusInfo();
//update the array of tyres data
for (int i = 0; i < 4; i++)
{ //find a matching ID if it already exists
if (id == TPMS[i].TPMS_ID)
{
UpdateTPMSData(i, id, status, realtemp, realpressure, TPMS[i].Typename);
IDFound = true;
break;
}
}
if (IDFound == false) {
prefindex = GetPreferredIndex(id);
if (prefindex >= 0) { //not found a specified index, so use the next available one..
for (byte i = 0; i < 4; i++) {
if (TPMS[i].TPMS_ID == 0) {
UpdateTPMSData(i, id, status, realtemp, realpressure, IDLookup[prefindex][1]);
break;
}
}
}
}
#ifdef SHOWVALIDTPMS
Serial.println(F(""));
#endif
//UpdateDisplay();
}
#ifndef USE_PROGMEMCRC
IRAM_ATTR void CalulateTable_CRC8()
{
const byte generator = 0x07;
/* iterate over all byte values 0 - 255 */
for (int divident = 0; divident < 256; divident++)
{
byte currByte = (byte)divident;
/* calculate the CRC-8 value for current byte */
for (byte bit = 0; bit < 8; bit++)
{
if ((currByte & 0x80) != 0)
{
currByte <<= 1;
currByte ^= generator;
}
else
{
currByte <<= 1;
}
}
/* store CRC value in lookup table */
crctable[divident] = currByte;
Serial.print("0x");
if (currByte < 16)
Serial.print("0");
Serial.print(currByte,HEX);
Serial.print(", ");
}
}
#endif
IRAM_ATTR byte Compute_CRC8( int bcount)
{
byte crc = 0x80;
for (int c = 0; c < bcount; c++)
{
byte b = RXBytes[c];
/* XOR-in next input byte */
byte data = (byte)(b ^ crc);
/* get current CRC value = remainder */
#ifdef USE_PROGMEMCRC
crc = (byte)(pgm_read_byte(&crctable2[data]));
#else
crc = (byte)(crctable[data]);
#endif
}
return crc;
}
// V5.1.1 - Added functions for extracting RSSI as dbm
/*
IRAM_ATTR int GetRSSI_dbm()
{
byte RSSI_Read;
byte RSSI_Offset = 74;
int ret;
RSSI_Read = readStatusReg(CC1101_RSSI);
if (RSSI_Read >= 128)
{
ret = (int)((int)(RSSI_Read - 256) / 2) - RSSI_Offset;
}
else
{
ret = (RSSI_Read / 2) - RSSI_Offset;
}
return(ret);
}
*/
IRAM_ATTR int GetRSSI_dbm()
{
int rssi;
rssi = readStatusReg(CC1101_RSSI);
if (rssi >= 128){rssi = (rssi-256)/2-74;}
else{rssi = (rssi/2)-74;}
return (rssi);
}
IRAM_ATTR void ClearRXBuffer()
{
memset(RXBytes,0,sizeof(RXBytes));
}
IRAM_ATTR void EdgeInterrupt()
{
unsigned long ts = micros();
unsigned long BitWidth;
if (TimingsIndex == 255)
{
return;
}
if (WaitingFirstEdge)
{
if (digitalRead(RXPin) == LOW)
{
FirstEdgeIsHighToLow = true;
}
else
{
FirstEdgeIsHighToLow = false;
}
}
WaitingFirstEdge = false;
BitWidth = ts - LastEdgeTime_us;
if (BitWidth <= 12) //ignore glitches
{
return;
}
if (BitWidth > 8000)
BitWidth = 8000;
LastEdgeTime_us = ts;
// if ((BitWidth >= 38) && (BitWidth <= 250))
// {//ignore out of spec pulses
Timings[TimingsIndex++] = (unsigned int)BitWidth;
// }
// digitalWrite(DEBUGPIN,HIGH);
// delayMicroseconds(3);
// digitalWrite(DEBUGPIN,LOW);
}
/*
IRAM_ATTR bool IsValidSync(unsigned int Width)
{
return (Width >= 175) && (Width <= 750);
}
IRAM_ATTR bool IsValidShort(unsigned int Width)
{
return (Width >= 40) && (Width <= 70);
}
IRAM_ATTR bool IsValidLong(unsigned int Width)
{
return (Width >= 80) && (Width <= 120);
}
*/
#define IsValidSync(Width) ((Width >= 175) && (Width <= 750))
#define IsValidShort(Width) ((Width >= 40) && (Width <= 70))
#define IsValidLong(Width) ((Width >= 80) && (Width <= 120))
IRAM_ATTR int ValidateBit()
{
unsigned int BitWidth = Timings[CheckIndex];
unsigned int BitWidthNext = Timings[CheckIndex + 1];
if (IsValidLong(BitWidth))
return (1);
if (IsValidShort(BitWidth))
return (0);
if (IsValidSync(BitWidth))
return (2);
return (-1);
}
IRAM_ATTR void ValidateTimings()
{
unsigned int BitWidth;
unsigned int BitWidthNext;
unsigned int BitWidthNextPlus1;
unsigned int BitWidthPrevious;
unsigned int diff = TimingsIndex - CheckIndex;
//unsigned long tmp;
bool WaitingTrailingZeroEdge = false;
int ret;
StartDataIndex = 0;
if (diff < EXPECTEDBITCOUNT)
{ //not enough in the buffer to consider a valid message
#ifdef SHOWDEBUGINFO
Serial.print(F("Insufficient data in buffer ("));
Serial.print(diff);
Serial.println(")";
#endif
return;
}
SyncFound = false;
while ((diff > 0) && (BitCount < EXPECTEDBITCOUNT))
{ //something in buffer to process...
diff = TimingsIndex - CheckIndex;
BitWidth = Timings[CheckIndex];
if (SyncFound == false)
{
if (IsValidSync(BitWidth))
{
SyncFound = true;
BitIndex = 0;
BitCount = 0;
WaitingTrailingZeroEdge = false;
StartDataIndex = CheckIndex + 1;
}
}
else
{
ret = ValidateBit();
switch (ret)
{
case -1:
//invalid bit
// BitIndex = 0;
// BitCount = 0;
// WaitingTrailingZeroEdge = false;
// StartDataIndex = CheckIndex + 1;
SyncFound = false;
break;
case 0:
if (WaitingTrailingZeroEdge)
{
//BitTimings[BitIndex] = BitWidth;
IncomingBits[BitIndex++] = 0;
BitCount++;
WaitingTrailingZeroEdge = false;
}
else
{
WaitingTrailingZeroEdge = true;
}
break;
case 1:
if (WaitingTrailingZeroEdge)
{ //shouldn't get a long pulse when waiting for the second short pulse (i.e. expecting bit = 0)
//try to resync from here?
BitIndex = 0;
BitCount = 0;
WaitingTrailingZeroEdge = false;
CheckIndex--; //recheck this entry
StartDataIndex = CheckIndex + 1;
SyncFound = false;
}
else
{
//BitTimings[BitIndex] = BitWidth;
IncomingBits[BitIndex++] = 1;
BitCount++;
}
break;
case 2:
SyncFound = true;
BitIndex = 0;
BitCount = 0;
WaitingTrailingZeroEdge = false;
StartDataIndex = CheckIndex + 1;
break;
}
}
CheckIndex++;
}
}
IRAM_ATTR void InitDataBuffer()
{
BitIndex = 0;
BitCount = 0;
ValidBlock = false;
//WaitingTrailingZeroEdge = false;
WaitingFirstEdge = true;
CheckIndex = 0;
TimingsIndex = 0;
SyncFound = false;
//digitalWrite(DEBUGPIN, LOW);
}
IRAM_ATTR void UpdateStatusInfo()
{
FreqOffset = readStatusReg(CC1101_FREQEST);
DemodLinkQuality = readStatusReg(CC1101_LQI);
RSSIvalue = readStatusReg(CC1101_RSSI);
}
IRAM_ATTR int ReceiveMessage()
{
//Check bytes in FIFO
int FIFOcount;
int resp;
int lRSSI = 0;
//set up timing of edges using interrupts...
LastEdgeTime_us = micros();
CD_Width = LastEdgeTime_us;
attachInterrupt(digitalPinToInterrupt(RXPin), EdgeInterrupt, CHANGE);
while (GetCarrierStatus());
/*
RSSIvalue = -1000;
while (GetCarrierStatus() == true)
{
//get the maximum RSSI value seen during data receive window
lRSSI = GetRSSI_dbm();
if (lRSSI > RSSIvalue)
{
RSSIvalue = lRSSI;
}
}
*/
delayMicroseconds(500); //there is a delay on the serial data stream so ensure we allow a bit of extra time after CD finishes to ensure all the data is captured
detachInterrupt(digitalPinToInterrupt(RXPin));
CD_Width = micros() - CD_Width;
if ((CD_Width >= CDWIDTH_MIN) && (CD_Width <= CDWIDTH_MAX))
{
PulseDebugPin(100);
#ifdef SHOWDEGUGINFO
Serial.println(F("Checking.."));
#endif
digitalWrite(LED_RX,LED_ON);
CheckIndex = 0;
ValidateTimings();
#ifdef SHOWDEGUGINFO
Serial.print(F("CD_Width="));
Serial.println(CD_Width);
Serial.print(F("TimingsIndex="));
Serial.println(TimingsIndex);
Serial.print(F("Checking complete. Bitcount: "));
Serial.print(BitCount);
Serial.print(F(" StartDataIndex: "));
Serial.println(StartDataIndex);
Serial.print(F(" RSSI(dBm):"));
Serial.println(RSSIvalue);
if (BitCount <= EXPECTEDBITCOUNT)
{
PrintTimings(0,TimingsIndex+1);
PrintData(BitCount);
}
#endif
digitalWrite(LED_RX,LED_OFF);
return (BitCount);
}
else
{
#ifdef SHOWDEGUGINFO
if (CD_Width > CDWIDTH_MIN)
{
Serial.print(F("CD_Width*="));
Serial.println(CD_Width);
}
#endif
return (0);
}
}