diff --git a/Dockerfile b/Dockerfile index 816a9b80e38..5fee66ac705 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,7 +37,7 @@ RUN pip3 install --break-system-packages --no-cache-dir $PYTHONPATH/panda/[dev] # TODO: this should be a "pip install" or not even in this repo at all RUN git config --global --add safe.directory $PYTHONPATH/panda -ENV OPENDBC_REF="da0a5e3d2b3984b56ebf5e25d9769f5c77807e4d" +ENV OPENDBC_REF="d631299b8d49253fdeb5b709684af031ab1d18be" RUN cd /tmp/ && \ git clone --depth 1 https://github.com/commaai/opendbc opendbc_repo && \ cd opendbc_repo && git fetch origin $OPENDBC_REF && git checkout FETCH_HEAD && rm -rf .git/ && \ diff --git a/SConscript b/SConscript index f617410b434..8f9544527d5 100644 --- a/SConscript +++ b/SConscript @@ -84,7 +84,9 @@ def build_project(project_name, project, extra_flags): '..', panda_root, f"{panda_root}/board/", + f"{panda_root}/../opendbc/", f"{panda_root}/../opendbc/safety/", + f"{panda_root}/../opendbc/safety/board/", ] env = Environment( diff --git a/board/can.h b/board/can.h index 9b1200384de..e2da275ed8b 100644 --- a/board/can.h +++ b/board/can.h @@ -1,7 +1,7 @@ #pragma once -#include "can_declarations.h" + +// bump this when changing the CAN packet +#define CAN_PACKET_VERSION 4 static const uint8_t PANDA_CAN_CNT = 3U; static const uint8_t PANDA_BUS_CNT = 3U; - -static const unsigned char dlc_to_len[] = {0U, 1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U, 12U, 16U, 20U, 24U, 32U, 48U, 64U}; diff --git a/board/can_declarations.h b/board/can_declarations.h deleted file mode 100644 index 878d9c77e2e..00000000000 --- a/board/can_declarations.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -// bump this when changing the CAN packet -#define CAN_PACKET_VERSION 4 - -#define CANPACKET_HEAD_SIZE 6U - -#if !defined(STM32F4) - #define CANFD - #define CANPACKET_DATA_SIZE_MAX 64U -#else - #define CANPACKET_DATA_SIZE_MAX 8U -#endif - -typedef struct { - unsigned char fd : 1; - unsigned char bus : 3; - unsigned char data_len_code : 4; // lookup length with dlc_to_len - unsigned char rejected : 1; - unsigned char returned : 1; - unsigned char extended : 1; - unsigned int addr : 29; - unsigned char checksum; - unsigned char data[CANPACKET_DATA_SIZE_MAX]; -} __attribute__((packed, aligned(4))) CANPacket_t; - -#define GET_BUS(msg) ((msg)->bus) -#define GET_LEN(msg) (dlc_to_len[(msg)->data_len_code]) -#define GET_ADDR(msg) ((msg)->addr) diff --git a/board/drivers/can_common.h b/board/drivers/can_common.h index fa69c698180..ecffaf5ec62 100644 --- a/board/drivers/can_common.h +++ b/board/drivers/can_common.h @@ -1,4 +1,7 @@ #include "can_common_declarations.h" +#include "can.h" +#include "safety/board/drivers/can_common.h" +#include "safety/safety_declarations.h" uint32_t safety_tx_blocked = 0; uint32_t safety_rx_invalid = 0; @@ -218,23 +221,6 @@ bool can_tx_check_min_slots_free(uint32_t min) { (can_slots_empty(&can_tx3_q) >= min); } -uint8_t calculate_checksum(const uint8_t *dat, uint32_t len) { - uint8_t checksum = 0U; - for (uint32_t i = 0U; i < len; i++) { - checksum ^= dat[i]; - } - return checksum; -} - -void can_set_checksum(CANPacket_t *packet) { - packet->checksum = 0U; - packet->checksum = calculate_checksum((uint8_t *) packet, CANPACKET_HEAD_SIZE + GET_LEN(packet)); -} - -bool can_check_checksum(CANPacket_t *packet) { - return (calculate_checksum((uint8_t *) packet, CANPACKET_HEAD_SIZE + GET_LEN(packet)) == 0U); -} - void can_send(CANPacket_t *to_push, uint8_t bus_number, bool skip_tx_hook) { if (skip_tx_hook || safety_tx_hook(to_push) != 0) { if (bus_number < PANDA_BUS_CNT) { diff --git a/board/drivers/registers.h b/board/drivers/registers.h index ce4be14a92a..12744d717a5 100644 --- a/board/drivers/registers.h +++ b/board/drivers/registers.h @@ -1,4 +1,5 @@ #include "registers_declarations.h" +#include "safety/board/faults.h" static reg register_map[REGISTER_MAP_SIZE]; diff --git a/board/fake_stm.h b/board/fake_stm.h index 984fde14f1f..bfcfe83849b 100644 --- a/board/fake_stm.h +++ b/board/fake_stm.h @@ -3,7 +3,7 @@ #include #include -#include "utils.h" +#include "safety/board/utils.h" #define CANFD #define ALLOW_DEBUG diff --git a/board/faults.h b/board/faults.h deleted file mode 100644 index 0fc9d2c5cfb..00000000000 --- a/board/faults.h +++ /dev/null @@ -1,25 +0,0 @@ -#include "faults_declarations.h" - -uint8_t fault_status = FAULT_STATUS_NONE; -uint32_t faults = 0U; - -void fault_occurred(uint32_t fault) { - if ((faults & fault) == 0U) { - if ((PERMANENT_FAULTS & fault) != 0U) { - print("Permanent fault occurred: 0x"); puth(fault); print("\n"); - fault_status = FAULT_STATUS_PERMANENT; - } else { - print("Temporary fault occurred: 0x"); puth(fault); print("\n"); - fault_status = FAULT_STATUS_TEMPORARY; - } - } - faults |= fault; -} - -void fault_recovered(uint32_t fault) { - if ((PERMANENT_FAULTS & fault) == 0U) { - faults &= ~fault; - } else { - print("Cannot recover from a permanent fault!\n"); - } -} diff --git a/board/faults_declarations.h b/board/faults_declarations.h index eab9fcc08bb..b8570b03ea0 100644 --- a/board/faults_declarations.h +++ b/board/faults_declarations.h @@ -1,4 +1,5 @@ #pragma once +#include "safety/board/faults_declarations.h" #define FAULT_STATUS_NONE 0U #define FAULT_STATUS_TEMPORARY 1U @@ -36,9 +37,3 @@ // Permanent faults #define PERMANENT_FAULTS 0U - -extern uint8_t fault_status; -extern uint32_t faults; - -void fault_occurred(uint32_t fault); -void fault_recovered(uint32_t fault); diff --git a/board/jungle/main.c b/board/jungle/main.c index 1c73edd7ce9..b592b32755f 100644 --- a/board/jungle/main.c +++ b/board/jungle/main.c @@ -1,8 +1,7 @@ // ********************* Includes ********************* #include "board/config.h" -#include "safety.h" - +#include "safety/safety.h" #include "board/drivers/led.h" #include "board/drivers/pwm.h" #include "board/drivers/usb.h" diff --git a/board/main.c b/board/main.c index 9770f8368f8..183b0a63417 100644 --- a/board/main.c +++ b/board/main.c @@ -10,8 +10,7 @@ #include "early_init.h" #include "provision.h" -#include "safety.h" - +#include "safety/safety.h" #include "health.h" #include "drivers/can_common.h" diff --git a/board/stm32f4/stm32f4_config.h b/board/stm32f4/stm32f4_config.h index b3fc2a8907e..1b71add9ad0 100644 --- a/board/stm32f4/stm32f4_config.h +++ b/board/stm32f4/stm32f4_config.h @@ -30,7 +30,6 @@ #define PROVISION_CHUNK_ADDRESS 0x1FFF79E0U #define DEVICE_SERIAL_NUMBER_ADDRESS 0x1FFF79C0U -#include "can.h" #include "comms_definitions.h" #ifndef BOOTSTUB @@ -41,8 +40,8 @@ #include "libc.h" #include "critical.h" -#include "faults.h" -#include "utils.h" +#include "faults_declarations.h" +#include "safety/board/utils.h" #include "drivers/registers.h" #include "drivers/interrupts.h" diff --git a/board/stm32h7/stm32h7_config.h b/board/stm32h7/stm32h7_config.h index 4355fe8e089..cd3d73eadb8 100644 --- a/board/stm32h7/stm32h7_config.h +++ b/board/stm32h7/stm32h7_config.h @@ -43,7 +43,6 @@ separate IRQs for RX and TX. #define PROVISION_CHUNK_ADDRESS 0x080FFFE0U #define DEVICE_SERIAL_NUMBER_ADDRESS 0x080FFFC0U -#include "can.h" #include "comms_definitions.h" #ifndef BOOTSTUB @@ -54,8 +53,8 @@ separate IRQs for RX and TX. #include "libc.h" #include "critical.h" -#include "faults.h" -#include "utils.h" +#include "faults_declarations.h" +#include "safety/board/utils.h" #include "drivers/registers.h" #include "drivers/interrupts.h" diff --git a/board/utils.h b/board/utils.h deleted file mode 100644 index f355ce8c2f2..00000000000 --- a/board/utils.h +++ /dev/null @@ -1,47 +0,0 @@ -// cppcheck-suppress-macro misra-c2012-1.2; allow __typeof__ extension -#define MIN(a, b) ({ \ - __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - (_a < _b) ? _a : _b; \ -}) - -// cppcheck-suppress-macro misra-c2012-1.2; allow __typeof__ extension -#define MAX(a, b) ({ \ - __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - (_a > _b) ? _a : _b; \ -}) - -// cppcheck-suppress-macro misra-c2012-1.2; allow __typeof__ extension -#define CLAMP(x, low, high) ({ \ - __typeof__(x) __x = (x); \ - __typeof__(low) __low = (low);\ - __typeof__(high) __high = (high);\ - (__x > __high) ? __high : ((__x < __low) ? __low : __x); \ -}) - -// cppcheck-suppress-macro misra-c2012-1.2; allow __typeof__ extension -#define ABS(a) ({ \ - __typeof__ (a) _a = (a); \ - (_a > 0) ? _a : (-_a); \ -}) - -#ifndef NULL -// this just provides a standard implementation of NULL -// in lieu of including libc in the panda build -// cppcheck-suppress [misra-c2012-21.1] -#define NULL ((void*)0) -#endif - -// STM32 HAL defines this -#ifndef UNUSED -#define UNUSED(x) ((void)(x)) -#endif - -#define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - (2 * (!(pred) ? 1 : 0))])) - -// compute the time elapsed (in microseconds) from 2 counter samples -// case where ts < ts_last is ok: overflow is properly re-casted into uint32_t -uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last) { - return ts - ts_last; -} diff --git a/tests/libpanda/SConscript b/tests/libpanda/SConscript index a690ba75776..bbfc840cefc 100644 --- a/tests/libpanda/SConscript +++ b/tests/libpanda/SConscript @@ -16,7 +16,13 @@ env = Environment( '-Wfatal-errors', '-Wno-pointer-to-int-cast', ], - CPPPATH=[".", "../../board/", "../../../opendbc/safety/"], + CPPPATH=[ + ".", + "../../board/", + "../../../opendbc/", + "../../../opendbc/safety/", + "../../../opendbc/safety/board/", + ], ) if system == "Darwin": env.PrependENVPath('PATH', '/opt/homebrew/bin') diff --git a/tests/libpanda/panda.c b/tests/libpanda/panda.c index 303d72e7d00..5e4df76f0ac 100644 --- a/tests/libpanda/panda.c +++ b/tests/libpanda/panda.c @@ -12,10 +12,11 @@ void can_tx_comms_resume_usb(void) { }; void can_tx_comms_resume_spi(void) { }; #include "health.h" -#include "faults.h" +#include "faults_declarations.h" #include "libc.h" #include "boards/board_declarations.h" -#include "safety.h" +#include "safety/board/faults.h" +#include "safety/safety.h" #include "main_definitions.h" #include "drivers/can_common.h" diff --git a/tests/misra/test_misra.sh b/tests/misra/test_misra.sh index aaa7b0c365f..3f51181678b 100755 --- a/tests/misra/test_misra.sh +++ b/tests/misra/test_misra.sh @@ -47,7 +47,9 @@ cppcheck() { $CPPCHECK_DIR/cppcheck --inline-suppr -I $PANDA_DIR/board/ \ -I "$(arm-none-eabi-gcc -print-file-name=include)" \ -I $PANDA_DIR/board/stm32f4/inc/ -I $PANDA_DIR/board/stm32h7/inc/ \ + -I $PANDA_DIR/../opendbc/ \ -I $PANDA_DIR/../opendbc/safety/ \ + -I $PANDA_DIR/../opendbc/safety/board/ \ --suppressions-list=$DIR/suppressions.txt --suppress=*:*inc/* \ --suppress=*:*include/* --error-exitcode=2 --check-level=exhaustive --safety \ --platform=arm32-wchar_t4 $COMMON_DEFINES --checkers-report=$CHECKLIST.tmp \