1 /******************************************************************************
2 *  Filename:       aon_batmon.h
3 *  Revised:        2016-10-06 17:21:09 +0200 (Thu, 06 Oct 2016)
4 *  Revision:       47343
5 *
6 *  Description:    Defines and prototypes for the AON Battery and Temperature
7 *                  Monitor
8 *
9 *  Copyright (c) 2015 - 2017, Texas Instruments Incorporated
10 *  All rights reserved.
11 *
12 *  Redistribution and use in source and binary forms, with or without
13 *  modification, are permitted provided that the following conditions are met:
14 *
15 *  1) Redistributions of source code must retain the above copyright notice,
16 *     this list of conditions and the following disclaimer.
17 *
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 *
22 *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
23 *     be used to endorse or promote products derived from this software without
24 *     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
29 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
30 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 *  POSSIBILITY OF SUCH DAMAGE.
37 *
38 ******************************************************************************/
39 
40 //*****************************************************************************
41 //
42 //! \addtogroup aon_group
43 //! @{
44 //! \addtogroup aonbatmon_api
45 //! @{
46 //
47 //*****************************************************************************
48 
49 #ifndef __AON_BATMON_H__
50 #define __AON_BATMON_H__
51 
52 //*****************************************************************************
53 //
54 // If building with a C++ compiler, make all of the definitions in this header
55 // have a C binding.
56 //
57 //*****************************************************************************
58 #ifdef __cplusplus
59 extern "C"
60 {
61 #endif
62 
63 #include <stdbool.h>
64 #include <stdint.h>
65 #include "../inc/hw_types.h"
66 #include "../inc/hw_memmap.h"
67 #include "../inc/hw_aon_batmon.h"
68 #include "debug.h"
69 
70 //*****************************************************************************
71 //
72 // Support for DriverLib in ROM:
73 // This section renames all functions that are not "static inline", so that
74 // calling these functions will default to implementation in flash. At the end
75 // of this file a second renaming will change the defaults to implementation in
76 // ROM for available functions.
77 //
78 // To force use of the implementation in flash, e.g. for debugging:
79 // - Globally: Define DRIVERLIB_NOROM at project level
80 // - Per function: Use prefix "NOROM_" when calling the function
81 //
82 //*****************************************************************************
83 #if !defined(DOXYGEN)
84     #define AONBatMonTemperatureGetDegC     NOROM_AONBatMonTemperatureGetDegC
85 #endif
86 
87 
88 //*****************************************************************************
89 //
90 // API Functions and prototypes
91 //
92 //*****************************************************************************
93 
94 //*****************************************************************************
95 //
96 //! \brief Enable the temperature and battery monitoring.
97 //!
98 //! This function will enable the measurements of the temperature and the
99 //! battery voltage.
100 //!
101 //! To speed up the measurement of the levels the measurement can be enabled
102 //! before configuring the battery and temperature settings. When all of the
103 //! AON_BATMON registers are configured, the calculation of the voltage and
104 //! temperature values can be enabled (the measurement will now take
105 //! effect/propagate to other blocks).
106 //!
107 //! It is possible to enable both at the same time, after the AON_BATMON
108 //! registers are configured, but then the first values will be ready at a
109 //! later point compared to the scenario above.
110 //!
111 //! \note Temperature and battery voltage measurements are not done in
112 //! parallel. The measurement cycle is controlled by a hardware Finite State
113 //! Machine. First the temperature and then the battery voltage each taking
114 //! one cycle to complete. However, if the comparator measuring the battery
115 //! voltage detects a change on the reference value, a new measurement of the
116 //! battery voltage only is performed immediately after. This has no impact on
117 //! the cycle count.
118 //!
119 //! \return None
120 //
121 //*****************************************************************************
122 __STATIC_INLINE void
AONBatMonEnable(void)123 AONBatMonEnable(void)
124 {
125     // Enable the measurements.
126     HWREG(AON_BATMON_BASE + AON_BATMON_O_CTL) =
127         AON_BATMON_CTL_CALC_EN |
128         AON_BATMON_CTL_MEAS_EN;
129 }
130 
131 //*****************************************************************************
132 //
133 //! \brief Disable the temperature and battery monitoring.
134 //!
135 //! This function will disable the measurements of the temperature and the
136 //! battery voltage.
137 //!
138 //! \return None
139 //
140 //*****************************************************************************
141 __STATIC_INLINE void
AONBatMonDisable(void)142 AONBatMonDisable(void)
143 {
144     // Disable the measurements.
145     HWREG(AON_BATMON_BASE + AON_BATMON_O_CTL) = 0;
146 }
147 
148 
149 //*****************************************************************************
150 //
151 //! \brief Get the current temperature measurement as a signed value in Deg Celsius.
152 //!
153 //! This function returns an calibrated and rounded value in degree Celsius.
154 //! The temperature measurements are updated every cycle.
155 //!
156 //! \note The temperature drifts slightly depending on the battery voltage.
157 //! This function compensates for this drift and returns a calibrated temperature.
158 //!
159 //! \note Use the function AONBatMonNewTempMeasureReady() to test for a new measurement.
160 //!
161 //! \return Returns signed integer part of temperature in Deg C (-256 .. +255)
162 //!
163 //! \sa AONBatMonNewTempMeasureReady()
164 //
165 //*****************************************************************************
166 extern int32_t AONBatMonTemperatureGetDegC( void );
167 
168 //*****************************************************************************
169 //
170 //! \brief Get the battery monitor measurement.
171 //!
172 //! This function will return the current battery monitor measurement.
173 //! The battery voltage measurements are updated every cycle.
174 //!
175 //! \note The returned value is NOT sign-extended!
176 //!
177 //! \note Use the function \ref AONBatMonNewBatteryMeasureReady() to test for
178 //! a change in measurement.
179 //!
180 //! \return Returns the current battery monitor value of the battery voltage
181 //! measurement in a <int.frac> format size <3.8> in units of volt.
182 //!
183 //! \sa AONBatMonNewBatteryMeasureReady()
184 //
185 //*****************************************************************************
186 __STATIC_INLINE uint32_t
AONBatMonBatteryVoltageGet(void)187 AONBatMonBatteryVoltageGet(void)
188 {
189     uint32_t ui32CurrentBattery;
190 
191     ui32CurrentBattery = HWREG(AON_BATMON_BASE + AON_BATMON_O_BAT);
192 
193     // Return the current battery voltage measurement.
194     return (ui32CurrentBattery >> AON_BATMON_BAT_FRAC_S);
195 }
196 
197 //*****************************************************************************
198 //
199 //! \brief Check if battery monitor measurement has changed.
200 //!
201 //! This function checks if a new battery monitor value is available. If the
202 //! measurement value has \b changed since last clear the function returns \c true.
203 //!
204 //! If the measurement has changed the function will automatically clear the
205 //! status bit.
206 //!
207 //! \note It is always possible to read out the current value of the
208 //! battery level using AONBatMonBatteryVoltageGet() but this function can be
209 //! used to check if the measurement has changed.
210 //!
211 //! \return Returns \c true if the measurement value has changed and \c false
212 //! otherwise.
213 //!
214 //! \sa AONBatMonNewTempMeasureReady(), AONBatMonBatteryVoltageGet()
215 //
216 //*****************************************************************************
217 __STATIC_INLINE bool
AONBatMonNewBatteryMeasureReady(void)218 AONBatMonNewBatteryMeasureReady(void)
219 {
220     bool bStatus;
221 
222     // Check the status bit.
223     bStatus = HWREG(AON_BATMON_BASE + AON_BATMON_O_BATUPD) &
224               AON_BATMON_BATUPD_STAT ? true : false;
225 
226     // Clear status bit if set.
227     if(bStatus)
228     {
229         HWREG(AON_BATMON_BASE + AON_BATMON_O_BATUPD) = 1;
230     }
231 
232     // Return status.
233     return (bStatus);
234 }
235 
236 //*****************************************************************************
237 //
238 //! \brief Check if temperature monitor measurement has changed.
239 //!
240 //! This function checks if a new temperature value is available. If the
241 //! measurement value has \b changed since last clear the function returns \c true.
242 //!
243 //! If the measurement has changed the function will automatically clear the
244 //! status bit.
245 //!
246 //! \note It is always possible to read out the current value of the
247 //! temperature using \ref AONBatMonTemperatureGetDegC()
248 //! but this function can be used to check if the measurement has changed.
249 //!
250 //! \return Returns \c true if the measurement value has changed and \c false
251 //! otherwise.
252 //!
253 //! \sa AONBatMonNewBatteryMeasureReady(), AONBatMonTemperatureGetDegC()
254 //
255 //*****************************************************************************
256 __STATIC_INLINE bool
AONBatMonNewTempMeasureReady(void)257 AONBatMonNewTempMeasureReady(void)
258 {
259     bool bStatus;
260 
261     // Check the status bit.
262     bStatus = HWREG(AON_BATMON_BASE + AON_BATMON_O_TEMPUPD) &
263               AON_BATMON_TEMPUPD_STAT ? true : false;
264 
265     // Clear status bit if set.
266     if(bStatus)
267     {
268         HWREG(AON_BATMON_BASE + AON_BATMON_O_TEMPUPD) = 1;
269     }
270 
271     // Return status.
272     return (bStatus);
273 }
274 
275 //*****************************************************************************
276 //
277 // Support for DriverLib in ROM:
278 // Redirect to implementation in ROM when available.
279 //
280 //*****************************************************************************
281 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
282     #include "../driverlib/rom.h"
283     #ifdef ROM_AONBatMonTemperatureGetDegC
284         #undef  AONBatMonTemperatureGetDegC
285         #define AONBatMonTemperatureGetDegC     ROM_AONBatMonTemperatureGetDegC
286     #endif
287 #endif
288 
289 //*****************************************************************************
290 //
291 // Mark the end of the C bindings section for C++ compilers.
292 //
293 //*****************************************************************************
294 #ifdef __cplusplus
295 }
296 #endif
297 
298 #endif //  __AON_BATMON_H__
299 
300 //*****************************************************************************
301 //
302 //! Close the Doxygen group.
303 //! @}
304 //! @}
305 //
306 //*****************************************************************************
307