In the other post I noted that the RTC was not very accurate, but I'd try and check this out ...
So I've now conducted a test with 3 different MOD54415s. They were in their default RTC state ie no RTC correction / compensation was applied.As I said before not very accurate - but I'm interested in seeing how good the compensation register might make it. I've studied the Freescale manual and I think I've got it sorted in my mind. You can set correction time interval of between 1 and 255 seconds and then once per this time interval a correction value (-128 to +127) that will be used to adjust the 32768 divider that gives the 1 Hz clock. The trouble is you'd have to monitor the RTC over a significant length of time and compare against an accurate clock to judge how much to compensate by. Then hope that all the crystals in a batch of modules are similar enough to apply the same correction! But interesting.
Using my PC as the reference time base for this test (it connects to a time server periodically, and it's the best reference that I have easy access to), all 3 modules internal RTC were set to the correct time on Friday 14th Nov at 1pm.
The power was then switched off (so the RTC has been running on the 3v lithium rechargeable battery).
They were powered up (system time is set from the RTC on power on) and the time checked at 7pm 18th Nov. Elapsed time 4 days 6 hours = 102 hours or 367200 seconds.
Results were MOD1 14 seconds slow, MOD2 14 seconds slow, MOD3 12 seconds slow (slow means the RTC time was behind the real time on my PC).
Promising that they were all so similar. All 3 were bought at the same time and thus the expectation should be that the 32kHz crystals were all from the same manufacturing batch.
The first thing to say is that this was not as bad as I thought it might be, but it is still an error of 38.13 ppm. This is an error of 102 seconds (1 min 42 secs) in a 31 day month. Or put another way, if this was used as a RTC in an instrument where the time / date was only corrected once every 6 months (when daylight saving changed), the time would be about 10 minutes in error and could be corrected at that time. So for many applications that might be good enough?
It's probably important to say that this is the result of one test at one point in time and two possibly significant factors might have a bearing on the results...
1. The modules were on a machine in an industrial factory environment over a weekend in Britain in November when the factory would be mostly unoccupied. Although the weather has been unseasonably mild, it would have been cold through the night, and warmer through the day. Crystals are temperature sensitive devices, so this might influence the results.
2. The power was off so the RTC was operating on 3v battery. If the power was on (3.3v) this might influence the results.
Having noted those points (and now ignoring them!), what about correcting the RTC error using the on board RTC compensation ...
WARNING - the next discussion is theoretical (at the present), based on my understanding of the operation of the RTC compensation from reading the Freescale 54415 reference manual (section 37, particularly 37.4.18 and 37.6.1). I have not tested this (yet) and will report back to this post if I get round to trying it.
Having said that the following is my understanding of what would be required to correct for the 14 seconds in 367200 seconds error.
The RTC has an up counter counting the 32kHz crystal oscillations. When this counter reaches the terminal value this 1 Hz signal is used to increment all other date / time registers.
My understanding is that the compensation works by correcting the 32767 up counter with a pre-loaded VALUE (-128 to +127). This is done every INTERVAL (0 to 255) seconds. INTERVAL and VALUE are the numbers required to be loaded into the RTC Compensation Register (RTC_COMPEN) address 0xFC0A_8028 or sim2.rtc.compen in Netburner speak.
This is a 16 bit (word) register. The least sig byte is the compensation VALUE, the most sig byte is the compensation INTERVAL and the register is reset to all zeros on power on reset. An INTERVAL of zero disables compensation, so this is the default state. As far as I can see the NB RTC set and get functions do not change the compensation register.
In a really ideal world, you'd want to apply the compensation every seconds so that each second was of identical duration. But this would only give you a 1 in 32768 resolution of correction, ie steps of about 30ppm.
From my measured data above I need to correct for 14 seconds in 367200 seconds (about 38.13 ppm).
Put another way, each 1 seconds the 32kHz up counter needs to be corrected by 32768 * (14 / 367200) = 1.249 ticks. Obviously you can only "adjust" this counter in integers, so by simple iteration every 4 seconds the count needs adjusting by 1.249 * 4 = 4.996 counts. This is pretty close (close enough for me - remember other factors like temperature variation will probably have a bigger effect than the error in this compensation). The RTC on my modules were slow, so I assume to speed them up I need to preload a positive number as the compensation VALUE so that seconds will be faster to reach the terminal count and cause a second increment.
So my theoretical calculations suggest that if I use an INTERVAL of 4 and a VALUE of +5 I will compensate for the 14 in 367200 error.
As a quick check 5 counts in 4 times 32768 counts is 5 / 131072 ie 38.1470ppm and we were aiming for 38.1264ppm - good enough.
Applying this correction to my original error of 14 seconds in 367200 seconds.
In 367200 seconds there are 367200 / 4 = 91800 correction INTERVALs. Each of these corrects by 5 counts of the 32kHz counter, so 5 * 91800 = 459000 counts.
This represents a correction over the 367200 seconds of 459000 / 32768 = 14.0076 seconds. Close enough for me!
If I get around to trying this I will add some feedback to this topic.
There is a potential complication, and the Freescale manual does contradict itself about the repetative nature of the compensation.
In section 37.4.18 Table 37.21 it says that the INTERVAL register is cleared at the end of each compensation cycle when a bit in the status register (CDON) is set
If this were to be the case, you would need software to continually check the CDON bit and reload the INTERVAL and VALUE data into the compensation register for compensation to be continuous.Field1 Description
15–8
INTERVAL
Compensation interval. Indicates the window in seconds when the compensation must be carried out. A bon-zero
value starts the compensation logic. This register is cleared when RTC_SR[CDON] is set.
0x00 Compensation logic disabled
0x01 1 second
...
0xFF 255 seconds
This seems to me to contradict the notes in section 37.6.1.2 that say...
and ...If no new value is programmed, the state machine continues to perform compensation with the previously programmed values until compensation is disabled by writing zero to the compensation interval.
I hope that at the end of a compensation interval (every 4 seconds in my case), the compensation register is left unchanged an the compensation continues to operate until changed or disabled.When the compensation interval completes, the RTC_SR[CDON] bit is set and if compensation is still enabled, the next compensation cycle starts
This whole procedure is of course not very practical for volume manufacture as it requires individual (or maybe batch?) testing to calculate the compensation required and then individually applying to each NB module. If it were possible access to an Time server would be much preferable. But for those occasions where this is not possible, it suggests that by individual tuning you can achieve very acceptable accuracy.
I hope this is of some interest ...