Greetings Freaks -
I have been playing with the RTC and the TCA peripherals, as I will describe. It has been a bit like a puzzle that does not have all the pieces!
Here is what I have been trying to do. I'm trying to figure out a way to determine the calibration correction for a crystal-based 32.768KHz RTC. Because this will rely on an external time reference (note: probably GPS module with 1pps output), I want to make sure that all of the required ins and outs for this part are accounted for before proceeding with other parts of the project. Here is what I plan on doing. This is for calibration only, and not for normal operation.
1. Clock source to be calibrated is a 32.768KHz crystal connected to Mega4809 (XOSC32K).
2. Oscillator output is routed to RTC with no prescaling and no active correction
3. RTC.CNT is enabled via RTC.CTRLA RTCEN bit
4. Period is set to 0X1000, just for development. It will eventually be 0xFFFF.
5. RTC overflow is assigned as the Generator for EVSYS.CHANNEL0
6. EVSYS.CHANNEL0 is assigned user EVOUTB which routes the signal to PB2 which I observe with logic analyzer
7. EVSYS.CHANNEL0 is ALSO assigned user TCA0. which is capable of providing clock input to TCA0. In code, the following is used:
TCA0.SINGLE.EVCTRL = TCA_SINGLE_EVACT_POSEDGE_gc | TCA_SINGLE_CNTEI_bm;
8. An approximately 1pps signal (generated by a TCB, which does not appear to be relevant) is generated as a surrogate for the future precision 1pps time reference
9. The 1PPs signal is counted. After an arbitrary number of counts (4, in this case), the RTC is turned on via RTCEN bit and is turned off, using the same bit after a
total of 16 1pps pulses. This "gate" signal is also routed to a port pin for observation wth the LA.
10. After the count is stopped, the RTC.CNT register is read, as is TCA0.CNT register.
11. TCA0.CNT ALWAYS shows 35. The logic analyzer ALWAYS shows that there were 38 pulses going into TCA0.
12. I can change the period of the RTC OVF pulse coming, via EVSYS, from the RTC (by changing RTC.PER). Halving RTC.PER to 0x0800 doubles the number of
missed pulses! That suggests something time (or system clock) related.
Image, below, shows the Logic Analyzer display with RTC.PER = 0x0800
Note that the RTC OVF pulses (LA Channel 1) begin exactly when expected and stop exactly when expected. Gate signal is LA Channel 0. TCA0.CNT = 70 but a software count of the pulses shows 76 and a manual count shows the same.
I DO have an alternative which is heavier on the software so this is not catastrophic. However, something just does not seem "right". I can post code, though I will have to heavily edit it do remove copious development notes.
Thanks for your time and your thoughts....