@@ -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
@@ -373,6 +375,8 @@ static void RTC_computePrediv(int8_t *asynch, int16_t *synch)
373
375
Error_Handler ();
374
376
}
375
377
* synch = (int16_t )predivS ;
378
+
379
+ fqce_apre = clkVal / (* asynch + 1 );
376
380
}
377
381
#endif /* !STM32F1xx */
378
382
@@ -707,8 +711,13 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
707
711
#if defined(RTC_SSR_SS )
708
712
if (subSeconds != NULL ) {
709
713
if (initMode == MODE_BINARY_MIX ) {
710
- * subSeconds = UINT32_MAX - RTC_TimeStruct .SubSeconds ;
714
+ /* The subsecond is the free-running downcounter, to be converted in milliseconds */
715
+ * subSeconds = (((UINT32_MAX - RTC_TimeStruct .SubSeconds + 1 ) & UINT32_MAX )
716
+ * 1000 ) / fqce_apre ; /* give one more to compensate the 3.9ms uncertainty */
717
+ * subSeconds = * subSeconds % 1000 ; /* nb of milliseconds */
718
+ /* predivAsync is 0x7F with LSE clock source */
711
719
} else {
720
+ /* the subsecond register value is converted in millisec */
712
721
* subSeconds = ((predivSync - RTC_TimeStruct .SubSeconds ) * 1000 ) / (predivSync + 1 );
713
722
}
714
723
}
@@ -778,7 +787,7 @@ void RTC_GetDate(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *wday)
778
787
* @param hours: 0-12 or 0-23 depends on the hours mode.
779
788
* @param minutes: 0-59
780
789
* @param seconds: 0-59
781
- * @param subSeconds: 0-999
790
+ * @param subSeconds: 0-999 milliseconds
782
791
* @param period: HOUR_AM or HOUR_PM if in 12 hours mode else ignored.
783
792
* @param mask: configure alarm behavior using alarmMask_t combination.
784
793
* See AN4579 Table 5 for possible values.
@@ -813,7 +822,12 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
813
822
{
814
823
RTC_AlarmStructure .AlarmSubSecondMask = predivSync_bits << RTC_ALRMASSR_MASKSS_Pos ;
815
824
}
816
- RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - (subSeconds * (predivSync + 1 )) / 1000 ;
825
+ if (initMode == MODE_BINARY_MIX ) {
826
+ /* the subsecond is the millisecond to be converted in a subsecond downcounter value */
827
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - ((subSeconds * fqce_apre ) / 1000 + 1 );
828
+ } else {
829
+ RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - (subSeconds * (predivSync + 1 )) / 1000 ;
830
+ }
817
831
} else {
818
832
RTC_AlarmStructure .AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL ;
819
833
}
@@ -882,8 +896,8 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
882
896
/* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM A */
883
897
RTC_AlarmStructure .AlarmSubSecondMask = mask << RTC_ALRMASSR_MASKSS_Pos ;
884
898
}
885
- /* Special case for ALARM B configuration when using subsecond reg. in RTC Mix mode */
886
- RTC_AlarmStructure .AlarmTime .SubSeconds = subSeconds ;
899
+ /* The subsecond in ms is converted in ticks unit 1 tick is 1000 / fqce_apre */
900
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - ( subSeconds * fqce_apre ) / 1000 ;
887
901
}
888
902
889
903
#else
@@ -980,7 +994,11 @@ void RTC_GetAlarm(alarm_t name, uint8_t *day, uint8_t *hours, uint8_t *minutes,
980
994
#if defined(RTC_SSR_SS )
981
995
if (subSeconds != NULL ) {
982
996
if (initMode == MODE_BINARY_MIX ) {
983
- * subSeconds = UINT32_MAX - RTC_AlarmStructure .AlarmTime .SubSeconds ;
997
+ /*
998
+ * The subsecond is the bit SS[14:0] of the ALARM SSR register (not ALARMxINR)
999
+ * to be converted in milliseconds
1000
+ */
1001
+ * subSeconds = (((0x7fff - RTC_AlarmStructure .AlarmTime .SubSeconds + 1 ) & 0x7fff ) * 1000 ) / fqce_apre ;
984
1002
} else {
985
1003
* subSeconds = ((predivSync - RTC_AlarmStructure .AlarmTime .SubSeconds ) * 1000 ) / (predivSync + 1 );
986
1004
}
0 commit comments