Skip to content

Commit

Permalink
cleanup setting of TIM1_AUTORELOAD
Browse files Browse the repository at this point in the history
this replaces the manual setting with a calculation based on a 24kHz
nominal PWM
  • Loading branch information
tridge committed Nov 23, 2024
1 parent 28739ce commit 9d6eb05
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 16 deletions.
20 changes: 12 additions & 8 deletions Inc/targets.h
Original file line number Diff line number Diff line change
Expand Up @@ -3328,7 +3328,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
Expand Down Expand Up @@ -3357,7 +3356,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
Expand All @@ -3377,7 +3375,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
Expand Down Expand Up @@ -3411,7 +3408,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
Expand Down Expand Up @@ -3444,7 +3440,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
Expand All @@ -3462,7 +3457,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
Expand Down Expand Up @@ -3493,7 +3487,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
Expand All @@ -3514,7 +3507,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
Expand Down Expand Up @@ -3548,3 +3540,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


2 changes: 1 addition & 1 deletion Mcu/e230/Src/peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Mcu/f031/Src/peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Mcu/f051/Src/peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Mcu/f415/Src/peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Mcu/f421/Src/peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Mcu/g071/Src/peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Mcu/g431/Src/peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Mcu/l431/Src/peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9d6eb05

Please sign in to comment.