cancel
Showing results for 
Search instead for 
Did you mean: 

Cube generated code - Encoder mode on a F401 - No interrupts

dcurran2
Associate II
Posted on July 06, 2015 at 11:04

Folks,

Hopefully someone may be able to point me in the right direction

I have a F401RE nucleous board connected to a Quadrature encoder via Tim1

I have used Cube v4.8 to try set the device up ( and use it inconjunction with Keil V5.15) .

The Encoder code does not appear to function correctly in the fact that it appears only the IO is setup and the mode of the timer is set.  

I have noticed that in none cube examples given by others there is code to set up the time base etc  

So when I set the debugger going I can see DIR bit changing dependent upon the encoder movement but there is no count change.

Also I have had trouble getting any interrupts to work ( eg Serial port ) all seem to be configured in a similar manner to the original CMSIS hal but with the Cube CMSIS the interrupts fail to work 

Has anyone come across this? 

The encoder functionality at the mo is my priority 

this is the code for the tim1 producved by Cube what has caught my interst is that it sets 

 htim1.Init.Period = 0;

in none cube code it sets that to 0xFFFF 

plus .Period is a uint32_t  but it has warning in stm32f4xx_hal_tim.h about only setting it to a max of 0xFFFF

e.g.   uint32_t Period;            /*!< Specifies the period value to be loaded into the active

                                   Auto-Reload Register at the next update event.

                                   This parameter can be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF.  */

void MX_TIM1_Init(void)

{

  TIM_Encoder_InitTypeDef sConfig;

  TIM_MasterConfigTypeDef sMasterConfig;

  htim1.Instance = TIM1;

  htim1.Init.Prescaler = 0;

  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim1.Init.Period = 0;

  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  htim1.Init.RepetitionCounter = 0;

  sConfig.EncoderMode = TIM_ENCODERMODE_TI12;

  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;

  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;

  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;

  sConfig.IC1Filter = 0;

  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;

  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;

  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;

  sConfig.IC2Filter = 0;

  HAL_TIM_Encoder_Init(&htim1, &sConfig);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);

}

2 REPLIES 2
dcurran2
Associate II
Posted on July 06, 2015 at 14:17

Folks at ST maintaining the Cube code base.

Change the Timebase period to 0xFFFF then your encoder code will work 

I suggest you add

  • A function to get the count from htim1
  • A function to reset the encoder count 

chrif
Associate II
Posted on July 08, 2015 at 12:05

Hi Danny,

1-Related to the period, you should set it through TIM1 configuration (Parameter settings)  in cube Mx, so you will obtain the period you want. The value 0 is a default one.

2-Also, you can enable interrupts  through TIM1 configuration (NVIC Settings).

3-Have a look to the example under the F4 package STM32F401RE-Nucleo\Examples\TIM.

Hope this can help you

Regards