From 27504639f07fe658ba105df66d1e8d91a3c14251 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Wed, 30 Nov 2022 14:15:09 -0800 Subject: [PATCH] cleanup + prep for more tests! (#1172) * panda for pc! * little more * fake panda * move some more stuff * increase timeout * move that * print helpers * cleanup * just move it for now * revert jenkins changes --- board/config.h | 42 +++++++++--------- board/fake_stm.h | 30 +++++++++++++ board/panda.h | 15 ------- board/utils.h | 25 +++++++++++ tests/safety/test.c | 72 +++---------------------------- {board/tests => tests}/test_rsa.c | 0 6 files changed, 80 insertions(+), 104 deletions(-) create mode 100644 board/fake_stm.h delete mode 100644 board/panda.h rename {board/tests => tests}/test_rsa.c (100%) diff --git a/board/config.h b/board/config.h index cc54df25..99835906 100644 --- a/board/config.h +++ b/board/config.h @@ -1,5 +1,6 @@ -#ifndef PANDA_CONFIG_H -#define PANDA_CONFIG_H +#pragma once + +#include //#define DEBUG //#define DEBUG_UART @@ -8,31 +9,28 @@ //#define DEBUG_FAULTS //#define DEBUG_COMMS +#define CAN_INIT_TIMEOUT_MS 500U #define DEEPSLEEP_WAKEUP_DELAY 3U +#define USBPACKET_MAX_SIZE 0x40U +#define MAX_CAN_MSGS_PER_BULK_TRANSFER 51U +#define MAX_EP1_CHUNK_PER_BULK_TRANSFER 16256U // max data stream chunk in bytes, shouldn't be higher than 16320 or counter will overflow -#define NULL ((void*)0) -#define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - (2 * ((int)(!(pred))))])) +// USB definitions +#define USB_VID 0xBBAAU -#define MIN(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - (_a < _b) ? _a : _b; }) +#ifdef BOOTSTUB + #define USB_PID 0xDDEEU +#else + #define USB_PID 0xDDCCU +#endif -#define MAX(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - (_a > _b) ? _a : _b; }) - -#define ABS(a) \ - ({ __typeof__ (a) _a = (a); \ - (_a > 0) ? _a : (-_a); }) - -#include -#include "panda.h" +// platform includes #ifdef STM32H7 #include "stm32h7/stm32h7_config.h" -#else +#elif defined(STM32F2) || defined(STM32F4) #include "stm32fx/stm32fx_config.h" -#endif - +#else + // TODO: uncomment this, cppcheck complains + // building for tests + //#include "fake_stm.h" #endif diff --git a/board/fake_stm.h b/board/fake_stm.h new file mode 100644 index 00000000..d1e14ee9 --- /dev/null +++ b/board/fake_stm.h @@ -0,0 +1,30 @@ +// minimal code to fake a panda for tests +#include +#include +#include + +#include "utils.h" + +#define CANFD +#define ALLOW_DEBUG +#define PANDA + +void print(const char *a) { + printf(a); +} + +void puth(unsigned int i) { + printf("%u", i); +} + +typedef struct { + uint32_t CNT; +} TIM_TypeDef; + +TIM_TypeDef timer; +TIM_TypeDef *MICROSECOND_TIMER = &timer; +uint32_t microsecond_timer_get(void); + +uint32_t microsecond_timer_get(void) { + return MICROSECOND_TIMER->CNT; +} diff --git a/board/panda.h b/board/panda.h deleted file mode 100644 index 77598e79..00000000 --- a/board/panda.h +++ /dev/null @@ -1,15 +0,0 @@ -// USB definitions -#define USB_VID 0xBBAAU - -#ifdef BOOTSTUB - #define USB_PID 0xDDEEU -#else - #define USB_PID 0xDDCCU -#endif - -#define USBPACKET_MAX_SIZE 0x40U - -#define MAX_CAN_MSGS_PER_BULK_TRANSFER 51U -#define MAX_EP1_CHUNK_PER_BULK_TRANSFER 16256U // max data stream chunk in bytes, shouldn't be higher than 16320 or counter will overflow - -#define CAN_INIT_TIMEOUT_MS 500U diff --git a/board/utils.h b/board/utils.h index d767738b..4232e0a9 100644 --- a/board/utils.h +++ b/board/utils.h @@ -1,3 +1,28 @@ +#define MIN(a,b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + (_a < _b) ? _a : _b; }) + +#define MAX(a,b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + (_a > _b) ? _a : _b; }) + +#define ABS(a) \ + ({ __typeof__ (a) _a = (a); \ + (_a > 0) ? _a : (-_a); }) + +#ifndef NULL +#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 * ((int)(!(pred))))])) + // 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) { diff --git a/tests/safety/test.c b/tests/safety/test.c index 16448545..a39716f6 100644 --- a/tests/safety/test.c +++ b/tests/safety/test.c @@ -1,74 +1,12 @@ -#include -#include -#include -#include - -#include "panda.h" +#include "config.h" +#include "fake_stm.h" #include "can_definitions.h" -#include "utils.h" +#include "main_declarations.h" +#include "boards/board_declarations.h" -#define CANFD - -typedef struct { - uint32_t CNT; -} TIM_TypeDef; - -struct sample_t torque_meas; -struct sample_t torque_driver; - -TIM_TypeDef timer; -TIM_TypeDef *MICROSECOND_TIMER = &timer; -uint32_t microsecond_timer_get(void); - -// from board_declarations.h -#define HW_TYPE_UNKNOWN 0U -#define HW_TYPE_WHITE_PANDA 1U -#define HW_TYPE_GREY_PANDA 2U -#define HW_TYPE_BLACK_PANDA 3U -#define HW_TYPE_PEDAL 4U -#define HW_TYPE_UNO 5U -#define HW_TYPE_DOS 6U - -#define ALLOW_DEBUG - -// from main_declarations.h -uint8_t hw_type = HW_TYPE_UNKNOWN; - -// from config.h -#define MIN(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a < _b ? _a : _b; }) - -#define MAX(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a > _b ? _a : _b; }) - -#define ABS(a) \ - ({ __typeof__ (a) _a = (a); \ - (_a > 0) ? _a : (-_a); }) - -// from faults.h -#define FAULT_RELAY_MALFUNCTION (1U << 0) -void fault_occurred(uint32_t fault) { -} -void fault_recovered(uint32_t fault) { -} - -#define UNUSED(x) (void)(x) - -#ifndef PANDA -#define PANDA -#endif -#define NULL ((void*)0) -#define static +#include "faults.h" #include "safety.h" -uint32_t microsecond_timer_get(void) { - return MICROSECOND_TIMER->CNT; -} - void safety_tick_current_rx_checks() { safety_tick(current_rx_checks); } diff --git a/board/tests/test_rsa.c b/tests/test_rsa.c similarity index 100% rename from board/tests/test_rsa.c rename to tests/test_rsa.c