forked from igkov/bcomp11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobd.c
203 lines (190 loc) · 6.52 KB
/
obd.c
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
/*
Óïðîùåííûé ìåíåäæåð. Çàïðîñû ÷åðåç ôèêñèðîâàííûå èíòåðâàëû âðåìåíè.
*/
#include <stdint.h>
#include <string.h>
#include "event.h"
#include "obd.h"
#include "obd_pids.h"
#include "dbg.h"
#include "bcomp.h"
// Ïåðåìåííûå îáðàáîò÷èêà
static uint16_t can_pid;
static uint16_t can_id;
static uint8_t can_len;
static uint8_t can_offset;
static uint8_t can_cnt;
static uint8_t can_buffer[32];
static uint8_t can_done;
// Ïåðåìåííàÿ äëÿ "êàðóñåëè":
static int count;
/* Ñïèñîê PID-îâ äëÿ ïîëó÷åíèÿ. */
pid_obd_t pids_list[] = {
// CAN ADDR | PID | ACT | RES1 RES2 RES3
//{ PID_REQUEST_ENGINE, ENGINE_RPM, 0, {0xFF, 0xFF, 0xFF} }, // 1. Îáîðîòû (ïîëó÷àåì ñûðûìè äàííûìè).
//{ PID_REQUEST_ENGINE, VEHICLE_SPEED, 0, {0xFF, 0xFF, 0xFF} }, // 2. Ñïèäîìåòð (ïîëó÷àåì ñûðûìè äàííûìè).
//{ PID_REQUEST_ENGINE, ENGINE_COOLANT_TEMP, 0, {0xFF, 0xFF, 0xFF} }, // 3. Òåìï. äâèãàòåëÿ (ïîëó÷àåì ñûðûìè äàííûìè).
{ PID_REQUEST_ENGINE, ECU_VOLTAGE, 1, {0xFF, 0xFF, 0xFF} }, // 4. Íàïðÿæåíèå áîðòîâîé ñåòè.
{ PID_REQUEST_AT, PAJERO_AT_INFO, 1, {0xFF, 0xFF, 0xFF} }, // 5. Òåìï. êîðîáêè.
{ PID_REQUEST_ENGINE, GET_VIN, 1, {0xFF, 0xFF, 0xFF} }, // 6. Ïîëó÷åíèå VIN-àâòîìîáèëÿ.
{ PID_REQUEST_ENGINE, INTAKE_PRESSURE, 1, {0xFF, 0xFF, 0xFF} }, // 7. Äàâëåíèå íàääóâà.
//{ PID_REQUEST_ENGINE, ENGINE_RUNTIME, 0, {0xFF, 0xFF, 0xFF} }, // 8. Âðåìÿ ðàáîòû äâèãàòåëÿ.
{ PID_REQUEST_ENGINE, FUEL_RAIL_PRES_ALT, 1, {0xFF, 0xFF, 0xFF} }, // 9. Äàâëåíèå òîïëèâà â ðåéêå.
{ PID_REQUEST_ENGINE, ECU_VOLTAGE, 1, {0xFF, 0xFF, 0xFF} }, // 10. Íàïðÿæåíèå áîðòîâîé ñåòè.
//{ PID_REQUEST_ENGINE, MAF_SENSOR, 0, {0xFF, 0xFF, 0xFF} }, // 11. Äàííûå ñ MAF-ñåíñîðà.
//{ PID_REQUEST_ENGINE, BAROMETRIC_PRESSURE, 0, {0xFF, 0xFF, 0xFF} }, // 12. Íàðóæíîå äàâëåíèå.
{ PID_REQUEST_ENGINE, STATUS_DTC, 1, {0xFF, 0xFF, 0xFF} }, // 13. Èíôîðìàöèÿ îá îùèáêàõ â ÝÁÓ äâèãàòåëÿ.
{ PID_REQUEST_ENGINE, FREEZE_DTC, 0, {0xFF, 0xFF, 0xFF} }, // 14. Ïîëó÷åíèå êîäà îøèáêè èç ïàìÿòè (îòêëþ÷åíî ïî-óìîë÷àíèþ).
{ PID_REQUEST_ENGINE, PAJERO_ODO_INFO, 0, {0xFF, 0xFF, 0xFF} }, // 15. Ïîëó÷åíèå äàííûõ îäîìåòðà.
};
// Îáðàáîò÷èê ïàêåòîâ îò øèíû CAN (ïî ïðîòîêîëó J1979):
void obd_loopback(CAN_msg *p) {
#if defined( NO_FILTER )
if (can_id != p->id) {
// Ñûðûå ïàêåòû äàííûõ:
bcomp_raw(p->id, p->data, p->len);
return;
}
#endif
if (can_len > can_offset) {
if (can_cnt == p->data[0]) {
memcpy(&can_buffer[can_offset], &p->data[1], (can_len-can_offset)>7?7:(can_len-can_offset));
can_offset += (can_len-can_offset)>7?7:(can_len-can_offset);
can_cnt++;
if (can_offset == can_len)
goto obd_loopback_recv;
}
else {
DBG("Bad data sequence (%02x)!\r\n", p->data[0]);
}
}
else if (p->data[0] == 0x10) {
if (p->data[2] != (0x40+(can_pid>>8))) {
DBG("ERROR: incorrect Mode in answer (wait Mode=%02x, received Mode=%02x)!\r\n", can_pid, p->data[2]);
return;
}
if (p->data[3] != (can_pid&0xFF)) {
DBG("ERROR: incorrect PID in answer (wait PID=%02x, received PID=%02x)!\r\n", can_pid, p->data[3]);
return;
}
memset(can_buffer, 0, sizeof(can_buffer));
memcpy(can_buffer, &p->data[2], 6);
can_len = p->data[1];
can_offset = 6;
can_cnt = 0x21;
// get more:
memset(CAN_TxMsg.data, 0x00, 8);
CAN_TxMsg.data[0] = 0x30;
CAN_TxMsg.len = 8;
CAN_TxMsg.format = STANDARD_FORMAT;
CAN_TxMsg.type = DATA_FRAME;
//CAN_writeMsg(1, &CAN_TxMsg);
CAN_wrMsg(&CAN_TxMsg);
}
else if (p->data[0] >= 2 &&
p->data[0] <= 7 ) {
if (p->data[1] != (0x40+(can_pid>>8))) {
DBG("ERROR: incorrect Mode in answer (wait %02x, receive %02x)!\r\n", can_pid, p->data[1]);
return;
}
if (p->data[2] != (can_pid&0xFF)) {
DBG("ERROR: incorrect PID in answer (wait %02x, receive %02x)!\r\n", (can_pid&0xFF), p->data[2]);
return;
}
// Êîïèðóåì:
can_len = p->data[0];
can_offset = p->data[0];
memset(can_buffer, 0, sizeof(can_buffer));
memcpy(can_buffer, &p->data[1], can_len);
obd_loopback_recv:
// Çàâåðøèëèñü, ôëàã ãîòîâíîñòè äàííûõ:
can_done = 1;
}
else {
DBG("ERROR: incorrect lenght in answer!\r\n");
return;
}
}
void obd_getpid(uint16_t addr, uint16_t pid) {
//DBG("get_pid(%04x, %04x);\r\n", addr, pid);
#if !defined( NO_FILTER )
// Íàñòðîéêà ïðèåìà:
CAN_wrFilter(addr+0x08, STANDARD_FORMAT);
#endif
// Èíèöèàëèçàöèÿ îáðàáîò÷èêà:
can_len = 0;
can_id = addr+0x08;
can_pid = pid;
can_offset = 0;
can_cnt = 0;
can_done = 0;
// Ñîáèðàåì çàïðîñ:
CAN_TxMsg.id = addr;
memset(CAN_TxMsg.data, 0x00, 8);
CAN_TxMsg.data[0] = 0x02;
CAN_TxMsg.data[1] = pid>>8;
CAN_TxMsg.data[2] = pid&0xFF;
CAN_TxMsg.len = 8;
CAN_TxMsg.format = STANDARD_FORMAT;
CAN_TxMsg.type = DATA_FRAME;
// Îòïðàâêà çàïðîñà:
//CAN_writeMsg(1, &CAN_TxMsg);
CAN_wrMsg(&CAN_TxMsg);
}
static void obd_manage(void) {
if (can_done == 1) {
// Çàïðîñ îáðàáîòàí!
bcomp_proc(can_pid, can_buffer, can_len);
//Ñâîáîäíû, îáðàáîòêà ñëåä. çàïðîñà:
can_pid = can_id = can_done = 0;
//Çàäåðæêà ïåðåä ñëåäóþùåé îáðàáîòêîé:
event_set(obd_manage, 200);
} else
if (can_pid == 0) {
//  ñëó÷àå óñòàíîâëåííîãî ñåðâèñíîãî ôëàãà, çàïðîñû â ÝÁÓ íå îòïðàâëÿþòñÿ.
// Ïàññèâíîå ïðîñëóøèâàíèå øèíû ïðè ýòîì îñòàåòñÿ àêòèâíûì.
if (bcomp.service == 0) {
// Çàïðîñ ê ÝÁÓ.
obd_getpid(pids_list[count%PIDS_SIZE].addr, pids_list[count%PIDS_SIZE].pid);
}
// Ïîçèöèÿ äëÿ ñëåäóþùåãî çàïðîñà:
do {
count++;
} while (pids_list[count%PIDS_SIZE].act == 0);
// Çàïðîñ îòïðàâëåí, îæèäàåì äî 100ìñ:
event_set(obd_manage, 100);
return;
} else {
// Òàéìàóò!
can_pid = 0;
event_set(obd_manage, 10);
}
}
void obd_init(void) {
// Îáíóëÿåì ïåðåìåííûå:
can_pid = 0;
can_len = 0;
can_offset = 0;
can_cnt = 0;
can_done = 0;
count = 0;
memset(can_buffer, 0, sizeof(can_buffer));
// Èíèöèàëèçèðóåì CAN-èíòåðôåéñ:
CAN_setup(bconfig.can_speed); /* setup CAN Controller, 500kbit/s */
CAN_start(); /* start CAN Controller */
CAN_waitReady(); /* wait til tx mbx is empty */
#if defined( NO_FILTER )
CAN_noFilter(STANDARD_FORMAT);
#endif
// 5000ms îáðàáîòêà, çàäåðæêà 5 ñåêóíä ïåðåä íà÷àëîì îïðîñà ECU:
event_set(obd_manage, 5000);
}
int obd_act_set(uint16_t pid, int flag) {
int i;
for (i=0; i<PIDS_SIZE; i++) {
if (pids_list[i].pid == pid) {
pids_list[i].act = flag;
}
}
return 1;
}