Skip to content

Commit 1d1f647

Browse files
authored
Merge pull request #28 from fpistm/SubSecond
Proper subsecond management
2 parents 3589270 + edee865 commit 1d1f647

File tree

10 files changed

+1039
-205
lines changed

10 files changed

+1039
-205
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ _RTC clock source_
3434
* **`void setClockSource(Source_Clock source)`** : this function must be called before `begin()`.
3535

3636
_RTC Asynchronous and Synchronous prescaler_
37-
* **`void getPrediv(int8_t *predivA, int16_t *predivS)`** : get user (a)synchronous prescaler values if set else computed ones for the current clock source.
38-
* **`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.
37+
* **`void getPrediv(int8_t *predivA, int16_t *predivS)`** : get (a)synchronous prescaler values if set else computed ones for the current clock source.
38+
* **`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))_**
3939

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

6363
_SubSeconds alarm management_
6464

65+
Important note:
66+
- STM32F1 and STM32L1xx (Ultra Low Power Medium (ULPM) density) series do not support subsecond.
67+
- Subsecond “resolution” depends on synchronous prescaler value. Bigger than this value is, better resolution will get for subsecond.
68+
6569
* **`void setAlarmSubSeconds(uint32_t subSeconds)`**
6670

6771
* **Updated API:**

examples/Epoch/Epoch.ino

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
1-
/**
2-
******************************************************************************
3-
* @file Epoch.ino
4-
* @author WI6LABS
5-
* @version V1.0.0
6-
* @date 12-December-2017
7-
* @brief RTC epoch example
8-
*
9-
******************************************************************************
10-
* @attention
11-
*
12-
* <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
13-
*
14-
* Redistribution and use in source and binary forms, with or without modification,
15-
* are permitted provided that the following conditions are met:
16-
* 1. Redistributions of source code must retain the above copyright notice,
17-
* this list of conditions and the following disclaimer.
18-
* 2. Redistributions in binary form must reproduce the above copyright notice,
19-
* this list of conditions and the following disclaimer in the documentation
20-
* and/or other materials provided with the distribution.
21-
* 3. Neither the name of STMicroelectronics nor the names of its contributors
22-
* may be used to endorse or promote products derived from this software
23-
* without specific prior written permission.
24-
*
25-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35-
*
36-
******************************************************************************
37-
*/
1+
/*
2+
Epoch
3+
4+
This sketch shows how to manage the RTC using Epoch time
5+
6+
Creation 12 Dec 2017
7+
by Wi6Labs
8+
Modified 03 Jul 2020
9+
by Frederic Pillon for STMicroelectronics
10+
11+
This example code is in the public domain.
12+
13+
https://github.com/stm32duino/STM32RTC
14+
*/
3815

3916
#include <STM32RTC.h>
17+
#include <time.h>
4018

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

5634
void loop() {
35+
uint32_t ss = rtc.getSubSeconds();
36+
uint32_t epoch = rtc.getEpoch();
37+
time_t rawtime = epoch;
38+
struct tm ts;
39+
char buf[80];
40+
5741
Serial.print("Unix time = ");
58-
Serial.println(rtc.getEpoch());
42+
Serial.println(epoch);
5943

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

63-
// Print date...
64-
Serial.print(rtc.getDay());
65-
Serial.print("/");
66-
Serial.print(rtc.getMonth());
67-
Serial.print("/");
68-
Serial.print(rtc.getYear());
69-
Serial.print("\t");
70-
71-
// ...and time
72-
print2digits(rtc.getHours());
73-
Serial.print(":");
74-
print2digits(rtc.getMinutes());
75-
Serial.print(":");
76-
print2digits(rtc.getSeconds());
77-
47+
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
48+
ts = *localtime(&rawtime);
49+
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S", &ts);
50+
Serial.print(buf);
51+
Serial.print(".");
52+
print2digits(ss);
7853
Serial.println();
7954

80-
delay(1000);
55+
delay(678);
8156
}
8257

83-
void print2digits(int number) {
58+
void print2digits(uint32_t number) {
59+
if (number < 100) {
60+
Serial.print("0");
61+
}
8462
if (number < 10) {
8563
Serial.print("0");
8664
}
8765
Serial.print(number);
88-
}
66+
}

examples/RTCClockSelection/RTCClockSelection.ino

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,22 @@
1-
/**
2-
******************************************************************************
3-
* @file RTCClockSelection.ino
4-
* @author WI6LABS
5-
* @version V1.0.0
6-
* @date 15-March-2018
7-
* @brief RTC clock selection: LSI, LSE or HSE. Refer to board datasheet to
8-
* know available clock.
9-
*
10-
******************************************************************************
11-
* @attention
12-
*
13-
* <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
14-
*
15-
* Redistribution and use in source and binary forms, with or without modification,
16-
* are permitted provided that the following conditions are met:
17-
* 1. Redistributions of source code must retain the above copyright notice,
18-
* this list of conditions and the following disclaimer.
19-
* 2. Redistributions in binary form must reproduce the above copyright notice,
20-
* this list of conditions and the following disclaimer in the documentation
21-
* and/or other materials provided with the distribution.
22-
* 3. Neither the name of STMicroelectronics nor the names of its contributors
23-
* may be used to endorse or promote products derived from this software
24-
* without specific prior written permission.
25-
*
26-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36-
*
37-
******************************************************************************
38-
*/
1+
/*
2+
RTCClockSelection
3+
4+
This sketch shows how to select one of the RTC clock:
5+
- LSI (default)
6+
- LSE
7+
- HSE
8+
9+
Refer to board datasheet to know available clock
10+
11+
Creation 15 March 2018
12+
by Wi6Labs
13+
Modified 03 Jul 2020
14+
by Frederic Pillon for STMicroelectronics
15+
16+
This example code is in the public domain.
17+
18+
https://github.com/stm32duino/STM32RTC
19+
*/
3920

4021
#include <STM32RTC.h>
4122

@@ -96,17 +77,19 @@ void loop()
9677
print2digits(rtc.getMinutes());
9778
Serial.print(":");
9879
print2digits(rtc.getSeconds());
99-
80+
Serial.print(".");
81+
print2digits(rtc.getSubSeconds());
10082
Serial.println();
10183

10284
delay(1000);
10385
}
10486

105-
106-
10787
void print2digits(int number) {
88+
if (number < 100) {
89+
Serial.print("0");
90+
}
10891
if (number < 10) {
109-
Serial.print("0"); // print a 0 before if the number is < than 10
92+
Serial.print("0");
11093
}
11194
Serial.print(number);
112-
}
95+
}

examples/SimpleRTC/SimpleRTC.ino

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
1-
/**
2-
******************************************************************************
3-
* @file SimpleRTC.ino
4-
* @author WI6LABS
5-
* @version V1.0.0
6-
* @date 12-December-2017
7-
* @brief Simple RTC example.
8-
*
9-
******************************************************************************
10-
* @attention
11-
*
12-
* <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
13-
*
14-
* Redistribution and use in source and binary forms, with or without modification,
15-
* are permitted provided that the following conditions are met:
16-
* 1. Redistributions of source code must retain the above copyright notice,
17-
* this list of conditions and the following disclaimer.
18-
* 2. Redistributions in binary form must reproduce the above copyright notice,
19-
* this list of conditions and the following disclaimer in the documentation
20-
* and/or other materials provided with the distribution.
21-
* 3. Neither the name of STMicroelectronics nor the names of its contributors
22-
* may be used to endorse or promote products derived from this software
23-
* without specific prior written permission.
24-
*
25-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35-
*
36-
******************************************************************************
37-
*/
1+
/*
2+
SimpleRTC
3+
4+
This sketch shows how to configure the RTC and to display
5+
the date and time periodically
6+
7+
Creation 12 Dec 2017
8+
by Wi6Labs
9+
Modified 03 Jul 2020
10+
by Frederic Pillon for STMicroelectronics
11+
12+
This example code is in the public domain.
13+
14+
https://github.com/stm32duino/STM32RTC
15+
*/
3816

3917
#include <STM32RTC.h>
4018

@@ -95,17 +73,19 @@ void loop()
9573
print2digits(rtc.getMinutes());
9674
Serial.print(":");
9775
print2digits(rtc.getSeconds());
98-
76+
Serial.print(".");
77+
print2digits(rtc.getSubSeconds());
9978
Serial.println();
10079

10180
delay(1000);
10281
}
10382

104-
105-
10683
void print2digits(int number) {
84+
if (number < 100) {
85+
Serial.print("0");
86+
}
10787
if (number < 10) {
108-
Serial.print("0"); // print a 0 before if the number is < than 10
88+
Serial.print("0");
10989
}
11090
Serial.print(number);
111-
}
91+
}

examples/advancedRTCAlarm/advancedRTCAlarm.ino

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
It uses the optional 'data' alarm callback parameters to
66
reload alarm with 'atime' offset indefinitely.
77
8-
98
Creation 25 May 2018
109
by Frederic Pillon for STMicroelectronics
10+
Modified 03 Jul 2020
11+
by Frederic Pillon for STMicroelectronics
1112
1213
This example code is in the public domain.
1314
1415
https://github.com/stm32duino/STM32RTC
15-
1616
*/
1717

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

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

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

5253
rtc.attachInterrupt(alarmMatch, &atime);
5354
rtc.setAlarmDay(day);
54-
rtc.setAlarmTime(16, 0, 10);
55+
rtc.setAlarmTime(16, 0, 10, 567);
5556
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
5657
}
5758

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

6364
void alarmMatch(void *data)
6465
{
65-
uint32_t sec = 1;
66-
if(data != NULL) {
67-
sec = *(uint32_t*)data;
66+
uint32_t epoc;
67+
uint32_t epoc_ms;
68+
uint32_t sec = 0;
69+
uint32_t _millis = 1000;
70+
71+
if (data != NULL) {
72+
_millis = *(uint32_t*)data;
6873
// Minimum is 1 second
69-
if (sec == 0){
74+
if (sec == 0) {
7075
sec = 1;
7176
}
7277
}
73-
alarmMatch_counter++;
74-
Serial.print("Alarm Match ");
75-
Serial.println(alarmMatch_counter);
76-
rtc.setAlarmEpoch( rtc.getEpoch() + sec);
78+
79+
sec = _millis / 1000;
80+
#ifdef STM32F1xx
81+
// Minimum is 1 second
82+
if (sec == 0) {
83+
sec = 1;
84+
}
85+
epoc = rtc.getEpoch(&epoc_ms);
86+
#else
87+
_millis = _millis % 1000;
88+
epoc = rtc.getEpoch(&epoc_ms);
89+
90+
//Update epoch_ms - might need to add a second to epoch
91+
epoc_ms += _millis;
92+
if (epoc_ms >= 1000) {
93+
sec ++;
94+
epoc_ms -= 1000;
95+
}
96+
#endif
97+
Serial.printf("Alarm Match %i\n", ++alarmMatch_counter);
98+
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms);
7799
}

0 commit comments

Comments
 (0)