Class for the RTC module.

Dependents:   FRDM_RTC

Please pay attention

This library works only with the FRDM board connected to USB via OpenSDA connector The OpenSDA chip has a pin connected, via the resistor R24, to the KL25Z CLOCKIN pin. So this is why it works.

If you use the board alone, you must configure the MCU in a different manner. I follow the instruction in this link: https://community.freescale.com/docs/DOC-94734 but without success.

This is the code I added, for testing purpose, to my library:

Initialization code for RTC use

    // Enable the internal reference clock. MCGIRCLK is active.
    MCG->C1 |= MCG_C1_IRCLKEN_MASK;
    // Select the slow internal reference clock source.
    MCG->C2 &= ~(MCG_C2_IRCS_MASK);
    // Set PTC1 as RTC_CLKIN and select 32 KHz clock source for the RTC module.
    PORTC->PCR[1] &= ~PORT_PCR_MUX_MASK;
    PORTC->PCR[1] = PORT_PCR_MUX(1);    
    SIM->SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK;
    SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(2);
    // Set PTC3 as CLKOUT pin and selects the MCGIRCLK clock to output on the CLKOUT pin.
    SIM->SOPT2 |= SIM_SOPT2_CLKOUTSEL(0x4);
    PORTC->PCR[3] |= (PORT_PCR_MUX(0x5));
    
    // enable RTC clock
    SIM->SCGC6 |= SIM_SCGC6_RTC_MASK;
    RTC->CR = RTC_CR_SWR_MASK;
    RTC->CR &= ~RTC_CR_SWR_MASK;
    
    if (RTC->SR & RTC_SR_TIF_MASK){
         RTC->TSR = 0x00000000;
    }    
    RTC->TCR = RTC_TCR_CIR(1) | RTC_TCR_TCR(0xFF);

    ecc...

But as I said this did not work for me.

I build this class around the rtc api available from mbed. I just added user call back function and the alarm setting.

The demo software use two function to put the MCU in sleep or deepsleep. The MCU will restart for the RTC IRQ.

Revision:
0:12422f7c30d3
Child:
1:2e81444e49a9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KL25Z_RTC.cpp	Tue Jun 18 06:14:54 2013 +0000
@@ -0,0 +1,67 @@
+/* Copyright (c) 2010-2011 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "KL25Z_RTC.h"
+#include "rtc_api.h"
+
+unsigned int _alarm;
+
+KL25Z_RTC::KL25Z_RTC( unsigned int alarm)
+{
+    _alarm = alarm;
+}
+
+unsigned int KL25Z_RTC::RTC_GetAlarm( void)
+{
+    return _alarm;
+}
+
+void KL25Z_RTC::RTC_Start( void)
+{
+    rtc_init();
+
+    NVIC_EnableIRQ( RTC_Seconds_IRQn);
+    NVIC_EnableIRQ( RTC_IRQn);
+    
+    NVIC_SetVector( RTC_Seconds_IRQn, (uint32_t)&_RTC_Seconds_IRQHandler); 
+    NVIC_SetVector( RTC_IRQn, (uint32_t)&_RTC_IRQHandler); 
+
+    RTC->TAR = RTC->TSR + _alarm;
+    RTC->IER = RTC_IER_TSIE_MASK | RTC_IER_TAIE_MASK;
+
+}
+
+unsigned int KL25Z_RTC::RTC_Read( void)
+{
+    return RTC->TSR;
+}
+
+void KL25Z_RTC::_RTC_IRQHandler(void) {
+    // error_led = 1;
+    if ( RTC->SR & 0x04) {
+        printf("RTC_Alarm\r\n");
+        RTC->TAR = RTC->TSR + _alarm;
+    }
+}
+
+
+void KL25Z_RTC::_RTC_Seconds_IRQHandler(void) {
+    //
+    printf("RTC_Seconds_IRQHandler [%0d].\r\n", rtc_read());
+}
+