From 4886b6b9846c795baa4641f78caf2e5e31f49863 Mon Sep 17 00:00:00 2001 From: rbiasini Date: Wed, 26 Jun 2019 16:16:35 -0700 Subject: [PATCH] Misra 12.1: The precedence of operators within expressions should be made explicit (#227) * Fixed all misra 12.1 violations --- board/config.h | 2 +- board/drivers/can.h | 6 +++--- board/drivers/gmlan_alt.h | 16 ++++++++-------- board/drivers/llcan.h | 2 +- board/drivers/uart.h | 14 +++++++------- board/drivers/usb.h | 10 +++++----- board/gpio.h | 6 +++--- board/libc.h | 2 +- board/main.c | 2 +- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/board/config.h b/board/config.h index 0230856c..219ce64c 100644 --- a/board/config.h +++ b/board/config.h @@ -22,7 +22,7 @@ #include #define NULL ((void*)0) -#define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)])) +#define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - (2 * (!(pred)))])) #define MIN(a,b) \ ({ __typeof__ (a) _a = (a); \ diff --git a/board/drivers/can.h b/board/drivers/can.h index 1dbfa278..11e8ff3a 100644 --- a/board/drivers/can.h +++ b/board/drivers/can.h @@ -162,7 +162,7 @@ void can_init_all(void) { } void can_set_gmlan(int bus) { - if (bus == -1 || bus != can_num_lookup[3]) { + if ((bus == -1) || (bus != can_num_lookup[3])) { // GMLAN OFF switch (can_num_lookup[3]) { case 1: @@ -309,7 +309,7 @@ void can_rx(uint8_t can_number) { to_push.RDTR = (to_push.RDTR & 0xFFFF000F) | (bus_number << 4); // forwarding (panda only) - int bus_fwd_num = can_forwarding[bus_number] != -1 ? can_forwarding[bus_number] : safety_fwd_hook(bus_number, &to_push); + int bus_fwd_num = (can_forwarding[bus_number] != -1) ? can_forwarding[bus_number] : safety_fwd_hook(bus_number, &to_push); if (bus_fwd_num != -1) { CAN_FIFOMailBox_TypeDef to_send; to_send.RIR = to_push.RIR | 1; // TXRQ @@ -347,7 +347,7 @@ void can_send(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number) { // add CAN packet to send queue // bus number isn't passed through to_push->RDTR &= 0xF; - if (bus_number == 3 && can_num_lookup[3] == 0xFF) { + if ((bus_number == 3) && (can_num_lookup[3] == 0xFF)) { // TODO: why uint8 bro? only int8? bitbang_gmlan(to_push); } else { diff --git a/board/drivers/gmlan_alt.h b/board/drivers/gmlan_alt.h index 3b7cb828..3654a21a 100644 --- a/board/drivers/gmlan_alt.h +++ b/board/drivers/gmlan_alt.h @@ -185,7 +185,7 @@ int gmlan_fail_count = 0; void TIM4_IRQHandler(void) { if (gmlan_alt_mode == BITBANG) { - if (TIM4->SR & TIM_SR_UIF && gmlan_sendmax != -1) { + if ((TIM4->SR & TIM_SR_UIF) && (gmlan_sendmax != -1)) { int read = get_gpio_input(GPIOB, 12); if (gmlan_silent_count < REQUIRED_SILENT_TIME) { if (read == 0) { @@ -196,14 +196,14 @@ void TIM4_IRQHandler(void) { } else if (gmlan_silent_count == REQUIRED_SILENT_TIME) { bool retry = 0; // in send loop - if (gmlan_sending > 0 && // not first bit - (read == 0 && pkt_stuffed[gmlan_sending-1] == 1) && // bus wrongly dominant - gmlan_sending != (gmlan_sendmax-11)) { //not ack bit + if ((gmlan_sending > 0) && // not first bit + ((read == 0) && (pkt_stuffed[gmlan_sending-1] == 1)) && // bus wrongly dominant + (gmlan_sending != (gmlan_sendmax - 11))) { //not ack bit puts("GMLAN ERR: bus driven at "); puth(gmlan_sending); puts("\n"); retry = 1; - } else if (read == 1 && gmlan_sending == (gmlan_sendmax-11)) { // recessive during ACK + } else if ((read == 1) && (gmlan_sending == (gmlan_sendmax - 11))) { // recessive during ACK puts("GMLAN ERR: didn't recv ACK\n"); retry = 1; } @@ -221,7 +221,7 @@ void TIM4_IRQHandler(void) { gmlan_sending++; } } - if (gmlan_sending == gmlan_sendmax || gmlan_fail_count == MAX_FAIL_COUNT) { + if ((gmlan_sending == gmlan_sendmax) || (gmlan_fail_count == MAX_FAIL_COUNT)) { set_bitbanged_gmlan(1); // recessive set_gpio_mode(GPIOB, 13, MODE_INPUT); TIM4->DIER = 0; // no update interrupt @@ -233,8 +233,8 @@ void TIM4_IRQHandler(void) { } //bit bang mode else if (gmlan_alt_mode == GPIO_SWITCH) { - if (TIM4->SR & TIM_SR_UIF && gmlan_switch_below_timeout != -1) { - if (can_timeout_counter == 0 && gmlan_switch_timeout_enable) { + if ((TIM4->SR & TIM_SR_UIF) && (gmlan_switch_below_timeout != -1)) { + if ((can_timeout_counter == 0) && gmlan_switch_timeout_enable) { //it has been more than 1 second since timeout was reset; disable timer and restore the GMLAN output set_gpio_output(GPIOB, 13, GMLAN_LOW); gmlan_switch_below_timeout = -1; diff --git a/board/drivers/llcan.h b/board/drivers/llcan.h index baa3a880..5ee93ba7 100644 --- a/board/drivers/llcan.h +++ b/board/drivers/llcan.h @@ -31,7 +31,7 @@ bool llcan_set_speed(CAN_TypeDef *CAN, uint32_t speed, bool loopback, bool silen #define CAN_TIMEOUT 1000000 int tmp = 0; - while((CAN->MSR & CAN_MSR_INAK) == CAN_MSR_INAK && tmp < CAN_TIMEOUT) tmp++; + while(((CAN->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) && (tmp < CAN_TIMEOUT)) tmp++; if (tmp < CAN_TIMEOUT) { return true; } diff --git a/board/drivers/uart.h b/board/drivers/uart.h index 5df63154..4ea41616 100644 --- a/board/drivers/uart.h +++ b/board/drivers/uart.h @@ -83,7 +83,7 @@ void uart_ring_process(uart_ring *q) { q->uart->CR1 &= ~USART_CR1_TXEIE; } - if (sr & USART_SR_RXNE || sr & USART_SR_ORE) { + if ((sr & USART_SR_RXNE) || (sr & USART_SR_ORE)) { uint8_t c = q->uart->DR; // TODO: can drop packets if (q != &esp_ring) { uint16_t next_w_ptr = (q->w_ptr_rx + 1) % FIFO_SIZE; @@ -188,10 +188,10 @@ void clear_uart_buff(uart_ring *q) { // ***************************** start UART code ***************************** -#define __DIV(_PCLK_, _BAUD_) (((_PCLK_)*25)/(4*(_BAUD_))) -#define __DIVMANT(_PCLK_, _BAUD_) (__DIV((_PCLK_), (_BAUD_))/100) -#define __DIVFRAQ(_PCLK_, _BAUD_) (((__DIV((_PCLK_), (_BAUD_)) - (__DIVMANT((_PCLK_), (_BAUD_)) * 100)) * 16 + 50) / 100) -#define __USART_BRR(_PCLK_, _BAUD_) ((__DIVMANT((_PCLK_), (_BAUD_)) << 4)|(__DIVFRAQ((_PCLK_), (_BAUD_)) & 0x0F)) +#define __DIV(_PCLK_, _BAUD_) (((_PCLK_) * 25) / (4 * (_BAUD_))) +#define __DIVMANT(_PCLK_, _BAUD_) (__DIV((_PCLK_), (_BAUD_)) / 100) +#define __DIVFRAQ(_PCLK_, _BAUD_) ((((__DIV((_PCLK_), (_BAUD_)) - (__DIVMANT((_PCLK_), (_BAUD_)) * 100)) * 16) + 50) / 100) +#define __USART_BRR(_PCLK_, _BAUD_) ((__DIVMANT((_PCLK_), (_BAUD_)) << 4) | (__DIVFRAQ((_PCLK_), (_BAUD_)) & 0x0F)) void uart_set_baud(USART_TypeDef *u, int baud) { if (u == USART1) { @@ -210,7 +210,7 @@ void uart_dma_drain(void) { enter_critical_section(); - if (DMA2->HISR & DMA_HISR_TCIF5 || DMA2->HISR & DMA_HISR_HTIF5 || DMA2_Stream5->NDTR != USART1_DMA_LEN) { + if ((DMA2->HISR & DMA_HISR_TCIF5) || (DMA2->HISR & DMA_HISR_HTIF5) || (DMA2_Stream5->NDTR != USART1_DMA_LEN)) { // disable DMA q->uart->CR3 &= ~USART_CR3_DMAR; DMA2_Stream5->CR &= ~DMA_SxCR_EN; @@ -338,7 +338,7 @@ void puth2(unsigned int i) { void hexdump(const void *a, int l) { int i; for (i=0;iDTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= 0x40) { + if ((ep0_txlen != 0) && ((USBx_INEP(0)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= 0x40)) { uint16_t len = MIN(ep0_txlen, 0x40); USB_WritePacket(ep0_txdata, len, 0); ep0_txdata += len; diff --git a/board/gpio.h b/board/gpio.h index ca8a2aa6..905bde4a 100644 --- a/board/gpio.h +++ b/board/gpio.h @@ -381,9 +381,9 @@ void jump_to_bootloader(void) { void early(void) { // after it's been in the bootloader, things are initted differently, so we reset - if (enter_bootloader_mode != BOOT_NORMAL && - enter_bootloader_mode != ENTER_BOOTLOADER_MAGIC && - enter_bootloader_mode != ENTER_SOFTLOADER_MAGIC) { + if ((enter_bootloader_mode != BOOT_NORMAL) && + (enter_bootloader_mode != ENTER_BOOTLOADER_MAGIC) && + (enter_bootloader_mode != ENTER_SOFTLOADER_MAGIC)) { enter_bootloader_mode = BOOT_NORMAL; NVIC_SystemReset(); } diff --git a/board/libc.h b/board/libc.h index 62d4d0bd..99eb7a2e 100644 --- a/board/libc.h +++ b/board/libc.h @@ -50,7 +50,7 @@ void enter_critical_section(void) { void exit_critical_section(void) { // this is safe because interrupts are disabled critical_depth -= 1; - if (critical_depth == 0 && interrupts_enabled) { + if ((critical_depth == 0) && interrupts_enabled) { __enable_irq(); } } diff --git a/board/main.c b/board/main.c index 32ea3e8a..cb311004 100644 --- a/board/main.c +++ b/board/main.c @@ -586,7 +586,7 @@ void TIM3_IRQHandler(void) { // turn off the blue LED, turned on by CAN // unless we are in power saving mode - set_led(LED_BLUE, (tcnt&1) && power_save_status == POWER_SAVE_STATUS_ENABLED); + set_led(LED_BLUE, (tcnt & 1) && (power_save_status == POWER_SAVE_STATUS_ENABLED)); // on to the next one tcnt += 1;