1 /*
2 * Copyright (c) 2016-2017, 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 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35
36 #include <ti/drivers/Capture.h>
37 #include <ti/drivers/dpl/HwiP.h>
38
39 extern const Capture_Config Capture_config[];
40 extern const uint_least8_t Capture_count;
41
42 /* Default Parameters */
43 static const Capture_Params defaultParams = {
44 .callbackFxn = NULL,
45 .mode = Capture_RISING_EDGE,
46 .periodUnit = Capture_PERIOD_COUNTS
47 };
48
49 static bool isInitialized = false;
50
51 /*
52 * ======== Capture_close ========
53 */
Capture_close(Capture_Handle handle)54 void Capture_close(Capture_Handle handle)
55 {
56 handle->fxnTablePtr->closeFxn(handle);
57 }
58
59 /*
60 * ======== Capture_control ========
61 */
Capture_control(Capture_Handle handle,uint_fast16_t cmd,void * arg)62 int_fast16_t Capture_control(Capture_Handle handle, uint_fast16_t cmd,
63 void *arg)
64 {
65 return (handle->fxnTablePtr->controlFxn(handle, cmd, arg));
66 }
67
68 /*
69 * ======== Capture_init ========
70 */
Capture_init(void)71 void Capture_init(void)
72 {
73 uint_least8_t i;
74 uint_fast32_t key;
75
76 key = HwiP_disable();
77
78 if (!isInitialized) {
79 isInitialized = (bool) true;
80
81 /* Call each driver's init function */
82 for (i = 0; i < Capture_count; i++) {
83 Capture_config[i].fxnTablePtr->initFxn((Capture_Handle) &(Capture_config[i]));
84 }
85 }
86
87 HwiP_restore(key);
88 }
89
90 /*
91 * ======== Capture_open ========
92 */
Capture_open(uint_least8_t index,Capture_Params * params)93 Capture_Handle Capture_open(uint_least8_t index, Capture_Params *params)
94 {
95 Capture_Handle handle = NULL;
96
97 /* Verify driver index and state */
98 if (isInitialized && (index < Capture_count)) {
99 /* If parameters are NULL use defaults */
100 if (params == NULL) {
101 params = (Capture_Params *) &defaultParams;
102 }
103
104 /* Get handle for this driver instance */
105 handle = (Capture_Handle) &(Capture_config[index]);
106 handle = handle->fxnTablePtr->openFxn(handle, params);
107 }
108
109 return (handle);
110 }
111
112 /*
113 * ======== Capture_Params_init ========
114 */
Capture_Params_init(Capture_Params * params)115 void Capture_Params_init(Capture_Params *params)
116 {
117 *params = defaultParams;
118 }
119
120 /*
121 * ======== Capture_start ========
122 */
Capture_start(Capture_Handle handle)123 int32_t Capture_start(Capture_Handle handle)
124 {
125 return (handle->fxnTablePtr->startFxn(handle));
126 }
127
128 /*
129 * ======== Capture_stop ========
130 */
Capture_stop(Capture_Handle handle)131 void Capture_stop(Capture_Handle handle)
132 {
133 handle->fxnTablePtr->stopFxn(handle);
134 }
135