Revert "F4: cleanup clock source (#1160)"

This reverts commit f0f67ccf85.
This commit is contained in:
Adeeb Shihadeh 2022-11-21 20:31:01 -08:00
parent 2e90b6f308
commit c075050d5d
14 changed files with 124 additions and 15 deletions

View File

@ -202,5 +202,6 @@ 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
};

View File

@ -10,6 +10,7 @@ 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);
@ -35,6 +36,7 @@ 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;
};

View File

@ -110,6 +110,10 @@ 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);
}
@ -178,7 +182,7 @@ void dos_init(void) {
}
// Init clock source (camera strobe) using PWM
clock_source_init();
dos_set_clock_source_mode(CLOCK_SOURCE_MODE_PWM);
}
const harness_configuration dos_harness_config = {
@ -222,5 +226,6 @@ 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
};

View File

@ -56,5 +56,6 @@ 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
};

View File

@ -94,5 +94,6 @@ 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
};

View File

@ -194,5 +194,6 @@ 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
};

View File

@ -25,5 +25,6 @@ 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
};

View File

@ -57,5 +57,6 @@ 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 = unused_set_siren
};

View File

@ -262,5 +262,6 @@ 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
};

View File

@ -14,6 +14,10 @@ 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);
}

View File

@ -258,5 +258,6 @@ 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
};

View File

@ -525,6 +525,10 @@ 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);

View File

@ -1,19 +1,98 @@
void clock_source_init(void) {
// 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);
#define CLOCK_SOURCE_MODE_DISABLED 0U
#define CLOCK_SOURCE_MODE_FREE_RUNNING 1U
#define CLOCK_SOURCE_MODE_PWM 2U
// Set PWM mode
register_set(&(TIM1->CCMR1), (0b110 << TIM_CCMR1_OC2M_Pos), 0xFFFFU);
register_set(&(TIM1->CCMR2), (0b110 << TIM_CCMR2_OC3M_Pos), 0xFFFFU);
#define CLOCK_SOURCE_PERIOD_MS 50U
#define CLOCK_SOURCE_PULSE_LEN_MS 2U
// Enable output
register_set(&(TIM1->BDTR), TIM_BDTR_MOE, 0xFFFFU);
uint8_t clock_source_mode = CLOCK_SOURCE_MODE_DISABLED;
// Enable complementary compares
register_set_bits(&(TIM1->CCER), TIM_CCER_CC2NE | TIM_CCER_CC3NE);
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){
// 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
register_set(&(TIM1->CCER), TIM_CCER_CC1E, 0xFFFFU); // Enable compare 1
register_set(&(TIM1->CCR1), (CLOCK_SOURCE_PULSE_LEN_MS*10U), 0xFFFFU); // Compare 1 value
register_set(&(TIM1->CCR2), (CLOCK_SOURCE_PULSE_LEN_MS*10U), 0xFFFFU); // Compare 1 value
register_set(&(TIM1->CCR3), (CLOCK_SOURCE_PULSE_LEN_MS*10U), 0xFFFFU); // Compare 1 value
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);
// Set PWM mode
register_set(&(TIM1->CCMR1), (0b110 << TIM_CCMR1_OC2M_Pos), 0xFFFFU);
register_set(&(TIM1->CCMR2), (0b110 << TIM_CCMR2_OC3M_Pos), 0xFFFFU);
// Enable output
register_set(&(TIM1->BDTR), TIM_BDTR_MOE, 0xFFFFU);
// Enable complementary compares
register_set_bits(&(TIM1->CCER), TIM_CCER_CC2NE | TIM_CCER_CC3NE);
clock_source_mode = CLOCK_SOURCE_MODE_PWM;
break;
default:
puts("Unknown clock source mode: "); puth(mode); puts("\n");
break;
}
}

View File

@ -204,6 +204,9 @@ 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)
@ -837,6 +840,10 @@ class Panda:
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'')