Skip to content

Commit

Permalink
Cuatro fan (commaai#1999)
Browse files Browse the repository at this point in the history
* cuatro fan debugging

* fix

* faster fan, faster interrupts

* fix comment

---------

Co-authored-by: Comma Device <device@comma.ai>
  • Loading branch information
robbederks and Comma Device authored Aug 19, 2024
1 parent cfa8b79 commit 080e53f
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions board/boards/black.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ board board_black = {
.has_spi = false,
.has_canfd = false,
.fan_max_rpm = 0U,
.fan_max_pwm = 100U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
Expand Down
1 change: 1 addition & 0 deletions board/boards/board_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct board {
const uint16_t avdd_mV;
const bool fan_stall_recovery;
const uint8_t fan_enable_cooldown_time;
const uint8_t fan_max_pwm;
board_init init;
board_init_bootloader init_bootloader;
board_enable_can_transceiver enable_can_transceiver;
Expand Down
10 changes: 8 additions & 2 deletions board/boards/cuatro.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ uint32_t cuatro_read_current_mA(void) {
return adc_get_mV(3) * 2U;
}

void cuatro_set_fan_enabled(bool enabled) {
set_gpio_output(GPIOD, 3, !enabled);
}

void cuatro_init(void) {
red_chiplet_init();

Expand Down Expand Up @@ -93,6 +97,7 @@ void cuatro_init(void) {

// fan setup
set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3);
register_set_bits(&(GPIOC->OTYPER), GPIO_OTYPER_OT8); // open drain

// Initialize IR PWM and set to 0%
set_gpio_alternate(GPIOC, 9, GPIO_AF2_TIM3);
Expand All @@ -108,7 +113,8 @@ board board_cuatro = {
.has_obd = true,
.has_spi = true,
.has_canfd = true,
.fan_max_rpm = 6600U,
.fan_max_rpm = 12500U,
.fan_max_pwm = 99U, // it can go up to 14k RPM, but 99% -> 100% is very non-linear
.avdd_mV = 1800U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 3U,
Expand All @@ -121,7 +127,7 @@ board board_cuatro = {
.check_ignition = red_check_ignition,
.read_voltage_mV = cuatro_read_voltage_mV,
.read_current_mA = cuatro_read_current_mA,
.set_fan_enabled = tres_set_fan_enabled,
.set_fan_enabled = cuatro_set_fan_enabled,
.set_ir_power = tres_set_ir_power,
.set_siren = unused_set_siren,
.set_bootkick = tres_set_bootkick,
Expand Down
1 change: 1 addition & 0 deletions board/boards/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ board board_dos = {
#endif
.has_canfd = false,
.fan_max_rpm = 6500U,
.fan_max_pwm = 100U,
.avdd_mV = 3300U,
.fan_stall_recovery = true,
.fan_enable_cooldown_time = 3U,
Expand Down
1 change: 1 addition & 0 deletions board/boards/grey.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ board board_grey = {
.has_spi = false,
.has_canfd = false,
.fan_max_rpm = 0U,
.fan_max_pwm = 100U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
Expand Down
1 change: 1 addition & 0 deletions board/boards/red.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ board board_red = {
.has_spi = false,
.has_canfd = true,
.fan_max_rpm = 0U,
.fan_max_pwm = 100U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
Expand Down
1 change: 1 addition & 0 deletions board/boards/tres.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ board board_tres = {
.has_spi = true,
.has_canfd = true,
.fan_max_rpm = 6600U,
.fan_max_pwm = 100U,
.avdd_mV = 1800U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 3U,
Expand Down
1 change: 1 addition & 0 deletions board/boards/uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ board board_uno = {
.has_spi = false,
.has_canfd = false,
.fan_max_rpm = 5100U,
.fan_max_pwm = 100U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
Expand Down
1 change: 1 addition & 0 deletions board/boards/white.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ board board_white = {
.has_spi = false,
.has_canfd = false,
.fan_max_rpm = 0U,
.fan_max_pwm = 100U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
Expand Down
2 changes: 1 addition & 1 deletion board/drivers/fan.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void fan_tick(void) {
float error = fan_state.target_rpm - fan_rpm_fast;
fan_state.error_integral += FAN_I * error;
}
fan_state.power = CLAMP(fan_state.error_integral, 0U, 100U);
fan_state.power = CLAMP(fan_state.error_integral, 0U, current_board->fan_max_pwm);

// Set PWM and enable line
pwm_set(TIM3, 3, fan_state.power);
Expand Down
4 changes: 2 additions & 2 deletions board/stm32h7/llfan.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ void EXTI2_IRQ_Handler(void) {
}

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

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

0 comments on commit 080e53f

Please sign in to comment.