diff --git a/board/drivers/can.h b/board/drivers/can.h index d1b9de1be4b5e1..dbc1eb85d9d3d3 100644 --- a/board/drivers/can.h +++ b/board/drivers/can.h @@ -156,48 +156,45 @@ void can_init_all(void) { } } -void can_set_gmlan(int bus) { - if ((bus == -1) || (bus != can_num_lookup[3])) { - // GMLAN OFF - switch (can_num_lookup[3]) { +void can_set_gmlan(uint8_t bus) { + + // first, disable GMLAN on prev bus + uint8_t prev_bus = can_num_lookup[3]; + if (bus != prev_bus) { + switch (prev_bus) { case 1: - puts("disable GMLAN on CAN2\n"); - set_can_mode(1, 0); - bus_lookup[1] = 1; - can_num_lookup[1] = 1; - can_num_lookup[3] = -1; - can_init(1); - break; case 2: - puts("disable GMLAN on CAN3\n"); - set_can_mode(2, 0); - bus_lookup[2] = 2; - can_num_lookup[2] = 2; + puts("Disable GMLAN on CAN"); + puth(prev_bus + 1U); + puts("\n"); + set_can_mode(prev_bus, 0); + bus_lookup[prev_bus] = prev_bus; + can_num_lookup[prev_bus] = prev_bus; can_num_lookup[3] = -1; - can_init(2); + can_init(prev_bus); break; default: - puts("GMLAN bus value invalid\n"); + // GMLAN was not set on either BUS 1 or 2 break; } } - if (bus == 1) { - puts("GMLAN on CAN2\n"); - // GMLAN on CAN2 - set_can_mode(1, 1); - bus_lookup[1] = 3; - can_num_lookup[1] = -1; - can_num_lookup[3] = 1; - can_init(1); - } else if (bus == 2) { - puts("GMLAN on CAN3\n"); - // GMLAN on CAN3 - set_can_mode(2, 1); - bus_lookup[2] = 3; - can_num_lookup[2] = -1; - can_num_lookup[3] = 2; - can_init(2); + // now enable GMLAN on the new bus + switch (bus) { + case 1: + case 2: + puts("Enable GMLAN on CAN"); + puth(bus + 1U); + puts("\n"); + set_can_mode(bus, 1); + bus_lookup[bus] = 3; + can_num_lookup[bus] = -1; + can_num_lookup[3] = bus; + can_init(bus); + break; + default: + puts("GMLAN can only be set on CAN2 or CAN3"); + break; } } diff --git a/board/drivers/gmlan_alt.h b/board/drivers/gmlan_alt.h index c3d5d304e63c48..b362a95bf7eb7a 100644 --- a/board/drivers/gmlan_alt.h +++ b/board/drivers/gmlan_alt.h @@ -196,7 +196,7 @@ void TIM4_IRQHandler(void) { } else { gmlan_silent_count++; } - } else if (gmlan_silent_count == REQUIRED_SILENT_TIME) { + } else { bool retry = 0; // in send loop if ((gmlan_sending > 0) && // not first bit @@ -209,6 +209,8 @@ void TIM4_IRQHandler(void) { } else if ((read == 1) && (gmlan_sending == (gmlan_sendmax - 11))) { // recessive during ACK puts("GMLAN ERR: didn't recv ACK\n"); retry = 1; + } else { + // do not retry } if (retry) { // reset sender (retry after 7 silent) @@ -233,9 +235,7 @@ void TIM4_IRQHandler(void) { } } TIM4->SR = 0; - } //bit bang mode - - else if (gmlan_alt_mode == GPIO_SWITCH) { + } 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) { //it has been more than 1 second since timeout was reset; disable timer and restore the GMLAN output @@ -258,7 +258,9 @@ void TIM4_IRQHandler(void) { } } TIM4->SR = 0; - } //gmlan switch mode + } else { + puts("invalid gmlan_alt_mode\n"); + } } void bitbang_gmlan(CAN_FIFOMailBox_TypeDef *to_bang) { diff --git a/board/drivers/llcan.h b/board/drivers/llcan.h index 5d0b8884583afa..293f40dfe41780 100644 --- a/board/drivers/llcan.h +++ b/board/drivers/llcan.h @@ -8,6 +8,8 @@ // 5000 = 500 kbps #define can_speed_to_prescaler(x) (CAN_PCLK / CAN_QUANTA * 10 / (x)) +void puts(const char *a); + bool llcan_set_speed(CAN_TypeDef *CAN, uint32_t speed, bool loopback, bool silent) { // initialization mode CAN->MCR = CAN_MCR_TTCM | CAN_MCR_INRQ; @@ -70,6 +72,8 @@ void llcan_init(CAN_TypeDef *CAN) { NVIC_EnableIRQ(CAN3_RX0_IRQn); NVIC_EnableIRQ(CAN3_SCE_IRQn); #endif + } else { + puts("Invalid CAN: initialization failed\n"); } } diff --git a/board/drivers/uart.h b/board/drivers/uart.h index 56453715aa701c..6eef563e1e1c88 100644 --- a/board/drivers/uart.h +++ b/board/drivers/uart.h @@ -289,6 +289,8 @@ void uart_init(USART_TypeDef *u, int baud) { NVIC_EnableIRQ(USART3_IRQn); } else if (u == UART5) { NVIC_EnableIRQ(UART5_IRQn); + } else { + // USART type undefined, skip } } diff --git a/board/drivers/usb.h b/board/drivers/usb.h index aadd52811a10dc..ae82b276543d83 100644 --- a/board/drivers/usb.h +++ b/board/drivers/usb.h @@ -793,18 +793,19 @@ void usb_irqhandler(void) { if ((gintsts & USB_OTG_GINTSTS_RXFLVL) != 0) { // 1. Read the Receive status pop register volatile unsigned int rxst = USBx->GRXSTSP; + int status = (rxst & USB_OTG_GRXSTSP_PKTSTS) >> 17; #ifdef DEBUG_USB puts(" RX FIFO:"); puth(rxst); puts(" status: "); - puth((rxst & USB_OTG_GRXSTSP_PKTSTS) >> 17); + puth(status); puts(" len: "); puth((rxst & USB_OTG_GRXSTSP_BCNT) >> 4); puts("\n"); #endif - if (((rxst & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT) { + if (status == STS_DATA_UPDT) { int endpoint = (rxst & USB_OTG_GRXSTSP_EPNUM); int len = (rxst & USB_OTG_GRXSTSP_BCNT) >> 4; (void)USB_ReadPacket(&usbdata, len); @@ -822,13 +823,15 @@ void usb_irqhandler(void) { if (endpoint == 3) { usb_cb_ep3_out(usbdata, len, 1); } - } else if (((rxst & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT) { + } else if (status == STS_SETUP_UPDT) { (void)USB_ReadPacket(&setup, 8); #ifdef DEBUG_USB puts(" setup "); hexdump(&setup, 8); puts("\n"); #endif + } else { + // status is neither STS_DATA_UPDT or STS_SETUP_UPDT, skip } } @@ -903,6 +906,8 @@ void usb_irqhandler(void) { puts("OUTEP3 error "); puth(USBx_OUTEP(3)->DOEPINT); puts("\n"); + } else { + // USBx_OUTEP(3)->DOEPINT is 0, ok to skip } if ((USBx_OUTEP(0)->DOEPINT & USB_OTG_DIEPINT_XFRC) != 0) { diff --git a/board/gpio.h b/board/gpio.h index 26a82d6275e05f..255789a0414b30 100644 --- a/board/gpio.h +++ b/board/gpio.h @@ -127,6 +127,8 @@ void set_can_enable(CAN_TypeDef *CAN, bool enabled) { // CAN3_EN set_gpio_output(GPIOA, 0, !enabled); #endif + } else { + puts("Invalid CAN: enabling failed\n"); } } @@ -171,6 +173,8 @@ void set_can_mode(int can, bool use_gmlan) { set_gpio_alternate(GPIOB, 3, GPIO_AF11_CAN3); set_gpio_alternate(GPIOB, 4, GPIO_AF11_CAN3); #endif + } else { + puts("Invalid CAN: mode setting failed\n"); } } else { if (can == 1) { @@ -190,6 +194,8 @@ void set_can_mode(int can, bool use_gmlan) { set_gpio_alternate(GPIOA, 8, GPIO_AF11_CAN3); set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3); #endif + } else { + puts("Invalid CAN: mode setting failed\n"); } } } diff --git a/board/main.c b/board/main.c index 3e3820e440a874..810b09286b7867 100644 --- a/board/main.c +++ b/board/main.c @@ -285,6 +285,8 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired) can_set_gmlan(1); } else if (setup->b.wIndex.w == 2) { can_set_gmlan(2); + } else { + puts("Invalid bus num for GMLAN CAN set\n"); } } else { can_set_gmlan(-1); @@ -332,6 +334,8 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired) can_set_forwarding(setup->b.wValue.w, setup->b.wIndex.w & CAN_BUS_NUM_MASK); } else if((setup->b.wValue.w < BUS_MAX) && (setup->b.wIndex.w == 0xFF)){ //Clear Forwarding can_set_forwarding(setup->b.wValue.w, -1); + } else { + puts("Invalid CAN bus forwarding\n"); } break; // **** 0xde: set can bitrate @@ -462,6 +466,8 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired) } else if (setup->b.wValue.w < BUS_MAX) { puts("Clearing CAN Tx queue\n"); can_clear(can_queues[setup->b.wValue.w]); + } else { + puts("Clearing CAN CAN ring buffer failed: wrong bus number\n"); } break; // **** 0xf2: Clear UART ring buffer.