diff --git a/board/crc.h b/board/crc.h index 3e20fb09..a3caf785 100644 --- a/board/crc.h +++ b/board/crc.h @@ -1,5 +1,6 @@ #pragma once +#if defined(ENABLE_SPI) || defined(BOOTSTUB) uint8_t crc_checksum(const uint8_t *dat, int len, const uint8_t poly) { uint8_t crc = 0xFFU; int i; @@ -17,3 +18,4 @@ uint8_t crc_checksum(const uint8_t *dat, int len, const uint8_t poly) { } return crc; } +#endif diff --git a/board/drivers/can_common.h b/board/drivers/can_common.h index fa69c698..fd2cfad4 100644 --- a/board/drivers/can_common.h +++ b/board/drivers/can_common.h @@ -178,13 +178,13 @@ void ignition_can_hook(CANPacket_t *to_push) { // 0x152 overlaps with Subaru pre-global which has this bit as the high beam int counter = GET_BYTE(to_push, 1) & 0xFU; // max is only 14 - static int prev_counter = -1; - if ((counter == ((prev_counter + 1) % 15)) && (prev_counter != -1)) { + static int prev_counter_rivian = -1; + if ((counter == ((prev_counter_rivian + 1) % 15)) && (prev_counter_rivian != -1)) { // VDM_OutputSignals->VDM_EpasPowerMode ignition_can = ((GET_BYTE(to_push, 7) >> 4U) & 0x3U) == 1U; // VDM_EpasPowerMode_Drive_On=1 ignition_can_cnt = 0U; } - prev_counter = counter; + prev_counter_rivian = counter; } // Tesla Model 3/Y exception @@ -192,14 +192,14 @@ void ignition_can_hook(CANPacket_t *to_push) { // 0x221 overlaps with Rivian which has random data on byte 0 int counter = GET_BYTE(to_push, 6) >> 4; - static int prev_counter = -1; - if ((counter == ((prev_counter + 1) % 16)) && (prev_counter != -1)) { + static int prev_counter_tesla = -1; + if ((counter == ((prev_counter_tesla + 1) % 16)) && (prev_counter_tesla != -1)) { // VCFRONT_LVPowerState->VCFRONT_vehiclePowerState int power_state = (GET_BYTE(to_push, 0) >> 5U) & 0x3U; ignition_can = power_state == 0x3; // VEHICLE_POWER_STATE_DRIVE=3 ignition_can_cnt = 0U; } - prev_counter = counter; + prev_counter_tesla = counter; } // Mazda exception diff --git a/board/drivers/gpio.h b/board/drivers/gpio.h index 0b8fc091..aea60425 100644 --- a/board/drivers/gpio.h +++ b/board/drivers/gpio.h @@ -10,11 +10,6 @@ #define OUTPUT_TYPE_PUSH_PULL 0U #define OUTPUT_TYPE_OPEN_DRAIN 1U -typedef struct { - GPIO_TypeDef * const bank; - uint8_t pin; -} gpio_t; - void set_gpio_mode(GPIO_TypeDef *GPIO, unsigned int pin, unsigned int mode) { ENTER_CRITICAL(); uint32_t tmp = GPIO->MODER; @@ -68,6 +63,12 @@ int get_gpio_input(const GPIO_TypeDef *GPIO, unsigned int pin) { return (GPIO->IDR & (1UL << pin)) == (1UL << pin); } +#ifdef PANDA_JUNGLE +typedef struct { + GPIO_TypeDef * const bank; + uint8_t pin; +} gpio_t; + void gpio_set_all_output(gpio_t *pins, uint8_t num_pins, bool enabled) { for (uint8_t i = 0; i < num_pins; i++) { set_gpio_output(pins[i].bank, pins[i].pin, enabled); @@ -79,6 +80,7 @@ void gpio_set_bitmask(gpio_t *pins, uint8_t num_pins, uint32_t bitmask) { set_gpio_output(pins[i].bank, pins[i].pin, (bitmask >> i) & 1U); } } +#endif // Detection with internal pullup #define PULL_EFFECTIVE_DELAY 4096 diff --git a/board/drivers/watchdog.h b/board/drivers/watchdog.h deleted file mode 100644 index 51337ba6..00000000 --- a/board/drivers/watchdog.h +++ /dev/null @@ -1,24 +0,0 @@ -typedef enum { - WATCHDOG_50_MS = (400U - 1U), - WATCHDOG_500_MS = 4000U, -} WatchdogTimeout; - -static void watchdog_feed(void) { - IND_WDG->KR = 0xAAAAU; -} - -void watchdog_init(WatchdogTimeout timeout) { - // enable watchdog - IND_WDG->KR = 0xCCCCU; - IND_WDG->KR = 0x5555U; - - // 32KHz / 4 prescaler = 8000Hz - register_set(&(IND_WDG->PR), 0x0U, IWDG_PR_PR_Msk); - register_set(&(IND_WDG->RLR), timeout, IWDG_RLR_RL_Msk); - - // wait for watchdog to be updated - while (IND_WDG->SR != 0U); - - // start the countdown - watchdog_feed(); -} diff --git a/board/flasher.h b/board/flasher.h index ba9d77a8..9cf21542 100644 --- a/board/flasher.h +++ b/board/flasher.h @@ -137,11 +137,12 @@ void soft_flasher_start(void) { // enable USB usb_init(); - // enable SPI +#ifdef ENABLE_SPI if (current_board->has_spi) { gpio_spi_init(); spi_init(); } +#endif // green LED on for flashing led_set(LED_GREEN, 1); diff --git a/board/stm32f4/lluart.h b/board/stm32f4/lluart.h index f4b1a5f1..660c1ce1 100644 --- a/board/stm32f4/lluart.h +++ b/board/stm32f4/lluart.h @@ -20,61 +20,9 @@ void uart_tx_ring(uart_ring *q){ EXIT_CRITICAL(); } -static void uart_rx_ring(uart_ring *q){ - ENTER_CRITICAL(); - - // Read out RX buffer - uint8_t c = q->uart->DR; // This read after reading SR clears a bunch of interrupts - - uint16_t next_w_ptr = (q->w_ptr_rx + 1U) % q->rx_fifo_size; - - if ((next_w_ptr == q->r_ptr_rx) && q->overwrite) { - // overwrite mode: drop oldest byte - q->r_ptr_rx = (q->r_ptr_rx + 1U) % q->rx_fifo_size; - } - - // Do not overwrite buffer data - if (next_w_ptr != q->r_ptr_rx) { - q->elems_rx[q->w_ptr_rx] = c; - q->w_ptr_rx = next_w_ptr; - if (q->callback != NULL) { - q->callback(q); - } - } - - EXIT_CRITICAL(); -} - // This read after reading SR clears all error interrupts. We don't want compiler warnings, nor optimizations #define UART_READ_DR(uart) volatile uint8_t t = (uart)->DR; UNUSED(t); -static void uart_interrupt_handler(uart_ring *q) { - ENTER_CRITICAL(); - - // Read UART status. This is also the first step necessary in clearing most interrupts - uint32_t status = q->uart->SR; - - // If RXNE is set, perform a read. This clears RXNE, ORE, IDLE, NF and FE - if((status & USART_SR_RXNE) != 0U){ - uart_rx_ring(q); - } - - // Detect errors and clear them - uint32_t err = (status & USART_SR_ORE) | (status & USART_SR_NE) | (status & USART_SR_FE) | (status & USART_SR_PE); - if(err != 0U){ - #ifdef DEBUG_UART - print("Encountered UART error: "); puth(err); print("\n"); - #endif - UART_READ_DR(q->uart) - } - // Send if necessary - uart_tx_ring(q); - - EXIT_CRITICAL(); -} - -void USART2_IRQ_Handler(void) { uart_interrupt_handler(&uart_ring_debug); } - // ***************************** Hardware setup ***************************** #define DIV_(_PCLK_, _BAUD_) (((_PCLK_) * 25U) / (4U * (_BAUD_))) diff --git a/board/stm32f4/peripherals.h b/board/stm32f4/peripherals.h index 9c0b8ee6..fc974091 100644 --- a/board/stm32f4/peripherals.h +++ b/board/stm32f4/peripherals.h @@ -9,6 +9,7 @@ static void gpio_usb_init(void) { GPIOA->OSPEEDR = GPIO_OSPEEDER_OSPEEDR11 | GPIO_OSPEEDER_OSPEEDR12; } +#ifdef ENABLE_SPI void gpio_spi_init(void) { // A4-A7: SPI set_gpio_alternate(GPIOA, 4, GPIO_AF5_SPI1); @@ -17,12 +18,15 @@ void gpio_spi_init(void) { set_gpio_alternate(GPIOA, 7, GPIO_AF5_SPI1); register_set_bits(&(GPIOA->OSPEEDR), GPIO_OSPEEDER_OSPEEDR4 | GPIO_OSPEEDER_OSPEEDR5 | GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7); } +#endif +#ifdef BOOTSTUB void gpio_usart2_init(void) { // A2,A3: USART 2 for debugging set_gpio_alternate(GPIOA, 2, GPIO_AF7_USART2); set_gpio_alternate(GPIOA, 3, GPIO_AF7_USART2); } +#endif // Common GPIO initialization void common_init_gpio(void) { @@ -45,12 +49,14 @@ void common_init_gpio(void) { set_gpio_alternate(GPIOB, 9, GPIO_AF8_CAN1); } +#ifdef BOOTSTUB void flasher_peripherals_init(void) { RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN; RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN; RCC->APB1ENR |= RCC_APB1ENR_USART2EN; } +#endif // Peripheral initialization void peripherals_init(void) { diff --git a/board/stm32f4/stm32f4_config.h b/board/stm32f4/stm32f4_config.h index ee2759a7..b3fc2a89 100644 --- a/board/stm32f4/stm32f4_config.h +++ b/board/stm32f4/stm32f4_config.h @@ -52,7 +52,6 @@ #include "drivers/timers.h" #include "stm32f4/board.h" #include "stm32f4/clock.h" -#include "drivers/watchdog.h" #include "drivers/spi.h" #include "stm32f4/llspi.h" diff --git a/board/stm32h7/board.h b/board/stm32h7/board.h index da47465d..0507ca3f 100644 --- a/board/stm32h7/board.h +++ b/board/stm32h7/board.h @@ -9,7 +9,6 @@ #include "drivers/harness.h" #include "drivers/fan.h" #include "stm32h7/llfan.h" -#include "stm32h7/lldac.h" #include "drivers/fake_siren.h" #include "stm32h7/sound.h" #include "drivers/clock_source.h" diff --git a/board/stm32h7/lldac.h b/board/stm32h7/lldac.h deleted file mode 100644 index 5726f624..00000000 --- a/board/stm32h7/lldac.h +++ /dev/null @@ -1,42 +0,0 @@ -void dac_init(DAC_TypeDef *dac, uint8_t channel, bool dma) { - register_set(&dac->CR, 0U, 0xFFFFU); - register_set(&dac->MCR, 0U, 0xFFFFU); - - switch(channel) { - case 1: - if (dma) { - register_set_bits(&dac->CR, DAC_CR_DMAEN1); - // register_set(&DAC->CR, (6U << DAC_CR_TSEL1_Pos), DAC_CR_TSEL1); - register_set_bits(&dac->CR, DAC_CR_TEN1); - } else { - register_clear_bits(&dac->CR, DAC_CR_DMAEN1); - } - register_set_bits(&dac->CR, DAC_CR_EN1); - break; - case 2: - if (dma) { - register_set_bits(&dac->CR, DAC_CR_DMAEN2); - } else { - register_clear_bits(&dac->CR, DAC_CR_DMAEN2); - } - register_set_bits(&dac->CR, DAC_CR_EN2); - break; - default: - break; - } -} - -// Set channel 1 value, in mV -void dac_set(DAC_TypeDef *dac, uint8_t channel, uint32_t value) { - uint32_t raw_val = MAX(MIN(value * (1UL << 8U) / 3300U, (1UL << 8U)), 0U); - switch(channel) { - case 1: - register_set(&dac->DHR8R1, raw_val, 0xFFU); - break; - case 2: - register_set(&dac->DHR8R2, raw_val, 0xFFU); - break; - default: - break; - } -} diff --git a/board/stm32h7/peripherals.h b/board/stm32h7/peripherals.h index a0b541cc..2cdbea6d 100644 --- a/board/stm32h7/peripherals.h +++ b/board/stm32h7/peripherals.h @@ -9,6 +9,7 @@ static void gpio_usb_init(void) { GPIOA->OSPEEDR = GPIO_OSPEEDR_OSPEED11 | GPIO_OSPEEDR_OSPEED12; } +#ifdef ENABLE_SPI void gpio_spi_init(void) { set_gpio_alternate(GPIOE, 11, GPIO_AF5_SPI4); set_gpio_alternate(GPIOE, 12, GPIO_AF5_SPI4); @@ -16,12 +17,15 @@ void gpio_spi_init(void) { set_gpio_alternate(GPIOE, 14, GPIO_AF5_SPI4); register_set_bits(&(GPIOE->OSPEEDR), GPIO_OSPEEDR_OSPEED11 | GPIO_OSPEEDR_OSPEED12 | GPIO_OSPEEDR_OSPEED13 | GPIO_OSPEEDR_OSPEED14); } +#endif +#ifdef BOOTSTUB void gpio_usart2_init(void) { // A2,A3: USART 2 for debugging set_gpio_alternate(GPIOA, 2, GPIO_AF7_USART2); set_gpio_alternate(GPIOA, 3, GPIO_AF7_USART2); } +#endif void gpio_uart7_init(void) { // E7,E8: UART 7 for debugging @@ -62,6 +66,7 @@ void common_init_gpio(void) { set_gpio_alternate(GPIOG, 10, GPIO_AF2_FDCAN3); } +#ifdef BOOTSTUB void flasher_peripherals_init(void) { RCC->AHB1ENR |= RCC_AHB1ENR_USB1OTGHSEN; @@ -69,6 +74,7 @@ void flasher_peripherals_init(void) { RCC->APB2ENR |= RCC_APB2ENR_SPI4EN; RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN; } +#endif // Peripheral initialization void peripherals_init(void) { diff --git a/board/stm32h7/stm32h7_config.h b/board/stm32h7/stm32h7_config.h index bfc12e8c..4355fe8e 100644 --- a/board/stm32h7/stm32h7_config.h +++ b/board/stm32h7/stm32h7_config.h @@ -63,7 +63,6 @@ separate IRQs for RX and TX. #include "stm32h7/peripherals.h" #include "stm32h7/interrupt_handlers.h" #include "drivers/timers.h" -#include "drivers/watchdog.h" #if !defined(BOOTSTUB) #include "drivers/uart.h"