1852 views|5 replies

501

Posts

4

Resources
The OP
 

[ST NUCLEO-U575ZI-Q Review] Systick time base usage and testing using HAL/LL library [Copy link]

Preface

Systick is a standard core peripheral of CORTEX-M series microcontrollers.

Generally used for time base, timestamp, or RTOS tick timing interrupt.

Based on the GPIO in the previous article, we can test whether the systick timing is correct. We can flip the IO in the systick interrupt and use an oscilloscope/logic analyzer to measure the waveform period of the IO to test whether the timing is accurate.

process

Systick can refer to the kernel architecture document of CORTEX-M3. I will not go into details here. Just look at the code directly.

The HAL library also uses systick as a timer, and operations such as delay depend on systick.

initialization

The code is located in HAL_Init in stm32u5xx_hal.c.

HAL_StatusTypeDef HAL_Init(void)

{

/* Configure Flash prefetch */

#if (PREFETCH_ENABLE != 0U)

__HAL_FLASH_PREFETCH_BUFFER_ENABLE();

#endif /* PREFETCH_ENABLE */

/* Set Interrupt Group Priority */

HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

/* Update the SystemCoreClock global variable */

SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR2 & RCC_CFGR2_HPRE) >> RCC_CFGR2_HPRE_Pos];

/* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */

if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)

{

return HAL_ERROR;

}

/* Init the low level hardware */

HAL_MspInit();

/* Return function status */

return HAL_OK;

}

Call HAL_InitTick for initialization

__weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)

{

/* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that don't take the value zero)*/

if ((uint32_t)uwTickFreq == 0UL)

{

return HAL_ERROR;

}

/* Configure the SysTick to have interrupt in 1ms time basis*/

if (HAL_SYSTICK_Config(SystemCoreClock / (1000UL / (uint32_t)uwTickFreq)) > 0U)

{

return HAL_ERROR;

}

/* Configure the SysTick IRQ priority */

if (TickPriority < (1UL << __NVIC_PRIO_BITS))

{

HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);

uwTickPrio = TickPriority;

}

else

{

return HAL_ERROR;

}

/* Return function status */

return HAL_OK;

}

The timing period is defined by HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */

The default is

typedef enum

{

HAL_TICK_FREQ_10HZ = 100U,

HAL_TICK_FREQ_100HZ = 10U,

HAL_TICK_FREQ_1KHZ = 1U,

HAL_TICK_FREQ_DEFAULT = HAL_TICK_FREQ_1KHZ

} HAL_TickFreqTypeDef;

Interrupt handling

SysTick_Handler calls HAL_IncTick to update the counter.

void SysTick_Handler(void)

{

static volatile uint32_t num = 0;

if(num++ >= 1000)

{

LL_GPIO_TogglePin(GPIOB, 1u<<7);

num=0;

}

HAL_IncTick();

}

Measuring time

Total code

include "stm32u575xx.h"

include "stm32u5xx_ll_gpio.h"

include "stm32u5xx_ll_bus.h"

void SysTick_Handler(void)

{

static volatile uint32_t num = 0;

if(num++ >= 1000)

{

LL_GPIO_TogglePin(GPIOB, 1u<<7);

num=0;

}

HAL_IncTick();

}

void delay(uint32_t t)

{

volatile uint32_t timeout = t;

while(t--);

}

int main(void)

{

LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);

LL_GPIO_InitTypeDef GPIO_InitStruct;

//LL_GPIO_StructInit(&GPIO_InitStruct);

GPIO_InitStruct.Pin = LL_GPIO_PIN_7;

GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;

GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;

GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;

GPIO_InitStruct.Alternate = LL_GPIO_AF_0;

LL_GPIO_Init(GPIOB, &GPIO_InitStruct);

HAL_Init();

while(1)

{

///delay(1000000ul);

///LL_GPIO_TogglePin(GPIOB, 1u<<7);

}

}

Using a logic analyzer to measure, the IO flip time is 1.982917488/2=0.991458744S.

Since interrupt response and execution of processing code take a certain amount of time, there are some errors.

Summarize

This article introduces the use of systick and tests its accuracy. Later, we will often use time base, delay, etc. Thanks to the official library, it can be done efficiently with just a few lines of code.

This post is from stm32/stm8

Latest reply

Review summary: Free application | ST NUCLEO-U575ZI-Q https://bbs.eeworld.com.cn/thread-1228653-1-1.html   Details Published on 2023-1-12 09:42
 

6069

Posts

4

Resources
2
 

systick is a good thing.

This post is from stm32/stm8
 
 

224

Posts

0

Resources
3
 

Learned

I don't quite understand why we need a LL library when we already have a HAL library

When porting FreeRTOS, you need to shield the three interrupts of PendSV, SVHC and Systick

This post is from stm32/stm8

Comments

LL mostly uses inline functions to encapsulate register operations, which is equivalent to directly operating registers. It is more efficient than HAL. HAL and LL can be used in combination.  Details Published on 2022-12-17 09:27
 
 

501

Posts

4

Resources
4
 
starcat123 posted on 2022-12-17 00:18 I don't quite understand why we need to have an LL library when we have a HAL library. When porting FreeRTOS, we need to block PendSV, SVHC and Systick...

Most LL uses inline functions to encapsulate register operations, which is equivalent to directly operating registers.

More efficient than HAL, HAL and LL can be used in combination.

This post is from stm32/stm8

Comments

Oh, after listening to the big guy's explanation, it seems that I suddenly understand what inline is. I still know what it is. When I learned C51, it was there. In order to further optimize the code speed   Details Published on 2022-12-17 15:34
 
 
 

224

Posts

0

Resources
5
 
qinyunti posted on 2022-12-17 09:27 LL mostly uses inline functions to encapsulate register operations, which is equivalent to directly operating registers. It is more efficient than HAL execution. HAL and LL ...

Oh, after listening to the big guy's explanation, it seems to be clear.

I still know what inline is. It was there when I was learning C51. In order to further optimize the code speed

This post is from stm32/stm8
 
 
 

1w

Posts

204

Resources
6
 

Review summary: Free application | ST NUCLEO-U575ZI-Q https://bbs.eeworld.com.cn/thread-1228653-1-1.html

This post is from stm32/stm8
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
Personal signature

玩板看这里:

https://bbs.eeworld.com.cn/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list