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.

Committer:
clemente
Date:
Sun Aug 25 22:13:55 2013 +0000
Revision:
4:3bd0dc0c2b2e
Parent:
3:2cee0b9ac1ff
First release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
clemente 0:12422f7c30d3 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
clemente 0:12422f7c30d3 2 *
clemente 0:12422f7c30d3 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
clemente 0:12422f7c30d3 4 * and associated documentation files (the "Software"), to deal in the Software without
clemente 0:12422f7c30d3 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
clemente 0:12422f7c30d3 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
clemente 0:12422f7c30d3 7 * Software is furnished to do so, subject to the following conditions:
clemente 0:12422f7c30d3 8 *
clemente 0:12422f7c30d3 9 * The above copyright notice and this permission notice shall be included in all copies or
clemente 0:12422f7c30d3 10 * substantial portions of the Software.
clemente 0:12422f7c30d3 11 *
clemente 0:12422f7c30d3 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
clemente 0:12422f7c30d3 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
clemente 0:12422f7c30d3 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
clemente 0:12422f7c30d3 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
clemente 0:12422f7c30d3 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
clemente 0:12422f7c30d3 17 */
clemente 0:12422f7c30d3 18
clemente 0:12422f7c30d3 19 #ifndef __KL25Z_RTC_H
clemente 0:12422f7c30d3 20 #define __KL25Z_RTC_H
clemente 0:12422f7c30d3 21
clemente 0:12422f7c30d3 22 #include "mbed.h"
clemente 0:12422f7c30d3 23
clemente 2:56a50064749e 24 // Usage:
clemente 2:56a50064749e 25 // #include "mbed.h"
clemente 2:56a50064749e 26 // #include "KL25Z_RTC.h"
clemente 2:56a50064749e 27 //
clemente 2:56a50064749e 28 // DigitalOut myled(LED1);
clemente 2:56a50064749e 29 // KL25Z_RTC rtc( 15);
clemente 2:56a50064749e 30 // Serial pc(USBTX, USBRX);
clemente 2:56a50064749e 31 //
clemente 2:56a50064749e 32 // void alm ( void);
clemente 2:56a50064749e 33 // void sec ( void);
clemente 2:56a50064749e 34 //
clemente 2:56a50064749e 35 // int main() {
clemente 2:56a50064749e 36 //
clemente 2:56a50064749e 37 // pc.baud( 230400);
clemente 2:56a50064749e 38 // pc.printf("RTC Management.\r\n");
clemente 2:56a50064749e 39 //
clemente 2:56a50064749e 40 // rtc.RTC_Start( &sec, &alm);
clemente 2:56a50064749e 41 //
clemente 2:56a50064749e 42 // while(1) {
clemente 2:56a50064749e 43 // // pc.printf("RTC [%0d].\r\n", rtc.RTC_Read());
clemente 2:56a50064749e 44 // wait( 1.0);
clemente 2:56a50064749e 45 // }
clemente 2:56a50064749e 46 // }
clemente 2:56a50064749e 47 //
clemente 2:56a50064749e 48 // void sec ( void)
clemente 2:56a50064749e 49 // {
clemente 2:56a50064749e 50 // pc.printf("sec\r\n");
clemente 2:56a50064749e 51 // }
clemente 2:56a50064749e 52 //
clemente 2:56a50064749e 53 // void alm ( void)
clemente 2:56a50064749e 54 // {
clemente 2:56a50064749e 55 // pc.printf("alrm\r\n");
clemente 2:56a50064749e 56 // }
clemente 2:56a50064749e 57 //
clemente 2:56a50064749e 58
clemente 2:56a50064749e 59
clemente 0:12422f7c30d3 60 class KL25Z_RTC
clemente 0:12422f7c30d3 61 {
clemente 0:12422f7c30d3 62 public:
clemente 3:2cee0b9ac1ff 63 /** Constructor.
clemente 3:2cee0b9ac1ff 64 *
clemente 3:2cee0b9ac1ff 65 * @param alarm espressed in seconds
clemente 3:2cee0b9ac1ff 66 */
clemente 0:12422f7c30d3 67 KL25Z_RTC( unsigned int alarm);
clemente 3:2cee0b9ac1ff 68
clemente 4:3bd0dc0c2b2e 69 /** Desctructor
clemente 4:3bd0dc0c2b2e 70 */
clemente 4:3bd0dc0c2b2e 71 ~KL25Z_RTC();
clemente 4:3bd0dc0c2b2e 72
clemente 4:3bd0dc0c2b2e 73 /** Start the RTC module, using IRQ but without registering user defined callback functions.
clemente 4:3bd0dc0c2b2e 74 * Access to the elapsed time is possible using the "Read" methid.
clemente 4:3bd0dc0c2b2e 75 * The IRQ transaction is visible through "RTC_isIRQxxxxDone" methods.
clemente 4:3bd0dc0c2b2e 76 *
clemente 3:2cee0b9ac1ff 77 * @param none
clemente 3:2cee0b9ac1ff 78 * @return none
clemente 3:2cee0b9ac1ff 79 */
clemente 0:12422f7c30d3 80 void RTC_Start( void);
clemente 3:2cee0b9ac1ff 81
clemente 3:2cee0b9ac1ff 82 /** Start the module. Setting up the IRQ handler for the second IRQ and/or for the Alarm
clemente 3:2cee0b9ac1ff 83 * @param sec_ptr, alrm_ptr pointer to the IRQ handler
clemente 3:2cee0b9ac1ff 84 * @return none
clemente 3:2cee0b9ac1ff 85 */
clemente 1:2e81444e49a9 86 void RTC_Start( void(*sec_ptr)(void), void(*alrm_ptr)(void));
clemente 3:2cee0b9ac1ff 87
clemente 3:2cee0b9ac1ff 88 /** Return the alarm value
clemente 3:2cee0b9ac1ff 89 * @param none
clemente 3:2cee0b9ac1ff 90 * #return the alarm value
clemente 3:2cee0b9ac1ff 91 */
clemente 0:12422f7c30d3 92 unsigned int RTC_GetAlarm( void);
clemente 3:2cee0b9ac1ff 93
clemente 3:2cee0b9ac1ff 94 /** Configure the alarm value
clemente 3:2cee0b9ac1ff 95 * @param alarm in seconds
clemente 3:2cee0b9ac1ff 96 * @return none
clemente 3:2cee0b9ac1ff 97 */
clemente 2:56a50064749e 98 void RTC_SetAlarm( unsigned int alarm);
clemente 3:2cee0b9ac1ff 99
clemente 3:2cee0b9ac1ff 100 /** Return the second elapsed
clemente 3:2cee0b9ac1ff 101 * @param none
clemente 3:2cee0b9ac1ff 102 * @return the second elapsed
clemente 3:2cee0b9ac1ff 103 */
clemente 0:12422f7c30d3 104 unsigned int RTC_Read( void);
clemente 4:3bd0dc0c2b2e 105
clemente 4:3bd0dc0c2b2e 106 /** Return the status of the IRQ for seconds elapsed.
clemente 4:3bd0dc0c2b2e 107 * Use this function for a polling like method, using the IRQ internally the module but without callbacks define.
clemente 4:3bd0dc0c2b2e 108 * The flag is reset after the read.
clemente 4:3bd0dc0c2b2e 109 *
clemente 4:3bd0dc0c2b2e 110 * @param none
clemente 4:3bd0dc0c2b2e 111 * @return 1 if the IRQ is done.
clemente 4:3bd0dc0c2b2e 112 */
clemente 4:3bd0dc0c2b2e 113 unsigned int RTC_isIRQSecondDone( void);
clemente 4:3bd0dc0c2b2e 114
clemente 4:3bd0dc0c2b2e 115 /** Return the status of the IRQ for the alarm.
clemente 4:3bd0dc0c2b2e 116 * Use this function for a polling like method, using the IRQ internally the module but without callbacks define.
clemente 4:3bd0dc0c2b2e 117 * The flag is reset after the read.
clemente 4:3bd0dc0c2b2e 118 *
clemente 4:3bd0dc0c2b2e 119 * @param none
clemente 4:3bd0dc0c2b2e 120 * @return 1 if the IRQ is done.
clemente 4:3bd0dc0c2b2e 121 */
clemente 4:3bd0dc0c2b2e 122 unsigned int RTC_isIRQAlarmDone( void);
clemente 0:12422f7c30d3 123
clemente 0:12422f7c30d3 124 private:
clemente 0:12422f7c30d3 125
clemente 0:12422f7c30d3 126 static void _RTC_IRQHandler(void);
clemente 0:12422f7c30d3 127 static void _RTC_Seconds_IRQHandler(void);
clemente 0:12422f7c30d3 128
clemente 0:12422f7c30d3 129 };
clemente 0:12422f7c30d3 130
clemente 0:12422f7c30d3 131 #endif