Skip to content

Commit 6b94d3c

Browse files
committed
RTC Time and Alarm configuration in BCD mode (BINARY_NONE)
Signed-off-by: Francois Ramu <[email protected]>
1 parent 6d14e5e commit 6b94d3c

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

src/rtc.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSe
700700
*/
701701
void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, hourAM_PM_t *period)
702702
{
703-
RTC_TimeTypeDef RTC_TimeStruct;
703+
RTC_TimeTypeDef RTC_TimeStruct = {0}; /* in BIN mode, only the subseco is used */
704704

705705
if ((hours != NULL) && (minutes != NULL) && (seconds != NULL)) {
706706
#if defined(STM32F1xx)
@@ -723,7 +723,7 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
723723
}
724724
#if defined(RTC_SSR_SS)
725725
if (subSeconds != NULL) {
726-
if (initMode == MODE_BINARY_MIX) {
726+
if ((initMode == MODE_BINARY_MIX) || (initMode == MODE_BINARY_ONLY)) {
727727
/* The subsecond is the free-running downcounter, to be converted in milliseconds */
728728
*subSeconds = (((UINT32_MAX - RTC_TimeStruct.SubSeconds + 1) & UINT32_MAX)
729729
* 1000) / fqce_apre; /* give one more to compensate the 3.9ms uncertainty */
@@ -782,7 +782,7 @@ void RTC_SetDate(uint8_t year, uint8_t month, uint8_t day, uint8_t wday)
782782
*/
783783
void RTC_GetDate(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *wday)
784784
{
785-
RTC_DateTypeDef RTC_DateStruct;
785+
RTC_DateTypeDef RTC_DateStruct = {0}; /* in BIN mode, the date is not used */
786786

787787
if ((year != NULL) && (month != NULL) && (day != NULL) && (wday != NULL)) {
788788
HAL_RTC_GetDate(&RtcHandle, &RTC_DateStruct, RTC_FORMAT_BIN);
@@ -835,7 +835,8 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
835835
{
836836
RTC_AlarmStructure.AlarmSubSecondMask = predivSync_bits << RTC_ALRMASSR_MASKSS_Pos;
837837
}
838-
if (initMode == MODE_BINARY_MIX) {
838+
/* in MIX or BCD Mode, the subsecond param is a nb of milliseconds */
839+
if ((initMode == MODE_BINARY_MIX) || (initMode == MODE_BINARY_NONE)) {
839840
/* the subsecond is the millisecond to be converted in a subsecond downcounter value */
840841
RTC_AlarmStructure.AlarmTime.SubSeconds = UINT32_MAX - ((subSeconds * fqce_apre) / 1000 + 1);
841842
} else {
@@ -886,7 +887,7 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
886887
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
887888
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
888889
#if defined(RTC_ICSR_BIN)
889-
} else if (RtcHandle.Init.BinMode != RTC_BINARY_NONE) {
890+
} else if ((initMode == MODE_BINARY_MIX) || (initMode == MODE_BINARY_ONLY)) {
890891
/* We have an SubSecond alarm to set in RTC_BINARY_MIX or RTC_BINARY_ONLY mode */
891892
#else
892893
} else {
@@ -913,6 +914,36 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
913914
RTC_AlarmStructure.AlarmTime.SubSeconds = UINT32_MAX - (subSeconds * fqce_apre) / 1000;
914915
}
915916

917+
#else
918+
UNUSED(subSeconds);
919+
#endif /* RTC_SSR_SS */
920+
/* Set RTC_Alarm */
921+
HAL_RTC_SetAlarm_IT(&RtcHandle, &RTC_AlarmStructure, RTC_FORMAT_BIN);
922+
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
923+
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
924+
#if defined(RTC_ICSR_BIN)
925+
} else {
926+
/* We have an SubSecond alarm to set in BCD (RTC_BINARY_NONE) mode */
927+
#if defined(RTC_SSR_SS)
928+
{
929+
#if defined(RTC_ALRMASSR_SSCLR)
930+
RTC_AlarmStructure.BinaryAutoClr = RTC_ALARMSUBSECONDBIN_AUTOCLR_NO;
931+
#endif /* RTC_ALRMASSR_SSCLR */
932+
RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_ALL;
933+
934+
#ifdef RTC_ALARM_B
935+
if (name == ALARM_B) {
936+
/* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM B */
937+
RTC_AlarmStructure.AlarmSubSecondMask = mask << RTC_ALRMBSSR_MASKSS_Pos;
938+
} else
939+
#endif
940+
{
941+
/* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM A */
942+
RTC_AlarmStructure.AlarmSubSecondMask = mask << RTC_ALRMASSR_MASKSS_Pos;
943+
}
944+
RTC_AlarmStructure.AlarmTime.SubSeconds = subSeconds * fqce_apre / 1000;
945+
}
946+
916947
#else
917948
UNUSED(subSeconds);
918949
#endif /* RTC_SSR_SS */
@@ -921,6 +952,8 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
921952
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
922953
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
923954
}
955+
#endif /* RTC_ICSR_BIN */
956+
924957
}
925958

926959
/**
@@ -1006,7 +1039,7 @@ void RTC_GetAlarm(alarm_t name, uint8_t *day, uint8_t *hours, uint8_t *minutes,
10061039
}
10071040
#if defined(RTC_SSR_SS)
10081041
if (subSeconds != NULL) {
1009-
if (initMode == MODE_BINARY_MIX) {
1042+
if ((initMode == MODE_BINARY_MIX) || (initMode == MODE_BINARY_ONLY)) {
10101043
/*
10111044
* The subsecond is the bit SS[14:0] of the ALARM SSR register (not ALARMxINR)
10121045
* to be converted in milliseconds

0 commit comments

Comments
 (0)