Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | |
2 | |||
3 | #include "libTinyWB/tinyWB.h" |
||
4 | |||
5 | #include "libSerial/serialCalls.H" |
||
6 | |||
7 | #define CHARS_PER_FRAME 9 |
||
8 | #define START_CODE 1 |
||
9 | |||
10 | /// @brief time between character bursts |
||
11 | static const uint32_t GAP_TIME_MS = 40; |
||
12 | |||
13 | |||
14 | /// @brief Time of last reception of a character |
||
15 | static uint32_t lastCharTime = 0; |
||
16 | |||
17 | static uint8_t characterIndex = 0; |
||
18 | |||
19 | /// @note Documentation from DIY-EFI |
||
20 | /// Data Rate: 115200 baud |
||
21 | /// Length: 8 bytes |
||
22 | /// Data Format : |
||
23 | /// byte0 = AFR*10 (so 148 = 14.8 AFR) |
||
24 | /// byte1 = Temp in C of the sensor |
||
25 | /// byte2-7 = not used |
||
26 | /// The TinyWB is based on the SLCFree by 14point7 |
||
3 | mjames | 27 | /// actually this document is wrong - below is the decode from using protocol analyser. |
28 | /// when the lambda sensor is not connected, the unit outputs something like 1,200 gap of 50mS and repeats# |
||
2 | mjames | 29 | /// |
3 | mjames | 30 | /// when the lambda sensor is connected, the unit outputs this pattern |
2 | mjames | 31 | |
32 | //[0] 0.0000000000,1,, sync ? |
||
33 | //[1] 0.0000867750,200,, afr |
||
34 | //[2] 0.0001735250,41,, temperature |
||
35 | //[3] 0.0002603000,2,, filler |
||
36 | //[4] 0.0003471000,3,, filler |
||
37 | //[5] 0.0004338750,4,, filler |
||
38 | //[6] 0.0005206750,5,, filler |
||
39 | //[7] 0.0006074500,6,, filler |
||
40 | //[8] 0.0006942250,200,, afr again ? at byte 8. |
||
41 | |||
42 | |||
43 | |||
44 | // store first two characters, ignore the rest |
||
45 | static char afrCode = 0; |
||
46 | static char tempCode = 0; |
||
47 | |||
48 | char pollTinyWB(struct usart_ctl *instance, float *AFRvalue, float *temperature) |
||
49 | { |
||
50 | int valid = 0; |
||
3 | mjames | 51 | // PollSerial returns non zero if characters are in the serial input buffer |
52 | while (PollSerial(instance)) |
||
2 | mjames | 53 | { |
54 | // deal with time-based synchronisation - 8 byte bursts with gaps |
||
55 | uint32_t now = HAL_GetTick(); |
||
56 | if (now - lastCharTime > GAP_TIME_MS) |
||
57 | { |
||
58 | characterIndex = 0; |
||
59 | } |
||
60 | lastCharTime = now; |
||
3 | mjames | 61 | // GetCharSerial is implemented as a blocking read from a USART : but because it is |
62 | // only called when PollSerial is true, it is never going to block. |
||
2 | mjames | 63 | char c = GetCharSerial(instance); |
64 | if (characterIndex < CHARS_PER_FRAME) |
||
65 | |||
66 | switch (characterIndex) |
||
67 | { |
||
68 | case 0: |
||
69 | if (c != 1) |
||
70 | goto fail; |
||
71 | characterIndex++; |
||
72 | break; |
||
73 | // for characters 1 and 2 store in buffer |
||
74 | case 1: |
||
75 | afrCode = c; |
||
76 | characterIndex++; |
||
77 | break; |
||
78 | case 2: |
||
79 | tempCode = c; |
||
80 | characterIndex++; |
||
81 | break; |
||
82 | |||
83 | case 3: |
||
84 | case 4: |
||
85 | case 5: |
||
86 | case 6: |
||
87 | case 7: |
||
88 | if (c != characterIndex - 1) |
||
89 | goto fail; |
||
90 | characterIndex++; |
||
91 | break; |
||
92 | |||
93 | case CHARS_PER_FRAME - 1: |
||
94 | *AFRvalue = afrCode / 10.0; |
||
95 | *temperature = tempCode; |
||
96 | valid = 1; // have valid readings |
||
97 | |||
98 | fail: |
||
99 | characterIndex = 0; |
||
100 | break; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return valid; |
||
105 | } |