2024-08-15 07:21 AM
Hello,
I am struggling to get the TIM1 of a STM32F411 to be triggered by an external GPIO.
What I want to achieve is having ETR1 (PA12) to trigger TIM1 and have 4 repetition of 199 period
I am using STCubeMX and I must do something wrong as it is not working, my setup is
Slave Mode : Trigger:
Trigger Source: ETR1 (PA12)
Clock Source: Disable
Channel1 Output compare No Output
Counter Settings:
Period : 199
RCR: 4
AutoReload: disable
Slave Mode controller : Trigger Mode
Trigger :
Trigger Polarity : non inverted
Prescaler not used
Filter: 0
Output compare No Output Channel 1
Mode: Frozen
Pulse : 0
CH Idle State Reset.
when I use the code
2024-08-15 07:41 AM
What signal is on PA12?
Read out and check/post content of TIM and relevant GPIO registers. If you are sure there is no signal on ETR which would trigger the timer, step through the Cube/HAL function you are calling (Cube is open source so you can debug its functions as your own code) while observing the timer registers, to find out why does it enable the timer's counter.
If you want the timer to automatically stop after a preset number of repetitions, you have to set it in One-Pulse Mode (i.e. set TIMx_CR1.OPM).
JW
2024-08-15 08:02 AM
Well,
This is the code generated by STCubeMX
static void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_SlaveConfigTypeDef sSlaveConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 199;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
sSlaveConfig.InputTrigger = TIM_TS_ETRF;
sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
sSlaveConfig.TriggerFilter = 0;
if (HAL_TIM_SlaveConfigSynchro(&htim1, &sSlaveConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_TIMING;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
With auto reload : disable I should not get counter to be reloaded ?
with One pulse mode I do not get my 4 repetitions
2024-08-15 08:21 AM
The mcu does not work out of source code nor "libraries" such as Cube/HAL, but it works out of its registers. That's why I recommend debugging by looking at the registers content.
> with One pulse mode I do not get my 4 repetitions
But you also present us inconsistent information:
> RCR: 4
> htim1.Init.RepetitionCounter = 0;
For the functionality of Autoreload preload, read TIMx_CR1.ARPE description in RM.
JW
2024-08-16 11:50 PM
Hello Jan,
Regarding ARPE, my bad sorry I had a misconception around it.
Regarding RCR, I was trying many things and I set the value to 4.
How do I get 4 overflow interrupt with RCR ? the UEV event is sent only when RCR=0, am I right ?
Vincent
2024-08-17 02:50 AM
> How do I get 4 overflow interrupt with RCR?
You don't.
You can get 4 CCx interrupts, though.
JW