Skip to content

Commit 9e2f974

Browse files
FRASTMfpistm
authored andcommitted
feat: RTC Time and Alarm configuration in BCD and BIN modes
define the Getime and startAlarm function when the RTC is configured in BCD (MODE_BINARY_NONE) or BIN (MODE_BINARY_ONLY). In MODE_BINARY_ONLY, Time and Date are not used, only subsecond Signed-off-by: Francois Ramu <francois.ramu@st.com>
1 parent 88f7733 commit 9e2f974

File tree

1 file changed

+55
-45
lines changed

1 file changed

+55
-45
lines changed

src/rtc.c

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ void HAL_RTC_MspDeInit(RTC_HandleTypeDef *rtcHandle)
151151
* @param None
152152
* @retval pointer to RTC_HandleTypeDef
153153
*/
154-
RTC_HandleTypeDef *RTC_GetHandle(void) {
154+
RTC_HandleTypeDef *RTC_GetHandle(void)
155+
{
155156
return &RtcHandle;
156157
}
157158

@@ -707,7 +708,7 @@ void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSe
707708
*/
708709
void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, hourAM_PM_t *period)
709710
{
710-
RTC_TimeTypeDef RTC_TimeStruct;
711+
RTC_TimeTypeDef RTC_TimeStruct = {0}; /* in BIN mode, only the subsecond is used */
711712

712713
if ((hours != NULL) && (minutes != NULL) && (seconds != NULL)) {
713714
#if defined(STM32F1xx)
@@ -730,15 +731,21 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
730731
}
731732
#if defined(RTC_SSR_SS)
732733
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+
*/
733738
if (initMode == MODE_BINARY_MIX) {
734-
/* The subsecond is the free-running downcounter, to be converted in milliseconds */
735739
*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;
739745
} 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;
742749
}
743750
}
744751
#else
@@ -789,7 +796,7 @@ void RTC_SetDate(uint8_t year, uint8_t month, uint8_t day, uint8_t wday)
789796
*/
790797
void RTC_GetDate(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *wday)
791798
{
792-
RTC_DateTypeDef RTC_DateStruct;
799+
RTC_DateTypeDef RTC_DateStruct = {0}; /* in BIN mode, the date is not used */
793800

794801
if ((year != NULL) && (month != NULL) && (day != NULL) && (wday != NULL)) {
795802
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)
815822
*/
816823
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)
817824
{
825+
#if !defined(RTC_SSR_SS)
826+
UNUSED(subSeconds);
827+
#endif
818828
RTC_AlarmTypeDef RTC_AlarmStructure;
819829

820830
/* 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
842852
{
843853
RTC_AlarmStructure.AlarmSubSecondMask = predivSync_bits << RTC_ALRMASSR_MASKSS_Pos;
844854
}
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)) {
846860
/* 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;
848862
} else {
849-
RTC_AlarmStructure.AlarmTime.SubSeconds = predivSync - (subSeconds * (predivSync + 1)) / 1000;
863+
RTC_AlarmStructure.AlarmTime.SubSeconds = predivSync - (subSeconds * (predivSync + 1)) / 1000 + 1;
850864
}
851865
} else {
852866
RTC_AlarmStructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
853867
}
854-
#else
855-
UNUSED(subSeconds);
856868
#endif /* RTC_SSR_SS */
857869
if (period == HOUR_PM) {
858870
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
882894
}
883895
}
884896
#else
885-
UNUSED(subSeconds);
886897
UNUSED(period);
887898
UNUSED(day);
888899
UNUSED(mask);
@@ -892,42 +903,40 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
892903
HAL_RTC_SetAlarm_IT(&RtcHandle, &RTC_AlarmStructure, RTC_FORMAT_BIN);
893904
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
894905
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+
}
901907
#if defined(RTC_SSR_SS)
902-
{
908+
else {
909+
/* SS have to be managed*/
903910
#if defined(RTC_ALRMASSR_SSCLR)
904-
RTC_AlarmStructure.BinaryAutoClr = RTC_ALARMSUBSECONDBIN_AUTOCLR_NO;
911+
RTC_AlarmStructure.BinaryAutoClr = RTC_ALARMSUBSECONDBIN_AUTOCLR_NO;
905912
#endif /* RTC_ALRMASSR_SSCLR */
906-
RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_ALL;
907-
913+
RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_ALL;
908914
#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
914919
#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 */
919927
/* 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;
921933
}
922-
923-
#else
924-
UNUSED(subSeconds);
925-
#endif /* RTC_SSR_SS */
926934
/* Set RTC_Alarm */
927935
HAL_RTC_SetAlarm_IT(&RtcHandle, &RTC_AlarmStructure, RTC_FORMAT_BIN);
928936
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
929937
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
930938
}
939+
#endif /* RTC_SSR_SS */
931940
}
932941

933942
/**
@@ -1013,14 +1022,15 @@ void RTC_GetAlarm(alarm_t name, uint8_t *day, uint8_t *hours, uint8_t *minutes,
10131022
}
10141023
#if defined(RTC_SSR_SS)
10151024
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 */
10211031
*subSeconds = (((0x7fff - RTC_AlarmStructure.AlarmTime.SubSeconds + 1) & 0x7fff) * 1000) / fqce_apre;
10221032
} else {
1023-
*subSeconds = ((predivSync - RTC_AlarmStructure.AlarmTime.SubSeconds) * 1000) / (predivSync + 1);
1033+
*subSeconds = (((predivSync - RTC_AlarmStructure.AlarmTime.SubSeconds + 1) & predivSync) * 1000) / (predivSync + 1);
10241034
}
10251035
}
10261036
#else

0 commit comments

Comments
 (0)