System ticks

February 6, 2013

Interfaces: Listing 3 shows the interfaces for the SYSTICK timer service. Here, we show how to "abstract" the systick timer into a set of function calls. Of course, we could have easily accessed the SYSTICK object directly using the ST-micro access calls. However, what if we do another project that uses the NXP ARM Cortex processor and not the ST-micro? In this way, we can get away with using this library and simply redefine the interface. This allows us to retain the "investment" in the other modules that require the use of the SYSTICK interface with a different silicon implementation.

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

/****************************************************************************/
/*                            CODE STARTS HERE                              */
/****************************************************************************/
long sysTick_capture (void)
{
    return ((long)SysTick->VAL);
}

unsigned long SysTick_GetReload (void)
{
    return (SysTick->LOAD & 0x00FFFFFF);
}

unsigned long SysTick_GetUpCountValue (void)
{
    unsigned long my_value  = (SysTick->VAL  & 0x00FFFFFF);
    return (((SysTick->LOAD & 0x00FFFFFF) + 1) - my_value);
}

Listing 3: A set of interfaces for the SYSTICK timer service

Process: In the SYSTICK handler, the process is the interrupt service. Listing 4 shows the code to implement the core SYSTICK interrupt service routine. In our case, the SysTickHandler() function is defined as a global in an assembly file. In this case, the file defines all of the interrupt vectors for the ARM Cortex processor. The process updates three global variables. These variables are:

1. SysTick_1ms, this counter updates once per millisecond
2. SysTick_10ms, this counter updates or counts once per 10 milliseconds
3. SysTick_1sec, this counter is the 1 second global system uptime clock.

Notice that there is no interrupt keyword being used here. This is because the ARM Cortex has hardware that does the context-switch, which in turn saves the processor state upon entry into the service. The hardware also restores the context when the service returns.

#define  __SYSTICKHANDLER_C
/****************************************************************************/
/*    SysTickHandler.c                                                      */
/*                                                                          */
/*    This is the actual interrupt service routine for the ARM Cortex       */
/*    sysTick timer service. The toolchain has predefined the function      */
/*    void SysTickHandler (void); in the global namespace as an interrupt   */
/*    vector.                                                               */
/*                                                                          */
/*    BY:   Ken Wada                                                        */
/*          27-January-2013                                                 */
/*                                                                          */
/****************************************************************************/
volatile unsigned long  SysTick_1ms;
volatile unsigned long  SysTick_10ms;
volatile unsigned long  SysTick_1sec;

#define _TIC_10     10
#define _TIC_100    100

/****************************************************************************/
/*                            CODE STARTS HERE                              */
/****************************************************************************/
void SysTickHandler(void)
{
    static int _prescaler_10ms  = _TIC_10;
    static int _prescaler_sec   = _TIC_100;
    ++SysTick_1ms;
    --_prescaler_10ms;
    if (_prescaler_10ms<=0)
    {
      _prescaler_10ms = _TIC_10;
      ++SysTick_10ms;
      --_prescaler_sec;
	  if(_prescaler_sec<=0)
	  {
        _prescaler_sec  = _TIC_100;
		  ++SysTick_1sec;
	  }
    }
}


Listing 4: The SYSTICK interrupt service routine.
< Previous
Page 3 of 5
Next >

Loading comments...

Parts Search Datasheets.com

KNOWLEDGE CENTER