refactoring

This commit is contained in:
Firmware Batman
2017-07-21 11:48:03 -07:00
parent 9783a2ab13
commit d9fc3b372c
9 changed files with 138 additions and 151 deletions

View File

@@ -11,15 +11,20 @@
#include "stm32f2xx.h"
#endif
#ifdef PANDA
#define ENABLE_CURRENT_SENSOR
#define ENABLE_SPI
#endif
#define USB_VID 0xbbaa
#define USB_PID 0xddcc
#define NULL ((void*)0)
#define COMPILE_TIME_ASSERT(pred) switch(0){case 0:case pred:;}
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#endif

View File

@@ -61,6 +61,11 @@ void adc_init();
uint32_t adc_get(int channel);
// ********************* TIMER *********************
void timer_init(TIM_TypeDef *TIM, int psc);
// ********************* SPI *********************
// IRQs: DMA2_Stream2, DMA2_Stream3, EXTI4
@@ -96,8 +101,11 @@ extern int can_live, pending_can_live;
extern int can_loopback, can_silent;
extern uint32_t can_speed[];
void can_init(uint8_t can_number);
void can_send(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number);
int can_pop(can_ring *q, CAN_FIFOMailBox_TypeDef *elem);
void can_set_forwarding(int from, int to);
void can_init(uint8_t can_number);
void can_init_all();
void can_send(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number);
int can_pop(can_ring *q, CAN_FIFOMailBox_TypeDef *elem);

View File

@@ -1,4 +1,3 @@
#ifdef ENABLE_SPI
// IRQs: DMA2_Stream2, DMA2_Stream3, EXTI4
#define SPI_BUF_SIZE 256
@@ -93,5 +92,3 @@ void EXTI4_IRQHandler(void) {
EXTI->PR = pr;
}
#endif

7
board/drivers/timer.h Normal file
View File

@@ -0,0 +1,7 @@
void timer_init(TIM_TypeDef *TIM, int psc) {
TIM->PSC = psc-1;
TIM->DIER = TIM_DIER_UIE;
TIM->CR1 = TIM_CR1_CEN;
TIM->SR = 0;
}

View File

@@ -6,6 +6,71 @@
#include "llgpio.h"
// ********************* bringup *********************
void clock_init() {
// enable external oscillator
RCC->CR |= RCC_CR_HSEON;
while ((RCC->CR & RCC_CR_HSERDY) == 0);
// divide shit
RCC->CFGR = RCC_CFGR_HPRE_DIV1 | RCC_CFGR_PPRE2_DIV2 | RCC_CFGR_PPRE1_DIV4;
#ifdef PANDA
RCC->PLLCFGR = RCC_PLLCFGR_PLLQ_2 | RCC_PLLCFGR_PLLM_3 |
RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_5 | RCC_PLLCFGR_PLLSRC_HSE;
#else
RCC->PLLCFGR = RCC_PLLCFGR_PLLQ_2 | RCC_PLLCFGR_PLLM_3 |
RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLSRC_HSE;
#endif
// start PLL
RCC->CR |= RCC_CR_PLLON;
while ((RCC->CR & RCC_CR_PLLRDY) == 0);
// Configure Flash prefetch, Instruction cache, Data cache and wait state
// *** without this, it breaks ***
FLASH->ACR = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_5WS;
// switch to PLL
RCC->CFGR |= RCC_CFGR_SW_PLL;
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);
// *** running on PLL ***
}
void periph_init() {
// enable GPIOB, UART2, CAN, USB clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;
RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
#ifdef PANDA
RCC->APB1ENR |= RCC_APB1ENR_UART5EN;
#endif
RCC->APB1ENR |= RCC_APB1ENR_CAN1EN;
RCC->APB1ENR |= RCC_APB1ENR_CAN2EN;
#ifdef CAN3
RCC->APB1ENR |= RCC_APB1ENR_CAN3EN;
#endif
RCC->APB1ENR |= RCC_APB1ENR_DACEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
//RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN;
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
// needed?
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
}
// ********************* setters *********************
void set_can_enable(CAN_TypeDef *CAN, int enabled) {
// enable CAN busses
if (CAN == CAN1) {
@@ -52,39 +117,6 @@ void set_led(int led_num, int on) {
#endif
}
// TODO: does this belong here?
void periph_init() {
// enable GPIOB, UART2, CAN, USB clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;
RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
#ifdef PANDA
RCC->APB1ENR |= RCC_APB1ENR_UART5EN;
#endif
RCC->APB1ENR |= RCC_APB1ENR_CAN1EN;
RCC->APB1ENR |= RCC_APB1ENR_CAN2EN;
#ifdef CAN3
RCC->APB1ENR |= RCC_APB1ENR_CAN3EN;
#endif
RCC->APB1ENR |= RCC_APB1ENR_DACEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
//RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN;
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
// needed?
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
}
void set_can_mode(int can, int use_gmlan) {
// connects to CAN2 xcvr or GMLAN xcvr
if (use_gmlan) {
@@ -155,6 +187,8 @@ void set_usb_power_mode(int mode) {
}
}
// ********************* big init function *********************
// board specific
void gpio_init() {
// pull low to hold ESP in reset??
@@ -206,7 +240,6 @@ void gpio_init() {
#endif
// B8,B9: CAN 1
set_can_enable(CAN1, 0);
#ifdef STM32F4
set_gpio_alternate(GPIOB, 8, GPIO_AF8_CAN1);
set_gpio_alternate(GPIOB, 9, GPIO_AF8_CAN1);
@@ -214,15 +247,16 @@ void gpio_init() {
set_gpio_alternate(GPIOB, 8, GPIO_AF9_CAN1);
set_gpio_alternate(GPIOB, 9, GPIO_AF9_CAN1);
#endif
set_can_enable(CAN1, 1);
// B5,B6: CAN 2
set_can_enable(CAN2, 0);
set_can_mode(1, 0);
set_can_enable(CAN2, 1);
// A8,A15: CAN 3
#ifdef CAN3
set_can_enable(CAN3, 0);
set_can_mode(2, 0);
set_can_enable(CAN3, 1);
#endif
/* GMLAN mode pins:
@@ -265,21 +299,3 @@ void gpio_init() {
}
}
void timer_init(TIM_TypeDef *TIM, int psc) {
TIM->PSC = psc-1;
TIM->DIER = TIM_DIER_UIE;
TIM->CR1 = TIM_CR1_CEN;
TIM->SR = 0;
}
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;
}

View File

@@ -1,46 +1,5 @@
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
// **** shitty libc ****
void clock_init() {
// enable external oscillator
RCC->CR |= RCC_CR_HSEON;
while ((RCC->CR & RCC_CR_HSERDY) == 0);
// divide shit
RCC->CFGR = RCC_CFGR_HPRE_DIV1 | RCC_CFGR_PPRE2_DIV2 | RCC_CFGR_PPRE1_DIV4;
#ifdef PANDA
RCC->PLLCFGR = RCC_PLLCFGR_PLLQ_2 | RCC_PLLCFGR_PLLM_3 |
RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_5 | RCC_PLLCFGR_PLLSRC_HSE;
#else
RCC->PLLCFGR = RCC_PLLCFGR_PLLQ_2 | RCC_PLLCFGR_PLLM_3 |
RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLSRC_HSE;
#endif
// start PLL
RCC->CR |= RCC_CR_PLLON;
while ((RCC->CR & RCC_CR_PLLRDY) == 0);
// Configure Flash prefetch, Instruction cache, Data cache and wait state
// *** without this, it breaks ***
FLASH->ACR = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_5WS;
// switch to PLL
RCC->CFGR |= RCC_CFGR_SW_PLL;
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);
// *** running on PLL ***
}
void delay(int a) {
volatile int i;
for (i=0;i<a;i++);

View File

@@ -6,16 +6,6 @@
#include "obj/gitversion.h"
// debug safety check: is controls allowed?
int started = 0;
// optional features
int started_signal_detected = 0;
// these are set in the Honda safety hooks...this is the wrong place
int controls_allowed = 0;
int gas_interceptor_detected = 0;
// ********************* includes *********************
#include "libc.h"
@@ -29,6 +19,20 @@ int gas_interceptor_detected = 0;
#include "drivers/usb.h"
#include "drivers/can.h"
#include "drivers/spi.h"
#include "drivers/timer.h"
// ***************************** 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;
}
// ********************* serial debugging *********************
@@ -76,14 +80,19 @@ int get_health_pkt(void *dat) {
uint8_t started_signal_detected;
uint8_t started_alt;
} *health = dat;
#ifdef PANDA
int started_signal = (GPIOB->IDR & (1 << 12)) == 0;
#else
int started_signal = (GPIOC->IDR & (1 << 13)) != 0;
#endif
health->voltage = adc_get(ADCCHAN_VOLTAGE);
#ifdef ENABLE_CURRENT_SENSOR
#ifdef PANDA
health->current = adc_get(ADCCHAN_CURRENT);
#else
health->current = 0;
#endif
health->started = started;
health->started = started_signal;
#ifdef PANDA
health->started_alt = (GPIOA->IDR & (1 << 1)) == 0;
@@ -93,7 +102,10 @@ int get_health_pkt(void *dat) {
health->controls_allowed = controls_allowed;
health->gas_interceptor_detected = gas_interceptor_detected;
health->started_signal_detected = started_signal_detected;
// DEPRECATED
health->started_signal_detected = 0;
return sizeof(*health);
}
@@ -134,10 +146,6 @@ void usb_cb_ep3_out(uint8_t *usbdata, int len, int hardwired) {
}
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);
puts("USB enumeration complete\n");
}
@@ -268,7 +276,8 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
ur = get_ring_by_number(setup->b.wValue.w);
if (!ur) break;
// read
while (resp_len < min(setup->b.wLength.w, MAX_RESP_LEN) && getc(ur, (char*)&resp[resp_len])) {
while ((resp_len < min(setup->b.wLength.w, MAX_RESP_LEN)) &&
getc(ur, (char*)&resp[resp_len])) {
++resp_len;
}
break;
@@ -354,8 +363,9 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
return resp_len;
}
#ifdef ENABLE_SPI
#ifdef PANDA
// can't go on the stack cause it's DMAed
uint8_t spi_tx_buf[0x44];
void spi_cb_rx(uint8_t *data, int len) {
@@ -392,8 +402,13 @@ void spi_cb_rx(uint8_t *data, int len) {
GPIOB->ODR &= ~(GPIO_ODR_ODR_0);
}
#else
void spi_cb_rx(uint8_t *data, int len) {};
#endif
// ***************************** main code *****************************
void __initialize_hardware_early() {
@@ -436,7 +451,7 @@ int main() {
adc_init();
#ifdef ENABLE_SPI
#ifdef PANDA
spi_init();
#endif
@@ -448,8 +463,6 @@ int main() {
__enable_irq();
puts("OPTCR: "); puth(FLASH->OPTCR); puts("\n");
// LED should keep on blinking all the time
uint64_t cnt;
for (cnt=0;;cnt++) {
@@ -465,9 +478,6 @@ int main() {
puth(can_tx2_q.r_ptr); puts(" "); puth(can_tx2_q.w_ptr); puts("\n");
#endif
/*puts("voltage: "); puth(adc_get(ADCCHAN_VOLTAGE)); puts(" ");
puts("current: "); puth(adc_get(ADCCHAN_CURRENT)); puts("\n");*/
// set LED to be controls allowed, blue on panda, green on legacy
#ifdef PANDA
set_led(LED_BLUE, controls_allowed);
@@ -485,26 +495,6 @@ int main() {
#ifdef PANDA
set_led(LED_GREEN, 0);
#endif
// 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
fan_set_speed(32768);
} else {
started = 0;
// turn off fan
fan_set_speed(0);
}
}
return 0;

View File

@@ -14,6 +14,9 @@ typedef struct {
tx_lin_hook tx_lin;
} safety_hooks;
// This can be set by the safety hooks.
int controls_allowed = 0;
// Include the actual safety policies.
#include "safety/safety_defaults.h"
#include "safety/safety_honda.h"

View File

@@ -4,6 +4,8 @@
// out-state
// cancel button
// these are set in the Honda safety hooks...this is the wrong place
int gas_interceptor_detected = 0;
// all commands: brake and steering
// if controls_allowed