Jump to content

Recommended Posts

Kicking off my MSP430FR2433 hobby effort I looked into the RTC peripheral, which amounts to a really simple stupid counter, the likes of which I often used WDT for back in the day.

It just counts up to RTCMOD and then fires the ISR.

So without the fancy date tracking that RTC_B did on the Wolverine chips, I had to up the ante a bit and write myself a library to convert "epoch" seconds format (# of seconds since Jan 1 1970 midnight UTC) into a "struct tm" year/month/day/hour/minute/second/etc. structure and vice versa.  I looked around for other implementations and found some inspiration from an Arduino/AVR forum, but no real code I found of use.

So I present msprtckit - https://github.com/spirilis/msprtckit

It can provide the RTC_ISR for you (for chips defining __MSP430_HAS_RTC__ like the FR4xxx/2xxx chips), or you can ignore that and implement the counter yourself, then use rtc_interpret() and rtc_epoch() to convert the timestamp into useful formats.  It corrects for leap years, and only supports dates starting 1973. (made it easier to begin after the 1st leap year after the start of the epoch)

The use of unsigned long means over 4 billion seconds will elapse before rolling over, so it might have a Y2.1K problem but we'll be long gone by then...

At some point I want to implement timezone interpretation too.


If you use the RTC_ISR that comes with the library, it will manage several things for you-

1. rtcepoch (you need to initialize this yourself somewhere to match current time)

2. rtcalarm0 & rtcalarm1 - epoch timestamps for 2 supported alarms

3. rtcalarm0_incr & rtcalarm1_incr - if > 0, these will tell the ISR to auto-increment the appropriate rtcalarm0 or rtcalarm1 when it triggers so you don't have to re-set the alarms in your code

4. These variables (rtcepoch/rtcalarm*) by default live in .infoA, which is FRAM.  You do need to have the DFPW write protection disabled for this to work & to allow the RTC ISR to update them:

// Disable FRAM write protection for .infoA section, but keep enabled for program memory


5. rtc_status is checked by your code upon wakeup to see if RTC_TICK bitfield is set, RTCALARM_0_TRIGGERED or RTCALARM_1_TRIGGERED.

6. By default the processor will not wake up every time the RTC tick occurs, but you can tell it to do so (rtc_status |= RTC_TICK_DOES_WAKEUP after rtc_init() accomplishes this)


Anyway you can probably guess this is a project I banged out in preparation for doing a clock in the future.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...