Hi Marc
Here is the code in the uTasker project to initialise the RTC:
if (MCF_CLOCK_RTCCR != (SECRET_BIT | RTCSEL_OSC | RTC_LPEN | RTC_REFS | KHZEN_KHZ | RTC_OSCEN)) { // only on first start initialise 32k
RTCCTL = RTC_ENABLE; [0x80]
MCF_CLOCK_RTCCR = (SECRET_BIT | RTCSEL_OSC | RTC_LPEN | RTC_REFS | KHZEN_KHZ | RTC_OSCEN); [0x57]
RTCGOCL = (unsigned short)(32768/4); [0x2000]
MCF_CLOCK_RTCCR = (SECRET_BIT | RTCSEL_OSC | RTC_LPEN | RTC_REFS | KHZEN_KHZ | RTC_OSCEN);
}
The values in [] are the values written to the registers in case you want to try the sequence in the debugger. The SECRET_BIT (0x40) seems to be an undocumented bit which is needed to be set to get the 32k RTC counting.
I am not sure about the double-write to RTCCR - it may be left over code after a change some time...(?)
To run from the main clock:
RTCCTL = RTC_ENABLE; [0x80]
MCF_CLOCK_RTCCR = 0; // select system clock for RTC
RTCGOCU = (unsigned short)((CRYSTAL_FREQ) >> 16); [0x02dc]
RTCGOCL = (unsigned short)(CRYSTAL_FREQ); [0x6c00]
If I remember correctly it is important to write to RTCGOCU and RTCGOCL as 32 bit registers (although the content is only 16 bit width). If the code writes them as 16 bit registers the writes aren't accepted and so it doesn't run.
I believe that the code has been successfully used on M5221X, M521XX and M5225X devices so tell me if you have any problems.
Regards
Mark
www.uTasker.com
- OS, TCP/IP stack, USB, device drivers and simulator for M521X, M521XX, M5221X, M5222X, M5223X, M5225X. One package does them all - "Embedding it better..."