Comparator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, 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 <b>PRELIMINARY</b> Comparator Driver Interface
35  *
36  * <b>WARNING</b> These APIs are <b>PRELIMINARY</b>, and subject to
37  * change in the next few months.
38  *
39  * @anchor ti_drivers_Comparator_Overview
40  * # Overview
41  *
42  * The Comparator driver serves as the main interface for a typical RTOS
43  * application. Its purpose is to redirect the Comparator APIs to device
44  * specific implementations which are specified using a pointer to a
45  * #Comparator_FxnTable. The device specific implementations are responsible
46  * for creating all the RTOS specific primitives to allow for thread-safe
47  * operation.
48  *
49  * The Comparator driver is an analog driver that accepts a configuration of
50  * two analog input signals, compares those values, and then outputs high on
51  * the output signal if the positive input is more positive than that of the
52  * negative input terminal. This output signal can be configured in either
53  * inverted or non-inverted based off the user configuration.
54  *
55  * Device specific capabilities such as output analog filters and programmable
56  * hysteresis are configured via the implementation specific hwAttrs
57  * configuration structure. This top level driver provides all APIs needed to
58  * provide a uniform and generic Comparator driver experience regardless of the
59  * underlying peripheral implementation.
60  *
61  * <hr>
62  * @anchor ti_drivers_Comparator_Usage
63  * # Usage
64  *
65  * This documentation provides a basic @ref ti_drivers_Comparator_Synopsis
66  * "usage summary" and a set of @ref ti_drivers_Comparator_Examples "examples"
67  * in the form of commented code fragments. Detailed descriptions of the
68  * APIs are provided in subsequent sections.
69  *
70  * @anchor ti_drivers_Comparator_Synopsis
71  * ## Synopsis
72  *
73  * The Comparator driver is used to monitor two input analog signals and
74  * generate an output if the potential of the positive input terminal is
75  * greater than that of the negative input channel. This is commonly used for
76  * power supply supervision as well as precision slope analog-to-digital
77  * conversions. The code below sets up two arbitrary input signals to
78  * the Comparator driver and handles triggers of the voltage crossing in
79  * the appropriate callback function.
80  *
81  * The Comparator driver supports two distinct methods of accessing/utilizing
82  * the underlying comparator peripheral's output signal: accessing the output
83  * level dynamically and providing a user callback function.
84  *
85  * Accessing the output level directly can be done by calling the
86  * Comparator_getLevel() API after the driver has been successfully opened
87  * and started. This function will return a #Comparator_OUTPUT_HIGH if the
88  * positive input terminal is more positive than the negative terminal and
89  * #Comparator_OUTPUT_LOW if the reverse is true. If the output level of the
90  * comparator peripheral cannot be determined or the device is in an error
91  * state a value of #Comparator_OUTPUT_NOT_AVAILABLE is returned.
92  *
93  * The user callback functionality provides a way for the Comparator driver
94  * and its underlying implementation to communicate functional events as well
95  * as errors to the calling user application. If a non-null function pointer is
96  * provided to the callbackFxn configuration parameter a call to the callback
97  * will be invoked whenever a relevant event occurs in the driver. Primarily
98  * this callback will be invoked when the output of the comparator is
99  * triggered (or inversely triggered), however error events can also be
100  * passed through this callback.
101  *
102  * Note that these programming models are compatible with each other and can
103  * be mixed accordingly. For example a user callback can be provided and the
104  * Comparator_getLevel() API can be called in any scenario.
105  *
106  * @anchor ti_drivers_Comparator_Synopsis_Code
107  *
108  * @code
109  *
110  * #include <ti/drivers/Comparator.h>
111  *
112  * void comparatorCallback(Comparator_Handle handle, int32_t event)
113  * {
114  * switch (event)
115  * {
116  * case Comparator_EVENT_OUTPUT_TRIGGERED:
117  * // Output triggered
118  * sem_post(&someSemaphore);
119  * break;
120  * case Comparator_EVENT_OUTPUT_INVERTED_TRIGGERED:
121  * // Output triggered in the other direction
122  * sem_post(&someSemaphore);
123  * break;
124  * case Comparator_EVENT_ERROR:
125  * __breakpoint(0);
126  * break;
127  * default:
128  * break;
129  * }
130  * }
131  *
132  * void someComparatorFunction()
133  * {
134  * Comparator_Handle handle;
135  * Comparator_Params params;
136  *
137  * Comparator_Params_init(&params);
138  * params.callbackFxn = &comparatorCallback;
139  * params.interruptLevel = Comparator_INTERRUPT_RISING;
140  *
141  * handle = Comparator_open(CONFIG_COMPARATOR0, &params);
142  *
143  * if (handle == NULL)
144  * {
145  * //Comparator_open() failed
146  * while(1);
147  * }
148  *
149  * status = Comparator_start(handle);
150  *
151  * if (status == Comparator_STATUS_ERROR)
152  * {
153  * //Comparator_start() failed
154  * while(1);
155  * }
156  *
157  * // Waiting for some output event to signal from the callback
158  * sem_wait(&someSemaphore);
159  *
160  * Comparator_stop(handle);
161  * }
162  *
163  * @endcode
164  *
165  * Note that while the code above operates with a callback function provided,
166  * if a NULL value is given as the callbackFxn parameter the
167  * Comparator_getLevel() function can be invoked to dynamically get the output
168  * level of an initialized/started Comparator driver instance.
169  *
170  * <hr>
171  * @anchor ti_drivers_Comparator_Examples
172  * # Examples
173  *
174  * @li @ref ti_drivers_Comparator_Examples_open "Opening a Comparator instance"
175  *
176  * @anchor ti_drivers_Comparator_Examples_open
177  * ## Opening a Comparator instance
178  *
179  * @code
180  * Comparator_Handle comparator;
181  * Comparator_Params params;
182  *
183  * Comparator_Params_init(&params);
184  * comparator = Comparator_open(CONFIG_COMP0, &params);
185  * if (comparator == NULL)
186  * {
187  * // Comparator_open() failed
188  * while (1);
189  * }
190  * @endcode
191  *
192  * <hr>
193  * @anchor ti_drivers_Comparator_Configuration
194  * # Configuration
195  *
196  * Refer to the @ref driver_configuration "Driver's Configuration" section
197  * for driver configuration information.
198  * <hr>
199  *
200  *******************************************************************************
201  */
202 
203 #ifndef ti_drivers_Comparator__include
204 #define ti_drivers_Comparator__include
205 
206 #include <stdint.h>
207 
208 #ifdef __cplusplus
209 extern "C"
210 {
211 #endif
212 
230 #define Comparator_STATUS_RESERVED (-32)
231 
236 #define Comparator_STATUS_SUCCESS (0)
237 
242 #define Comparator_STATUS_ERROR (-1)
243 
262 #define Comparator_EVENT_RESERVED (32)
263 
272 #define Comparator_EVENT_OUTPUT_TRIGGERED (1)
273 
280 #define Comparator_EVENT_OUTPUT_INVERTED_TRIGGERED (2)
281 
288 #define Comparator_EVENT_ERROR (-1)
289 
298 
305 typedef enum
306 {
316 
325 typedef enum
326 {
337 
345 typedef enum
346 {
361 
375 typedef void (*Comparator_CallBackFxn)(Comparator_Handle handle, int32_t event);
376 
384 typedef struct
385 {
388 
390  Comparator_OutputPolarity outputPolarity;
391 
393  Comparator_InterruptLevel interruptLevel;
394 
396 
401 typedef void (*Comparator_CloseFxn)(Comparator_Handle handle);
402 
407 typedef uint32_t (*Comparator_GetLevelFxn)(Comparator_Handle handle);
408 
413 typedef void (*Comparator_InitFxn)(Comparator_Handle handle);
414 
419 typedef Comparator_Handle (*Comparator_OpenFxn)(Comparator_Handle handle,
420  Comparator_Params *params);
421 
426 typedef int32_t (*Comparator_StartFxn)(Comparator_Handle handle);
427 
432 typedef void (*Comparator_StopFxn)(Comparator_Handle handle);
433 
438 typedef int32_t (*Comparator_GetParamsFxn)(Comparator_Handle handle,
439  Comparator_Params *params);
440 
445 typedef int32_t (*Comparator_SetParamsFxn)(Comparator_Handle handle,
446  Comparator_Params *params);
447 
453 typedef struct
454 {
457 
460 
463 
466 
469 
472 
475 
479 
486 typedef struct Comparator_Config_
487 {
491 
493  void *object;
494 
497  void const *hwAttrs;
499 
509 extern void Comparator_close(Comparator_Handle handle);
510 
526 extern int_fast16_t Comparator_control(Comparator_Handle handle,
527  uint_fast16_t cmd,
528  void *arg);
529 
545 extern Comparator_OutputLevel Comparator_getLevel(Comparator_Handle handle);
546 
552 extern void Comparator_init(void);
553 
573 extern Comparator_Handle Comparator_open(uint32_t index,
574  Comparator_Params *params);
575 
587 extern void Comparator_Params_init(Comparator_Params *params);
588 
601 extern int32_t Comparator_start(Comparator_Handle handle);
602 
614 extern void Comparator_stop(Comparator_Handle handle);
615 
630 extern int32_t Comparator_getParams(Comparator_Handle handle,
631  Comparator_Params *params);
632 
647 extern int32_t Comparator_setParams(Comparator_Handle handle,
648  Comparator_Params *params);
649 
650 #ifdef __cplusplus
651 }
652 #endif
653 
654 #endif /* ti_drivers_Comparator__include */
Comparator_OpenFxn openFxn
Definition: Comparator.h:465
void Comparator_init(void)
Function to initialize the Comparator driver.
The definition of a comparator function table that contains the required set of functions to control ...
Definition: Comparator.h:453
void(* Comparator_CallBackFxn)(Comparator_Handle handle, int32_t event)
Comparator callback function.
Definition: Comparator.h:375
Comparator_CloseFxn closeFxn
Definition: Comparator.h:456
void(* Comparator_CloseFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_close().
Definition: Comparator.h:401
Definition: Comparator.h:311
Definition: Comparator.h:330
Definition: Comparator.h:358
Definition: Comparator.h:327
Comparator_SetParamsFxn setParamsFxn
Definition: Comparator.h:477
Definition: Comparator.h:347
Comparator_FxnTable const * fxnTablePtr
Definition: Comparator.h:490
Comparator_OutputLevel
Comparator Output Level.
Definition: Comparator.h:325
void(* Comparator_StopFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_stop().
Definition: Comparator.h:432
Definition: Comparator.h:307
int32_t(* Comparator_StartFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_start().
Definition: Comparator.h:426
Comparator_InitFxn initFxn
Definition: Comparator.h:462
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:419
int32_t Comparator_setParams(Comparator_Handle handle, Comparator_Params *params)
Function to get the parameters of a comparator instance.
Comparator_GetLevelFxn getLevelFxn
Definition: Comparator.h:459
struct Comparator_Config_ * Comparator_Handle
A handle that is returned from a Comparator_open() call.
Definition: Comparator.h:297
Definition: Comparator.h:333
Definition: Comparator.h:353
struct Comparator_Config_ Comparator_Config
Comparator driver&#39;s custom configuration structure.
int_fast16_t Comparator_control(Comparator_Handle handle, uint_fast16_t cmd, void *arg)
Function performs implementation specific features on a given Comparator_Handle.
Comparator_InterruptLevel interruptLevel
Definition: Comparator.h:393
Comparator_GetParamsFxn getParamsFxn
Definition: Comparator.h:474
int32_t Comparator_getParams(Comparator_Handle handle, Comparator_Params *params)
Function to get the parameters of a comparator instance.
void const * hwAttrs
Definition: Comparator.h:497
void Comparator_stop(Comparator_Handle handle)
Function to stop a comparator instance. If the comparator instance is already stopped this function h...
Comparator_OutputPolarity
Comparator Output Polarity.
Definition: Comparator.h:305
void Comparator_Params_init(Comparator_Params *params)
Function to initialize the Comparator_Params structure to its default values.
void * object
Definition: Comparator.h:493
int32_t(* Comparator_SetParamsFxn)(Comparator_Handle handle, Comparator_Params *params)
A function pointer to a driver specific implementation of Comparator_setParams(). ...
Definition: Comparator.h:445
Comparator driver&#39;s custom configuration structure.
Definition: Comparator.h:486
Comparator_InterruptLevel
Comparator Interrupt Level.
Definition: Comparator.h:345
Comparator_OutputLevel Comparator_getLevel(Comparator_Handle handle)
Function returns the level of the comparator output. The output level correlates to the polarity spec...
Comparator_Handle Comparator_open(uint32_t index, Comparator_Params *params)
Function to initialize the Comparator peripheral.
int32_t(* Comparator_GetParamsFxn)(Comparator_Handle handle, Comparator_Params *params)
A function pointer to a driver specific implementation of Comparator_getParams(). ...
Definition: Comparator.h:438
Comparator_StartFxn startFxn
Definition: Comparator.h:468
Comparator_OutputPolarity outputPolarity
Definition: Comparator.h:390
Comparator Parameters.
Definition: Comparator.h:384
void(* Comparator_InitFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_init().
Definition: Comparator.h:413
int32_t Comparator_start(Comparator_Handle handle)
Function to start the comparator instance.
Definition: Comparator.h:349
Comparator_StopFxn stopFxn
Definition: Comparator.h:471
Definition: Comparator.h:351
Comparator_CallBackFxn callbackFxn
Definition: Comparator.h:387
uint32_t(* Comparator_GetLevelFxn)(Comparator_Handle handle)
A function pointer to a driver specific implementation of Comparator_getLevel().
Definition: Comparator.h:407
Definition: Comparator.h:356
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale