refactoring
parent
22da59f663
commit
65809f25f4
28
board/can.h
28
board/can.h
|
@ -1,4 +1,5 @@
|
|||
// IRQs: CAN1_TX, CAN1_RX0, CAN1_SCE, CAN2_TX, CAN2_RX0, CAN2_SCE, CAN3_TX, CAN3_RX0, CAN3_SCE
|
||||
int can_live = 0, pending_can_live = 0, can_loopback = 0, can_silent = 1;
|
||||
|
||||
// ********************* instantiate queues *********************
|
||||
|
||||
|
@ -329,7 +330,7 @@ void process_can(uint8_t can_number) {
|
|||
exit_critical_section();
|
||||
}
|
||||
|
||||
void send_can(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number);
|
||||
void can_send(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number);
|
||||
|
||||
// CAN receive handlers
|
||||
// blink blue when we are receiving CAN messages
|
||||
|
@ -357,7 +358,7 @@ void can_rx(uint8_t can_number) {
|
|||
to_send.RDTR = to_push.RDTR;
|
||||
to_send.RDLR = to_push.RDLR;
|
||||
to_send.RDHR = to_push.RDHR;
|
||||
send_can(&to_send, can_forwarding[bus_number]);
|
||||
can_send(&to_send, can_forwarding[bus_number]);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -389,13 +390,22 @@ void CAN3_RX0_IRQHandler() { can_rx(2); }
|
|||
void CAN3_SCE_IRQHandler() { can_sce(CAN3); }
|
||||
#endif
|
||||
|
||||
void send_can(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number) {
|
||||
if (bus_number < BUS_MAX) {
|
||||
// add CAN packet to send queue
|
||||
// bus number isn't passed through
|
||||
to_push->RDTR &= 0xF;
|
||||
push(can_queues[bus_number], to_push);
|
||||
process_can(CAN_NUM_FROM_BUS_NUM(bus_number));
|
||||
void can_send(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number) {
|
||||
if (safety_tx_hook(to_push)) {
|
||||
if (bus_number < BUS_MAX) {
|
||||
// add CAN packet to send queue
|
||||
// bus number isn't passed through
|
||||
to_push->RDTR &= 0xF;
|
||||
push(can_queues[bus_number], to_push);
|
||||
process_can(CAN_NUM_FROM_BUS_NUM(bus_number));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void can_set_silent(int silent) {
|
||||
if (can_silent != silent) {
|
||||
can_silent = silent;
|
||||
can_init_all();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
11
board/gpio.h
11
board/gpio.h
|
@ -265,3 +265,14 @@ void gpio_init() {
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
38
board/main.c
38
board/main.c
|
@ -12,7 +12,6 @@
|
|||
// debug safety check: is controls allowed?
|
||||
int controls_allowed = 0;
|
||||
int started = 0;
|
||||
int can_live = 0, pending_can_live = 0, can_loopback = 0, can_silent = 1;
|
||||
|
||||
// optional features
|
||||
int gas_interceptor_detected = 0;
|
||||
|
@ -83,6 +82,7 @@ int get_health_pkt(void *dat) {
|
|||
uint8_t started_signal_detected;
|
||||
uint8_t started_alt;
|
||||
} *health = dat;
|
||||
|
||||
health->voltage = adc_get(ADCCHAN_VOLTAGE);
|
||||
#ifdef ENABLE_CURRENT_SENSOR
|
||||
health->current = adc_get(ADCCHAN_CURRENT);
|
||||
|
@ -98,32 +98,25 @@ int get_health_pkt(void *dat) {
|
|||
#endif
|
||||
|
||||
health->controls_allowed = controls_allowed;
|
||||
|
||||
health->gas_interceptor_detected = gas_interceptor_detected;
|
||||
health->started_signal_detected = started_signal_detected;
|
||||
return sizeof(*health);
|
||||
}
|
||||
|
||||
void set_fan_speed(int fan_speed) {
|
||||
TIM3->CCR3 = fan_speed;
|
||||
}
|
||||
|
||||
int usb_cb_ep1_in(uint8_t *usbdata, int len, int hardwired) {
|
||||
CAN_FIFOMailBox_TypeDef *reply = (CAN_FIFOMailBox_TypeDef *)usbdata;
|
||||
|
||||
int ilen = 0;
|
||||
while (ilen < min(len/0x10, 4) && pop(&can_rx_q, &reply[ilen])) ilen++;
|
||||
|
||||
return ilen*0x10;
|
||||
}
|
||||
|
||||
// send on serial, first byte to select
|
||||
// send on serial, first byte to select the ring
|
||||
void usb_cb_ep2_out(uint8_t *usbdata, int len, int hardwired) {
|
||||
int i;
|
||||
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, hardwired)) {
|
||||
if ((usbdata[0] < 2) || safety_tx_lin_hook(usbdata[0]-2, usbdata+1, len-1)) {
|
||||
for (i = 1; i < len; i++) while (!putc(ur, usbdata[i]));
|
||||
}
|
||||
}
|
||||
|
@ -142,9 +135,7 @@ void usb_cb_ep3_out(uint8_t *usbdata, int len, int hardwired) {
|
|||
to_push.RIR = tf[0];
|
||||
|
||||
uint8_t bus_number = (to_push.RDTR >> 4) & CAN_BUS_NUM_MASK;
|
||||
if (safety_tx_hook(&to_push, hardwired)) {
|
||||
send_can(&to_push, bus_number);
|
||||
}
|
||||
can_send(&to_push, bus_number);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,6 +144,7 @@ void usb_cb_enumeration_complete() {
|
|||
// this doesn't work and makes the board unflashable
|
||||
// because the ESP spews shit on serial on startup
|
||||
//GPIOC->ODR &= ~(1 << 14);
|
||||
puts("USB enumeration complete\n");
|
||||
did_usb_enumerate = 1;
|
||||
}
|
||||
|
||||
|
@ -195,7 +187,7 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
|
|||
break;
|
||||
// **** 0xd3: set fan speed
|
||||
case 0xd3:
|
||||
set_fan_speed(setup->b.wValue.w);
|
||||
fan_set_speed(setup->b.wValue.w);
|
||||
break;
|
||||
// **** 0xd6: get version
|
||||
case 0xd6:
|
||||
|
@ -255,9 +247,7 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
|
|||
case 0xdc:
|
||||
if (hardwired) {
|
||||
// silent mode if the safety mode is nooutput
|
||||
can_silent = (setup->b.wValue.w == SAFETY_NOOUTPUT);
|
||||
set_safety_mode(setup->b.wValue.w);
|
||||
can_init_all();
|
||||
}
|
||||
break;
|
||||
// **** 0xdd: enable can forwarding
|
||||
|
@ -371,7 +361,7 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
|
|||
|
||||
#ifdef ENABLE_SPI
|
||||
|
||||
void handle_spi(uint8_t *data, int len) {
|
||||
void spi_cb_handle(uint8_t *data, int len) {
|
||||
memset(spi_tx_buf, 0xaa, 0x44);
|
||||
// data[0] = endpoint
|
||||
// data[2] = length
|
||||
|
@ -446,7 +436,7 @@ int main() {
|
|||
usb_init();
|
||||
|
||||
// default to silent mode to prevent issues with Ford
|
||||
can_silent = 1;
|
||||
set_safety_mode(SAFETY_NOOUTPUT);
|
||||
can_init_all();
|
||||
|
||||
adc_init();
|
||||
|
@ -455,13 +445,9 @@ int main() {
|
|||
spi_init();
|
||||
#endif
|
||||
|
||||
// timer for fan PWM
|
||||
TIM3->CCMR2 = TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1;
|
||||
TIM3->CCER = TIM_CCER_CC3E;
|
||||
timer_init(TIM3, 10);
|
||||
|
||||
// set PWM
|
||||
set_fan_speed(65535);
|
||||
fan_init();
|
||||
fan_set_speed(65535);
|
||||
|
||||
puts("**** INTERRUPTS ON ****\n");
|
||||
|
||||
|
@ -517,12 +503,12 @@ int main() {
|
|||
started = 1;
|
||||
|
||||
// turn on fan at half speed
|
||||
set_fan_speed(32768);
|
||||
fan_set_speed(32768);
|
||||
} else {
|
||||
started = 0;
|
||||
|
||||
// turn off fan
|
||||
set_fan_speed(0);
|
||||
fan_set_speed(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
void safety_rx_hook(CAN_FIFOMailBox_TypeDef *to_push);
|
||||
int safety_tx_hook(CAN_FIFOMailBox_TypeDef *to_send, int hardwired);
|
||||
int safety_tx_lin_hook(int lin_num, uint8_t *data, int len, int hardwired);
|
||||
int safety_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
|
||||
int safety_tx_lin_hook(int lin_num, uint8_t *data, int len);
|
||||
|
||||
typedef void (*safety_hook_init)();
|
||||
typedef void (*rx_hook)(CAN_FIFOMailBox_TypeDef *to_push);
|
||||
typedef int (*tx_hook)(CAN_FIFOMailBox_TypeDef *to_send, int hardwired);
|
||||
typedef int (*tx_lin_hook)(int lin_num, uint8_t *data, int len, int hardwired);
|
||||
typedef int (*tx_hook)(CAN_FIFOMailBox_TypeDef *to_send);
|
||||
typedef int (*tx_lin_hook)(int lin_num, uint8_t *data, int len);
|
||||
|
||||
typedef struct {
|
||||
safety_hook_init init;
|
||||
|
@ -24,12 +24,12 @@ void safety_rx_hook(CAN_FIFOMailBox_TypeDef *to_push){
|
|||
current_hooks->rx(to_push);
|
||||
}
|
||||
|
||||
int safety_tx_hook(CAN_FIFOMailBox_TypeDef *to_send, int hardwired){
|
||||
return current_hooks->tx(to_send, hardwired);
|
||||
int safety_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
|
||||
return current_hooks->tx(to_send);
|
||||
}
|
||||
|
||||
int safety_tx_lin_hook(int lin_num, uint8_t *data, int len, int hardwired){
|
||||
return current_hooks->tx_lin(lin_num, data, len, hardwired);
|
||||
int safety_tx_lin_hook(int lin_num, uint8_t *data, int len){
|
||||
return current_hooks->tx_lin(lin_num, data, len);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
|
@ -50,6 +50,7 @@ const safety_hook_config safety_hook_registry[] = {
|
|||
#define HOOK_CONFIG_COUNT (sizeof(safety_hook_registry)/sizeof(safety_hook_config))
|
||||
|
||||
int set_safety_mode(uint16_t mode){
|
||||
can_set_silent(mode == SAFETY_NOOUTPUT);
|
||||
for (int i = 0; i < HOOK_CONFIG_COUNT; i++) {
|
||||
if (safety_hook_registry[i].id == mode) {
|
||||
current_hooks = safety_hook_registry[i].hooks;
|
||||
|
|
|
@ -5,11 +5,11 @@ void default_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {}
|
|||
static void nooutput_init() {
|
||||
}
|
||||
|
||||
static int nooutput_tx_hook(CAN_FIFOMailBox_TypeDef *to_send, int hardwired) {
|
||||
static int nooutput_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static int nooutput_tx_lin_hook(int lin_num, uint8_t *data, int len, int hardwired) {
|
||||
static int nooutput_tx_lin_hook(int lin_num, uint8_t *data, int len) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -25,12 +25,12 @@ const safety_hooks nooutput_hooks = {
|
|||
static void alloutput_init() {
|
||||
}
|
||||
|
||||
static int alloutput_tx_hook(CAN_FIFOMailBox_TypeDef *to_send, int hardwired) {
|
||||
return hardwired;
|
||||
static int alloutput_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static int alloutput_tx_lin_hook(int lin_num, uint8_t *data, int len, int hardwired) {
|
||||
return hardwired;
|
||||
static int alloutput_tx_lin_hook(int lin_num, uint8_t *data, int len) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const safety_hooks alloutput_hooks = {
|
||||
|
|
|
@ -50,7 +50,7 @@ static void honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
|
|||
}
|
||||
}
|
||||
|
||||
static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send, int hardwired) {
|
||||
static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
|
||||
// BRAKE: safety check
|
||||
if ((to_send->RIR>>21) == 0x1FA) {
|
||||
if (controls_allowed) {
|
||||
|
@ -79,11 +79,12 @@ static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send, int hardwired) {
|
|||
}
|
||||
|
||||
// 1 allows the message through
|
||||
return hardwired;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int honda_tx_lin_hook(int lin_num, uint8_t *data, int len, int hardwired) {
|
||||
return hardwired;
|
||||
static int honda_tx_lin_hook(int lin_num, uint8_t *data, int len) {
|
||||
// TODO: add safety if using LIN
|
||||
return true;
|
||||
}
|
||||
|
||||
static void honda_init() {
|
||||
|
|
|
@ -66,13 +66,13 @@ void spi_rx_dma(void *addr, int len) {
|
|||
|
||||
// ***************************** SPI IRQs *****************************
|
||||
|
||||
void handle_spi(uint8_t *data, int len);
|
||||
void spi_cb_handle(uint8_t *data, int len);
|
||||
|
||||
// SPI RX
|
||||
void DMA2_Stream2_IRQHandler(void) {
|
||||
// ack
|
||||
DMA2->LIFCR = DMA_LIFCR_CTCIF2;
|
||||
handle_spi(spi_buf, 0x14);
|
||||
spi_cb_handle(spi_buf, 0x14);
|
||||
}
|
||||
|
||||
// SPI TX
|
||||
|
|
Loading…
Reference in New Issue