cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 strange timer1 behavior in Master-Slave Configuration - mb code issue

hof2
Associate II
Posted on July 14, 2015 at 15:40

I'm currently working on an embedded system which is meant to output a specific pulse sequence at equidistant timings. Therefore, I use the STM32 - DISCO board with the FreeRTOS kernel, as a first test. I configured TIM3 as Master triggering TIM1. TIM1 is triggered with a frequency of 1Hz or every second. TIM3 then generates a pulse sequence on it's output. I configured the TIM3 output to PB4, and TIM1 to PA9. This configuration works as expected, but now I wanted to change the configuration on the fly with a structure that stores my settings and can be passed as pointer to a function that configures both timers.

As a first step, I generated a data structure and initialized it in my timer function to configure TIM3.


PG_ERR TIM_Master_Init(
void
){


PG_HandleTypeDef hPG_timer;

hPG_timer.PLS.Prescaler = 139;

hPG_timer.PLS.Period = 60000;

hPG_timer.PLS.DutyCycle = 30000;

hPG_timer.PLS.RepetitionCounter = 5;

hPG_timer.PLS.PercentChange = 0;


/* Timer3 handler declaration: Master */

TIM_HandleTypeDef TimMasterHandle;


/* Master configuration structure */

TIM_MasterConfigTypeDef sMasterConfig;


/* Output compare structure */

TIM_OC_InitTypeDef sOCConfig;


__TIM3_CLK_ENABLE();


PG_ERR xPGERR = PG_ERR_NONE;

/* Compute the prescaler value to have TIM3 counter clock equal to 60 KHz */


/* Set TIMx instance */

TimMasterHandle.Instance = MASTER_TIM;

/* Master configuration: TIM3 */

TimMasterHandle.Init.Period = 60000 - 1;

TimMasterHandle.Init.Prescaler = 1399;

TimMasterHandle.Init.ClockDivision = 0;

TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;

if
(HAL_TIM_PWM_Init(&TimMasterHandle) != HAL_OK){

xPGERR = PG_ERR_TIM;

}


/* Configure the PWM_channel_1 */

sOCConfig.OCMode = TIM_OCMODE_PWM1;

sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH;

sOCConfig.OCFastMode = TIM_OCFAST_ENABLE;

sOCConfig.Pulse = 30000;


if
(HAL_TIM_PWM_ConfigChannel(&TimMasterHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK){

xPGERR = PG_ERR_TIM;

}


/* Configure TIM3 as master & use the update event as Trigger Output (TRGO) */

sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1REF;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;

if
(HAL_TIMEx_MasterConfigSynchronization(&TimMasterHandle, &sMasterConfig) != HAL_OK){

xPGERR = PG_ERR_TIM;

} 


/* Start PWM Timer3*/

if
(HAL_TIM_PWM_Start(&TimMasterHandle, TIM_CHANNEL_1) != HAL_OK){

xPGERR = PG_ERR_TIM;

}


if
(HAL_TIM_Base_Start_IT(&TimMasterHandle) != HAL_OK){

xPGERR = PG_ERR_TIM;

}


return
xPGERR;

}

1) With this configuration i get some really odd behaviour. After including the data structure:

1.
PG_HandleTypeDef hPG_timer;
2.
hPG_timer.PLS.Prescaler = 139;
3.
hPG_timer.PLS.Period = 60000;
4.
hPG_timer.PLS.DutyCycle = 30000;
5.
hPG_timer.PLS.RepetitionCounter = 5;
6.
hPG_timer.PLS.PercentChange = 0;

After that code snipped, I don't get any output on PIN PB4 (Master - TIM3) which should still be toggled at 1Hz.

2) To get even more confusing, when I substitute the code block with a handle:

1.
PG_HandleTypeDef hPG_timer;
2.
PG_HandleTypeDef *hPG_timer;
3.
hPG_timer = &hPG_timer_config;
4.
hPG_timer.PLS.Prescaler = 139;
5.
hPG_timer.PLS.Period = 60000;
6.
hPG_timer.PLS.DutyCycle = 30000;
7.
hPG_timer.PLS.RepetitionCounter = 5;
8.
hPG_timer.PLS.PercentChange = 0;

Now I can see the output on PB4 (Master - TIM3) with 1Hz but the output polarity of PA9 (Slave - TIM1) is reversed.

I tried to investigate the problem, I focused on the stack/heap of the FreeRTOS. I tested the system with large heap/stack = (uint32_t) 65535; I couldn't observe any changes in the behaviour.

I hope somebody came across a similar problem or has an idea how to resolve this. I'm thankful for any input in this, I'm at the end of my knowledge unfortunately.

Edit: I spend some more time with the problem, and I think I can be more specific. In case of the pointer use, the TimMasterHandle is locked right after the initialization. If I unlock the handle TimMasterHandle.lock = HAL_UNLOCK; all works well, but that is just masking the problem and i would like to know where this is coming from.

It some how looks still like an heap or stack problem. Is there any way, I can check for heap or stack overflow. I am using Keil uVision 5.

Thank you for your time and help,

eimer

#stm32-disco-timer-c-embedded
0 REPLIES 0