cancel
Showing results for 
Search instead for 
Did you mean: 

Why chaining TIM5 to TIM3 fail ?

nicolas
Associate III

I'm using a STM32L486RG and i try to chain TIM5 to TIM3 and it fail (TIM3->CNT doesn't increment). The other chaining combinations work correctly. Here is my test code:

#include <assert.h>
#include <stdbool.h>
 
#include "stm32l4xx.h"
 
#define TIM_MASTER       TIM5
#define TIM_SLAVE        TIM3
#define TIM_TS_ITR       TIM_TS_ITR2 // look for "TIMx Internal trigger connection" in RM0351
 
static TIM_HandleTypeDef master_timer;
static TIM_HandleTypeDef slave_timer;
 
/**
 * @brief  This function is executed in case of error occurrence.
 * @param  file: The file name as string.
 * @param  line: The line in file as a number.
 * @retval None
 */
void _Error_Handler(char *file, int line)
{
    assert(false);
}
 
void master_init(void)
{
    TIM_MasterConfigTypeDef master_config;
 
    master_timer.Instance = TIM_MASTER;
    master_timer.Init.Prescaler = (SystemCoreClock / 1000000) - 1; // 1 us tick
    master_timer.Init.CounterMode = TIM_COUNTERMODE_UP;
    master_timer.Init.Period = 0xFFFF;
    master_timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    if (HAL_TIM_Base_Init(&master_timer) != HAL_OK) {
        _Error_Handler(__FILE__, __LINE__);
    }
 
    master_config.MasterOutputTrigger = TIM_TRGO_UPDATE;
    master_config.MasterOutputTrigger2 = TIM_TRGO2_UPDATE;
    master_config.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    if (HAL_TIMEx_MasterConfigSynchronization(&master_timer, &master_config)
            != HAL_OK) {
        _Error_Handler(__FILE__, __LINE__);
    }
 
    HAL_TIM_Base_Start(&master_timer);
}
 
void slave_init(void)
{
    TIM_SlaveConfigTypeDef slave_config;
 
    slave_timer.Instance = TIM_SLAVE;
    slave_timer.Init.Prescaler = 0;
    slave_timer.Init.CounterMode = TIM_COUNTERMODE_UP;
    slave_timer.Init.Period = 0xFFFF;
    slave_timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    if (HAL_TIM_Base_Init(&slave_timer) != HAL_OK) {
        _Error_Handler(__FILE__, __LINE__);
    }
 
    slave_config.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
    slave_config.InputTrigger = TIM_TS_ITR;
    if (HAL_TIM_SlaveConfigSynchronization(&slave_timer, &slave_config)
            != HAL_OK) {
        _Error_Handler(__FILE__, __LINE__);
    }
 
    HAL_TIM_Base_Start(&slave_timer);
}
 
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_handle)
{
    if (tim_handle->Instance == TIM1) {
        __HAL_RCC_TIM1_CLK_ENABLE();
    } else if (tim_handle->Instance == TIM2) {
        __HAL_RCC_TIM2_CLK_ENABLE();
    } else if (tim_handle->Instance == TIM3) {
        __HAL_RCC_TIM3_CLK_ENABLE();
    } else if (tim_handle->Instance == TIM4) {
        __HAL_RCC_TIM4_CLK_ENABLE();
    } else if (tim_handle->Instance == TIM5) {
        __HAL_RCC_TIM5_CLK_ENABLE();
    } else if (tim_handle->Instance == TIM8) {
        __HAL_RCC_TIM8_CLK_ENABLE();
    } else if (tim_handle->Instance == TIM15) {
        __HAL_RCC_TIM15_CLK_ENABLE();
    }
}
 
int main(void)
{
    HAL_Init();
 
    master_init();
    slave_init();
 
    while (true) {
 
    }
}
 
 
#ifdef  USE_FULL_ASSERT
 
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
    assert(false);
}
 
#endif

4 REPLIES 4

This was discussed last week, there is a typo in the.manual. Use TIM15 as the source not TIM5. ​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

This is the thread Clive refers to: https://community.st.com/s/question/0D50X00009sWbv9SAC/masterslave-problem-on-tim3-in-stm32l476-slave-not-counting-when-itr2-selected

Isn't it strange that the same question pops up independently twice in a short time period?

JW

These things are like a gravity well distorting space-time. Would have hunted for the cite, but pasting from the phone is such a pain, and I miss the auto-save of Jive..​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
nicolas
Associate III

Thanks for the answers. Too bad i didn't saw this thread sooner.