Skip to content

Proper subsecond management #28

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

Merged
merged 3 commits into from
Jul 3, 2020
Merged
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ _RTC clock source_
* **`void setClockSource(Source_Clock source)`** : this function must be called before `begin()`.

_RTC Asynchronous and Synchronous prescaler_
* **`void getPrediv(int8_t *predivA, int16_t *predivS)`** : get user (a)synchronous prescaler values if set else computed ones for the current clock source.
* **`void setPrediv(int8_t predivA, int16_t predivS)`** : set user (a)synchronous prescaler values. This function must be called before `begin()`. Use -1 to reset value and use computed ones.
* **`void getPrediv(int8_t *predivA, int16_t *predivS)`** : get (a)synchronous prescaler values if set else computed ones for the current clock source.
* **`void setPrediv(int8_t predivA, int16_t predivS)`** : set (a)synchronous prescaler values. This function must be called before `begin()`. Use -1 to reset value and use computed ones. Those values have to match the following conditions: **_1Hz = RTC CLK source / ((predivA + 1) * (predivS + 1))_**

_SubSeconds management_
* **`uint32_t getSubSeconds(void)`**
Expand All @@ -62,6 +62,10 @@ _Time and date configuration (added for convenience)_

_SubSeconds alarm management_

Important note:
- STM32F1 and STM32L1xx (Ultra Low Power Medium (ULPM) density) series do not support subsecond.
- Subsecond “resolution” depends on synchronous prescaler value. Bigger than this value is, better resolution will get for subsecond.

* **`void setAlarmSubSeconds(uint32_t subSeconds)`**

* **Updated API:**
Expand Down
90 changes: 34 additions & 56 deletions examples/Epoch/Epoch.ino
Original file line number Diff line number Diff line change
@@ -1,42 +1,20 @@
/**
******************************************************************************
* @file Epoch.ino
* @author WI6LABS
* @version V1.0.0
* @date 12-December-2017
* @brief RTC epoch example
*
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/*
Epoch

This sketch shows how to manage the RTC using Epoch time

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

This example code is in the public domain.

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

#include <STM32RTC.h>
#include <time.h>

/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();
Expand All @@ -54,35 +32,35 @@ void setup() {
}

void loop() {
uint32_t ss = rtc.getSubSeconds();
uint32_t epoch = rtc.getEpoch();
time_t rawtime = epoch;
struct tm ts;
char buf[80];

Serial.print("Unix time = ");
Serial.println(rtc.getEpoch());
Serial.println(epoch);

Serial.print("Seconds since Jan 1 2000 = ");
Serial.println(rtc.getY2kEpoch());

// Print date...
Serial.print(rtc.getDay());
Serial.print("/");
Serial.print(rtc.getMonth());
Serial.print("/");
Serial.print(rtc.getYear());
Serial.print("\t");

// ...and time
print2digits(rtc.getHours());
Serial.print(":");
print2digits(rtc.getMinutes());
Serial.print(":");
print2digits(rtc.getSeconds());

// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
ts = *localtime(&rawtime);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S", &ts);
Serial.print(buf);
Serial.print(".");
print2digits(ss);
Serial.println();

delay(1000);
delay(678);
}

void print2digits(int number) {
void print2digits(uint32_t number) {
if (number < 100) {
Serial.print("0");
}
if (number < 10) {
Serial.print("0");
}
Serial.print(number);
}
}
69 changes: 26 additions & 43 deletions examples/RTCClockSelection/RTCClockSelection.ino
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
/**
******************************************************************************
* @file RTCClockSelection.ino
* @author WI6LABS
* @version V1.0.0
* @date 15-March-2018
* @brief RTC clock selection: LSI, LSE or HSE. Refer to board datasheet to
* know available clock.
*
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/*
RTCClockSelection

This sketch shows how to select one of the RTC clock:
- LSI (default)
- LSE
- HSE

Refer to board datasheet to know available clock

Creation 15 March 2018
by Wi6Labs
Modified 03 Jul 2020
by Frederic Pillon for STMicroelectronics

This example code is in the public domain.

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

#include <STM32RTC.h>

Expand Down Expand Up @@ -96,17 +77,19 @@ void loop()
print2digits(rtc.getMinutes());
Serial.print(":");
print2digits(rtc.getSeconds());

Serial.print(".");
print2digits(rtc.getSubSeconds());
Serial.println();

delay(1000);
}



void print2digits(int number) {
if (number < 100) {
Serial.print("0");
}
if (number < 10) {
Serial.print("0"); // print a 0 before if the number is < than 10
Serial.print("0");
}
Serial.print(number);
}
}
64 changes: 22 additions & 42 deletions examples/SimpleRTC/SimpleRTC.ino
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
/**
******************************************************************************
* @file SimpleRTC.ino
* @author WI6LABS
* @version V1.0.0
* @date 12-December-2017
* @brief Simple RTC example.
*
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/*
SimpleRTC

This sketch shows how to configure the RTC and to display
the date and time periodically

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

This example code is in the public domain.

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

#include <STM32RTC.h>

Expand Down Expand Up @@ -95,17 +73,19 @@ void loop()
print2digits(rtc.getMinutes());
Serial.print(":");
print2digits(rtc.getSeconds());

Serial.print(".");
print2digits(rtc.getSubSeconds());
Serial.println();

delay(1000);
}



void print2digits(int number) {
if (number < 100) {
Serial.print("0");
}
if (number < 10) {
Serial.print("0"); // print a 0 before if the number is < than 10
Serial.print("0");
}
Serial.print(number);
}
}
48 changes: 35 additions & 13 deletions examples/advancedRTCAlarm/advancedRTCAlarm.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
It uses the optional 'data' alarm callback parameters to
reload alarm with 'atime' offset indefinitely.


Creation 25 May 2018
by Frederic Pillon for STMicroelectronics
Modified 03 Jul 2020
by Frederic Pillon for STMicroelectronics

This example code is in the public domain.

https://github.com/stm32duino/STM32RTC

*/

#include <STM32RTC.h>
Expand All @@ -23,8 +23,9 @@ STM32RTC& rtc = STM32RTC::getInstance();
/* Declare it volatile since it's incremented inside an interrupt */
volatile int alarmMatch_counter = 0;

/* Change this value to set alarm match offset */
static uint32_t atime = 5;
/* Change this value to set alarm match offset in millisecond */
/* Note that STM32F1xx does not manage subsecond only second */
static uint32_t atime = 678;

/* Change these values to set the current initial time */
const byte seconds = 0;
Expand All @@ -51,7 +52,7 @@ void setup()

rtc.attachInterrupt(alarmMatch, &atime);
rtc.setAlarmDay(day);
rtc.setAlarmTime(16, 0, 10);
rtc.setAlarmTime(16, 0, 10, 567);
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
}

Expand All @@ -62,16 +63,37 @@ void loop()

void alarmMatch(void *data)
{
uint32_t sec = 1;
if(data != NULL) {
sec = *(uint32_t*)data;
uint32_t epoc;
uint32_t epoc_ms;
uint32_t sec = 0;
uint32_t _millis = 1000;

if (data != NULL) {
_millis = *(uint32_t*)data;
// Minimum is 1 second
if (sec == 0){
if (sec == 0) {
sec = 1;
}
}
alarmMatch_counter++;
Serial.print("Alarm Match ");
Serial.println(alarmMatch_counter);
rtc.setAlarmEpoch( rtc.getEpoch() + sec);

sec = _millis / 1000;
#ifdef STM32F1xx
// Minimum is 1 second
if (sec == 0) {
sec = 1;
}
epoc = rtc.getEpoch(&epoc_ms);
#else
_millis = _millis % 1000;
epoc = rtc.getEpoch(&epoc_ms);

//Update epoch_ms - might need to add a second to epoch
epoc_ms += _millis;
if (epoc_ms >= 1000) {
sec ++;
epoc_ms -= 1000;
}
#endif
Serial.printf("Alarm Match %i\n", ++alarmMatch_counter);
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms);
}
Loading