Comparator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022, 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 Comparator.h
34  * @brief Comparator Driver
35  *
36  * @anchor ti_drivers_comparator_Overview
37  * # Overview
38  *
39  * The comparator driver allows you to manage the voltage comparator peripheral
40  * via simple and portable APIs. This driver supports monitoring of analog
41  * voltages on external pins to allow event generation when the voltage crosses
42  * a defined threshold.
43  *
44  * The Comparator driver serves as the main interface for a typical RTOS
45  * application. Its purpose is to redirect the Comparator APIs to
46  * device-specific implementations which are specified using a pointer to a
47  * #Comparator_FxnTable.
48  * The device-specific implementations are responsible for creating all the
49  * RTOS specific primitives to allow for thread-safe operation.
50  *
51  * The Comparator driver is an analog driver that accepts a configuration of
52  * two analog input signals, compares those values, and then outputs high on
53  * the output signal if the positive input is more positive than that of the
54  * negative input terminal.
55  *
56  * Device-specific capabilities such as input voltage division or output analog
57  * filtering are configured via the implementation specific hwAttrs
58  * configuration structure. This top level driver provides all APIs
59  * needed to provide a uniform and generic Comparator driver experience
60  * regardless of the underlying peripheral implementation.
61  *
62  * <hr>
63  * @anchor ti_drivers_Comparator_Usage
64  * # Usage
65  *
66  * This documentation provides a basic @ref ti_drivers_Comparator_Synopsis
67  * "usage summary" and a set of @ref ti_drivers_Comparator_Examples "examples"
68  * in the form of commented code fragments. Detailed descriptions of the
69  * APIs are provided in subsequent sections.
70  *
71  * @anchor ti_drivers_Comparator_Synopsis
72  * ## Synopsis
73  *
74  * The Comparator driver is used to monitor two input analog signals (internal
75  * or external) and generate an output if the potential of the positive input
76  * terminal is greater than that of the negative input channel. This is
77  * commonly used for power supply supervision as well as precision slope
78  * analog-to-digital conversions. The code below sets up two arbitrary
79  * input signals to the Comparator driver and handles triggers of the voltage
80  * crossing in the appropriate callback function.
81  *
82  * The Comparator driver supports two distinct methods of accessing/utilizing
83  * the underlying comparator peripheral's output signal:
84  * - Returning the output level on request.
85  * - Providing a user callback function invoked upon a Comparator trigger
86  * event.
87  *
88  * Accessing the output level directly can be done by calling the
89  * Comparator_getLevel() API after the driver has been successfully opened
90  * and started. This function will return a #Comparator_OUTPUT_HIGH if the
91  * positive input terminal is more positive than the negative terminal and
92  * #Comparator_OUTPUT_LOW if the opposite is true. If the output level of the
93  * comparator peripheral cannot be determined or the device is in an error
94  * state a value of #Comparator_OUTPUT_NOT_AVAILABLE is returned.
95  *
96  * The user callback functionality provides a way for the Comparator driver
97  * and its underlying implementation to communicate events to the calling user
98  * application. The user callback is selected through the
99  * #Comparator_Params.callbackFxn field.
100  * If a non-null function pointer is provided to the callbackFxn configuration
101  * parameter, the callback will be invoked whenever a relevant event occurs in
102  * the driver. Primarily this callback will be invoked when the output of the
103  * comparator is triggered, however error events can also be passed through
104  * this callback. Since the user's callback may be called in the context of a
105  * hardware interrupt, the callback function must not make any RTOS blocking
106  * calls.
107  *
108  * Note that accessing the output level is compatible with callback mode and
109  * can be mixed accordingly. For example a user callback can be provided and
110  * the Comparator_getLevel() API can still be called when needed.
111  *
112  * @anchor ti_drivers_Comparator_Synopsis_Code
113  *
114  * @code
115  *
116  * #include <ti/drivers/Comparator.h>
117  *
118  * void comparatorCallback(Comparator_Handle handle,
119  * int_fast16_t returnValue,
120  * Comparator_Trigger trigger)
121  * {
122  * // React to Comparator event
123  * sem_post(&someSemaphore);
124  * }
125  *
126  * void someComparatorFunction()
127  * {
128  * Comparator_Handle handle;
129  * Comparator_Params params;
130  *
131  * Comparator_Params_init(&params);
132  * params.callbackFxn = &comparatorCallback;
133  * params.trigger = Comparator_TRIGGER_RISING;
134  *
135  * handle = Comparator_open(CONFIG_COMPARATOR_0, &params);
136  *
137  * if (handle == NULL)
138  * {
139  * //Comparator_open() failed
140  * while (1) {}
141  * }
142  *
143  * status = Comparator_start(handle);
144  *
145  * if (status == Comparator_STATUS_ERROR)
146  * {
147  * //Comparator_start() failed
148  * while (1) {}
149  * }
150  *
151  * // Waiting for some output event to signal from the callback
152  * sem_wait(&someSemaphore);
153  *
154  * Comparator_stop(handle);
155  * }
156  *
157  * @endcode
158  *
159  * Note that while the code above operates with a callback function provided,
160  * if a NULL value is given as the callbackFxn parameter the
161  * Comparator_getLevel() function can be invoked to dynamically get the output
162  * level of an initialized/started Comparator driver instance.
163  *
164  * <hr>
165  * @anchor ti_drivers_Comparator_Examples
166  * # Examples
167  *
168  * @li @ref ti_drivers_Comparator_Examples_open "Opening a Comparator instance"
169  *
170  * @anchor ti_drivers_Comparator_Examples_open
171  * ## Opening a Comparator instance
172  *
173  * @code
174  * Comparator_Handle comparator;
175  * Comparator_Params params;
176  *
177  * Comparator_Params_init(&params);
178  * comparator = Comparator_open(CONFIG_COMPARATOR_0, &params);
179  * if (comparator == NULL)
180  * {
181  * // Comparator_open() failed
182  * while (1) {}
183  * }
184  * @endcode
185  *
186  * <hr>
187  * @anchor ti_drivers_Comparator_Configuration
188  * # Configuration
189  *
190  * Refer to the @ref driver_configuration "Driver's Configuration" section
191  * for driver configuration information.
192  * <hr>
193  *
194  ******************************************************************************
195  */
196 
197 #ifndef ti_drivers_Comparator__include
198 #define ti_drivers_Comparator__include
199 
200 #include <stdint.h>
201 
202 #ifdef __cplusplus
203 extern "C" {
204 #endif
205 
223 #define Comparator_STATUS_RESERVED (-32)
224 
229 #define Comparator_STATUS_SUCCESS (0)
230 
235 #define Comparator_STATUS_ERROR (-1)
236 
242 
249 typedef enum
250 {
264 
272 typedef enum
273 {
284 
320 typedef void (*Comparator_CallBackFxn)(Comparator_Handle handle, int_fast16_t returnValue, Comparator_Trigger trigger);
321 
329 typedef struct
330 {
333 
335  Comparator_Trigger trigger;
337 
342 typedef void (*Comparator_CloseFxn)(Comparator_Handle handle);
343 
348 typedef Comparator_OutputLevel (*Comparator_GetLevelFxn)(Comparator_Handle handle);
349 
354 typedef Comparator_Trigger (*Comparator_getTriggerFxn)(Comparator_Handle handle);
355 
360 typedef void (*Comparator_InitFxn)(Comparator_Handle handle);
361 
366 typedef Comparator_Handle (*Comparator_OpenFxn)(Comparator_Handle handle, Comparator_Params *params);
367 
372 typedef int_fast16_t (*Comparator_setTriggerFxn)(Comparator_Handle handle, Comparator_Trigger trigger);
373 
378 typedef int_fast16_t (*Comparator_StartFxn)(Comparator_Handle handle);
379 
384 typedef void (*Comparator_StopFxn)(Comparator_Handle handle);
385 
391 typedef struct
392 {
395 
398 
401 
404 
407 
410 
413 
417 
424 typedef struct Comparator_Config
425 {
429 
431  void *object;
432 
435  void const *hwAttrs;
437 
438 extern const Comparator_Config Comparator_config[];
439 
447 extern void Comparator_close(Comparator_Handle handle);
448 
461 extern Comparator_OutputLevel Comparator_getLevel(Comparator_Handle handle);
462 
475 extern Comparator_Trigger Comparator_getTrigger(Comparator_Handle handle);
476 
482 extern void Comparator_init(void);
483 
503 extern Comparator_Handle Comparator_open(uint32_t index, Comparator_Params *params);
504 
515 
542 extern int_fast16_t Comparator_setTrigger(Comparator_Handle handle, Comparator_Trigger trigger);
543 
566 extern int_fast16_t Comparator_start(Comparator_Handle handle);
567 
579 extern void Comparator_stop(Comparator_Handle handle);
580 
581 #ifdef __cplusplus
582 }
583 #endif
584 
585 #endif /* ti_drivers_Comparator__include */
Comparator_OpenFxn openFxn
Definition: Comparator.h:406
void Comparator_init(void)
Function to initialize the Comparator driver.
ADC_Params params
Definition: Driver_Init.h:11
The definition of a comparator function table that contains the required set of functions to control ...
Definition: Comparator.h:391
Comparator_Trigger
Comparator interrupt trigger.
Definition: Comparator.h:272
struct Comparator_Config * Comparator_Handle
A handle that is returned from a Comparator_open() call.
Definition: Comparator.h:241
Comparator_CloseFxn closeFxn
Definition: Comparator.h:394
void(* Comparator_CloseFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_close().
Definition: Comparator.h:342
Comparator driver&#39;s custom configuration structure.
Definition: Comparator.h:424
Definition: Comparator.h:255
Definition: Comparator.h:251
Comparator_OutputLevel
Comparator output level.
Definition: Comparator.h:249
void(* Comparator_StopFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_stop().
Definition: Comparator.h:384
Comparator_InitFxn initFxn
Definition: Comparator.h:403
Comparator_OutputLevel(* Comparator_GetLevelFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_getLevel().
Definition: Comparator.h:348
Comparator_Trigger Comparator_getTrigger(Comparator_Handle handle)
Function to get the interrupt trigger of the comparator instance.
void Comparator_close(Comparator_Handle handle)
Function to close a Comparator driver instance.
Comparator_Handle(* Comparator_OpenFxn)(Comparator_Handle handle, Comparator_Params *params)
A function pointer to a driver specific implementation of Comparator_open().
Definition: Comparator.h:366
Comparator_GetLevelFxn getLevelFxn
Definition: Comparator.h:397
int_fast16_t Comparator_start(Comparator_Handle handle)
Function to start the comparator instance.
Comparator_setTriggerFxn setTriggerFxn
Definition: Comparator.h:409
Definition: Comparator.h:259
Comparator_Trigger(* Comparator_getTriggerFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_getLevel().
Definition: Comparator.h:354
Comparator_getTriggerFxn getTriggerFxn
Definition: Comparator.h:400
Comparator_FxnTable const * fxnTablePtr
Definition: Comparator.h:428
struct Comparator_Config Comparator_Config
Comparator driver&#39;s custom configuration structure.
Definition: Comparator.h:274
void Comparator_stop(Comparator_Handle handle)
Function to stop a comparator instance. If the comparator instance is already stopped this function h...
const Comparator_Config Comparator_config[]
void Comparator_Params_init(Comparator_Params *params)
Initialize an Comparator_Params structure to its default values.
int_fast16_t(* Comparator_setTriggerFxn)(Comparator_Handle handle, Comparator_Trigger trigger)
A function pointer to a driver specific implementation of Comparator_open().
Definition: Comparator.h:372
Definition: Comparator.h:277
void const * hwAttrs
Definition: Comparator.h:435
Comparator_OutputLevel Comparator_getLevel(Comparator_Handle handle)
Function which returns the level of the comparator output.
Comparator_Handle Comparator_open(uint32_t index, Comparator_Params *params)
Function to initialize the Comparator peripheral.
void(* Comparator_CallBackFxn)(Comparator_Handle handle, int_fast16_t returnValue, Comparator_Trigger trigger)
Comparator callback function.
Definition: Comparator.h:320
Comparator_StartFxn startFxn
Definition: Comparator.h:412
int_fast16_t(* Comparator_StartFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_start().
Definition: Comparator.h:378
Definition: Comparator.h:280
Comparator parameters.
Definition: Comparator.h:329
void(* Comparator_InitFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_init().
Definition: Comparator.h:360
void * object
Definition: Comparator.h:431
Comparator_StopFxn stopFxn
Definition: Comparator.h:415
Comparator_CallBackFxn callbackFxn
Definition: Comparator.h:332
Comparator_Trigger trigger
Definition: Comparator.h:335
int_fast16_t Comparator_setTrigger(Comparator_Handle handle, Comparator_Trigger trigger)
Function to set at run-time the interrupt trigger.
© Copyright 1995-2023, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale