BatteryMonitor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-2023, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  * @file BatteryMonitor.h
34  *
35  * @brief Battery Monitor driver
36  *
37  * @anchor ti_drivers_BatteryMonitor_Overview
38  * # Overview #
39  * The Battery Monitor driver provides services related to measuring and
40  * reacting to the current supply voltage of the device, and changes to it.
41  *
42  * The two main services provided are:
43  * - Getting the current supply voltage
44  * - Providing notification callbacks when the supply voltage changes
45  *
46  * @anchor ti_drivers_BatteryMonitor_Usage
47  * # Usage #
48  *
49  * ## Initialisation #
50  * Unlike most drivers, there is only a single instance of the Battery Monitor
51  * driver that is always available once #BatteryMonitor_init() is called.
52  * #BatteryMonitor_init() should be called once before using other Battery
53  * Monitor driver APIs. Subsequent #BatteryMonitor_init() calls will have no
54  * effect.
55  *
56  * ## Getting the Current Supply Voltage #
57  * The driver can read the current supply voltage and return it. The resolution
58  * is device-specific (see device-specific Battery Monitor documentation), but
59  * the voltage will always be encoded as an unsigned integer in millivolts
60  * (mV).
61  *
62  * ## Notifications #
63  * The Battery Monitor driver can notify the application when the supply
64  * voltage crosses an application-defined threshold.
65  *
66  * There are three default use cases for this:
67  * - High threshold.
68  * The application will receive a notification callback when
69  * currentVoltage >= thresholdHigh.
70  * - Low threshold.
71  * The application will receive a notification callback when
72  * currentVoltage <= thresholdLow.
73  * - Range threshold.
74  * The application will receive a notification callback when
75  * currentVoltage >= thresholdHigh || currentVoltage <=
76  * thresholdLow. This setup addresses use cases
77  * where a notification is required when the supply voltage changes by a
78  * certain amount regardless of whether it is up or down.
79  *
80  * ### Registering Notifications
81  * There are three functions that register a notification for the application:
82  * - #BatteryMonitor_registerNotifyHigh()
83  * - #BatteryMonitor_registerNotifyLow()
84  * - #BatteryMonitor_registerNotifyRange()
85  *
86  * Multiple notifications may be registered. The different parts of the
87  * application and drivers that need to respond to a supply voltage change do
88  * not need to know of one another.
89  * Each notification must have its own #BatteryMonitor_NotifyObj and must be
90  * registered individually.
91  *
92  * ### Notification Callbacks
93  * Once the supply voltage crosses the smallest "high threshold" or largest
94  * "low threshold amongst the registered notifications, the driver will
95  * iterate over the entire list of registered notification and check which
96  * ones have triggered. Notifications that have triggered are removed from the
97  * list of registered notifications before their callback function is invoked.
98  *
99  * If an application wishes to re-register a notification that just triggered
100  * and was unregistered, it may register it again from within the notification
101  * callback or another context.
102  *
103  * It is possible to determine whether the high or low threshold triggered
104  * the notification callback as follows:
105  * - currentVoltage <= thresholdVoltage: Low threshold triggered
106  * - currentVoltage >= thresholdVoltage: High threshold triggered
107  * This information is only reasonably useful when registering a notification
108  * with both a high and low threshold using
109  * #BatteryMonitor_registerNotifyRange(). Even then, the expected basic use
110  * case only cares about the current voltage and adding an offset to it when
111  * registering the notification again.
112  *
113  * ### Unregistering Notifications
114  * Registered notifications are unregistered in two ways:
115  * - Automatically when a notification triggers
116  * - By calling #BatteryMonitor_unregisterNotify()
117  *
118  * Unregistered notifications may be registered again at any time.
119  *
120  * @anchor ti_drivers_BatteryMonitor_Synopsis
121  * # Synopsis #
122  * @anchor ti_drivers_BatteryMonitor_Synopsis_Code
123  * @code
124  * #include <ti/drivers/BatteryMonitor.h>
125  *
126  * #define WINDOW_DELTA_MILLIVOLT 300
127  *
128  * BatteryMonitor_init();
129  *
130  * currentVoltage = BatteryMonitor_getVoltage();
131  *
132  * result = BatteryMonitor_registerNotifyRange(&notifyObject,
133  * currentVoltage + WINDOW_DELTA_MILLIVOLT,
134  * currentVoltage - WINDOW_DELTA_MILLIVOLT,
135  * myNotifyFxn,
136  * clientArg);
137  * @endcode
138  *
139  * @anchor ti_drivers_BatteryMonitor_Examples
140  * # Examples #
141  *
142  * ## Register a High Threshold Notification #
143  *
144  * @code
145  *
146  * // The notification will trigger when the supply voltage reaches 3.5V
147  * #define THRESHOLD_CUTOFF_MILLIVOLT 3500
148  *
149  * #include <ti/drivers/BatteryMonitor.h>
150  *
151  * void thresholdNotifyFxn(uint16_t currentVoltage,
152  * uint16_t thresholdVoltage,
153  * uintptr_t clientArg,
154  * BatteryMonitor_NotifyObj *notifyObject) {
155  * // Post a semaphore, set a flag, or otherwise act upon the voltage change.
156  * }
157  *
158  * ...
159  *
160  * // Initialize the Battery Monitor driver and register a notification.
161  *
162  * BatteryMonitor_init();
163  *
164  * int_fast16_t status = BatteryMonitor_registerNotifyHigh(notifyObject,
165  * THRESHOLD_CUTOFF_MILLIVOLT,
166  * thresholdNotifyFxn,
167  * NULL);
168  *
169  * if (status != BatteryMonitor_STATUS_SUCCESS) {
170  * // Handle error
171  * }
172  *
173  * @endcode
174  *
175  * ## Register a Range Threshold Notification and re-register in Callback #
176  *
177  * @code
178  *
179  * #define THRESHOLD_DELTA_MILLIVOLT 300
180  *
181  * #include <ti/drivers/BatteryMonitor.h>
182  *
183  *
184  * void deltaNotificationFxn(uint16_t currentVoltage,
185  * uint16_t thresholdVoltage,
186  * uintptr_t clientArg,
187  * BatteryMonitor_NotifyObj *notifyObject) {
188  * int_fast16_t status;
189  *
190  * status = BatteryMonitor_registerNotifyRange(notifyObject,
191  * currentVoltage + THRESHOLD_DELTA_MILLIVOLT,
192  * currentVoltage - THRESHOLD_DELTA_MILLIVOLT,
193  * deltaNotificationFxn,
194  * clientArg);
195  *
196  * if (status != BatteryMonitor_STATUS_SUCCESS) {
197  * while(1);
198  * }
199  * }
200  *
201  * ...
202  *
203  * // Initialize the Battery Monitor driver and register a notification.
204  *
205  * BatteryMonitor_init();
206  *
207  * BatteryMonitor_NotifyObj rangeNotifyObject;
208  *
209  * uint16_t currentVoltage = BatteryMonitor_getVoltage();
210  *
211  * int_fast16_t status = BatteryMonitor_registerNotifyRange(6rangeNotifyObject,
212  * currentVoltage + THRESHOLD_DELTA_MILLIVOLT,
213  * currentVoltage - THRESHOLD_DELTA_MILLIVOLT,
214  * deltaNotificationFxn,
215  * (uintptr_t)NULL);
216  * @endcode
217  */
218 
219 #ifndef ti_drivers_BatteryMonitor__include
220 #define ti_drivers_BatteryMonitor__include
221 
222 #include <stdbool.h>
223 #include <stddef.h>
224 #include <stdint.h>
225 
226 #include <ti/drivers/utils/List.h>
227 
228 #ifdef __cplusplus
229 extern "C" {
230 #endif
231 
244 #define BatteryMonitor_STATUS_RESERVED (-32)
245 
252 #define BatteryMonitor_STATUS_SUCCESS (0)
253 
260 #define BatteryMonitor_STATUS_ERROR (-1)
261 
262 /* @cond NODOC
263  *
264  * Type declaration for the notification object made separately from the
265  * struct definition because of the circular dependency between
266  * #BatteryMonitor_NotifyFxn() and #BatteryMonitor_NotifyObj.
267  */
269 /* @endcond */
270 
288 typedef void (*BatteryMonitor_NotifyFxn)(uint16_t currentVoltage,
289  uint16_t thresholdVoltage,
290  uintptr_t clientArg,
291  BatteryMonitor_NotifyObj *notifyObject);
292 
303 {
306  uint16_t thresholdHigh;
307  uint16_t thresholdLow;
308  uintptr_t clientArg;
310 };
311 
319 void BatteryMonitor_init(void);
320 
326 uint16_t BatteryMonitor_getVoltage(void);
327 
357  uint16_t thresholdHigh,
359  uintptr_t clientArg);
360 
388  uint16_t thresholdLow,
390  uintptr_t clientArg);
391 
425  uint16_t thresholdHigh,
426  uint16_t thresholdLow,
428  uintptr_t clientArg);
429 
446 
461 static inline uint16_t BatteryMonitor_getThresholdHigh(BatteryMonitor_NotifyObj *notifyObject)
462 {
463  return notifyObject->thresholdHigh;
464 }
465 
480 static inline uint16_t BatteryMonitor_getThresholdLow(BatteryMonitor_NotifyObj *notifyObject)
481 {
482  return notifyObject->thresholdLow;
483 }
484 
505  uint16_t *thresholdHigh,
506  uint16_t *thresholdLow)
507 {
508  *thresholdHigh = notifyObject->thresholdHigh;
509  *thresholdLow = notifyObject->thresholdLow;
510 }
511 
522 static inline uintptr_t BatteryMonitor_getClientArg(BatteryMonitor_NotifyObj *notifyObject)
523 {
524  return notifyObject->clientArg;
525 }
526 
538 {
539  return notifyObject->notifyFxn;
540 }
541 
542 #ifdef __cplusplus
543 }
544 #endif
545 
546 #endif /* ti_drivers_BatteryMonitor__include */
static void BatteryMonitor_getThresholdRange(BatteryMonitor_NotifyObj *notifyObject, uint16_t *thresholdHigh, uint16_t *thresholdLow)
Get the high and low threshold of a notification.
Definition: BatteryMonitor.h:504
static BatteryMonitor_NotifyFxn BatteryMonitor_getNotifyFxn(BatteryMonitor_NotifyObj *notifyObject)
Get the notifyFxn provided during registration.
Definition: BatteryMonitor.h:537
uint16_t thresholdHigh
Definition: BatteryMonitor.h:306
static uintptr_t BatteryMonitor_getClientArg(BatteryMonitor_NotifyObj *notifyObject)
Get the application-provided clientArg of a notification.
Definition: BatteryMonitor.h:522
static uint16_t BatteryMonitor_getThresholdLow(BatteryMonitor_NotifyObj *notifyObject)
Get the low threshold of a notification.
Definition: BatteryMonitor.h:480
int_fast16_t BatteryMonitor_registerNotifyRange(BatteryMonitor_NotifyObj *notifyObject, uint16_t thresholdHigh, uint16_t thresholdLow, BatteryMonitor_NotifyFxn notifyFxn, uintptr_t clientArg)
Registers a notification with both a high and low threshold.
void(* BatteryMonitor_NotifyFxn)(uint16_t currentVoltage, uint16_t thresholdVoltage, uintptr_t clientArg, BatteryMonitor_NotifyObj *notifyObject)
Function prototype for a notification callback.
Definition: BatteryMonitor.h:288
int_fast16_t BatteryMonitor_registerNotifyLow(BatteryMonitor_NotifyObj *notifyObject, uint16_t thresholdLow, BatteryMonitor_NotifyFxn notifyFxn, uintptr_t clientArg)
Registers a notification with a low threshold.
int_fast16_t BatteryMonitor_registerNotifyHigh(BatteryMonitor_NotifyObj *notifyObject, uint16_t thresholdHigh, BatteryMonitor_NotifyFxn notifyFxn, uintptr_t clientArg)
Registers a notification with a high threshold.
uint16_t thresholdLow
Definition: BatteryMonitor.h:307
void BatteryMonitor_init(void)
This function initializes the Battery Monitor driver.
uint16_t BatteryMonitor_getVoltage(void)
Gets the current supply voltage in millivolts.
uintptr_t clientArg
Definition: BatteryMonitor.h:308
Definition: List.h:126
List_Elem link
Definition: BatteryMonitor.h:304
Battery Monitor notify object structure.
Definition: BatteryMonitor.h:302
bool isRegistered
Definition: BatteryMonitor.h:309
BatteryMonitor_NotifyFxn notifyFxn
Definition: BatteryMonitor.h:305
int_fast16_t BatteryMonitor_unregisterNotify(BatteryMonitor_NotifyObj *notifyObject)
Unregisters a currently registered notification.
Linked List interface for use in drivers.
static uint16_t BatteryMonitor_getThresholdHigh(BatteryMonitor_NotifyObj *notifyObject)
Get the high threshold of a notification.
Definition: BatteryMonitor.h:461
© Copyright 1995-2023, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale