Skip to content

Commit 4afc4e4

Browse files
committed
[NUCLEO_F030R8] Add rtc api
1 parent c9bd305 commit 4afc4e4

File tree

2 files changed

+139
-2
lines changed

2 files changed

+139
-2
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#define DEVICE_INTERRUPTIN 1
3838

3939
#define DEVICE_ANALOGIN 1
40-
#define DEVICE_ANALOGOUT 1
40+
#define DEVICE_ANALOGOUT 0 // Not present on this device
4141

4242
#define DEVICE_SERIAL 1
4343

@@ -47,7 +47,7 @@
4747
#define DEVICE_SPI 0
4848
#define DEVICE_SPISLAVE 0
4949

50-
#define DEVICE_RTC 0
50+
#define DEVICE_RTC 1
5151

5252
#define DEVICE_PWMOUT 0
5353

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/* mbed Microcontroller Library
2+
*******************************************************************************
3+
* Copyright (c) 2014, STMicroelectronics
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*******************************************************************************
29+
*/
30+
#include "rtc_api.h"
31+
32+
static int rtc_inited = 0;
33+
34+
void rtc_init(void) {
35+
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock
36+
37+
PWR_BackupAccessCmd(ENABLE); // Enable access to RTC
38+
39+
// Note: the LSI is used as RTC source clock
40+
// The RTC Clock may vary due to LSI frequency dispersion.
41+
42+
RCC_LSICmd(ENABLE); // Enable LSI
43+
44+
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready
45+
46+
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select LSI as RTC Clock Source
47+
48+
RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock
49+
50+
RTC_WaitForSynchro(); // Wait for RTC registers synchronization
51+
52+
uint32_t lsi_freq = 40000; // *** TODO** To be measured precisely using a timer input capture
53+
54+
RTC_InitTypeDef RTC_InitStructure;
55+
RTC_InitStructure.RTC_AsynchPrediv = 127;
56+
RTC_InitStructure.RTC_SynchPrediv = (lsi_freq / 128) - 1;
57+
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
58+
RTC_Init(&RTC_InitStructure);
59+
60+
PWR_BackupAccessCmd(DISABLE); // Disable access to RTC
61+
62+
rtc_inited = 1;
63+
}
64+
65+
void rtc_free(void) {
66+
RCC_DeInit(); // Resets the RCC clock configuration to the default reset state
67+
rtc_inited = 0;
68+
}
69+
70+
int rtc_isenabled(void) {
71+
return rtc_inited;
72+
}
73+
74+
/*
75+
RTC Registers
76+
RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday
77+
RTC_Month 1=january, 2=february, ..., 12=december
78+
RTC_Date day of the month 1-31
79+
RTC_Year year 0-99
80+
struct tm
81+
tm_sec seconds after the minute 0-61
82+
tm_min minutes after the hour 0-59
83+
tm_hour hours since midnight 0-23
84+
tm_mday day of the month 1-31
85+
tm_mon months since January 0-11
86+
tm_year years since 1900
87+
tm_wday days since Sunday 0-6
88+
tm_yday days since January 1 0-365
89+
tm_isdst Daylight Saving Time flag
90+
*/
91+
time_t rtc_read(void) {
92+
RTC_DateTypeDef dateStruct;
93+
RTC_TimeTypeDef timeStruct;
94+
struct tm timeinfo;
95+
96+
// Read actual date and time
97+
RTC_GetTime(RTC_Format_BIN, &timeStruct);
98+
RTC_GetDate(RTC_Format_BIN, &dateStruct);
99+
100+
// Setup a tm structure based on the RTC
101+
timeinfo.tm_wday = dateStruct.RTC_WeekDay;
102+
timeinfo.tm_mon = dateStruct.RTC_Month - 1;
103+
timeinfo.tm_mday = dateStruct.RTC_Date;
104+
timeinfo.tm_year = dateStruct.RTC_Year + 100;
105+
timeinfo.tm_hour = timeStruct.RTC_Hours;
106+
timeinfo.tm_min = timeStruct.RTC_Minutes;
107+
timeinfo.tm_sec = timeStruct.RTC_Seconds;
108+
109+
// Convert to timestamp
110+
time_t t = mktime(&timeinfo);
111+
112+
return t;
113+
}
114+
115+
void rtc_write(time_t t) {
116+
RTC_DateTypeDef dateStruct;
117+
RTC_TimeTypeDef timeStruct;
118+
119+
// Convert the time into a tm
120+
struct tm *timeinfo = localtime(&t);
121+
122+
// Fill RTC structures
123+
dateStruct.RTC_WeekDay = timeinfo->tm_wday;
124+
dateStruct.RTC_Month = timeinfo->tm_mon + 1;
125+
dateStruct.RTC_Date = timeinfo->tm_mday;
126+
dateStruct.RTC_Year = timeinfo->tm_year - 100;
127+
timeStruct.RTC_Hours = timeinfo->tm_hour;
128+
timeStruct.RTC_Minutes = timeinfo->tm_min;
129+
timeStruct.RTC_Seconds = timeinfo->tm_sec;
130+
timeStruct.RTC_H12 = RTC_HourFormat_24;
131+
132+
// Change the RTC current date/time
133+
PWR_BackupAccessCmd(ENABLE); // Enable access to RTC
134+
RTC_SetDate(RTC_Format_BIN, &dateStruct);
135+
RTC_SetTime(RTC_Format_BIN, &timeStruct);
136+
PWR_BackupAccessCmd(DISABLE); // Disable access to RTC
137+
}

0 commit comments

Comments
 (0)