Skip to content

Commit d5a88e2

Browse files
aubswrobbederks
authored andcommitted
Add guards around platform-specific code (commaai#2189)
* guard platform specific code * more simple * less dumb guard * stragglers * blankspace * blankspace2 * crc.h * keep condition * good call * consistency * another one --------- Co-authored-by: Robbe Derks <robbe.derks@gmail.com>
1 parent 1560242 commit d5a88e2

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

board/crc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#if defined(ENABLE_SPI) || defined(BOOTSTUB)
34
uint8_t crc_checksum(const uint8_t *dat, int len, const uint8_t poly) {
45
uint8_t crc = 0xFFU;
56
int i;
@@ -17,3 +18,4 @@ uint8_t crc_checksum(const uint8_t *dat, int len, const uint8_t poly) {
1718
}
1819
return crc;
1920
}
21+
#endif

board/drivers/gpio.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
#define OUTPUT_TYPE_PUSH_PULL 0U
1111
#define OUTPUT_TYPE_OPEN_DRAIN 1U
1212

13-
typedef struct {
14-
GPIO_TypeDef * const bank;
15-
uint8_t pin;
16-
} gpio_t;
17-
1813
void set_gpio_mode(GPIO_TypeDef *GPIO, unsigned int pin, unsigned int mode) {
1914
ENTER_CRITICAL();
2015
uint32_t tmp = GPIO->MODER;
@@ -68,6 +63,12 @@ int get_gpio_input(const GPIO_TypeDef *GPIO, unsigned int pin) {
6863
return (GPIO->IDR & (1UL << pin)) == (1UL << pin);
6964
}
7065

66+
#ifdef PANDA_JUNGLE
67+
typedef struct {
68+
GPIO_TypeDef * const bank;
69+
uint8_t pin;
70+
} gpio_t;
71+
7172
void gpio_set_all_output(gpio_t *pins, uint8_t num_pins, bool enabled) {
7273
for (uint8_t i = 0; i < num_pins; i++) {
7374
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) {
7980
set_gpio_output(pins[i].bank, pins[i].pin, (bitmask >> i) & 1U);
8081
}
8182
}
83+
#endif
8284

8385
// Detection with internal pullup
8486
#define PULL_EFFECTIVE_DELAY 4096

board/flasher.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,13 @@ void soft_flasher_start(void) {
170170
usb_init();
171171
print("USB initialized\n");
172172

173-
// enable SPI
173+
#ifdef ENABLE_SPI
174174
if (current_board->has_spi) {
175175
gpio_spi_init();
176176
spi_init();
177177
print("SPI initialized\n");
178178
}
179+
#endif
179180

180181
#ifdef HW_RICHIE_REV1
181182
// LED footprint is incorrect. Green LED turns power on to LEDs when 0

board/stm32f4/peripherals.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ static void gpio_usb_init(void) {
99
GPIOA->OSPEEDR = GPIO_OSPEEDER_OSPEEDR11 | GPIO_OSPEEDER_OSPEEDR12;
1010
}
1111

12+
#ifdef ENABLE_SPI
1213
void gpio_spi_init(void) {
1314
// A4-A7: SPI
1415
set_gpio_alternate(GPIOA, 4, GPIO_AF5_SPI1);
@@ -17,12 +18,15 @@ void gpio_spi_init(void) {
1718
set_gpio_alternate(GPIOA, 7, GPIO_AF5_SPI1);
1819
register_set_bits(&(GPIOA->OSPEEDR), GPIO_OSPEEDER_OSPEEDR4 | GPIO_OSPEEDER_OSPEEDR5 | GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7);
1920
}
21+
#endif
2022

23+
#ifdef BOOTSTUB
2124
void gpio_usart2_init(void) {
2225
// A2,A3: USART 2 for debugging
2326
set_gpio_alternate(GPIOA, 2, GPIO_AF7_USART2);
2427
set_gpio_alternate(GPIOA, 3, GPIO_AF7_USART2);
2528
}
29+
#endif
2630

2731
// Common GPIO initialization
2832
void common_init_gpio(void) {
@@ -45,12 +49,14 @@ void common_init_gpio(void) {
4549
set_gpio_alternate(GPIOB, 9, GPIO_AF8_CAN1);
4650
}
4751

52+
#ifdef BOOTSTUB
4853
void flasher_peripherals_init(void) {
4954
RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
5055
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
5156
RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN;
5257
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
5358
}
59+
#endif
5460

5561
// Peripheral initialization
5662
void peripherals_init(void) {

board/stm32h7/peripherals.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ static void gpio_usb_init(void) {
99
GPIOA->OSPEEDR = GPIO_OSPEEDR_OSPEED11 | GPIO_OSPEEDR_OSPEED12;
1010
}
1111

12+
#ifdef ENABLE_SPI
1213
void gpio_spi_init(void) {
1314
set_gpio_alternate(GPIOE, 11, GPIO_AF5_SPI4);
1415
set_gpio_alternate(GPIOE, 12, GPIO_AF5_SPI4);
@@ -17,7 +18,9 @@ void gpio_spi_init(void) {
1718
register_set_bits(&(GPIOE->OSPEEDR), GPIO_OSPEEDR_OSPEED11 | GPIO_OSPEEDR_OSPEED12 |
1819
GPIO_OSPEEDR_OSPEED13 | GPIO_OSPEEDR_OSPEED14);
1920
}
21+
#endif
2022

23+
#ifdef BOOTSTUB
2124
void gpio_usart2_init(void) {
2225
#ifndef RICHIE
2326
// A2,A3: USART 2 for debugging
@@ -29,6 +32,7 @@ void gpio_usart2_init(void) {
2932
set_gpio_alternate(GPIOD, 6, GPIO_AF7_USART2);
3033
#endif
3134
}
35+
#endif
3236

3337
void gpio_uart7_init(void) {
3438
// E7,E8: UART 7 connected to nRF
@@ -75,13 +79,15 @@ void common_init_gpio(void) {
7579
#endif
7680
}
7781

82+
#ifdef BOOTSTUB
7883
void flasher_peripherals_init(void) {
7984
RCC->AHB1ENR |= RCC_AHB1ENR_USB1OTGHSEN;
8085

8186
// SPI + DMA
8287
RCC->APB2ENR |= RCC_APB2ENR_SPI4EN;
8388
RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
8489
}
90+
#endif
8591

8692
// Peripheral initialization
8793
void peripherals_init(void) {

0 commit comments

Comments
 (0)