stm32duino / STM32RTC

Arduino RTC library for STM32.
131 stars 48 forks source link

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

Closed FRASTM closed 1 year ago

FRASTM commented 1 year ago

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:

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 commented 1 year ago

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

FRASTM commented 1 year ago

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
FRASTM commented 1 year ago

Add the sketch mixRTCAlarm

Start at 16:22:06.199
Set Alarm A in 12s (at 16:22:18)
Set Alarm B (in 400 ms) at 601 ms
Alarm B Match at 601 ms
Alarm A Match at 16:22:18
FRASTM commented 1 year ago

using the https://github.com/stm32duino/STM32RTC/pull/98 with MSP Init function for any stm32 device with a SS underflow flag with BCD mode for getTime and GetAlarm

in BIN (BINARY ONLY) mode, the SSR is down-counting ck_apre ticks (like the MIX mode does) on 32-bit and Timer/Date Registers are not used in BCD (BINARY NONE) mode, the SSR is down-counting ck_apre ticks on 16-bit

FRASTM commented 1 year ago

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