2019-07-23 15:07:06 -07:00
|
|
|
// ********************* Includes *********************
|
2017-06-27 19:27:24 -07:00
|
|
|
#include "config.h"
|
2017-07-20 23:45:48 -07:00
|
|
|
|
2019-10-25 16:22:42 -07:00
|
|
|
#include "drivers/pwm.h"
|
2017-07-20 23:45:48 -07:00
|
|
|
#include "drivers/usb.h"
|
2018-08-02 14:13:57 -04:00
|
|
|
#include "drivers/gmlan_alt.h"
|
2020-07-10 14:18:24 -07:00
|
|
|
#include "drivers/kline_init.h"
|
2017-07-21 11:48:03 -07:00
|
|
|
|
2021-07-02 18:25:35 -07:00
|
|
|
#include "early_init.h"
|
|
|
|
|
#include "provision.h"
|
2019-07-23 15:07:06 -07:00
|
|
|
|
2019-04-01 22:45:00 -07:00
|
|
|
#include "power_saving.h"
|
2019-05-22 08:30:32 -07:00
|
|
|
#include "safety.h"
|
2019-07-23 15:07:06 -07:00
|
|
|
|
2022-09-19 16:11:49 -07:00
|
|
|
#include "health.h"
|
|
|
|
|
|
2021-07-16 12:22:28 -07:00
|
|
|
#include "drivers/can_common.h"
|
2021-08-02 20:26:15 -07:00
|
|
|
|
|
|
|
|
#ifdef STM32H7
|
|
|
|
|
#include "drivers/fdcan.h"
|
|
|
|
|
#else
|
|
|
|
|
#include "drivers/bxcan.h"
|
|
|
|
|
#endif
|
2017-07-29 17:53:39 -07:00
|
|
|
|
2021-07-02 18:25:35 -07:00
|
|
|
#include "obj/gitversion.h"
|
|
|
|
|
|
2022-08-03 13:11:52 +02:00
|
|
|
#include "main_comms.h"
|
2019-12-09 10:39:04 -08:00
|
|
|
|
|
|
|
|
|
2019-07-23 15:07:06 -07:00
|
|
|
// ********************* Serial debugging *********************
|
2017-07-20 13:41:21 -07:00
|
|
|
|
2019-10-22 16:12:18 -07:00
|
|
|
bool check_started(void) {
|
2022-08-12 12:43:36 +02:00
|
|
|
bool started = current_board->check_ignition() || ignition_can;
|
|
|
|
|
ignition_seen |= started;
|
|
|
|
|
return started;
|
2019-10-22 15:27:55 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-20 13:41:21 -07:00
|
|
|
void debug_ring_callback(uart_ring *ring) {
|
|
|
|
|
char rcv;
|
|
|
|
|
while (getc(ring, &rcv)) {
|
2019-07-03 16:01:01 -07:00
|
|
|
(void)putc(ring, rcv); // misra-c2012-17.7: cast to void is ok: debug function
|
2017-07-20 13:41:21 -07:00
|
|
|
|
2019-12-10 08:14:30 -08:00
|
|
|
// only allow bootloader entry on debug builds
|
|
|
|
|
#ifdef ALLOW_DEBUG
|
|
|
|
|
// jump to DFU flash
|
|
|
|
|
if (rcv == 'z') {
|
|
|
|
|
enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
|
|
|
|
|
NVIC_SystemReset();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2017-07-20 13:41:21 -07:00
|
|
|
|
|
|
|
|
// normal reset
|
|
|
|
|
if (rcv == 'x') {
|
|
|
|
|
NVIC_SystemReset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 15:07:06 -07:00
|
|
|
// ****************************** safety mode ******************************
|
|
|
|
|
|
|
|
|
|
// this is the only way to leave silent mode
|
2022-05-02 13:36:19 -07:00
|
|
|
void set_safety_mode(uint16_t mode, uint16_t param) {
|
2019-11-20 15:13:26 -08:00
|
|
|
uint16_t mode_copy = mode;
|
|
|
|
|
int err = set_safety_hooks(mode_copy, param);
|
2019-07-23 15:07:06 -07:00
|
|
|
if (err == -1) {
|
2019-11-20 13:36:29 -08:00
|
|
|
puts("Error: safety set mode failed. Falling back to SILENT\n");
|
2019-11-20 15:13:26 -08:00
|
|
|
mode_copy = SAFETY_SILENT;
|
2022-04-20 23:25:03 -07:00
|
|
|
err = set_safety_hooks(mode_copy, 0U);
|
2019-11-20 13:36:29 -08:00
|
|
|
if (err == -1) {
|
|
|
|
|
puts("Error: Failed setting SILENT mode. Hanging\n");
|
|
|
|
|
while (true) {
|
|
|
|
|
// TERMINAL ERROR: we can't continue if SILENT safety mode isn't succesfully set
|
2019-09-27 17:18:02 -07:00
|
|
|
}
|
2019-11-20 13:36:29 -08:00
|
|
|
}
|
2019-07-23 15:07:06 -07:00
|
|
|
}
|
2022-09-15 13:08:46 -07:00
|
|
|
safety_tx_blocked = 0;
|
|
|
|
|
safety_rx_invalid = 0;
|
2022-01-27 17:46:24 -08:00
|
|
|
|
2019-11-20 15:13:26 -08:00
|
|
|
switch (mode_copy) {
|
2019-11-20 13:36:29 -08:00
|
|
|
case SAFETY_SILENT:
|
|
|
|
|
set_intercept_relay(false);
|
2021-07-02 18:25:35 -07:00
|
|
|
if (current_board->has_obd) {
|
2019-11-20 13:36:29 -08:00
|
|
|
current_board->set_can_mode(CAN_MODE_NORMAL);
|
|
|
|
|
}
|
|
|
|
|
can_silent = ALL_CAN_SILENT;
|
|
|
|
|
break;
|
|
|
|
|
case SAFETY_NOOUTPUT:
|
|
|
|
|
set_intercept_relay(false);
|
2021-07-02 18:25:35 -07:00
|
|
|
if (current_board->has_obd) {
|
2019-11-20 13:36:29 -08:00
|
|
|
current_board->set_can_mode(CAN_MODE_NORMAL);
|
|
|
|
|
}
|
|
|
|
|
can_silent = ALL_CAN_LIVE;
|
|
|
|
|
break;
|
|
|
|
|
case SAFETY_ELM327:
|
|
|
|
|
set_intercept_relay(false);
|
|
|
|
|
heartbeat_counter = 0U;
|
2021-06-04 22:07:45 -07:00
|
|
|
heartbeat_lost = false;
|
2021-07-02 18:25:35 -07:00
|
|
|
if (current_board->has_obd) {
|
2022-04-20 23:25:03 -07:00
|
|
|
if (param == 0U) {
|
2021-06-24 13:12:17 +02:00
|
|
|
current_board->set_can_mode(CAN_MODE_OBD_CAN2);
|
|
|
|
|
} else {
|
|
|
|
|
current_board->set_can_mode(CAN_MODE_NORMAL);
|
|
|
|
|
}
|
2019-11-20 13:36:29 -08:00
|
|
|
}
|
|
|
|
|
can_silent = ALL_CAN_LIVE;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
set_intercept_relay(true);
|
|
|
|
|
heartbeat_counter = 0U;
|
2021-06-04 22:07:45 -07:00
|
|
|
heartbeat_lost = false;
|
2021-07-02 18:25:35 -07:00
|
|
|
if (current_board->has_obd) {
|
2019-11-20 13:36:29 -08:00
|
|
|
current_board->set_can_mode(CAN_MODE_NORMAL);
|
|
|
|
|
}
|
|
|
|
|
can_silent = ALL_CAN_LIVE;
|
|
|
|
|
break;
|
2019-11-20 14:04:07 -08:00
|
|
|
}
|
2019-11-20 13:36:29 -08:00
|
|
|
can_init_all();
|
2019-07-23 15:07:06 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-12 21:53:14 -07:00
|
|
|
bool is_car_safety_mode(uint16_t mode) {
|
|
|
|
|
return (mode != SAFETY_SILENT) &&
|
|
|
|
|
(mode != SAFETY_NOOUTPUT) &&
|
2022-08-17 22:42:18 -07:00
|
|
|
(mode != SAFETY_ALLOUTPUT) &&
|
2021-07-12 21:53:14 -07:00
|
|
|
(mode != SAFETY_ELM327);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 18:11:36 -07:00
|
|
|
// ***************************** main code *****************************
|
|
|
|
|
|
2019-07-08 15:13:52 -07:00
|
|
|
// cppcheck-suppress unusedFunction ; used in headers not included in cppcheck
|
2019-06-24 10:25:30 -07:00
|
|
|
void __initialize_hardware_early(void) {
|
2021-07-02 18:25:35 -07:00
|
|
|
early_initialization();
|
2017-04-06 18:11:36 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-24 10:25:30 -07:00
|
|
|
void __attribute__ ((noinline)) enable_fpu(void) {
|
2018-04-11 19:52:42 -07:00
|
|
|
// enable the FPU
|
2019-07-04 01:04:58 -07:00
|
|
|
SCB->CPACR |= ((3UL << (10U * 2U)) | (3UL << (11U * 2U)));
|
2018-04-11 19:52:42 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-21 13:54:09 -07:00
|
|
|
// go into SILENT when heartbeat isn't received for this amount of seconds.
|
|
|
|
|
#define HEARTBEAT_IGNITION_CNT_ON 5U
|
|
|
|
|
#define HEARTBEAT_IGNITION_CNT_OFF 2U
|
2019-05-23 11:58:57 -07:00
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
// called at 8Hz
|
|
|
|
|
uint8_t loop_counter = 0U;
|
2021-07-02 18:25:35 -07:00
|
|
|
void tick_handler(void) {
|
|
|
|
|
if (TICK_TIMER->SR != 0) {
|
2020-08-17 15:04:01 +02:00
|
|
|
// siren
|
2021-06-29 16:38:50 +02:00
|
|
|
current_board->set_siren((loop_counter & 1U) && (siren_enabled || (siren_countdown > 0U)));
|
2019-05-23 11:58:57 -07:00
|
|
|
|
2022-08-31 20:46:07 -07:00
|
|
|
// tick drivers at 8Hz
|
2022-08-17 20:43:49 -07:00
|
|
|
fan_tick();
|
|
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
// decimated to 1Hz
|
2021-07-12 21:53:14 -07:00
|
|
|
if (loop_counter == 0U) {
|
2020-08-17 15:04:01 +02:00
|
|
|
can_live = pending_can_live;
|
2019-05-23 11:58:57 -07:00
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
//puth(usart1_dma); puts(" "); puth(DMA2_Stream5->M0AR); puts(" "); puth(DMA2_Stream5->NDTR); puts("\n");
|
2019-05-23 11:58:57 -07:00
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
// reset this every 16th pass
|
|
|
|
|
if ((uptime_cnt & 0xFU) == 0U) {
|
|
|
|
|
pending_can_live = 0;
|
|
|
|
|
}
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
puts("** blink ");
|
2021-09-25 21:46:19 -07:00
|
|
|
puts("rx:"); puth4(can_rx_q.r_ptr); puts("-"); puth4(can_rx_q.w_ptr); puts(" ");
|
|
|
|
|
puts("tx1:"); puth4(can_tx1_q.r_ptr); puts("-"); puth4(can_tx1_q.w_ptr); puts(" ");
|
|
|
|
|
puts("tx2:"); puth4(can_tx2_q.r_ptr); puts("-"); puth4(can_tx2_q.w_ptr); puts(" ");
|
|
|
|
|
puts("tx3:"); puth4(can_tx3_q.r_ptr); puts("-"); puth4(can_tx3_q.w_ptr); puts("\n");
|
2020-08-17 15:04:01 +02:00
|
|
|
#endif
|
2019-10-25 16:22:42 -07:00
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
// set green LED to be controls allowed
|
2022-11-17 11:46:43 -08:00
|
|
|
current_board->set_led(LED_GREEN, controls_allowed);
|
2019-07-23 15:07:06 -07:00
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
// turn off the blue LED, turned on by CAN
|
|
|
|
|
// unless we are in power saving mode
|
|
|
|
|
current_board->set_led(LED_BLUE, (uptime_cnt & 1U) && (power_save_status == POWER_SAVE_STATUS_ENABLED));
|
2019-07-23 15:07:06 -07:00
|
|
|
|
2022-08-31 20:46:07 -07:00
|
|
|
// tick drivers at 1Hz
|
|
|
|
|
const bool recent_heartbeat = heartbeat_counter == 0U;
|
|
|
|
|
current_board->board_tick(check_started(), usb_enumerated, recent_heartbeat);
|
|
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
// increase heartbeat counter and cap it at the uint32 limit
|
|
|
|
|
if (heartbeat_counter < __UINT32_MAX__) {
|
|
|
|
|
heartbeat_counter += 1U;
|
2019-11-21 12:53:00 -08:00
|
|
|
}
|
2020-01-30 19:23:19 -08:00
|
|
|
|
2022-08-17 22:42:18 -07:00
|
|
|
// disabling heartbeat not allowed while in safety mode
|
|
|
|
|
if (is_car_safety_mode(current_safety_mode)) {
|
|
|
|
|
heartbeat_disabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 16:38:50 +02:00
|
|
|
if (siren_countdown > 0U) {
|
|
|
|
|
siren_countdown -= 1U;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 15:19:42 -07:00
|
|
|
if (controls_allowed) {
|
|
|
|
|
controls_allowed_countdown = 30U;
|
|
|
|
|
} else if (controls_allowed_countdown > 0U) {
|
|
|
|
|
controls_allowed_countdown -= 1U;
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 17:28:57 -08:00
|
|
|
// exit controls allowed if unused by openpilot for a few seconds
|
|
|
|
|
if (controls_allowed && !heartbeat_engaged) {
|
|
|
|
|
heartbeat_engaged_mismatches += 1U;
|
|
|
|
|
if (heartbeat_engaged_mismatches >= 3U) {
|
|
|
|
|
controls_allowed = 0U;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
heartbeat_engaged_mismatches = 0U;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 13:54:09 -07:00
|
|
|
if (!heartbeat_disabled) {
|
|
|
|
|
// if the heartbeat has been gone for a while, go to SILENT safety mode and enter power save
|
|
|
|
|
if (heartbeat_counter >= (check_started() ? HEARTBEAT_IGNITION_CNT_ON : HEARTBEAT_IGNITION_CNT_OFF)) {
|
|
|
|
|
puts("device hasn't sent a heartbeat for 0x");
|
|
|
|
|
puth(heartbeat_counter);
|
|
|
|
|
puts(" seconds. Safety is set to SILENT mode.\n");
|
2021-06-29 16:38:50 +02:00
|
|
|
|
2021-09-07 15:19:42 -07:00
|
|
|
if (controls_allowed_countdown > 0U) {
|
2021-06-29 16:38:50 +02:00
|
|
|
siren_countdown = 5U;
|
2021-09-07 15:19:42 -07:00
|
|
|
controls_allowed_countdown = 0U;
|
2021-06-29 16:38:50 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-12 21:53:14 -07:00
|
|
|
// set flag to indicate the heartbeat was lost
|
|
|
|
|
if (is_car_safety_mode(current_safety_mode)) {
|
|
|
|
|
heartbeat_lost = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 13:54:09 -07:00
|
|
|
if (current_safety_mode != SAFETY_SILENT) {
|
|
|
|
|
set_safety_mode(SAFETY_SILENT, 0U);
|
|
|
|
|
}
|
2022-11-17 17:59:11 +01:00
|
|
|
|
2021-06-21 13:54:09 -07:00
|
|
|
if (power_save_status != POWER_SAVE_STATUS_ENABLED) {
|
|
|
|
|
set_power_save_state(POWER_SAVE_STATUS_ENABLED);
|
|
|
|
|
}
|
2020-06-11 18:14:41 -07:00
|
|
|
|
2021-06-21 13:54:09 -07:00
|
|
|
// Also disable IR when the heartbeat goes missing
|
|
|
|
|
current_board->set_ir_power(0U);
|
2020-08-17 15:04:01 +02:00
|
|
|
|
2022-11-17 21:02:37 -08:00
|
|
|
// TODO: need a SPI equivalent
|
2021-06-21 13:54:09 -07:00
|
|
|
// If enumerated but no heartbeat (phone up, boardd not running), turn the fan on to cool the device
|
2022-11-17 21:02:37 -08:00
|
|
|
if (usb_enumerated) {
|
2022-08-17 20:43:49 -07:00
|
|
|
fan_set_power(50U);
|
2021-06-21 13:54:09 -07:00
|
|
|
} else {
|
2022-08-17 20:43:49 -07:00
|
|
|
fan_set_power(0U);
|
2021-06-21 13:54:09 -07:00
|
|
|
}
|
2020-08-17 15:04:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-23 11:58:57 -07:00
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
// check registers
|
|
|
|
|
check_registers();
|
2019-12-09 10:39:04 -08:00
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
// set ignition_can to false after 2s of no CAN seen
|
|
|
|
|
if (ignition_can_cnt > 2U) {
|
|
|
|
|
ignition_can = false;
|
2021-07-12 21:53:14 -07:00
|
|
|
}
|
2019-12-03 16:44:55 -08:00
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
// on to the next one
|
|
|
|
|
uptime_cnt += 1U;
|
|
|
|
|
safety_mode_cnt += 1U;
|
|
|
|
|
ignition_can_cnt += 1U;
|
|
|
|
|
|
|
|
|
|
// synchronous safety check
|
2021-09-03 11:45:17 -07:00
|
|
|
safety_tick(current_rx_checks);
|
2020-08-17 15:04:01 +02:00
|
|
|
}
|
2019-12-21 10:25:54 +01:00
|
|
|
|
2020-08-17 15:04:01 +02:00
|
|
|
loop_counter++;
|
|
|
|
|
loop_counter %= 8U;
|
2019-05-23 11:58:57 -07:00
|
|
|
}
|
2021-07-02 18:25:35 -07:00
|
|
|
TICK_TIMER->SR = 0;
|
2019-05-23 11:58:57 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 14:24:04 -08:00
|
|
|
void EXTI_IRQ_Handler(void) {
|
|
|
|
|
if (check_exti_irq()) {
|
|
|
|
|
exti_irq_clear();
|
|
|
|
|
clock_init();
|
|
|
|
|
|
|
|
|
|
set_power_save_state(POWER_SAVE_STATUS_DISABLED);
|
2022-08-01 17:25:48 -07:00
|
|
|
deepsleep_allowed = false;
|
2022-03-07 14:24:04 -08:00
|
|
|
heartbeat_counter = 0U;
|
|
|
|
|
usb_soft_disconnect(false);
|
|
|
|
|
|
|
|
|
|
NVIC_EnableIRQ(TICK_TIMER_IRQ);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t rtc_counter = 0;
|
|
|
|
|
void RTC_WKUP_IRQ_Handler(void) {
|
|
|
|
|
exti_irq_clear();
|
|
|
|
|
clock_init();
|
|
|
|
|
|
|
|
|
|
rtc_counter++;
|
|
|
|
|
if ((rtc_counter % 2U) == 0U) {
|
|
|
|
|
current_board->set_led(LED_BLUE, false);
|
|
|
|
|
} else {
|
|
|
|
|
current_board->set_led(LED_BLUE, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rtc_counter == __UINT8_MAX__) {
|
|
|
|
|
rtc_counter = 1U;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 18:25:35 -07:00
|
|
|
|
2019-06-24 10:25:30 -07:00
|
|
|
int main(void) {
|
2019-12-05 14:19:29 -08:00
|
|
|
// Init interrupt table
|
2019-11-27 18:11:21 -08:00
|
|
|
init_interrupts(true);
|
2019-12-05 14:19:29 -08:00
|
|
|
|
2017-07-20 23:42:19 -07:00
|
|
|
// shouldn't have interrupts here, but just in case
|
2019-08-28 12:53:51 -07:00
|
|
|
disable_interrupts();
|
2017-07-20 23:14:27 -07:00
|
|
|
|
2017-07-27 15:54:55 -07:00
|
|
|
// init early devices
|
2017-04-06 18:11:36 -07:00
|
|
|
clock_init();
|
2019-07-23 15:07:06 -07:00
|
|
|
peripherals_init();
|
|
|
|
|
detect_board_type();
|
|
|
|
|
adc_init();
|
2019-09-27 17:18:02 -07:00
|
|
|
|
2017-07-27 15:54:55 -07:00
|
|
|
// print hello
|
|
|
|
|
puts("\n\n\n************************ MAIN START ************************\n");
|
2017-04-17 13:57:34 -07:00
|
|
|
|
2019-07-23 15:07:06 -07:00
|
|
|
// check for non-supported board types
|
|
|
|
|
if(hw_type == HW_TYPE_UNKNOWN){
|
|
|
|
|
puts("Unsupported board type\n");
|
|
|
|
|
while (1) { /* hang */ }
|
2019-06-28 13:46:57 -07:00
|
|
|
}
|
2019-05-19 09:12:03 -07:00
|
|
|
|
2019-07-23 15:07:06 -07:00
|
|
|
puts("Config:\n");
|
|
|
|
|
puts(" Board type: "); puts(current_board->board_type); puts("\n");
|
|
|
|
|
|
|
|
|
|
// init board
|
|
|
|
|
current_board->init();
|
2017-04-06 18:11:36 -07:00
|
|
|
|
2018-04-11 19:52:42 -07:00
|
|
|
// panda has an FPU, let's use it!
|
|
|
|
|
enable_fpu();
|
|
|
|
|
|
2021-07-02 18:25:35 -07:00
|
|
|
if (current_board->has_gps) {
|
2020-08-26 15:37:50 -07:00
|
|
|
uart_init(&uart_ring_gps, 9600);
|
2018-01-25 23:51:52 -08:00
|
|
|
} else {
|
|
|
|
|
// enable ESP uart
|
2020-08-26 15:37:50 -07:00
|
|
|
uart_init(&uart_ring_gps, 115200);
|
2018-01-25 23:51:52 -08:00
|
|
|
}
|
2019-05-21 09:47:43 -07:00
|
|
|
|
2022-11-17 21:02:37 -08:00
|
|
|
if (current_board->has_lin) {
|
2019-07-23 15:07:06 -07:00
|
|
|
// enable LIN
|
2019-10-04 13:28:56 -07:00
|
|
|
uart_init(&uart_ring_lin1, 10400);
|
2019-07-23 15:07:06 -07:00
|
|
|
UART5->CR2 |= USART_CR2_LINEN;
|
2019-10-04 13:28:56 -07:00
|
|
|
uart_init(&uart_ring_lin2, 10400);
|
2019-07-23 15:07:06 -07:00
|
|
|
USART3->CR2 |= USART_CR2_LINEN;
|
|
|
|
|
}
|
2017-04-06 18:11:36 -07:00
|
|
|
|
2022-11-17 21:02:37 -08:00
|
|
|
if (current_board->fan_max_rpm > 0U) {
|
|
|
|
|
llfan_init();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 18:25:35 -07:00
|
|
|
microsecond_timer_init();
|
2017-10-26 18:44:25 -07:00
|
|
|
|
2019-11-20 11:56:26 -08:00
|
|
|
// init to SILENT and can silent
|
2022-04-20 23:25:03 -07:00
|
|
|
set_safety_mode(SAFETY_SILENT, 0U);
|
2017-05-30 09:46:21 -07:00
|
|
|
|
2019-11-21 13:42:36 -08:00
|
|
|
// enable CAN TXs
|
2020-09-28 16:19:09 +02:00
|
|
|
current_board->enable_can_transceivers(true);
|
2019-11-21 13:42:36 -08:00
|
|
|
|
2021-07-02 18:25:35 -07:00
|
|
|
// 8Hz timer
|
|
|
|
|
REGISTER_INTERRUPT(TICK_TIMER_IRQ, tick_handler, 10U, FAULT_INTERRUPT_RATE_TICK)
|
|
|
|
|
tick_timer_init();
|
2019-05-23 11:58:57 -07:00
|
|
|
|
2019-04-01 22:45:00 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
puts("DEBUG ENABLED\n");
|
|
|
|
|
#endif
|
2019-07-23 15:07:06 -07:00
|
|
|
// enable USB (right before interrupts or enum can fail!)
|
|
|
|
|
usb_init();
|
2017-04-06 18:11:36 -07:00
|
|
|
|
2022-11-10 03:46:20 +01:00
|
|
|
#ifdef ENABLE_SPI
|
2022-11-13 19:57:20 -08:00
|
|
|
if (current_board->has_spi) {
|
|
|
|
|
spi_init();
|
|
|
|
|
}
|
2022-11-10 03:46:20 +01:00
|
|
|
#endif
|
|
|
|
|
|
2017-04-06 18:11:36 -07:00
|
|
|
puts("**** INTERRUPTS ON ****\n");
|
2019-06-14 13:28:15 -07:00
|
|
|
enable_interrupts();
|
2017-04-06 18:11:36 -07:00
|
|
|
|
|
|
|
|
// LED should keep on blinking all the time
|
2017-08-22 08:27:00 -07:00
|
|
|
uint64_t cnt = 0;
|
|
|
|
|
|
2017-04-06 18:11:36 -07:00
|
|
|
for (cnt=0;;cnt++) {
|
2019-05-23 11:58:57 -07:00
|
|
|
if (power_save_status == POWER_SAVE_STATUS_DISABLED) {
|
2019-11-27 18:11:21 -08:00
|
|
|
#ifdef DEBUG_FAULTS
|
2022-08-31 20:46:07 -07:00
|
|
|
if (fault_status == FAULT_STATUS_NONE) {
|
2019-11-27 18:11:21 -08:00
|
|
|
#endif
|
|
|
|
|
// useful for debugging, fade breaks = panda is overloaded
|
2022-08-31 20:46:07 -07:00
|
|
|
for (uint32_t fade = 0U; fade < MAX_LED_FADE; fade += 1U) {
|
2020-06-02 14:21:41 -07:00
|
|
|
current_board->set_led(LED_RED, true);
|
|
|
|
|
delay(fade >> 4);
|
|
|
|
|
current_board->set_led(LED_RED, false);
|
2021-07-02 18:25:35 -07:00
|
|
|
delay((MAX_LED_FADE - fade) >> 4);
|
2019-05-21 09:47:43 -07:00
|
|
|
}
|
2020-06-02 14:21:41 -07:00
|
|
|
|
2022-08-31 20:46:07 -07:00
|
|
|
for (uint32_t fade = MAX_LED_FADE; fade > 0U; fade -= 1U) {
|
2020-06-02 14:21:41 -07:00
|
|
|
current_board->set_led(LED_RED, true);
|
|
|
|
|
delay(fade >> 4);
|
|
|
|
|
current_board->set_led(LED_RED, false);
|
2021-07-02 18:25:35 -07:00
|
|
|
delay((MAX_LED_FADE - fade) >> 4);
|
2020-06-02 14:21:41 -07:00
|
|
|
}
|
|
|
|
|
|
2019-11-27 18:11:21 -08:00
|
|
|
#ifdef DEBUG_FAULTS
|
|
|
|
|
} else {
|
|
|
|
|
current_board->set_led(LED_RED, 1);
|
|
|
|
|
delay(512000U);
|
|
|
|
|
current_board->set_led(LED_RED, 0);
|
|
|
|
|
delay(512000U);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2019-05-23 11:58:57 -07:00
|
|
|
} else {
|
2022-08-12 12:43:36 +02:00
|
|
|
if (deepsleep_allowed && !usb_enumerated && !check_started() && ignition_seen && (heartbeat_counter > 20U)) {
|
2022-03-07 14:24:04 -08:00
|
|
|
usb_soft_disconnect(true);
|
2022-08-17 20:43:49 -07:00
|
|
|
fan_set_power(0U);
|
2022-03-07 14:24:04 -08:00
|
|
|
NVIC_DisableIRQ(TICK_TIMER_IRQ);
|
|
|
|
|
delay(512000U);
|
|
|
|
|
|
|
|
|
|
// Init IRQs for CAN transceiver and ignition line
|
|
|
|
|
exti_irq_init();
|
|
|
|
|
|
|
|
|
|
// Init RTC Wakeup event on EXTI22
|
|
|
|
|
REGISTER_INTERRUPT(RTC_WKUP_IRQn, RTC_WKUP_IRQ_Handler, 10U, FAULT_INTERRUPT_RATE_EXTI)
|
|
|
|
|
rtc_wakeup_init();
|
|
|
|
|
|
|
|
|
|
// STOP mode
|
|
|
|
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
|
|
|
|
}
|
2019-05-23 11:58:57 -07:00
|
|
|
__WFI();
|
2022-03-07 14:24:04 -08:00
|
|
|
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
|
2017-08-22 09:44:08 -07:00
|
|
|
}
|
2017-04-06 18:11:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|