2017-06-28 10:27:24 +08:00
|
|
|
#include "config.h"
|
2017-04-07 09:11:36 +08:00
|
|
|
#include "obj/gitversion.h"
|
|
|
|
|
2017-07-13 02:25:10 +08:00
|
|
|
// ********************* includes *********************
|
|
|
|
|
|
|
|
#include "libc.h"
|
2017-07-21 14:14:27 +08:00
|
|
|
#include "safety.h"
|
2017-07-30 23:26:48 +08:00
|
|
|
#include "provision.h"
|
2017-07-21 14:45:48 +08:00
|
|
|
|
2017-07-22 02:30:07 +08:00
|
|
|
#include "drivers/drivers.h"
|
|
|
|
|
2017-07-30 08:53:39 +08:00
|
|
|
#include "drivers/llgpio.h"
|
|
|
|
#include "gpio.h"
|
|
|
|
|
2017-07-21 14:45:48 +08:00
|
|
|
#include "drivers/uart.h"
|
|
|
|
#include "drivers/adc.h"
|
|
|
|
#include "drivers/usb.h"
|
|
|
|
#include "drivers/can.h"
|
|
|
|
#include "drivers/spi.h"
|
2017-07-22 02:48:03 +08:00
|
|
|
#include "drivers/timer.h"
|
|
|
|
|
2017-07-30 08:53:39 +08:00
|
|
|
|
2017-07-22 02:48:03 +08:00
|
|
|
// ***************************** fan *****************************
|
|
|
|
|
|
|
|
void fan_init() {
|
|
|
|
// timer for fan PWM
|
|
|
|
TIM3->CCMR2 = TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1;
|
|
|
|
TIM3->CCER = TIM_CCER_CC3E;
|
|
|
|
timer_init(TIM3, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fan_set_speed(int fan_speed) {
|
|
|
|
TIM3->CCR3 = fan_speed;
|
|
|
|
}
|
2017-07-13 02:25:10 +08:00
|
|
|
|
2017-07-21 14:42:19 +08:00
|
|
|
// ********************* serial debugging *********************
|
2017-07-21 04:41:21 +08:00
|
|
|
|
|
|
|
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-22 02:48:03 +08:00
|
|
|
#ifdef PANDA
|
|
|
|
int started_signal = (GPIOB->IDR & (1 << 12)) == 0;
|
|
|
|
#else
|
|
|
|
int started_signal = (GPIOC->IDR & (1 << 13)) != 0;
|
|
|
|
#endif
|
2017-07-21 14:36:06 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
health->voltage = adc_get(ADCCHAN_VOLTAGE);
|
2017-07-22 02:48:03 +08:00
|
|
|
#ifdef PANDA
|
2017-04-07 09:11:36 +08:00
|
|
|
health->current = adc_get(ADCCHAN_CURRENT);
|
|
|
|
#else
|
|
|
|
health->current = 0;
|
|
|
|
#endif
|
2017-07-22 02:48:03 +08:00
|
|
|
health->started = started_signal;
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
#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;
|
2017-07-22 02:48:03 +08:00
|
|
|
|
|
|
|
// DEPRECATED
|
|
|
|
health->started_signal_detected = 0;
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
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;
|
2017-07-22 02:30:07 +08:00
|
|
|
while (ilen < min(len/0x10, 4) && can_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
|
|
|
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-08-17 17:03:08 +08:00
|
|
|
for (int i = 1; i < len; i++) while (!putc(ur, usbdata[i]));
|
2017-05-02 11:22:19 +08:00
|
|
|
}
|
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() {
|
2017-07-21 14:36:06 +08:00
|
|
|
puts("USB enumeration complete\n");
|
2017-07-13 02:25:10 +08:00
|
|
|
}
|
|
|
|
|
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-07-30 23:26:48 +08:00
|
|
|
get_provision_chunk(resp);
|
|
|
|
resp_len = PROVISION_CHUNK_LEN;
|
2017-04-29 08:49:55 +08:00
|
|
|
}
|
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-07-23 05:18:00 +08:00
|
|
|
// this allows reflashing of the bootstub
|
|
|
|
// so it's blocked over wifi
|
2017-07-25 06:16:22 +08:00
|
|
|
switch (setup->b.wValue.w) {
|
|
|
|
case 0:
|
|
|
|
if (hardwired) {
|
2017-07-28 06:54:55 +08:00
|
|
|
puts("-> entering bootloader\n");
|
2017-07-25 06:16:22 +08:00
|
|
|
enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
2017-07-28 06:54:55 +08:00
|
|
|
puts("-> entering softloader\n");
|
2017-07-25 06:16:22 +08:00
|
|
|
enter_bootloader_mode = ENTER_SOFTLOADER_MAGIC;
|
|
|
|
NVIC_SystemReset();
|
|
|
|
break;
|
2017-04-29 00:45:58 +08:00
|
|
|
}
|
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) {
|
2017-07-23 05:18:00 +08:00
|
|
|
set_esp_mode(ESP_ENABLED);
|
2017-04-27 03:38:33 +08:00
|
|
|
} else {
|
2017-07-23 05:18:00 +08:00
|
|
|
set_esp_mode(ESP_DISABLED);
|
2017-04-27 03:38:33 +08:00
|
|
|
}
|
|
|
|
break;
|
2017-07-18 01:48:16 +08:00
|
|
|
// **** 0xda: reset ESP, with optional boot mode
|
|
|
|
case 0xda:
|
2017-07-23 05:18:00 +08:00
|
|
|
set_esp_mode(ESP_DISABLED);
|
2017-04-07 09:11:36 +08:00
|
|
|
delay(1000000);
|
2017-04-18 09:17:34 +08:00
|
|
|
if (setup->b.wValue.w == 1) {
|
2017-07-23 05:18:00 +08:00
|
|
|
set_esp_mode(ESP_BOOTMODE);
|
|
|
|
} else {
|
|
|
|
set_esp_mode(ESP_ENABLED);
|
2017-04-07 09:11:36 +08:00
|
|
|
}
|
2017-07-23 05:18:00 +08:00
|
|
|
delay(1000000);
|
|
|
|
set_esp_mode(ESP_ENABLED);
|
2017-04-07 09:11:36 +08:00
|
|
|
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) {
|
2017-07-21 14:54:45 +08:00
|
|
|
// might be ignored on rev b panda
|
2017-07-21 14:14:27 +08:00
|
|
|
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:
|
2017-07-23 05:18:00 +08:00
|
|
|
// this is the only way to leave silent mode
|
|
|
|
// and it's blocked over WiFi
|
2017-08-12 07:17:43 +08:00
|
|
|
// Allow ELM security mode to be set over wifi.
|
2017-08-22 05:28:33 +08:00
|
|
|
if (hardwired || setup->b.wValue.w == SAFETY_NOOUTPUT || setup->b.wValue.w == SAFETY_ELM327) {
|
2017-07-21 14:42:19 +08:00
|
|
|
safety_set_mode(setup->b.wValue.w);
|
2017-08-22 05:28:33 +08:00
|
|
|
switch (setup->b.wValue.w) {
|
|
|
|
case SAFETY_NOOUTPUT:
|
|
|
|
can_silent = 0xff;
|
|
|
|
break;
|
|
|
|
case SAFETY_ELM327:
|
|
|
|
can_silent = 0xfe;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
can_silent = 0;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-22 02:30:07 +08:00
|
|
|
can_init_all();
|
2017-07-18 01:48:16 +08:00
|
|
|
}
|
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-22 02:30:07 +08:00
|
|
|
can_set_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-22 02:30:07 +08:00
|
|
|
can_set_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-07-22 02:48:03 +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;
|
2017-08-17 17:03:08 +08:00
|
|
|
// **** 0xf1: Clear CAN ring buffer.
|
|
|
|
case 0xf1:
|
|
|
|
if (setup->b.wValue.w == 0xFFFF) {
|
|
|
|
puts("Clearing CAN Rx queue\n");
|
|
|
|
can_clear(&can_rx_q);
|
|
|
|
} else if (setup->b.wValue.w < BUS_MAX) {
|
|
|
|
puts("Clearing CAN Tx queue\n");
|
|
|
|
can_clear(can_queues[setup->b.wValue.w]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xf2: Clear UART ring buffer.
|
|
|
|
case 0xf2:
|
|
|
|
{
|
|
|
|
uart_ring * rb = get_ring_by_number(setup->b.wValue.w);
|
|
|
|
if (rb) {
|
|
|
|
puts("Clearing UART queue.\n");
|
|
|
|
clear_uart_buff(rb);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-04-07 09:11:36 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-22 02:48:03 +08:00
|
|
|
#ifdef PANDA
|
2017-07-25 03:31:47 +08:00
|
|
|
int spi_cb_rx(uint8_t *data, int len, uint8_t *data_out) {
|
2017-04-18 22:34:56 +08:00
|
|
|
// data[0] = endpoint
|
|
|
|
// data[2] = length
|
|
|
|
// data[4:] = data
|
2017-07-25 03:31:47 +08:00
|
|
|
|
|
|
|
int resp_len = 0;
|
2017-04-18 22:34:56 +08:00
|
|
|
switch (data[0]) {
|
|
|
|
case 0:
|
|
|
|
// control transfer
|
2017-07-25 03:31:47 +08:00
|
|
|
resp_len = usb_cb_control_msg((USB_Setup_TypeDef *)(data+4), data_out, 0);
|
2017-04-18 22:34:56 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// ep 1, read
|
2017-07-25 03:31:47 +08:00
|
|
|
resp_len = usb_cb_ep1_in(data_out, 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;
|
|
|
|
}
|
2017-07-25 03:31:47 +08:00
|
|
|
return resp_len;
|
2017-05-04 06:54:47 +08:00
|
|
|
}
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-07-22 02:48:03 +08:00
|
|
|
#else
|
|
|
|
|
2017-08-01 09:00:53 +08:00
|
|
|
int spi_cb_rx(uint8_t *data, int len, uint8_t *data_out) { return 0; };
|
2017-07-22 02:48:03 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
#endif
|
|
|
|
|
2017-07-22 02:48:03 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
// ***************************** main code *****************************
|
|
|
|
|
|
|
|
void __initialize_hardware_early() {
|
|
|
|
early();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2017-07-21 14:42:19 +08:00
|
|
|
// shouldn't have interrupts here, but just in case
|
2017-07-21 14:14:27 +08:00
|
|
|
__disable_irq();
|
|
|
|
|
2017-07-28 06:54:55 +08:00
|
|
|
// init early devices
|
2017-04-07 09:11:36 +08:00
|
|
|
clock_init();
|
2017-04-18 04:57:34 +08:00
|
|
|
periph_init();
|
2017-07-28 06:54:55 +08:00
|
|
|
detect();
|
|
|
|
|
|
|
|
// print hello
|
|
|
|
puts("\n\n\n************************ MAIN START ************************\n");
|
2017-04-18 04:57:34 +08:00
|
|
|
|
2017-07-21 14:42:19 +08:00
|
|
|
// detect the revision and init the GPIOs
|
2017-07-23 05:28:11 +08:00
|
|
|
puts("config:\n");
|
|
|
|
#ifdef PANDA
|
|
|
|
puts(revision == PANDA_REV_C ? " panda rev c\n" : " panda rev a or b\n");
|
|
|
|
#else
|
|
|
|
puts(" legacy\n");
|
|
|
|
#endif
|
2017-07-30 09:16:08 +08:00
|
|
|
puts(has_external_debug_serial ? " real serial\n" : " USB serial\n");
|
2017-07-23 05:28:11 +08:00
|
|
|
puts(is_giant_panda ? " GIANTpanda detected\n" : " not GIANTpanda\n");
|
|
|
|
puts(is_entering_bootmode ? " ESP wants bootmode\n" : " no bootmode\n");
|
2017-04-07 09:11:36 +08:00
|
|
|
gpio_init();
|
|
|
|
|
2017-07-30 10:50:35 +08:00
|
|
|
// enable main uart if it's connected
|
|
|
|
if (has_external_debug_serial) {
|
|
|
|
// WEIRDNESS: without this gate around the UART, it would "crash", but only if the ESP is enabled
|
|
|
|
// assuming it's because the lines were left floating and spurious noise was on them
|
|
|
|
uart_init(USART2, 115200);
|
|
|
|
}
|
2017-04-07 09:11:36 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
// 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:42:19 +08:00
|
|
|
safety_set_mode(SAFETY_NOOUTPUT);
|
2017-07-22 02:30:07 +08:00
|
|
|
can_silent = 1;
|
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();
|
|
|
|
|
2017-07-22 02:48:03 +08:00
|
|
|
#ifdef PANDA
|
2017-04-07 09:11:36 +08:00
|
|
|
spi_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// set PWM
|
2017-07-21 14:36:06 +08:00
|
|
|
fan_init();
|
2017-07-25 03:31:47 +08:00
|
|
|
fan_set_speed(0);
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
puts("**** INTERRUPTS ON ****\n");
|
|
|
|
|
|
|
|
__enable_irq();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|