Temperature.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2024, 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. It is not called automatically via SysConfig generated code.
54  * Subsequent #Temperature_init() calls will have no effect.
55  *
56  * ## Getting the Current Temperature #
57  * The most basic function of the driver is to provide the current temperature
58  * and return it. It is encoded as a signed integer in degrees C.
59  *
60  * ## Notifications #
61  * The other major function of the Temperature driver is to notify the
62  * application when the temperature changes and crosses an application-defined
63  * threshold.
64  *
65  * There are three default usecases for this:
66  * - High threshold.
67  * The application will receive a notification callback when
68  * currentTemperature >= thresholdHigh.
69  * - Low threshold.
70  * The application will receive a notification callback when
71  * currentTemperature <= thresholdLow.
72  * - Range threshold.
73  * The application will receive a notification callback when
74  * currentTemperature >= thresholdHigh || currentTemperature <=
75  * thresholdLow. This setup addresses usecases
76  * where a notification is required when the temperature changes by a
77  * certain amount regardless of whether it is up or down. Adjusting
78  * clock offsets based on temperature is a good example of this.
79  *
80  * ### Registering Notifications
81  * There are three functions that register a notification for the application:
82  * - #Temperature_registerNotifyHigh()
83  * - #Temperature_registerNotifyLow()
84  * - #Temperature_registerNotifyRange()
85  *
86  * Multiple notifications may be registered. The different parts of the
87  * application and drivers that need to respond to a temperature change do not
88  * need to know of one another.
89  * Each notification must have its own #Temperature_NotifyObj and must be
90  * registered individually.
91  *
92  * ### Notification Callbacks
93  * Once the chip temperature 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
97  * the list of registered notifications and thus are no longer registered.
98  * Their callback function is then invoked.
99  *
100  * If an application wishes to re-register a notification that just triggered
101  * and was unregistered, it may register it again from within the notification
102  * callback or another context.
103  *
104  * It is possible to determine whether the high or low threshold triggered
105  * the notification callback as follows:
106  * - currentTemperature <= thresholdTemperature: Low threshold triggered
107  * - currentTemperature >= thresholdTemperature: High threshold triggered
108  * This information is only reasonably useful when registering a notification
109  * with both a high and low threshold using #Temperature_registerNotifyRange().
110  * Even then, the expected basic usecase only cares about the current
111  * temperature and adding an offset to it when registering the notification
112  * again.
113  *
114  * ### Unregistering Notifications
115  * Registered notifications are unregistered in two ways:
116  * - Automatically when a notification triggers
117  * - By calling #Temperature_unregisterNotify()
118  *
119  * Unregistered notifications may be registered again at any time.
120  *
121  * # Measured vs True Temperature
122  * While the driver aims to supply and act on an accurate absolute temperature,
123  * there will be differences between the measured vs the true temperature due
124  * to inherent variances in the manufacturing process. The nature of these
125  * differences varies by device family.
126  *
127  * Examples of such differences:
128  * - A constant per-chip offset between the measured and the true
129  * temperature
130  * - An temperature dependent per-chip offset between the measured and the
131  * true temperature
132  * - A variance in the measured temperature when measuring multiple times
133  * at the same chip temperature
134  *
135  * It is strongly recommended to read the device-specific Temperature driver
136  * documentation for details of the temperature sensor characteristics and
137  * how they might affect choices of threshold values.
138  *
139  * @anchor ti_drivers_Temperature_Synopsis
140  * # Synopsis #
141  * @anchor ti_drivers_Temperature_Synopsis_Code
142  * @code
143  * #include <ti/drivers/Temperature.h>
144  *
145  * #define WINDOW_DELTA 10
146  *
147  * Temperature_init();
148  *
149  * currentTemperature = Temperature_getTemperature();
150  *
151  * result = Temperature_registerNotifyRange(&notifyObject,
152  * currentTemperature + WINDOW_DELTA,
153  * currentTemperature - WINDOW_DELTA,
154  * myNotifyFxn,
155  * clientArg);
156  * @endcode
157  *
158  * @anchor ti_drivers_Temperature_Examples
159  * # Examples #
160  *
161  * ## Register a High Threshold Notification #
162  *
163  * @code
164  *
165  * // The notification will trigger when the temperature reaches 40 C
166  * #define THRESHOLD_CUTOFF 40
167  *
168  * #include <ti/drivers/Temperature.h>
169  *
170  * void thresholdNotifyFxn(int16_t currentTemperature,
171  * int16_t thresholdTemperature,
172  * uintptr_t clientArg,
173  * Temperature_NotifyObj *notifyObject) {
174  * // Post a semaphore, set a flag, or otherwise act upon the temperature
175  * // change.
176  * }
177  *
178  * ...
179  *
180  * // Initialize the Temperature driver and register a notification.
181  *
182  * Temperature_init();
183  *
184  * int_fast16_t status = Temperature_registerNotifyHigh(notifyObject,
185  * THRESHOLD_CUTOFF,
186  * thresholdNotifyFxn,
187  * NULL);
188  *
189  * if (status != Temperature_STATUS_SUCCESS) {
190  * // Handle error
191  * }
192  *
193  * @endcode
194  *
195  * ## Register a Range Threshold Notification and Re-register in Callback #
196  *
197  * @code
198  *
199  * #define THRESHOLD_DELTA 5
200  *
201  * #include <ti/drivers/Temperature.h>
202  *
203  *
204  * void deltaNotificationFxn(int16_t currentTemperature,
205  * int16_t thresholdTemperature,
206  * uintptr_t clientArg,
207  * Temperature_NotifyObj *notifyObject) {
208  * int_fast16_t status;
209  *
210  * status = Temperature_registerNotifyRange(notifyObject,
211  * currentTemperature + THRESHOLD_DELTA,
212  * currentTemperature - THRESHOLD_DELTA,
213  * deltaNotificationFxn,
214  * NULL);
215  *
216  * if (status != Temperature_STATUS_SUCCESS) {
217  * while(1);
218  * }
219  * }
220  *
221  * ...
222  *
223  * // Initialize the Temperature driver and register a notification.
224  *
225  * Temperature_init();
226  *
227  * int16_t currentTemperature = Temperature_getTemperature();
228  *
229  * int_fast16_t status = Temperature_registerNotifyRange(notifyObject,
230  * currentTemperature + THRESHOLD_DELTA,
231  * currentTemperature - THRESHOLD_DELTA,
232  * deltaNotificationFxn,
233  * NULL);
234  * @endcode
235  */
236 
237 #ifndef ti_drivers_Temperature__include
238 #define ti_drivers_Temperature__include
239 
240 #include <stdbool.h>
241 #include <stddef.h>
242 #include <stdint.h>
243 
244 #include <ti/drivers/utils/List.h>
245 
246 #ifdef __cplusplus
247 extern "C" {
248 #endif
249 
262 #define Temperature_STATUS_RESERVED (-32)
263 
270 #define Temperature_STATUS_SUCCESS (0)
271 
278 #define Temperature_STATUS_ERROR (-1)
279 
280 /* @cond
281  *
282  * Type declaration for the notification object made separately from the
283  * struct definition because of the circular dependency between
284  * #Temperature_NotifyFxn() and #Temperature_NotifyObj.
285  */
287 /* @endcond */
288 
307 typedef void (*Temperature_NotifyFxn)(int16_t currentTemperature,
308  int16_t thresholdTemperature,
309  uintptr_t clientArg,
310  Temperature_NotifyObj *notifyObject);
311 
322 {
325  int16_t thresholdHigh;
326  int16_t thresholdLow;
327  uintptr_t clientArg;
329 };
330 
341 void Temperature_init(void);
342 
348 int16_t Temperature_getTemperature(void);
349 
377 int_fast16_t Temperature_registerNotifyHigh(Temperature_NotifyObj *notifyObject,
378  int16_t thresholdHigh,
380  uintptr_t clientArg);
381 
409 int_fast16_t Temperature_registerNotifyLow(Temperature_NotifyObj *notifyObject,
410  int16_t thresholdLow,
412  uintptr_t clientArg);
413 
445 int_fast16_t Temperature_registerNotifyRange(Temperature_NotifyObj *notifyObject,
446  int16_t thresholdHigh,
447  int16_t thresholdLow,
449  uintptr_t clientArg);
450 
467 int_fast16_t Temperature_unregisterNotify(Temperature_NotifyObj *notifyObject);
468 
484 
500 
519 void Temperature_getThresholdRange(Temperature_NotifyObj *notifyObject, int16_t *thresholdHigh, int16_t *thresholdLow);
520 
531 uintptr_t Temperature_getClientArg(Temperature_NotifyObj *notifyObject);
532 
544 
545 #ifdef __cplusplus
546 }
547 #endif
548 
549 #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:307
Temperature_NotifyFxn notifyFxn
Definition: Temperature.h:324
int16_t thresholdLow
Definition: Temperature.h:326
Temperature notify object structure.
Definition: Temperature.h:321
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:325
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:328
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:327
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:323
Linked List interface for use in drivers.
© Copyright 1995-2024, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale