Skip to content

Commit

Permalink
F4: Cleanup clock src, second try (commaai#1193)
Browse files Browse the repository at this point in the history
* F4: cleanup clock source

* little more

* void

* restore the timer setup

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
Co-authored-by: Comma Device <device@comma.ai>
  • Loading branch information
3 people committed Dec 14, 2022
1 parent d1d1f8a commit 1d732d4
Show file tree
Hide file tree
Showing 14 changed files with 15 additions and 110 deletions.
1 change: 0 additions & 1 deletion board/boards/black.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,5 @@ const board board_black = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_clock_source_mode = unused_set_clock_source_mode,
.set_siren = unused_set_siren
};
2 changes: 0 additions & 2 deletions board/boards/board_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ typedef uint32_t (*board_read_current)(void);
typedef void (*board_set_ir_power)(uint8_t percentage);
typedef void (*board_set_fan_enabled)(bool enabled);
typedef void (*board_set_phone_power)(bool enabled);
typedef void (*board_set_clock_source_mode)(uint8_t mode);
typedef void (*board_set_siren)(bool enabled);
typedef void (*board_board_tick)(bool ignition, bool usb_enum, bool heartbeat_seen);

Expand All @@ -36,7 +35,6 @@ struct board {
board_set_ir_power set_ir_power;
board_set_fan_enabled set_fan_enabled;
board_set_phone_power set_phone_power;
board_set_clock_source_mode set_clock_source_mode;
board_set_siren set_siren;
board_board_tick board_tick;
};
Expand Down
7 changes: 1 addition & 6 deletions board/boards/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ void dos_set_fan_enabled(bool enabled){
set_gpio_output(GPIOA, 1, enabled);
}

void dos_set_clock_source_mode(uint8_t mode){
clock_source_init(mode);
}

void dos_set_siren(bool enabled){
set_gpio_output(GPIOC, 12, enabled);
}
Expand Down Expand Up @@ -182,7 +178,7 @@ void dos_init(void) {
}

// Init clock source (camera strobe) using PWM
dos_set_clock_source_mode(CLOCK_SOURCE_MODE_PWM);
clock_source_init();
}

const harness_configuration dos_harness_config = {
Expand Down Expand Up @@ -226,6 +222,5 @@ const board board_dos = {
.set_fan_enabled = dos_set_fan_enabled,
.set_ir_power = dos_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_clock_source_mode = dos_set_clock_source_mode,
.set_siren = dos_set_siren
};
1 change: 0 additions & 1 deletion board/boards/grey.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,5 @@ const board board_grey = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_clock_source_mode = unused_set_clock_source_mode,
.set_siren = unused_set_siren
};
1 change: 0 additions & 1 deletion board/boards/pedal.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,5 @@ const board board_pedal = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_clock_source_mode = unused_set_clock_source_mode,
.set_siren = unused_set_siren
};
1 change: 0 additions & 1 deletion board/boards/red.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,5 @@ const board board_red = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_clock_source_mode = unused_set_clock_source_mode,
.set_siren = unused_set_siren
};
1 change: 0 additions & 1 deletion board/boards/red_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ const board board_red_v2 = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_clock_source_mode = unused_set_clock_source_mode,
.set_siren = unused_set_siren
};
1 change: 0 additions & 1 deletion board/boards/tres.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,5 @@ const board board_tres = {
.set_fan_enabled = red_chiplet_set_fan_or_usb_load_switch,
.set_ir_power = tres_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_clock_source_mode = unused_set_clock_source_mode,
.set_siren = fake_siren_set
};
1 change: 0 additions & 1 deletion board/boards/uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,5 @@ const board board_uno = {
.set_fan_enabled = uno_set_fan_enabled,
.set_ir_power = uno_set_ir_power,
.set_phone_power = uno_set_phone_power,
.set_clock_source_mode = unused_set_clock_source_mode,
.set_siren = unused_set_siren
};
4 changes: 0 additions & 4 deletions board/boards/unused_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ void unused_set_phone_power(bool enabled) {
UNUSED(enabled);
}

void unused_set_clock_source_mode(uint8_t mode) {
UNUSED(mode);
}

void unused_set_siren(bool enabled) {
UNUSED(enabled);
}
Expand Down
1 change: 0 additions & 1 deletion board/boards/white.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,5 @@ const board board_white = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_clock_source_mode = unused_set_clock_source_mode,
.set_siren = unused_set_siren
};
4 changes: 0 additions & 4 deletions board/main_comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,6 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
}
}
break;
// **** 0xf5: set clock source mode
case 0xf5:
current_board->set_clock_source_mode(req->param1);
break;
// **** 0xf6: set siren enabled
case 0xf6:
siren_enabled = (req->param1 != 0U);
Expand Down
93 changes: 14 additions & 79 deletions board/stm32fx/clock_source.h
Original file line number Diff line number Diff line change
@@ -1,45 +1,8 @@

#define CLOCK_SOURCE_MODE_DISABLED 0U
#define CLOCK_SOURCE_MODE_FREE_RUNNING 1U
#define CLOCK_SOURCE_MODE_PWM 2U

#define CLOCK_SOURCE_PERIOD_MS 50U
#define CLOCK_SOURCE_PULSE_LEN_MS 2U

uint8_t clock_source_mode = CLOCK_SOURCE_MODE_DISABLED;

void TIM1_UP_TIM10_IRQ_Handler(void) {
if((TIM1->SR & TIM_SR_UIF) != 0) {
if(clock_source_mode == CLOCK_SOURCE_MODE_FREE_RUNNING) {
// Start clock pulse
set_gpio_output(GPIOB, 14, true);
set_gpio_output(GPIOB, 15, true);
set_gpio_output(GPIOC, 5, true);
}

// Reset interrupt
TIM1->SR &= ~(TIM_SR_UIF);
}
}

void TIM1_CC_IRQ_Handler(void) {
if((TIM1->SR & TIM_SR_CC1IF) != 0) {
if(clock_source_mode == CLOCK_SOURCE_MODE_FREE_RUNNING) {
// End clock pulse
set_gpio_output(GPIOB, 14, false);
set_gpio_output(GPIOB, 15, false);
set_gpio_output(GPIOC, 5, false);
}

// Reset interrupt
TIM1->SR &= ~(TIM_SR_CC1IF);
}
}

void clock_source_init(uint8_t mode){
void clock_source_init(void) {
// Setup timer
REGISTER_INTERRUPT(TIM1_UP_TIM10_IRQn, TIM1_UP_TIM10_IRQ_Handler, (1200U / CLOCK_SOURCE_PERIOD_MS) , FAULT_INTERRUPT_RATE_TIM1)
REGISTER_INTERRUPT(TIM1_CC_IRQn, TIM1_CC_IRQ_Handler, (1200U / CLOCK_SOURCE_PERIOD_MS) , FAULT_INTERRUPT_RATE_TIM1)
register_set(&(TIM1->PSC), ((APB2_FREQ*100U)-1U), 0xFFFFU); // Tick on 0.1 ms
register_set(&(TIM1->ARR), ((CLOCK_SOURCE_PERIOD_MS*10U) - 1U), 0xFFFFU); // Period
register_set(&(TIM1->CCMR1), 0U, 0xFFFFU); // No output on compare
Expand All @@ -50,49 +13,21 @@ void clock_source_init(uint8_t mode){
register_set_bits(&(TIM1->DIER), TIM_DIER_UIE | TIM_DIER_CC1IE); // Enable interrupts
register_set(&(TIM1->CR1), TIM_CR1_CEN, 0x3FU); // Enable timer

// Set mode
switch(mode) {
case CLOCK_SOURCE_MODE_DISABLED:
// No clock signal
NVIC_DisableIRQ(TIM1_UP_TIM10_IRQn);
NVIC_DisableIRQ(TIM1_CC_IRQn);

// Disable pulse if we were in the middle of it
set_gpio_output(GPIOB, 14, false);
set_gpio_output(GPIOB, 15, false);

clock_source_mode = CLOCK_SOURCE_MODE_DISABLED;
break;
case CLOCK_SOURCE_MODE_FREE_RUNNING:
// Clock signal is based on internal timer
NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
NVIC_EnableIRQ(TIM1_CC_IRQn);

clock_source_mode = CLOCK_SOURCE_MODE_FREE_RUNNING;
break;
case CLOCK_SOURCE_MODE_PWM:
// No interrupts
NVIC_DisableIRQ(TIM1_UP_TIM10_IRQn);
NVIC_DisableIRQ(TIM1_CC_IRQn);

// Set GPIO as timer channels
set_gpio_alternate(GPIOB, 14, GPIO_AF1_TIM1);
set_gpio_alternate(GPIOB, 15, GPIO_AF1_TIM1);
// No interrupts
NVIC_DisableIRQ(TIM1_UP_TIM10_IRQn);
NVIC_DisableIRQ(TIM1_CC_IRQn);

// Set PWM mode
register_set(&(TIM1->CCMR1), (0b110 << TIM_CCMR1_OC2M_Pos), 0xFFFFU);
register_set(&(TIM1->CCMR2), (0b110 << TIM_CCMR2_OC3M_Pos), 0xFFFFU);
// Set GPIO as timer channels
set_gpio_alternate(GPIOB, 14, GPIO_AF1_TIM1);
set_gpio_alternate(GPIOB, 15, GPIO_AF1_TIM1);

// Enable output
register_set(&(TIM1->BDTR), TIM_BDTR_MOE, 0xFFFFU);
// Set PWM mode
register_set(&(TIM1->CCMR1), (0b110 << TIM_CCMR1_OC2M_Pos), 0xFFFFU);
register_set(&(TIM1->CCMR2), (0b110 << TIM_CCMR2_OC3M_Pos), 0xFFFFU);

// Enable complementary compares
register_set_bits(&(TIM1->CCER), TIM_CCER_CC2NE | TIM_CCER_CC3NE);
// Enable output
register_set(&(TIM1->BDTR), TIM_BDTR_MOE, 0xFFFFU);

clock_source_mode = CLOCK_SOURCE_MODE_PWM;
break;
default:
print("Unknown clock source mode: "); puth(mode); print("\n");
break;
}
// Enable complementary compares
register_set_bits(&(TIM1->CCER), TIM_CCER_CC2NE | TIM_CCER_CC3NE);
}
7 changes: 0 additions & 7 deletions python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,6 @@ class Panda:
INTERNAL_DEVICES = (HW_TYPE_UNO, HW_TYPE_DOS)
HAS_OBD = (HW_TYPE_BLACK_PANDA, HW_TYPE_UNO, HW_TYPE_DOS, HW_TYPE_RED_PANDA, HW_TYPE_RED_PANDA_V2, HW_TYPE_TRES)

CLOCK_SOURCE_MODE_DISABLED = 0
CLOCK_SOURCE_MODE_FREE_RUNNING = 1

# first byte is for EPS scaling factor
FLAG_TOYOTA_ALT_BRAKE = (1 << 8)
FLAG_TOYOTA_STOCK_LONGITUDINAL = (2 << 8)
Expand Down Expand Up @@ -835,10 +832,6 @@ def get_fan_rpm(self):
def set_phone_power(self, enabled):
self._handle.controlWrite(Panda.REQUEST_OUT, 0xb3, int(enabled), 0, b'')

# ************** Clock Source **************
def set_clock_source_mode(self, mode):
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf5, int(mode), 0, b'')

# ****************** Siren *****************
def set_siren(self, enabled):
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf6, int(enabled), 0, b'')

0 comments on commit 1d732d4

Please sign in to comment.