Skip to content

Commit

Permalink
Merge commit 'b4442a7c930aac112cdd82cddfc3dd12254a56e1'
Browse files Browse the repository at this point in the history
  • Loading branch information
programanichiro committed Feb 20, 2024
2 parents 0a15ee0 + b4442a7 commit ed4966d
Show file tree
Hide file tree
Showing 23 changed files with 77 additions and 78 deletions.
4 changes: 2 additions & 2 deletions board/drivers/can_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ extern int can_live;
extern int pending_can_live;

// must reinit after changing these
extern int can_loopback;
extern int can_silent;
extern bool can_loopback;

// Ignition detected from CAN meessages
bool ignition_can = false;
Expand All @@ -40,8 +40,8 @@ uint32_t ignition_can_cnt = 0U;

int can_live = 0;
int pending_can_live = 0;
int can_loopback = 0;
int can_silent = ALL_CAN_SILENT;
bool can_loopback = false;

// ******************* functions prototypes *********************
bool can_init(uint8_t can_number);
Expand Down
4 changes: 2 additions & 2 deletions board/drivers/gmlan_alt.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ int do_bitstuff(char *out, const char *in, int in_len) {
bit_cnt++;
if (bit_cnt == 5) {
// 5 in a row the same, do stuff
last_bit = !bit;
last_bit = !bit ? 1 : 0;
out[j] = last_bit;
j++;
bit_cnt = 1;
}
} else {
// this is a new bit
last_bit = bit;
last_bit = (int)bit;
bit_cnt = 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion board/drivers/interrupts.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void interrupt_timer_handler(void) {
// Calculate interrupt load
// The bootstub does not have the FPU enabled, so can't do float operations.
#if !defined(BOOTSTUB)
interrupt_load = ((busy_time + idle_time) > 0U) ? ((float) busy_time) / (busy_time + idle_time) : 0.0f;
interrupt_load = ((busy_time + idle_time) > 0U) ? ((float) (((float) busy_time) / (busy_time + idle_time))) : 0.0f;
#endif
idle_time = 0U;
busy_time = 0U;
Expand Down
6 changes: 3 additions & 3 deletions board/drivers/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,12 @@ void usb_reset(void) {
USBx_OUTEP(0)->DOEPTSIZ = USB_OTG_DOEPTSIZ_STUPCNT | (USB_OTG_DOEPTSIZ_PKTCNT & (1UL << 19)) | (3U << 3);
}

char to_hex_char(int a) {
char to_hex_char(uint8_t a) {
char ret;
if (a < 10) {
if (a < 10U) {
ret = '0' + a;
} else {
ret = 'a' + (a - 10);
ret = 'a' + (a - 10U);
}
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion board/main_comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
break;
// **** 0xe5: set CAN loopback (for testing)
case 0xe5:
can_loopback = (req->param1 > 0U);
can_loopback = req->param1 > 0U;
can_init_all();
break;
// **** 0xe6: set custom clock source period
Expand Down
4 changes: 2 additions & 2 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ bool get_longitudinal_allowed(void) {
// Given a CRC-8 poly, generate a static lookup table to use with a fast CRC-8
// algorithm. Called at init time for safety modes using CRC-8.
void gen_crc_lookup_table_8(uint8_t poly, uint8_t crc_lut[]) {
for (int i = 0; i < 256; i++) {
uint8_t crc = i;
for (uint16_t i = 0U; i <= 0xFFU; i++) {
uint8_t crc = (uint8_t)i;
for (int j = 0; j < 8; j++) {
if ((crc & 0x80U) != 0U) {
crc = (uint8_t)((crc << 1) ^ poly);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static void chrysler_rx_hook(const CANPacket_t *to_push) {
// enter controls on rising edge of ACC, exit controls on ACC off
const int das_3_bus = (chrysler_platform == CHRYSLER_PACIFICA) ? 0 : 2;
if ((bus == das_3_bus) && (addr == chrysler_addrs->DAS_3)) {
bool cruise_engaged = GET_BIT(to_push, 21U) == 1U;
bool cruise_engaged = GET_BIT(to_push, 21U);
pcm_cruise_check(cruise_engaged);
}

Expand Down Expand Up @@ -226,7 +226,7 @@ static bool chrysler_tx_hook(const CANPacket_t *to_send) {
const SteeringLimits limits = (chrysler_platform == CHRYSLER_PACIFICA) ? CHRYSLER_STEERING_LIMITS :
(chrysler_platform == CHRYSLER_RAM_DT) ? CHRYSLER_RAM_DT_STEERING_LIMITS : CHRYSLER_RAM_HD_STEERING_LIMITS;

bool steer_req = (chrysler_platform == CHRYSLER_PACIFICA) ? (GET_BIT(to_send, 4U) != 0U) : ((GET_BYTE(to_send, 3) & 0x7U) == 2U);
bool steer_req = (chrysler_platform == CHRYSLER_PACIFICA) ? GET_BIT(to_send, 4U) : (GET_BYTE(to_send, 3) & 0x7U) == 2U;
if (steer_torque_cmd_checks(desired_torque, steer_req, limits)) {
tx = false;
}
Expand Down
8 changes: 4 additions & 4 deletions board/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,15 @@ static bool ford_tx_hook(const CANPacket_t *to_send) {
// Signal: AccBrkTot_A_Rq
int accel = ((GET_BYTE(to_send, 0) & 0x1FU) << 8) | GET_BYTE(to_send, 1);
// Signal: CmbbDeny_B_Actl
int cmbb_deny = GET_BIT(to_send, 37U);
bool cmbb_deny = GET_BIT(to_send, 37U);

bool violation = false;
violation |= longitudinal_accel_checks(accel, FORD_LONG_LIMITS);
violation |= longitudinal_gas_checks(gas, FORD_LONG_LIMITS);
violation |= longitudinal_gas_checks(gas_pred, FORD_LONG_LIMITS);

// Safety check for stock AEB
violation |= cmbb_deny != 0; // do not prevent stock AEB actuation
violation |= cmbb_deny; // do not prevent stock AEB actuation

if (violation) {
tx = false;
Expand All @@ -302,8 +302,8 @@ static bool ford_tx_hook(const CANPacket_t *to_send) {
// Violation if resume button is pressed while controls not allowed, or
// if cancel button is pressed when cruise isn't engaged.
bool violation = false;
violation |= (GET_BIT(to_send, 8U) == 1U) && !cruise_engaged_prev; // Signal: CcAslButtnCnclPress (cancel)
violation |= (GET_BIT(to_send, 25U) == 1U) && !controls_allowed; // Signal: CcAsllButtnResPress (resume)
violation |= GET_BIT(to_send, 8U) && !cruise_engaged_prev; // Signal: CcAslButtnCnclPress (cancel)
violation |= GET_BIT(to_send, 25U) && !controls_allowed; // Signal: CcAsllButtnResPress (resume)

if (violation) {
tx = false;
Expand Down
8 changes: 4 additions & 4 deletions board/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static void gm_rx_hook(const CANPacket_t *to_push) {
}

if ((addr == 0xC9) && (gm_hw == GM_CAM)) {
brake_pressed = GET_BIT(to_push, 40U) != 0U;
brake_pressed = GET_BIT(to_push, 40U);
}

if (addr == 0x1C4) {
Expand Down Expand Up @@ -153,7 +153,7 @@ static bool gm_tx_hook(const CANPacket_t *to_send) {
int desired_torque = ((GET_BYTE(to_send, 0) & 0x7U) << 8) + GET_BYTE(to_send, 1);
desired_torque = to_signed(desired_torque, 11);

bool steer_req = (GET_BIT(to_send, 3U) != 0U);
bool steer_req = GET_BIT(to_send, 3U);

if (steer_torque_cmd_checks(desired_torque, steer_req, GM_STEERING_LIMITS)) {
tx = false;
Expand All @@ -162,7 +162,7 @@ static bool gm_tx_hook(const CANPacket_t *to_send) {

// GAS/REGEN: safety check
if (addr == 0x2CB) {
bool apply = GET_BIT(to_send, 0U) != 0U;
bool apply = GET_BIT(to_send, 0U);
int gas_regen = ((GET_BYTE(to_send, 2) & 0x7FU) << 5) + ((GET_BYTE(to_send, 3) & 0xF8U) >> 3);

bool violation = false;
Expand Down Expand Up @@ -204,7 +204,7 @@ static int gm_fwd_hook(int bus_num, int addr) {
// block lkas message and acc messages if gm_cam_long, forward all others
bool is_lkas_msg = (addr == 0x180);
bool is_acc_msg = (addr == 0x315) || (addr == 0x2CB) || (addr == 0x370);
int block_msg = is_lkas_msg || (is_acc_msg && gm_cam_long);
bool block_msg = is_lkas_msg || (is_acc_msg && gm_cam_long);
if (!block_msg) {
bus_fwd = 0;
}
Expand Down
20 changes: 10 additions & 10 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ static uint32_t honda_compute_checksum(const CANPacket_t *to_push) {
uint8_t checksum = 0U;
unsigned int addr = GET_ADDR(to_push);
while (addr > 0U) {
checksum += (addr & 0xFU); addr >>= 4;
checksum += (uint8_t)(addr & 0xFU); addr >>= 4;
}
for (int j = 0; j < len; j++) {
uint8_t byte = GET_BYTE(to_push, j);
checksum += (byte & 0xFU) + (byte >> 4U);
checksum += (uint8_t)(byte & 0xFU) + (byte >> 4U);
if (j == (len - 1)) {
checksum -= (byte & 0xFU); // remove checksum in message
}
Expand Down Expand Up @@ -166,7 +166,7 @@ static void honda_rx_hook(const CANPacket_t *to_push) {

// enter controls when PCM enters cruise state
if (pcm_cruise && (addr == 0x17C)) {
const bool cruise_engaged = GET_BIT(to_push, 38U) != 0U;
const bool cruise_engaged = GET_BIT(to_push, 38U);
// engage on rising edge
if (cruise_engaged && !cruise_engaged_prev) {
controls_allowed = true;
Expand Down Expand Up @@ -207,13 +207,13 @@ static void honda_rx_hook(const CANPacket_t *to_push) {
// accord, crv: 0x1BE
if (honda_alt_brake_msg) {
if (addr == 0x1BE) {
brake_pressed = GET_BIT(to_push, 4U) != 0U;
brake_pressed = GET_BIT(to_push, 4U);
}
} else {
if (addr == 0x17C) {
// also if brake switch is 1 for two CAN frames, as brake pressed is delayed
const bool brake_switch = GET_BIT(to_push, 32U) != 0U;
brake_pressed = (GET_BIT(to_push, 53U) != 0U) || (brake_switch && honda_brake_switch_prev);
const bool brake_switch = GET_BIT(to_push, 32U);
brake_pressed = (GET_BIT(to_push, 53U)) || (brake_switch && honda_brake_switch_prev);
honda_brake_switch_prev = brake_switch;
}
}
Expand All @@ -234,7 +234,7 @@ static void honda_rx_hook(const CANPacket_t *to_push) {
// disable stock Honda AEB in alternative experience
if (!(alternative_experience & ALT_EXP_DISABLE_STOCK_AEB)) {
if ((bus == 2) && (addr == 0x1FA)) {
bool honda_stock_aeb = GET_BIT(to_push, 29U) != 0U;
bool honda_stock_aeb = GET_BIT(to_push, 29U);
int honda_stock_brake = (GET_BYTE(to_push, 0) << 2) | (GET_BYTE(to_push, 1) >> 6);

// Forward AEB when stock braking is higher than openpilot braking
Expand Down Expand Up @@ -383,7 +383,7 @@ static safety_config honda_nidec_init(uint16_t param) {
enable_gas_interceptor = GET_FLAG(param, HONDA_PARAM_GAS_INTERCEPTOR);

safety_config ret;

bool enable_nidec_alt = GET_FLAG(param, HONDA_PARAM_NIDEC_ALT);
if (enable_nidec_alt) {
enable_gas_interceptor ? SET_RX_CHECKS(honda_nidec_alt_interceptor_rx_checks, ret) : \
Expand Down Expand Up @@ -465,8 +465,8 @@ static int honda_bosch_fwd_hook(int bus_num, int addr) {
bus_fwd = 2;
}
if (bus_num == 2) {
int is_lkas_msg = (addr == 0xE4) || (addr == 0xE5) || (addr == 0x33D) || (addr == 0x33DA) || (addr == 0x33DB);
int is_acc_msg = ((addr == 0x1C8) || (addr == 0x30C)) && honda_bosch_radarless && honda_bosch_long;
bool is_lkas_msg = (addr == 0xE4) || (addr == 0xE5) || (addr == 0x33D) || (addr == 0x33DA) || (addr == 0x33DB);
bool is_acc_msg = ((addr == 0x1C8) || (addr == 0x30C)) && honda_bosch_radarless && honda_bosch_long;
bool block_msg = is_lkas_msg || is_acc_msg;
if (!block_msg) {
bus_fwd = 0;
Expand Down
14 changes: 7 additions & 7 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static void hyundai_rx_hook(const CANPacket_t *to_push) {
// ACC steering wheel buttons
if (addr == 0x4F1) {
int cruise_button = GET_BYTE(to_push, 0) & 0x7U;
int main_button = GET_BIT(to_push, 3U);
bool main_button = GET_BIT(to_push, 3U);
hyundai_common_cruise_buttons_check(cruise_button, main_button);
}

Expand Down Expand Up @@ -221,10 +221,10 @@ static bool hyundai_tx_hook(const CANPacket_t *to_send) {
// FCA11: Block any potential actuation
if (addr == 0x38D) {
int CR_VSM_DecCmd = GET_BYTE(to_send, 1);
int FCA_CmdAct = GET_BIT(to_send, 20U);
int CF_VSM_DecCmdAct = GET_BIT(to_send, 31U);
bool FCA_CmdAct = GET_BIT(to_send, 20U);
bool CF_VSM_DecCmdAct = GET_BIT(to_send, 31U);

if ((CR_VSM_DecCmd != 0) || (FCA_CmdAct != 0) || (CF_VSM_DecCmdAct != 0)) {
if ((CR_VSM_DecCmd != 0) || FCA_CmdAct || CF_VSM_DecCmdAct) {
tx = false;
}
}
Expand All @@ -235,14 +235,14 @@ static bool hyundai_tx_hook(const CANPacket_t *to_send) {
int desired_accel_val = ((GET_BYTE(to_send, 5) << 3) | (GET_BYTE(to_send, 4) >> 5)) - 1023U;

int aeb_decel_cmd = GET_BYTE(to_send, 2);
int aeb_req = GET_BIT(to_send, 54U);
bool aeb_req = GET_BIT(to_send, 54U);

bool violation = false;

violation |= longitudinal_accel_checks(desired_accel_raw, HYUNDAI_LONG_LIMITS);
violation |= longitudinal_accel_checks(desired_accel_val, HYUNDAI_LONG_LIMITS);
violation |= (aeb_decel_cmd != 0);
violation |= (aeb_req != 0);
violation |= aeb_req;

if (violation) {
tx = false;
Expand All @@ -252,7 +252,7 @@ static bool hyundai_tx_hook(const CANPacket_t *to_send) {
// LKA STEER: safety check
if (addr == 0x340) {
int desired_torque = ((GET_BYTES(to_send, 0, 4) >> 16) & 0x7ffU) - 1024U;
bool steer_req = GET_BIT(to_send, 27U) != 0U;
bool steer_req = GET_BIT(to_send, 27U);

const SteeringLimits limits = hyundai_alt_limits ? HYUNDAI_STEERING_LIMITS_ALT : HYUNDAI_STEERING_LIMITS;
if (steer_torque_cmd_checks(desired_torque, steer_req, limits)) {
Expand Down
10 changes: 5 additions & 5 deletions board/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static void hyundai_canfd_rx_hook(const CANPacket_t *to_push) {
// cruise buttons
const int button_addr = hyundai_canfd_alt_buttons ? 0x1aa : 0x1cf;
if (addr == button_addr) {
int main_button = 0;
bool main_button = false;
int cruise_button = 0;
if (addr == 0x1cf) {
cruise_button = GET_BYTE(to_push, 2) & 0x7U;
Expand All @@ -186,15 +186,15 @@ static void hyundai_canfd_rx_hook(const CANPacket_t *to_push) {
if ((addr == 0x35) && hyundai_ev_gas_signal) {
gas_pressed = GET_BYTE(to_push, 5) != 0U;
} else if ((addr == 0x105) && hyundai_hybrid_gas_signal) {
gas_pressed = (GET_BIT(to_push, 103U) != 0U) || (GET_BYTE(to_push, 13) != 0U) || (GET_BIT(to_push, 112U) != 0U);
gas_pressed = GET_BIT(to_push, 103U) || (GET_BYTE(to_push, 13) != 0U) || GET_BIT(to_push, 112U);
} else if ((addr == 0x100) && !hyundai_ev_gas_signal && !hyundai_hybrid_gas_signal) {
gas_pressed = GET_BIT(to_push, 176U) != 0U;
gas_pressed = GET_BIT(to_push, 176U);
} else {
}

// brake press
if (addr == 0x175) {
brake_pressed = GET_BIT(to_push, 81U) != 0U;
brake_pressed = GET_BIT(to_push, 81U);
}

// vehicle moving
Expand Down Expand Up @@ -235,7 +235,7 @@ static bool hyundai_canfd_tx_hook(const CANPacket_t *to_send) {
const int steer_addr = (hyundai_canfd_hda2 && !hyundai_longitudinal) ? hyundai_canfd_hda2_get_lkas_addr() : 0x12a;
if (addr == steer_addr) {
int desired_torque = (((GET_BYTE(to_send, 6) & 0xFU) << 7U) | (GET_BYTE(to_send, 5) >> 1U)) - 1024U;
bool steer_req = GET_BIT(to_send, 52U) != 0U;
bool steer_req = GET_BIT(to_send, 52U);

if (steer_torque_cmd_checks(desired_torque, steer_req, HYUNDAI_CANFD_STEERING_LIMITS)) {
tx = false;
Expand Down
7 changes: 3 additions & 4 deletions board/safety/safety_hyundai_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void hyundai_common_init(uint16_t param) {
#endif
}

void hyundai_common_cruise_state_check(const int cruise_engaged) {
void hyundai_common_cruise_state_check(const bool cruise_engaged) {
// some newer HKG models can re-enable after spamming cancel button,
// so keep track of user button presses to deny engagement if no interaction

Expand All @@ -62,9 +62,8 @@ void hyundai_common_cruise_state_check(const int cruise_engaged) {
}
}

void hyundai_common_cruise_buttons_check(const int cruise_button, const int main_button) {
if ((cruise_button == HYUNDAI_BTN_RESUME) || (cruise_button == HYUNDAI_BTN_SET) || (cruise_button == HYUNDAI_BTN_CANCEL) ||
(main_button != 0)) {
void hyundai_common_cruise_buttons_check(const int cruise_button, const bool main_button) {
if ((cruise_button == HYUNDAI_BTN_RESUME) || (cruise_button == HYUNDAI_BTN_SET) || (cruise_button == HYUNDAI_BTN_CANCEL) || main_button) {
hyundai_last_button_interaction = 0U;
} else {
hyundai_last_button_interaction = MIN(hyundai_last_button_interaction + 1U, HYUNDAI_PREV_BUTTON_SAMPLES);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_nissan.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ static int nissan_fwd_hook(int bus_num, int addr) {
int bus_fwd = -1;

if (bus_num == 0) {
int block_msg = (addr == 0x280); // CANCEL_MSG
bool block_msg = (addr == 0x280); // CANCEL_MSG
if (!block_msg) {
bus_fwd = 2; // ADAS
}
}

if (bus_num == 2) {
// 0x169 is LKAS, 0x2b1 LKAS_HUD, 0x4cc LKAS_HUD_INFO_MSG
int block_msg = ((addr == 0x169) || (addr == 0x2b1) || (addr == 0x4cc));
bool block_msg = ((addr == 0x169) || (addr == 0x2b1) || (addr == 0x4cc));
if (!block_msg) {
bus_fwd = 0; // V-CAN
}
Expand Down
8 changes: 4 additions & 4 deletions board/safety/safety_subaru.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static void subaru_rx_hook(const CANPacket_t *to_push) {

// enter controls on rising edge of ACC, exit controls on ACC off
if ((addr == MSG_SUBARU_CruiseControl) && (bus == alt_main_bus)) {
bool cruise_engaged = GET_BIT(to_push, 41U) != 0U;
bool cruise_engaged = GET_BIT(to_push, 41U);
pcm_cruise_check(cruise_engaged);
}

Expand All @@ -169,7 +169,7 @@ static void subaru_rx_hook(const CANPacket_t *to_push) {
}

if ((addr == MSG_SUBARU_Brake_Status) && (bus == alt_main_bus)) {
brake_pressed = GET_BIT(to_push, 62U) != 0U;
brake_pressed = GET_BIT(to_push, 62U);
}

if ((addr == MSG_SUBARU_Throttle) && (bus == SUBARU_MAIN_BUS)) {
Expand All @@ -189,7 +189,7 @@ static bool subaru_tx_hook(const CANPacket_t *to_send) {
int desired_torque = ((GET_BYTES(to_send, 0, 4) >> 16) & 0x1FFFU);
desired_torque = -1 * to_signed(desired_torque, 13);

bool steer_req = GET_BIT(to_send, 29U) != 0U;
bool steer_req = GET_BIT(to_send, 29U);

const SteeringLimits limits = subaru_gen2 ? SUBARU_GEN2_STEERING_LIMITS : SUBARU_STEERING_LIMITS;
violation |= steer_torque_cmd_checks(desired_torque, steer_req, limits);
Expand All @@ -204,7 +204,7 @@ static bool subaru_tx_hook(const CANPacket_t *to_send) {
// check es_distance cruise_throttle limits
if (addr == MSG_SUBARU_ES_Distance) {
int cruise_throttle = (GET_BYTES(to_send, 2, 2) & 0x1FFFU);
bool cruise_cancel = GET_BIT(to_send, 56U) != 0U;
bool cruise_cancel = GET_BIT(to_send, 56U);

if (subaru_longitudinal) {
violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS);
Expand Down
Loading

0 comments on commit ed4966d

Please sign in to comment.