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