@@ -76,6 +76,9 @@ static int16_t predivSync = -1;
76
76
static uint32_t prediv = RTC_AUTO_1_SECOND ;
77
77
#endif /* !STM32F1xx */
78
78
79
+ /* predivAsync is 0x7F with LSE clock source */
80
+ static uint32_t fqce_apre ;
81
+
79
82
static hourFormat_t initFormat = HOUR_FORMAT_12 ;
80
83
static binaryMode_t initMode = MODE_BINARY_NONE ;
81
84
@@ -326,6 +329,8 @@ static void RTC_computePrediv(int8_t *asynch, int16_t *synch)
326
329
Error_Handler ();
327
330
}
328
331
* synch = (int16_t )predivS ;
332
+
333
+ fqce_apre = clkVal / (* asynch + 1 );
329
334
}
330
335
#endif /* !STM32F1xx */
331
336
@@ -586,7 +591,7 @@ bool RTC_IsConfigured(void)
586
591
* @param hours: 0-12 or 0-23. Depends on the format used.
587
592
* @param minutes: 0-59
588
593
* @param seconds: 0-59
589
- * @param subSeconds: 0-999 (not used)
594
+ * @param subSeconds: 0-999 (not used and SSR register is set to 0xffffffff )
590
595
* @param period: select HOUR_AM or HOUR_PM period in case RTC is set in 12 hours mode. Else ignored.
591
596
* @retval None
592
597
*/
@@ -660,8 +665,13 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
660
665
#if defined(RTC_SSR_SS )
661
666
if (subSeconds != NULL ) {
662
667
if (initMode == MODE_BINARY_MIX ) {
663
- * subSeconds = UINT32_MAX - RTC_TimeStruct .SubSeconds ;
668
+ /* The subsecond is the free-running downcounter, to be converted in milliseconds */
669
+ * subSeconds = (((UINT32_MAX - RTC_TimeStruct .SubSeconds + 1 ) & UINT32_MAX )
670
+ * 1000 ) / fqce_apre ;
671
+ /* give one more to compensate the 3.9ms uncertainty */
672
+ * subSeconds = * subSeconds % 1000 ; /* value in milliseconds [0-999] */
664
673
} else {
674
+ /* the subsecond register value is converted in millisec */
665
675
* subSeconds = ((predivSync - RTC_TimeStruct .SubSeconds ) * 1000 ) / (predivSync + 1 );
666
676
}
667
677
}
@@ -731,7 +741,7 @@ void RTC_GetDate(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *wday)
731
741
* @param hours: 0-12 or 0-23 depends on the hours mode.
732
742
* @param minutes: 0-59
733
743
* @param seconds: 0-59
734
- * @param subSeconds: 0-999
744
+ * @param subSeconds: 0-999 milliseconds
735
745
* @param period: HOUR_AM or HOUR_PM if in 12 hours mode else ignored.
736
746
* @param mask: configure alarm behavior using alarmMask_t combination.
737
747
* See AN4579 Table 5 for possible values.
@@ -766,7 +776,13 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
766
776
{
767
777
RTC_AlarmStructure .AlarmSubSecondMask = predivSync_bits << RTC_ALRMASSR_MASKSS_Pos ;
768
778
}
769
- RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - (subSeconds * (predivSync + 1 )) / 1000 ;
779
+ if (initMode == MODE_BINARY_MIX ) {
780
+ /* the subsecond is the millisecond to be converted in a subsecond downcounter value */
781
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - ((subSeconds * fqce_apre ) / 1000 + 1 );
782
+ /* give one more to compensate the 3.9ms uncertainty */
783
+ } else {
784
+ RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - (subSeconds * (predivSync + 1 )) / 1000 ;
785
+ }
770
786
} else {
771
787
RTC_AlarmStructure .AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL ;
772
788
}
@@ -830,8 +846,8 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
830
846
/* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM A */
831
847
RTC_AlarmStructure .AlarmSubSecondMask = mask << RTC_ALRMASSR_MASKSS_Pos ;
832
848
}
833
- /* Special case for ALARM B configuration for stm32WL Lorawan */
834
- RTC_AlarmStructure .AlarmTime .SubSeconds = subSeconds ;
849
+ /* The subsecond in ms is converted in ticks unit 1 tick is 1000 / fqce_apre */
850
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - ( subSeconds * fqce_apre ) / 1000 ;
835
851
}
836
852
837
853
#else
@@ -928,7 +944,12 @@ void RTC_GetAlarm(alarm_t name, uint8_t *day, uint8_t *hours, uint8_t *minutes,
928
944
#if defined(RTC_SSR_SS )
929
945
if (subSeconds != NULL ) {
930
946
if (initMode == MODE_BINARY_MIX ) {
931
- * subSeconds = UINT32_MAX - RTC_AlarmStructure .AlarmTime .SubSeconds ;
947
+ /*
948
+ * The subsecond is the bit SS[14:0] of the ALARM SSR register (not ALARMxINR)
949
+ * to be converted in milliseconds
950
+ */
951
+ * subSeconds = (((0x7fff - RTC_AlarmStructure .AlarmTime .SubSeconds + 1 ) & 0x7fff ) * 1000 ) / fqce_apre ;
952
+ * subSeconds = * subSeconds % 1000 ; /* value in milliseconds [0-999] */
932
953
} else {
933
954
* subSeconds = ((predivSync - RTC_AlarmStructure .AlarmTime .SubSeconds ) * 1000 ) / (predivSync + 1 );
934
955
}
0 commit comments