1 /*!
2  * \file      rtc-board.h
3  *
4  * \brief     Target board RTC timer and low power modes management
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013-2017 Semtech
16  *
17  * \endcode
18  *
19  * \author    Miguel Luis ( Semtech )
20  *
21  * \author    Gregory Cristian ( Semtech )
22  */
23 #ifndef __RTC_BOARD_H__
24 #define __RTC_BOARD_H__
25 
26 #ifdef __cplusplus
27 extern "C"
28 {
29 #endif
30 
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include "timer.h"
34 
35 /*!
36  * \brief Temperature coefficient of the clock source
37  */
38 #define RTC_TEMP_COEFFICIENT                            ( -0.035f )
39 
40 /*!
41  * \brief Temperature coefficient deviation of the clock source
42  */
43 #define RTC_TEMP_DEV_COEFFICIENT                        ( 0.0035f )
44 
45 /*!
46  * \brief Turnover temperature of the clock source
47  */
48 #define RTC_TEMP_TURNOVER                               ( 25.0f )
49 
50 /*!
51  * \brief Turnover temperature deviation of the clock source
52  */
53 #define RTC_TEMP_DEV_TURNOVER                           ( 5.0f )
54 
55 /*!
56  * \brief Initializes the RTC timer
57  *
58  * \remark The timer is based on the RTC
59  */
60 void RtcInit( void );
61 
62 /*!
63  * \brief Returns the minimum timeout value
64  *
65  * \retval minTimeout Minimum timeout value in in ticks
66  */
67 uint32_t RtcGetMinimumTimeout( void );
68 
69 /*!
70  * \brief converts time in ms to time in ticks
71  *
72  * \param[IN] milliseconds Time in milliseconds
73  * \retval returns time in timer ticks
74  */
75 uint32_t RtcMs2Tick( TimerTime_t milliseconds );
76 
77 /*!
78  * \brief converts time in ticks to time in ms
79  *
80  * \param[IN] time in timer ticks
81  * \retval returns time in milliseconds
82  */
83 TimerTime_t RtcTick2Ms( uint32_t tick );
84 
85 /*!
86  * \brief Performs a delay of milliseconds by polling RTC
87  *
88  * \param[IN] milliseconds Delay in ms
89  */
90 void RtcDelayMs( TimerTime_t milliseconds );
91 
92 /*!
93  * \brief Sets the alarm
94  *
95  * \note The alarm is set at now (read in this funtion) + timeout
96  *
97  * \param timeout [IN] Duration of the Timer ticks
98  */
99 void RtcSetAlarm( uint32_t timeout );
100 
101 /*!
102  * \brief Stops the Alarm
103  */
104 void RtcStopAlarm( void );
105 
106 /*!
107  * \brief Starts wake up alarm
108  *
109  * \note  Alarm in RtcTimerContext.Time + timeout
110  *
111  * \param [IN] timeout Timeout value in ticks
112  */
113 void RtcStartAlarm( uint32_t timeout );
114 
115 /*!
116  * \brief Sets the RTC timer reference
117  *
118  * \retval value Timer reference value in ticks
119  */
120 uint32_t RtcSetTimerContext( void );
121 
122 /*!
123  * \brief Gets the RTC timer reference
124  *
125  * \retval value Timer value in ticks
126  */
127 uint32_t RtcGetTimerContext( void );
128 
129 /*!
130  * \brief Gets the system time with the number of seconds elapsed since epoch
131  *
132  * \param [OUT] milliseconds Number of milliseconds elapsed since epoch
133  * \retval seconds Number of seconds elapsed since epoch
134  */
135 uint32_t RtcGetCalendarTime( uint16_t *milliseconds );
136 
137 /*!
138  * \brief Get the RTC timer value
139  *
140  * \retval RTC Timer value
141  */
142 uint32_t RtcGetTimerValue( void );
143 
144 /*!
145  * \brief Get the RTC timer elapsed time since the last Alarm was set
146  *
147  * \retval RTC Elapsed time since the last alarm in ticks.
148  */
149 uint32_t RtcGetTimerElapsedTime( void );
150 
151 /*!
152  * \brief Writes data0 and data1 to the RTC backup registers
153  *
154  * \param [IN] data0 1st Data to be written
155  * \param [IN] data1 2nd Data to be written
156  */
157 void RtcBkupWrite( uint32_t data0, uint32_t data1 );
158 
159 /*!
160  * \brief Reads data0 and data1 from the RTC backup registers
161  *
162  * \param [OUT] data0 1st Data to be read
163  * \param [OUT] data1 2nd Data to be read
164  */
165 void RtcBkupRead( uint32_t* data0, uint32_t* data1 );
166 
167 /*!
168  * \brief Processes pending timer events
169  */
170 void RtcProcess( void );
171 
172 /*!
173  * \brief Computes the temperature compensation for a period of time on a
174  *        specific temperature.
175  *
176  * \param [IN] period Time period to compensate in milliseconds
177  * \param [IN] temperature Current temperature
178  *
179  * \retval Compensated time period
180  */
181 TimerTime_t RtcTempCompensation( TimerTime_t period, float temperature );
182 
183 #ifdef __cplusplus
184 }
185 #endif
186 
187 #endif // __RTC_BOARD_H__
188