1 /*
2  * Copyright (c) 2016-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 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 
36 #include <ti/drivers/dpl/HwiP.h>
37 #include <ti/drivers/Timer.h>
38 
39 extern const Timer_Config Timer_config[];
40 extern const uint_least8_t Timer_count;
41 
42 /* Default Parameters */
43 static const Timer_Params defaultParams = {
44     .timerMode     = Timer_ONESHOT_BLOCKING,
45     .periodUnits   = Timer_PERIOD_COUNTS,
46     .timerCallback = NULL,
47     .period        = (uint16_t) ~0
48 };
49 
50 static bool isInitialized = false;
51 
52 /*
53  *  ======== Timer_control ========
54  */
Timer_control(Timer_Handle handle,uint_fast16_t cmd,void * arg)55 int_fast16_t Timer_control(Timer_Handle handle, uint_fast16_t cmd, void *arg)
56 {
57     return handle->fxnTablePtr->controlFxn(handle, cmd, arg);
58 }
59 
60 /*
61  *  ======== Timer_close ========
62  */
Timer_close(Timer_Handle handle)63 void Timer_close(Timer_Handle handle)
64 {
65     handle->fxnTablePtr->closeFxn(handle);
66 }
67 
68 /*
69  *  ======== Timer_getCount ========
70  */
Timer_getCount(Timer_Handle handle)71 uint32_t Timer_getCount(Timer_Handle handle)
72 {
73     return handle->fxnTablePtr->getCountFxn(handle);
74 }
75 
76 /*
77  *  ======== Timer_init ========
78  */
Timer_init(void)79 void Timer_init(void)
80 {
81     uint_least8_t i;
82     uint_fast32_t key;
83 
84     key = HwiP_disable();
85 
86     if (!isInitialized) {
87         isInitialized = (bool) true;
88 
89         /* Call each driver's init function */
90         for (i = 0; i < Timer_count; i++) {
91             Timer_config[i].fxnTablePtr->initFxn((Timer_Handle) &(Timer_config[i]));
92         }
93     }
94 
95     HwiP_restore(key);
96 }
97 
98 /*
99  *  ======== Timer_open ========
100  */
Timer_open(uint_least8_t index,Timer_Params * params)101 Timer_Handle Timer_open(uint_least8_t index, Timer_Params *params)
102 {
103     Timer_Handle handle = NULL;
104 
105     /* Verify driver index and state */
106     if (isInitialized && (index < Timer_count)) {
107         /* If parameters are NULL use defaults */
108         if (params == NULL) {
109             params = (Timer_Params *) &defaultParams;
110         }
111 
112         /* Get handle for this driver instance */
113         handle = (Timer_Handle) &(Timer_config[index]);
114         handle = handle->fxnTablePtr->openFxn(handle, params);
115     }
116 
117     return (handle);
118 }
119 
120 /*
121  *  ======== Timer_Params_init ========
122  */
Timer_Params_init(Timer_Params * params)123 void Timer_Params_init(Timer_Params *params)
124 {
125     *params = defaultParams;
126 }
127 
128 /*
129  *  ======== Timer_setPeriod ========
130  */
Timer_setPeriod(Timer_Handle handle,Timer_PeriodUnits periodUnits,uint32_t period)131 int32_t Timer_setPeriod(Timer_Handle handle, Timer_PeriodUnits periodUnits, uint32_t period)
132  {
133     return handle->fxnTablePtr->setPeriodFxn(handle, periodUnits, period);
134  }
135 
136 /*
137  *  ======== Timer_start ========
138  */
Timer_start(Timer_Handle handle)139 int32_t Timer_start(Timer_Handle handle)
140 {
141     return handle->fxnTablePtr->startFxn(handle);
142 }
143 
144 /*
145  *  ======== Timer_stop ========
146  */
Timer_stop(Timer_Handle handle)147 void Timer_stop(Timer_Handle handle)
148 {
149     handle->fxnTablePtr->stopFxn(handle);
150 }
151