1 /*
2  * Copyright 2018 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_common.h"
10 #include "fsl_lpi2c.h"
11 
12 #include "fsl_adapter_i2c.h"
13 
14 /*******************************************************************************
15  * Definitions
16  ******************************************************************************/
17 
18 /*! @brief i2c master state structure. */
19 typedef struct _hal_i2c_master
20 {
21     hal_i2c_master_transfer_callback_t callback;
22     void *callbackParam;
23     lpi2c_master_handle_t hardwareHandle;
24     uint8_t instance;
25 } hal_i2c_master_t;
26 
27 /*! @brief i2c slave state structure. */
28 typedef struct _hal_i2c_slave
29 {
30     hal_i2c_slave_transfer_callback_t callback;
31     void *callbackParam;
32     hal_i2c_slave_transfer_t transfer;
33     lpi2c_slave_handle_t hardwareHandle;
34     uint8_t instance;
35 } hal_i2c_slave_t;
36 
37 /*******************************************************************************
38  * Prototypes
39  ******************************************************************************/
40 
41 /*******************************************************************************
42  * Variables
43  ******************************************************************************/
44 
45 /*! @brief Pointers to i2c bases for each instance. */
46 static LPI2C_Type *const s_i2cBases[] = LPI2C_BASE_PTRS;
47 
48 /*******************************************************************************
49  * Code
50  ******************************************************************************/
51 
HAL_I2cGetStatus(status_t status)52 static hal_i2c_status_t HAL_I2cGetStatus(status_t status)
53 {
54     hal_i2c_status_t returnStatus;
55     switch (status)
56     {
57         case kStatus_Success:
58         {
59             returnStatus = kStatus_HAL_I2cSuccess;
60             break;
61         }
62         case kStatus_LPI2C_Busy:
63         {
64             returnStatus = kStatus_HAL_I2cBusy;
65             break;
66         }
67         case kStatus_LPI2C_Idle:
68         {
69             returnStatus = kStatus_HAL_I2cIdle;
70             break;
71         }
72         case kStatus_LPI2C_Nak:
73         {
74             returnStatus = kStatus_HAL_I2cNak;
75             break;
76         }
77         case kStatus_LPI2C_ArbitrationLost:
78         {
79             returnStatus = kStatus_HAL_I2cArbitrationLost;
80             break;
81         }
82         case kStatus_LPI2C_Timeout:
83         {
84             returnStatus = kStatus_HAL_I2cTimeout;
85             break;
86         }
87         default:
88         {
89             returnStatus = kStatus_HAL_I2cError;
90             break;
91         }
92     }
93     return returnStatus;
94 }
95 
HAL_I2cMasterCallback(LPI2C_Type * base,lpi2c_master_handle_t * handle,status_t status,void * callbackParam)96 static void HAL_I2cMasterCallback(LPI2C_Type *base, lpi2c_master_handle_t *handle, status_t status, void *callbackParam)
97 {
98     hal_i2c_master_t *i2cMasterHandle;
99     assert(NULL != callbackParam);
100 
101     i2cMasterHandle = (hal_i2c_master_t *)callbackParam;
102 
103     if (NULL != i2cMasterHandle->callback)
104     {
105         i2cMasterHandle->callback(i2cMasterHandle, HAL_I2cGetStatus(status), i2cMasterHandle->callbackParam);
106     }
107 }
108 
HAL_I2cSlaveCallback(LPI2C_Type * base,lpi2c_slave_transfer_t * xfer,void * callbackParam)109 static void HAL_I2cSlaveCallback(LPI2C_Type *base, lpi2c_slave_transfer_t *xfer, void *callbackParam)
110 {
111     hal_i2c_slave_t *i2cSlaveHandle;
112     assert(NULL != callbackParam);
113 
114     i2cSlaveHandle = (hal_i2c_slave_t *)callbackParam;
115 
116     if (NULL != i2cSlaveHandle->callback)
117     {
118         i2cSlaveHandle->transfer.event            = (hal_i2c_slave_transfer_event_t)xfer->event;
119         i2cSlaveHandle->transfer.data             = xfer->data;
120         i2cSlaveHandle->transfer.dataSize         = xfer->dataSize;
121         i2cSlaveHandle->transfer.completionStatus = HAL_I2cGetStatus(xfer->completionStatus);
122         i2cSlaveHandle->transfer.transferredCount = xfer->transferredCount;
123         i2cSlaveHandle->callback(i2cSlaveHandle, &i2cSlaveHandle->transfer, i2cSlaveHandle->callbackParam);
124         xfer->data     = i2cSlaveHandle->transfer.data;
125         xfer->dataSize = i2cSlaveHandle->transfer.dataSize;
126     }
127 }
128 
HAL_I2cMasterInit(hal_i2c_master_handle_t handle,const hal_i2c_master_config_t * config)129 hal_i2c_status_t HAL_I2cMasterInit(hal_i2c_master_handle_t handle, const hal_i2c_master_config_t *config)
130 {
131     hal_i2c_master_t *i2cMasterHandle;
132     lpi2c_master_config_t i2cConfig;
133 
134     assert(NULL != handle);
135     assert(NULL != config);
136     assert(HAL_I2C_MASTER_HANDLE_SIZE >= sizeof(hal_i2c_master_t));
137 
138     i2cMasterHandle = (hal_i2c_master_t *)handle;
139 
140     LPI2C_MasterGetDefaultConfig(&i2cConfig);
141     i2cConfig.enableMaster    = config->enableMaster;
142     i2cConfig.baudRate_Hz     = config->baudRate_Bps;
143     i2cMasterHandle->instance = config->instance;
144 
145     LPI2C_MasterInit(s_i2cBases[i2cMasterHandle->instance], &i2cConfig, config->srcClock_Hz);
146 
147     return kStatus_HAL_I2cSuccess;
148 }
149 
HAL_I2cSlaveInit(hal_i2c_slave_handle_t handle,const hal_i2c_slave_config_t * config)150 hal_i2c_status_t HAL_I2cSlaveInit(hal_i2c_slave_handle_t handle, const hal_i2c_slave_config_t *config)
151 {
152     hal_i2c_slave_t *i2cSlaveHandle;
153     lpi2c_slave_config_t i2cConfig;
154 
155     assert(NULL != handle);
156     assert(NULL != config);
157     assert(HAL_I2C_SLAVE_HANDLE_SIZE >= sizeof(hal_i2c_slave_t));
158 
159     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
160 
161     LPI2C_SlaveGetDefaultConfig(&i2cConfig);
162     i2cConfig.enableSlave    = config->enableSlave;
163     i2cConfig.address0       = (uint8_t)config->slaveAddress;
164     i2cSlaveHandle->instance = config->instance;
165 
166     LPI2C_SlaveInit(s_i2cBases[i2cSlaveHandle->instance], &i2cConfig, config->srcClock_Hz);
167 
168     return kStatus_HAL_I2cSuccess;
169 }
170 
HAL_I2cMasterDeinit(hal_i2c_master_handle_t handle)171 hal_i2c_status_t HAL_I2cMasterDeinit(hal_i2c_master_handle_t handle)
172 {
173     hal_i2c_master_t *i2cMasterHandle;
174 
175     assert(NULL != handle);
176 
177     i2cMasterHandle = (hal_i2c_master_t *)handle;
178 
179     LPI2C_MasterDeinit(s_i2cBases[i2cMasterHandle->instance]);
180 
181     return kStatus_HAL_I2cSuccess;
182 }
183 
HAL_I2cSlaveDeinit(hal_i2c_slave_handle_t handle)184 hal_i2c_status_t HAL_I2cSlaveDeinit(hal_i2c_slave_handle_t handle)
185 {
186     hal_i2c_slave_t *i2cSlaveHandle;
187 
188     assert(NULL != handle);
189 
190     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
191 
192     LPI2C_SlaveDeinit(s_i2cBases[i2cSlaveHandle->instance]);
193 
194     return kStatus_HAL_I2cSuccess;
195 }
196 
HAL_I2cMasterWriteBlocking(hal_i2c_master_handle_t handle,const uint8_t * txBuff,size_t txSize,uint32_t flags)197 hal_i2c_status_t HAL_I2cMasterWriteBlocking(hal_i2c_master_handle_t handle,
198                                             const uint8_t *txBuff,
199                                             size_t txSize,
200                                             uint32_t flags)
201 {
202     return kStatus_HAL_I2cError;
203 }
204 
HAL_I2cMasterReadBlocking(hal_i2c_master_handle_t handle,uint8_t * rxBuff,size_t rxSize,uint32_t flags)205 hal_i2c_status_t HAL_I2cMasterReadBlocking(hal_i2c_master_handle_t handle,
206                                            uint8_t *rxBuff,
207                                            size_t rxSize,
208                                            uint32_t flags)
209 {
210     return kStatus_HAL_I2cError;
211 }
212 
HAL_I2cSlaveWriteBlocking(hal_i2c_slave_handle_t handle,const uint8_t * txBuff,size_t txSize)213 hal_i2c_status_t HAL_I2cSlaveWriteBlocking(hal_i2c_slave_handle_t handle, const uint8_t *txBuff, size_t txSize)
214 {
215     return kStatus_HAL_I2cError;
216 }
217 
HAL_I2cSlaveReadBlocking(hal_i2c_slave_handle_t handle,uint8_t * rxBuff,size_t rxSize)218 hal_i2c_status_t HAL_I2cSlaveReadBlocking(hal_i2c_slave_handle_t handle, uint8_t *rxBuff, size_t rxSize)
219 {
220     return kStatus_HAL_I2cError;
221 }
222 
HAL_I2cMasterTransferBlocking(hal_i2c_master_handle_t handle,hal_i2c_master_transfer_t * xfer)223 hal_i2c_status_t HAL_I2cMasterTransferBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer)
224 {
225     hal_i2c_master_t *i2cMasterHandle;
226     lpi2c_master_transfer_t transfer;
227 
228     assert(NULL != handle);
229     assert(NULL != xfer);
230 
231     i2cMasterHandle = (hal_i2c_master_t *)handle;
232 
233     transfer.flags          = xfer->flags;
234     transfer.slaveAddress   = xfer->slaveAddress;
235     transfer.direction      = (kHAL_I2cRead == xfer->direction) ? kLPI2C_Read : kLPI2C_Write;
236     transfer.subaddress     = xfer->subaddress;
237     transfer.subaddressSize = xfer->subaddressSize;
238     transfer.data           = xfer->data;
239     transfer.dataSize       = xfer->dataSize;
240 
241     return HAL_I2cGetStatus(LPI2C_MasterTransferBlocking(s_i2cBases[i2cMasterHandle->instance], &transfer));
242 }
243 
HAL_I2cMasterTransferInstallCallback(hal_i2c_master_handle_t handle,hal_i2c_master_transfer_callback_t callback,void * callbackParam)244 hal_i2c_status_t HAL_I2cMasterTransferInstallCallback(hal_i2c_master_handle_t handle,
245                                                       hal_i2c_master_transfer_callback_t callback,
246                                                       void *callbackParam)
247 {
248     hal_i2c_master_t *i2cMasterHandle;
249 
250     assert(NULL != handle);
251 
252     i2cMasterHandle = (hal_i2c_master_t *)handle;
253 
254     i2cMasterHandle->callback      = callback;
255     i2cMasterHandle->callbackParam = callbackParam;
256     LPI2C_MasterTransferCreateHandle(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle,
257                                      HAL_I2cMasterCallback, i2cMasterHandle);
258 
259     return kStatus_HAL_I2cSuccess;
260 }
261 
HAL_I2cMasterTransferNonBlocking(hal_i2c_master_handle_t handle,hal_i2c_master_transfer_t * xfer)262 hal_i2c_status_t HAL_I2cMasterTransferNonBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer)
263 {
264     hal_i2c_master_t *i2cMasterHandle;
265     lpi2c_master_transfer_t transfer;
266 
267     assert(NULL != handle);
268     assert(NULL != xfer);
269 
270     i2cMasterHandle = (hal_i2c_master_t *)handle;
271 
272     transfer.flags          = xfer->flags;
273     transfer.slaveAddress   = xfer->slaveAddress;
274     transfer.direction      = (kHAL_I2cRead == xfer->direction) ? kLPI2C_Read : kLPI2C_Write;
275     transfer.subaddress     = xfer->subaddress;
276     transfer.subaddressSize = xfer->subaddressSize;
277     transfer.data           = xfer->data;
278     transfer.dataSize       = xfer->dataSize;
279     return HAL_I2cGetStatus(LPI2C_MasterTransferNonBlocking(s_i2cBases[i2cMasterHandle->instance],
280                                                             &i2cMasterHandle->hardwareHandle, &transfer));
281 }
282 
HAL_I2cMasterTransferGetCount(hal_i2c_master_handle_t handle,size_t * count)283 hal_i2c_status_t HAL_I2cMasterTransferGetCount(hal_i2c_master_handle_t handle, size_t *count)
284 {
285     hal_i2c_master_t *i2cMasterHandle;
286 
287     assert(NULL != handle);
288     assert(NULL != count);
289 
290     i2cMasterHandle = (hal_i2c_master_t *)handle;
291     return HAL_I2cGetStatus(
292         LPI2C_MasterTransferGetCount(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle, count));
293 }
294 
HAL_I2cMasterTransferAbort(hal_i2c_master_handle_t handle)295 hal_i2c_status_t HAL_I2cMasterTransferAbort(hal_i2c_master_handle_t handle)
296 {
297     hal_i2c_master_t *i2cMasterHandle;
298 
299     assert(NULL != handle);
300 
301     i2cMasterHandle = (hal_i2c_master_t *)handle;
302     LPI2C_MasterTransferAbort(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle);
303 
304     return kStatus_HAL_I2cSuccess;
305 }
306 
HAL_I2cSlaveTransferInstallCallback(hal_i2c_slave_handle_t handle,hal_i2c_slave_transfer_callback_t callback,void * callbackParam)307 hal_i2c_status_t HAL_I2cSlaveTransferInstallCallback(hal_i2c_slave_handle_t handle,
308                                                      hal_i2c_slave_transfer_callback_t callback,
309                                                      void *callbackParam)
310 {
311     hal_i2c_slave_t *i2cSlaveHandle;
312 
313     assert(NULL != handle);
314 
315     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
316 
317     i2cSlaveHandle->callback      = callback;
318     i2cSlaveHandle->callbackParam = callbackParam;
319     LPI2C_SlaveTransferCreateHandle(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle,
320                                     HAL_I2cSlaveCallback, i2cSlaveHandle);
321 
322     return kStatus_HAL_I2cSuccess;
323 }
324 
HAL_I2cSlaveTransferNonBlocking(hal_i2c_slave_handle_t handle,uint32_t eventMask)325 hal_i2c_status_t HAL_I2cSlaveTransferNonBlocking(hal_i2c_slave_handle_t handle, uint32_t eventMask)
326 {
327     hal_i2c_slave_t *i2cSlaveHandle;
328 
329     assert(NULL != handle);
330 
331     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
332 
333     return HAL_I2cGetStatus(LPI2C_SlaveTransferNonBlocking(s_i2cBases[i2cSlaveHandle->instance],
334                                                            &i2cSlaveHandle->hardwareHandle, eventMask));
335 }
336 
HAL_I2cSlaveTransferAbort(hal_i2c_slave_handle_t handle)337 hal_i2c_status_t HAL_I2cSlaveTransferAbort(hal_i2c_slave_handle_t handle)
338 {
339     hal_i2c_slave_t *i2cSlaveHandle;
340 
341     assert(NULL != handle);
342 
343     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
344 
345     LPI2C_SlaveTransferAbort(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle);
346 
347     return kStatus_HAL_I2cSuccess;
348 }
349 
HAL_I2cSlaveTransferGetCount(hal_i2c_slave_handle_t handle,size_t * count)350 hal_i2c_status_t HAL_I2cSlaveTransferGetCount(hal_i2c_slave_handle_t handle, size_t *count)
351 {
352     hal_i2c_slave_t *i2cSlaveHandle;
353 
354     assert(NULL != handle);
355     assert(NULL != count);
356 
357     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
358 
359     return HAL_I2cGetStatus(
360         LPI2C_SlaveTransferGetCount(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle, count));
361 }
362