Defines | Functions

xpal_async32khz.h File Reference

Header for the async Timer Base Functions. More...

#include "cb_list.h"
#include "xpal_err_log.h"

Go to the source code of this file.

Defines

#define ASYNC_IDLE   0x00
 Idle, no timer Interrupt pending.
#define ASYNC_SLOW_ACTIVE   0x01
 Slow INT active.
#define ASYNC_FAST_ACTIVE   0x02
 Fast INT pending.
#define ASYNC_FAST_STOP   0x02
 Fast INT disabled (eg. due to power save).
#define ASYNC_SLOW_OVERRUN   0x10
 Slow INT overrun.
#define ASYNC_FAST_OVERRUN   0x20
 Fast INT overrun.
#define ASYNC_FAST_BLOCK   0x40
 Fast INT blocked by Slow INT.
#define ASYNC_SLOW_BLOCK   0x80
 SLOW INT blocked by FAST INT.

Functions

void hl_asyn_init (void)
 Initialize the 32kHz timer (timer2).
void hl_asyn_init_osc (void)
 Init async timer system - initiate oscillator startup.
void hl_asyn_init_timer (void)
 init async timer system - initialize timer device
void hl_asyn_StartSysTimer (void)
 Start the system Timer again.
void hl_asyn_StopSysTimer (void)
 Stop the system Timer.
void hl_asyn_Register1sTimer (cb_function_t fkt2call, void *arg)
 Register a call back function for the slow timer.
void hl_asyn_RegisterFastTimer (cb_function_t fkt2call, void *arg)
 Register a call back function for the fast timer.
void hl_asyn_ReportErrors (errString_t errMsg)
 Report error string if available.
void hl_asyn_Sync1sTimer (void)
 Sync 1s Timer.

Detailed Description

Header for the async Timer Base Functions.

Todo:
Add function to register timer callback functions for application layer

Define Documentation

#define ASYNC_FAST_BLOCK   0x40

Fast INT blocked by Slow INT.

A fast interrupt was called while the slow INT was in execution At the end of the slow ISR, the fast int will get called

#define ASYNC_SLOW_BLOCK   0x80

SLOW INT blocked by FAST INT.

A slow interrupt was called while the fast INT was in execution At the end of the fast ISR, the slow int will get called


Function Documentation

void hl_asyn_init ( void   ) 

Initialize the 32kHz timer (timer2).

  • 1s overrun
  • OverflowA to generate 19.53ms interrupts

                       {

        // init callback lists
#if 0 /* not needed since data gets initialised to 0 by startup */
        cbListSlowFirst = NULL;

        cbListFastFirst = NULL;
#endif

          hl_asyn_init_osc ();

        // allow xtal to startup
        _delay_ms(1000);

          hl_asyn_init_timer ();
}

void hl_asyn_Register1sTimer ( cb_function_t  fkt2call,
void *  arg 
)

Register a call back function for the slow timer.

The function will add an entry to the list of functions to be called when the 1sec timer expires

The new entry is added to the front of the list.

{
  cb_register_call (&cbListSlowFirst, fkt2call, arg);
}

void hl_asyn_RegisterFastTimer ( cb_function_t  fkt2call,
void *  arg 
)

Register a call back function for the fast timer.

The function will add an entry to the list of functions to be called when the 19,53ms timer expires

The new entry is added to the front of the list.

{
  cb_register_call (&cbListFastFirst, fkt2call, arg);
}

void hl_asyn_ReportErrors ( errString_t  errMsg  ) 

Report error string if available.

Report error string if available.

Function will return a zero terminated string containing the async timer error msg.

                                             {

        if(SysTimerStatus & (ASYNC_SLOW_OVERRUN | ASYNC_FAST_OVERRUN)){
                
                // One of the timers has had an overrun 
                switch (SysTimerStatus & (ASYNC_SLOW_OVERRUN | ASYNC_FAST_OVERRUN)){
                
                        case(ASYNC_SLOW_OVERRUN):
                                sprintf_P(errMsg, PSTR("ERR - Asyn 1s TMR OVR"));
                                // Clear Error condition
                                SysTimerStatus &= ~(ASYNC_SLOW_OVERRUN);
                        break;          
                        case(ASYNC_FAST_OVERRUN):
                                sprintf_P(errMsg, PSTR("ERR - Asyn 20ms TMR OVR"));
                                // Clear Error condition
                                SysTimerStatus &= ~(ASYNC_FAST_OVERRUN);
                        break;
                        case(ASYNC_SLOW_OVERRUN | ASYNC_FAST_OVERRUN):
                                sprintf_P(errMsg, PSTR("ERR - Both TMR OVR"));
                                // Clear Error condition
                                SysTimerStatus &= ~(ASYNC_SLOW_OVERRUN | ASYNC_FAST_OVERRUN);
                        break;
                        default:
                                sprintf_P(errMsg, PSTR("ERR - TMR"));
                        break;          
                }
        }else{
                // No Errors detected
                errMsg[0] = 0;
        }
}

void hl_asyn_StartSysTimer ( void   ) 

Start the system Timer again.

Restart the system timer again when it was stopped to save power

                                {
        
        FastTimerInc = HL_ASYN_FAST_INC + TCNT2;        
        OCR2A = FastTimerInc;   // Compare value for next fast system timer

        // set fast timer status
        SysTimerStatus &= ~(ASYNC_FAST_STOP);

        /* Reset any pending interrupt */
        TIFR2 = _BV(OCF2A);

        /* Start System Timer if not running */
        TIMSK2 |= _BV(OCIE2A);
}

void hl_asyn_StopSysTimer ( void   ) 

Stop the system Timer.

Stop the system timer to save power

                               {
        /* Start System Timer if not running */
        TIMSK2 &= ~(_BV(OCIE2A));

        // set fast timer status
        SysTimerStatus |= ASYNC_FAST_STOP;
}

void hl_asyn_Sync1sTimer ( void   )  [inline]

Sync 1s Timer.

When called the timer compare register will be set to the current state of the counter minus one. This will sync the timer in a way that the next 1s interrupt will be generated 1s after this call. Basic idea is that the clock is set to the correct time and the next second has to be counted in one second from now.

Maximum error can be two 32kHz timer ticks = 61us.

                                     {
        OCR2B = TCNT2 - ((uint8_t)1);

}