Skip to content

Commit

Permalink
Merge pull request #93 from FRASTM/rtc_mix
Browse files Browse the repository at this point in the history
Rtc mix mode  with subseconds param. expressed in milliseconds
  • Loading branch information
fpistm authored Sep 25, 2023
2 parents 5f53d03 + e466ec9 commit d3f6526
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 28 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,30 @@ It is possible to use it thanks all alarm API with an extra argument:
rtc.enableAlarm(rtc.MATCH_DHHMMSS, STM32RTC::ALARM_B);
```

### Since STM32RTC version higher than 1.3.7
_Get the RTC handle_

* **`RTC_HandleTypeDef *RTC_GetHandle(void)`**

_binary and mix modes_

Some STM32 RTC have a binary mode with 32-bit free-running counter
in addition to their BCD mode for calendar (for example stm32wl55).
Combined with BCD this is the MIX mode. Only using the binary counter is the BIN mode.
Three RTC functional modes are available:
- `STM32RTC::MODE_BCD`
- `STM32RTC::MODE_MIX`
- `STM32RTC::MODE_BIN`

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


Any API using the Subsecond parameter is expressed in milliseconds
whatever the RTC input clock. This parameter is [0..999] in MIX or BCD mode
and [0..0xFFFFFFFF] in BIN mode. In this configuration, time and date registers
are not used by the RTC.

Refer to the Arduino RTC documentation for the other functions
http://arduino.cc/en/Reference/RTC

Expand Down
85 changes: 85 additions & 0 deletions examples/bin_onlyRTCAlarm/bin_onlyRTCAlarm.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
mode BINary only RTC alarm
This sketch shows how to configure the alarm A & B of the RTC in BIN mode
Creation 12 Dec 2017
by Wi6Labs
Modified 03 Jul 2020
by Frederic Pillon for STMicroelectronics
Modified 03 sept 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();

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 RTC mode */
rtc.setBinaryMode(STM32RTC::MODE_BIN);

/* in BIN mode time and Date register are not used, only the subsecond register for milisseconds */
rtc.begin(true, STM32RTC::HOUR_24);

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

/* subsecond expressed in milliseconds */
Serial.printf("Start at %d ms \r\n", rtc.getSubSeconds());

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

/* Program the AlarmA in 12 seconds */
rtc.setAlarmTime(0, 0, 0, 12000);
rtc.enableAlarm(rtc.MATCH_SUBSEC);
Serial.printf("Set Alarm A in 12s (at %d ms)\r\n", rtc.getAlarmSubSeconds());

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

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", 600,
rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
#endif /* RTC_ALARM_B */

}

void loop()
{

}

void alarmAMatch(void *data)
{
UNUSED(data);
rtc.disableAlarm(STM32RTC::ALARM_A);
Serial.printf("Alarm A Match at %d ms \r\n", rtc.getSubSeconds());
}

#ifdef RTC_ALARM_B
void alarmBMatch(void *data)
{
UNUSED(data);
rtc.disableAlarm(STM32RTC::ALARM_B); /* Else it will trig again */
Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds());
}
#endif /* RTC_ALARM_B */

99 changes: 99 additions & 0 deletions examples/mixRTCAlarm/mixRTCAlarm.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
mode Mix RTC alarm
This sketch shows how to configure the alarm A & B (if exists)
of the RTC in MIX or BCD (BINARY none) 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 RTC mode : STM32RTC::MODE_MIX or STM32RTC::MODE_BCD */
rtc.setBinaryMode(STM32RTC::MODE_BCD);

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

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

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

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 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 600ms ms from now (keep timeout < 1000ms) */
timeout = rtc.getSubSeconds() + 600;

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", 600,
rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
#endif /* RTC_ALARM_B */
}

void loop()
{
/* Just wait for Alarm */
}

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

#ifdef RTC_ALARM_B
void alarmBMatch(void *data)
{
UNUSED(data);
rtc.disableAlarm(STM32RTC::ALARM_B);
Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds());
}
#endif /* RTC_ALARM_B */
8 changes: 8 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ STM32RTC KEYWORD1
#######################################

getInstance KEYWORD2
getHandle KEYWORD2

getBinaryMode KEYWORD2
setBinaryMode KEYWORD2

getWeekDay KEYWORD2
getDay KEYWORD2
Expand Down Expand Up @@ -85,6 +89,7 @@ IS_HOUR_FORMAT KEYWORD2
# Constants (LITERAL1)
#######################################
MATCH_OFF LITERAL1
MATCH_SUBSEC LITERAL1
MATCH_SS LITERAL1
MATCH_MMSS LITERAL1
MATCH_HHMMSS LITERAL1
Expand All @@ -100,3 +105,6 @@ LSI_CLOCK LITERAL1
HSE_CLOCK LITERAL1
ALARM_A LITERAL1
ALARM_B LITERAL1
MODE_BCD LITERAL1
MODE_BIN LITERAL1
MODE_MIX LITERAL1
Loading

0 comments on commit d3f6526

Please sign in to comment.