1 /******************************************************************************
2 *  Filename:       aon_batmon.h
3 *  Revised:        2015-07-16 12:12:04 +0200 (Thu, 16 Jul 2015)
4 *  Revision:       44151
5 *
6 *  Description:    Defines and prototypes for the AON Battery and Temperature
7 *                  Monitor
8 *
9 *  Copyright (c) 2015, 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 <driverlib/debug.h>
69 
70 
71 //*****************************************************************************
72 //
73 // API Functions and prototypes
74 //
75 //*****************************************************************************
76 
77 //*****************************************************************************
78 //
79 //! \brief Enable the temperature and battery monitoring.
80 //!
81 //! This function will enable the measurements of the temperature and the
82 //! battery voltage.
83 //!
84 //! To speed up the measurement of the levels the measurement can be enabled
85 //! before configuring the battery and temperature settings. When all of the
86 //! AON_BATMON registers are configured, the calculation of the voltage and
87 //! temperature values can be enabled (the measurement will now take
88 //! effect/propagate to other blocks).
89 //!
90 //! It is possible to enable both at the same time, after the AON_BATMON
91 //! registers are configured, but then the first values will be ready at a
92 //! later point compared to the scenario above.
93 //!
94 //! \note Temperature and battery voltage measurements are not done in
95 //! parallel. The measurement cycle is controlled by a hardware Finite State
96 //! Machine. First the temperature and then the battery voltage each taking
97 //! one cycle to complete. However, if the comparator measuring the battery
98 //! voltage detects a change on the reference value, a new measurement of the
99 //! battery voltage only is performed immediately after. This has no impact on
100 //! the cycle count.
101 //!
102 //! \return None
103 //
104 //*****************************************************************************
105 __STATIC_INLINE void
AONBatMonEnable(void)106 AONBatMonEnable(void)
107 {
108     //
109     // Enable the measurements.
110     //
111     HWREG(AON_BATMON_BASE + AON_BATMON_O_CTL) =
112         AON_BATMON_CTL_CALC_EN |
113         AON_BATMON_CTL_MEAS_EN;
114 }
115 
116 //*****************************************************************************
117 //
118 //! \brief Disable the temperature and battery monitoring.
119 //!
120 //! This function will disable the measurements of the temperature and the
121 //! battery voltage.
122 //!
123 //! \return None
124 //
125 //*****************************************************************************
126 __STATIC_INLINE void
AONBatMonDisable(void)127 AONBatMonDisable(void)
128 {
129     //
130     // Disable the measurements.
131     //
132     HWREG(AON_BATMON_BASE + AON_BATMON_O_CTL) = 0;
133 }
134 
135 
136 //*****************************************************************************
137 //
138 //! \brief Get the current temperature measurement as a signed value in Deg Celsius.
139 //!
140 //! This function returns an calibrated and rounded value in degree Celsius.
141 //! The temperature measurements are updated every cycle.
142 //!
143 //! \note The temperature drifts slightly depending on the battery voltage.
144 //! This function compensates for this drift and returns a calibrated temperature.
145 //!
146 //! \note Use the function AONBatMonNewTempMeasureReady() to test for a new measurement.
147 //!
148 //! \return Returns signed integer part of temperature in Deg C (-256 .. +255)
149 //!
150 //! \sa AONBatMonNewTempMeasureReady()
151 //
152 //*****************************************************************************
153 int32_t
154 AONBatMonTemperatureGetDegC( void );
155 
156 //*****************************************************************************
157 //
158 //! \brief Get the battery monitor measurement.
159 //!
160 //! This function will return the current battery monitor measurement.
161 //! The battery voltage measurements are updated every cycle.
162 //!
163 //! \note The returned value is NOT sign-extended!
164 //!
165 //! \note Use the function \ref AONBatMonNewBatteryMeasureReady() to test for
166 //! a change in measurement.
167 //!
168 //! \return Returns the current battery monitor value of the battery voltage
169 //! measurement in a <int.frac> format size <3.8> in units of volt.
170 //!
171 //! \sa AONBatMonNewBatteryMeasureReady()
172 //
173 //*****************************************************************************
174 __STATIC_INLINE uint32_t
AONBatMonBatteryVoltageGet(void)175 AONBatMonBatteryVoltageGet(void)
176 {
177     uint32_t ui32CurrentBattery;
178 
179     ui32CurrentBattery = HWREG(AON_BATMON_BASE + AON_BATMON_O_BAT);
180 
181     //
182     // Return the current battery voltage measurement.
183     //
184     return (ui32CurrentBattery >> AON_BATMON_BAT_FRAC_S);
185 }
186 
187 //*****************************************************************************
188 //
189 //! \brief Check if battery monitor measurement has changed.
190 //!
191 //! This function checks if a new battery monitor value is available. If the
192 //! measurement value has \b changed since last clear the function returns \c true.
193 //!
194 //! If the measurement has changed the function will automatically clear the
195 //! status bit.
196 //!
197 //! \note It is always possible to read out the current value of the
198 //! battery level using AONBatMonBatteryVoltageGet() but this function can be
199 //! used to check if the measurement has changed.
200 //!
201 //! \return Returns \c true if the measurement value has changed and \c false
202 //! otherwise.
203 //!
204 //! \sa AONBatMonNewTempMeasureReady(), AONBatMonBatteryVoltageGet()
205 //
206 //*****************************************************************************
207 __STATIC_INLINE bool
AONBatMonNewBatteryMeasureReady(void)208 AONBatMonNewBatteryMeasureReady(void)
209 {
210     bool bStatus;
211 
212     //
213     // Check the status bit.
214     //
215     bStatus = HWREG(AON_BATMON_BASE + AON_BATMON_O_BATUPD) &
216               AON_BATMON_BATUPD_STAT ? true : false;
217 
218     //
219     // Clear status bit if set.
220     //
221     if(bStatus)
222     {
223         HWREG(AON_BATMON_BASE + AON_BATMON_O_BATUPD) = 1;
224     }
225 
226     //
227     // Return status.
228     //
229     return (bStatus);
230 }
231 
232 //*****************************************************************************
233 //
234 //! \brief Check if temperature monitor measurement has changed.
235 //!
236 //! This function checks if a new temperature value is available. If the
237 //! measurement value has \b changed since last clear the function returns \c true.
238 //!
239 //! If the measurement has changed the function will automatically clear the
240 //! status bit.
241 //!
242 //! \note It is always possible to read out the current value of the
243 //! temperature using \ref AONBatMonTemperatureGetDegC()
244 //! but this function can be used to check if the measurement has changed.
245 //!
246 //! \return Returns \c true if the measurement value has changed and \c false
247 //! otherwise.
248 //!
249 //! \sa AONBatMonNewBatteryMeasureReady(), AONBatMonTemperatureGetDegC()
250 //
251 //*****************************************************************************
252 __STATIC_INLINE bool
AONBatMonNewTempMeasureReady(void)253 AONBatMonNewTempMeasureReady(void)
254 {
255     bool bStatus;
256 
257     //
258     // Check the status bit.
259     //
260     bStatus = HWREG(AON_BATMON_BASE + AON_BATMON_O_TEMPUPD) &
261               AON_BATMON_TEMPUPD_STAT ? true : false;
262 
263     //
264     // Clear status bit if set.
265     //
266     if(bStatus)
267     {
268         HWREG(AON_BATMON_BASE + AON_BATMON_O_TEMPUPD) = 1;
269     }
270 
271     //
272     // Return status.
273     //
274     return (bStatus);
275 }
276 
277 //*****************************************************************************
278 //
279 // Mark the end of the C bindings section for C++ compilers.
280 //
281 //*****************************************************************************
282 #ifdef __cplusplus
283 }
284 #endif
285 
286 #endif //  __AON_BATMON_H__
287 
288 //*****************************************************************************
289 //
290 //! Close the Doxygen group.
291 //! @}
292 //! @}
293 //
294 //*****************************************************************************
295