Systick principle and application of Stm32

Source: Internet
Author: User


/*Systick Tick Timer One, the function Systick Timer is a simple timer, the CM3\CM4 core chip has this timer. The Systick timer is often used for delay, and the system clock is used when using a real-time system. Regardless of whether it is used as a delay or as a system heartbeat clock, the Systick is capable of not requiring too complex functionality. Second, the realization Principle Systick timer is a 24-bit inverted count, when the countdown is 0 o'clock, will be taken from the reload register value as the initial value of the timer, at the same time can choose to produce an interrupt at this time (Exception number: 15). For example, the value from reload is 999, then when the counter count is 0 o'clock, the countdown is continued from reset to 999. As long as you do not put it in the Systick control and Status register in the Enable bit clear, will never stop, even in sleep mode can continue to work. Three, Systick register (in the core_cm3.h have a definition, generally M3 the core of the microcontroller are the same)*/#defineSysTick ((Systick_type *) systick_base)#defineSystick_base (Scs_base + 0x0010)#defineScs_base (0xe000e000)typedefstruct{__io uint32_t CTRL; //Control and status registers__io uint32_t LOAD;//Reload Value Register__io uint32_t VAL;//Current count value Register__i uint32_t Calib;//Calibration Register} systick_type;/*Systick->ctrl: (Can be set by Systick_clksourceconfig () function) Countflag R: Count flag bit when SysTick number to 0, then the bit is hardware 1, when the bit is read, will be hardware clear 0 clksource (2) r/w: clock Source setting 1 = external clock Source (STCLK) (AHB bus clock 1/8 (HCLK/8)) 0 = Core Clock (FCLK) (AHB Bus clock frequency (HCLK)) Tickint (1) r/w: Interrupt Enable bit 1 = SysTick countdown to 0 o'clock generates SysTick exception request 0 = number to 0 o'clock no action enable (0) R/W: SysTick timer enable bit (after interrupt is enabled, pay attention to void systick_handler (void) function) Systick_type->load: (The Systick_config () function sets the Register) RELOAD (23:0) r/w: Reloads the value register when the SysTick number to 0, the values that will be reloaded systick_type-> VAL: (The Systick_config () function sets the Register) current (23:0) R/WC: The current count value register reads when the currently inverted count is returned, writes it to clear it, and clears the SysTick control and status register. Countflag logo. Iv. library function Analysis MISC.C----------------------------------------------------------------------------------*/#defineSystick_clksource_hclk_div8 ((uint32_t) 0xFFFFFFFB)#defineSYSTICK_CLKSOURCE_HCLK ((uint32_t) 0x00000004)#defineIs_systick_clk_source (source) ((source) = = SYSTICK_CLKSOURCE_HCLK) | | \(SOURCE)==systick_clksource_hclk_div8))voidsystick_clksourceconfig (uint32_t systick_clksource) {/*Check The Parameters*/Assert_param (Is_systick_clk_source (Systick_clksource)); if(Systick_clksource = =SYSTICK_CLKSOURCE_HCLK) {SysTick->ctrl |= SYSTICK_CLKSOURCE_HCLK;//set Clksource to 1 } Else{SysTick->ctrl &= Systick_clksource_hclk_div8;//set Clksource to 0}}core_cm3.c----------------------------------------------------------------------------------#defineSystick_load_reload_pos 0#defineSystick_load_reload_msk (0xFFFFFFul << systick_load_reload_pos)typedefenumirqn{//...SYSTICK_IRQN =-1,//...} irqn_type;#define__nvic_prio_bits 4#defineSystick_ctrl_clksource_pos 2#defineSystick_ctrl_clksource_msk (1ul << systick_ctrl_clksource_pos)#defineSystick_ctrl_tickint_pos 1#defineSystick_ctrl_tickint_msk (1ul << systick_ctrl_tickint_pos)#defineSystick_ctrl_enable_pos 0#defineSystick_ctrl_enable_msk (1ul << systick_ctrl_enable_pos)Static__inline uint32_t systick_config (uint32_t ticks) {if(Ticks > Systick_load_reload_msk)return(1);/*Reload value Impossible*/ //set the Count value to Ticks-1//cause 1: Video says it takes time to execute these codes, so reduce a beat//Reason 2: I think it is because the SysTick count to 0, such as setting 1000, then the range should be 999 ~ 0. Systick->load = (Ticks & Systick_load_reload_msk)-1; //set the interrupt priority levelNvic_setpriority (SYSTICK_IRQN, (1<<__nvic_prio_bits)-1); SysTick->val =0; //set the clock source as an external clock source while turning on interrupts and enabling the SysTick timerSystick->ctrl = Systick_ctrl_clksource_msk |Systick_ctrl_tickint_msk|Systick_ctrl_enable_msk; return(0);}/*v. Delay application 1, interrupt mode*/Static__io uint32_t timingdelay;voidDelay (__io uint32_t ntime) {Timingdelay=Ntime; while(Timingdelay! =0);}/*Interrupt Service Function*/voidSystick_handler (void){ if(Timingdelay! =0x00) {Timingdelay--; }}intMainvoid){// ... if(Systick_config (Systemcoreclock/ +))//Note that here the Systick clock is hclk, interrupt interval 1ms { while(1); } while(1) {Delay ( $);//2ms// ... }}/*Systick_config (systemcoreclock/1000): (The original code here assumes that the clock source is HCLK) is set here is 72000000hz/1000 = 72000 ticks, that is SysTick from (7 2000-1) Start the countdown. An interrupt is triggered every 72,000 beats in the countdown. The time of a beat is: 72000000/72000 = 1000US = = 1mssystick_config ((systemcoreclock/8000000) * 1): Systick_config () sets the clock source to H CLK/8 so the actual application can not follow the above code parameters. Systemcoreclock/8000000:1us beats number 1us beats number * 1000:1ms beats number 1ms beats number * 1: Set 1ms a Systick interrupt, that is, from ((systemcoreclock/8000 000) * 1000 * 1)-1 countdown starts. 2. Polling Method*/StaticU8 fac_us=0;//US delay multiplierStaticU16 fac_ms=0;//ms Delay multipliervoidDelay_init () {systick_clksourceconfig (SYSTICK_CLKSOURCE_HCLK_DIV8);//Select external Clock hclk/8Fac_us = systemcoreclock/8000000;//1/8 for the system clock 1us = 72000000/8000000 = 9 beatsFac_ms = (U16) fac_us* +;//1ms requires 9 * 1000 = 9,000 beats}//Delay NUS microsecondsvoidDelay_us (u32 nus) {u32 temp; SysTick->load=nus*fac_us;//Time LoadingSystick->val=0x00;//Empty CounterSystick->ctrl|=systick_ctrl_enable_msk;//Start counting . Do{Temp=systick->CTRL; } while((temp&0x01) &&! (temp& (1<< -)));//wait Time arrivessystick->ctrl&=~systick_ctrl_enable_msk;//Turn off CountersSystick->val =0x00;//Empty Counter}//Time- lapse NMS//note the scope of the NMS//The Systick->load is a 24-bit register, so the maximum delay is://NMS<=0XFFFFFF*8*1000/SYSCLK//SYSCLK Unit is HZ,NMS unit for Ms//for 72M conditions, the nms<=1864voidDelay_ms (U16 NMS) {U32 temp; SysTick->load= (u32) Nms*fac_ms;//Time-loaded (Systick->load to 24bit)Systick->val =0x00;//Empty CounterSystick->ctrl|=systick_ctrl_enable_msk;//Start counting . Do{Temp=systick->CTRL; //wait time arrives, here uses a small technique, through (TEMP&0X01) examines the SysTick the enable bit, avoids the SysTick timer to be closed and causes the infinite loop } while((temp&0x01) &&! (temp& (1<< -))); SysTick->ctrl&=~systick_ctrl_enable_msk;//Turn off CountersSystick->val =0x00;//Empty Counter}

SYSTick Timer
CM3 Core processor, the internal contains a SysTick timer, (SysTick clock from HCLK 8, 8 system clock cycle SysTick jump One, that is, 8*1/72M=1/9 us) SysTick is a 24-bit countdown timer, when counted to 0 o'clock , the timing initial value is automatically reloaded from the reload register. As long as the enable bits in the Systick control and status register are not removed, it will never cease.
The internal SysTick of the STM32 is used to implement the delay, which neither occupies the interrupt nor consumes the system timer. Because in the Ucos under the systic can no longer be arbitrarily changed, if we also want to use Systick to do delay_us or Delay_ms delay, we must think of a way, here we are using the clock extraction method. Take Delay_us as an example, for example, Delay_us (50), when just entered Delay_us first calculate the time delay need to wait for the number of Systick count, here is 50*9 (assuming the system clock is 72Mhz, then systick each increase of 1, is 1/9u s), and then we've been counting changes in Systick until this value changes 50*9, and once the change is detected to reach or exceed this value, it means that the delay time is 50us. ——— essentially does not change the Systick base unit length, with the basic unit time as the fundamental element to do multiple arrival harvesting.

Systick principle and application of Stm32

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.