foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
rtc.h
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file rtc.h
44  * @author foxBMS Team
45  * @date 2021-02-22 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup DRIVERS
49  * @prefix RTC
50  *
51  * @brief Header file of the RTC driver
52  * @details Uses the NXP PCF2131 RTC over I2C
53  *
54  */
55 
56 #ifndef FOXBMS__RTC_H_
57 #define FOXBMS__RTC_H_
58 
59 /*========== Includes =======================================================*/
60 
61 #include "fstd_types.h"
62 
63 #include <stdint.h>
64 
65 /*========== Macros and Definitions =========================================*/
66 
67 /** RTC I2C interface */
68 #define RTC_I2C_INTERFACE (i2cREG1)
69 /* I2C address of the RTC IC */
70 #define RTC_I2C_ADDRESS (0x53u)
71 
72 /** Time data of the RTC */
73 typedef struct {
74  uint8_t hundredthOfSeconds; /* [0-99] */
75  uint8_t seconds; /* [0-59] */
76  uint8_t minutes; /* [0-59] */
77  uint8_t hours; /* [0-23], 24 hour mode is used */
78  uint8_t weekday; /* [0-6], 0 is Sunday, 1 is Monday */
79  uint8_t day; /* [1-31] */
80  uint8_t month; /* [1-12] */
81  uint8_t year; /* [0-99] */
83 
84 /* Defines for the I2C communication */
85 /**@{*/
86 #define RTC_MAX_I2C_TRANSACTION_SIZE_IN_BYTES (10u)
87 #define RTC_WRITE_REGISTER_I2C_TRANSACTION_SIZE_IN_BYTES (2u)
88 /**@}*/
89 
90 /* Defines for the RTC control_1 register */
91 /**@{*/
92 #define RTC_REG_CONTROL_1_ADDR (0x0u)
93 #define RTC_CTRL1_STOP_BIT_MASK (0x20u)
94 #define RTC_CTRL1_12_24HOUR_MODE_BIT_MASK (0x04u)
95 /**@}*/
96 
97 /* Defines for the RTC control_3 register */
98 /**@{*/
99 #define RTC_REG_CONTROL_3_ADDR (0x02u)
100 #define RTC_CTRL3_BATTERY_PWRMNG_BITS_MASK (0xE0u)
101 #define RTC_CTRL3_BATTERY_PWRMNG_BITS_POSITION (5u)
102 #define RTC_CTRL3_BATTERY_LOW_FLAG_BIT_MASK (0x04u)
103 #define RTC_CTRL3_BATTERY_LOW_FLAG_BIT_POSITION (2u)
104 /**@}*/
105 
106 /* Power management configuration value to switch to battery voltage
107  and monitor battery voltage */
108 #define RTC_CTRL3_PWRMNG_DIRECTSWITCH_LOWDETECTIONENABLE_MODE (0x03u)
109 
110 /* Defines for the RTC CLKOUT_ctl register */
111 /**@{*/
112 #define RTC_REG_CLKOUT_CTL_ADDR (0x13u)
113 #define RTC_CLKOUT_CTL_OTPR_BIT_MASK (0x20u)
114 #define RTC_CLKOUT_CTL_OTPR_BIT_POSITION (5u)
115 /**@}*/
116 
117 /* Timeout in 10ms to wait for OTPR bit to be cleared or set */
118 #define RTC_OTPR_BIT_WAIT_TIMEOUT_ms (150u)
119 
120 /* Mask for the OSF bit */
121 #define RTC_SECONDS_OSF_BIT_MASK (0x80u)
122 /* Position of the OSF bit */
123 #define RTC_SECONDS_OSF_BIT_POSITION (7u)
124 
125 /* Defines for the RTC software reset register */
126 /**@{*/
127 #define RTC_REG_SOFTWARE_RESET_ADDR (0x5u)
128 #define RTC_SWRESET_CLEARPRESCALER (0xA4u)
129 /**@}*/
130 
131 /* Defines for the RTC time data registers */
132 /**@{*/
133 #define RTC_REG_TIME_START_ADDR (0x06u)
134 #define RTC_100TH_OF_SECONDS_OFFSET (0x0u)
135 #define RTC_SECONDS_OFFSET (0x1u)
136 #define RTC_MINUTES_OFFSET (0x2u)
137 #define RTC_HOURS_OFFSET (0x3u)
138 #define RTC_DAYS_OFFSET (0x4u)
139 #define RTC_WEEKDAYS_OFFSET (0x5u)
140 #define RTC_MONTHS_OFFSET (0x6u)
141 #define RTC_YEARS_OFFSET (0x7u)
142 /**@}*/
143 
144 /* Number of bytes needed to store all the RTC time data */
145 #define RTC_NUMBER_OF_TIMEDATA_BYTES (8u)
146 
147 /* Defines to access time data for ten's place */
148 /**@{*/
149 #define RTC_TENS_PLACE_FACTOR (10u)
150 #define RTC_TENS_PLACE_OFFSET (4u)
151 #define RTC_HUNDREDTH_OF_SECONDS_TENS_PLACE_MASK (0xF0u)
152 #define RTC_SECONDS_TENS_PLACE_MASK (0x70u)
153 #define RTC_MINUTES_TENS_PLACE_MASK (0x70u)
154 #define RTC_HOURS_TENS_PLACE_MASK (0x30u)
155 #define RTC_DAYS_TENS_PLACE_MASK (0x30u)
156 #define RTC_MONTHS_TENS_PLACE_MASK (0x10u)
157 #define RTC_YEARS_TENS_PLACE_MASK (0xF0u)
158 /**@}*/
159 
160 /* Defines to access time data for unit's place */
161 /**@{*/
162 #define RTC_UNITS_PLACE_MASK (0x0Fu)
163 #define RTC_WEEKDAYS_UNITS_PLACE_MASK (0x07u)
164 /**@}*/
165 
166 /* Offset for the RTC year, because it is encoded with two digits */
167 #define RTC_START_YEAR (2000)
168 
169 /* Defines for the C library time */
170 /**@{*/
171 #define RTC_CTIME_YEAR_START (1900)
172 #define RTC_CTIME_MONTH_START (1)
173 /**@}*/
174 
175 /* Max allowed difference between timer form RTC timer and IC time */
176 #define RTC_MAX_DIFFERENCE_BETWEEN_TIMER_AND_IC_s (1)
177 /* Factor to convert minutes to milliseconds */
178 #define RTC_FACTOR_MIN_TO_MS (60000u)
179 /* Time interval between two comparisons between RTC timer and RTC IC */
180 #define RTC_TIME_BETWEEN_RTC_TIMER_ADJUSTMENT_min (60u)
181 /* Time interval between two check of the battery low flag */
182 #define RTC_TIME_BETWEEN_BLF_FLAG_CHECK_min (10u)
183 
184 /** States for the OTPR procedure */
185 typedef enum {
195 
196 /*========== Extern Constant and Variable Declarations ======================*/
197 
198 /*========== Extern Function Prototypes =====================================*/
199 
200 /**
201  * @brief get the RTC system timer.
202  * @details This function is used to get the RTC system timer value.
203  * @return value of the RTC system timer
204  */
206 
207 /**
208  * @brief increment the RTC system timer.
209  * @details This function must be called every millisecond and so
210  * must be called in the 1ms task.
211  */
212 extern void RTC_IncrementSystemTime(void);
213 
214 /**
215  * @brief initialization of the RTC IC.
216  * @return STD_OK if initialization is finished, STD_NOT_OK otherwise
217  */
218 extern STD_RETURN_TYPE_e RTC_Initialize(void);
219 
220 /**
221  * @brief initialize system time with RTC time.
222  * @details This function is called once at the startup of the system.
223  * It reads the time from the RTC IC over I2C and sets the RTC
224  * system timer with this value. It must be called once the
225  * 1ms task is already running, otherwise the RTC system timer
226  * will not be incremented and there will be a discrepancy between
227  * the RTC IC time and the RTC system timer.
228  */
229 extern void RTC_InitializeSystemTimeWithRtc(void);
230 
231 /**
232  * @brief set the RTC system timer.
233  * @details This function is used to set the RTC system timer with
234  * RTC time data. It is called at startup by
235  * RTC_InitializeSystemTimeWithRtc().
236  * @param timeRtcFormat time data to set the RTC system timer
237  */
238 extern void RTC_SetSystemTimeRtcFormat(RTC_TIME_DATA_s timeRtcFormat);
239 
240 /**
241  * @brief trigger function for the RTC driver.
242  * @details This function uses RTC_SetOverCanMessage() to check in the RTC
243  * queue for requests to set the RTC time.
244  * If a request was made, the time of the RTC IC is set over I2C and
245  * the RTC system timer is set.
246  *
247  */
248 extern void RTC_Trigger(void);
249 
250 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
251 #ifdef UNITY_UNIT_TEST
252 #endif
253 
254 #endif /* FOXBMS__RTC_H_ */
Definition of foxBMS standard types.
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
void RTC_IncrementSystemTime(void)
increment the RTC system timer.
Definition: rtc.c:586
void RTC_InitializeSystemTimeWithRtc(void)
initialize system time with RTC time.
Definition: rtc.c:577
void RTC_SetSystemTimeRtcFormat(RTC_TIME_DATA_s timeRtcFormat)
set the RTC system timer.
Definition: rtc.c:595
void RTC_Trigger(void)
trigger function for the RTC driver.
Definition: rtc.c:414
RTC_INIT_STATES_e
Definition: rtc.h:185
@ RTC_WAIT_CLEAR_OTPR
Definition: rtc.h:189
@ RTC_SET_BLF
Definition: rtc.h:187
@ RTC_CLEAR_OTPR
Definition: rtc.h:188
@ RTC_SET_SYSTEM_TIMER
Definition: rtc.h:186
@ RTC_WAIT_SET_OTPR
Definition: rtc.h:192
@ RTC_SET_OTPR
Definition: rtc.h:191
@ RTC_OTPR_CLEARED
Definition: rtc.h:190
@ RTC_INIT_SET_FINISHED
Definition: rtc.h:193
STD_RETURN_TYPE_e RTC_Initialize(void)
initialization of the RTC IC.
Definition: rtc.c:441
RTC_TIME_DATA_s RTC_GetSystemTimeRtcFormat(void)
get the RTC system timer.
Definition: rtc.c:607
uint8_t hours
Definition: rtc.h:77
uint8_t minutes
Definition: rtc.h:76
uint8_t seconds
Definition: rtc.h:75
uint8_t month
Definition: rtc.h:80
uint8_t weekday
Definition: rtc.h:78
uint8_t hundredthOfSeconds
Definition: rtc.h:74
uint8_t year
Definition: rtc.h:81
uint8_t day
Definition: rtc.h:79