Skip to content

Commit

Permalink
enable misra-c2012-8.7 + misra-c2012-8.9- objects reduce scope
Browse files Browse the repository at this point in the history
If objects are referenced in one function, reduce its scope instead of making static - to avoid misra 8.9
  • Loading branch information
dzid26 committed May 22, 2024
1 parent 22974d3 commit 96368e3
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 162 deletions.
4 changes: 2 additions & 2 deletions board/drivers/fan.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ struct fan_state_t {
} fan_state_t;
struct fan_state_t fan_state;

const float FAN_I = 0.001f;

const uint8_t FAN_TICK_FREQ = 8U;
const uint8_t FAN_STALL_THRESHOLD_MIN = 3U;
const uint8_t FAN_STALL_THRESHOLD_MAX = 8U;
Expand All @@ -31,6 +29,8 @@ void fan_init(void) {

// Call this at FAN_TICK_FREQ
void fan_tick(void) {
const float FAN_I = 0.001f;

if (current_board->fan_max_rpm > 0U) {
// Measure fan RPM
uint16_t fan_rpm_fast = fan_state.tach_counter * (60U * FAN_TICK_FREQ / 4U); // 4 interrupts per rotation
Expand Down
4 changes: 2 additions & 2 deletions board/provision.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#define PROVISION_CHUNK_LEN 0x20

const char unprovisioned_text[] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";

void get_provision_chunk(uint8_t *resp) {
const char unprovisioned_text[] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";

(void)memcpy(resp, (uint8_t *)PROVISION_CHUNK_ADDRESS, PROVISION_CHUNK_LEN);
if (memcmp(resp, unprovisioned_text, 0x20) == 0) {
(void)memcpy(resp, "unprovisioned\x00\x00\x00testing123\x00\x00\xa3\xa6\x99\xec", 0x20);
Expand Down
60 changes: 30 additions & 30 deletions board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
const SteeringLimits CHRYSLER_STEERING_LIMITS = {
.max_steer = 261,
.max_rt_delta = 112,
.max_rt_interval = 250000,
.max_rate_up = 3,
.max_rate_down = 3,
.max_torque_error = 80,
.type = TorqueMotorLimited,
};

const SteeringLimits CHRYSLER_RAM_DT_STEERING_LIMITS = {
.max_steer = 350,
.max_rt_delta = 112,
.max_rt_interval = 250000,
.max_rate_up = 6,
.max_rate_down = 6,
.max_torque_error = 80,
.type = TorqueMotorLimited,
};

const SteeringLimits CHRYSLER_RAM_HD_STEERING_LIMITS = {
.max_steer = 361,
.max_rt_delta = 182,
.max_rt_interval = 250000,
.max_rate_up = 14,
.max_rate_down = 14,
.max_torque_error = 80,
.type = TorqueMotorLimited,
};

typedef struct {
const int EPS_2;
const int ESP_1;
Expand Down Expand Up @@ -215,6 +185,36 @@ static void chrysler_rx_hook(const CANPacket_t *to_push) {
}

static bool chrysler_tx_hook(const CANPacket_t *to_send) {
const SteeringLimits CHRYSLER_STEERING_LIMITS = {
.max_steer = 261,
.max_rt_delta = 112,
.max_rt_interval = 250000,
.max_rate_up = 3,
.max_rate_down = 3,
.max_torque_error = 80,
.type = TorqueMotorLimited,
};

const SteeringLimits CHRYSLER_RAM_DT_STEERING_LIMITS = {
.max_steer = 350,
.max_rt_delta = 112,
.max_rt_interval = 250000,
.max_rate_up = 6,
.max_rate_down = 6,
.max_torque_error = 80,
.type = TorqueMotorLimited,
};

const SteeringLimits CHRYSLER_RAM_HD_STEERING_LIMITS = {
.max_steer = 361,
.max_rt_delta = 182,
.max_rt_interval = 250000,
.max_rate_up = 14,
.max_rate_down = 14,
.max_torque_error = 80,
.type = TorqueMotorLimited,
};

bool tx = true;
int addr = GET_ADDR(to_send);

Expand Down
50 changes: 25 additions & 25 deletions board/safety/safety_gm.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
const SteeringLimits GM_STEERING_LIMITS = {
.max_steer = 300,
.max_rate_up = 10,
.max_rate_down = 15,
.driver_torque_allowance = 65,
.driver_torque_factor = 4,
.max_rt_delta = 128,
.max_rt_interval = 250000,
.type = TorqueDriverLimited,
};

const LongitudinalLimits GM_ASCM_LONG_LIMITS = {
.max_gas = 3072,
.min_gas = 1404,
.inactive_gas = 1404,
.max_brake = 400,
};

const LongitudinalLimits GM_CAM_LONG_LIMITS = {
.max_gas = 3400,
.min_gas = 1514,
.inactive_gas = 1554,
.max_brake = 400,
};

const LongitudinalLimits *gm_long_limits;

const int GM_STANDSTILL_THRSLD = 10; // 0.311kph
Expand Down Expand Up @@ -139,6 +114,17 @@ static void gm_rx_hook(const CANPacket_t *to_push) {
}

static bool gm_tx_hook(const CANPacket_t *to_send) {
const SteeringLimits GM_STEERING_LIMITS = {
.max_steer = 300,
.max_rate_up = 10,
.max_rate_down = 15,
.driver_torque_allowance = 65,
.driver_torque_factor = 4,
.max_rt_delta = 128,
.max_rt_interval = 250000,
.type = TorqueDriverLimited,
};

bool tx = true;
int addr = GET_ADDR(to_send);

Expand Down Expand Up @@ -218,6 +204,20 @@ static int gm_fwd_hook(int bus_num, int addr) {
}

static safety_config gm_init(uint16_t param) {
const LongitudinalLimits GM_ASCM_LONG_LIMITS = {
.max_gas = 3072,
.min_gas = 1404,
.inactive_gas = 1404,
.max_brake = 400,
};

const LongitudinalLimits GM_CAM_LONG_LIMITS = {
.max_gas = 3400,
.min_gas = 1514,
.inactive_gas = 1554,
.max_brake = 400,
};

gm_hw = GET_FLAG(param, GM_PARAM_HW_CAM) ? GM_CAM : GM_ASCM;

if (gm_hw == GM_ASCM) {
Expand Down
6 changes: 3 additions & 3 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
.has_steer_req_tolerance = true, \
}

const SteeringLimits HYUNDAI_STEERING_LIMITS = HYUNDAI_LIMITS(384, 3, 7);
const SteeringLimits HYUNDAI_STEERING_LIMITS_ALT = HYUNDAI_LIMITS(270, 2, 3);

const LongitudinalLimits HYUNDAI_LONG_LIMITS = {
.max_accel = 200, // 1/100 m/s2
.min_accel = -350, // 1/100 m/s2
Expand Down Expand Up @@ -215,6 +212,9 @@ static void hyundai_rx_hook(const CANPacket_t *to_push) {
}

static bool hyundai_tx_hook(const CANPacket_t *to_send) {
const SteeringLimits HYUNDAI_STEERING_LIMITS = HYUNDAI_LIMITS(384, 3, 7);
const SteeringLimits HYUNDAI_STEERING_LIMITS_ALT = HYUNDAI_LIMITS(270, 2, 3);

bool tx = true;
int addr = GET_ADDR(to_send);

Expand Down
36 changes: 18 additions & 18 deletions board/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
#include "safety_hyundai_common.h"

const SteeringLimits HYUNDAI_CANFD_STEERING_LIMITS = {
.max_steer = 270,
.max_rt_delta = 112,
.max_rt_interval = 250000,
.max_rate_up = 2,
.max_rate_down = 3,
.driver_torque_allowance = 250,
.driver_torque_factor = 2,
.type = TorqueDriverLimited,

// the EPS faults when the steering angle is above a certain threshold for too long. to prevent this,
// we allow setting torque actuation bit to 0 while maintaining the requested torque value for two consecutive frames
.min_valid_request_frames = 89,
.max_invalid_request_frames = 2,
.min_valid_request_rt_interval = 810000, // 810ms; a ~10% buffer on cutting every 90 frames
.has_steer_req_tolerance = true,
};

const CanMsg HYUNDAI_CANFD_HDA2_TX_MSGS[] = {
{0x50, 0, 16}, // LKAS
{0x1CF, 1, 8}, // CRUISE_BUTTON
Expand Down Expand Up @@ -228,6 +210,24 @@ static void hyundai_canfd_rx_hook(const CANPacket_t *to_push) {
}

static bool hyundai_canfd_tx_hook(const CANPacket_t *to_send) {
const SteeringLimits HYUNDAI_CANFD_STEERING_LIMITS = {
.max_steer = 270,
.max_rt_delta = 112,
.max_rt_interval = 250000,
.max_rate_up = 2,
.max_rate_down = 3,
.driver_torque_allowance = 250,
.driver_torque_factor = 2,
.type = TorqueDriverLimited,

// the EPS faults when the steering angle is above a certain threshold for too long. to prevent this,
// we allow setting torque actuation bit to 0 while maintaining the requested torque value for two consecutive frames
.min_valid_request_frames = 89,
.max_invalid_request_frames = 2,
.min_valid_request_rt_interval = 810000, // 810ms; a ~10% buffer on cutting every 90 frames
.has_steer_req_tolerance = true,
};

bool tx = true;
int addr = GET_ADDR(to_send);

Expand Down
22 changes: 11 additions & 11 deletions board/safety/safety_mazda.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@
#define MAZDA_AUX 1
#define MAZDA_CAM 2

const SteeringLimits MAZDA_STEERING_LIMITS = {
.max_steer = 800,
.max_rate_up = 10,
.max_rate_down = 25,
.max_rt_delta = 300,
.max_rt_interval = 250000,
.driver_torque_factor = 1,
.driver_torque_allowance = 15,
.type = TorqueDriverLimited,
};

const CanMsg MAZDA_TX_MSGS[] = {{MAZDA_LKAS, 0, 8}, {MAZDA_CRZ_BTNS, 0, 8}, {MAZDA_LKAS_HUD, 0, 8}};

RxCheck mazda_rx_checks[] = {
Expand Down Expand Up @@ -69,6 +58,17 @@ static void mazda_rx_hook(const CANPacket_t *to_push) {
}

static bool mazda_tx_hook(const CANPacket_t *to_send) {
const SteeringLimits MAZDA_STEERING_LIMITS = {
.max_steer = 800,
.max_rate_up = 10,
.max_rate_down = 25,
.max_rt_delta = 300,
.max_rt_interval = 250000,
.driver_torque_factor = 1,
.driver_torque_allowance = 15,
.type = TorqueDriverLimited,
};

bool tx = true;
int addr = GET_ADDR(to_send);
int bus = GET_BUS(to_send);
Expand Down
8 changes: 3 additions & 5 deletions board/safety/safety_subaru.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
.has_steer_req_tolerance = true, \
}


const SteeringLimits SUBARU_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(2047, 50, 70);
const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(1000, 40, 40);


const LongitudinalLimits SUBARU_LONG_LIMITS = {
.min_gas = 808, // appears to be engine braking
.max_gas = 3400, // approx 2 m/s^2 when maxing cruise_rpm and cruise_throttle
Expand Down Expand Up @@ -180,6 +175,9 @@ static void subaru_rx_hook(const CANPacket_t *to_push) {
}

static bool subaru_tx_hook(const CANPacket_t *to_send) {
const SteeringLimits SUBARU_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(2047, 50, 70);
const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(1000, 40, 40);

bool tx = true;
int addr = GET_ADDR(to_send);
bool violation = false;
Expand Down
22 changes: 11 additions & 11 deletions board/safety/safety_subaru_preglobal.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
const SteeringLimits SUBARU_PG_STEERING_LIMITS = {
.max_steer = 2047,
.max_rt_delta = 940,
.max_rt_interval = 250000,
.max_rate_up = 50,
.max_rate_down = 70,
.driver_torque_factor = 10,
.driver_torque_allowance = 75,
.type = TorqueDriverLimited,
};

// Preglobal platform
// 0x161 is ES_CruiseThrottle
// 0x164 is ES_LKAS
Expand Down Expand Up @@ -78,6 +67,17 @@ static void subaru_preglobal_rx_hook(const CANPacket_t *to_push) {
}

static bool subaru_preglobal_tx_hook(const CANPacket_t *to_send) {
const SteeringLimits SUBARU_PG_STEERING_LIMITS = {
.max_steer = 2047,
.max_rt_delta = 940,
.max_rt_interval = 250000,
.max_rate_up = 50,
.max_rate_down = 70,
.driver_torque_factor = 10,
.driver_torque_allowance = 75,
.type = TorqueDriverLimited,
};

bool tx = true;
int addr = GET_ADDR(to_send);

Expand Down
24 changes: 12 additions & 12 deletions board/safety/safety_tesla.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
const SteeringLimits TESLA_STEERING_LIMITS = {
.angle_deg_to_can = 10,
.angle_rate_up_lookup = {
{0., 5., 15.},
{10., 1.6, .3}
},
.angle_rate_down_lookup = {
{0., 5., 15.},
{10., 7.0, .8}
},
};

const LongitudinalLimits TESLA_LONG_LIMITS = {
.max_accel = 425, // 2. m/s^2
.min_accel = 287, // -3.52 m/s^2 // TODO: limit to -3.48
Expand Down Expand Up @@ -129,6 +117,18 @@ static void tesla_rx_hook(const CANPacket_t *to_push) {


static bool tesla_tx_hook(const CANPacket_t *to_send) {
const SteeringLimits TESLA_STEERING_LIMITS = {
.angle_deg_to_can = 10,
.angle_rate_up_lookup = {
{0., 5., 15.},
{10., 1.6, .3}
},
.angle_rate_down_lookup = {
{0., 5., 15.},
{10., 7.0, .8}
},
};

bool tx = true;
int addr = GET_ADDR(to_send);
bool violation = false;
Expand Down
8 changes: 5 additions & 3 deletions board/safety/safety_toyota.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const SteeringLimits TOYOTA_STEERING_LIMITS = {
};

const int TOYOTA_LTA_MAX_ANGLE = 1657; // EPS only accepts up to 94.9461
const int TOYOTA_LTA_MAX_MEAS_TORQUE = 1500;
const int TOYOTA_LTA_MAX_DRIVER_TORQUE = 150;

// longitudinal limits
const LongitudinalLimits TOYOTA_LONG_LIMITS = {
Expand Down Expand Up @@ -75,7 +73,6 @@ RxCheck toyota_lta_rx_checks[] = {
// safety param flags
// first byte is for EPS factor, second is for flags
const uint32_t TOYOTA_PARAM_OFFSET = 8U;
const uint32_t TOYOTA_EPS_FACTOR = (1UL << TOYOTA_PARAM_OFFSET) - 1U;
const uint32_t TOYOTA_PARAM_ALT_BRAKE = 1UL << TOYOTA_PARAM_OFFSET;
const uint32_t TOYOTA_PARAM_STOCK_LONGITUDINAL = 2UL << TOYOTA_PARAM_OFFSET;
const uint32_t TOYOTA_PARAM_LTA = 4UL << TOYOTA_PARAM_OFFSET;
Expand Down Expand Up @@ -184,6 +181,9 @@ static void toyota_rx_hook(const CANPacket_t *to_push) {
}

static bool toyota_tx_hook(const CANPacket_t *to_send) {
const int TOYOTA_LTA_MAX_DRIVER_TORQUE = 150;
const int TOYOTA_LTA_MAX_MEAS_TORQUE = 1500;

bool tx = true;
int addr = GET_ADDR(to_send);
int bus = GET_BUS(to_send);
Expand Down Expand Up @@ -302,6 +302,8 @@ static bool toyota_tx_hook(const CANPacket_t *to_send) {
}

static safety_config toyota_init(uint16_t param) {
const uint32_t TOYOTA_EPS_FACTOR = (1UL << TOYOTA_PARAM_OFFSET) - 1U;

toyota_alt_brake = GET_FLAG(param, TOYOTA_PARAM_ALT_BRAKE);
toyota_stock_longitudinal = GET_FLAG(param, TOYOTA_PARAM_STOCK_LONGITUDINAL);
toyota_lta = GET_FLAG(param, TOYOTA_PARAM_LTA);
Expand Down
Loading

0 comments on commit 96368e3

Please sign in to comment.