TIM6 set for 1ms timer using ST Low Layer API

This is true for the STM32CubeMX 4.22.1 and STM32CubeL1 driver 1.8.0. Actually, CubeMX does not generate the complete functional code and the snippet included will serve for me as a future reference.

TIM6 general timer is used as an alternative to the Systick until the Low Layer API will have the same features for the 1ms system tick as HAL API does.

/* //////////////////////////////////////////////////////
* main.h file
*
*/

volatile uint32_t my_tick;
void TimerUpdate_Callback(void);


/* //////////////////////////////////////////////////////
* main.c file
*
*/


volatile uint32_t my_current, my_previous;
const uint32_t led_ms_delay = 500; /* delay in milliseconds */

int main(void)
{
my_tick = 0;
my_previous = 0;

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
LL_Init();

/* Configure the system clock */
SystemClock_Config();

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM6_Init();


/* Infinite loop */
while (1){
my_current = my_tick;
if((my_current - my_previous) >= led_ms_delay){
my_previous = my_current;
LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
}
}


/*
* Backup for the SystemClock_Config function.
* Code to be added at the beginning, inside the function:
*
* LL_FLASH_Enable64bitAccess(); // added manually 😦
*
*/


/* TIM6 init function */
static void MX_TIM6_Init(void)
{
LL_TIM_InitTypeDef TIM_InitStruct;

/* Peripheral clock enable */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM6);

/* TIM6 interrupt Init */
NVIC_SetPriority(TIM6_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
NVIC_EnableIRQ(TIM6_IRQn);

/* For Nucleo_L152RE board (32000000Hz / 10000Hz - 1) = 3199 */
TIM_InitStruct.Prescaler = 3199;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload = 9;
LL_TIM_Init(TIM6, &TIM_InitStruct);

LL_TIM_SetTriggerOutput(TIM6, LL_TIM_TRGO_UPDATE);

LL_TIM_DisableMasterSlaveMode(TIM6);

// Below, code added manualy - it will be overwritten by the code generator
/* Enable the update interrupt */
LL_TIM_EnableIT_UPDATE(TIM6);
/* Enable counter */
LL_TIM_EnableCounter(TIM6);
/* Force update generation */
LL_TIM_GenerateEvent_UPDATE(TIM6);
}

void TimerUpdate_Callback(void)
{
my_tick++;
}


/* //////////////////////////////////////////////////////
* stm32l1xx_it.c file
*
*/

void TIM6_IRQHandler(void)
{
/* Check whether update interrupt is pending */
if(LL_TIM_IsActiveFlag_UPDATE(TIM6) == 1)
{
/* Clear the update interrupt flag*/
LL_TIM_ClearFlag_UPDATE(TIM6);
}

/* TIM6 update interrupt processing */
TimerUpdate_Callback();
}


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.