rtc.h File Reference


Detailed Description

RTC driver for AVR32 UC3.

AVR32 Real Time Counter driver module.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file rtc.h.

#include "compiler.h"
#include <avr32/io.h>

Go to the source code of this file.

Defines

Oscillator Types
#define RTC_OSC_32KHZ   1
#define RTC_OSC_RC   0
Predefined PSEL Values
#define RTC_PSEL_32KHZ_1HZ   14
 The PSEL value to set the RTC source clock (after the prescaler) to 1 Hz, when using an external 32-kHz crystal.
#define RTC_PSEL_RC_1_76HZ   15
 The PSEL value to set the RTC source clock (after the prescaler) to 1.76 Hz, when using the internal RC oscillator (~ 115 kHz).

Functions

void rtc_clear_interrupt (volatile avr32_rtc_t *rtc)
 Clear the interrupt flag. Call this function once you handled the interrupt.
void rtc_disable (volatile avr32_rtc_t *rtc)
 Disable the RTC.
void rtc_disable_interrupt (volatile avr32_rtc_t *rtc)
 Disable the interrupt feature of the RTC.
void rtc_disable_wake_up (volatile avr32_rtc_t *rtc)
 Disable the wake up feature of the RTC.
void rtc_enable (volatile avr32_rtc_t *rtc)
 Enable the RTC.
void rtc_enable_interrupt (volatile avr32_rtc_t *rtc)
 Enable the interrupt feature of the RTC. An interrupt is raised when the value of the RTC is equal to its top value.
void rtc_enable_wake_up (volatile avr32_rtc_t *rtc)
 Enable the wake up feature of the RTC.
unsigned long rtc_get_top_value (volatile avr32_rtc_t *rtc)
 This function returns the RTC current top value.
unsigned long rtc_get_value (volatile avr32_rtc_t *rtc)
 This function returns the RTC current value.
int rtc_init (volatile avr32_rtc_t *rtc, unsigned char osc_type, unsigned char psel)
 This function will initialise the RTC module. If you use the 32 KHz oscillator, it will enable this module. This function also set the top value of the RTC to 0xFFFFFFFF and the value to 0.
int rtc_interrupt_enabled (volatile avr32_rtc_t *rtc)
 Get the status of interrupts.
int rtc_is_busy (volatile avr32_rtc_t *rtc)
 This function checks if the RTC is busy or not.
int rtc_is_interrupt (volatile avr32_rtc_t *rtc)
 Check if an interrupt is raised.
void rtc_set_top_value (volatile avr32_rtc_t *rtc, unsigned long top)
 This function sets the RTC current top value.
void rtc_set_value (volatile avr32_rtc_t *rtc, unsigned long val)
 This function sets the RTC current value.


Define Documentation

#define RTC_OSC_32KHZ   1

Definition at line 58 of file rtc.h.

Referenced by main(), and rtc_init().

#define RTC_OSC_RC   0

Definition at line 59 of file rtc.h.

#define RTC_PSEL_32KHZ_1HZ   14

The PSEL value to set the RTC source clock (after the prescaler) to 1 Hz, when using an external 32-kHz crystal.

Definition at line 68 of file rtc.h.

Referenced by main().

#define RTC_PSEL_RC_1_76HZ   15

The PSEL value to set the RTC source clock (after the prescaler) to 1.76 Hz, when using the internal RC oscillator (~ 115 kHz).

Definition at line 72 of file rtc.h.


Function Documentation

void rtc_clear_interrupt ( volatile avr32_rtc_t *  rtc  ) 

Clear the interrupt flag. Call this function once you handled the interrupt.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).

Definition at line 174 of file rtc.c.

Referenced by rtc_irq().

00175 {
00176   Bool global_interrupt_enabled = Is_global_interrupt_enabled();
00177 
00178   if (global_interrupt_enabled) Disable_global_interrupt();
00179   rtc->icr = AVR32_RTC_ICR_TOPI_MASK;
00180   rtc->isr;
00181   if (global_interrupt_enabled) Enable_global_interrupt();
00182 }

void rtc_disable ( volatile avr32_rtc_t *  rtc  ) 

Disable the RTC.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).

Definition at line 146 of file rtc.c.

References rtc_is_busy().

00147 {
00148   // Wait until the rtc CTRL register is up-to-date
00149   while (rtc_is_busy(rtc));
00150   // Disable the RTC
00151   rtc->ctrl &= ~AVR32_RTC_CTRL_EN_MASK;
00152   // Wait until write is done
00153   while (rtc_is_busy(rtc));
00154 }

void rtc_disable_interrupt ( volatile avr32_rtc_t *  rtc  ) 

Disable the interrupt feature of the RTC.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).

Definition at line 163 of file rtc.c.

00164 {
00165   Bool global_interrupt_enabled = Is_global_interrupt_enabled();
00166 
00167   if (global_interrupt_enabled) Disable_global_interrupt();
00168   rtc->idr = AVR32_RTC_IDR_TOPI_MASK;
00169   rtc->imr;
00170   if (global_interrupt_enabled) Enable_global_interrupt();
00171 }

void rtc_disable_wake_up ( volatile avr32_rtc_t *  rtc  ) 

Disable the wake up feature of the RTC.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).

Definition at line 124 of file rtc.c.

References rtc_is_busy().

00125 {
00126   // Wait until the rtc CTRL register is up-to-date
00127   while (rtc_is_busy(rtc));
00128   // Disable the wake up of the RTC
00129   rtc->ctrl &= ~AVR32_RTC_CTRL_WAKE_EN_MASK;
00130   // Wait until write is done
00131   while (rtc_is_busy(rtc));
00132 }

void rtc_enable ( volatile avr32_rtc_t *  rtc  ) 

Enable the RTC.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).

Definition at line 135 of file rtc.c.

References rtc_is_busy().

Referenced by main().

00136 {
00137   // Wait until the rtc CTRL register is up-to-date
00138   while (rtc_is_busy(rtc));
00139   // Enable the RTC
00140   rtc->ctrl |= AVR32_RTC_CTRL_EN_MASK;
00141   // Wait until write is done
00142   while (rtc_is_busy(rtc));
00143 }

void rtc_enable_interrupt ( volatile avr32_rtc_t *  rtc  ) 

Enable the interrupt feature of the RTC. An interrupt is raised when the value of the RTC is equal to its top value.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).

Definition at line 157 of file rtc.c.

Referenced by main().

00158 {
00159   rtc->ier = AVR32_RTC_IER_TOPI_MASK;
00160 }

void rtc_enable_wake_up ( volatile avr32_rtc_t *  rtc  ) 

Enable the wake up feature of the RTC.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).

Definition at line 113 of file rtc.c.

References rtc_is_busy().

00114 {
00115   // Wait until the rtc CTRL register is up-to-date
00116   while (rtc_is_busy(rtc));
00117   // Enable the wake up of the RTC
00118   rtc->ctrl |= AVR32_RTC_CTRL_WAKE_EN_MASK;
00119   // Wait until write is done
00120   while (rtc_is_busy(rtc));
00121 }

unsigned long rtc_get_top_value ( volatile avr32_rtc_t *  rtc  ) 

This function returns the RTC current top value.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).
Returns:
The RTC current top value.

Definition at line 196 of file rtc.c.

00197 {
00198   return rtc->top;
00199 }

unsigned long rtc_get_value ( volatile avr32_rtc_t *  rtc  ) 

This function returns the RTC current value.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).
Returns:
The RTC current value.

Definition at line 107 of file rtc.c.

00108 {
00109   return rtc->val;
00110 }

int rtc_init ( volatile avr32_rtc_t *  rtc,
unsigned char  osc_type,
unsigned char  psel 
)

This function will initialise the RTC module. If you use the 32 KHz oscillator, it will enable this module. This function also set the top value of the RTC to 0xFFFFFFFF and the value to 0.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).
osc_type The oscillator you want to use. If you need a better accuracy, use the 32 KHz oscillator (i.e. RTC_OSC_32KHZ).
psel The preselector value for the corresponding oscillator (4-bits). To obtain this value, you can use this formula: psel = log(Fosc/Frtc)/log(2)-1, where Fosc is the frequency of the oscillator you are using (32 KHz or 115 KHz) and Frtc the frequency desired.
Returns:
1 if the initialisation succeds otherwize it will return 0.

Definition at line 60 of file rtc.c.

References rtc_is_busy(), RTC_OSC_32KHZ, rtc_set_top_value(), and rtc_set_value().

Referenced by main().

00061 {
00062   // If exit, it means that the configuration has not been set correctly
00063   if (osc_type > (1 << AVR32_RTC_CTRL_CLK32_SIZE) - 1 ||
00064       psel > (1 << AVR32_RTC_CTRL_PSEL_SIZE) - 1)
00065     return 0;
00066 
00067   // If we use the 32-kHz oscillator, we have to enable it first
00068   if (osc_type == RTC_OSC_32KHZ)
00069   {
00070     // Select the 32-kHz oscillator crystal
00071     pm_enable_osc32_crystal(&AVR32_PM);
00072     // Enable the 32-kHz clock
00073     pm_enable_clk32_no_wait(&AVR32_PM, AVR32_PM_OSCCTRL32_STARTUP_0_RCOSC);
00074   }
00075 
00076   // Wait until the rtc CTRL register is up-to-date
00077   while (rtc_is_busy(rtc));
00078 
00079   // Set the new RTC configuration
00080   rtc->ctrl = osc_type << AVR32_RTC_CTRL_CLK32_OFFSET |
00081               psel << AVR32_RTC_CTRL_PSEL_OFFSET |
00082               AVR32_RTC_CTRL_CLKEN_MASK;
00083 
00084   // Wait until write is done
00085   while (rtc_is_busy(rtc));
00086 
00087   // Set the counter value to 0
00088   rtc_set_value(rtc, 0x00000000);
00089   // Set the top value to 0xFFFFFFFF
00090   rtc_set_top_value(rtc, 0xFFFFFFFF);
00091 
00092   return 1;
00093 }

int rtc_interrupt_enabled ( volatile avr32_rtc_t *  rtc  ) 

Get the status of interrupts.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).
Returns:
1 if the interrupts are enabled otherwize it returns 0.

Definition at line 202 of file rtc.c.

00203 {
00204   return (rtc->imr & AVR32_RTC_IMR_TOPI_MASK) != 0;
00205 }

int rtc_is_busy ( volatile avr32_rtc_t *  rtc  ) 

This function checks if the RTC is busy or not.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).
Returns:
1 if the RTC is busy otherwize it will return 0.

Definition at line 54 of file rtc.c.

Referenced by rtc_disable(), rtc_disable_wake_up(), rtc_enable(), rtc_enable_wake_up(), rtc_init(), rtc_set_top_value(), and rtc_set_value().

00055 {
00056   return (rtc->ctrl & AVR32_RTC_CTRL_BUSY_MASK) != 0;
00057 }

int rtc_is_interrupt ( volatile avr32_rtc_t *  rtc  ) 

Check if an interrupt is raised.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).
Returns:
1 if an interrupt is currently raised otherwize it returns 0.

Definition at line 208 of file rtc.c.

00209 {
00210   return (rtc->isr & AVR32_RTC_ISR_TOPI_MASK) != 0;
00211 }

void rtc_set_top_value ( volatile avr32_rtc_t *  rtc,
unsigned long  top 
)

This function sets the RTC current top value.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).
top The top value you want to store.

Definition at line 185 of file rtc.c.

References rtc_is_busy().

Referenced by main(), and rtc_init().

00186 {
00187   // Wait until we can write into the VAL register
00188   while (rtc_is_busy(rtc));
00189   // Set the new val value
00190   rtc->top = top;
00191   // Wait until write is done
00192   while (rtc_is_busy(rtc));
00193 }

void rtc_set_value ( volatile avr32_rtc_t *  rtc,
unsigned long  val 
)

This function sets the RTC current value.

Parameters:
rtc Base address of the RTC (i.e. &AVR32_RTC).
val The value you want to store.

Definition at line 96 of file rtc.c.

References rtc_is_busy().

Referenced by rtc_init().

00097 {
00098   // Wait until we can write into the VAL register
00099   while (rtc_is_busy(rtc));
00100   // Set the new val value
00101   rtc->val = val;
00102   // Wait until write is done
00103   while (rtc_is_busy(rtc));
00104 }


Generated on Fri Feb 19 02:26:16 2010 for AVR32 UC3 - Real Time Counter Driver by  doxygen 1.5.5