Defines | Functions | Variables

xpal_rtc.c File Reference

implement clock using the functions provided by xpal_async32khz.c . More...

#include <avr/power.h>
#include <util/atomic.h>
#include <avr/sleep.h>
#include "config.h"
#include "rtc.h"
#include "xpal_async32khz.h"

Defines

#define TIM1_PRESCALE   (256)

Functions

void hl_rtc_init_osc (void)
 activate low-frequency oscillator
void hl_rtc_init (void)
 initialize low-frequency timer
hl_time_t hl_get_current_time (void)
 return current time
void hl_sleep_for_tick (uint16_t ms)
 enter power-save mode until next timer overflow or other wake-up event

Variables

uint32_t time_sec

Detailed Description

implement clock using the functions provided by xpal_async32khz.c .

We provide functions to query a counter incremented once per second, and additionaly sub-second precision (1/256 s) using the current value of the timer register.

usage example (startup):

   hl_rtc_init_osc ();
   // do something which does _not_ need the timer
   hl_rtc_init ();
   // now the clock functions give meaningful results

Function Documentation

void hl_rtc_init ( void   ) 

initialize low-frequency timer

hl_rtc_init() blocks as long as timer1 indicates that the necessary time for oscillator start-up has not elapsed.

{
  hl_asyn_Register1sTimer (hl_rtc_1s, NULL);

  /* await Overflow Compare on Timer1 - one second has elapsed */
  while ((TIFR1 & _BV(OCF1A)) == 0);
  /* stop timer1 */
  TCCR1B = 0;
  /* reset OCF1A */
  TIFR1 = _BV(OCF1A);
  /* power down timer1 */
  power_timer1_disable ();

  hl_asyn_init_timer();
}

void hl_rtc_init_osc ( void   ) 

activate low-frequency oscillator

After calling this function, wait at least one second before starting to use the timer, as this is the time the oscillator needs to stabilize.

hl_rtc_init_osc() also starts timer1 to measure the elapsed time since startup of the oscillator. The idea is to be able to do some other initialization tasks which do not need the asynchronous oscillator.

{
  hl_asyn_init_osc();

  /*  prepare for 1 second wait until oscillator is ready */

  /* reset counter */
  TCNT2 = 0;

  /* initialize Timer1 */
  power_timer1_enable ();

  /* reset counter */
  TCNT1 = 0;
  /* OC1A/B/C disconnected; WGM: normal mode */
  TCCR1A = 0;
#define TIM1_PRESCALE (256)
  /* set compare value (round up to be on safe side) */
  OCR1A = F_CPU/TIM1_PRESCALE+1;
  /* do not generate interrupt */
  TIMSK1 = 0;
  /* reset interrupt flag */
  TIFR1 = _BV(OCF1A);
  /* input capture off; WGM: normal mode; prescaler 1:256 */
  TCCR1B = _BV(CS12); /* this starts the timer */
}