Skip to content

Commit 647cd28

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

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/rtc.c

Lines changed: 42 additions & 7 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);
@@ -828,14 +828,16 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
828828
#if defined(RTC_SSR_SS)
829829
if (subSeconds < 1000) {
830830
#ifdef RTC_ALARM_B
831-
if (name == ALARM_B) {
831+
if (name == ALARM_B)
832+
{
832833
RTC_AlarmStructure.AlarmSubSecondMask = predivSync_bits << RTC_ALRMBSSR_MASKSS_Pos;
833834
} else
834835
#endif
835836
{
836837
RTC_AlarmStructure.AlarmSubSecondMask = predivSync_bits << RTC_ALRMASSR_MASKSS_Pos;
837838
}
838-
if (initMode == MODE_BINARY_MIX) {
839+
/* in MIX or BCD Mode, the subsecond param is a nb of milliseconds */
840+
if ((initMode == MODE_BINARY_MIX) || (initMode == MODE_BINARY_NONE)) {
839841
/* the subsecond is the millisecond to be converted in a subsecond downcounter value */
840842
RTC_AlarmStructure.AlarmTime.SubSeconds = UINT32_MAX - ((subSeconds * fqce_apre) / 1000 + 1);
841843
} else {
@@ -886,7 +888,7 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
886888
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
887889
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
888890
#if defined(RTC_ICSR_BIN)
889-
} else if (RtcHandle.Init.BinMode != RTC_BINARY_NONE) {
891+
} else if ((initMode == MODE_BINARY_MIX) || (initMode == MODE_BINARY_ONLY)) {
890892
/* We have an SubSecond alarm to set in RTC_BINARY_MIX or RTC_BINARY_ONLY mode */
891893
#else
892894
} else {
@@ -913,6 +915,37 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
913915
RTC_AlarmStructure.AlarmTime.SubSeconds = UINT32_MAX - (subSeconds * fqce_apre) / 1000;
914916
}
915917

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

926961
/**
@@ -1006,7 +1041,7 @@ void RTC_GetAlarm(alarm_t name, uint8_t *day, uint8_t *hours, uint8_t *minutes,
10061041
}
10071042
#if defined(RTC_SSR_SS)
10081043
if (subSeconds != NULL) {
1009-
if (initMode == MODE_BINARY_MIX) {
1044+
if ((initMode == MODE_BINARY_MIX) || (initMode == MODE_BINARY_ONLY)) {
10101045
/*
10111046
* The subsecond is the bit SS[14:0] of the ALARM SSR register (not ALARMxINR)
10121047
* to be converted in milliseconds

0 commit comments

Comments
 (0)