@@ -151,7 +151,8 @@ void HAL_RTC_MspDeInit(RTC_HandleTypeDef *rtcHandle)
151
151
* @param None
152
152
* @retval pointer to RTC_HandleTypeDef
153
153
*/
154
- RTC_HandleTypeDef * RTC_GetHandle (void ) {
154
+ RTC_HandleTypeDef * RTC_GetHandle (void )
155
+ {
155
156
return & RtcHandle ;
156
157
}
157
158
@@ -707,7 +708,7 @@ void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSe
707
708
*/
708
709
void RTC_GetTime (uint8_t * hours , uint8_t * minutes , uint8_t * seconds , uint32_t * subSeconds , hourAM_PM_t * period )
709
710
{
710
- RTC_TimeTypeDef RTC_TimeStruct ;
711
+ RTC_TimeTypeDef RTC_TimeStruct = { 0 }; /* in BIN mode, only the subsecond is used */
711
712
712
713
if ((hours != NULL ) && (minutes != NULL ) && (seconds != NULL )) {
713
714
#if defined(STM32F1xx )
@@ -730,15 +731,21 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
730
731
}
731
732
#if defined(RTC_SSR_SS )
732
733
if (subSeconds != NULL ) {
734
+ /*
735
+ * The subsecond is the free-running downcounter, to be converted in milliseconds.
736
+ * Give one more to compensate the fqce_apre uncertainty
737
+ */
733
738
if (initMode == MODE_BINARY_MIX ) {
734
- /* The subsecond is the free-running downcounter, to be converted in milliseconds */
735
739
* subSeconds = (((UINT32_MAX - RTC_TimeStruct .SubSeconds + 1 ) & UINT32_MAX )
736
- * 1000 ) / fqce_apre ; /* give one more to compensate the 3.9ms uncertainty */
737
- * subSeconds = * subSeconds % 1000 ; /* nb of milliseconds */
738
- /* predivAsync is 0x7F with LSE clock source */
740
+ * 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 )
744
+ * 1000 ) / fqce_apre ;
739
745
} else {
740
- /* the subsecond register value is converted in millisec */
741
- * subSeconds = ((predivSync - RTC_TimeStruct .SubSeconds ) * 1000 ) / (predivSync + 1 );
746
+ /* the subsecond register value is converted in millisec on 32bit */
747
+ * subSeconds = (((predivSync - RTC_TimeStruct .SubSeconds + 1 ) & predivSync )
748
+ * 1000 ) / fqce_apre ;
742
749
}
743
750
}
744
751
#else
@@ -789,7 +796,7 @@ void RTC_SetDate(uint8_t year, uint8_t month, uint8_t day, uint8_t wday)
789
796
*/
790
797
void RTC_GetDate (uint8_t * year , uint8_t * month , uint8_t * day , uint8_t * wday )
791
798
{
792
- RTC_DateTypeDef RTC_DateStruct ;
799
+ RTC_DateTypeDef RTC_DateStruct = { 0 }; /* in BIN mode, the date is not used */
793
800
794
801
if ((year != NULL ) && (month != NULL ) && (day != NULL ) && (wday != NULL )) {
795
802
HAL_RTC_GetDate (& RtcHandle , & RTC_DateStruct , RTC_FORMAT_BIN );
@@ -815,6 +822,9 @@ void RTC_GetDate(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *wday)
815
822
*/
816
823
void RTC_StartAlarm (alarm_t name , uint8_t day , uint8_t hours , uint8_t minutes , uint8_t seconds , uint32_t subSeconds , hourAM_PM_t period , uint8_t mask )
817
824
{
825
+ #if !defined(RTC_SSR_SS )
826
+ UNUSED (subSeconds );
827
+ #endif
818
828
RTC_AlarmTypeDef RTC_AlarmStructure ;
819
829
820
830
/* Ignore time AM PM configuration if in 24 hours format */
@@ -842,17 +852,19 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
842
852
{
843
853
RTC_AlarmStructure .AlarmSubSecondMask = predivSync_bits << RTC_ALRMASSR_MASKSS_Pos ;
844
854
}
845
- if (initMode == MODE_BINARY_MIX ) {
855
+ /*
856
+ * The subsecond param is a nb of milliseconds to be converted in a subsecond
857
+ * downcounter value and to be comapred to the SubSecond register
858
+ */
859
+ if ((initMode == MODE_BINARY_MIX ) || (initMode == MODE_BINARY_NONE )) {
846
860
/* the subsecond is the millisecond to be converted in a subsecond downcounter value */
847
- RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - (( subSeconds * fqce_apre ) / 1000 + 1 ) ;
861
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - (subSeconds * ( predivSync + 1 )) / 1000 + 1 ;
848
862
} else {
849
- RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - (subSeconds * (predivSync + 1 )) / 1000 ;
863
+ RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - (subSeconds * (predivSync + 1 )) / 1000 + 1 ;
850
864
}
851
865
} else {
852
866
RTC_AlarmStructure .AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL ;
853
867
}
854
- #else
855
- UNUSED (subSeconds );
856
868
#endif /* RTC_SSR_SS */
857
869
if (period == HOUR_PM ) {
858
870
RTC_AlarmStructure .AlarmTime .TimeFormat = RTC_HOURFORMAT12_PM ;
@@ -882,7 +894,6 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
882
894
}
883
895
}
884
896
#else
885
- UNUSED (subSeconds );
886
897
UNUSED (period );
887
898
UNUSED (day );
888
899
UNUSED (mask );
@@ -892,42 +903,40 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
892
903
HAL_RTC_SetAlarm_IT (& RtcHandle , & RTC_AlarmStructure , RTC_FORMAT_BIN );
893
904
HAL_NVIC_SetPriority (RTC_Alarm_IRQn , RTC_IRQ_PRIO , RTC_IRQ_SUBPRIO );
894
905
HAL_NVIC_EnableIRQ (RTC_Alarm_IRQn );
895
- #if defined(RTC_ICSR_BIN )
896
- } else if (RtcHandle .Init .BinMode != RTC_BINARY_NONE ) {
897
- /* We have an SubSecond alarm to set in RTC_BINARY_MIX or RTC_BINARY_ONLY mode */
898
- #else
899
- } else {
900
- #endif /* RTC_ICSR_BIN */
906
+ }
901
907
#if defined(RTC_SSR_SS )
902
- {
908
+ else {
909
+ /* SS have to be managed*/
903
910
#if defined(RTC_ALRMASSR_SSCLR )
904
- RTC_AlarmStructure .BinaryAutoClr = RTC_ALARMSUBSECONDBIN_AUTOCLR_NO ;
911
+ RTC_AlarmStructure .BinaryAutoClr = RTC_ALARMSUBSECONDBIN_AUTOCLR_NO ;
905
912
#endif /* RTC_ALRMASSR_SSCLR */
906
- RTC_AlarmStructure .AlarmMask = RTC_ALARMMASK_ALL ;
907
-
913
+ RTC_AlarmStructure .AlarmMask = RTC_ALARMMASK_ALL ;
908
914
#ifdef RTC_ALARM_B
909
- if (name == ALARM_B )
910
- {
911
- /* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM B */
912
- RTC_AlarmStructure .AlarmSubSecondMask = mask << RTC_ALRMBSSR_MASKSS_Pos ;
913
- } else
915
+ if (name == ALARM_B ) {
916
+ /* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM B */
917
+ RTC_AlarmStructure .AlarmSubSecondMask = mask << RTC_ALRMBSSR_MASKSS_Pos ;
918
+ } else
914
919
#endif
915
- {
916
- /* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM A */
917
- RTC_AlarmStructure .AlarmSubSecondMask = mask << RTC_ALRMASSR_MASKSS_Pos ;
918
- }
920
+ {
921
+ /* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM A */
922
+ RTC_AlarmStructure .AlarmSubSecondMask = mask << RTC_ALRMASSR_MASKSS_Pos ;
923
+ }
924
+ #if defined(RTC_ICSR_BIN )
925
+ if ((initMode == MODE_BINARY_MIX ) || (initMode == MODE_BINARY_ONLY )) {
926
+ /* We have an SubSecond alarm to set in RTC_BINARY_MIX or RTC_BINARY_ONLY mode */
919
927
/* The subsecond in ms is converted in ticks unit 1 tick is 1000 / fqce_apre */
920
- RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - (subSeconds * fqce_apre ) / 1000 ;
928
+ RTC_AlarmStructure .AlarmTime .SubSeconds = UINT32_MAX - (subSeconds * (predivSync + 1 )) / 1000 ;
929
+ } else
930
+ #endif /* RTC_ICSR_BIN */
931
+ {
932
+ RTC_AlarmStructure .AlarmTime .SubSeconds = predivSync - subSeconds * (predivSync + 1 ) / 1000 ;
921
933
}
922
-
923
- #else
924
- UNUSED (subSeconds );
925
- #endif /* RTC_SSR_SS */
926
934
/* Set RTC_Alarm */
927
935
HAL_RTC_SetAlarm_IT (& RtcHandle , & RTC_AlarmStructure , RTC_FORMAT_BIN );
928
936
HAL_NVIC_SetPriority (RTC_Alarm_IRQn , RTC_IRQ_PRIO , RTC_IRQ_SUBPRIO );
929
937
HAL_NVIC_EnableIRQ (RTC_Alarm_IRQn );
930
938
}
939
+ #endif /* RTC_SSR_SS */
931
940
}
932
941
933
942
/**
@@ -1013,14 +1022,15 @@ void RTC_GetAlarm(alarm_t name, uint8_t *day, uint8_t *hours, uint8_t *minutes,
1013
1022
}
1014
1023
#if defined(RTC_SSR_SS )
1015
1024
if (subSeconds != NULL ) {
1016
- if (initMode == MODE_BINARY_MIX ) {
1017
- /*
1018
- * The subsecond is the bit SS[14:0] of the ALARM SSR register (not ALARMxINR)
1019
- * to be converted in milliseconds
1020
- */
1025
+ /*
1026
+ * The subsecond is the bit SS[14:0] of the ALARM SSR register (not ALARMxINR)
1027
+ * to be converted in milliseconds
1028
+ */
1029
+ if ((initMode == MODE_BINARY_MIX ) || (initMode == MODE_BINARY_ONLY )) {
1030
+ /* read the ALARM SSR register on SS[14:0] bits --> 0x7FFF */
1021
1031
* subSeconds = (((0x7fff - RTC_AlarmStructure .AlarmTime .SubSeconds + 1 ) & 0x7fff ) * 1000 ) / fqce_apre ;
1022
1032
} else {
1023
- * subSeconds = ((predivSync - RTC_AlarmStructure .AlarmTime .SubSeconds ) * 1000 ) / (predivSync + 1 );
1033
+ * subSeconds = ((( predivSync - RTC_AlarmStructure .AlarmTime .SubSeconds + 1 ) & predivSync ) * 1000 ) / (predivSync + 1 );
1024
1034
}
1025
1035
}
1026
1036
#else
0 commit comments