Allows easy usage of the LPC1768 RTC interrupts

Dependents:   Mini_DK_clk MakerBotServer GT-ACQUAPLUS_Consumo GT-ACQUAPLUS_Eficiencia ... more

The RTC library allows easy access to the LPC1768s interrupt sources on its RTC. There are two different interrupt sources: periodic interrupts, for example every new second, hour, etc, and an alarm, which is called at a specified time.

Time initialization

This library only allows easy usage of the RTC interrupts. You have to initialize the time functions (so the RTC) the normal way specified here: http://mbed.org/handbook/Time?action=view&revision=11592

Hello World!

#include "mbed.h"
#include "RTC.h"

DigitalOut led(LED1);

void ledFunction( void )
{
    led = 1;
    RTC::detach(RTC::Second);
}

void displayFunction( void )
{
    time_t seconds = time(NULL);
    printf("%s", ctime(&seconds));
}

void alarmFunction( void )
{
    error("Not most useful alarm function");
}

int main()
{
    set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37

    tm t = RTC::getDefaultTM();
    t.tm_sec = 5;
    t.tm_min = 36;

    RTC::alarm(&alarmFunction, t);
    RTC::attach(&displayFunction, RTC::Second);
    RTC::attach(&ledFunction, RTC::Minute);

    while(1);
}

Periodic interrupts

Periodic interrups can be attached by using:

RTC::attach([function], [TimeUnit]);

The TimeUnit specifies which unit should increase for the function to be called. This function is useful if you are making for example a clock: You can simply connect the display update to an RTC interrupt. Of course you can do something similar by using timer objects, but they aren't timed exactly correct, and it is nicer to do it directly on the RTC.

Alarm function

The LPC1768's RTC also allows for one alarm to be activated. The alarm goes off the first time there is a compare match on a specified time and the current time. All fields of the normal C tm structure are supported (http://www.cplusplus.com/reference/ctime/tm/), set a field at -1 for it to be ignored. The RTC::getDefaultTM() function helps with that, it returns a tm structure with every field initialized to -1, so don't care.

So if you want to make an alarm that gets called every monday, every 5 minutes and 30 seconds after the hour, you would use:

tm t = RTC::getDefaultTM();
t.tm_sec = 30;     //30 seconds
t.tm_min = 5;      //5 minute
t.tm_wday = 1;     //monday
RTC::alarm([yourFunction], t);

Attaching member functions

For compactness of the documentation attaching member functions of objects isn't included. However it works exactly the same as for example attaching one to a Ticker object: http://mbed.org/users/mbed_official/code/mbed/docs/63cdd78b2dc1/classmbed_1_1Ticker.html#af92f41ff11906b1f96fa6bbe0b17aa29

Disclaimer

Believe it or not, but I didn't test every possible combination of alarm settings / interrupts. So especially for the larger timescales there is an increased chance on bugs.

RTC.h

Committer:
Sissors
Date:
2012-12-07
Revision:
1:be9d058ee5c7
Parent:
0:39767ffe05e6

File content as of revision 1:be9d058ee5c7:

#ifndef RTC_H
#define RTC_H

#include "mbed.h"

/**
* Library to get access to the interrupt functionality of the LPC1768's RTC.
*
* This class is completely static: which means you don't have to create an RTC object,
* there is always one object automatically created when you include this class. Since 
* there is only one RTC, more than one would make no sense.
*
* @code
* #include "mbed.h"
* #include "RTC.h"
*
* DigitalOut led(LED1);
*
* void ledFunction( void )
* {
*     led = 1;
*     RTC::detach(RTC::Second);
* }
* 
* void displayFunction( void )
* {
*     time_t seconds = time(NULL);
*     printf("%s", ctime(&seconds));
* }
* 
* void alarmFunction( void )
* {
*     error("Not most useful alarm function");
* }
* 
* int main()
* {
*     set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37
* 
*     tm t = RTC::getDefaultTM();
*     t.tm_sec = 5;
*     t.tm_min = 36;
* 
*     RTC::alarm(&alarmFunction, t);
*     RTC::attach(&displayFunction, RTC::Second);
*     RTC::attach(&ledFunction, RTC::Minute);
* 
*     while(1);
* }
* @endcode
**/
class RTC {
public:
    /**
    * Available time units for interrupts
    *
    * RTC::Second, RTC::Minute, RTC::Hour,
    * RTC::Day, RTC::Month, RTC::Year
    */
    enum TimeUnit {Second, Minute, Hour, 
                    Day, Month, Year};
                    
    /**
    * Call a function when the specified time unit increases
    *
    * You can attach one function for each TimeUnit. When several are
    * attached the smalles TimeUnit is called first.
    *
    * Member functions of a class can be attached the normal way (similar to for example Ticker).
    *
    * @param function - the function to call
    * @param interval - the TimeUnit which specifies the interval
    */
    static void attach(void (*function)(void), TimeUnit interval);
    template<typename T>
        void attach(T *object, void (T::*member)(void), TimeUnit interval);
    
    /**
    * Detach an interrupt function
    *
    * @param interval - the TimeUnit of the interrupt to detach
    */
    static void detach(TimeUnit interval);
    
    /**
    * Call a function when a specified time is reached
    *
    * Only one alarm is possible. Make fields of the tm structure -1 for don't care.
    *
    * Member functions of a class can be attached the normal way (similar to for example Ticker).
    *
    * @param function - the function to call
    * @param alarmTime - tm structure which specifies when to activate the alarm
    */
    static void alarm(void (*function)(void), tm alarmTime);
    template<typename T>
        void alarm(T *object, void (T::*member)(void), tm alarmTime);
    
    /**
    * Disable the alarm
    */
    static void alarmOff( void );
    
    /**
    * Returns a default tm structure where each field is initialized
    * to -1, so it is ignored by the alarm function.
    *
    * Available fields: http://www.cplusplus.com/reference/ctime/tm/
    * Except tm_isdst all of them can be used for the alarm
    *
    * @param return - tm structure initialized to -1
    */
    static tm getDefaultTM( void );
    
    

private:
    static void IRQHandler( void );

    static FunctionPointer attachCB[6];
    static FunctionPointer alarmCB;
    
    //If someone knows a nicer way to do this, please tell me
    static bool initialRun;
    static void _attach(TimeUnit interval);
    static void _alarm(tm alarmTime);


};

#endif