Line | Branch | Exec | Source |
---|---|---|---|
1 | #pragma once | ||
2 | |||
3 | #include "safety_declarations.h" | ||
4 | |||
5 | // CAN msgs we care about | ||
6 | #define MAZDA_LKAS 0x243 | ||
7 | #define MAZDA_LKAS_HUD 0x440 | ||
8 | #define MAZDA_CRZ_CTRL 0x21c | ||
9 | #define MAZDA_CRZ_BTNS 0x09d | ||
10 | #define MAZDA_STEER_TORQUE 0x240 | ||
11 | #define MAZDA_ENGINE_DATA 0x202 | ||
12 | #define MAZDA_PEDALS 0x165 | ||
13 | |||
14 | // CAN bus numbers | ||
15 | #define MAZDA_MAIN 0 | ||
16 | #define MAZDA_CAM 2 | ||
17 | |||
18 | // track msgs coming from OP so that we know what CAM msgs to drop and what to forward | ||
19 | 8928 | static void mazda_rx_hook(const CANPacket_t *to_push) { | |
20 |
2/2✓ Branch 0 taken 3296 times.
✓ Branch 1 taken 5632 times.
|
8928 | if ((int)GET_BUS(to_push) == MAZDA_MAIN) { |
21 | 3296 | int addr = GET_ADDR(to_push); | |
22 | |||
23 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3278 times.
|
3296 | if (addr == MAZDA_ENGINE_DATA) { |
24 | // sample speed: scale by 0.01 to get kph | ||
25 | 18 | int speed = (GET_BYTE(to_push, 2) << 8) | GET_BYTE(to_push, 3); | |
26 | 18 | vehicle_moving = speed > 10; // moving when speed > 0.1 kph | |
27 | } | ||
28 | |||
29 |
2/2✓ Branch 0 taken 445 times.
✓ Branch 1 taken 2851 times.
|
3296 | if (addr == MAZDA_STEER_TORQUE) { |
30 | 445 | int torque_driver_new = GET_BYTE(to_push, 0) - 127U; | |
31 | // update array of samples | ||
32 | 445 | update_sample(&torque_driver, torque_driver_new); | |
33 | } | ||
34 | |||
35 | // enter controls on rising edge of ACC, exit controls on ACC off | ||
36 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3288 times.
|
3296 | if (addr == MAZDA_CRZ_CTRL) { |
37 | 8 | bool cruise_engaged = GET_BYTE(to_push, 0) & 0x8U; | |
38 | 8 | pcm_cruise_check(cruise_engaged); | |
39 | } | ||
40 | |||
41 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3278 times.
|
3296 | if (addr == MAZDA_ENGINE_DATA) { |
42 |
3/4✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 12 times.
|
18 | gas_pressed = (GET_BYTE(to_push, 4) || (GET_BYTE(to_push, 5) & 0xF0U)); |
43 | } | ||
44 | |||
45 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3283 times.
|
3296 | if (addr == MAZDA_PEDALS) { |
46 | 13 | brake_pressed = (GET_BYTE(to_push, 0) & 0x10U); | |
47 | } | ||
48 | |||
49 | 3296 | generic_rx_checks((addr == MAZDA_LKAS)); | |
50 | } | ||
51 | 8928 | } | |
52 | |||
53 | 6087 | static bool mazda_tx_hook(const CANPacket_t *to_send) { | |
54 | const SteeringLimits MAZDA_STEERING_LIMITS = { | ||
55 | .max_steer = 800, | ||
56 | .max_rate_up = 10, | ||
57 | .max_rate_down = 25, | ||
58 | .max_rt_delta = 300, | ||
59 | .max_rt_interval = 250000, | ||
60 | .driver_torque_multiplier = 1, | ||
61 | .driver_torque_allowance = 15, | ||
62 | .type = TorqueDriverLimited, | ||
63 | }; | ||
64 | |||
65 | 6087 | bool tx = true; | |
66 | 6087 | int bus = GET_BUS(to_send); | |
67 | // Check if msg is sent on the main BUS | ||
68 |
1/2✓ Branch 0 taken 6087 times.
✗ Branch 1 not taken.
|
6087 | if (bus == MAZDA_MAIN) { |
69 | 6087 | int addr = GET_ADDR(to_send); | |
70 | |||
71 | // steer cmd checks | ||
72 |
2/2✓ Branch 0 taken 6081 times.
✓ Branch 1 taken 6 times.
|
6087 | if (addr == MAZDA_LKAS) { |
73 | 6081 | int desired_torque = (((GET_BYTE(to_send, 0) & 0x0FU) << 8) | GET_BYTE(to_send, 1)) - 2048U; | |
74 | |||
75 |
2/2✓ Branch 1 taken 3235 times.
✓ Branch 2 taken 2846 times.
|
6081 | if (steer_torque_cmd_checks(desired_torque, -1, MAZDA_STEERING_LIMITS)) { |
76 | 3235 | tx = false; | |
77 | } | ||
78 | } | ||
79 | |||
80 | // cruise buttons check | ||
81 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6082 times.
|
6087 | if (addr == MAZDA_CRZ_BTNS) { |
82 | // allow resume spamming while controls allowed, but | ||
83 | // only allow cancel while contrls not allowed | ||
84 | 5 | bool cancel_cmd = (GET_BYTE(to_send, 0) == 0x1U); | |
85 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
|
5 | if (!controls_allowed && !cancel_cmd) { |
86 | 2 | tx = false; | |
87 | } | ||
88 | } | ||
89 | } | ||
90 | |||
91 | 6087 | return tx; | |
92 | } | ||
93 | |||
94 | 8448 | static int mazda_fwd_hook(int bus, int addr) { | |
95 | 8448 | int bus_fwd = -1; | |
96 | |||
97 |
2/2✓ Branch 0 taken 2816 times.
✓ Branch 1 taken 5632 times.
|
8448 | if (bus == MAZDA_MAIN) { |
98 | 2816 | bus_fwd = MAZDA_CAM; | |
99 |
2/2✓ Branch 0 taken 2816 times.
✓ Branch 1 taken 2816 times.
|
5632 | } else if (bus == MAZDA_CAM) { |
100 |
4/4✓ Branch 0 taken 2815 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2814 times.
|
2816 | bool block = (addr == MAZDA_LKAS) || (addr == MAZDA_LKAS_HUD); |
101 |
2/2✓ Branch 0 taken 2814 times.
✓ Branch 1 taken 2 times.
|
2816 | if (!block) { |
102 | 2814 | bus_fwd = MAZDA_MAIN; | |
103 | } | ||
104 | } else { | ||
105 | // don't fwd | ||
106 | } | ||
107 | |||
108 | 8448 | return bus_fwd; | |
109 | } | ||
110 | |||
111 | 30 | static safety_config mazda_init(uint16_t param) { | |
112 | static const CanMsg MAZDA_TX_MSGS[] = {{MAZDA_LKAS, 0, 8}, {MAZDA_CRZ_BTNS, 0, 8}, {MAZDA_LKAS_HUD, 0, 8}}; | ||
113 | |||
114 | static RxCheck mazda_rx_checks[] = { | ||
115 | {.msg = {{MAZDA_CRZ_CTRL, 0, 8, .frequency = 50U}, { 0 }, { 0 }}}, | ||
116 | {.msg = {{MAZDA_CRZ_BTNS, 0, 8, .frequency = 10U}, { 0 }, { 0 }}}, | ||
117 | {.msg = {{MAZDA_STEER_TORQUE, 0, 8, .frequency = 83U}, { 0 }, { 0 }}}, | ||
118 | {.msg = {{MAZDA_ENGINE_DATA, 0, 8, .frequency = 100U}, { 0 }, { 0 }}}, | ||
119 | {.msg = {{MAZDA_PEDALS, 0, 8, .frequency = 50U}, { 0 }, { 0 }}}, | ||
120 | }; | ||
121 | |||
122 | UNUSED(param); | ||
123 | 30 | return BUILD_SAFETY_CFG(mazda_rx_checks, MAZDA_TX_MSGS); | |
124 | } | ||
125 | |||
126 | const safety_hooks mazda_hooks = { | ||
127 | .init = mazda_init, | ||
128 | .rx = mazda_rx_hook, | ||
129 | .tx = mazda_tx_hook, | ||
130 | .fwd = mazda_fwd_hook, | ||
131 | }; | ||
132 |