You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					353 lines
				
				8.0 KiB
			
		
		
			
		
	
	
					353 lines
				
				8.0 KiB
			| 
								 
											6 years ago
										 
									 | 
							
								// ********************* Includes *********************
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#include "../config.h"
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#include "libc.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "main_declarations.h"
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#include "critical.h"
							 | 
						||
| 
								 | 
							
								#include "faults.h"
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#include "drivers/registers.h"
							 | 
						||
| 
								 | 
							
								#include "drivers/interrupts.h"
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#include "drivers/llcan.h"
							 | 
						||
| 
								 | 
							
								#include "drivers/llgpio.h"
							 | 
						||
| 
								 | 
							
								#include "drivers/adc.h"
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "board.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "drivers/clock.h"
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#include "drivers/dac.h"
							 | 
						||
| 
								 | 
							
								#include "drivers/timer.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "gpio.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define CAN CAN1
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								//#define PEDAL_USB
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef PEDAL_USB
							 | 
						||
| 
								 | 
							
								  #include "drivers/uart.h"
							 | 
						||
| 
								 | 
							
								  #include "drivers/usb.h"
							 | 
						||
| 
								 | 
							
								#else
							 | 
						||
| 
								 | 
							
								  // no serial either
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  void puts(const char *a) {
							 | 
						||
| 
								 | 
							
								    UNUSED(a);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  void puth(unsigned int i) {
							 | 
						||
| 
								 | 
							
								    UNUSED(i);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  void puth2(unsigned int i) {
							 | 
						||
| 
								 | 
							
								    UNUSED(i);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define ENTER_BOOTLOADER_MAGIC 0xdeadbeef
							 | 
						||
| 
								 | 
							
								uint32_t enter_bootloader_mode;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								// cppcheck-suppress unusedFunction ; used in headers not included in cppcheck
							 | 
						||
| 
								 | 
							
								void __initialize_hardware_early(void) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  early();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// ********************* serial debugging *********************
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef PEDAL_USB
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void debug_ring_callback(uart_ring *ring) {
							 | 
						||
| 
								 | 
							
								  char rcv;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  while (getc(ring, &rcv) != 0) {
							 | 
						||
| 
								 | 
							
								    (void)putc(ring, rcv);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								int usb_cb_ep1_in(void *usbdata, int len, bool hardwired) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  UNUSED(usbdata);
							 | 
						||
| 
								 | 
							
								  UNUSED(len);
							 | 
						||
| 
								 | 
							
								  UNUSED(hardwired);
							 | 
						||
| 
								 | 
							
								  return 0;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								void usb_cb_ep2_out(void *usbdata, int len, bool hardwired) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  UNUSED(usbdata);
							 | 
						||
| 
								 | 
							
								  UNUSED(len);
							 | 
						||
| 
								 | 
							
								  UNUSED(hardwired);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								void usb_cb_ep3_out(void *usbdata, int len, bool hardwired) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  UNUSED(usbdata);
							 | 
						||
| 
								 | 
							
								  UNUSED(len);
							 | 
						||
| 
								 | 
							
								  UNUSED(hardwired);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								void usb_cb_enumeration_complete(void) {}
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  UNUSED(hardwired);
							 | 
						||
| 
								 | 
							
								  unsigned int resp_len = 0;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  uart_ring *ur = NULL;
							 | 
						||
| 
								 | 
							
								  switch (setup->b.bRequest) {
							 | 
						||
| 
								 | 
							
								    // **** 0xe0: uart read
							 | 
						||
| 
								 | 
							
								    case 0xe0:
							 | 
						||
| 
								 | 
							
								      ur = get_ring_by_number(setup->b.wValue.w);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      if (!ur) {
							 | 
						||
| 
								 | 
							
								        break;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      // read
							 | 
						||
| 
								 | 
							
								      while ((resp_len < MIN(setup->b.wLength.w, MAX_RESP_LEN)) &&
							 | 
						||
| 
								 | 
							
								                         getc(ur, (char*)&resp[resp_len])) {
							 | 
						||
| 
								 | 
							
								        ++resp_len;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								      break;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    default:
							 | 
						||
| 
								 | 
							
								      puts("NO HANDLER ");
							 | 
						||
| 
								 | 
							
								      puth(setup->b.bRequest);
							 | 
						||
| 
								 | 
							
								      puts("\n");
							 | 
						||
| 
								 | 
							
								      break;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  return resp_len;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// ***************************** pedal can checksum *****************************
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t pedal_checksum(uint8_t *dat, int len) {
							 | 
						||
| 
								 | 
							
								  uint8_t crc = 0xFF;
							 | 
						||
| 
								 | 
							
								  uint8_t poly = 0xD5; // standard crc8
							 | 
						||
| 
								 | 
							
								  int i, j;
							 | 
						||
| 
								 | 
							
								  for (i = len - 1; i >= 0; i--) {
							 | 
						||
| 
								 | 
							
								    crc ^= dat[i];
							 | 
						||
| 
								 | 
							
								    for (j = 0; j < 8; j++) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      if ((crc & 0x80U) != 0U) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        crc = (uint8_t)((crc << 1) ^ poly);
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								      else {
							 | 
						||
| 
								 | 
							
								        crc <<= 1;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  return crc;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// ***************************** can port *****************************
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// addresses to be used on CAN
							 | 
						||
| 
								 | 
							
								#define CAN_GAS_INPUT  0x200
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#define CAN_GAS_OUTPUT 0x201U
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#define CAN_GAS_SIZE 6
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#define COUNTER_CYCLE 0xFU
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								void CAN1_TX_IRQ_Handler(void) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  // clear interrupt
							 | 
						||
| 
								 | 
							
								  CAN->TSR |= CAN_TSR_RQCP0;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// two independent values
							 | 
						||
| 
								 | 
							
								uint16_t gas_set_0 = 0;
							 | 
						||
| 
								 | 
							
								uint16_t gas_set_1 = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#define MAX_TIMEOUT 10U
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								uint32_t timeout = 0;
							 | 
						||
| 
								 | 
							
								uint32_t current_index = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#define NO_FAULT 0U
							 | 
						||
| 
								 | 
							
								#define FAULT_BAD_CHECKSUM 1U
							 | 
						||
| 
								 | 
							
								#define FAULT_SEND 2U
							 | 
						||
| 
								 | 
							
								#define FAULT_SCE 3U
							 | 
						||
| 
								 | 
							
								#define FAULT_STARTUP 4U
							 | 
						||
| 
								 | 
							
								#define FAULT_TIMEOUT 5U
							 | 
						||
| 
								 | 
							
								#define FAULT_INVALID 6U
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								uint8_t state = FAULT_STARTUP;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								void CAN1_RX0_IRQ_Handler(void) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  while ((CAN->RF0R & CAN_RF0R_FMP0) != 0) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    #ifdef DEBUG
							 | 
						||
| 
								 | 
							
								      puts("CAN RX\n");
							 | 
						||
| 
								 | 
							
								    #endif
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    int address = CAN->sFIFOMailBox[0].RIR >> 21;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    if (address == CAN_GAS_INPUT) {
							 | 
						||
| 
								 | 
							
								      // softloader entry
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      if (GET_BYTES_04(&CAN->sFIFOMailBox[0]) == 0xdeadface) {
							 | 
						||
| 
								 | 
							
								        if (GET_BYTES_48(&CAN->sFIFOMailBox[0]) == 0x0ab00b1e) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								          enter_bootloader_mode = ENTER_SOFTLOADER_MAGIC;
							 | 
						||
| 
								 | 
							
								          NVIC_SystemReset();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        } else if (GET_BYTES_48(&CAN->sFIFOMailBox[0]) == 0x02b00b1e) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								          enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
							 | 
						||
| 
								 | 
							
								          NVIC_SystemReset();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								          puts("Failed entering Softloader or Bootloader\n");
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        }
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      // normal packet
							 | 
						||
| 
								 | 
							
								      uint8_t dat[8];
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      for (int i=0; i<8; i++) {
							 | 
						||
| 
								 | 
							
								        dat[i] = GET_BYTE(&CAN->sFIFOMailBox[0], i);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      }
							 | 
						||
| 
								 | 
							
								      uint16_t value_0 = (dat[0] << 8) | dat[1];
							 | 
						||
| 
								 | 
							
								      uint16_t value_1 = (dat[2] << 8) | dat[3];
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      bool enable = ((dat[4] >> 7) & 1U) != 0U;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      uint8_t index = dat[4] & COUNTER_CYCLE;
							 | 
						||
| 
								 | 
							
								      if (pedal_checksum(dat, CAN_GAS_SIZE - 1) == dat[5]) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        if (((current_index + 1U) & COUNTER_CYCLE) == index) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								          #ifdef DEBUG
							 | 
						||
| 
								 | 
							
								            puts("setting gas ");
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								            puth(value_0);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								            puts("\n");
							 | 
						||
| 
								 | 
							
								          #endif
							 | 
						||
| 
								 | 
							
								          if (enable) {
							 | 
						||
| 
								 | 
							
								            gas_set_0 = value_0;
							 | 
						||
| 
								 | 
							
								            gas_set_1 = value_1;
							 | 
						||
| 
								 | 
							
								          } else {
							 | 
						||
| 
								 | 
							
								            // clear the fault state if values are 0
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								            if ((value_0 == 0U) && (value_1 == 0U)) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								              state = NO_FAULT;
							 | 
						||
| 
								 | 
							
								            } else {
							 | 
						||
| 
								 | 
							
								              state = FAULT_INVALID;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								            gas_set_0 = 0;
							 | 
						||
| 
								 | 
							
								            gas_set_1 = 0;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								          }
							 | 
						||
| 
								 | 
							
								          // clear the timeout
							 | 
						||
| 
								 | 
							
								          timeout = 0;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        current_index = index;
							 | 
						||
| 
								 | 
							
								      } else {
							 | 
						||
| 
								 | 
							
								        // wrong checksum = fault
							 | 
						||
| 
								 | 
							
								        state = FAULT_BAD_CHECKSUM;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    // next
							 | 
						||
| 
								 | 
							
								    CAN->RF0R |= CAN_RF0R_RFOM0;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								void CAN1_SCE_IRQ_Handler(void) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  state = FAULT_SCE;
							 | 
						||
| 
								 | 
							
								  llcan_clear_send(CAN);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								uint32_t pdl0 = 0;
							 | 
						||
| 
								 | 
							
								uint32_t pdl1 = 0;
							 | 
						||
| 
								 | 
							
								unsigned int pkt_idx = 0;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								int led_value = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								void TIM3_IRQ_Handler(void) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  #ifdef DEBUG
							 | 
						||
| 
								 | 
							
								    puth(TIM3->CNT);
							 | 
						||
| 
								 | 
							
								    puts(" ");
							 | 
						||
| 
								 | 
							
								    puth(pdl0);
							 | 
						||
| 
								 | 
							
								    puts(" ");
							 | 
						||
| 
								 | 
							
								    puth(pdl1);
							 | 
						||
| 
								 | 
							
								    puts("\n");
							 | 
						||
| 
								 | 
							
								  #endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // check timer for sending the user pedal and clearing the CAN
							 | 
						||
| 
								 | 
							
								  if ((CAN->TSR & CAN_TSR_TME0) == CAN_TSR_TME0) {
							 | 
						||
| 
								 | 
							
								    uint8_t dat[8];
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    dat[0] = (pdl0 >> 8) & 0xFFU;
							 | 
						||
| 
								 | 
							
								    dat[1] = (pdl0 >> 0) & 0xFFU;
							 | 
						||
| 
								 | 
							
								    dat[2] = (pdl1 >> 8) & 0xFFU;
							 | 
						||
| 
								 | 
							
								    dat[3] = (pdl1 >> 0) & 0xFFU;
							 | 
						||
| 
								 | 
							
								    dat[4] = ((state & 0xFU) << 4) | pkt_idx;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    dat[5] = pedal_checksum(dat, CAN_GAS_SIZE - 1);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    CAN->sTxMailBox[0].TDLR = dat[0] | (dat[1] << 8) | (dat[2] << 16) | (dat[3] << 24);
							 | 
						||
| 
								 | 
							
								    CAN->sTxMailBox[0].TDHR = dat[4] | (dat[5] << 8);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    CAN->sTxMailBox[0].TDTR = 6;  // len of packet is 5
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    CAN->sTxMailBox[0].TIR = (CAN_GAS_OUTPUT << 21) | 1U;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    ++pkt_idx;
							 | 
						||
| 
								 | 
							
								    pkt_idx &= COUNTER_CYCLE;
							 | 
						||
| 
								 | 
							
								  } else {
							 | 
						||
| 
								 | 
							
								    // old can packet hasn't sent!
							 | 
						||
| 
								 | 
							
								    state = FAULT_SEND;
							 | 
						||
| 
								 | 
							
								    #ifdef DEBUG
							 | 
						||
| 
								 | 
							
								      puts("CAN MISS\n");
							 | 
						||
| 
								 | 
							
								    #endif
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // blink the LED
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  current_board->set_led(LED_GREEN, led_value);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  led_value = !led_value;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  TIM3->SR = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // up timeout for gas set
							 | 
						||
| 
								 | 
							
								  if (timeout == MAX_TIMEOUT) {
							 | 
						||
| 
								 | 
							
								    state = FAULT_TIMEOUT;
							 | 
						||
| 
								 | 
							
								  } else {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    timeout += 1U;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// ***************************** main code *****************************
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								void pedal(void) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  // read/write
							 | 
						||
| 
								 | 
							
								  pdl0 = adc_get(ADCCHAN_ACCEL0);
							 | 
						||
| 
								 | 
							
								  pdl1 = adc_get(ADCCHAN_ACCEL1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // write the pedal to the DAC
							 | 
						||
| 
								 | 
							
								  if (state == NO_FAULT) {
							 | 
						||
| 
								 | 
							
								    dac_set(0, MAX(gas_set_0, pdl0));
							 | 
						||
| 
								 | 
							
								    dac_set(1, MAX(gas_set_1, pdl1));
							 | 
						||
| 
								 | 
							
								  } else {
							 | 
						||
| 
								 | 
							
								    dac_set(0, pdl0);
							 | 
						||
| 
								 | 
							
								    dac_set(1, pdl1);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  watchdog_feed();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								int main(void) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  // Init interrupt table
							 | 
						||
| 
								 | 
							
								  init_interrupts(true);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  REGISTER_INTERRUPT(CAN1_TX_IRQn, CAN1_TX_IRQ_Handler, CAN_INTERRUPT_RATE, FAULT_INTERRUPT_RATE_CAN_1)
							 | 
						||
| 
								 | 
							
								  REGISTER_INTERRUPT(CAN1_RX0_IRQn, CAN1_RX0_IRQ_Handler, CAN_INTERRUPT_RATE, FAULT_INTERRUPT_RATE_CAN_1)
							 | 
						||
| 
								 | 
							
								  REGISTER_INTERRUPT(CAN1_SCE_IRQn, CAN1_SCE_IRQ_Handler, CAN_INTERRUPT_RATE, FAULT_INTERRUPT_RATE_CAN_1)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // Should run at around 732Hz (see init below)
							 | 
						||
| 
								 | 
							
								  REGISTER_INTERRUPT(TIM3_IRQn, TIM3_IRQ_Handler, 1000U, FAULT_INTERRUPT_RATE_TIM3)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  disable_interrupts();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  // init devices
							 | 
						||
| 
								 | 
							
								  clock_init();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  peripherals_init();
							 | 
						||
| 
								 | 
							
								  detect_configuration();
							 | 
						||
| 
								 | 
							
								  detect_board_type();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  // init board
							 | 
						||
| 
								 | 
							
								  current_board->init();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#ifdef PEDAL_USB
							 | 
						||
| 
								 | 
							
								  // enable USB
							 | 
						||
| 
								 | 
							
								  usb_init();
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // pedal stuff
							 | 
						||
| 
								 | 
							
								  dac_init();
							 | 
						||
| 
								 | 
							
								  adc_init();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // init can
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  bool llcan_speed_set = llcan_set_speed(CAN1, 5000, false, false);
							 | 
						||
| 
								 | 
							
								  if (!llcan_speed_set) {
							 | 
						||
| 
								 | 
							
								    puts("Failed to set llcan speed");
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  llcan_init(CAN1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // 48mhz / 65536 ~= 732
							 | 
						||
| 
								 | 
							
								  timer_init(TIM3, 15);
							 | 
						||
| 
								 | 
							
								  NVIC_EnableIRQ(TIM3_IRQn);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  watchdog_init();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  puts("**** INTERRUPTS ON ****\n");
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  enable_interrupts();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  // main pedal loop
							 | 
						||
| 
								 | 
							
								  while (1) {
							 | 
						||
| 
								 | 
							
								    pedal();
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  return 0;
							 | 
						||
| 
								 | 
							
								}
							 |