initial commit of kl25z rtc code. local changes for debug

Dependents:   demo_FRDM_RTC

Fork of KL25Z_RTC by clemente di caprio

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);   // causes startup problems?
00066     NVIC_SetVector( RTC_Seconds_IRQn, (uint32_t)&_RTC_Seconds_IRQHandler); 
00067     //NVIC_SetVector( RTC_IRQn, (uint32_t)&_RTC_IRQHandler); // causes startup problems?
00068  
00069     RTC->TAR = RTC->TSR + _alarm;
00070     RTC->IER = RTC_IER_TSIE_MASK | RTC_IER_TAIE_MASK;
00071 
00072 }
00073 
00074 void KL25Z_RTC::RTC_Start( void(*sec_ptr)(void), void(*alrm_ptr)(void))
00075 {
00076     rtc_init();
00077     
00078     RTC->IER = 0;
00079     
00080     if ( sec_ptr != NULL) {
00081         NVIC_EnableIRQ( RTC_Seconds_IRQn);
00082         NVIC_SetVector( RTC_Seconds_IRQn, (uint32_t)&_RTC_Seconds_IRQHandler); 
00083         RTC_usr1_fptr = sec_ptr;
00084         RTC->IER |= RTC_IER_TSIE_MASK;
00085     } else {
00086         NVIC_DisableIRQ( RTC_Seconds_IRQn);
00087         RTC->IER &= ~RTC_IER_TSIE_MASK;
00088     }
00089     
00090     if ( alrm_ptr != NULL) {
00091         RTC->TAR = RTC->TSR + _alarm;
00092         NVIC_EnableIRQ( RTC_IRQn);
00093         NVIC_SetVector( RTC_IRQn, (uint32_t)&_RTC_IRQHandler); 
00094         RTC_usr2_fptr = alrm_ptr;
00095         RTC->IER |= RTC_IER_TAIE_MASK;
00096     } else {
00097         NVIC_DisableIRQ( RTC_IRQn);
00098         RTC->IER &= ~RTC_IER_TAIE_MASK;
00099     }
00100     
00101 }
00102 
00103 unsigned int KL25Z_RTC::RTC_Read( void)
00104 {
00105     return RTC->TSR;
00106 }
00107 
00108 void KL25Z_RTC::_RTC_IRQHandler(void) 
00109 {
00110     //
00111     if ( RTC->SR & 0x04) {
00112         // printf("RTC_Alarm\r\n");
00113         RTC->TAR = RTC->TSR + _alarm;
00114         // Run the user supplied function
00115         if ( RTC_usr2_fptr != NULL)
00116             RTC_usr2_fptr();
00117         // 
00118         _alrmIRQ_Done=1;
00119     }
00120     
00121 }
00122 
00123 void KL25Z_RTC::_RTC_Seconds_IRQHandler(void) 
00124 {
00125     //
00126     _secIRQ_Done=1;
00127     
00128     // printf("RTC_Seconds_IRQHandler [%0d].\r\n", RTC->TSR);
00129     // Run the user supplied function
00130     if ( RTC_usr1_fptr != NULL)
00131         RTC_usr1_fptr();
00132 }
00133 
00134 unsigned int KL25Z_RTC::RTC_isIRQSecondDone( void)
00135 {
00136     if ( _secIRQ_Done) {
00137         _secIRQ_Done=0;
00138         return 1;
00139     } else
00140         return 0;        
00141 }
00142 
00143 unsigned int KL25Z_RTC::RTC_isIRQAlarmDone( void)
00144 {
00145     if ( _alrmIRQ_Done) {
00146         _alrmIRQ_Done=0;
00147         return 1;
00148     } else
00149         return 0;        
00150 }
00151