Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rtc mix sketch #94

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions examples/mixRTCAlarm/mixRTCAlarm.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
mode Mix RTC alarm

This sketch shows how to configure the alarm A & B of the RTC in MIX mode

Creation 12 Dec 2017
by Wi6Labs
Modified 03 Jul 2020
by Frederic Pillon for STMicroelectronics
Modified 03 Jul 2023
by Francois Ramu for STMicroelectronics

This example code is in the public domain.

https://github.com/stm32duino/STM32RTC
*/

#include <STM32RTC.h>

/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();

/* Change these values to set the current initial time */
const byte seconds = 06;
const byte minutes = 22;
const byte hours = 16;

/* Change these values to set the current initial date */
const byte day = 25;
const byte month = 6;
const byte year = 23;

uint32_t timeout;

void setup()
{
Serial.begin(115200);

// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
rtc.setClockSource(STM32RTC::LSE_CLOCK);

/* Configure the MIX mode */
rtc.setBinaryMode(STM32RTC::MODE_MIX);

rtc.begin(true, STM32RTC::HOUR_24);

rtc.setTime(hours, minutes, seconds);
rtc.setDate(day, month, year);

/* wait for a while */
delay(200);

Serial.printf("Start at %02d:%02d:%02d.%03d\r\n",
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds(), rtc.getSubSeconds());

/* Attach the callback function before enabling Interrupt */
rtc.attachInterrupt(alarmAMatch);

/* Program the AlarmA in a 12 seconds */
rtc.setAlarmDay(day);
rtc.setAlarmTime(hours, minutes, seconds + 12);
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
Serial.printf("Set Alarm A in 12s (at %02d:%02d:%02d)\r\n",
rtc.getAlarmHours(), rtc.getAlarmMinutes(), rtc.getAlarmSeconds());

#ifdef RTC_ALARM_B
/* Program ALARM B in 400ms ms from now (keep timeout < 1000ms) */
timeout = rtc.getSubSeconds() + 400;

rtc.attachInterrupt(alarmBMatch, STM32RTC::ALARM_B);
rtc.setAlarmSubSeconds(timeout, STM32RTC::ALARM_B);
rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_B);
Serial.printf("Set Alarm B (in %d ms) at %d ms\r\n", 400,
rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
#endif

}

void loop()
{

}

void alarmAMatch(void *data)
{
UNUSED(data);
Serial.printf("Alarm A Match at %02d:%02d:%02d\r\n",
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds());
}

void alarmBMatch(void *data)
{
UNUSED(data);
Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds());
}


39 changes: 38 additions & 1 deletion src/STM32RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void STM32RTC::begin(bool resetTime, Hour_Format format)

_format = format;
reinit = RTC_init((format == HOUR_12) ? HOUR_FORMAT_12 : HOUR_FORMAT_24,
(_mode == MODE_MIX) ? ::MODE_BINARY_MIX : ((_mode == MODE_BIN) ? ::MODE_BINARY_ONLY : ::MODE_BINARY_NONE),
(_clockSource == LSE_CLOCK) ? ::LSE_CLOCK :
(_clockSource == HSE_CLOCK) ? ::HSE_CLOCK : ::LSI_CLOCK
, resetTime);
Expand Down Expand Up @@ -131,6 +132,26 @@ void STM32RTC::setClockSource(Source_Clock source)
}
}

/**
* @brief get the Binary Mode.
* @retval mode: MODE_BCD, MODE_BIN or MODE_MIX
*/
STM32RTC::Binary_Mode STM32RTC::getBinaryMode(void)
{
return _mode;
}

/**
* @brief set the Binary Mode. By default MODE_BCD is selected. This
* method must be called before begin().
* @param mode: the RTC mode: MODE_BCD, MODE_BIN or MODE_MIX
* @retval None
*/
void STM32RTC::setBinaryMode(Binary_Mode mode)
{
_mode = mode;
}

#if defined(STM32F1xx)
/**
* @brief get user asynchronous prescaler value for the current clock source.
Expand Down Expand Up @@ -216,6 +237,21 @@ void STM32RTC::enableAlarm(Alarm_Match match, Alarm name)
RTC_StopAlarm(::ALARM_A);
}
break;
case MATCH_SUBSEC:
/* force _alarmday to 0 to go to the right alarm config in MIX mode */
#ifdef RTC_ALARM_B
if (name == ALARM_B) {
RTC_StartAlarm(::ALARM_B, 0, 0, 0, 0,
_alarmBSubSeconds, (_alarmBPeriod == AM) ? HOUR_AM : HOUR_PM,
static_cast<uint8_t>(31UL));
} else
#endif
{
RTC_StartAlarm(::ALARM_A, 0, 0, 0, 0,
_alarmSubSeconds, (_alarmPeriod == AM) ? HOUR_AM : HOUR_PM,
static_cast<uint8_t>(31UL));
}
break;
case MATCH_YYMMDDHHMMSS://kept for compatibility
case MATCH_MMDDHHMMSS: //kept for compatibility
case MATCH_DHHMMSS:
Expand Down Expand Up @@ -611,7 +647,7 @@ uint8_t STM32RTC::getAlarmYear(void)

/**
* @brief set RTC subseconds.
* @param subseconds: 0-999
* @param subseconds: 0-999 milliseconds
* @retval none
*/
void STM32RTC::setSubSeconds(uint32_t subSeconds)
Expand Down Expand Up @@ -1246,6 +1282,7 @@ bool STM32RTC::isAlarmEnabled(Alarm name)
void STM32RTC::syncTime(void)
{
hourAM_PM_t p = HOUR_AM;

RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p);
_hoursPeriod = (p == HOUR_AM) ? AM : PM;
}
Expand Down
11 changes: 11 additions & 0 deletions src/STM32RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ class STM32RTC {
PM = HOUR_PM
};

enum Binary_Mode : uint8_t {
MODE_BCD = MODE_BINARY_NONE, /* not used */
MODE_BIN = MODE_BINARY_ONLY,
MODE_MIX = MODE_BINARY_MIX
};

enum Alarm_Match : uint8_t {
MATCH_OFF = OFF_MSK, // Never
MATCH_SUBSEC = SUBSEC_MSK, // Every Subsecond
MATCH_SS = SS_MSK, // Every Minute
MATCH_MMSS = SS_MSK | MM_MSK, // Every Hour
MATCH_HHMMSS = SS_MSK | MM_MSK | HH_MSK, // Every Day
Expand Down Expand Up @@ -128,6 +135,9 @@ class STM32RTC {
Source_Clock getClockSource(void);
void setClockSource(Source_Clock source);

Binary_Mode getBinaryMode(void);
void setBinaryMode(Binary_Mode mode);

void enableAlarm(Alarm_Match match, Alarm name = ALARM_A);
void disableAlarm(Alarm name = ALARM_A);

Expand Down Expand Up @@ -237,6 +247,7 @@ class STM32RTC {
static bool _timeSet;

Hour_Format _format;
Binary_Mode _mode;
AM_PM _hoursPeriod;
uint8_t _hours;
uint8_t _minutes;
Expand Down
Loading
Loading