STM32 Systick (System timer)

Source: Internet
Author: User

The Systick timer is bundled in the Nvic to produce a Systick exception (the exception number is 15). (Again, played 51 MCU all know the role of the timer)

The STM32 in the kernel section is included with a simple timer –systick timer. Since this timer is available on all CORTEX-M3 chips, the software can be reduced to a single job between the CORTEX-M3 devices of different chip manufacturers.
The clock source for the timer can be either an internal clock (FCLK, a free-running clock on the CM3), or an external clock (STCLK signal on the CM3 processor). However, the specific source of STCLK is determined by the chip designer, so the clock frequency between different products can be
can be very different. Therefore, it is necessary to read the user manual of the chip to determine what to choose as the clock source.
* (SysTick in STM32 with HCLK (AHB clock) or HCLK/8 as the running clock. )
The Systick timer can interrupt, and the CORTEX-M3 specifically makes an exception type for it and exists in the vector table. So he can make it easier to transplant the OS and other system software between CORTEX-M3 devices, because Systick is handled the same way between all CORTEX-M3 products.

SysTick Timer Job Analysis:

The SysTick is a 24-bit timer that can count up to 224 clock pulses at a time, which is saved to the current count register Stk_val (SysTick valueregister) and can only be counted down, each receiving a clock pulse The value of Stk_val is reduced by 1 to 0, and when the value of Stk_val is reduced to 0 o'clock, the overloaded registers are automatically loaded by the hardware
The saved data in Stk_load (SysTick reload value register) is loaded into the stk_val and is counted down again. When the value of Stk_val is counted to 0 o'clock, the exception is triggered, and the timed event can be processed in the Interrupt service function.
(SysTick configuration is required to make the SysTick work.) Its control configuration is simple, only three control bits and a flag bit, all located in register Stk_ctrl)

Bit0:enable:
For the SysTick timer's enable bit, this bit is 1 to enable SysTick timer, this bit is 0 when the SysTick timer is closed.

Bit1:tickint:
When the enable bit is triggered for an exception, this bit is 1 and the stk_val count to 0 o'clock triggers the Systick exception, which is configured to 0 without triggering an exception

Bit2:clksource:
Select a bit for the SysTick clock, when this bit is 1, the SysTick clock is the AHB clock, and this bit is 0 when the SysTick clock is AHB/8 (AHB eight).

Bit16:countflag:
For a count of 0 flag bits, if the stk_val count to 0, this flag bit will be set to 1.
(Stk_calib registers are used for calibration, not commonly used.) All Systick related registers can be found in the STM32 data sheet)

Example Analysis Systick (time-lapse lamp)

main function function:
int main ()
{
Led_gpio_config (); LED Gpio Initialization

SysTick_Init();    //配置SysTickwhile(1)           //一个死循环的流水灯{    LED1(0);    Delay_us(50000);    LED1(1);    LED2(0);    Delay_us(50000);    LED2(1);    LED3(0);    Delay_us(50000);    LED3(1);}return 0;

}

Systick_init () analysis

void Systick_init (void)
{

/* systemfrequency/1000 1ms interrupts once
systemfrequency/100000 10US Interrupt Once
systemfrequency/1000000 1us Interrupts once */

if (Systick_config (systemfrequency/100000))//ST3.0.0 library version
if (Systick_config (systemcoreclock/100000))//ST3.5.0 library version
{
while (1);
}

SysTick->CTRL &= ~ SysTick_CTRL_ENABLE_Msk;//关闭定时器

}
In fact, the function is called a function systick_config (), this function is the kernel layer of the CORTEX-M3 general function, located in Core_cm3.h. function is very simple, that is, if the call Systick_config () is unsuccessful, then into the dead loop, when the initialization of the Systick successful, first close the timer, need to use the time to open again. (We can follow this function in the Keil environment to see the definition of the function)
/*
* @brief Initialize and start the SysTick counter and its interrupt.
*
* @param ticks number of ticks between and interrupts
* @return 1 = failed, 0 = successful
*
* initi Alise the system tick timer and its interrupt and start the
* system tick Timer/counter in free running mode to Gene Rate
* Periodical interrupts.
*/
Static __inline uint32_t systick_config (uint32_t ticks)
{
/* Reload value impossible */
if (Ticks & Gt SYSTICK_LOAD_RELOAD_MSK) return (1);
/* Set Reload Register */
Systick->load = (Ticks & Systick_load_reload_msk)-1;
/* Set priority for CORTEX-M0 System interrupts */
Nvic_setpriority (SYSTICK_IRQN, (1<<__nvic_prio_bits)-1 );
/* Load the SysTick Counter Value */
Systick->val = 0;

Systick->ctrl = Systick_ctrl_clksource_msk |
Systick_ctrl_tickint_msk |
Systick_ctrl_enable_msk; /* Enab
Le SysTick IRQ and SysTick Timer */
return (0); /* Func
tion Successful */
}

The main function of this function: Start the Systimer, and configure it to count to 0 to cause the interrupt, the input parameter ticks the number of pulses between two interrupts (both ticks clock cycle will cause one interrupt), the configuration Systick return 0 on success, error return 1

The following is a detailed analysis:
if (Ticks > Systick_load_reload_msk) return (1);
Because ticks this parameter is a pulse count value, to be saved to the Reload Register Stk_load Register, and then by the hardware to load the Stk_load value into the current count value register Stk_val use, because Stk_load and stk_val are 24 bits, Therefore, when the input ticks is greater than the maximum value that can be stored, this line of code checks for an error return.

Systick->load = (Ticks & Systick_load_reload_msk)-1;
When the value of the ticks is not wrong, the ticks-1 is assigned to the Stk_load register (note minus 1), and when Stk_val is counted down from ticks-1 to 0, the ticks pulses are passed.
(The SYSTICK_LOAD_RELOAD_MSK macro used here (in the Keil environment to track the definition), this macro is used to indicate the register "specific location" or for "bit masking", so called bit to indicate the macro or bit masking macro, which is often used in the Operation Register Code, is usually used as a control register for some bits ("or" and "and" actions))

Nvic_setpriority (SYSTICK_IRQN, (1<<__nvic_prio_bits)-1);
The nvic_setpriority () function is called to configure the Systick interrupt, so we do not need to manually configure it. After the configuration is complete, the Stk_val register is re-assigned to a value of 0.

Systick->ctrl Configuration
Write the control parameters of the Systick timer to the Stk_ctrl, configure it as a clock, enable the interrupt to count to 0, enable the Systick, and then Systick to start running, do the pulse count.
(If you want to use AHB/8 as a clock, you can call the library function systick_clksourceconfig () or modify the Enable configuration of the Systick->ctrl)

Finally after Systick_config () is called, the Systick timer is opened, but we do not need it, so we close the timer after the call is finished.
Enable timer
Systick->ctrl |= Systick_ctrl_enable_msk;
Turn off the timer
Systick->ctrl &= ~ Systick_ctrl_enable_msk;
(Here again with the shielding macro in place, visible is very extensive)

Calculation of timing time:
When the Systick_config () is called again, the input ticks is systemcoreclock/100000 Systemcoreclock to the macro that defines the system clock (SYSCLK) frequency, which equals the AHB clock frequency. The process used to AHB are 72MHz, so systemcoreclock this macro expansion is 7200 0000.
According to the previous face Systick_config () function, its input parameter is SysTick will count the number of pulses, after ticks pulses (ticks clock cycle) will trigger an interrupt, triggering the interruption and restart the count.

Thus we can calculate the timing time, the following is the formula:
T=ticks* (f/F)
T is the total time to be timed.
Ticks is the input parameter for Systick_config ().
The SysTick is the clock period for the clock source used by the timer, and F is the clock frequency of the clock source, which is constant when the clock source is determined.
For example, this example uses the clock source as the AHB clock, and its frequency is configured to 72MHz. When the function is called, the ticks is assigned to ticks=systemfrequency/10000 = 720, indicating that 720 clock cycles are interrupted, (f/F) is the time of the clock cycle, at this point (f/F =1/72us), so the final timing total time t=720* (1/ 72), for 720 clock cycles, exactly 10us.
The timing time of the SysTick timer (configured to trigger interrupts, which is the interrupt cycle), is determined by the ticks parameter and cannot exceed 224 of the maximum timing period. Here are a few of the commonly used interrupt cycle configurations, which are calculated based on the above formula.

Interrupt Service function:
In main, we let the LED work in the infinite loop, the switch between the call Delay_us (), once called, the Systick will be turned on, according to set a good time period to decrement the count, when the Systick count value reduced to 0 o'clock, to enter the interrupt, The interrupt is executed and then re-counted.

void Delay_us (__io u32 ntime)
{
Timingdelay = Ntime;

SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; //使能定时器while(TimingDelay != 0);

}
After enabling the Systick, use the while (Timingdelay! = 0) statement to wait for the Timingdelay variable to become 0, which is modified in the Interrupt service function.

/*
* @brief This function handles SysTick Handler.
* @param None
* @retval: None
*/
void Systick_handler (void)
{
if (timingdelay! = 0x00)
{
timingdelay–;
}
}

The Systick interrupt belongs to the system exception vector, and its interrupt service function Systick_handler () is already default in the Stm32f10x_it.c file, but the content is empty. So we're going to write it ourselves.
Each time you enter the SysTick interrupt, the global variable is timingdelay from one time. The user function Delay_us () does not exit the delay loop until Timingdelay is reduced to 0 o'clock, that is, the number of times that we assign to the Timingdelay is to be interrupted.

So the total delay time t delay = t Interrupt period * Timingdelay.

PS:
Timers we can also use as a calculation program execution time:
When we turn on the SysTick timer, the timer starts to work, we can define a variable A to record the number of interrupts, when the timer enters the interrupt, this variable is a++, when we turn off the timer, the value of the variable multiplied with the timer interrupt period is equal to the measurement time. In particular, the program involved in the algorithm, which is very helpful for the optimization algorithm. If your algorithm is the US level, then the SysTick should be set to the US-class interrupt, if the MS level, the SysTick is set to a MS-class interrupt.

STM32 Systick (System timer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.