Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rtc mix mode with subseconds param. expressed in milliseconds #93

Merged
merged 8 commits into from
Sep 25, 2023

Conversation

FRASTM
Copy link
Contributor

@FRASTM FRASTM commented Jun 27, 2023

stm32duino RTC add a parameter to set binary or bcd or Mix mode

The MIX mode of the RTC combines the binary and the BCD calendar modes
"The SSR binary down-counter is extended to 32-bit length and is free running. The time and
date calendar BCD registers are also available."

This is available of several stm32 devices like the stm32WL, stm32L4plus, stm32U5

The STM32RTC APi has subsecond parameter which is a value in milliseconds.
Because of the BINARY MIX mode, the parameter subsecond could be confusing as the subsecond register of the RTC is:

  • a nb of millisec [0-999] in BCD mode

  • a nb of ticks in BIN or MIX mode -> one tick is one counter unit at the fqce = RTC clock source freq / fqce_apre

For example, in MIX mode, when RTC is clocked by the LSE (32768Hz) the tick is downcounting at 256Hz
(1 tick is ~ 3.9ms )
The RTC subsecond register is converted in ms by (0xFFFFFFFF - SubSecReg) * 1000/256
And N miliseconds is converted in ticks (count unit) by 0xFFFFFFFF - (SubSecReg * 256) /1000

With this PR, the STM32RTC API is always giving the subsecond parameter in a nb of milliseconds.
Especially with functions:

getSubSeconds() and getTime()

getAlarmSubSeconds()

setAlarmSubSeconds() and setAlarmTime()

setSubSeconds() and setTime() (but Subsecond value is ignored there RO register)

@FRASTM
Copy link
Contributor Author

FRASTM commented Jun 27, 2023

Testing examples/advancedRTCAlarm or examples/RTC_Seconds with arduino RTC sketch when the on the nucleo_wl55jc or disco_l4s5i_iot1a
rtc.setRTCMode(STM32RTC::MODE_MIX);
before
rtc.begin(); // initialize RTC 24H format

fpistm
fpistm previously requested changes Jun 30, 2023
src/rtc.c Outdated Show resolved Hide resolved
@FRASTM FRASTM force-pushed the rtc_mix branch 3 times, most recently from 23d55e4 to 0cad1db Compare June 30, 2023 11:13
@FRASTM
Copy link
Contributor Author

FRASTM commented Jun 30, 2023

The change to include the RTC MIX mode when manipulating the subsecond register
With this change, we can program alarm in MIX mode for few ms or seconds

13:03:53.626 -> start at subsec = 0x12
13:03:53.626 -> set aAlarm A at = 0xda 
13:03:53.626 -> set aAlarm B at = 0x1012 --> 4096 is 16 seconds with a fqce_APRE
13:03:54.241 -> Alarm A Match! at subsec = 0xda
13:04:09.460 -> Alarm B Match! at subsec = 0x1012

Setting an ALARM A in calendar and an alarm B in subsecond :

14:02:18.298 -> start at subsec = 18
14:02:18.298 -> set Alarm A 
14:02:18.298 -> set Alarm B at = 318
14:02:19.454 -> Alarm B Match! at subsec = 318
14:02:23.208 -> Alarm A Match! at subsec = 1280

src/rtc.c Outdated Show resolved Hide resolved
src/rtc.c Outdated Show resolved Hide resolved
@FRASTM FRASTM requested a review from fpistm September 7, 2023 07:16
@FRASTM FRASTM changed the title Rtc mix Rtc mix mode with subseconds param. expressed in milliseconds Sep 7, 2023
@FRASTM FRASTM force-pushed the rtc_mix branch 6 times, most recently from 62a49d8 to d98d2a1 Compare September 8, 2023 14:44
@FRASTM
Copy link
Contributor Author

FRASTM commented Sep 22, 2023

Testing on stm32g474 nucleo : RTC has BCD mode only or stm32wl55 nucleo RTC in BCD or MIX mode:

12:00:27.195 -> Start at 16:22:06.300
12:00:27.195 -> Set Alarm A in 12s (at 16:22:18)
12:00:27.195 -> Set Alarm B (in 600 ms) at 902 ms
12:00:27.806 -> Alarm B Match at 902 ms
12:00:39.893 -> Alarm A Match at 16:22:18

Testing on stm32wl55 nucleo RTC in BIN mode:

12:59:00.320 -> Start at 300 ms 
12:59:00.349 -> Set Alarm A in 12s (at 12003 ms)
12:59:00.349 -> Set Alarm B (in 600 ms) at 902 ms
12:59:00.925 -> Alarm B Match at 902 ms
12:59:12.046 -> Alarm A Match at 12003 ms 

@FRASTM FRASTM force-pushed the rtc_mix branch 2 times, most recently from c14822f to 0d67dc0 Compare September 22, 2023 14:44
@FRASTM FRASTM marked this pull request as ready for review September 22, 2023 16:02
FRASTM and others added 7 commits September 25, 2023 09:54
Configure the alarm depending on the MIX mode
In case the RTC is running in MIX mode (binary and calendar),
the subsecond register is a 32-bit value (and not msec)

The Subsecond parameter is expressed in millisecond in
RTC_SetTime/GetTime RTC_StartAlarm/GetAlarm

Signed-off-by: Francois Ramu <[email protected]>
define the Getime and startAlarm function when the RTC is configured
in BCD (MODE_BINARY_NONE) or BIN (MODE_BINARY_ONLY).
In MODE_BINARY_ONLY, Time and Date are not used, only subsecond

Signed-off-by: Francois Ramu <[email protected]>
Add new API to set alarm Time with subseconds which is a nb of
milliseconds. Similar to setAlarmSubSeconds.

Signed-off-by: Francois Ramu <[email protected]>
This examples is configuring the RTC in MIX (binary + calendar) or BCD
and sets Alarm A & B (if exists) few ms after the current time.

Signed-off-by: Francois Ramu <[email protected]>
This examples is configuring the RTC in BIN mode (binary only)
and sets Alarm A & B (if exists) a few ms after the current time.

Signed-off-by: Francois Ramu <[email protected]>
Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Tested with stm32duino/STM32LoRaWAN#25

@fpistm fpistm merged commit d3f6526 into stm32duino:main Sep 25, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

2 participants