The RTC driver topic

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: The RTC driver topic

Postby Giovanni » Mon Jul 13, 2015 7:51 am

Release is done, open a ticket about this, it will go in 3.0.1.

Giovanni

AdShea
Posts: 6
Joined: Mon Nov 30, 2015 9:03 pm
Has thanked: 1 time
Been thanked: 2 times

Re: The RTC driver topic

Postby AdShea » Thu Feb 09, 2017 7:08 pm

I'm trying to get an SNTP client (and PTP eventually) working, the HAL works great for getting and setting time, but frequency calibration is missing. I propose a pair of functions which handle adjusting trim values:

Code: Select all

int32_t rtcGetTrim(RTCDriver *rtcp);
int32_t rtcSetTrim(RTCDriver *rtcp, int32_t ppb);


The return values are the current RTC actual trim in ppb and the rtcSetTrim call does the limiting and conversion to hardware trim internally so that the interface can be platform agnostic. This functionality is important for getting good RTC performance over any real length of time.

I've been using this for the STM32F4 where the range is clamped to -487,100ppb and +488,500ppb and the hardware step size is 954ppb. The conversion from ppb to hw is (ppb*4502062)>>32 (this is one SMMUL instruction) followed by masking to 0x1FF and some bit twiddling to handle the CALP bit. Converting back is just bit-twiddling the CALP bit back in and multiplying by 954.

Currently they're not in the ChibiOS coding style and access registers directly, but I can split them into a top-level and an lld and reformat things if there's interest.

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: The RTC driver topic

Postby Giovanni » Thu Feb 09, 2017 7:52 pm

Hi,

Please go ahead. I am not sure it will make the next release however.

Giovanni

steved
Posts: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: The RTC driver topic

Postby steved » Fri Feb 10, 2017 9:28 am

Not sure whether 'ppb' is the most appropriate unit to use; might be better to deal with smaller integers which fairly directly translate to the actual values written to the calibration register.

Regardless of the final definition, the ppb multiplier (i.e. adjustment granularity) needs to be available, and could be implemented as a platform-specific macro. (IIRC there's at least one RTC where the multiplier is different for positive and negative corrections, which is a complication; so a parameter might need to be supported).

I think its implicit in your definitions, but please make sure that the sense of the trim values is defined to be consistent across device familes - so increasing the value is guaranteed to make the clock run faster, regardless of the clock chip, for example.

And IMO an attempt to set a trim value which is out of range should set the nearest value which is within range, rather than give an error.

I'm looking at how my own use case would map to the interface here; my clock trimming mostly starts as commands to 'run faster' or 'run slower', which translate into increments/decrements of the physical register trim values

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: The RTC driver topic

Postby Giovanni » Fri Feb 10, 2017 11:13 am

I agree with above points, in addition, we need to define the behavior if the feature is not available:

1) Driver implementations should silently do nothing.
2) An RTC_SUPPORTS_TRIM macro should be exported (TRUE/FALSE), the API is only exported if the low level driver reports TRUE.

Giovanni

AdShea
Posts: 6
Joined: Mon Nov 30, 2015 9:03 pm
Has thanked: 1 time
Been thanked: 2 times

Re: The RTC driver topic

Postby AdShea » Fri Feb 10, 2017 9:04 pm

Currently, my implementation changes ppb into the internal representation, where + is faster and - is slower then returns the actual value set including scaling and clamping so filters can take system accuracy into account. This never returns an error and allows you to use the same time-management filter constants across different RTCs. I chose ppb as the unit because I didn't find any RTC that trims more finely than that and it allows for an integer parameter that is platform agnostic. This also allows the driver to wrap non-linear mechanisms where larger trims are less accurate (the STM32F4 can actually trim several percent through the trim register though the resolution is lower at higher trim values).

If the platform doesn't support trim, both of these return zero for any arguments.

I'll definitely add an RTC_SUPPORTS_TRIM flag.

caheo
Posts: 9
Joined: Mon Jun 12, 2017 2:40 pm
Has thanked: 2 times

Re: The RTC driver topic

Postby caheo » Mon Jun 12, 2017 2:48 pm

Hi all, i'm a newbie to ChibiOS and thanks @Giovanni for releasing ChibiOS to the world :)

I'm not sure it's a bug or not but RTC wakeup from Power-Down Deep-Sleep mode on my STM32F103C8T6 only works if STM32_RTCSEL is set to STM32_RTCSEL_LSI (LSE is broken so I haven't gotten a chance to test out but I assume it will work). I tried to enable both HSE and LSI and set STM32_RTCSEL_HSEDIV and connect VBAT to 3.3V but it didn't work. Any idea why it happens?

One more question, from my understanding, VBAT needs to be supplied with 1.8-3.6V in order to make RTC, external LSE and backup registers functioning properly. But it doesn't required for LSI? Is that correct?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: The RTC driver topic

Postby Giovanni » Mon Jun 12, 2017 3:40 pm

hi,

It is probably because HSE is shut down, about LSI, I don't know if VBAT is required, it is possible if it is part of the backup domain.

Giovanni

caheo
Posts: 9
Joined: Mon Jun 12, 2017 2:40 pm
Has thanked: 2 times

Re: The RTC driver topic

Postby caheo » Mon Jun 12, 2017 3:56 pm

Giovanni wrote:hi,

It is probably because HSE is shut down, about LSI, I don't know if VBAT is required, it is possible if it is part of the backup domain.

Giovanni

I'll try with LSE tomorrow and will update here so others may aware of.


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 9 guests