Skip to content

Add support for both Alarm_A and Alarm_B #75

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

Closed
wants to merge 2 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
56 changes: 56 additions & 0 deletions examples/RTC_Alarm_A_B/RTC_Alarm_A_B.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
SimpleRTC

This sketch shows how to configure the RTC
for two separate interrupts

This example code is in the public domain.

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

#define SERIAL_ENABLED
#ifdef __AVR__
#define TheSerial Serial
#else
#include "HardwareSerial.h"
// RX TX
HardwareSerial Serial2(PA3, PA2);
#define TheSerial Serial2
#endif

#include <STM32RTC.h>

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

const byte alarm_a_second = 30;
const byte alarm_b_second = 12;

void setup()
{
TheSerial.begin(38400);

rtc.begin();
rtc.setAlarmType(RTC_ALARM_A);
rtc.attachInterrupt(alarmACallback, NULL);
rtc.setAlarmEpoch(rtc.getEpoch() + alarm_a_second);

rtc.setAlarmType(RTC_ALARM_B);
rtc.attachInterrupt(alarmBCallback, NULL);
rtc.setAlarmEpoch(rtc.getEpoch() + alarm_b_second);
}

void loop()
{
}

void alarmACallback(void *data)
{
TheSerial.println("Alarm A called");
}

void alarmBCallback(void *data)
{
TheSerial.println("Alarm A called");
}
34 changes: 28 additions & 6 deletions src/STM32RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ void STM32RTC::begin(bool resetTime, Hour_Format format)
{
bool reinit;

_alarmType = RTC_ALARM_A;

if (resetTime == true) {
_timeSet = false;
}
Expand Down Expand Up @@ -198,7 +200,8 @@ void STM32RTC::enableAlarm(Alarm_Match match)
case MATCH_HHMMSS:
case MATCH_MMSS:
case MATCH_SS:
RTC_StartAlarm(_alarmDay, _alarmHours, _alarmMinutes, _alarmSeconds,
v2_RTC_StartAlarm(_alarmType,
_alarmDay, _alarmHours, _alarmMinutes, _alarmSeconds,
_alarmSubSeconds, (_alarmPeriod == AM) ? HOUR_AM : HOUR_PM,
static_cast<uint8_t>(_alarmMatch));
break;
Expand All @@ -213,7 +216,7 @@ void STM32RTC::enableAlarm(Alarm_Match match)
*/
void STM32RTC::disableAlarm(void)
{
RTC_StopAlarm();
v2_RTC_StopAlarm(_alarmType);
}

/**
Expand All @@ -223,7 +226,7 @@ void STM32RTC::disableAlarm(void)
*/
void STM32RTC::attachInterrupt(voidFuncPtr callback, void *data)
{
attachAlarmCallback(callback, data);
v2_attachAlarmCallback(_alarmType, callback, data);
}

/**
Expand All @@ -232,7 +235,7 @@ void STM32RTC::attachInterrupt(voidFuncPtr callback, void *data)
*/
void STM32RTC::detachInterrupt(void)
{
detachAlarmCallback();
v2_detachAlarmCallback(_alarmType);
}

#ifdef ONESECOND_IRQn
Expand Down Expand Up @@ -266,6 +269,15 @@ void STM32RTC::standbyMode(void)
* Get Functions
*/

/**
* @brief get alarmType month.
* @retval return the value of alarmType.
*/
uint32_t STM32RTC::getAlarmType(void)
{
return _alarmType;
}

/**
* @brief get RTC subseconds.
* @retval return the current subseconds from the RTC.
Expand Down Expand Up @@ -487,6 +499,15 @@ uint8_t STM32RTC::getAlarmYear(void)
* Set Functions
*/

/**
* @brief set RTC Alarm type (A, B) we currently operating on.
* @param alarmType: RTC_ALARM_A, RTC_ALARM_B
* @retval none
*/
void STM32RTC::setAlarmType(uint32_t alarmType) {
_alarmType = alarmType;
}

/**
* @brief set RTC subseconds.
* @param subseconds: 0-999
Expand Down Expand Up @@ -954,7 +975,7 @@ void STM32RTC::configForLowPower(Source_Clock source)
setDate(weekDay, day, month, years);
setAlarmTime(alarmHours, alarmMinutes, alarmSeconds, alarmPeriod);
setAlarmDay(alarmDay);
if (RTC_IsAlarmSet()) {
if (v2_RTC_IsAlarmSet(_alarmType)) {
enableAlarm(alarmMatch);
}
}
Expand Down Expand Up @@ -994,7 +1015,8 @@ void STM32RTC::syncAlarmTime(void)
{
hourAM_PM_t p = HOUR_AM;
uint8_t match;
RTC_GetAlarm(&_alarmDay, &_alarmHours, &_alarmMinutes, &_alarmSeconds,
v2_RTC_GetAlarm(_alarmType,
&_alarmDay, &_alarmHours, &_alarmMinutes, &_alarmSeconds,
&_alarmSubSeconds, &p, &match);
_alarmPeriod = (p == HOUR_AM) ? AM : PM;
switch (static_cast<Alarm_Match>(match)) {
Expand Down
4 changes: 4 additions & 0 deletions src/STM32RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class STM32RTC {
void standbyMode();

/* Get Functions */
uint32_t getAlarmType(void);

uint32_t getSubSeconds(void);
uint8_t getSeconds(void);
Expand All @@ -160,6 +161,7 @@ class STM32RTC {
uint8_t getAlarmYear();

/* Set Functions */
void setAlarmType(uint32_t alarmType);

void setSubSeconds(uint32_t subSeconds);
void setSeconds(uint8_t seconds);
Expand Down Expand Up @@ -226,6 +228,8 @@ class STM32RTC {

static bool _timeSet;

uint32_t _alarmType;

Hour_Format _format;
AM_PM _hoursPeriod;
uint8_t _hours;
Expand Down
Loading