Add basic HRTIM implementation for half_bridge

This commit is contained in:
Martin Jäger
2021-02-05 18:12:31 +01:00
parent fc71934010
commit 6aa015a140
2 changed files with 160 additions and 1 deletions
@@ -107,6 +107,57 @@
spi-uext = &spi1;
i2c-uext = &i2c1;
};
soc {
hrtim1: timers@40016800 {
compatible = "st,stm32-timers";
reg = <0x40016800 0x400>;
clocks = <&rcc STM32_CLOCK_BUS_APB2 0x04000000>;
interrupts = <67 0>, <68 0>, <69 0>, <70 0>,
<71 0>, <72 0>, <73 0>, <74 0>;
interrupt-names = "master", "tima", "timb", "timc",
"timd", "time", "flt", "timf";
status = "okay";
label = "HRTIM_1";
};
/* below nodes are required until following PR is merged:
* https://github.com/zephyrproject-rtos/hal_stm32/pull/87
*/
pinctrl: pin-controller@48000000 {
hrtim1_cha1_pa8: hrtim1_cha1_pa8 {
pinmux = <STM32_PINMUX('A', 8, AF13)>;
};
hrtim1_cha2_pa9: hrtim1_cha2_pa9 {
pinmux = <STM32_PINMUX('A', 9, AF13)>;
};
hrtim1_chb1_pa10: hrtim1_chb1_pa10 {
pinmux = <STM32_PINMUX('A', 10, AF13)>;
};
hrtim1_chb2_pa11: hrtim1_chb2_pa11 {
pinmux = <STM32_PINMUX('A', 11, AF13)>;
};
hrtim1_chc1_pb12: hrtim1_chc1_pb12 {
pinmux = <STM32_PINMUX('B', 12, AF13)>;
};
hrtim1_chc2_pb13: hrtim1_chc2_pb13 {
pinmux = <STM32_PINMUX('B', 13, AF13)>;
};
hrtim1_chd1_pb14: hrtim1_chd1_pb14 {
pinmux = <STM32_PINMUX('B', 14, AF13)>;
};
hrtim1_chd2_pb15: hrtim1_chd2_pb15 {
pinmux = <STM32_PINMUX('B', 15, AF13)>;
};
};
};
};
&usart1 {
@@ -132,6 +183,19 @@
status = "okay";
};
&hrtim1 {
status = "okay";
halfbridge {
compatible = "half-bridge";
pinctrl-0 = <&hrtim1_cha1_pa8 &hrtim1_cha2_pa9>;
frequency = <70000>;
deadtime = <300>;
};
};
/*
// alternative to HRTIM
&timers1 {
status = "okay";
@@ -142,6 +206,7 @@
deadtime = <300>;
};
};
*/
&rtc {
status = "okay";
+95 -1
View File
@@ -219,7 +219,101 @@ bool half_bridge_enabled()
return TIM1->BDTR & TIM_BDTR_MOE;
}
#endif // TIM1
#elif TIMER_ADDR == HRTIM1_BASE
/*
* Below initial HRTIM implementation only works for channel A and it does not make use of the
* possible higher resolution yet.
*/
#include <stm32_ll_system.h>
#include <stm32_ll_bus.h>
static void tim_init_registers(int freq_kHz)
{
stm32_dt_pinctrl_configure(tim_pinctrl, ARRAY_SIZE(tim_pinctrl), TIMER_ADDR);
// Enable HRTIM1 clock
RCC->APB2ENR |= RCC_APB2ENR_HRTIM1EN;
// Enable periodic calibration of delay-locked loop (DLL) with lowest calibration period
HRTIM1->sCommonRegs.DLLCR |= HRTIM_DLLCR_CALEN | HRTIM_DLLCR_CALRTE_0 | HRTIM_DLLCR_CALRTE_1;
// Wait for calibration to finish
while ((HRTIM1_COMMON->ISR & HRTIM_ISR_DLLRDY) == 0) {;}
// Prescaler 32 --> SystemClock (same as normal timers like TIM1)
HRTIM1_TIMA->TIMxCR |= (5U << HRTIM_TIMCR_CK_PSC_Pos);
// Prescaler 8 / 2^3 --> same frequency as HRTIM
HRTIM1_TIMA->DTxR |= (3U << HRTIM_DTR_DTPRSC_Pos);
// Continuous mode operation
HRTIM1_TIMA->TIMxCR |= HRTIM_TIMCR_CONT;
// Enable preloading with timer reset update trigger
//HRTIM1_TIMA->TIMxCR |= HRTIM_TIMCR_PREEN | HRTIM_TIMCR_TRSTU;
// Enable preloading with timer E update trigger
//HRTIM1_TIMA->TIMxCR |= HRTIM_TIMCR_PREEN | HRTIM_TIMCR_TEU;
// Timer period register
HRTIM1_TIMA->PERxR = SystemCoreClock / (freq_kHz * 1000) + 1;
HRTIM1_TIMA->SETx1R = HRTIM_SET1R_PER;
HRTIM1_TIMA->RSTx1R = HRTIM_SET1R_CMP1;
HRTIM1_TIMA->OUTxR = HRTIM_OUTR_DTEN;
// Set deadtime values and lock deadtime signs
HRTIM1_TIMA->DTxR |=
HRTIM_DTR_DTFSLK | (tim_dt_clocks << 16U) |
HRTIM_DTR_DTRSLK | tim_dt_clocks;
// activate trigger for ADC
HRTIM1_COMMON->CR1 = HRTIM_CR1_ADC1USRC_0; // ADC trigger update: Timer A
HRTIM1_COMMON->ADC1R = HRTIM_ADC1R_AD1TAC3; // ADC trigger event: Timer A compare 3
// Start timer A
HRTIM1->sMasterRegs.MCR = HRTIM_MCR_TACEN;
}
void half_bridge_start()
{
HRTIM1->sCommonRegs.OENR = HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN;
}
void half_bridge_stop()
{
HRTIM1->sCommonRegs.ODISR = HRTIM_ODISR_TA1ODIS | HRTIM_ODISR_TA2ODIS;
}
uint16_t half_bridge_get_arr()
{
return HRTIM1_TIMA->PERxR;
}
uint16_t half_bridge_get_ccr()
{
return HRTIM1_TIMA->CMP1xR;
}
void half_bridge_set_ccr(uint16_t ccr)
{
HRTIM1_TIMA->CMP1xR = clamp_ccr(ccr);
// Trigger ADC for current measurement in the middle of the cycle.
// A negative offset of 80 clocks was found to improve current measurement accuracy and
// compensate the ADC delay (no risk of underflow as CCR is around 214 even at 0.1% duty).
HRTIM1_TIMA->CMP3xR = HRTIM1_TIMA->CMP1xR / 2 - 80;
}
bool half_bridge_enabled()
{
return (HRTIM1->sCommonRegs.OENR & (HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN)) > 0;
}
#endif // HRTIM1
#else // UNIT_TEST