rusEFI
The most advanced open source ECU
sent_logic.h
Go to the documentation of this file.
1 /**
2  * sent_logic.h
3  */
4 
5 #pragma once
6 
7 #ifndef SENT_CHANNELS_NUM
8 #define SENT_CHANNELS_NUM 4 // Number of sent channels
9 #endif
10 
11 /* Maximum slow shannel mailboxes, DO NOT CHANGE */
12 #define SENT_SLOW_CHANNELS_MAX 16
13 
14 /* collect statistic */
15 #define SENT_STATISTIC_COUNTERS 1
16 
17 typedef enum
18 {
31 
33  uint32_t hwOverflowCnt;
34 
35  uint32_t ShortIntervalErr;
36  uint32_t LongIntervalErr;
37  uint32_t SyncErr;
38  uint32_t CrcErrCnt;
39  uint32_t FrameCnt;
40  uint32_t PauseCnt;
41  uint32_t RestartCnt;
42 
43  /* Slow channel */
44  uint32_t sc;
45  uint32_t scCrcErr;
46 uint32_t getTotalError() {
48 }
49 
50 float getErrorRate() {
51  return 1.0 * getTotalError() / (FrameCnt + getTotalError());
52 }
53 
54 };
55 
56 class sent_channel {
57 private:
59 
60  /* Unit interval in timer clocks - adjusted on SYNC */
61  uint32_t tickPerUnit = 0;
62  uint32_t pulseCounter = 0;
63  /* pulses skipped in init or calibration state while waiting for SYNC */
65  bool pausePulseReceived = false;
66 
67  /* fast channel shift register*/
68  uint32_t rxReg;
69  bool hasValidFast = false;
70  /* fast channel last received valid message */
71  uint32_t rxLast;
72 
73  /* slow channel shift registers and flags */
74  uint16_t scMsgFlags;
75  uint32_t scShift2; /* shift register for bit 2 from status nibble */
76  uint32_t scShift3; /* shift register for bit 3 from status nibble */
77  uint32_t scCrcShift; /* shift register for special order for CRC6 calculation */
78  /* Slow channel decoder and helpers */
79  int StoreSlowChannelValue(uint8_t id, uint16_t data);
80  int FastChannelDecoder(uint32_t clocks);
81  int SlowChannelDecoder();
83 
84  /* CRC */
85  uint8_t crc4(uint32_t data);
86  uint8_t crc4_gm(uint32_t data);
87  uint8_t crc4_gm_v2(uint32_t data);
88  /* Slow channel CRC6 */
89  uint8_t crc6(uint32_t data);
90 
91  /* calc unit tick time from sync pulse */
92  void calcTickPerUnit(uint32_t clocks);
93 
94  /* check if current pulse looks like sync pulse */
95  bool isSyncPulse(uint32_t clocks);
96 
97  void restart();
98 
99 public:
100  /* slow channel data */
101  struct {
102  uint16_t data;
103  uint8_t id;
104  } scMsg[SENT_SLOW_CHANNELS_MAX];
105 
106  /* Statistic counters */
107 #if SENT_STATISTIC_COUNTERS
109 #endif // SENT_STATISTIC_COUNTERS
110 
111  /* Decoder */
112  int Decoder(uint32_t clocks, uint8_t flags = 0);
113 
114  /* Get last raw message */
115  int GetMsg(uint32_t* rx);
116  /* Unpack last valid message to status, signal0 and signal1
117  * Note:
118  * sig0 is nibbles 0 .. 2, where nibble 0 is MSB
119  * sig1 is nibbles 5 .. 3, where nibble 5 is MSB */
120  int GetSignals(uint8_t *pStat, uint16_t *pSig0, uint16_t *pSig1);
121 
122  /* Get slow channel value for given ID 8*/
123  int GetSlowChannelValue(uint8_t id);
124 
125  /* Current tick time in CPU/timer clocks */
126  float getTickTime();
127 
128  /* Show status */
129  void Info();
130 };
bool pausePulseReceived
Definition: sent_logic.h:65
bool isSyncPulse(uint32_t clocks)
Definition: sent.cpp:98
uint32_t pulseCounter
Definition: sent_logic.h:62
uint8_t crc4_gm_v2(uint32_t data)
Definition: sent.cpp:513
uint8_t crc4(uint32_t data)
Definition: sent.cpp:479
uint8_t crc6(uint32_t data)
Definition: sent.cpp:529
uint32_t tickPerUnit
Definition: sent_logic.h:61
int Decoder(uint32_t clocks, uint8_t flags=0)
Definition: sent.cpp:294
uint32_t scShift2
Definition: sent_logic.h:75
void restart()
Definition: sent.cpp:64
uint8_t crc4_gm(uint32_t data)
Definition: sent.cpp:497
uint16_t data
Definition: sent_logic.h:102
void SlowChannelDecoderReset()
Definition: sent.cpp:470
int StoreSlowChannelValue(uint8_t id, uint16_t data)
Definition: sent.cpp:362
void calcTickPerUnit(uint32_t clocks)
Definition: sent.cpp:88
uint32_t rxReg
Definition: sent_logic.h:68
uint32_t scCrcShift
Definition: sent_logic.h:77
SENT_STATE_enum state
Definition: sent_logic.h:58
int FastChannelDecoder(uint32_t clocks)
Definition: sent.cpp:110
uint8_t id
Definition: sent_logic.h:103
struct sent_channel::@47 scMsg[SENT_SLOW_CHANNELS_MAX]
uint32_t rxLast
Definition: sent_logic.h:71
bool hasValidFast
Definition: sent_logic.h:69
float getTickTime()
Definition: sent.cpp:94
int GetSignals(uint8_t *pStat, uint16_t *pSig0, uint16_t *pSig1)
Definition: sent.cpp:330
int SlowChannelDecoder()
Definition: sent.cpp:403
int GetSlowChannelValue(uint8_t id)
Definition: sent.cpp:389
uint16_t scMsgFlags
Definition: sent_logic.h:74
uint32_t scShift3
Definition: sent_logic.h:76
int GetMsg(uint32_t *rx)
Definition: sent.cpp:318
uint32_t currentStatePulseCounter
Definition: sent_logic.h:64
sent_channel_stat statistic
Definition: sent_logic.h:108
void Info()
Definition: sent.cpp:559
SENT_STATE_enum
Definition: sent_logic.h:18
@ SENT_STATE_CALIB
Definition: sent_logic.h:19
@ SENT_STATE_STATUS
Definition: sent_logic.h:22
@ SENT_STATE_SIG2_DATA1
Definition: sent_logic.h:26
@ SENT_STATE_SIG1_DATA2
Definition: sent_logic.h:24
@ SENT_STATE_SIG1_DATA3
Definition: sent_logic.h:25
@ SENT_STATE_SIG1_DATA1
Definition: sent_logic.h:23
@ SENT_STATE_SIG2_DATA2
Definition: sent_logic.h:27
@ SENT_STATE_INIT
Definition: sent_logic.h:20
@ SENT_STATE_SYNC
Definition: sent_logic.h:21
@ SENT_STATE_CRC
Definition: sent_logic.h:29
@ SENT_STATE_SIG2_DATA3
Definition: sent_logic.h:28
uint32_t FrameCnt
Definition: sent_logic.h:39
uint32_t PauseCnt
Definition: sent_logic.h:40
uint32_t getTotalError()
Definition: sent_logic.h:46
uint32_t SyncErr
Definition: sent_logic.h:37
uint32_t ShortIntervalErr
Definition: sent_logic.h:35
uint32_t CrcErrCnt
Definition: sent_logic.h:38
uint32_t scCrcErr
Definition: sent_logic.h:45
uint32_t hwOverflowCnt
Definition: sent_logic.h:33
float getErrorRate()
Definition: sent_logic.h:50
uint32_t RestartCnt
Definition: sent_logic.h:41
uint32_t LongIntervalErr
Definition: sent_logic.h:36