3
3
#include "safety_declarations.h"
4
4
5
5
// Stock longitudinal
6
- #define TOYOTA_COMMON_TX_MSGS \
7
- {0x2E4, 0, 5}, {0x191, 0, 8}, {0x412, 0, 8}, {0x343, 0, 8}, {0x1D2, 0, 8}, /* LKAS + LTA + ACC & PCM cancel cmds */ \
6
+ #define TOYOTA_BASE_TX_MSGS \
7
+ {0x191, 0, 8}, {0x412, 0, 8}, {0x343, 0, 8}, {0x1D2, 0, 8}, /* LKAS + LTA + ACC & PCM cancel cmds */ \
8
+
9
+ #define TOYOTA_COMMON_TX_MSGS \
10
+ TOYOTA_BASE_TX_MSGS \
11
+ {0x2E4, 0, 5}, \
12
+
13
+ #define TOYOTA_COMMON_SECOC_TX_MSGS \
14
+ TOYOTA_BASE_TX_MSGS \
15
+ {0x2E4, 0, 8}, {0x131, 0, 8}, \
8
16
9
17
#define TOYOTA_COMMON_LONG_TX_MSGS \
10
18
TOYOTA_COMMON_TX_MSGS \
16
24
#define TOYOTA_COMMON_RX_CHECKS (lta ) \
17
25
{.msg = {{ 0xaa , 0 , 8 , .check_checksum = false, .frequency = 83U }, { 0 }, { 0 }}}, \
18
26
{.msg = {{0x260 , 0 , 8 , .check_checksum = true, .quality_flag = (lta ), .frequency = 50U }, { 0 }, { 0 }}}, \
19
- {.msg = {{0x1D2 , 0 , 8 , .check_checksum = true, .frequency = 33U }, { 0 }, { 0 }}}, \
20
- {.msg = {{0x224 , 0 , 8 , .check_checksum = false, .frequency = 40U }, \
21
- {0x226 , 0 , 8 , .check_checksum = false, .frequency = 40U }, { 0 }}}, \
27
+ {.msg = {{0x1D2 , 0 , 8 , .check_checksum = true, .frequency = 33U }, \
28
+ {0x176 , 0 , 8 , .check_checksum = true, .frequency = 32U }, { 0 }}}, \
29
+ {.msg = {{0x101 , 0 , 8 , .check_checksum = false, .frequency = 50U }, \
30
+ {0x224 , 0 , 8 , .check_checksum = false, .frequency = 40U }, \
31
+ {0x226 , 0 , 8 , .check_checksum = false, .frequency = 40U }}}, \
22
32
33
+ static bool toyota_secoc = false;
23
34
static bool toyota_alt_brake = false;
24
35
static bool toyota_stock_longitudinal = false;
25
36
static bool toyota_lta = false;
@@ -87,14 +98,31 @@ static void toyota_rx_hook(const CANPacket_t *to_push) {
87
98
}
88
99
89
100
// enter controls on rising edge of ACC, exit controls on ACC off
90
- // exit controls on rising edge of gas press
91
- if (addr == 0x1D2 ) {
92
- // 5th bit is CRUISE_ACTIVE
93
- bool cruise_engaged = GET_BIT (to_push , 5U );
94
- pcm_cruise_check (cruise_engaged );
95
-
96
- // sample gas pedal
97
- gas_pressed = !GET_BIT (to_push , 4U );
101
+ // exit controls on rising edge of gas press, if not alternative experience
102
+ // exit controls on rising edge of brake press
103
+ if (toyota_secoc ) {
104
+ if (addr == 0x176 ) {
105
+ bool cruise_engaged = GET_BIT (to_push , 5U ); // PCM_CRUISE.CRUISE_ACTIVE
106
+ pcm_cruise_check (cruise_engaged );
107
+ }
108
+ if (addr == 0x116 ) {
109
+ gas_pressed = GET_BYTE (to_push , 1 ) != 0U ; // GAS_PEDAL.GAS_PEDAL_USER
110
+ }
111
+ if (addr == 0x101 ) {
112
+ brake_pressed = GET_BIT (to_push , 3U ); // BRAKE_MODULE.BRAKE_PRESSED (toyota_rav4_prime_generated.dbc)
113
+ }
114
+ } else {
115
+ if (addr == 0x1D2 ) {
116
+ bool cruise_engaged = GET_BIT (to_push , 5U ); // PCM_CRUISE.CRUISE_ACTIVE
117
+ pcm_cruise_check (cruise_engaged );
118
+ gas_pressed = !GET_BIT (to_push , 4U ); // PCM_CRUISE.GAS_RELEASED
119
+ }
120
+ if (!toyota_alt_brake && (addr == 0x226 )) {
121
+ brake_pressed = GET_BIT (to_push , 37U ); // BRAKE_MODULE.BRAKE_PRESSED (toyota_nodsu_pt_generated.dbc)
122
+ }
123
+ if (toyota_alt_brake && (addr == 0x224 )) {
124
+ brake_pressed = GET_BIT (to_push , 5U ); // BRAKE_MODULE.BRAKE_PRESSED (toyota_new_mc_pt_generated.dbc)
125
+ }
98
126
}
99
127
100
128
// sample speed
@@ -111,12 +139,6 @@ static void toyota_rx_hook(const CANPacket_t *to_push) {
111
139
UPDATE_VEHICLE_SPEED (speed / 4.0 * 0.01 / 3.6 );
112
140
}
113
141
114
- // most cars have brake_pressed on 0x226, corolla and rav4 on 0x224
115
- if (((addr == 0x224 ) && toyota_alt_brake ) || ((addr == 0x226 ) && !toyota_alt_brake )) {
116
- uint8_t bit = (addr == 0x224 ) ? 5U : 37U ;
117
- brake_pressed = GET_BIT (to_push , bit );
118
- }
119
-
120
142
bool stock_ecu_detected = addr == 0x2E4 ; // STEERING_LKA
121
143
if (!toyota_stock_longitudinal && (addr == 0x343 )) {
122
144
stock_ecu_detected = true; // ACC_CONTROL
@@ -203,7 +225,7 @@ static bool toyota_tx_hook(const CANPacket_t *to_send) {
203
225
}
204
226
}
205
227
206
- // LTA angle steering check
228
+ // STEERING_LTA angle steering check
207
229
if (addr == 0x191 ) {
208
230
// check the STEER_REQUEST, STEER_REQUEST_2, TORQUE_WIND_DOWN, STEER_ANGLE_CMD signals
209
231
bool lta_request = GET_BIT (to_send , 0U );
@@ -251,6 +273,20 @@ static bool toyota_tx_hook(const CANPacket_t *to_send) {
251
273
}
252
274
}
253
275
276
+ // STEERING_LTA_2 angle steering check (SecOC)
277
+ if (toyota_secoc && (addr == 0x131 )) {
278
+ // SecOC cars block any form of LTA actuation for now
279
+ bool lta_request = GET_BIT (to_send , 3U ); // STEERING_LTA_2.STEER_REQUEST
280
+ bool lta_request2 = GET_BIT (to_send , 0U ); // STEERING_LTA_2.STEER_REQUEST_2
281
+ int lta_angle_msb = GET_BYTE (to_send , 2 ); // STEERING_LTA_2.STEER_ANGLE_CMD (MSB)
282
+ int lta_angle_lsb = GET_BYTE (to_send , 3 ); // STEERING_LTA_2.STEER_ANGLE_CMD (LSB)
283
+
284
+ bool actuation = lta_request || lta_request2 || (lta_angle_msb != 0 ) || (lta_angle_lsb != 0 );
285
+ if (actuation ) {
286
+ tx = false;
287
+ }
288
+ }
289
+
254
290
// STEER: safety check on bytes 2-3
255
291
if (addr == 0x2E4 ) {
256
292
int desired_torque = (GET_BYTE (to_send , 1 ) << 8 ) | GET_BYTE (to_send , 2 );
@@ -286,6 +322,10 @@ static safety_config toyota_init(uint16_t param) {
286
322
TOYOTA_COMMON_TX_MSGS
287
323
};
288
324
325
+ static const CanMsg TOYOTA_SECOC_TX_MSGS [] = {
326
+ TOYOTA_COMMON_SECOC_TX_MSGS
327
+ };
328
+
289
329
static const CanMsg TOYOTA_LONG_TX_MSGS [] = {
290
330
TOYOTA_COMMON_LONG_TX_MSGS
291
331
};
@@ -298,14 +338,23 @@ static safety_config toyota_init(uint16_t param) {
298
338
const uint32_t TOYOTA_PARAM_STOCK_LONGITUDINAL = 2UL << TOYOTA_PARAM_OFFSET ;
299
339
const uint32_t TOYOTA_PARAM_LTA = 4UL << TOYOTA_PARAM_OFFSET ;
300
340
341
+ #ifdef ALLOW_DEBUG
342
+ const uint32_t TOYOTA_PARAM_SECOC = 8UL << TOYOTA_PARAM_OFFSET ;
343
+ toyota_secoc = GET_FLAG (param , TOYOTA_PARAM_SECOC );
344
+ #endif
345
+
301
346
toyota_alt_brake = GET_FLAG (param , TOYOTA_PARAM_ALT_BRAKE );
302
347
toyota_stock_longitudinal = GET_FLAG (param , TOYOTA_PARAM_STOCK_LONGITUDINAL );
303
348
toyota_lta = GET_FLAG (param , TOYOTA_PARAM_LTA );
304
349
toyota_dbc_eps_torque_factor = param & TOYOTA_EPS_FACTOR ;
305
350
306
351
safety_config ret ;
307
352
if (toyota_stock_longitudinal ) {
308
- SET_TX_MSGS (TOYOTA_TX_MSGS , ret );
353
+ if (toyota_secoc ) {
354
+ SET_TX_MSGS (TOYOTA_SECOC_TX_MSGS , ret );
355
+ } else {
356
+ SET_TX_MSGS (TOYOTA_TX_MSGS , ret );
357
+ }
309
358
} else {
310
359
SET_TX_MSGS (TOYOTA_LONG_TX_MSGS , ret );
311
360
}
@@ -340,6 +389,8 @@ static int toyota_fwd_hook(int bus_num, int addr) {
340
389
// block stock lkas messages and stock acc messages (if OP is doing ACC)
341
390
// in TSS2, 0x191 is LTA which we need to block to avoid controls collision
342
391
bool is_lkas_msg = ((addr == 0x2E4 ) || (addr == 0x412 ) || (addr == 0x191 ));
392
+ // on SecOC cars 0x131 is also LTA
393
+ is_lkas_msg |= toyota_secoc && (addr == 0x131 );
343
394
// in TSS2 the camera does ACC as well, so filter 0x343
344
395
bool is_acc_msg = (addr == 0x343 );
345
396
bool block_msg = is_lkas_msg || (is_acc_msg && !toyota_stock_longitudinal );
0 commit comments