Use of SysTick under STM32 V3.5 firmware library

Publisher:快乐的旅程Latest update time:2015-09-18 Source: eefocusKeywords:STM32  SysTick Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The SysTick timer is bundled in the NVIC and is used to generate the SysTick exception (exception number: 15). In the past, the operating system and all systems that used a time base had to have a hardware timer to generate the required "tick" interrupt as the time base for the entire system. Tick interrupts are particularly important for operating systems. For example, the operating system can grant different numbers of time slices to multiple tasks to ensure that no task can monopolize the system; or grant a certain time range of each timer cycle to a specific task, etc., and the various timing functions provided by the operating system are all related to this tick timer. Therefore, a timer is needed to generate periodic interrupts, and it is best to prevent user programs from arbitrarily accessing its registers to maintain the rhythm of the operating system's "heartbeat".
 The Cortex-M3 processor contains a simple timer. Because all CM3 chips have this timer, porting software between different CM3 devices is simplified. The clock source of this timer can be an internal clock (FCLK, the 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 vary greatly. Therefore, it is necessary to check the device manual of the chip to decide what to choose as the clock source.
    The SysTick timer can generate an interrupt, and the CM3 has a special exception type for it and a place for it in the vector table. It makes porting operating systems and other system software between CM3 devices much simpler because SysTick is handled the same way in all CM3 products.
There are 4 registers controlling the SysTick timer, as shown in Table 8.9 to Table 8.12.  The calibration value register provides a solution that allows the system to generate a constant SysTick interrupt frequency even if it runs on different CM3 products. The simplest approach is to directly write the value of TENMS into the reload register, so that as long as the "elasticity limit" of the system is not exceeded, a SysTick exception can be generated every 10ms. If other SysTick exception periods are required, they can be proportionally calculated according to the value of TENMS. However, in a few cases, the CM3 chip may not accurately provide the value of TENMS (for example, the calibration input signal of the CM3 is pulled low), so for safety reasons, it is best to check the reference manual of the device before using TENMS.  In addition to serving the operating system, the SysTick timer can also be used for other purposes: as an alarm, for measuring time, etc. It should be noted that when the processor is halted during debugging, the SysTick timer will also be suspended.  In the 3.5 firmware library, SysTick is defined in core_cm3.h:[Reprint] Use of SysTick under STM32 V3.5 firmware library
  [Reprint] Use of SysTick under STM32 V3.5 firmware library

   
   

   
static __INLINE uint32_t SysTick_Config(uint32_t ticks)
{
  if (ticks > SysTick_LOAD_RELOAD_Msk)   return (1);            
                                                              
  SysTick->LOAD   = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;      
  NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); 
  SysTick->VAL    = 0;                                          
  SysTick->CTRL   = SysTick_CTRL_CLKSOURCE_Msk |
                   SysTick_CTRL_TICKINT_Msk    |
                   SysTick_CTRL_ENABLE_Msk;                    
  return (0);                                                  
}
    In the 3.5 library, it is very simple to use SysTick. Just call the SysTick_Config() function to write the reload beat number during initialization. For example, set the SysTick timer to generate an interrupt every 1ms:
SysTick_Config(72000);//1ms
On this basis, make a Delay function:
void Delay_ms(u16 time)
    {
       nTime = time;
       while(nTime);
    }
Among them, nTime is a global variable, and the decrement operation is performed in the SysTick interrupt service program:
void SysTick_Handler(void)
    {
       nTime--;
    }
 
Keywords:STM32  SysTick Reference address:Use of SysTick under STM32 V3.5 firmware library

Previous article:STM32 builds its own project based on the library
Next article:STM32F103 Basics 1

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号