Skip to content

Commit

Permalink
cuatro beeper (commaai#2068)
Browse files Browse the repository at this point in the history
beeper works

Co-authored-by: Comma Device <[email protected]>
  • Loading branch information
robbederks and Comma Device authored Oct 30, 2024
1 parent 3066f93 commit 352e7ff
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
14 changes: 11 additions & 3 deletions board/boards/cuatro.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static void cuatro_set_led(uint8_t color, bool enabled) {
set_gpio_output(GPIOD, 15, !enabled);
break;
case LED_GREEN:
set_gpio_output(GPIOD, 14, !enabled);
set_gpio_output(GPIOB, 2, !enabled);
break;
case LED_BLUE:
set_gpio_output(GPIOE, 2, !enabled);
Expand Down Expand Up @@ -71,12 +71,16 @@ static void cuatro_set_bootkick(BootState state) {
//set_gpio_output(GPIOC, 12, state != BOOT_RESET);
}

static void cuatro_set_siren(bool enabled){
beeper_enable(enabled);
}

static void cuatro_init(void) {
red_chiplet_init();

// init LEDs as open drain
set_gpio_output_type(GPIOE, 2, OUTPUT_TYPE_OPEN_DRAIN);
set_gpio_output_type(GPIOD, 14, OUTPUT_TYPE_OPEN_DRAIN);
set_gpio_output_type(GPIOB, 2, OUTPUT_TYPE_OPEN_DRAIN);
set_gpio_output_type(GPIOD, 15, OUTPUT_TYPE_OPEN_DRAIN);

// Power readout
Expand Down Expand Up @@ -120,6 +124,10 @@ static void cuatro_init(void) {

// Clock source
clock_source_init();

// Beeper
set_gpio_alternate(GPIOD, 14, GPIO_AF2_TIM4);
beeper_init();
}

board board_cuatro = {
Expand All @@ -143,7 +151,7 @@ board board_cuatro = {
.read_current_mA = cuatro_read_current_mA,
.set_fan_enabled = cuatro_set_fan_enabled,
.set_ir_power = tres_set_ir_power,
.set_siren = unused_set_siren,
.set_siren = cuatro_set_siren,
.set_bootkick = cuatro_set_bootkick,
.read_som_gpio = tres_read_som_gpio
};
26 changes: 26 additions & 0 deletions board/drivers/beeper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#define BEEPER_COUNTER_OVERFLOW 25000U // 4kHz

void beeper_enable(bool enabled) {
if (enabled) {
register_set_bits(&(TIM4->CCER), TIM_CCER_CC3E);
} else {
register_clear_bits(&(TIM4->CCER), TIM_CCER_CC3E);
}
}

void beeper_init(void) {
// Enable timer and auto-reload
register_set(&(TIM4->CR1), TIM_CR1_CEN | TIM_CR1_ARPE, 0x3FU);

// Set channel as PWM mode 1 and disable output
register_set_bits(&(TIM4->CCMR2), (TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3PE));
beeper_enable(false);

// Set max counter value and compare to get 50% duty
register_set(&(TIM4->CCR3), BEEPER_COUNTER_OVERFLOW / 2U, 0xFFFFU);
register_set(&(TIM4->ARR), BEEPER_COUNTER_OVERFLOW, 0xFFFFU);

// Update registers and clear counter
TIM4->EGR |= TIM_EGR_UG;
}
1 change: 1 addition & 0 deletions board/stm32h7/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "drivers/fan.h"
#include "stm32h7/llfan.h"
#include "stm32h7/lldac.h"
#include "drivers/beeper.h"
#include "drivers/fake_siren.h"
#include "drivers/clock_source.h"
#include "boards/red.h"
Expand Down
1 change: 1 addition & 0 deletions board/stm32h7/peripherals.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ void peripherals_init(void) {
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // clock source timer
RCC->APB1LENR |= RCC_APB1LENR_TIM2EN; // main counter
RCC->APB1LENR |= RCC_APB1LENR_TIM3EN; // fan pwm
RCC->APB1LENR |= RCC_APB1LENR_TIM4EN; // beeper source
RCC->APB1LENR |= RCC_APB1LENR_TIM6EN; // interrupt timer
RCC->APB1LENR |= RCC_APB1LENR_TIM7EN; // DMA trigger timer
RCC->APB2ENR |= RCC_APB2ENR_TIM8EN; // tick timer
Expand Down

0 comments on commit 352e7ff

Please sign in to comment.