2017-06-28 10:27:24 +08:00
|
|
|
#include "config.h"
|
|
|
|
#include "early.h"
|
2017-07-15 12:17:32 +08:00
|
|
|
#include <stdbool.h>
|
2017-07-13 02:25:10 +08:00
|
|
|
|
|
|
|
#define NULL ((void*)0)
|
2017-07-21 14:19:53 +08:00
|
|
|
#define COMPILE_TIME_ASSERT(pred) switch(0){case 0:case pred:;}
|
2017-07-13 02:25:10 +08:00
|
|
|
|
|
|
|
// *** end config ***
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
#include "obj/gitversion.h"
|
|
|
|
|
2017-07-13 02:25:10 +08:00
|
|
|
// debug safety check: is controls allowed?
|
|
|
|
int controls_allowed = 0;
|
2017-04-07 09:11:36 +08:00
|
|
|
int started = 0;
|
|
|
|
|
|
|
|
// optional features
|
2017-07-13 02:25:10 +08:00
|
|
|
int gas_interceptor_detected = 0;
|
2017-04-07 09:11:36 +08:00
|
|
|
int started_signal_detected = 0;
|
|
|
|
|
2017-07-13 02:25:10 +08:00
|
|
|
// detect high on UART
|
|
|
|
// TODO: check for UART high
|
|
|
|
int did_usb_enumerate = 0;
|
|
|
|
|
|
|
|
// Declare puts to supress warning
|
|
|
|
int puts ( const char * str );
|
|
|
|
|
|
|
|
// ********************* includes *********************
|
|
|
|
|
|
|
|
#include "libc.h"
|
|
|
|
#include "gpio.h"
|
|
|
|
#include "uart.h"
|
|
|
|
#include "adc.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "usb.h"
|
2017-07-21 14:14:27 +08:00
|
|
|
#include "safety.h"
|
2017-07-13 02:25:10 +08:00
|
|
|
#include "can.h"
|
|
|
|
#include "spi.h"
|
|
|
|
|
2017-07-21 04:41:21 +08:00
|
|
|
// ********************* debugging *********************
|
|
|
|
|
|
|
|
void debug_ring_callback(uart_ring *ring) {
|
|
|
|
char rcv;
|
|
|
|
while (getc(ring, &rcv)) {
|
|
|
|
putc(ring, rcv);
|
|
|
|
|
|
|
|
// jump to DFU flash
|
|
|
|
if (rcv == 'z') {
|
|
|
|
enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// normal reset
|
|
|
|
if (rcv == 'x') {
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable CDP mode
|
|
|
|
if (rcv == 'C') {
|
|
|
|
puts("switching USB to CDP mode\n");
|
|
|
|
set_usb_power_mode(USB_POWER_CDP);
|
|
|
|
}
|
|
|
|
if (rcv == 'c') {
|
|
|
|
puts("switching USB to client mode\n");
|
|
|
|
set_usb_power_mode(USB_POWER_CLIENT);
|
|
|
|
}
|
|
|
|
if (rcv == 'D') {
|
|
|
|
puts("switching USB to DCP mode\n");
|
|
|
|
set_usb_power_mode(USB_POWER_DCP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
// ***************************** USB port *****************************
|
|
|
|
|
|
|
|
int get_health_pkt(void *dat) {
|
|
|
|
struct __attribute__((packed)) {
|
|
|
|
uint32_t voltage;
|
|
|
|
uint32_t current;
|
|
|
|
uint8_t started;
|
|
|
|
uint8_t controls_allowed;
|
|
|
|
uint8_t gas_interceptor_detected;
|
|
|
|
uint8_t started_signal_detected;
|
|
|
|
uint8_t started_alt;
|
|
|
|
} *health = dat;
|
2017-07-21 14:36:06 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
health->voltage = adc_get(ADCCHAN_VOLTAGE);
|
|
|
|
#ifdef ENABLE_CURRENT_SENSOR
|
|
|
|
health->current = adc_get(ADCCHAN_CURRENT);
|
|
|
|
#else
|
|
|
|
health->current = 0;
|
|
|
|
#endif
|
|
|
|
health->started = started;
|
|
|
|
|
|
|
|
#ifdef PANDA
|
|
|
|
health->started_alt = (GPIOA->IDR & (1 << 1)) == 0;
|
|
|
|
#else
|
|
|
|
health->started_alt = 0;
|
|
|
|
#endif
|
|
|
|
|
2017-07-13 02:25:10 +08:00
|
|
|
health->controls_allowed = controls_allowed;
|
2017-04-07 09:11:36 +08:00
|
|
|
health->gas_interceptor_detected = gas_interceptor_detected;
|
|
|
|
health->started_signal_detected = started_signal_detected;
|
|
|
|
return sizeof(*health);
|
|
|
|
}
|
|
|
|
|
2017-05-02 13:46:12 +08:00
|
|
|
int usb_cb_ep1_in(uint8_t *usbdata, int len, int hardwired) {
|
2017-07-19 01:19:42 +08:00
|
|
|
CAN_FIFOMailBox_TypeDef *reply = (CAN_FIFOMailBox_TypeDef *)usbdata;
|
2017-04-07 09:11:36 +08:00
|
|
|
int ilen = 0;
|
|
|
|
while (ilen < min(len/0x10, 4) && pop(&can_rx_q, &reply[ilen])) ilen++;
|
2017-04-18 09:17:34 +08:00
|
|
|
return ilen*0x10;
|
2017-04-07 09:11:36 +08:00
|
|
|
}
|
|
|
|
|
2017-07-21 14:36:06 +08:00
|
|
|
// send on serial, first byte to select the ring
|
2017-05-02 13:46:12 +08:00
|
|
|
void usb_cb_ep2_out(uint8_t *usbdata, int len, int hardwired) {
|
2017-04-07 09:11:36 +08:00
|
|
|
int i;
|
|
|
|
if (len == 0) return;
|
|
|
|
uart_ring *ur = get_ring_by_number(usbdata[0]);
|
|
|
|
if (!ur) return;
|
2017-07-21 14:36:06 +08:00
|
|
|
if ((usbdata[0] < 2) || safety_tx_lin_hook(usbdata[0]-2, usbdata+1, len-1)) {
|
2017-05-02 11:22:19 +08:00
|
|
|
for (i = 1; i < len; i++) while (!putc(ur, usbdata[i]));
|
|
|
|
}
|
2017-04-07 09:11:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// send on CAN
|
2017-05-02 13:46:12 +08:00
|
|
|
void usb_cb_ep3_out(uint8_t *usbdata, int len, int hardwired) {
|
2017-04-07 09:11:36 +08:00
|
|
|
int dpkt = 0;
|
|
|
|
for (dpkt = 0; dpkt < len; dpkt += 0x10) {
|
|
|
|
uint32_t *tf = (uint32_t*)(&usbdata[dpkt]);
|
|
|
|
|
2017-04-29 00:56:01 +08:00
|
|
|
// make a copy
|
2017-04-07 09:11:36 +08:00
|
|
|
CAN_FIFOMailBox_TypeDef to_push;
|
|
|
|
to_push.RDHR = tf[3];
|
|
|
|
to_push.RDLR = tf[2];
|
2017-04-29 00:56:01 +08:00
|
|
|
to_push.RDTR = tf[1];
|
2017-04-07 09:11:36 +08:00
|
|
|
to_push.RIR = tf[0];
|
|
|
|
|
2017-07-15 03:30:34 +08:00
|
|
|
uint8_t bus_number = (to_push.RDTR >> 4) & CAN_BUS_NUM_MASK;
|
2017-07-21 14:36:06 +08:00
|
|
|
can_send(&to_push, bus_number);
|
2017-04-07 09:11:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 02:25:10 +08:00
|
|
|
void usb_cb_enumeration_complete() {
|
|
|
|
// power down the ESP
|
|
|
|
// this doesn't work and makes the board unflashable
|
|
|
|
// because the ESP spews shit on serial on startup
|
|
|
|
//GPIOC->ODR &= ~(1 << 14);
|
2017-07-21 14:36:06 +08:00
|
|
|
puts("USB enumeration complete\n");
|
2017-07-13 02:25:10 +08:00
|
|
|
did_usb_enumerate = 1;
|
|
|
|
}
|
|
|
|
|
2017-05-02 13:46:12 +08:00
|
|
|
int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
|
2017-04-07 09:11:36 +08:00
|
|
|
int resp_len = 0;
|
|
|
|
uart_ring *ur = NULL;
|
|
|
|
int i;
|
2017-04-18 09:17:34 +08:00
|
|
|
switch (setup->b.bRequest) {
|
2017-07-19 03:29:16 +08:00
|
|
|
// **** 0xc0: get CAN debug info
|
|
|
|
case 0xc0:
|
|
|
|
puts("can tx: "); puth(can_tx_cnt);
|
|
|
|
puts(" txd: "); puth(can_txd_cnt);
|
|
|
|
puts(" rx: "); puth(can_rx_cnt);
|
|
|
|
puts(" err: "); puth(can_err_cnt);
|
|
|
|
puts("\n");
|
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xd0: fetch serial number
|
2017-04-29 07:56:40 +08:00
|
|
|
case 0xd0:
|
|
|
|
#ifdef PANDA
|
2017-05-02 13:59:10 +08:00
|
|
|
// addresses are OTP
|
2017-04-29 08:49:55 +08:00
|
|
|
if (setup->b.wValue.w == 1) {
|
2017-05-02 13:59:10 +08:00
|
|
|
memcpy(resp, (void *)0x1fff79c0, 0x10);
|
2017-04-29 08:49:55 +08:00
|
|
|
resp_len = 0x10;
|
|
|
|
} else {
|
2017-05-02 13:59:10 +08:00
|
|
|
memcpy(resp, (void *)0x1fff79e0, 0x20);
|
2017-04-29 08:49:55 +08:00
|
|
|
resp_len = 0x20;
|
|
|
|
}
|
2017-04-29 07:56:40 +08:00
|
|
|
#endif
|
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xd1: enter bootloader mode
|
2017-04-07 09:11:36 +08:00
|
|
|
case 0xd1:
|
2017-04-29 00:45:58 +08:00
|
|
|
if (hardwired) {
|
|
|
|
enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
2017-04-07 09:11:36 +08:00
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xd2: get health packet
|
2017-04-07 09:11:36 +08:00
|
|
|
case 0xd2:
|
|
|
|
resp_len = get_health_pkt(resp);
|
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xd3: set fan speed
|
2017-04-07 09:11:36 +08:00
|
|
|
case 0xd3:
|
2017-07-21 14:36:06 +08:00
|
|
|
fan_set_speed(setup->b.wValue.w);
|
2017-04-07 09:11:36 +08:00
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xd6: get version
|
|
|
|
case 0xd6:
|
|
|
|
COMPILE_TIME_ASSERT(sizeof(gitversion) <= MAX_RESP_LEN)
|
2017-04-18 09:17:34 +08:00
|
|
|
memcpy(resp, gitversion, sizeof(gitversion));
|
|
|
|
resp_len = sizeof(gitversion);
|
2017-04-07 09:11:36 +08:00
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xd8: reset ST
|
|
|
|
case 0xd8:
|
2017-04-07 09:11:36 +08:00
|
|
|
NVIC_SystemReset();
|
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xd9: set ESP power
|
|
|
|
case 0xd9:
|
2017-04-27 03:38:33 +08:00
|
|
|
if (setup->b.wValue.w == 1) {
|
|
|
|
// on
|
|
|
|
GPIOC->ODR |= (1 << 14);
|
|
|
|
} else {
|
|
|
|
// off
|
|
|
|
GPIOC->ODR &= ~(1 << 14);
|
|
|
|
}
|
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xda: reset ESP, with optional boot mode
|
|
|
|
case 0xda:
|
2017-04-07 09:11:36 +08:00
|
|
|
// pull low for ESP boot mode
|
2017-04-18 09:17:34 +08:00
|
|
|
if (setup->b.wValue.w == 1) {
|
2017-04-07 09:11:36 +08:00
|
|
|
GPIOC->ODR &= ~(1 << 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
// do ESP reset
|
|
|
|
GPIOC->ODR &= ~(1 << 14);
|
|
|
|
delay(1000000);
|
|
|
|
GPIOC->ODR |= (1 << 14);
|
|
|
|
delay(1000000);
|
|
|
|
|
|
|
|
// reset done, no more boot mode
|
|
|
|
// TODO: ESP doesn't seem to listen here
|
2017-04-18 09:17:34 +08:00
|
|
|
if (setup->b.wValue.w == 1) {
|
2017-04-07 09:11:36 +08:00
|
|
|
GPIOC->ODR |= (1 << 5);
|
|
|
|
}
|
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xdb: set GMLAN multiplexing mode
|
|
|
|
case 0xdb:
|
2017-07-19 12:14:38 +08:00
|
|
|
#ifdef PANDA
|
|
|
|
if (setup->b.wValue.w == 1) {
|
|
|
|
// GMLAN ON
|
|
|
|
if (setup->b.wIndex.w == 1) {
|
2017-07-21 14:14:27 +08:00
|
|
|
can_set_gmlan(1);
|
|
|
|
} else if (setup->b.wIndex.w == 2) {
|
|
|
|
can_set_gmlan(2);
|
2017-07-19 12:14:38 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-07-21 14:14:27 +08:00
|
|
|
can_set_gmlan(-1);
|
2017-07-19 12:05:09 +08:00
|
|
|
}
|
2017-07-19 12:14:38 +08:00
|
|
|
#endif
|
2017-04-07 09:11:36 +08:00
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xdc: set safety mode
|
|
|
|
case 0xdc:
|
|
|
|
if (hardwired) {
|
|
|
|
// silent mode if the safety mode is nooutput
|
|
|
|
set_safety_mode(setup->b.wValue.w);
|
|
|
|
}
|
2017-05-02 14:40:49 +08:00
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xdd: enable can forwarding
|
|
|
|
case 0xdd:
|
|
|
|
// wValue = Can Bus Num to forward from
|
|
|
|
// wIndex = Can Bus Num to forward to
|
2017-07-19 12:05:09 +08:00
|
|
|
if (setup->b.wValue.w < BUS_MAX && setup->b.wIndex.w < BUS_MAX &&
|
|
|
|
setup->b.wValue.w != setup->b.wIndex.w) { // set forwarding
|
2017-07-15 03:30:34 +08:00
|
|
|
can_forwarding[setup->b.wValue.w] = setup->b.wIndex.w & CAN_BUS_NUM_MASK;
|
2017-07-19 12:05:09 +08:00
|
|
|
} else if(setup->b.wValue.w < BUS_MAX && setup->b.wIndex.w == 0xFF){ //Clear Forwarding
|
2017-07-15 03:30:34 +08:00
|
|
|
can_forwarding[setup->b.wValue.w] = -1;
|
2017-07-01 04:51:56 +08:00
|
|
|
}
|
2017-07-13 02:25:10 +08:00
|
|
|
break;
|
2017-07-18 10:43:30 +08:00
|
|
|
// **** 0xde: set can bitrate
|
|
|
|
case 0xde:
|
2017-07-19 12:05:09 +08:00
|
|
|
if (setup->b.wValue.w < BUS_MAX) {
|
2017-07-18 10:43:30 +08:00
|
|
|
can_speed[setup->b.wValue.w] = setup->b.wIndex.w;
|
2017-07-19 12:05:09 +08:00
|
|
|
can_init(CAN_NUM_FROM_BUS_NUM(setup->b.wValue.w));
|
2017-07-18 10:43:30 +08:00
|
|
|
}
|
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xe0: uart read
|
|
|
|
case 0xe0:
|
2017-04-18 09:17:34 +08:00
|
|
|
ur = get_ring_by_number(setup->b.wValue.w);
|
2017-04-07 09:11:36 +08:00
|
|
|
if (!ur) break;
|
2017-04-18 09:17:34 +08:00
|
|
|
// read
|
2017-05-02 13:59:10 +08:00
|
|
|
while (resp_len < min(setup->b.wLength.w, MAX_RESP_LEN) && getc(ur, (char*)&resp[resp_len])) {
|
2017-04-18 09:17:34 +08:00
|
|
|
++resp_len;
|
2017-04-07 09:11:36 +08:00
|
|
|
}
|
2017-04-18 16:12:04 +08:00
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xe1: uart set baud rate
|
|
|
|
case 0xe1:
|
2017-04-18 09:17:34 +08:00
|
|
|
ur = get_ring_by_number(setup->b.wValue.w);
|
2017-04-26 09:06:44 +08:00
|
|
|
if (!ur) break;
|
2017-04-18 09:17:34 +08:00
|
|
|
uart_set_baud(ur->uart, setup->b.wIndex.w);
|
2017-04-07 09:11:36 +08:00
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xe2: uart set parity
|
|
|
|
case 0xe2:
|
2017-04-26 06:16:23 +08:00
|
|
|
ur = get_ring_by_number(setup->b.wValue.w);
|
2017-04-26 09:06:44 +08:00
|
|
|
if (!ur) break;
|
2017-04-26 06:16:23 +08:00
|
|
|
switch (setup->b.wIndex.w) {
|
|
|
|
case 0:
|
2017-04-27 09:39:26 +08:00
|
|
|
// disable parity, 8-bit
|
|
|
|
ur->uart->CR1 &= ~(USART_CR1_PCE | USART_CR1_M);
|
2017-04-26 06:16:23 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2017-04-27 09:39:26 +08:00
|
|
|
// even parity, 9-bit
|
2017-04-26 06:16:23 +08:00
|
|
|
ur->uart->CR1 &= ~USART_CR1_PS;
|
2017-04-27 09:39:26 +08:00
|
|
|
ur->uart->CR1 |= USART_CR1_PCE | USART_CR1_M;
|
2017-04-26 06:16:23 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2017-04-27 09:39:26 +08:00
|
|
|
// odd parity, 9-bit
|
2017-04-26 06:16:23 +08:00
|
|
|
ur->uart->CR1 |= USART_CR1_PS;
|
2017-04-27 09:39:26 +08:00
|
|
|
ur->uart->CR1 |= USART_CR1_PCE | USART_CR1_M;
|
2017-04-26 06:16:23 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xe4: uart set baud rate extended
|
|
|
|
case 0xe4:
|
2017-05-30 14:03:11 +08:00
|
|
|
ur = get_ring_by_number(setup->b.wValue.w);
|
|
|
|
if (!ur) break;
|
|
|
|
uart_set_baud(ur->uart, (int)setup->b.wIndex.w*300);
|
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xe5: set CAN loopback (for testing)
|
|
|
|
case 0xe5:
|
2017-07-15 11:25:13 +08:00
|
|
|
can_loopback = (setup->b.wValue.w > 0);
|
2017-07-18 01:48:16 +08:00
|
|
|
can_init_all();
|
2017-07-15 11:25:13 +08:00
|
|
|
break;
|
2017-07-18 10:43:30 +08:00
|
|
|
// **** 0xf0: do k-line wValue pulse on uart2 for Acura
|
|
|
|
case 0xf0:
|
2017-04-18 09:17:34 +08:00
|
|
|
if (setup->b.wValue.w == 1) {
|
2017-04-07 09:11:36 +08:00
|
|
|
GPIOC->ODR &= ~(1 << 10);
|
|
|
|
GPIOC->MODER &= ~GPIO_MODER_MODER10_1;
|
|
|
|
GPIOC->MODER |= GPIO_MODER_MODER10_0;
|
|
|
|
} else {
|
|
|
|
GPIOC->ODR &= ~(1 << 12);
|
|
|
|
GPIOC->MODER &= ~GPIO_MODER_MODER12_1;
|
|
|
|
GPIOC->MODER |= GPIO_MODER_MODER12_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 80; i++) {
|
|
|
|
delay(8000);
|
2017-04-18 09:17:34 +08:00
|
|
|
if (setup->b.wValue.w == 1) {
|
2017-04-07 09:11:36 +08:00
|
|
|
GPIOC->ODR |= (1 << 10);
|
|
|
|
GPIOC->ODR &= ~(1 << 10);
|
|
|
|
} else {
|
|
|
|
GPIOC->ODR |= (1 << 12);
|
|
|
|
GPIOC->ODR &= ~(1 << 12);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 09:17:34 +08:00
|
|
|
if (setup->b.wValue.w == 1) {
|
2017-04-07 09:11:36 +08:00
|
|
|
GPIOC->MODER &= ~GPIO_MODER_MODER10_0;
|
|
|
|
GPIOC->MODER |= GPIO_MODER_MODER10_1;
|
|
|
|
} else {
|
|
|
|
GPIOC->MODER &= ~GPIO_MODER_MODER12_0;
|
|
|
|
GPIOC->MODER |= GPIO_MODER_MODER12_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
delay(140 * 9000);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
puts("NO HANDLER ");
|
2017-04-18 09:17:34 +08:00
|
|
|
puth(setup->b.bRequest);
|
2017-04-07 09:11:36 +08:00
|
|
|
puts("\n");
|
2017-07-13 02:25:10 +08:00
|
|
|
break;
|
2017-04-07 09:11:36 +08:00
|
|
|
}
|
2017-04-18 09:17:34 +08:00
|
|
|
return resp_len;
|
2017-04-07 09:11:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_SPI
|
|
|
|
|
2017-07-21 14:36:06 +08:00
|
|
|
void spi_cb_handle(uint8_t *data, int len) {
|
2017-04-18 22:34:56 +08:00
|
|
|
memset(spi_tx_buf, 0xaa, 0x44);
|
|
|
|
// data[0] = endpoint
|
|
|
|
// data[2] = length
|
|
|
|
// data[4:] = data
|
|
|
|
int *resp_len = (int*)spi_tx_buf;
|
|
|
|
*resp_len = 0;
|
|
|
|
switch (data[0]) {
|
|
|
|
case 0:
|
|
|
|
// control transfer
|
2017-07-13 02:25:10 +08:00
|
|
|
*resp_len = usb_cb_control_msg((USB_Setup_TypeDef *)(data+4), spi_tx_buf+4, 0);
|
2017-04-18 22:34:56 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// ep 1, read
|
2017-05-02 13:46:12 +08:00
|
|
|
*resp_len = usb_cb_ep1_in(spi_tx_buf+4, 0x40, 0);
|
2017-04-18 22:34:56 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// ep 2, send serial
|
2017-05-02 13:46:12 +08:00
|
|
|
usb_cb_ep2_out(data+4, data[2], 0);
|
2017-04-18 22:34:56 +08:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// ep 3, send CAN
|
2017-05-02 13:46:12 +08:00
|
|
|
usb_cb_ep3_out(data+4, data[2], 0);
|
2017-04-18 22:34:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
spi_tx_dma(spi_tx_buf, 0x44);
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-05-04 06:54:47 +08:00
|
|
|
// signal data is ready by driving low
|
|
|
|
// esp must be configured as input by this point
|
|
|
|
GPIOB->MODER &= ~(GPIO_MODER_MODER0);
|
|
|
|
GPIOB->MODER |= GPIO_MODER_MODER0_0;
|
|
|
|
GPIOB->ODR &= ~(GPIO_ODR_ODR_0);
|
|
|
|
}
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ***************************** main code *****************************
|
|
|
|
|
|
|
|
void __initialize_hardware_early() {
|
|
|
|
early();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2017-07-21 14:14:27 +08:00
|
|
|
__disable_irq();
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
// init devices
|
|
|
|
clock_init();
|
2017-04-18 04:57:34 +08:00
|
|
|
periph_init();
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
detect();
|
|
|
|
gpio_init();
|
|
|
|
|
|
|
|
// enable main uart
|
|
|
|
uart_init(USART2, 115200);
|
|
|
|
|
2017-07-21 14:19:53 +08:00
|
|
|
#ifdef PANDA
|
2017-04-07 09:11:36 +08:00
|
|
|
// enable ESP uart
|
|
|
|
uart_init(USART1, 115200);
|
|
|
|
|
|
|
|
// enable LIN
|
|
|
|
uart_init(UART5, 10400);
|
|
|
|
UART5->CR2 |= USART_CR2_LINEN;
|
|
|
|
uart_init(USART3, 10400);
|
|
|
|
USART3->CR2 |= USART_CR2_LINEN;
|
2017-07-21 14:19:53 +08:00
|
|
|
#endif
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-07-21 04:41:21 +08:00
|
|
|
// print if we have a hardware serial port
|
|
|
|
puts("EXTERNAL ");
|
2017-07-13 02:25:10 +08:00
|
|
|
puth(has_external_debug_serial);
|
2017-07-21 04:41:21 +08:00
|
|
|
puts("\n");
|
2017-07-13 02:25:10 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
// enable USB
|
|
|
|
usb_init();
|
|
|
|
|
2017-07-13 02:25:10 +08:00
|
|
|
// default to silent mode to prevent issues with Ford
|
2017-07-21 14:36:06 +08:00
|
|
|
set_safety_mode(SAFETY_NOOUTPUT);
|
2017-07-18 01:48:16 +08:00
|
|
|
can_init_all();
|
2017-05-31 00:46:21 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
adc_init();
|
|
|
|
|
|
|
|
#ifdef ENABLE_SPI
|
|
|
|
spi_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// set PWM
|
2017-07-21 14:36:06 +08:00
|
|
|
fan_init();
|
|
|
|
fan_set_speed(65535);
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
puts("**** INTERRUPTS ON ****\n");
|
|
|
|
|
|
|
|
__enable_irq();
|
|
|
|
|
2017-04-28 11:32:16 +08:00
|
|
|
puts("OPTCR: "); puth(FLASH->OPTCR); puts("\n");
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
// LED should keep on blinking all the time
|
|
|
|
uint64_t cnt;
|
|
|
|
for (cnt=0;;cnt++) {
|
|
|
|
can_live = pending_can_live;
|
|
|
|
|
|
|
|
// reset this every 16th pass
|
|
|
|
if ((cnt&0xF) == 0) pending_can_live = 0;
|
|
|
|
|
2017-07-21 04:41:21 +08:00
|
|
|
#ifdef DEBUG
|
2017-07-13 02:25:10 +08:00
|
|
|
puts("** blink ");
|
|
|
|
puth(can_rx_q.r_ptr); puts(" "); puth(can_rx_q.w_ptr); puts(" ");
|
|
|
|
puth(can_tx1_q.r_ptr); puts(" "); puth(can_tx1_q.w_ptr); puts(" ");
|
|
|
|
puth(can_tx2_q.r_ptr); puts(" "); puth(can_tx2_q.w_ptr); puts("\n");
|
2017-07-21 04:41:21 +08:00
|
|
|
#endif
|
2017-07-13 02:25:10 +08:00
|
|
|
|
|
|
|
/*puts("voltage: "); puth(adc_get(ADCCHAN_VOLTAGE)); puts(" ");
|
|
|
|
puts("current: "); puth(adc_get(ADCCHAN_CURRENT)); puts("\n");*/
|
|
|
|
|
2017-05-17 01:33:50 +08:00
|
|
|
// set LED to be controls allowed, blue on panda, green on legacy
|
|
|
|
#ifdef PANDA
|
2017-07-13 02:25:10 +08:00
|
|
|
set_led(LED_BLUE, controls_allowed);
|
2017-05-17 01:33:50 +08:00
|
|
|
#else
|
2017-07-13 02:25:10 +08:00
|
|
|
set_led(LED_GREEN, controls_allowed);
|
2017-05-17 01:33:50 +08:00
|
|
|
#endif
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
// blink the red LED
|
|
|
|
set_led(LED_RED, 0);
|
|
|
|
delay(2000000);
|
|
|
|
set_led(LED_RED, 1);
|
|
|
|
delay(2000000);
|
|
|
|
|
2017-05-17 01:33:50 +08:00
|
|
|
// turn off the green LED, turned on by CAN
|
|
|
|
#ifdef PANDA
|
|
|
|
set_led(LED_GREEN, 0);
|
|
|
|
#endif
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
// started logic
|
|
|
|
#ifdef PANDA
|
|
|
|
int started_signal = (GPIOB->IDR & (1 << 12)) == 0;
|
|
|
|
#else
|
|
|
|
int started_signal = (GPIOC->IDR & (1 << 13)) != 0;
|
|
|
|
#endif
|
|
|
|
if (started_signal) { started_signal_detected = 1; }
|
|
|
|
|
|
|
|
if (started_signal || (!started_signal_detected && can_live)) {
|
|
|
|
started = 1;
|
|
|
|
|
|
|
|
// turn on fan at half speed
|
2017-07-21 14:36:06 +08:00
|
|
|
fan_set_speed(32768);
|
2017-04-07 09:11:36 +08:00
|
|
|
} else {
|
|
|
|
started = 0;
|
|
|
|
|
|
|
|
// turn off fan
|
2017-07-21 14:36:06 +08:00
|
|
|
fan_set_speed(0);
|
2017-04-07 09:11:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-07-19 01:19:42 +08:00
|
|
|
|