@@ -571,6 +571,10 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
571
571
#else
572
572
RTC_getPrediv (& (RtcHandle .Init .AsynchPrediv ), & (RtcHandle .Init .SynchPrediv ));
573
573
#endif
574
+ /*
575
+ * TODO: RTC is already initialized, but RTC BIN mode is changed
576
+ * force the update of the BIN register in the RTC_ICSR
577
+ */
574
578
#if defined(RTC_BINARY_NONE )
575
579
RTC_BinaryConf (mode );
576
580
#endif /* RTC_BINARY_NONE */
@@ -615,6 +619,11 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
615
619
HAL_RTCEx_EnableBypassShadow (& RtcHandle );
616
620
#endif
617
621
622
+ /*
623
+ * NOTE: freezing the RTC during stop mode (lowPower deepSleep)
624
+ * could inhibit the alarm interrupt and prevent the system to wakeUp
625
+ * from stop mode even if the RTC alarm flag is set.
626
+ */
618
627
return reinit ;
619
628
}
620
629
@@ -733,19 +742,16 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
733
742
if (subSeconds != NULL ) {
734
743
/*
735
744
* The subsecond is the free-running downcounter, to be converted in milliseconds.
736
- * Give one more to compensate the fqce_apre uncertainty
737
745
*/
738
- if (initMode == MODE_BINARY_MIX ) {
746
+ if (initMode == MODE_BINARY_ONLY ) {
739
747
* subSeconds = (((UINT32_MAX - RTC_TimeStruct .SubSeconds + 1 ) & UINT32_MAX )
740
748
* 1000 ) / fqce_apre ;
741
- * subSeconds = * subSeconds % 1000 ; /* nb of milliseconds [0..999] */
742
- } else if (initMode == MODE_BINARY_ONLY ) {
743
- * subSeconds = (((UINT32_MAX - RTC_TimeStruct .SubSeconds + 1 ) & UINT32_MAX )
749
+ } else if (initMode == MODE_BINARY_MIX ) {
750
+ * subSeconds = (((UINT32_MAX - RTC_TimeStruct .SubSeconds ) & predivSync )
744
751
* 1000 ) / fqce_apre ;
745
752
} else {
746
753
/* the subsecond register value is converted in millisec on 32bit */
747
- * subSeconds = (((predivSync - RTC_TimeStruct .SubSeconds + 1 ) & predivSync )
748
- * 1000 ) / fqce_apre ;
754
+ * subSeconds = ((predivSync - RTC_TimeStruct .SubSeconds ) * 1000 ) / (predivSync + 1 );
749
755
}
750
756
}
751
757
#else
@@ -856,11 +862,14 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
856
862
* The subsecond param is a nb of milliseconds to be converted in a subsecond
857
863
* downcounter value and to be comapred to the SubSecond register
858
864
*/
859
- if (( initMode == MODE_BINARY_MIX ) || ( initMode == MODE_BINARY_NONE ) ) {
865
+ if (initMode == MODE_BINARY_ONLY ) {
860
866
/* the subsecond is the millisecond to be converted in a subsecond downcounter value */
861
- RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - (subSeconds * (predivSync + 1 )) / 1000 + 1 ;
867
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - (subSeconds * (predivSync + 1 )) / 1000 ;
868
+ } else if (initMode == MODE_BINARY_MIX ) {
869
+ /* the subsecond is the millisecond to be converted in a subsecond downcounter value */
870
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - (subSeconds * (predivSync + 1 )) / 1000 ;
862
871
} else {
863
- RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - (subSeconds * (predivSync + 1 )) / 1000 + 1 ;
872
+ RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - (subSeconds * (predivSync + 1 )) / 1000 ;
864
873
}
865
874
} else {
866
875
RTC_AlarmStructure .AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL ;
@@ -922,7 +931,11 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
922
931
RTC_AlarmStructure .AlarmSubSecondMask = mask << RTC_ALRMASSR_MASKSS_Pos ;
923
932
}
924
933
#if defined(RTC_ICSR_BIN )
925
- if ((initMode == MODE_BINARY_MIX ) || (initMode == MODE_BINARY_ONLY )) {
934
+ if (initMode == MODE_BINARY_ONLY ) {
935
+ /* We have an SubSecond alarm to set in RTC_BINARY_MIX or RTC_BINARY_ONLY mode */
936
+ /* The subsecond in ms is converted in ticks unit 1 tick is 1000 / fqce_apre */
937
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - (subSeconds * (predivSync + 1 )) / 1000 ;
938
+ } else if (initMode == MODE_BINARY_MIX ) {
926
939
/* We have an SubSecond alarm to set in RTC_BINARY_MIX or RTC_BINARY_ONLY mode */
927
940
/* The subsecond in ms is converted in ticks unit 1 tick is 1000 / fqce_apre */
928
941
RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - (subSeconds * (predivSync + 1 )) / 1000 ;
@@ -1026,11 +1039,14 @@ void RTC_GetAlarm(alarm_t name, uint8_t *day, uint8_t *hours, uint8_t *minutes,
1026
1039
* The subsecond is the bit SS[14:0] of the ALARM SSR register (not ALARMxINR)
1027
1040
* to be converted in milliseconds
1028
1041
*/
1029
- if ((initMode == MODE_BINARY_MIX ) || (initMode == MODE_BINARY_ONLY )) {
1042
+ if (initMode == MODE_BINARY_ONLY ) {
1043
+ /* read the ALARM SSR register on SS[14:0] bits --> 0x7FFF */
1044
+ * subSeconds = (((0x7fff - RTC_AlarmStructure .AlarmTime .SubSeconds + 1 ) & 0x7fff ) * 1000 ) / fqce_apre ;
1045
+ } else if (initMode == MODE_BINARY_MIX ) {
1030
1046
/* read the ALARM SSR register on SS[14:0] bits --> 0x7FFF */
1031
1047
* subSeconds = (((0x7fff - RTC_AlarmStructure .AlarmTime .SubSeconds + 1 ) & 0x7fff ) * 1000 ) / fqce_apre ;
1032
1048
} else {
1033
- * subSeconds = ((( predivSync - RTC_AlarmStructure .AlarmTime .SubSeconds + 1 ) & predivSync ) * 1000 ) / (predivSync + 1 );
1049
+ * subSeconds = ((predivSync - RTC_AlarmStructure .AlarmTime .SubSeconds ) * 1000 ) / (predivSync + 1 );
1034
1050
}
1035
1051
}
1036
1052
#else
0 commit comments