var rtc = RtcController.GetDefault();
// make sure RTC battery still good.
DateTime MyTime = new DateTime(2024, 8, 30, 15, 10, 20);
var newDt = RtcDateTime.FromDateTime(MyTime );
rtc.SetTime(newDt);
///rtc and system time are different, you may need update system time
SystemTime.SetTime(MyTime );
I executed this on the same board from Visual Studio Immediate Window.
?DateTime.Now.Ticks
636188257822428706
?DateTime.Now.Ticks
636188257840136941
?rtc.Now.Ticks
638605730480000000
?rtc.Now.Ticks
638605730480000000
?rtc.Now.Ticks
638605730480000000
?DateTime.Now.Ticks
636188257985426954
And this line … take about 4 seconds to execute:
var rtc = RtcController.GetDefault();
The board has USB power and a lithium battery.
Power to Vdd is 2.803 Vdc
VBat pin is not connected
Same board … if I disconnect all power and then reset the application.
It works ! RTC can be set and it runs … for a while.
The RTC seems to be getting into an unstable state.
Hi, we just tested again, no issue. Rtc and PC always have same time speed.
As the test below, RTC time are same as PC, after ~ 20 minutes disconnected power source.
~ 6 minutes you see System Time just mean 6minute from last boot.
After Power On, the RTC runs just fine. Then after a while it will switch to 1/10th the speed. And then later stop running completely.
Seems to me your battery is bad. Is this custom board or our dev board?
If our dev board that using big cap, you need to charge the cap if it is low. That cap can hold only ~ 2days, if I remember correctly. There is command to charge the cap.
rtc.SetChargeMode(BatteryChargeMode.Fast);
RTC runs incorrectly if battery is lower than needed.
After a while it randomly fails.
Once the RTC stops running or is running very slowly, it will give the errors I stated in the original post. Only fix is to COMPLETELY disconnect all power and restart. RESET does not fix the problem. Shutdown does not fix the problem.
Also … why does it take 4 seconds to talk to the RTC clock ?
var rtc = RtcController.GetDefault();
During the 4 seconds, TinyCLR misses interrupts, threads don’t run, etc.
Reduced 4 seconds to 1 seconds. This is timeout when not detect 32.768 crystal.
A note here is, the fix uses TIM16 to calibrate LSI clock, and TIM16 is PB8 pwm. if you are using PWM on this pin, you need call RtcController.GetDefault() before initialize pwm. I don’t see any wrong if call it right after Main(), all should be fine.
Also, it is using RC clock so still not perfect, I am seeing time is different 1-2 seconds after 10-20 minutes.
So there is
rtc.Calibrate(xxx);
xxx: from [-510…510] pulses.
each pulse: 0.030517578125ms.
You can add / remove xxx pulses every 32 seconds.
After a while it randomly fails.
Once the RTC stops running or is running very slowly, it will give the errors I stated in the original post.
I could not reproduce this issue. I need simple project that I can reproduce the issue. But hopefully the new version fix it.