Skip to content

Commit

Permalink
Merge pull request commaai#226 from commaai/misra_15_5
Browse files Browse the repository at this point in the history
Misra 15.5: A function should have a single point of exit at the end
  • Loading branch information
geohot authored Jun 27, 2019
2 parents 4886b6b + ec3d038 commit d972b87
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 148 deletions.
114 changes: 55 additions & 59 deletions board/drivers/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,29 +130,24 @@ void can_set_speed(uint8_t can_number) {
CAN_TypeDef *CAN = CANIF_FROM_CAN_NUM(can_number);
uint8_t bus_number = BUS_NUM_FROM_CAN_NUM(can_number);

while (true) {
if (llcan_set_speed(CAN, can_speed[bus_number], can_loopback, can_silent & (1 << can_number))) {
return;
}

if (!llcan_set_speed(CAN, can_speed[bus_number], can_loopback, can_silent & (1 << can_number))) {
puts("CAN init FAILED!!!!!\n");
puth(can_number); puts(" ");
puth(BUS_NUM_FROM_CAN_NUM(can_number)); puts("\n");
return;
}
}

void can_init(uint8_t can_number) {
if (can_number == 0xff) return;
if (can_number != 0xff) {
CAN_TypeDef *CAN = CANIF_FROM_CAN_NUM(can_number);
set_can_enable(CAN, 1);
can_set_speed(can_number);

CAN_TypeDef *CAN = CANIF_FROM_CAN_NUM(can_number);
set_can_enable(CAN, 1);
can_set_speed(can_number);
llcan_init(CAN);

llcan_init(CAN);

// in case there are queued up messages
process_can(can_number);
// in case there are queued up messages
process_can(can_number);
}
}

void can_init_all(void) {
Expand Down Expand Up @@ -234,57 +229,58 @@ void can_sce(CAN_TypeDef *CAN) {
// ***************************** CAN *****************************

void process_can(uint8_t can_number) {
if (can_number == 0xff) return;

enter_critical_section();

CAN_TypeDef *CAN = CANIF_FROM_CAN_NUM(can_number);
uint8_t bus_number = BUS_NUM_FROM_CAN_NUM(can_number);

// check for empty mailbox
CAN_FIFOMailBox_TypeDef to_send;
if ((CAN->TSR & CAN_TSR_TME0) == CAN_TSR_TME0) {
// add successfully transmitted message to my fifo
if ((CAN->TSR & CAN_TSR_RQCP0) == CAN_TSR_RQCP0) {
can_txd_cnt += 1;

if ((CAN->TSR & CAN_TSR_TXOK0) == CAN_TSR_TXOK0) {
CAN_FIFOMailBox_TypeDef to_push;
to_push.RIR = CAN->sTxMailBox[0].TIR;
to_push.RDTR = (CAN->sTxMailBox[0].TDTR & 0xFFFF000F) | ((CAN_BUS_RET_FLAG | bus_number) << 4);
to_push.RDLR = CAN->sTxMailBox[0].TDLR;
to_push.RDHR = CAN->sTxMailBox[0].TDHR;
can_push(&can_rx_q, &to_push);
}

if ((CAN->TSR & CAN_TSR_TERR0) == CAN_TSR_TERR0) {
#ifdef DEBUG
puts("CAN TX ERROR!\n");
#endif
if (can_number != 0xff) {

enter_critical_section();

CAN_TypeDef *CAN = CANIF_FROM_CAN_NUM(can_number);
uint8_t bus_number = BUS_NUM_FROM_CAN_NUM(can_number);

// check for empty mailbox
CAN_FIFOMailBox_TypeDef to_send;
if ((CAN->TSR & CAN_TSR_TME0) == CAN_TSR_TME0) {
// add successfully transmitted message to my fifo
if ((CAN->TSR & CAN_TSR_RQCP0) == CAN_TSR_RQCP0) {
can_txd_cnt += 1;

if ((CAN->TSR & CAN_TSR_TXOK0) == CAN_TSR_TXOK0) {
CAN_FIFOMailBox_TypeDef to_push;
to_push.RIR = CAN->sTxMailBox[0].TIR;
to_push.RDTR = (CAN->sTxMailBox[0].TDTR & 0xFFFF000F) | ((CAN_BUS_RET_FLAG | bus_number) << 4);
to_push.RDLR = CAN->sTxMailBox[0].TDLR;
to_push.RDHR = CAN->sTxMailBox[0].TDHR;
can_push(&can_rx_q, &to_push);
}

if ((CAN->TSR & CAN_TSR_TERR0) == CAN_TSR_TERR0) {
#ifdef DEBUG
puts("CAN TX ERROR!\n");
#endif
}

if ((CAN->TSR & CAN_TSR_ALST0) == CAN_TSR_ALST0) {
#ifdef DEBUG
puts("CAN TX ARBITRATION LOST!\n");
#endif
}

// clear interrupt
// careful, this can also be cleared by requesting a transmission
CAN->TSR |= CAN_TSR_RQCP0;
}

if ((CAN->TSR & CAN_TSR_ALST0) == CAN_TSR_ALST0) {
#ifdef DEBUG
puts("CAN TX ARBITRATION LOST!\n");
#endif
if (can_pop(can_queues[bus_number], &to_send)) {
can_tx_cnt += 1;
// only send if we have received a packet
CAN->sTxMailBox[0].TDLR = to_send.RDLR;
CAN->sTxMailBox[0].TDHR = to_send.RDHR;
CAN->sTxMailBox[0].TDTR = to_send.RDTR;
CAN->sTxMailBox[0].TIR = to_send.RIR;
}

// clear interrupt
// careful, this can also be cleared by requesting a transmission
CAN->TSR |= CAN_TSR_RQCP0;
}

if (can_pop(can_queues[bus_number], &to_send)) {
can_tx_cnt += 1;
// only send if we have received a packet
CAN->sTxMailBox[0].TDLR = to_send.RDLR;
CAN->sTxMailBox[0].TDHR = to_send.RDHR;
CAN->sTxMailBox[0].TDTR = to_send.RDTR;
CAN->sTxMailBox[0].TIR = to_send.RIR;
}
exit_critical_section();
}

exit_critical_section();
}

// CAN receive handlers
Expand Down
21 changes: 11 additions & 10 deletions board/drivers/gmlan_alt.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,19 @@ void TIM4_IRQHandler(void) {
void bitbang_gmlan(CAN_FIFOMailBox_TypeDef *to_bang) {
gmlan_alt_mode = BITBANG;
// TODO: make failure less silent
if (gmlan_sendmax != -1) return;
if (gmlan_sendmax == -1) {

int len = get_bit_message(pkt_stuffed, to_bang);
gmlan_fail_count = 0;
gmlan_silent_count = 0;
gmlan_sending = 0;
gmlan_sendmax = len;
int len = get_bit_message(pkt_stuffed, to_bang);
gmlan_fail_count = 0;
gmlan_silent_count = 0;
gmlan_sending = 0;
gmlan_sendmax = len;

// setup for bitbang loop
set_bitbanged_gmlan(1); // recessive
set_gpio_mode(GPIOB, 13, MODE_OUTPUT);
// setup for bitbang loop
set_bitbanged_gmlan(1); // recessive
set_gpio_mode(GPIOB, 13, MODE_OUTPUT);

setup_timer4();
setup_timer4();
}
}

5 changes: 3 additions & 2 deletions board/drivers/llcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ bool llcan_set_speed(CAN_TypeDef *CAN, uint32_t speed, bool loopback, bool silen

#define CAN_TIMEOUT 1000000
int tmp = 0;
bool ret = false;
while(((CAN->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) && (tmp < CAN_TIMEOUT)) tmp++;
if (tmp < CAN_TIMEOUT) {
return true;
ret = true;
}

return false;
return ret;
}

void llcan_init(CAN_TypeDef *CAN) {
Expand Down
4 changes: 2 additions & 2 deletions board/drivers/llgpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ void set_gpio_mode(GPIO_TypeDef *GPIO, int pin, int mode) {
GPIO->MODER = tmp;
}

void set_gpio_output(GPIO_TypeDef *GPIO, int pin, int val) {
if (val != 0) {
void set_gpio_output(GPIO_TypeDef *GPIO, int pin, bool enabled) {
if (enabled) {
GPIO->ODR |= (1 << pin);
} else {
GPIO->ODR &= ~(1 << pin);
Expand Down
17 changes: 12 additions & 5 deletions board/drivers/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,25 @@ uart_ring debug_ring = { .w_ptr_tx = 0, .r_ptr_tx = 0,


uart_ring *get_ring_by_number(int a) {
uart_ring *ring = NULL;
switch(a) {
case 0:
return &debug_ring;
ring = &debug_ring;
break;
case 1:
return &esp_ring;
ring = &esp_ring;
break;
case 2:
return &lin1_ring;
ring = &lin1_ring;
break;
case 3:
return &lin2_ring;
ring = &lin2_ring;
break;
default:
return NULL;
ring = NULL;
break;
}
return ring;
}

// ***************************** serial port *****************************
Expand Down
6 changes: 4 additions & 2 deletions board/drivers/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,13 @@ void usb_reset(void) {
}

char to_hex_char(int a) {
char ret;
if (a < 10) {
return '0' + a;
ret = '0' + a;
} else {
return 'a' + (a-10);
ret = 'a' + (a - 10);
}
return ret;
}

void usb_setup(void) {
Expand Down
6 changes: 3 additions & 3 deletions board/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void periph_init(void) {

// ********************* setters *********************

void set_can_enable(CAN_TypeDef *CAN, int enabled) {
void set_can_enable(CAN_TypeDef *CAN, bool enabled) {
// enable CAN busses
if (CAN == CAN1) {
#ifdef PANDA
Expand Down Expand Up @@ -139,13 +139,13 @@ void set_can_enable(CAN_TypeDef *CAN, int enabled) {
#endif

void set_led(int led_num, int on) {
if (led_num == -1) return;

if (led_num != -1) {
#ifdef PANDA
set_gpio_output(GPIOC, led_num, !on);
#else
set_gpio_output(GPIOB, led_num, !on);
#endif
}
}

void set_can_mode(int can, bool use_gmlan) {
Expand Down
8 changes: 6 additions & 2 deletions board/libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ void *memcpy(void *dest, const void *src, unsigned int n) {

int memcmp(const void * ptr1, const void * ptr2, unsigned int num) {
unsigned int i;
int ret = 0;
for (i = 0; i < num; i++) {
if ( ((uint8_t*)ptr1)[i] != ((uint8_t*)ptr2)[i] ) return -1;
if ( ((uint8_t*)ptr1)[i] != ((uint8_t*)ptr2)[i] ) {
ret = -1;
break;
}
}
return 0;
return ret;
}

// ********************* IRQ helpers *********************
Expand Down
23 changes: 10 additions & 13 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void debug_ring_callback(uart_ring *ring) {

// ***************************** started logic *****************************

int is_gpio_started(void) {
bool is_gpio_started(void) {
// ignition is on PA1
return (GPIOA->IDR & (1U << 1)) == 0;
}
Expand All @@ -80,11 +80,8 @@ void EXTI1_IRQHandler(void) {
delay(100000);

// set power savings mode here
if (is_gpio_started() == 1) {
power_save_disable();
} else {
power_save_enable();
}
int power_save_state = is_gpio_started() ? POWER_SAVE_STATUS_DISABLED : POWER_SAVE_STATUS_ENABLED;
set_power_save_state(power_save_state);
EXTI->PR = (1U << 1);
}
}
Expand Down Expand Up @@ -153,11 +150,11 @@ int usb_cb_ep1_in(uint8_t *usbdata, int len, bool hardwired) {
// send on serial, first byte to select the ring
void usb_cb_ep2_out(uint8_t *usbdata, int len, bool hardwired) {
UNUSED(hardwired);
if (len == 0) return;
uart_ring *ur = get_ring_by_number(usbdata[0]);
if (!ur) return;
if ((usbdata[0] < 2) || safety_tx_lin_hook(usbdata[0]-2, usbdata+1, len-1)) {
for (int i = 1; i < len; i++) while (!putc(ur, usbdata[i]));
if ((len != 0) && (ur != NULL)) {
if ((usbdata[0] < 2) || safety_tx_lin_hook(usbdata[0]-2, usbdata+1, len-1)) {
for (int i = 1; i < len; i++) while (!putc(ur, usbdata[i]));
}
}
}

Expand Down Expand Up @@ -294,7 +291,7 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired)
if (safety_ignition_hook() != -1) {
// if the ignition hook depends on something other than the started GPIO
// we have to disable power savings (fix for GM and Tesla)
power_save_disable();
set_power_save_state(POWER_SAVE_STATUS_DISABLED);
}
#ifndef EON
// always LIVE on EON
Expand Down Expand Up @@ -676,8 +673,8 @@ int main(void) {
set_esp_mode(ESP_DISABLED);
}
// only enter power save after the first cycle
/*if (is_gpio_started() == 0) {
power_save_enable();
/*if (is_gpio_started()) {
set_power_save_state(POWER_SAVE_STATUS_ENABLED);
}*/
// interrupt on started line
started_interrupt_init();
Expand Down
Loading

0 comments on commit d972b87

Please sign in to comment.