Skip to content

Commit

Permalink
tres fan (commaai#1162)
Browse files Browse the repository at this point in the history
* fan on

* little cleanup

* move to main
  • Loading branch information
adeebshihadeh authored Nov 18, 2022
1 parent e5e34b1 commit ae66bbc
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 34 deletions.
4 changes: 0 additions & 4 deletions board/boards/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ void dos_init(void) {
pwm_init(TIM4, 2);
dos_set_ir_power(0U);

// Initialize fan and set to 0%
fan_init();
dos_set_fan_enabled(false);

// Initialize harness
harness_init();

Expand Down
6 changes: 3 additions & 3 deletions board/boards/red_chiplet.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ void red_chiplet_enable_can_transceivers(bool enabled) {
}
}

void red_chiplet_set_usb_load_switch(bool enabled) {
void red_chiplet_set_fan_or_usb_load_switch(bool enabled) {
set_gpio_output(GPIOD, 3, enabled);
}

void red_chiplet_init(void) {
common_init_gpio();

//A8, A9 : OBD_SBU1_RELAY, OBD_SBU2_RELAY
// A8, A9: OBD_SBU1_RELAY, OBD_SBU2_RELAY
set_gpio_output_type(GPIOA, 8, OUTPUT_TYPE_OPEN_DRAIN);
set_gpio_pullup(GPIOA, 8, PULL_NONE);
set_gpio_mode(GPIOA, 8, MODE_OUTPUT);
Expand Down Expand Up @@ -75,7 +75,7 @@ void red_chiplet_init(void) {
set_gpio_mode(GPIOB, 0, MODE_ANALOG);

// Turn on USB load switch.
red_chiplet_set_usb_load_switch(true);
red_chiplet_set_fan_or_usb_load_switch(true);

// Initialize harness
harness_init();
Expand Down
7 changes: 5 additions & 2 deletions board/boards/tres.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ void tres_init(void) {
set_gpio_alternate(GPIOE, 13, GPIO_AF5_SPI4);
set_gpio_alternate(GPIOE, 14, GPIO_AF5_SPI4);
register_set_bits(&(GPIOE->OSPEEDR), GPIO_OSPEEDR_OSPEED11 | GPIO_OSPEEDR_OSPEED12 | GPIO_OSPEEDR_OSPEED13 | GPIO_OSPEEDR_OSPEED14);

// fan setup
set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3);
}

const board board_tres = {
Expand All @@ -33,7 +36,7 @@ const board board_tres = {
.has_spi = true,
.has_canfd = true,
.has_rtc_battery = true,
.fan_max_rpm = 0U,
.fan_max_rpm = 6500U, // TODO: verify this, copied from dos
.init = tres_init,
.enable_can_transceiver = red_chiplet_enable_can_transceiver,
.enable_can_transceivers = red_chiplet_enable_can_transceivers,
Expand All @@ -42,7 +45,7 @@ const board board_tres = {
.set_can_mode = red_set_can_mode,
.check_ignition = red_check_ignition,
.read_current = unused_read_current,
.set_fan_enabled = unused_set_fan_enabled,
.set_fan_enabled = red_chiplet_set_fan_or_usb_load_switch,
.set_ir_power = unused_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_siren = unused_set_siren
Expand Down
4 changes: 0 additions & 4 deletions board/boards/uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ void uno_init(void) {
pwm_init(TIM4, 2);
uno_set_ir_power(0U);

// Initialize fan and set to 0%
fan_init();
uno_set_fan_enabled(false);

// Initialize harness
harness_init();

Expand Down
2 changes: 1 addition & 1 deletion board/drivers/fan.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void fan_tick(void){
current_board->set_fan_enabled(fan_state.target_rpm > 0U);

// Stall detection
if(fan_state.power > 0U) {
if (fan_state.power > 0U) {
if (fan_rpm_fast == 0U) {
fan_state.stall_counter = MIN(fan_state.stall_counter + 1U, 255U);
} else {
Expand Down
9 changes: 7 additions & 2 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ void tick_handler(void) {
// Also disable IR when the heartbeat goes missing
current_board->set_ir_power(0U);

// TODO: need a SPI equivalent
// If enumerated but no heartbeat (phone up, boardd not running), turn the fan on to cool the device
if(usb_enumerated){
if (usb_enumerated) {
fan_set_power(50U);
} else {
fan_set_power(0U);
Expand Down Expand Up @@ -341,14 +342,18 @@ int main(void) {
uart_init(&uart_ring_gps, 115200);
}

if(current_board->has_lin){
if (current_board->has_lin) {
// enable LIN
uart_init(&uart_ring_lin1, 10400);
UART5->CR2 |= USART_CR2_LINEN;
uart_init(&uart_ring_lin2, 10400);
USART3->CR2 |= USART_CR2_LINEN;
}

if (current_board->fan_max_rpm > 0U) {
llfan_init();
}

microsecond_timer_init();

// init to SILENT and can silent
Expand Down
32 changes: 16 additions & 16 deletions board/stm32fx/llfan.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// TACH interrupt handler
void EXTI2_IRQ_Handler(void) {
volatile unsigned int pr = EXTI->PR & (1U << 2);
if ((pr & (1U << 2)) != 0U) {
fan_state.tach_counter++;
}
EXTI->PR = (1U << 2);
volatile unsigned int pr = EXTI->PR & (1U << 2);
if ((pr & (1U << 2)) != 0U) {
fan_state.tach_counter++;
}
EXTI->PR = (1U << 2);
}

void fan_init(void){
// 5000RPM * 4 tach edges / 60 seconds
REGISTER_INTERRUPT(EXTI2_IRQn, EXTI2_IRQ_Handler, 700U, FAULT_INTERRUPT_RATE_TACH)
void llfan_init(void) {
// 5000RPM * 4 tach edges / 60 seconds
REGISTER_INTERRUPT(EXTI2_IRQn, EXTI2_IRQ_Handler, 700U, FAULT_INTERRUPT_RATE_TACH)

// Init PWM speed control
pwm_init(TIM3, 3);
// Init PWM speed control
pwm_init(TIM3, 3);

// Init TACH interrupt
register_set(&(SYSCFG->EXTICR[0]), SYSCFG_EXTICR1_EXTI2_PD, 0xF00U);
register_set_bits(&(EXTI->IMR), (1U << 2));
register_set_bits(&(EXTI->RTSR), (1U << 2));
register_set_bits(&(EXTI->FTSR), (1U << 2));
NVIC_EnableIRQ(EXTI2_IRQn);
// Init TACH interrupt
register_set(&(SYSCFG->EXTICR[0]), SYSCFG_EXTICR1_EXTI2_PD, 0xF00U);
register_set_bits(&(EXTI->IMR), (1U << 2));
register_set_bits(&(EXTI->RTSR), (1U << 2));
register_set_bits(&(EXTI->FTSR), (1U << 2));
NVIC_EnableIRQ(EXTI2_IRQn);
}
25 changes: 23 additions & 2 deletions board/stm32h7/llfan.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
void EXTI2_IRQ_Handler(void) { }
void fan_init(void){ }
// TACH interrupt handler
void EXTI2_IRQ_Handler(void) {
volatile unsigned int pr = EXTI->PR1 & (1U << 2);
if ((pr & (1U << 2)) != 0U) {
fan_state.tach_counter++;
}
EXTI->PR1 = (1U << 2);
}

void llfan_init(void) {
// 5000RPM * 4 tach edges / 60 seconds
REGISTER_INTERRUPT(EXTI2_IRQn, EXTI2_IRQ_Handler, 700U, FAULT_INTERRUPT_RATE_TACH)

// Init PWM speed control
pwm_init(TIM3, 3);

// Init TACH interrupt
register_set(&(SYSCFG->EXTICR[0]), SYSCFG_EXTICR1_EXTI2_PD, 0xF00U);
register_set_bits(&(EXTI->IMR1), (1U << 2));
register_set_bits(&(EXTI->RTSR1), (1U << 2));
register_set_bits(&(EXTI->FTSR1), (1U << 2));
NVIC_EnableIRQ(EXTI2_IRQn);
}
1 change: 1 addition & 0 deletions board/stm32h7/peripherals.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ void peripherals_init(void) {
RCC->APB2ENR |= RCC_APB2ENR_SPI4EN; // SPI
RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN; // SPI DMA
RCC->APB1LENR |= RCC_APB1LENR_TIM2EN; // main counter
RCC->APB1LENR |= RCC_APB1LENR_TIM3EN; // fan pwm
RCC->APB1LENR |= RCC_APB1LENR_TIM6EN; // interrupt timer
RCC->APB1LENR |= RCC_APB1LENR_UART7EN; // SOM uart
RCC->APB2ENR |= RCC_APB2ENR_TIM8EN; // clock source timer
Expand Down

0 comments on commit ae66bbc

Please sign in to comment.