rtc.c

Go to the documentation of this file.
00001 /* This source file is part of the ATMEL AT32UC3A-SoftwareFramework-1.1.1 Release */
00002 
00003 /*This file is prepared for Doxygen automatic documentation generation.*/
00019 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
00020  *
00021  * Redistribution and use in source and binary forms, with or without
00022  * modification, are permitted provided that the following conditions are met:
00023  *
00024  * 1. Redistributions of source code must retain the above copyright notice,
00025  * this list of conditions and the following disclaimer.
00026  *
00027  * 2. Redistributions in binary form must reproduce the above copyright notice,
00028  * this list of conditions and the following disclaimer in the documentation
00029  * and/or other materials provided with the distribution.
00030  *
00031  * 3. The name of ATMEL may not be used to endorse or promote products derived
00032  * from this software without specific prior written permission.
00033  *
00034  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
00035  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00036  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
00037  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00038  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00039  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00040  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00041  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00042  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00043  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00044  */
00045 
00046 #include "rtc.h"
00047 #include "pm.h"
00048 
00049 int rtc_is_busy(volatile avr32_rtc_t *rtc)
00050 {
00051      return rtc->CTRL.busy;
00052 }
00053 
00054 int rtc_init(volatile avr32_rtc_t *rtc, unsigned char osc_type, unsigned char psel)
00055 {
00056      int ctrl;
00057 
00058      // if we use the 32KHz oscillator, we have to enable it first
00059      if (osc_type == RTC_OSC_32KHZ)
00060      {
00061           // Select the 32 KHz oscillator crystal
00062           pm_enable_osc32_crystal(&AVR32_PM);
00063           // Enable the clock 32 KHz
00064           pm_enable_clk32_no_wait(&AVR32_PM, 0);
00065      }
00066 
00067      // Wait until the rtc CTRL register is up to date
00068      while(rtc_is_busy(rtc));
00069 
00070      // Set the ctrl value to configure the RTC
00071      ctrl =
00072          (((int) osc_type) << AVR32_RTC_CTRL_CLK32_OFFSET)
00073      |   (((int) psel) << AVR32_RTC_CTRL_PSEL_OFFSET)
00074 #ifdef AVR32_RTC_CLKEN
00075      |   AVR32_RTC_CLKEN_MASK
00076 #endif
00077      ;
00078 
00079      // Set the new configuration
00080      rtc->ctrl = ctrl;
00081 
00082      // Wait until write is done
00083      while(rtc_is_busy(rtc));
00084 
00085      // if exit, it means that the configuration has not been set correctly
00086      if (rtc->ctrl != ctrl)
00087           return 0;
00088 
00089      // Set the counter value to 0
00090      rtc_set_value(rtc, 0);
00091      // Set the top value to 0xFFFFFFFF
00092      rtc_set_top_value(rtc, (unsigned long) -1);
00093 
00094      return 1;
00095 }
00096 
00097 void rtc_set_value(volatile avr32_rtc_t *rtc, unsigned long val)
00098 {
00099      // Wait until we can write into the VAL register
00100      while(rtc_is_busy(rtc));
00101      // Set the new val value
00102      rtc->val = val;
00103      // Wait until write is done
00104      while(rtc_is_busy(rtc));
00105 }
00106 
00107 unsigned long rtc_get_value(volatile avr32_rtc_t *rtc)
00108 {
00109      return rtc->val;
00110 }
00111 
00112 void rtc_enable_wake_up(volatile avr32_rtc_t *rtc)
00113 {
00114      // Wait until the rtc CTRL register is up to date
00115      while(rtc_is_busy(rtc));
00116      // Enable the wake up of the RTC
00117      rtc->ctrl |= 1 << AVR32_RTC_CTRL_WAKE_EN_OFFSET;
00118      // Wait until write is done
00119      while(rtc_is_busy(rtc));
00120 }
00121 
00122 void rtc_disable_wake_up(volatile avr32_rtc_t *rtc)
00123 {
00124      int ctrl;
00125 
00126      // Wait until the rtc CTRL register is up to date
00127      while(rtc_is_busy(rtc));
00128      // Read the current configuration
00129      ctrl = rtc->ctrl;
00130      // Disable the wake up of the RTC
00131      rtc->ctrl = ctrl & (~AVR32_RTC_CTRL_WAKE_EN_MASK);
00132      // Wait until write is done
00133      while(rtc_is_busy(rtc));
00134 }
00135 
00136 void rtc_enable(volatile avr32_rtc_t *rtc)
00137 {
00138      // Wait until the rtc CTRL register is up to date
00139      while(rtc_is_busy(rtc));
00140      // Enable the RTC
00141      rtc->ctrl |= 1 << AVR32_RTC_CTRL_EN_OFFSET;
00142      // Wait until write is done
00143      while(rtc_is_busy(rtc));
00144 }
00145 
00146 void rtc_disable(volatile avr32_rtc_t *rtc)
00147 {
00148      int ctrl;
00149 
00150      // Wait until the rtc CTRL register is up to date
00151      while(rtc_is_busy(rtc));
00152      // Read the current configuration
00153      ctrl = rtc->ctrl;
00154      // Disable the RTC
00155      rtc->ctrl = ctrl & (~AVR32_RTC_CTRL_EN_MASK);
00156      // Wait until write is done
00157      while(rtc_is_busy(rtc));
00158 }
00159 
00160 void rtc_enable_interrupt(volatile avr32_rtc_t *rtc)
00161 {
00162      rtc->IER.topi = 1;
00163 }
00164 
00165 void rtc_disable_interrupt(volatile avr32_rtc_t *rtc)
00166 {
00167      rtc->IDR.topi = 1;
00168 }
00169 
00170 void rtc_clear_interrupt(volatile avr32_rtc_t *rtc)
00171 {
00172      rtc->ICR.topi = 1;
00173 }
00174 
00175 void rtc_set_top_value(volatile avr32_rtc_t *rtc, unsigned long top)
00176 {
00177      // Wait until we can write into the VAL register
00178      while(rtc_is_busy(rtc));
00179      // Set the new val value
00180      rtc->top = top;
00181      // Wait until write is done
00182      while(rtc_is_busy(rtc));
00183 }
00184 
00185 unsigned long rtc_get_top_value(volatile avr32_rtc_t *rtc)
00186 {
00187      return rtc->top;
00188 }
00189 
00190 int rtc_interrupt_enabled(volatile avr32_rtc_t *rtc)
00191 {
00192      return rtc->IMR.topi;
00193 }
00194 
00195 int rtc_is_interrupt(volatile avr32_rtc_t *rtc)
00196 {
00197      return rtc->ISR.topi;
00198 }

Generated on Wed May 7 14:40:10 2008 for AVR32114 Using the AVR32 LCD Controller by  doxygen 1.5.3-20071008