@@ -76,6 +76,8 @@ static int16_t predivSync = -1;
76
76
static uint32_t prediv = RTC_AUTO_1_SECOND ;
77
77
#endif /* !STM32F1xx */
78
78
79
+ static uint32_t fqce_apre ;
80
+
79
81
static hourFormat_t initFormat = HOUR_FORMAT_12 ;
80
82
static binaryMode_t initMode = MODE_BINARY_NONE ;
81
83
@@ -371,6 +373,8 @@ static void RTC_computePrediv(int8_t *asynch, int16_t *synch)
371
373
Error_Handler ();
372
374
}
373
375
* synch = (int16_t )predivS ;
376
+
377
+ fqce_apre = clkVal / (* asynch + 1 );
374
378
}
375
379
#endif /* !STM32F1xx */
376
380
@@ -705,8 +709,13 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
705
709
#if defined(RTC_SSR_SS )
706
710
if (subSeconds != NULL ) {
707
711
if (initMode == MODE_BINARY_MIX ) {
708
- * subSeconds = UINT32_MAX - RTC_TimeStruct .SubSeconds ;
712
+ /* The subsecond is the free-running downcounter, to be converted in milliseconds */
713
+ * subSeconds = (((UINT32_MAX - RTC_TimeStruct .SubSeconds + 1 ) & UINT32_MAX )
714
+ * 1000 ) / fqce_apre ; /* give one more to compensate the 3.9ms uncertainty */
715
+ * subSeconds = * subSeconds % 1000 ; /* nb of milliseconds */
716
+ /* predivAsync is 0x7F with LSE clock source */
709
717
} else {
718
+ /* the subsecond register value is converted in millisec */
710
719
* subSeconds = ((predivSync - RTC_TimeStruct .SubSeconds ) * 1000 ) / (predivSync + 1 );
711
720
}
712
721
}
@@ -776,7 +785,7 @@ void RTC_GetDate(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *wday)
776
785
* @param hours: 0-12 or 0-23 depends on the hours mode.
777
786
* @param minutes: 0-59
778
787
* @param seconds: 0-59
779
- * @param subSeconds: 0-999
788
+ * @param subSeconds: 0-999 milliseconds
780
789
* @param period: HOUR_AM or HOUR_PM if in 12 hours mode else ignored.
781
790
* @param mask: configure alarm behavior using alarmMask_t combination.
782
791
* See AN4579 Table 5 for possible values.
@@ -811,7 +820,12 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
811
820
{
812
821
RTC_AlarmStructure .AlarmSubSecondMask = predivSync_bits << RTC_ALRMASSR_MASKSS_Pos ;
813
822
}
814
- RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - (subSeconds * (predivSync + 1 )) / 1000 ;
823
+ if (initMode == MODE_BINARY_MIX ) {
824
+ /* the subsecond is the millisecond to be converted in a subsecond downcounter value */
825
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - ((subSeconds * fqce_apre ) / 1000 + 1 );
826
+ } else {
827
+ RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - (subSeconds * (predivSync + 1 )) / 1000 ;
828
+ }
815
829
} else {
816
830
RTC_AlarmStructure .AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL ;
817
831
}
@@ -880,8 +894,8 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
880
894
/* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM A */
881
895
RTC_AlarmStructure .AlarmSubSecondMask = mask << RTC_ALRMASSR_MASKSS_Pos ;
882
896
}
883
- /* Special case for ALARM B configuration when using subsecond reg. in RTC Mix mode */
884
- RTC_AlarmStructure .AlarmTime .SubSeconds = subSeconds ;
897
+ /* The subsecond in ms is converted in ticks unit 1 tick is 1000 / fqce_apre */
898
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - ( subSeconds * fqce_apre ) / 1000 ;
885
899
}
886
900
887
901
#else
@@ -978,7 +992,11 @@ void RTC_GetAlarm(alarm_t name, uint8_t *day, uint8_t *hours, uint8_t *minutes,
978
992
#if defined(RTC_SSR_SS )
979
993
if (subSeconds != NULL ) {
980
994
if (initMode == MODE_BINARY_MIX ) {
981
- * subSeconds = UINT32_MAX - RTC_AlarmStructure .AlarmTime .SubSeconds ;
995
+ /*
996
+ * The subsecond is the bit SS[14:0] of the ALARM SSR register (not ALARMxINR)
997
+ * to be converted in milliseconds
998
+ */
999
+ * subSeconds = (((0x7fff - RTC_AlarmStructure .AlarmTime .SubSeconds + 1 ) & 0x7fff ) * 1000 ) / fqce_apre ;
982
1000
} else {
983
1001
* subSeconds = ((predivSync - RTC_AlarmStructure .AlarmTime .SubSeconds ) * 1000 ) / (predivSync + 1 );
984
1002
}
0 commit comments