Skip to content

Commit 6cb058c

Browse files
authored
Merge pull request #1 from fprwi6labs/dev
Add STM32RTC library source files
2 parents 3c81234 + d28089d commit 6cb058c

File tree

9 files changed

+1563
-0
lines changed

9 files changed

+1563
-0
lines changed

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
11
# STM32RTC
22
A RTC library for STM32.
3+
4+
# API
5+
6+
This library is based on the Arduino RTCZero library.
7+
The library allows to take control of the internal RTC of the STM32 boards.
8+
9+
The following functions are not supported:
10+
11+
* **`void standbyMode()`**: use the STM32 Low Power library instead.
12+
* **`uint8_t getAlarmMonth()`**: month not supported by STM32 RTC architecture.
13+
* **`uint8_t getAlarmYear()`**: year not supported by STM32 RTC architecture.
14+
* **`void setAlarmMonth(uint8_t month)`**: month not supported by STM32 RTC architecture.
15+
* **`void setAlarmYear(uint8_t year)`**: year not supported by STM32 RTC architecture.
16+
* **`void setAlarmDate(uint8_t day, uint8_t month, uint8_t year)`**: month and year not supported by STM32 RTC architecture.
17+
18+
The following functions have been added to support specific STM32 RTC features:
19+
20+
_RTC hours mode (12 or 24)_
21+
* **`void begin(RTCHourFormats_t format)`**
22+
23+
_RTC clock source_
24+
* **`void setClockSource(sourceClock_t source)`** : this function must be called before `begin()`
25+
26+
_SubSeconds management_
27+
* **`uint32_t getSubSeconds(void)`**
28+
* **`void setSubSeconds(uint32_t subSeconds)`**
29+
30+
_Hour format (AM or PM)_
31+
* **`uint8_t getHours(Hour12_AM_PM_t *format)`**
32+
* **`void setHours(uint8_t hours, Hour12_AM_PM_t format)`**
33+
* **`void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, Hour12_AM_PM_t format)`**
34+
* **`void setAlarmHours(uint8_t hours, Hour12_AM_PM_t format)`**
35+
* **`uint8_t getAlarmHours(Hour12_AM_PM_t *format)`**
36+
* **`void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, Hour12_AM_PM_t format)`**
37+
38+
_Week day configuration_
39+
* **`uint8_t getWeekDay(void)`**
40+
* **`void setWeekDay(uint8_t weekDay)`**
41+
* **`void setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year)`**
42+
43+
Refer to the Arduino RTC documentation for the other functions
44+
http://arduino.cc/en/Reference/RTC
45+
46+
## Source
47+
48+
Source files available at:
49+
https://github.com/stm32duino/STM32RTC

examples/Epoch/Epoch.ino

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
*/
38+
39+
#include <STM32RTC.h>
40+
41+
/* Create a rtc object */
42+
STM32RTC rtc;
43+
44+
void setup() {
45+
Serial.begin(9600);
46+
47+
rtc.begin(); // initialize RTC 24H format
48+
49+
rtc.setEpoch(1451606400); // Jan 1, 2016
50+
}
51+
52+
void loop() {
53+
Serial.print("Unix time = ");
54+
Serial.println(rtc.getEpoch());
55+
56+
Serial.print("Seconds since Jan 1 2000 = ");
57+
Serial.println(rtc.getY2kEpoch());
58+
59+
// Print date...
60+
Serial.print(rtc.getDay());
61+
Serial.print("/");
62+
Serial.print(rtc.getMonth());
63+
Serial.print("/");
64+
Serial.print(rtc.getYear());
65+
Serial.print("\t");
66+
67+
// ...and time
68+
print2digits(rtc.getHours());
69+
Serial.print(":");
70+
print2digits(rtc.getMinutes());
71+
Serial.print(":");
72+
print2digits(rtc.getSeconds());
73+
74+
Serial.println();
75+
76+
delay(1000);
77+
}
78+
79+
void print2digits(int number) {
80+
if (number < 10) {
81+
Serial.print("0");
82+
}
83+
Serial.print(number);
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
*/
39+
40+
#include <STM32RTC.h>
41+
42+
/* Create a rtc object */
43+
STM32RTC rtc;
44+
45+
/* Change these values to set the current initial time */
46+
const byte seconds = 0;
47+
const byte minutes = 0;
48+
const byte hours = 16;
49+
50+
/* Change these values to set the current initial date */
51+
/* Monday 15th June 2015 */
52+
const byte weekDay = 1;
53+
const byte day = 15;
54+
const byte month = 6;
55+
const byte year = 15;
56+
57+
void setup()
58+
{
59+
Serial.begin(9600);
60+
61+
// Select RTC clock source: RTC_LSI_CLOCK, RTC_LSE_CLOCK or RTC_HSE_CLOCK.
62+
// By default the LSI is selected as source.
63+
rtc.setClockSource(RTC_LSE_CLOCK);
64+
65+
rtc.begin(); // initialize RTC 24H format
66+
67+
// Set the time
68+
rtc.setHours(hours);
69+
rtc.setMinutes(minutes);
70+
rtc.setSeconds(seconds);
71+
72+
// Set the date
73+
rtc.setWeekDay(weekDay);
74+
rtc.setDay(day);
75+
rtc.setMonth(month);
76+
rtc.setYear(year);
77+
78+
// you can use also
79+
//rtc.setTime(hours, minutes, seconds);
80+
//rtc.setDate(weekDay, day, month, year);
81+
}
82+
83+
void loop()
84+
{
85+
// Print date...
86+
print2digits(rtc.getDay());
87+
Serial.print("/");
88+
print2digits(rtc.getMonth());
89+
Serial.print("/");
90+
print2digits(rtc.getYear());
91+
Serial.print(" ");
92+
93+
// ...and time
94+
print2digits(rtc.getHours());
95+
Serial.print(":");
96+
print2digits(rtc.getMinutes());
97+
Serial.print(":");
98+
print2digits(rtc.getSeconds());
99+
100+
Serial.println();
101+
102+
delay(1000);
103+
}
104+
105+
106+
107+
void print2digits(int number) {
108+
if (number < 10) {
109+
Serial.print("0"); // print a 0 before if the number is < than 10
110+
}
111+
Serial.print(number);
112+
}

examples/SimpleRTC/SimpleRTC.ino

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
*/
38+
39+
#include <STM32RTC.h>
40+
41+
/* Create a rtc object */
42+
STM32RTC rtc;
43+
44+
/* Change these values to set the current initial time */
45+
const byte seconds = 0;
46+
const byte minutes = 0;
47+
const byte hours = 16;
48+
49+
/* Change these values to set the current initial date */
50+
/* Monday 15th June 2015 */
51+
const byte weekDay = 1;
52+
const byte day = 15;
53+
const byte month = 6;
54+
const byte year = 15;
55+
56+
void setup()
57+
{
58+
Serial.begin(9600);
59+
60+
rtc.begin(); // initialize RTC 24H format
61+
62+
// Set the time
63+
rtc.setHours(hours);
64+
rtc.setMinutes(minutes);
65+
rtc.setSeconds(seconds);
66+
67+
// Set the date
68+
rtc.setWeekDay(weekDay);
69+
rtc.setDay(day);
70+
rtc.setMonth(month);
71+
rtc.setYear(year);
72+
73+
// you can use also
74+
//rtc.setTime(hours, minutes, seconds);
75+
//rtc.setDate(weekDay, day, month, year);
76+
}
77+
78+
void loop()
79+
{
80+
// Print date...
81+
print2digits(rtc.getDay());
82+
Serial.print("/");
83+
print2digits(rtc.getMonth());
84+
Serial.print("/");
85+
print2digits(rtc.getYear());
86+
Serial.print(" ");
87+
88+
// ...and time
89+
print2digits(rtc.getHours());
90+
Serial.print(":");
91+
print2digits(rtc.getMinutes());
92+
Serial.print(":");
93+
print2digits(rtc.getSeconds());
94+
95+
Serial.println();
96+
97+
delay(1000);
98+
}
99+
100+
101+
102+
void print2digits(int number) {
103+
if (number < 10) {
104+
Serial.print("0"); // print a 0 before if the number is < than 10
105+
}
106+
Serial.print(number);
107+
}

0 commit comments

Comments
 (0)