Saturday, October 5, 2013

Programming Example : System Tick Timer


#include "stm32f2xx.h"
static __IO uint32_t TimingDelay;
void Delay(__IO uint32_t nTime);

int main(void)
{
  RCC_ClocksTypeDef rcc_clocks;

  RCC_GetClocksFreq(&rcc_clocks);

  if (SysTick_Config(rcc_clocks.SYSCLK_Frequency / 1000))
  {
    /* Capture error */
    while (1);
  }

  while (1)
  {
    /* Todo: Toggle LED, etc. */

    ...

    /* Insert 50 ms delay */
    Delay(50);
  }
}


void Delay(__IO uint32_t nTime)
  TimingDelay = nTime;
  while(TimingDelay != 0);
}

void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}

/* System Tick Timer Handler */
void SysTick_Handler(void)
{
  TimingDelay_Decrement();
}


/*
<Get System Clock Frequency>
@reg1  RCC->CFGR
@desc  Bits 15:13, PPRE2: APB high-speed prescaler (APB2)
       Bits 12:10, PPRE1: APB low-speed prescaler (APB1)
       Bits 7:4, HPRE: AHB prescaler
       Bits 3:2, SWS: System clock switch status
@reg2  RCC->PLLCFGR
@desc  Bit 22, PLLSRC: Main PLL(PLL) and audio PLL(PLLI2S) entry clock source
       Bits 17:16, PLLP: Main PLL(PLL) division factor for main system clock
       Bits 14:6, PLLN: Main PLL(PLL) multiplication factor for VCO
       Bits 5:0, PLLM: Division factor for the main PLL(PLL) and audio PLL(PLLI2S) input clock
@fn    void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks);
@ref   stm32f2xx_rcc.h

<System Tick Timer Configuration>
@reg1   SysTick->LOAD
@desc  Bits 23:0, RELOAD[23:0]: Reload value can be any value in range of 0x00000001 ~ 0x00FFFFFF
@reg2   SysTick->VAL
@desc  Bits 23:0, CURRENT[23:0]: Current count value of the SysTick counter
@reg3   SysTick->CTRL
@desc  Bit 16, COUNTFLAG: Returns 1 if timer counted to 0 since last time this was read. Software can use COUNTFLAG to determine if SysTick has ever counted to zero.
       Bit 2, CLKSOURCE: Clock source selection
       Bit 1, TICKINT: SysTick exception request enable
       Bit 0, ENABLE: Counter enable
@reg4  SCB->SHPR3
@desc  Bits 31:24, PRI_15[7:0]: Priority of system handler 15, SysTick exception
@fn1   __STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
@fn2   __STATIC_INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
@param SysTick_IRQn
@ref   stm32f2xx.h, core_cm3.h
*/

No comments:

Post a Comment