I would like to create a RTC for stamping log messages, with 1ms resolution and including time from year to millisecond, like: YYYY-MM-DD HH:MM:SS.mmm.
What would be the best solution?
The 32kHz clock is not divisible by 1000, which means that some special handling is needed for accurate results.
I guess that will happen with any source. The most straight forward solution seems to me using RTC with some reasonable accuracy and periodic correction (to synchronize native clocks of the MCU and strange clock we human beings use for time measurements;) and then some mid/long term synchronization with "world" clock (like every 12/24/48 hours or longer, depending on the RTC accuracy inside MCU, environment, chip to chip variance etc.) I doubt there is some code ready off-the-shelf but I've implemented something like this on nRF51 with 4ms step, periodic corrections (there is obviously no settings for RTC or TIMER on nRF5x to achieve 100% 4ms stepping) and then out of band configuration to synchronize with world time and correct the thing periodically, it worked pretty well. Note that you can even implement over the air synchronization (where each station listens to specific advertisement packet from time to time and when it is received it corrects internal RTC accordingly) but then you still need to maintain one station with very accurate RTC.