AnsweredAssumed Answered

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

Question asked by on Jul 14, 2015

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.

01.PG_ERR TIM_Master_Init(void){
03.PG_HandleTypeDef hPG_timer;
04.hPG_timer.PLS.Prescaler = 139;
05.hPG_timer.PLS.Period = 60000;
06.hPG_timer.PLS.DutyCycle = 30000;
07.hPG_timer.PLS.RepetitionCounter = 5;
08.hPG_timer.PLS.PercentChange = 0;
10./* Timer3 handler declaration: Master */
11.TIM_HandleTypeDef           TimMasterHandle;
13./* Master configuration structure */
14.TIM_MasterConfigTypeDef sMasterConfig;
16./* Output compare structure */
17.TIM_OC_InitTypeDef          sOCConfig;
22./* Compute the prescaler value to have TIM3 counter clock equal to 60 KHz */
24./* Set TIMx instance */
25.TimMasterHandle.Instance = MASTER_TIM;
26./* Master configuration: TIM3  */
27.TimMasterHandle.Init.Period = 60000 - 1;
28.TimMasterHandle.Init.Prescaler = 1399;
29.TimMasterHandle.Init.ClockDivision = 0;
30.TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
31.if (HAL_TIM_PWM_Init(&TimMasterHandle) != HAL_OK){
32.    xPGERR = PG_ERR_TIM;
35./* Configure the PWM_channel_1  */
36.sOCConfig.OCMode     = TIM_OCMODE_PWM1;
37.sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
38.sOCConfig.OCFastMode = TIM_OCFAST_ENABLE;
39.sOCConfig.Pulse = 30000;
41.if (HAL_TIM_PWM_ConfigChannel(&TimMasterHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK){
42.    xPGERR = PG_ERR_TIM;
45./* Configure TIM3 as master & use the update event as Trigger Output (TRGO) */
46.sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1REF;
47.sMasterConfig.MasterSlaveMode           = TIM_MASTERSLAVEMODE_ENABLE;
48.if(HAL_TIMEx_MasterConfigSynchronization(&TimMasterHandle, &sMasterConfig) != HAL_OK){
49.    xPGERR = PG_ERR_TIM;
52./* Start PWM Timer3*/
53.if(HAL_TIM_PWM_Start(&TimMasterHandle, TIM_CHANNEL_1) != HAL_OK){
54.    xPGERR = PG_ERR_TIM;
57.if(HAL_TIM_Base_Start_IT(&TimMasterHandle) != HAL_OK){
58.    xPGERR = PG_ERR_TIM;
61.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.10.

Thank you for your time and help,