Restore continuous Kona factory steering handoff

This commit is contained in:
firestar5683
2026-11-24 22:14:05 -06:00
parent bf8efea392
commit 6a958352e2
47 changed files with 330 additions and 57 deletions
@@ -1,5 +1,6 @@
import os
from unittest.mock import Mock, patch
from types import SimpleNamespace
import pytest
@@ -8,7 +9,7 @@ from opendbc.car.hyundai.canfd_owner import config_index
from opendbc.car.hyundai.canfd_stock_aol import qualified_long
from opendbc.car.hyundai.interface import CarInterface
from opendbc.car.hyundai.values import CAR, HyundaiFlags
from openpilot.starpilot.car.hyundai.aol import independent_cruise_buttons_supported, native_accepts_cp, policy_for
from openpilot.starpilot.car.hyundai.aol import configure_kona_factory_handoff, independent_cruise_buttons_supported, native_accepts_cp, policy_for
CARS = (CAR.HYUNDAI_KONA_2ND_GEN, CAR.HYUNDAI_KONA_HEV_2ND_GEN)
@@ -226,3 +227,12 @@ def test_idle_handoff_omits_only_kona_steering_packet():
assert [frame for frame in after if frame[0] != 0x12A] == [frame for frame in before if frame[0] != 0x12A]
assert any(frame[0] == 0x12A for frame in before)
assert [data[2] for addr, data, _ in after if addr == 0x12A] == ([] if expected is None else [expected])
@pytest.mark.parametrize('panda_type,faults,expected', [('cuatro', [], 0x1C0C), ('redPanda', [], 0x0C0C), ('cuatro', ['fault'], 0x0C0C)])
def test_kona_factory_handoff_is_limited_to_healthy_internal_c4(panda_type, faults, expected):
cp = kona_params(CARS[0])
cp.alternativeExperience = 32
cp.safetyConfigs[0].safetyParam = 0x0C0C
panda = SimpleNamespace(pandaType=panda_type, faults=faults, ignitionLine=True, ignitionCan=False, controlsAllowed=False)
configure_kona_factory_handoff(cp, [panda])
assert cp.safetyConfigs[0].safetyParam == expected
@@ -232,6 +232,8 @@ typedef bool (*tx_hook)(const CANPacket_t *msg); // returns true if the message
typedef bool (*fwd_hook)(int bus_num, int addr); // returns true if the message should be blocked from forwarding
typedef bool (*fwd_packet_hook)(const CANPacket_t *msg);
typedef bool (*stage_tx_hook)(const CANPacket_t *msg);
typedef bool (*fwd_output_hook)(const CANPacket_t *msg, CANPacket_t *output, bool *replaced);
typedef struct {
safety_hook_init init;
@@ -240,6 +242,8 @@ typedef struct {
tx_hook tx;
fwd_hook fwd;
fwd_packet_hook fwd_packet;
stage_tx_hook stage_tx;
fwd_output_hook fwd_output;
get_checksum_t get_checksum;
compute_checksum_t compute_checksum;
get_counter_t get_counter;
@@ -371,6 +375,11 @@ extern safety_config current_safety_config;
int safety_fwd_hook(int bus_num, int addr);
int safety_fwd_packet_hook(const CANPacket_t *msg);
bool safety_uses_camera_slot(const CANPacket_t *msg);
uint32_t safety_camera_release_delay(const CANPacket_t *msg, uint32_t now);
bool safety_camera_release_current(const CANPacket_t *msg, uint32_t now);
bool safety_stage_tx_hook(const CANPacket_t *msg);
int safety_fwd_output_hook(const CANPacket_t *msg, CANPacket_t *output, bool *replaced);
int set_safety_hooks(uint16_t mode, uint16_t param);
extern const safety_hooks body_hooks;
+114 -21
View File
@@ -820,12 +820,15 @@ static safety_config hyundai_canfd_gv60_extend_tx(const safety_config original,
}
#endif
#define HYUNDAI_CANFD_STEER_MAX_TORQUE 409
#define HYUNDAI_CANFD_STEER_MAX_RATE_UP 10
static bool hyundai_canfd_tx_hook_valid(const CANPacket_t *msg) {
static uint8_t hyundai_canfd_ioniq6_lfa_mirror[4] = {0};
const TorqueSteeringLimits HYUNDAI_CANFD_STEERING_LIMITS = {
.max_torque = 409,
.max_torque = HYUNDAI_CANFD_STEER_MAX_TORQUE,
.max_rt_delta = 375,
.max_rate_up = 10,
.max_rate_up = HYUNDAI_CANFD_STEER_MAX_RATE_UP,
.max_rate_down = 3,
.driver_torque_allowance = 250,
.driver_torque_multiplier = 2,
@@ -1237,6 +1240,11 @@ static uint32_t hyundai_kona_camera_ts = 0U;
static uint32_t hyundai_kona_output_ts = 0U;
static uint8_t hyundai_kona_camera_counter = 0U;
static uint8_t hyundai_kona_output_counter = 0U;
static bool hyundai_kona_pending = false;
static bool hyundai_kona_emitting = false;
static bool hyundai_kona_slot_owned = false;
static uint32_t hyundai_kona_pending_ts = 0U;
static CANPacket_t hyundai_kona_pending_msg;
static bool hyundai_kona_host_steering(void) {
return controls_allowed || ((aol_get_permission_mask() & 1U) != 0U);
@@ -1262,26 +1270,106 @@ static void hyundai_kona_track_output(uint32_t now, bool host, uint8_t counter)
hyundai_kona_output_counter = counter;
}
static bool hyundai_kona_camera_fwd(const CANPacket_t *msg) {
static bool hyundai_kona_camera_without_slot(const CANPacket_t *msg) {
return hyundai_kona_idle_handoff && (msg->bus == 2U) && (msg->addr == 0x12AU);
}
static bool hyundai_kona_camera_release_current(const CANPacket_t *msg, uint32_t now) {
return (msg->bus == 2U) && hyundai_kona_output_seen && hyundai_kona_host_output &&
hyundai_kona_camera_seen && !relay_malfunction && !safety_rx_checks_invalid && aol_rx_healthy() &&
!hyundai_kona_host_steering() && hyundai_kona_lfa_valid(msg) &&
((uint8_t)(msg->data[2] - hyundai_kona_output_counter) == 1U) &&
((uint8_t)(msg->data[2] - hyundai_kona_camera_counter) == 1U) &&
(safety_get_ts_elapsed(now, hyundai_kona_camera_ts) <= 20000U);
}
static bool hyundai_kona_stage_tx(const CANPacket_t *msg) {
bool staged = false;
if (hyundai_kona_idle_handoff && (msg->addr == 0x12AU) && (msg->bus == 0U)) {
const uint32_t now = microsecond_timer_get();
const uint8_t source_delta = (uint8_t)(hyundai_kona_camera_counter - msg->data[2]);
staged = !safety_rx_checks_invalid && aol_rx_healthy() && hyundai_kona_host_steering() &&
hyundai_kona_lfa_valid(msg) && hyundai_kona_camera_seen &&
(safety_get_ts_elapsed(now, hyundai_kona_camera_ts) <= 20000U) && (source_delta <= 1U);
if (staged) {
hyundai_kona_pending_msg = *msg;
hyundai_kona_pending_ts = now;
hyundai_kona_pending = true;
} else {
hyundai_kona_pending = false;
}
}
return staged;
}
static bool hyundai_kona_camera_output(const CANPacket_t *msg, CANPacket_t *output, bool *replaced) {
bool blocked = false;
if (hyundai_kona_idle_handoff && (msg->bus == 2U) && (msg->addr == 0x12AU)) {
const uint32_t now = microsecond_timer_get();
const bool valid = hyundai_kona_lfa_valid(msg);
if (valid) {
const uint8_t delta = (uint8_t)(msg->data[2] - hyundai_kona_camera_counter);
if (!hyundai_kona_camera_seen ||
(safety_get_ts_elapsed(now, hyundai_kona_camera_ts) > 50000U) ||
((delta > 0U) && (delta <= 127U))) {
hyundai_kona_camera_seen = true;
hyundai_kona_camera_counter = msg->data[2];
hyundai_kona_camera_ts = now;
}
}
blocked = !valid || safety_rx_checks_invalid || !aol_rx_healthy() || hyundai_kona_host_steering() ||
(msg->data[2] != hyundai_kona_camera_counter) ||
!hyundai_kona_output_ready(now, false, msg->data[2]);
const uint8_t counter = msg->data[2];
const uint32_t camera_age = safety_get_ts_elapsed(now, hyundai_kona_camera_ts);
const uint8_t delta = (uint8_t)(counter - hyundai_kona_camera_counter);
const bool advancing = (delta > 0U) && (delta <= 127U);
const bool consecutive = hyundai_kona_camera_seen && (delta == 1U) && (camera_age <= 20000U);
blocked = !hyundai_kona_lfa_valid(msg) || safety_rx_checks_invalid || !aol_rx_healthy() ||
(hyundai_kona_camera_seen && (camera_age <= 50000U) && !advancing);
if (!blocked) {
hyundai_kona_track_output(now, false, msg->data[2]);
hyundai_kona_camera_counter = counter;
hyundai_kona_camera_seen = true;
hyundai_kona_camera_ts = now;
const bool authority = hyundai_kona_host_steering();
if (!authority) {
hyundai_kona_slot_owned = false;
}
if (authority && consecutive && hyundai_kona_pending &&
(safety_get_ts_elapsed(now, hyundai_kona_pending_ts) <= 20000U)) {
CANPacket_t candidate = hyundai_kona_pending_msg;
const uint8_t source_delta = (uint8_t)(counter - candidate.data[2]);
const uint8_t camera_grade = (msg->data[4] >> 6U) | ((msg->data[5] & 1U) << 2U);
const bool camera_warning = (camera_grade >= 4U) || ((msg->data[7] & 0xF0U) != 0U);
if (camera_warning) {
candidate.data[4] = (candidate.data[4] & 0x3FU) | (msg->data[4] & 0xC0U);
candidate.data[5] = (candidate.data[5] & 0xFEU) | (msg->data[5] & 0x01U);
candidate.data[7] = (candidate.data[7] & 0x0FU) | (msg->data[7] & 0xF0U);
}
const int torque = (int)((((unsigned int)candidate.data[6] & 0xFU) << 7U) | (candidate.data[5] >> 1U)) - 1024;
if ((source_delta <= 2U) &&
!safety_max_limit_check(torque, HYUNDAI_CANFD_STEER_MAX_TORQUE, -HYUNDAI_CANFD_STEER_MAX_TORQUE)) {
if (GET_BIT(&candidate, 52U)) {
const int max_torque = SAFETY_MAX(desired_torque_last, 0) + HYUNDAI_CANFD_STEER_MAX_RATE_UP;
const int min_torque = SAFETY_MIN(desired_torque_last, 0) - HYUNDAI_CANFD_STEER_MAX_RATE_UP;
const unsigned int raw = (unsigned int)(SAFETY_CLAMP(torque, min_torque, max_torque) + 1024);
candidate.data[5] = (candidate.data[5] & 0x01U) | (uint8_t)((raw & 0x7FU) << 1U);
candidate.data[6] = (candidate.data[6] & 0xF0U) | (uint8_t)(raw >> 7U);
}
candidate.data[2] = counter;
const uint16_t checksum = hyundai_common_canfd_compute_checksum(&candidate) ^ 0x041DU;
candidate.data[0] = (uint8_t)checksum;
candidate.data[1] = (uint8_t)(checksum >> 8U);
hyundai_kona_emitting = true;
*replaced = safety_tx_hook(&candidate);
hyundai_kona_emitting = false;
if (*replaced) {
*output = candidate;
hyundai_kona_slot_owned = true;
}
}
}
hyundai_kona_pending = false;
if (!*replaced) {
const bool camera_neutral = ((msg->data[6] & 0x3FU) == 0x08U) && ((msg->data[5] & 0xFEU) == 0U);
blocked = (hyundai_kona_slot_owned && !camera_neutral) ||
!hyundai_kona_output_ready(now, false, counter);
if (!blocked) {
if (hyundai_kona_host_output) {
desired_torque_last = 0;
}
hyundai_kona_track_output(now, false, counter);
}
}
} else {
hyundai_kona_pending = false;
}
}
return blocked;
@@ -1371,7 +1459,7 @@ static bool hyundai_canfd_tx_hook(const CANPacket_t *msg) {
bool allowed = true;
if (hyundai_kona_idle_handoff && (msg->addr == 0x12AU)) {
const uint32_t now = microsecond_timer_get();
allowed = !safety_rx_checks_invalid && aol_rx_healthy() && hyundai_kona_host_steering() &&
allowed = hyundai_kona_emitting && !safety_rx_checks_invalid && aol_rx_healthy() && hyundai_kona_host_steering() &&
hyundai_kona_lfa_valid(msg) && hyundai_kona_camera_seen &&
(safety_get_ts_elapsed(now, hyundai_kona_camera_ts) <= 50000U) &&
(msg->data[2] == hyundai_kona_camera_counter) && hyundai_kona_output_ready(now, true, msg->data[2]);
@@ -2335,10 +2423,13 @@ static safety_config hyundai_canfd_init(uint16_t raw_param) {
hyundai_daw_init(true, false);
hyundai_canfd_reset_ioniq6_state();
hyundai_ordinary_angle_reset();
#ifdef ALLOW_DEBUG
#if defined(ALLOW_DEBUG) && !defined(STM32F4) && !defined(PANDA_BODY)
hyundai_kona_idle_handoff = (raw_param == 0x1C0CU) && (alternative_experience == 32);
hyundai_kona_camera_seen = false;
hyundai_kona_output_seen = false;
hyundai_kona_pending = false;
hyundai_kona_emitting = false;
hyundai_kona_slot_owned = false;
const bool kona_profile_valid = (raw_param != 0x1C0CU) || hyundai_kona_idle_handoff;
const uint16_t param = hyundai_kona_idle_handoff ? 0x0C0CU : raw_param;
const bool valid = !GET_FLAG(param, 32768U) || (param == 0x8015U) || (param == 0x8095U) ||
@@ -2386,7 +2477,9 @@ const safety_hooks hyundai_canfd_hooks = {
.tx = hyundai_canfd_tx_hook,
.fwd = hyundai_canfd_fwd_hook,
#ifdef ALLOW_DEBUG
.fwd_packet = hyundai_kona_camera_fwd,
.fwd_packet = hyundai_kona_camera_without_slot,
.stage_tx = hyundai_kona_stage_tx,
.fwd_output = hyundai_kona_camera_output,
#endif
.get_counter = hyundai_canfd_get_counter,
.get_checksum = hyundai_canfd_get_checksum,
+46
View File
@@ -366,6 +366,52 @@ int safety_fwd_packet_hook(const CANPacket_t *msg) {
return destination_bus;
}
bool safety_uses_camera_slot(const CANPacket_t *msg) {
#ifdef ALLOW_DEBUG
return (current_safety_mode == SAFETY_HYUNDAI_CANFD) && hyundai_kona_idle_handoff && (msg->addr == 0x12AU);
#else
(void)msg;
return false;
#endif
}
bool safety_camera_release_current(const CANPacket_t *msg, uint32_t now) {
#ifdef ALLOW_DEBUG
return safety_uses_camera_slot(msg) && hyundai_kona_camera_release_current(msg, now);
#else
(void)msg;
(void)now;
return false;
#endif
}
uint32_t safety_camera_release_delay(const CANPacket_t *msg, uint32_t now) {
#ifdef ALLOW_DEBUG
const uint32_t age = safety_get_ts_elapsed(now, hyundai_kona_output_ts);
return ((age >= 9000U) && (age < 10000U) && safety_camera_release_current(msg, now)) ? (10000U - age) : 0U;
#else
(void)msg;
(void)now;
return 0U;
#endif
}
bool safety_stage_tx_hook(const CANPacket_t *msg) {
return !relay_malfunction && (current_hooks->stage_tx != NULL) && current_hooks->stage_tx(msg);
}
int safety_fwd_output_hook(const CANPacket_t *msg, CANPacket_t *output, bool *replaced) {
int destination = safety_fwd_hook(msg->bus, msg->addr);
if (destination >= 0) {
if (current_hooks->fwd_output != NULL) {
destination = current_hooks->fwd_output(msg, output, replaced) ? -1 : destination;
} else if ((current_hooks->fwd_packet != NULL) && current_hooks->fwd_packet(msg)) {
destination = -1;
}
}
return destination;
}
// 1Hz safety function called by main. Now just a check for lagging safety messages
void safety_tick(void) {
const uint8_t MAX_MISSED_MSGS = 10U;
@@ -29,50 +29,60 @@ class TestHyundaiKonaCcncHandoff(unittest.TestCase):
self.assertFalse(self.safety.safety_tx_hook(self.packet(0, 'a6770a1880001800000a000000900000')))
self.assertEqual(self.safety.safety_fwd_hook(2, 0x123), -1)
def forward(self, packet):
output = libsafety_py.new_CANPacket()
replaced = libsafety_py.ffi.new('bool *')
bus = self.safety.safety_fwd_output_hook(packet, output, replaced)
return bus, bool(replaced[0]), output
def test_handoff_requires_current_camera_and_one_output_owner(self):
if self.safety.set_safety_hooks(structs.CarParams.SafetyModel.allOutput, 0) != 0:
self.skipTest('Kona factory handoff is DEBUG only')
camera = self.packet(2, 'a6770a1880001800000a000000900000')
self.mode(0x0C0C)
self.assertEqual(self.safety.safety_fwd_packet_hook(camera), -1)
self.assertEqual(self.forward(camera)[0], -1)
self.mode(0x1C0C)
self.safety.set_timer(1_000_000)
self.assertEqual(self.safety.safety_fwd_packet_hook(camera), -1)
self.assertEqual(self.forward(camera)[0], -1)
for status in self.safety.rx_status:
status.msg_seen = True
status.valid_checksum = True
status.valid_quality_flag = True
status.last_timestamp = 1_000_000
self.assertEqual(self.safety.safety_fwd_packet_hook(camera), 0)
self.assertEqual(self.safety.safety_fwd_packet_hook(camera), -1)
self.assertEqual(self.forward(camera)[:2], (0, False))
self.assertEqual(self.forward(camera)[0], -1)
corrupt = self.packet(2, 'a6770a1881001800000a000000900000')
self.assertEqual(self.safety.safety_fwd_packet_hook(corrupt), -1)
self.assertFalse(self.safety.safety_tx_hook(self.packet(0, 'a6770a1880001800000a000000900000')))
self.assertEqual(self.forward(corrupt)[0], -1)
self.assertFalse(self.safety.safety_stage_tx_hook(self.packet(0, 'a6770a1880001800000a000000900000')))
warning = self.packet(2, '784d1400000108f0000a000000640000')
self.assertEqual(self.safety.safety_fwd_packet_hook(warning), 0)
self.assertEqual(self.forward(warning)[:2], (0, False))
self.safety.set_controls_allowed(True)
self.safety.set_aol_test_heartbeat(True)
self.safety.aol_set_host_request(3)
following = self.packet(2, '110d1600000108f0000a000000640000')
host = self.packet(0, 'a009150280001800000a000000900000')
self.safety.set_timer(1_009_999)
self.assertFalse(self.safety.safety_tx_hook(host)) # Require the matching camera counter first.
camera_match = self.packet(2, '5ce51500000108f0000a000000640000')
self.assertEqual(self.safety.safety_fwd_packet_hook(camera_match), -1)
host[0].data[2] = 20
from opendbc.car.hyundai.hyundaicanfd import hkg_can_fd_checksum
crc = hkg_can_fd_checksum(0x12A, None, bytearray(host[0].data[0:16]))
host[0].data[0], host[0].data[1] = crc & 255, crc >> 8
self.safety.set_timer(1_005_000)
self.assertFalse(self.safety.safety_tx_hook(host))
self.assertTrue(self.safety.safety_stage_tx_hook(host))
self.safety.set_timer(1_010_000)
self.assertTrue(self.safety.safety_tx_hook(host))
self.assertFalse(self.safety.safety_tx_hook(host))
match = self.packet(2, '5ce51500000108f0000a000000640000')
bus, replaced, output = self.forward(match)
self.assertEqual((bus, replaced, output[0].data[2]), (0, True, 21))
self.assertEqual(output[0].data[7] & 0xF0, 0xF0)
self.safety.set_controls_allowed(False)
self.safety.aol_set_host_request(0)
following = self.packet(2, '110d1600000108f0000a000000640000')
self.safety.set_timer(1_019_999)
self.assertEqual(self.safety.safety_fwd_packet_hook(following), -1)
self.assertEqual(self.safety.safety_camera_release_delay(following, 1_019_999), 1)
self.safety.set_timer(1_020_000)
self.assertEqual(self.safety.safety_fwd_packet_hook(following), 0)
self.assertEqual(self.safety.safety_fwd_packet_hook(following), -1)
self.assertEqual(self.forward(following)[:2], (0, False))
self.assertEqual(self.forward(following)[0], -1)
self.safety.set_relay_malfunction(True)
self.assertEqual(self.safety.safety_fwd_packet_hook(following), -1)
self.assertFalse(self.safety.safety_tx_hook(host))
self.assertEqual(self.forward(following)[0], -1)
self.assertFalse(self.safety.safety_stage_tx_hook(host))
+5
View File
@@ -365,6 +365,11 @@ class Car:
raise RuntimeError('AOL requires the exact vehicle safety configuration')
config.safetyParam |= aol_policy.safety_param_addition
self.CP.alternativeExperience |= aol_policy.alternative_experience_addition
if self.CP.carFingerprint == HYUNDAI_CAR.HYUNDAI_KONA_2ND_GEN and config.safetyParam == 0x0C0C and not is_release:
from openpilot.starpilot.car.hyundai.aol import configure_kona_factory_handoff
self.sm.update(0)
if self.sm.valid['pandaStates'] and self.sm.alive['pandaStates']:
configure_kona_factory_handoff(self.CP, self.sm['pandaStates'])
self.vehicle_startup.finalize_aol_configuration(self.CI)
from openpilot.starpilot.car.hyundai.redneck_cruise import configure
self.redneck_cruise = configure(self, subscribed_services)
Binary file not shown.
+11
View File
@@ -26,6 +26,7 @@ from opendbc.car.hyundai.canfd_owner import config_index
from opendbc.car.hyundai.canfd_stock_aol import (
qualified as qualified_canfd_stock, qualified_long as qualified_canfd_long,
LONG_AOL_WORDS as CANFD_LONG_AOL_WORDS, STOCK_AOL_MARKER, STOCK_AOL_WORDS,
KONA_IDLE_AOL_WORD, kona_idle_handoff_profile,
)
from opendbc.car.hyundai.non_scc_aol import qualified as qualified_non_scc, aol_word, AOL_MARKER, AOL_EXPERIENCE, AOL_WORDS, SOFTWARE_CRUISE
from opendbc.car.hyundai.kona_aol import allow_lateral_onset as kona_lateral_onset
@@ -202,6 +203,16 @@ def aol_safety_config(CP):
return CP.safetyConfigs[owner] if owner is not None else None
def configure_kona_factory_handoff(cp, pandas):
if (not kona_idle_handoff_profile(cp) or cp.alternativeExperience != 32 or
cp.safetyConfigs[0].safetyParam != 0x0C0C or len(pandas) != 1):
return
panda = pandas[0]
if (str(panda.pandaType) == 'cuatro' and not panda.faults and
(panda.ignitionCan or panda.ignitionLine) and not panda.controlsAllowed):
cp.safetyConfigs[0].safetyParam = KONA_IDLE_AOL_WORD
def policy_for(CP) -> AolVehiclePolicy:
policy = _policy_for(CP)
if (CP.carFingerprint in IONIQ_5_CARS and has_left_paddle(CP) and
+10 -2
View File
@@ -187,11 +187,19 @@ bool can_check_checksum(CANPacket_t *packet) {
void can_send(CANPacket_t *to_push, uint8_t bus_number, bool skip_tx_hook) {
bool tx_allowed = skip_tx_hook;
bool staged = false;
if (!tx_allowed) {
#ifdef STM32H7
mpu_safety_open();
#endif
tx_allowed = safety_tx_hook(to_push) != 0;
#ifdef STM32H7
if (safety_uses_camera_slot(to_push)) {
ENTER_CRITICAL();
staged = safety_stage_tx_hook(to_push);
EXIT_CRITICAL();
}
#endif
tx_allowed = !staged && (safety_tx_hook(to_push) != 0);
#ifdef STM32H7
mpu_safety_close();
#endif
@@ -202,7 +210,7 @@ void can_send(CANPacket_t *to_push, uint8_t bus_number, bool skip_tx_hook) {
tx_buffer_overflow += can_push(can_queues[bus_number], to_push) ? 0U : 1U;
process_can(CAN_NUM_FROM_BUS_NUM(bus_number));
}
} else {
} else if (!staged) {
safety_tx_blocked += 1U;
to_push->returned = 0U;
to_push->rejected = 1U;
+93 -12
View File
@@ -154,6 +154,60 @@ void process_can(uint8_t can_number) {
}
}
#if defined(ALLOW_DEBUG) && !defined(PANDA_BODY)
static bool kona_release_pending = false;
static CANPacket_t kona_release_frame;
static uint32_t kona_release_due = 0U;
static uint8_t kona_release_source = 0U;
static void kona_release_cancel(void) {
if (kona_release_pending) {
kona_release_pending = false;
register_clear_bits(&MICROSECOND_TIMER->DIER, TIM_DIER_CC1IE);
MICROSECOND_TIMER->SR = ~TIM_SR_CC1IF;
NVIC_ClearPendingIRQ(TIM2_IRQn);
}
}
static void kona_release_irq(void) {
ENTER_CRITICAL();
const uint32_t now = microsecond_timer_get();
const uint32_t late = get_ts_elapsed(now, kona_release_due);
if (kona_release_pending && (late < 0x80000000U)) {
kona_release_cancel();
mpu_safety_open();
CANPacket_t output = kona_release_frame;
bool replaced = false;
int destination = -1;
if ((late <= 1000U) && safety_camera_release_current(&kona_release_frame, now)) {
destination = safety_fwd_output_hook(&kona_release_frame, &output, &replaced);
}
mpu_safety_close();
if ((destination >= 0) && !replaced) {
can_set_checksum(&output);
can_send(&output, (uint8_t)destination, true);
can_health[kona_release_source].total_fwd_cnt += 1U;
}
}
MICROSECOND_TIMER->SR = ~TIM_SR_CC1IF;
EXIT_CRITICAL();
}
static void kona_release_schedule(const CANPacket_t *msg, uint8_t source, uint32_t due) {
kona_release_cancel();
kona_release_frame = *msg;
kona_release_source = source;
kona_release_due = due;
kona_release_pending = true;
MICROSECOND_TIMER->SR = ~TIM_SR_CC1IF;
MICROSECOND_TIMER->CCR1 = due;
register_set_bits(&MICROSECOND_TIMER->DIER, TIM_DIER_CC1IE);
if (get_ts_elapsed(microsecond_timer_get(), due) < 0x80000000U) {
NVIC_SetPendingIRQ(TIM2_IRQn);
}
}
#endif
// FDFDCANx_IT0 IRQ Handler (RX and errors)
// blink blue when we are receiving CAN messages
static void can_rx(uint8_t can_number) {
@@ -201,26 +255,46 @@ static void can_rx(uint8_t can_number) {
can_set_checksum(&to_push);
// forwarding
CANPacket_t to_send;
bool replaced = false;
#ifdef STM32H7
mpu_safety_open();
#endif
int bus_fwd_num = safety_fwd_packet_hook(&to_push);
int bus_fwd_num;
if (safety_uses_camera_slot(&to_push)) {
ENTER_CRITICAL();
#if defined(ALLOW_DEBUG) && !defined(PANDA_BODY)
const uint32_t now = microsecond_timer_get();
const uint32_t delay = safety_camera_release_delay(&to_push, now);
kona_release_cancel();
if (delay != 0U) {
kona_release_schedule(&to_push, can_number, now + delay);
bus_fwd_num = -1;
} else {
bus_fwd_num = safety_fwd_output_hook(&to_push, &to_send, &replaced);
}
#else
bus_fwd_num = safety_fwd_output_hook(&to_push, &to_send, &replaced);
#endif
EXIT_CRITICAL();
} else {
bus_fwd_num = safety_fwd_packet_hook(&to_push);
}
#ifdef STM32H7
mpu_safety_close();
#endif
if (bus_fwd_num >= 0) {
CANPacket_t to_send;
to_send.fd = to_push.fd;
to_send.returned = 0U;
to_send.rejected = 0U;
to_send.extended = to_push.extended;
to_send.addr = to_push.addr;
to_send.bus = to_push.bus;
to_send.data_len_code = to_push.data_len_code;
(void)memcpy(to_send.data, to_push.data, dlc_to_len[to_push.data_len_code]);
if (!replaced) {
to_send.fd = to_push.fd;
to_send.returned = 0U;
to_send.rejected = 0U;
to_send.extended = to_push.extended;
to_send.addr = to_push.addr;
to_send.bus = to_push.bus;
to_send.data_len_code = to_push.data_len_code;
(void)memcpy(to_send.data, to_push.data, dlc_to_len[to_push.data_len_code]);
}
can_set_checksum(&to_send);
can_send(&to_send, bus_fwd_num, true);
can_health[can_number].total_fwd_cnt += 1U;
}
@@ -279,6 +353,13 @@ bool can_init(uint8_t can_number) {
REGISTER_INTERRUPT(FDCAN2_IT1_IRQn, FDCAN2_IT1_IRQ_Handler, CAN_INTERRUPT_RATE, FAULT_INTERRUPT_RATE_CAN_2)
REGISTER_INTERRUPT(FDCAN3_IT0_IRQn, FDCAN3_IT0_IRQ_Handler, CAN_INTERRUPT_RATE, FAULT_INTERRUPT_RATE_CAN_3)
REGISTER_INTERRUPT(FDCAN3_IT1_IRQn, FDCAN3_IT1_IRQ_Handler, CAN_INTERRUPT_RATE, FAULT_INTERRUPT_RATE_CAN_3)
#if defined(ALLOW_DEBUG) && !defined(PANDA_BODY)
ENTER_CRITICAL();
REGISTER_INTERRUPT(TIM2_IRQn, kona_release_irq, 100U, FAULT_INTERRUPT_RATE_CAN_3)
kona_release_cancel();
nvic_enable(TIM2_IRQn);
EXIT_CRITICAL();
#endif
if (can_number != 0xffU) {
FDCAN_GlobalTypeDef *FDCANx = CANIF_FROM_CAN_NUM(can_number);
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -1 +1 @@
DEV-ab19fed8-Pf38f504f-DEBUG
DEV-5013abfc-Pf38f504f-DEBUG
Binary file not shown.
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "146e474c07017cd244799d7c894d93e2d02916a08b6ab954c3e171d5865f1f97", "schema_version": 1}
{"protected_inventory_sha256": "d5c1f322228933a75d790d0500dd3135b4d607bb0b7e356411ad52b1f246b079", "schema_version": 1}