Subversion Repositories AFRtranscoder

Rev

Go to most recent revision | Details | 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
27
/// actually this is rubbish - decode from using protocol analyser. 
28
/// when the sensor is not connected, the unit outputs something like 1,200 gap of 50mS and repeats#
29
/// 
30
// when the sensor is connected, the unit outputs this pattern
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;
51
    while (PollSerial(instance))
52
    {
53
        // deal with time-based synchronisation - 8 byte bursts with gaps
54
        uint32_t now = HAL_GetTick();
55
        if (now - lastCharTime > GAP_TIME_MS)
56
        {
57
            characterIndex = 0;
58
        }
59
        lastCharTime = now;
60
        char c = GetCharSerial(instance);
61
        if (characterIndex < CHARS_PER_FRAME)
62
 
63
            switch (characterIndex)
64
            {
65
            case 0:
66
                if (c != 1)
67
                    goto fail;
68
                characterIndex++;
69
                break;
70
            // for characters 1 and 2 store in buffer
71
            case 1:
72
                afrCode = c;
73
                characterIndex++;
74
                break;
75
            case 2:
76
                tempCode = c;
77
                characterIndex++;
78
                break;
79
 
80
            case 3:
81
            case 4:
82
            case 5:
83
            case 6:
84
            case 7:
85
                if (c != characterIndex - 1)
86
                    goto fail;
87
                characterIndex++;
88
                break;
89
 
90
            case CHARS_PER_FRAME - 1:
91
                *AFRvalue = afrCode / 10.0;
92
                *temperature = tempCode;
93
                valid = 1; // have valid readings
94
 
95
            fail:
96
                characterIndex = 0;
97
                break;
98
            }
99
    }
100
 
101
    return valid;
102
}