My current project is an industrial piece of equipment that must log data. To do that I need accurate real time clock and a unix UTC timestamp to log records with. Also this equipment is intended to be used for 20+ years in the field. 32 bit unix timestamps are destined to roll over and fail in the 2030's. I wanted to preclude this as a problem.
My initial try was to use the SSP HAL driver for the rtc. I am sure Renesas will clean this code up in the future but I discovered several problems that precluded me from using the SSP driver:
- I use IAR for synergy and S7G2 microcontroller
- When using the debugger the rtc would stop running
- It would run sometimes if I tested my code in the release configuration
- I needed a clock that ran all the time and I could go into and out of debug with no concerns that I would not have a valid real time clock. Battery operation is a MUST for me.
- I needed a rock solid unix timestamp to attach to my log records
In the end I decided I needed to write my own drivers until Renesas updated their drivers.
- I developed a power on routine that made sure the rtc was operating properly no matter if debug or release
- IAR provides 64 bit unix compatible timestamp functions in its time.h library. I wanted to use these 64 bit functions
- My RTC code uses the binary count mode instead of calendar - more compatible with timestamps
- I defined a constant offset that was "seconds from 1-1-1970 to 1-1-2017" The rtc counter will count seconds from 1-1-2017. Using this technique the rtc should have at least another 60 years before running out of 32 bits
- I have a high priority 1 ms interrupt used for critical control timing. I use this interrupt to increment my global 64 bit timestamp. Note you should use a mutex or disable interrupts to read this variable! Or you can get the timestamp from the rtc every time you need it.
- I wrote a set time to rtc and get time from rtc routines
The code works like a champ!!! I have correct real time without concerns if I cycle power, debug and start again, resets, etc. Battery operation works well. It seems to be bulletproof.
Warren suggested that I post the code to help others that have similar needs. I have attached a file with the code you will need.
Hope this helps ...
Steve D