From c2a425c1e4d614f8e6fca85a7c04432cacbb88c0 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 11 Nov 2024 15:14:13 +1100 Subject: [PATCH] cleanup setting of TIM1_AUTORELOAD this replaces the manual setting with a calculation based on a 24kHz nominal PWM --- Inc/targets.h | 20 ++++++++++++-------- Mcu/e230/Src/peripherals.c | 2 +- Mcu/f031/Src/peripherals.c | 2 +- Mcu/f051/Src/peripherals.c | 2 +- Mcu/f415/Src/peripherals.c | 2 +- Mcu/f421/Src/peripherals.c | 2 +- Mcu/g071/Src/peripherals.c | 2 +- Mcu/g431/Src/peripherals.c | 2 +- Mcu/l431/Src/peripherals.c | 2 +- 9 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Inc/targets.h b/Inc/targets.h index 8f13e3de..012fbe23 100644 --- a/Inc/targets.h +++ b/Inc/targets.h @@ -3322,7 +3322,6 @@ #define TEN_KHZ_TIMER TIM6 #define UTILITY_TIMER TIM17 #define COM_TIMER TIM14 -#define TIM1_AUTORELOAD 1999 #define APPLICATION_ADDRESS 0x08001000 #define MAIN_COMP COMP1 #define EXTI_LINE LL_EXTI_LINE_21 @@ -3349,7 +3348,6 @@ #define TEN_KHZ_TIMER TIM16 #define UTILITY_TIMER TIM17 #define COM_TIMER TIM14 -#define TIM1_AUTORELOAD 1999 #define APPLICATION_ADDRESS 0x08001000 #define TARGET_MIN_BEMF_COUNTS 3 // #define USE_SERIAL_TELEMETRY // moved to individual ESCs @@ -3369,7 +3367,6 @@ #define TEN_KHZ_TIMER TIM6 #define UTILITY_TIMER TIM17 #define COM_TIMER TIM14 -#define TIM1_AUTORELOAD 2667 #define APPLICATION_ADDRESS 0x08001000 #define MAIN_COMP COMP2 #define EXTI_LINE LL_EXTI_LINE_18 @@ -3403,7 +3400,6 @@ #define TEN_KHZ_TIMER TIM6 #define UTILITY_TIMER TIM17 #define COM_TIMER TIM16 -#define TIM1_AUTORELOAD 6249 #define APPLICATION_ADDRESS 0x08001000 #define MAIN_COMP COMP2 #define EXTI_LINE LL_EXTI_LINE_22 @@ -3436,7 +3432,6 @@ #define TEN_KHZ_TIMER TIMER13 #define UTILITY_TIMER TIMER16 #define COM_TIMER TIMER15 -#define TIM1_AUTORELOAD 3000 #define APPLICATION_ADDRESS 0x08001000 #define EXTI_LINE EXTI_21 #define TARGET_MIN_BEMF_COUNTS 4 @@ -3454,7 +3449,6 @@ #define TEN_KHZ_TIMER TMR14 #define UTILITY_TIMER TMR17 #define COM_TIMER TMR16 -#define TIM1_AUTORELOAD 5000 #define APPLICATION_ADDRESS 0x08001000 #define EXTI_LINE EXINT_LINE_21 #ifndef TARGET_MIN_BEMF_COUNTS @@ -3485,7 +3479,6 @@ #define TEN_KHZ_TIMER TMR9 #define UTILITY_TIMER TMR10 #define COM_TIMER TMR11 -#define TIM1_AUTORELOAD 6000 #define APPLICATION_ADDRESS 0x08001000 #define EXTI_LINE EXINT_LINE_19 #define TARGET_MIN_BEMF_COUNTS 3 @@ -3506,7 +3499,6 @@ #define TEN_KHZ_TIMER TIM6 #define UTILITY_TIMER TIM7 #define COM_TIMER TIM16 -#define TIM1_AUTORELOAD 3334 #define APPLICATION_ADDRESS 0x08001000 #ifdef USE_COMP_2 #define MAIN_COMP COMP2 @@ -3540,3 +3532,15 @@ #undef EEPROM_START_ADD #define EEPROM_START_ADD (uint32_t)0x0801F800 #endif + +#ifndef NOMINAL_PWM +// use a nominal PWM for commutation via TIM1 of 24kHz +#define NOMINAL_PWM 24000U +#endif + +#ifndef TIM1_AUTORELOAD +// calculate commutation timer ARR based on a nominal 24kHz PWM +#define TIM1_AUTORELOAD ((uint16_t)(CPU_FREQUENCY_MHZ * 1000U * 1000U / NOMINAL_PWM)-1) +#endif + + diff --git a/Mcu/e230/Src/peripherals.c b/Mcu/e230/Src/peripherals.c index ae7f46a9..5a18debc 100644 --- a/Mcu/e230/Src/peripherals.c +++ b/Mcu/e230/Src/peripherals.c @@ -86,7 +86,7 @@ void TIM0_Init(void) timer_initpara.prescaler = 0; timer_initpara.alignedmode = TIMER_COUNTER_EDGE; timer_initpara.counterdirection = TIMER_COUNTER_UP; - timer_initpara.period = 3000; + timer_initpara.period = TIM1_AUTORELOAD; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_initpara.repetitioncounter = 0; timer_init(TIMER0, &timer_initpara); diff --git a/Mcu/f031/Src/peripherals.c b/Mcu/f031/Src/peripherals.c index ed0392a8..7b08adee 100644 --- a/Mcu/f031/Src/peripherals.c +++ b/Mcu/f031/Src/peripherals.c @@ -131,7 +131,7 @@ void MX_TIM1_Init(void) TIM_InitStruct.Prescaler = 0; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; - TIM_InitStruct.Autoreload = 1999; + TIM_InitStruct.Autoreload = TIM1_AUTORELOAD; TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; TIM_InitStruct.RepetitionCounter = 0; LL_TIM_Init(TIM1, &TIM_InitStruct); diff --git a/Mcu/f051/Src/peripherals.c b/Mcu/f051/Src/peripherals.c index f0ec5ad7..e178ae9e 100644 --- a/Mcu/f051/Src/peripherals.c +++ b/Mcu/f051/Src/peripherals.c @@ -152,7 +152,7 @@ void MX_TIM1_Init(void) TIM_InitStruct.Prescaler = 0; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; - TIM_InitStruct.Autoreload = 1999; + TIM_InitStruct.Autoreload = TIM1_AUTORELOAD; TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; TIM_InitStruct.RepetitionCounter = 0; LL_TIM_Init(TIM1, &TIM_InitStruct); diff --git a/Mcu/f415/Src/peripherals.c b/Mcu/f415/Src/peripherals.c index 03a2340e..a72cdaab 100644 --- a/Mcu/f415/Src/peripherals.c +++ b/Mcu/f415/Src/peripherals.c @@ -160,7 +160,7 @@ void TIM1_Init(void) gpio_pin_remap_config(TMR1_GMUX_0001, TRUE); - TMR1->pr = 3000; + TMR1->pr = TIM1_AUTORELOAD; TMR1->div = 0; TMR1->cm1 = 0x6868; // Channel 1 and 2 in PWM output mode diff --git a/Mcu/f421/Src/peripherals.c b/Mcu/f421/Src/peripherals.c index 7bd12e35..5bad32c5 100644 --- a/Mcu/f421/Src/peripherals.c +++ b/Mcu/f421/Src/peripherals.c @@ -90,7 +90,7 @@ void MX_IWDG_Init(void) void TIM1_Init(void) { crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE); - TMR1->pr = 3000; + TMR1->pr = TIM1_AUTORELOAD; TMR1->div = 0; TMR1->cm1 = 0x6868; // Channel 1 and 2 in PWM output mode diff --git a/Mcu/g071/Src/peripherals.c b/Mcu/g071/Src/peripherals.c index 0c725ffd..a15b8854 100644 --- a/Mcu/g071/Src/peripherals.c +++ b/Mcu/g071/Src/peripherals.c @@ -278,7 +278,7 @@ void MX_TIM1_Init(void) /* USER CODE END TIM1_Init 1 */ TIM_InitStruct.Prescaler = 0; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; - TIM_InitStruct.Autoreload = 3000; + TIM_InitStruct.Autoreload = TIM1_AUTORELOAD; TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; TIM_InitStruct.RepetitionCounter = 0; LL_TIM_Init(TIM1, &TIM_InitStruct); diff --git a/Mcu/g431/Src/peripherals.c b/Mcu/g431/Src/peripherals.c index 2fa810ea..24f5b29b 100644 --- a/Mcu/g431/Src/peripherals.c +++ b/Mcu/g431/Src/peripherals.c @@ -190,7 +190,7 @@ void MX_TIM1_Init(void) LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1); TIM_InitStruct.Prescaler = 0; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; - TIM_InitStruct.Autoreload = 6249; + TIM_InitStruct.Autoreload = TIM1_AUTORELOAD; TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; TIM_InitStruct.RepetitionCounter = 0; LL_TIM_Init(TIM1, &TIM_InitStruct); diff --git a/Mcu/l431/Src/peripherals.c b/Mcu/l431/Src/peripherals.c index f0429fa6..a567620d 100644 --- a/Mcu/l431/Src/peripherals.c +++ b/Mcu/l431/Src/peripherals.c @@ -241,7 +241,7 @@ void MX_TIM1_Init(void) TIM_InitStruct.Prescaler = 0; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; - TIM_InitStruct.Autoreload = 3334; + TIM_InitStruct.Autoreload = TIM1_AUTORELOAD; TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; TIM_InitStruct.RepetitionCounter = 0; LL_TIM_Init(TIM1, &TIM_InitStruct);