System ticks

February 6, 2013

First steps for the SYSTICK timer
So, now that we have defined some approaches on how to make our system tick timer counter. How do we get started? In general, the method of choice for me is to first create the header file. This file helps me to focus on what I wish for the timer to do and what various interfaces I'll be creating for the timer. Listing 1 shows an example of the header file I created for the SYSTICK timer module.

#ifndef __SYSTICK_H
    #define __SYSTICK_H
/****************************************************************************/
/*    FILE: sysTick.h                                                       */
/*                                                                          */
/*    These files contain the interfaces for the ARM SYSTICK system         */
/*    peripheral.                                                           */
/*                                                                          */
/*      BY:   Ken Wada                                                      */
/*            27-January-2013                                               */
/*                                                                          */
/****************************************************************************/

    #ifdef  __cplusplus
      extern  "C" {
    #endif
      void  MySysTick_Configuration         (const unsigned long FsysTick);
      long  sysTick_capture                 (void);
      unsigned long SysTick_GetReload       (void);
      unsigned long SysTick_GetUpCountValue (void);
    #ifdef  __cplusplus
      }
    #endif
#endif

Listing 1: The SYSTICK system timer header file.

In this listing, I've included three interfaces and an initialization function. The initialization function, MySysTick_Configuration (const unsigned long FsysTick) will setup the sysTick timer interrupt and any globals that are required by the interrupt service. The sysTick_capture(), SysTick_GetReload(), and SysTick_GetUpCountValue() are the generic interfaces that allow one to get the various systick counts and control settings.

Initialization: Listing 2 shows the implementation of the SYSTICK timer initialization. In this case, only the interrupt service is initialized. I have chosen to use the ANSI C initialization for the counter variables. If you have a compiler or toolkit that does not support this type of initialization, it's a good idea to include the variable initialization here.

#define   __MYSYSTICK_CONFIGURATION_C
/****************************************************************************/
/*    FILE: MySysTick_Configuration.c                                       */
/*                                                                          */
/*    These files contain the methods and attributes for using ARM Cortex   */
/*    sysTick interface.                                                    */
/*                                                                          */
/*    BY:   Ken Wada                                                        */
/*          27-January-2013                                                 */
/****************************************************************************/
#include  "stm32f10x_lib.h"
#include  "sysTick.h"

/****************************************************************************/
/* Function Name  : MySysTick_Configuration                                 */
/* Description    : Configures sysTick to interrupt at FsysTick frequency   */
/* Input          : unsigned long FsysTick = timer interrupt frequency in Herz    */
/* Output         : None                                                    */
/* Return         : None                                                    */
/****************************************************************************/
void MySysTick_Configuration (const unsigned long FsysTick)
{
    RCC_ClocksTypeDef rcc;
    /* Select AHB clock(HCLK) as SysTick clock source */
    SysTick_CLKSourceConfig (SysTick_CLKSource_HCLK);

    /* Set SysTick Priority to 3 */
    NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);

    RCC_GetClocksFreq (&rcc);
    SysTick_SetReload (rcc.HCLK_Frequency / FsysTick);

    /* Enable the SysTick Interrupt */
    SysTick_ITConfig(ENABLE);
    //enable systick counter
    SysTick_CounterCmd(SysTick_Counter_Enable);

}

Listing 2: The SYSTICK timer initialization for the ARM Cortex processor.

< Previous
Page 2 of 5
Next >

Loading comments...

Parts Search Datasheets.com

KNOWLEDGE CENTER