Temperature.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2021, 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 Temperature.h
34  *
35  * @brief Temperature driver
36  *
37  * @anchor ti_drivers_Temperature_Overview
38  * # Overview #
39  * The Temperature driver provides services related to measuring and reacting
40  * to the current temperature of the chip and changes to it.
41  *
42  * The two main services provided are:
43  * - Getting the current temperature
44  * - Providing notification callbacks when the temperature changes
45  *
46  * @anchor ti_drivers_Temperature_Usage
47  * # Usage #
48  *
49  * ## Initialisation #
50  * Unlike most drivers, there is only a single instance of the temperature
51  * driver that is always available once #Temperature_init() is called.
52  * #Temperature_init() should be called once before using other Temperature
53  * driver APIs. Subsequent #Temperature_init() calls will have no effect.
54  *
55  * ## Getting the Current Temperature #
56  * The most basic function of the driver is to provide the current temperature
57  * and return it. It is encoded as a signed integer in degrees C.
58  *
59  * ## Notifications #
60  * The other major function of the Temperature driver is to notify the
61  * application when the temperature changes and crosses an application-defined
62  * threshold.
63  *
64  * There are three default usecases for this:
65  * - High threshold.
66  * The application will receive a notification callback when
67  * currentTemperature >= thresholdHigh.
68  * - Low threshold.
69  * The application will receive a notification callback when
70  * currentTemperature <= thresholdLow.
71  * - Range threshold.
72  * The application will receive a notification callback when
73  * currentTemperature >= thresholdHigh || currentTemperature <=
74  * thresholdLow. This setup addresses usecases
75  * where a notification is required when the temperature changes by a
76  * certain amount regardless of whether it is up or down. Adjusting
77  * clock offsets based on temperature is a good example of this.
78  *
79  * ### Registering Notifications
80  * There are three functions that register a notification for the application:
81  * - #Temperature_registerNotifyHigh()
82  * - #Temperature_registerNotifyLow()
83  * - #Temperature_registerNotifyRange()
84  *
85  * Multiple notifications may be registered. The different parts of the
86  * application and drivers that need to respond to a temperature change do not
87  * need to know of one another.
88  * Each notification must have its own #Temperature_NotifyObj and must be
89  * registered individually.
90  *
91  * ### Notification Callbacks
92  * Once the chip temperature crosses the smallest high threshold or largest
93  * low threshold amongst the registered notifications, the driver will
94  * iterate over the entire list of registered notification and check which
95  * ones have triggered. Notifications that have triggered are removed from
96  * the list of registered notifications and thus are no longer registered.
97  * Their callback function is then invoked.
98  *
99  * If an application wishes to reregister 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  * - currentTemperature <= thresholdTemperature: Low threshold triggered
106  * - currentTemperature >= thresholdTemperature: High threshold triggered
107  * This information is only reasonably useful when registering a notification
108  * with both a high and low threshold using #Temperature_registerNotifyRange().
109  * Even then, the expected basic usecase only cares about the current
110  * temperature and adding an offset to it when registering the notification
111  * again.
112  *
113  * ### Unregistering Notifications
114  * Registered notifications are unregistered in two ways:
115  * - Automatically when a notification triggers
116  * - By calling #Temperature_unregisterNotify()
117  *
118  * Unregistered notifications may be registered again at any time.
119  *
120  * # Measured vs True Temperature
121  * While the driver aims to supply and act on an accurate absolute temperature,
122  * there will be differences between the measured vs the true temperature due
123  * to inherent variances in the manufacturing process. The nature of these
124  * differences varies by device family.
125  *
126  * Examples of such differences:
127  * - A constant per-chip offset between the measured and the true
128  * temperature
129  * - An temperature dependent per-chip offset between the measured and the
130  * true temperature
131  * - A variance in the measured temperature when measuring multiple times
132  * at the same chip temperature
133  *
134  * It is strongly recommended to read the device-specific Temperature driver
135  * documentation for details of the temperature sensor characteristics and
136  * how they might affect choices of threshold values.
137  *
138  * @anchor ti_drivers_Temperature_Synopsis
139  * # Synopsis #
140  * @anchor ti_drivers_Temperature_Synopsis_Code
141  * @code
142  * #include <ti/drivers/Temperature.h>
143  *
144  * #define WINDOW_DELTA 10
145  *
146  * Temperature_init();
147  *
148  * currentTemperature = Temperature_getTemperature();
149  *
150  * result = Temperature_registerNotifyRange(&notifyObject,
151  * currentTemperature + WINDOW_DELTA,
152  * currentTemperature - WINDOW_DELTA,
153  * myNotifyFxn,
154  * clientArg);
155  * @endcode
156  *
157  * @anchor ti_drivers_Temperature_Examples
158  * # Examples #
159  *
160  * ## Register a High Threshold Notification #
161  *
162  * @code
163  *
164  * // The notification will trigger when the temperature reaches 40 C
165  * #define THRESHOLD_CUTOFF 40
166  *
167  * #include <ti/drivers/Temperature.h>
168  *
169  * void thresholdNotifyFxn(int16_t currentTemperature,
170  * int16_t thresholdTemperature,
171  * uintptr_t clientArg,
172  * Temperature_NotifyObj *notifyObject) {
173  * // Post a semaphore, set a flag, or otherwise act upon the temperature
174  * // change.
175  * }
176  *
177  * ...
178  *
179  * // Initialize the Temperature driver and register a notification.
180  *
181  * Temperature_init();
182  *
183  * int_fast16_t status = Temperature_registerNotifyHigh(notifyObject,
184  * THRESHOLD_CUTOFF,
185  * thresholdNotifyFxn,
186  * NULL);
187  *
188  * if (status != Temperature_STATUS_SUCCESS) {
189  * // Handle error
190  * }
191  *
192  * @endcode
193  *
194  * ## Register a Range Threshold Notification and Reregister in Callback #
195  *
196  * @code
197  *
198  * #define THRESHOLD_DELTA 5
199  *
200  * #include <ti/drivers/Temperature.h>
201  *
202  *
203  * void deltaNotificationFxn(int16_t currentTemperature,
204  * int16_t thresholdTemperature,
205  * uintptr_t clientArg,
206  * Temperature_NotifyObj *notifyObject) {
207  * int_fast16_t status;
208  *
209  * status = Temperature_registerNotifyRange(notifyObject,
210  * currentTemperature + THRESHOLD_DELTA,
211  * currentTemperature - THRESHOLD_DELTA,
212  * deltaNotificationFxn,
213  * NULL);
214  *
215  * if (status != Temperature_STATUS_SUCCESS) {
216  * while(1);
217  * }
218  * }
219  *
220  * ...
221  *
222  * // Initialize the Temperature driver and register a notification.
223  *
224  * Temperature_init();
225  *
226  * int16_t currentTemperature = Temperature_getTemperature();
227  *
228  * int_fast16_t status = Temperature_registerNotifyRange(notifyObject,
229  * currentTemperature + THRESHOLD_DELTA,
230  * currentTemperature - THRESHOLD_DELTA,
231  * deltaNotificationFxn,
232  * NULL);
233  * @endcode
234  */
235 
236 #ifndef ti_drivers_Temperature__include
237 #define ti_drivers_Temperature__include
238 
239 #include <stdbool.h>
240 #include <stddef.h>
241 #include <stdint.h>
242 
243 #include <ti/drivers/utils/List.h>
244 
245 #ifdef __cplusplus
246 extern "C" {
247 #endif
248 
261 #define Temperature_STATUS_RESERVED (-32)
262 
269 #define Temperature_STATUS_SUCCESS (0)
270 
277 #define Temperature_STATUS_ERROR (-1)
278 
279 /* @cond
280  *
281  * Type declaration for the notification object made separately from the
282  * struct definition because of the circular dependency between
283  * #Temperature_NotifyFxn() and #Temperature_NotifyObj.
284  */
286 /* @endcond */
287 
306 typedef void (*Temperature_NotifyFxn)(int16_t currentTemperature,
307  int16_t thresholdTemperature,
308  uintptr_t clientArg,
309  Temperature_NotifyObj *notifyObject);
310 
321 {
324  int16_t thresholdHigh;
325  int16_t thresholdLow;
326  uintptr_t clientArg;
328 };
329 
337 void Temperature_init(void);
338 
344 int16_t Temperature_getTemperature(void);
345 
373 int_fast16_t Temperature_registerNotifyHigh(Temperature_NotifyObj *notifyObject,
374  int16_t thresholdHigh,
376  uintptr_t clientArg);
377 
405 int_fast16_t Temperature_registerNotifyLow(Temperature_NotifyObj *notifyObject,
406  int16_t thresholdLow,
408  uintptr_t clientArg);
409 
441 int_fast16_t Temperature_registerNotifyRange(Temperature_NotifyObj *notifyObject,
442  int16_t thresholdHigh,
443  int16_t thresholdLow,
445  uintptr_t clientArg);
446 
463 int_fast16_t Temperature_unregisterNotify(Temperature_NotifyObj *notifyObject);
464 
480 
496 
515 void Temperature_getThresholdRange(Temperature_NotifyObj *notifyObject, int16_t *thresholdHigh, int16_t *thresholdLow);
516 
527 uintptr_t Temperature_getClientArg(Temperature_NotifyObj *notifyObject);
528 
540 
541 #ifdef __cplusplus
542 }
543 #endif
544 
545 #endif /* ti_drivers_Temperature__include */
int16_t Temperature_getThresholdHigh(Temperature_NotifyObj *notifyObject)
Get the high threshold of a notification.
void(* Temperature_NotifyFxn)(int16_t currentTemperature, int16_t thresholdTemperature, uintptr_t clientArg, Temperature_NotifyObj *notifyObject)
Function prototype for a notification callback.
Definition: Temperature.h:306
Temperature_NotifyFxn notifyFxn
Definition: Temperature.h:323
int16_t thresholdLow
Definition: Temperature.h:325
Temperature notify object structure.
Definition: Temperature.h:320
int_fast16_t Temperature_registerNotifyRange(Temperature_NotifyObj *notifyObject, int16_t thresholdHigh, int16_t thresholdLow, Temperature_NotifyFxn notifyFxn, uintptr_t clientArg)
Registers a notification with both a high and low threshold.
int_fast16_t Temperature_registerNotifyLow(Temperature_NotifyObj *notifyObject, int16_t thresholdLow, Temperature_NotifyFxn notifyFxn, uintptr_t clientArg)
Registers a notification with a low threshold.
int16_t thresholdHigh
Definition: Temperature.h:324
int_fast16_t Temperature_registerNotifyHigh(Temperature_NotifyObj *notifyObject, int16_t thresholdHigh, Temperature_NotifyFxn notifyFxn, uintptr_t clientArg)
Registers a notification with a high threshold.
bool isRegistered
Definition: Temperature.h:327
int_fast16_t Temperature_unregisterNotify(Temperature_NotifyObj *notifyObject)
Unregisters a currently registered notification.
void Temperature_getThresholdRange(Temperature_NotifyObj *notifyObject, int16_t *thresholdHigh, int16_t *thresholdLow)
Get the high and low threshold of a notification.
uintptr_t clientArg
Definition: Temperature.h:326
int16_t Temperature_getTemperature(void)
Gets the current temperature in degrees C.
int16_t Temperature_getThresholdLow(Temperature_NotifyObj *notifyObject)
Get the low threshold of a notification.
Temperature_NotifyFxn Temperature_getNotifyFxn(Temperature_NotifyObj *notifyObject)
Get the notifyFxn provided during registration.
void Temperature_init(void)
This function initializes the Temperature driver.
uintptr_t Temperature_getClientArg(Temperature_NotifyObj *notifyObject)
Get the application-provided clientArg of a notification.
Definition: List.h:126
List_Elem link
Definition: Temperature.h:322
Linked List interface for use in drivers.
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale