I am designing a device (KL05) which needs to synchronize its clock (which source is based on internal 4MHz oscillator) to external reference signal got from RF module.
The idea is to timestamp incoming air packets in interrupt as they arrive and use sender's timestamp carried in a message payload.
Having such pairs of timestamps and some window of observation I'd like to run an algorithm for computing TRIM value for 4MHz oscillator to make it oscillate at nominal frequency.
Questions I have now are as below:
1. What would be the way for storing array of temperature and trim values in FLASH memory?
2. How do I program trim registers during code execution with data taken from array in FLASH (I'd like to react on temperature changes)?
3. How do I preserve my calibration data, not to lose it in case of FLASH memory erase?
4. Are there any simple algorithms for frequency calibration with small code footprint?
Thanks for any suggestions.
Regarding your question, I do not know what you want to do, for your case, you can get the external reference signal from RF, if you want to synchronize the internal fast reference clock with the external reference signal from RF, it is impossible, without PLL, user can not synchronize the internal reference clock with the external reference signal from RF.
If you just want to control the period of the the internal reference clock so that user can compensate for the temperature effect, I think it is okay. User can output the fast internal reference clock to a external pad off line and connect the fast internal reference clock to a frequency meter, establish a table between the temperature and the the trim value. When the chip is running, testing the temperature and write the corresponding trim value to FCTRIM bits in MCG_C4 to compensate the temperature effect, it is okay.
As you know, the flash of KL05 supports page erase/program, it is okay not to erase the page which save the table.
Hope it can help you.
Hello XiangJun Rong,
thank you for your reply.
I have managed to use internal FLASH to store test data on one of memory pages. It really works and I have tested correct incremental change of the value stored in certain memory location between resets and power cycles (my code did only value++ in FLASH).
Now it is time to do 4MHz internal oscillator frequency correction with respect to external reference signal.
Basically I have a plan how to judge if KL05 is running to fast or to slow.
The question now is related to 4bit TRIM value: is every step in trim value responsible for some certain frequency change (absolute or percentage) and if it is linear?
What I need to know is if I would be required to measure trimming curve for each device separately or if there is any formula that says what is the frequency offset for selected change in trim value?
Thank you for your hints.
Regarding your question, firstly, setting the FCTRM value in MCG_C4 register can change the period of the internal fast 4MHz reference clock, the method of changing the unadjusted operating point is by changing the size of the capacitor, every step of trim value will adjust the period by a percent which is based on the unadjusted value(trim value is zero), in general, there is linearity between the adding period and the step. I suggest you output the 4MHz clock signal to a frequency meter so that the 4MHz can be calibrated. the 4MHz clock frequency is chip by chip, You should calibrate every chip separately, there is not a general formula.
Hope it can help you