Class for the RTC module.

Dependents:   FRDM_RTC

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers KL25Z_RTC.cpp Source File

KL25Z_RTC.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "KL25Z_RTC.h"
00020 #include "rtc_api.h"
00021 
00022 unsigned int _alarm=0;                  // Value at which tha alarm is set.
00023 volatile unsigned int _secIRQ_Done;
00024 volatile unsigned int _alrmIRQ_Done;
00025 
00026 void (*RTC_usr2_fptr)(void);               // Pointers to user function called after
00027 void (*RTC_usr1_fptr)(void);               // IRQ assertion.
00028 
00029 
00030 KL25Z_RTC::KL25Z_RTC( unsigned int alarm)
00031 {
00032     if ( alarm != 0)
00033         _alarm = alarm;
00034 }
00035 
00036 KL25Z_RTC::~KL25Z_RTC() 
00037 {
00038     NVIC_DisableIRQ( RTC_Seconds_IRQn);
00039     RTC_usr1_fptr = NULL;
00040     RTC->IER &= ~RTC_IER_TSIE_MASK;
00041     
00042     NVIC_DisableIRQ( RTC_IRQn);
00043     RTC_usr2_fptr = NULL;
00044     RTC->IER &= ~RTC_IER_TAIE_MASK;
00045     
00046     // Disable the clock to the RTC module.
00047     SIM->SCGC6 &= ~SIM_SCGC6_RTC_MASK;
00048 }
00049 
00050 unsigned int KL25Z_RTC::RTC_GetAlarm( void)
00051 {
00052     return _alarm;
00053 }
00054 
00055 void KL25Z_RTC::RTC_SetAlarm( unsigned int alarm)
00056 {
00057     _alarm = alarm;
00058 }
00059 
00060 void KL25Z_RTC::RTC_Start( void)
00061 {
00062     rtc_init();
00063 
00064     NVIC_EnableIRQ( RTC_Seconds_IRQn);
00065     NVIC_EnableIRQ( RTC_IRQn);
00066     
00067     NVIC_SetVector( RTC_Seconds_IRQn, (uint32_t)&_RTC_Seconds_IRQHandler); 
00068     NVIC_SetVector( RTC_IRQn, (uint32_t)&_RTC_IRQHandler); 
00069 
00070     RTC->TAR = RTC->TSR + _alarm;
00071     RTC->IER = RTC_IER_TSIE_MASK | RTC_IER_TAIE_MASK;
00072 
00073 }
00074 
00075 void KL25Z_RTC::RTC_Start( void(*sec_ptr)(void), void(*alrm_ptr)(void))
00076 {
00077     rtc_init();
00078     
00079     RTC->IER = 0;
00080     
00081     if ( sec_ptr != NULL) {
00082         NVIC_EnableIRQ( RTC_Seconds_IRQn);
00083         NVIC_SetVector( RTC_Seconds_IRQn, (uint32_t)&_RTC_Seconds_IRQHandler); 
00084         RTC_usr1_fptr = sec_ptr;
00085         RTC->IER |= RTC_IER_TSIE_MASK;
00086     } else {
00087         NVIC_DisableIRQ( RTC_Seconds_IRQn);
00088         RTC->IER &= ~RTC_IER_TSIE_MASK;
00089     }
00090     
00091     if ( alrm_ptr != NULL) {
00092         RTC->TAR = RTC->TSR + _alarm;
00093         NVIC_EnableIRQ( RTC_IRQn);
00094         NVIC_SetVector( RTC_IRQn, (uint32_t)&_RTC_IRQHandler); 
00095         RTC_usr2_fptr = alrm_ptr;
00096         RTC->IER |= RTC_IER_TAIE_MASK;
00097     } else {
00098         NVIC_DisableIRQ( RTC_IRQn);
00099         RTC->IER &= ~RTC_IER_TAIE_MASK;
00100     }
00101     
00102 }
00103 
00104 unsigned int KL25Z_RTC::RTC_Read( void)
00105 {
00106     return RTC->TSR;
00107 }
00108 
00109 void KL25Z_RTC::_RTC_IRQHandler(void) 
00110 {
00111     //
00112     if ( RTC->SR & 0x04) {
00113         // printf("RTC_Alarm\r\n");
00114         RTC->TAR = RTC->TSR + _alarm;
00115         // Run the user supplied function
00116         if ( RTC_usr2_fptr != NULL)
00117             RTC_usr2_fptr();
00118         // 
00119         _alrmIRQ_Done=1;
00120     }
00121     
00122 }
00123 
00124 void KL25Z_RTC::_RTC_Seconds_IRQHandler(void) 
00125 {
00126     //
00127     _secIRQ_Done=1;
00128     
00129     // printf("RTC_Seconds_IRQHandler [%0d].\r\n", RTC->TSR);
00130     // Run the user supplied function
00131     if ( RTC_usr1_fptr != NULL)
00132         RTC_usr1_fptr();
00133 }
00134 
00135 unsigned int KL25Z_RTC::RTC_isIRQSecondDone( void)
00136 {
00137     if ( _secIRQ_Done) {
00138         _secIRQ_Done=0;
00139         return 1;
00140     } else
00141         return 0;        
00142 }
00143 
00144 unsigned int KL25Z_RTC::RTC_isIRQAlarmDone( void)
00145 {
00146     if ( _alrmIRQ_Done) {
00147         _alrmIRQ_Done=0;
00148         return 1;
00149     } else
00150         return 0;        
00151 }
00152