1 /*
2  * Copyright (c) 2015-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  *  ======== I2C.c ========
34  */
35 
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 
40 #include <ti/drivers/dpl/HwiP.h>
41 #include <ti/drivers/dpl/SemaphoreP.h>
42 #include <ti/drivers/I2C.h>
43 
44 extern const I2C_Config I2C_config[];
45 extern const uint_least8_t I2C_count;
46 
47 /* Default I2C parameters structure */
48 const I2C_Params I2C_defaultParams = {
49     I2C_MODE_BLOCKING,  /* transferMode */
50     NULL,               /* transferCallbackFxn */
51     I2C_100kHz,         /* bitRate */
52     NULL                /* custom */
53 };
54 
55 static bool isInitialized = false;
56 
57 /*
58  *  ======== I2C_cancel ========
59  */
I2C_cancel(I2C_Handle handle)60 void I2C_cancel(I2C_Handle handle)
61 {
62     handle->fxnTablePtr->cancelFxn(handle);
63 }
64 
65 /*
66  *  ======== I2C_close ========
67  */
I2C_close(I2C_Handle handle)68 void I2C_close(I2C_Handle handle)
69 {
70     handle->fxnTablePtr->closeFxn(handle);
71 }
72 
73 /*
74  *  ======== I2C_control ========
75  */
I2C_control(I2C_Handle handle,uint_fast16_t cmd,void * controlArg)76 int_fast16_t I2C_control(I2C_Handle handle, uint_fast16_t cmd, void *controlArg)
77 {
78     return (handle->fxnTablePtr->controlFxn(handle, cmd, controlArg));
79 }
80 
81 /*
82  *  ======== I2C_init ========
83  */
I2C_init(void)84 void I2C_init(void)
85 {
86     uint_least8_t i;
87     uint_fast32_t key;
88 
89     key = HwiP_disable();
90 
91     if (!isInitialized) {
92         isInitialized = (bool) true;
93 
94         /* Call each driver's init function */
95         for (i = 0; i < I2C_count; i++) {
96             I2C_config[i].fxnTablePtr->initFxn((I2C_Handle)&(I2C_config[i]));
97         }
98     }
99 
100     HwiP_restore(key);
101 }
102 
103 /*
104  *  ======== I2C_open ========
105  */
I2C_open(uint_least8_t index,I2C_Params * params)106 I2C_Handle I2C_open(uint_least8_t index, I2C_Params *params)
107 {
108     I2C_Handle handle = NULL;
109 
110     if (isInitialized && (index < I2C_count)) {
111         /* If params are NULL use defaults. */
112         if (params == NULL) {
113             params = (I2C_Params *) &I2C_defaultParams;
114         }
115 
116         /* Get handle for this driver instance */
117         handle = (I2C_Handle)&(I2C_config[index]);
118         handle = handle->fxnTablePtr->openFxn(handle, params);
119     }
120 
121     return (handle);
122 }
123 
124 /*
125  *  ======== I2C_Params_init =======
126  */
I2C_Params_init(I2C_Params * params)127 void I2C_Params_init(I2C_Params *params)
128 {
129     *params = I2C_defaultParams;
130 }
131 
132 /*
133  *  ======== I2C_transfer ========
134  */
I2C_transfer(I2C_Handle handle,I2C_Transaction * transaction)135 bool I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction)
136 {
137     int_fast16_t result = (handle->fxnTablePtr->transferFxn(handle, transaction, I2C_WAIT_FOREVER));
138 
139     if(result == I2C_STATUS_SUCCESS) {
140         return (true);
141     }
142     else {
143         return (false);
144     }
145 }
146 
147 /*
148  *  ======== I2C_transferTimeout ========
149  */
I2C_transferTimeout(I2C_Handle handle,I2C_Transaction * transaction,uint32_t timeout)150 int_fast16_t I2C_transferTimeout(I2C_Handle handle, I2C_Transaction *transaction, uint32_t timeout)
151 {
152     return (handle->fxnTablePtr->transferFxn(handle, transaction, timeout));
153 }
154