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_i2c.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     i2c_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     i2c_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 I2C_Type *const s_i2cBases[] = I2C_BASE_PTRS;
47 
48 /*******************************************************************************
49  * Code
50  ******************************************************************************/
51 
HAL_I2cGetStatus(status_t status)52 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_I2C_Busy:
63         {
64             returnStatus = kStatus_HAL_I2cBusy;
65             break;
66         }
67         case kStatus_I2C_Idle:
68         {
69             returnStatus = kStatus_HAL_I2cIdle;
70             break;
71         }
72         case kStatus_I2C_Nak:
73         {
74             returnStatus = kStatus_HAL_I2cNak;
75             break;
76         }
77         case kStatus_I2C_ArbitrationLost:
78         {
79             returnStatus = kStatus_HAL_I2cArbitrationLost;
80             break;
81         }
82         case kStatus_I2C_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(I2C_Type * base,i2c_master_handle_t * handle,status_t status,void * callbackParam)96 static void HAL_I2cMasterCallback(I2C_Type *base, i2c_master_handle_t *handle, status_t status, void *callbackParam)
97 {
98     hal_i2c_master_t *i2cMasterHandle;
99     assert(callbackParam);
100 
101     i2cMasterHandle = (hal_i2c_master_t *)callbackParam;
102 
103     if (i2cMasterHandle->callback)
104     {
105         i2cMasterHandle->callback(i2cMasterHandle, HAL_I2cGetStatus(status), i2cMasterHandle->callbackParam);
106     }
107 }
108 
HAL_I2cSlaveCallback(I2C_Type * base,volatile i2c_slave_transfer_t * xfer,void * callbackParam)109 static void HAL_I2cSlaveCallback(I2C_Type *base, volatile i2c_slave_transfer_t *xfer, void *callbackParam)
110 {
111     hal_i2c_slave_t *i2cSlaveHandle;
112     assert(callbackParam);
113 
114     i2cSlaveHandle = (hal_i2c_slave_t *)callbackParam;
115 
116     if (i2cSlaveHandle->callback)
117     {
118         i2cSlaveHandle->transfer.event = (hal_i2c_slave_transfer_event_t)xfer->event;
119         if (kI2C_SlaveTransmitEvent == xfer->event)
120         {
121             i2cSlaveHandle->transfer.data     = (uint8_t *)xfer->txData;
122             i2cSlaveHandle->transfer.dataSize = xfer->txSize;
123         }
124         else
125         {
126             i2cSlaveHandle->transfer.data     = xfer->rxData;
127             i2cSlaveHandle->transfer.dataSize = xfer->rxSize;
128         }
129 
130         i2cSlaveHandle->transfer.completionStatus = HAL_I2cGetStatus(xfer->completionStatus);
131         i2cSlaveHandle->transfer.transferredCount = xfer->transferredCount;
132         i2cSlaveHandle->callback(i2cSlaveHandle, &i2cSlaveHandle->transfer, i2cSlaveHandle->callbackParam);
133         if (kI2C_SlaveTransmitEvent == xfer->event)
134         {
135             xfer->txData = i2cSlaveHandle->transfer.data;
136             xfer->txSize = i2cSlaveHandle->transfer.dataSize;
137         }
138         else
139         {
140             xfer->rxData = i2cSlaveHandle->transfer.data;
141             xfer->rxSize = i2cSlaveHandle->transfer.dataSize;
142         }
143     }
144 }
145 
HAL_I2cMasterInit(hal_i2c_master_handle_t handle,const hal_i2c_master_config_t * halI2cConfig)146 hal_i2c_status_t HAL_I2cMasterInit(hal_i2c_master_handle_t handle, const hal_i2c_master_config_t *halI2cConfig)
147 {
148     hal_i2c_master_t *i2cMasterHandle;
149     i2c_master_config_t i2cConfig;
150 
151     assert(handle);
152     assert(halI2cConfig);
153 
154     if (HAL_I2C_MASTER_HANDLE_SIZE < sizeof(hal_i2c_master_t))
155     {
156         return kStatus_HAL_I2cError;
157     }
158 
159     i2cMasterHandle = (hal_i2c_master_t *)handle;
160 
161     I2C_MasterGetDefaultConfig(&i2cConfig);
162     i2cConfig.enableMaster    = halI2cConfig->enableMaster;
163     i2cConfig.baudRate_Bps    = halI2cConfig->baudRate_Bps;
164     i2cMasterHandle->instance = halI2cConfig->instance;
165 
166     I2C_MasterInit(s_i2cBases[i2cMasterHandle->instance], &i2cConfig, halI2cConfig->srcClock_Hz);
167 
168     return kStatus_HAL_I2cSuccess;
169 }
170 
HAL_I2cSlaveInit(hal_i2c_slave_handle_t handle,const hal_i2c_slave_config_t * halI2cConfig)171 hal_i2c_status_t HAL_I2cSlaveInit(hal_i2c_slave_handle_t handle, const hal_i2c_slave_config_t *halI2cConfig)
172 {
173     hal_i2c_slave_t *i2cSlaveHandle;
174     i2c_slave_config_t i2cConfig;
175 
176     assert(handle);
177     assert(halI2cConfig);
178 
179     if (HAL_I2C_SLAVE_HANDLE_SIZE < sizeof(hal_i2c_slave_t))
180     {
181         return kStatus_HAL_I2cError;
182     }
183 
184     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
185 
186     I2C_SlaveGetDefaultConfig(&i2cConfig);
187     i2cConfig.enableSlave      = halI2cConfig->enableSlave;
188     i2cConfig.address0.address = halI2cConfig->slaveAddress;
189     i2cSlaveHandle->instance   = halI2cConfig->instance;
190 
191     I2C_SlaveInit(s_i2cBases[i2cSlaveHandle->instance], &i2cConfig, halI2cConfig->srcClock_Hz);
192 
193     return kStatus_HAL_I2cSuccess;
194 }
195 
HAL_I2cMasterDeinit(hal_i2c_master_handle_t handle)196 hal_i2c_status_t HAL_I2cMasterDeinit(hal_i2c_master_handle_t handle)
197 {
198     hal_i2c_master_t *i2cMasterHandle;
199 
200     assert(handle);
201 
202     i2cMasterHandle = (hal_i2c_master_t *)handle;
203 
204     I2C_MasterDeinit(s_i2cBases[i2cMasterHandle->instance]);
205 
206     return kStatus_HAL_I2cSuccess;
207 }
208 
HAL_I2cSlaveDeinit(hal_i2c_slave_handle_t handle)209 hal_i2c_status_t HAL_I2cSlaveDeinit(hal_i2c_slave_handle_t handle)
210 {
211     hal_i2c_slave_t *i2cSlaveHandle;
212 
213     assert(handle);
214 
215     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
216 
217     I2C_SlaveDeinit(s_i2cBases[i2cSlaveHandle->instance]);
218 
219     return kStatus_HAL_I2cSuccess;
220 }
221 
HAL_I2cMasterWriteBlocking(hal_i2c_master_handle_t handle,const uint8_t * txBuff,size_t txSize,uint32_t flags)222 hal_i2c_status_t HAL_I2cMasterWriteBlocking(hal_i2c_master_handle_t handle,
223                                             const uint8_t *txBuff,
224                                             size_t txSize,
225                                             uint32_t flags)
226 {
227     hal_i2c_master_t *i2cMasterHandle;
228 
229     assert(handle);
230 
231     i2cMasterHandle = (hal_i2c_master_t *)handle;
232 
233     return HAL_I2cGetStatus(I2C_MasterWriteBlocking(s_i2cBases[i2cMasterHandle->instance], txBuff, txSize, flags));
234 }
235 
HAL_I2cMasterReadBlocking(hal_i2c_master_handle_t handle,uint8_t * rxBuff,size_t rxSize,uint32_t flags)236 hal_i2c_status_t HAL_I2cMasterReadBlocking(hal_i2c_master_handle_t handle,
237                                            uint8_t *rxBuff,
238                                            size_t rxSize,
239                                            uint32_t flags)
240 {
241     hal_i2c_master_t *i2cMasterHandle;
242 
243     assert(handle);
244 
245     i2cMasterHandle = (hal_i2c_master_t *)handle;
246 
247     return HAL_I2cGetStatus(I2C_MasterReadBlocking(s_i2cBases[i2cMasterHandle->instance], rxBuff, rxSize, flags));
248 }
249 
HAL_I2cSlaveWriteBlocking(hal_i2c_slave_handle_t handle,const uint8_t * txBuff,size_t txSize)250 hal_i2c_status_t HAL_I2cSlaveWriteBlocking(hal_i2c_slave_handle_t handle, const uint8_t *txBuff, size_t txSize)
251 {
252     hal_i2c_slave_t *i2cSlaveHandle;
253 
254     assert(handle);
255 
256     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
257 
258     return HAL_I2cGetStatus(I2C_SlaveWriteBlocking(s_i2cBases[i2cSlaveHandle->instance], txBuff, txSize));
259 }
260 
HAL_I2cSlaveReadBlocking(hal_i2c_slave_handle_t handle,uint8_t * rxBuff,size_t rxSize)261 hal_i2c_status_t HAL_I2cSlaveReadBlocking(hal_i2c_slave_handle_t handle, uint8_t *rxBuff, size_t rxSize)
262 {
263     hal_i2c_slave_t *i2cSlaveHandle;
264 
265     assert(handle);
266 
267     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
268 
269     return HAL_I2cGetStatus(I2C_SlaveReadBlocking(s_i2cBases[i2cSlaveHandle->instance], rxBuff, rxSize));
270 }
271 
HAL_I2cMasterTransferBlocking(hal_i2c_master_handle_t handle,hal_i2c_master_transfer_t * xfer)272 hal_i2c_status_t HAL_I2cMasterTransferBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer)
273 {
274     hal_i2c_master_t *i2cMasterHandle;
275     i2c_master_transfer_t transfer;
276 
277     assert(handle);
278     assert(xfer);
279 
280     i2cMasterHandle = (hal_i2c_master_t *)handle;
281 
282     transfer.flags          = xfer->flags;
283     transfer.slaveAddress   = xfer->slaveAddress;
284     transfer.direction      = (kHAL_I2cRead == xfer->direction) ? kI2C_Read : kI2C_Write;
285     transfer.subaddress     = xfer->subaddress;
286     transfer.subaddressSize = xfer->subaddressSize;
287     transfer.data           = xfer->data;
288     transfer.dataSize       = xfer->dataSize;
289 
290     return HAL_I2cGetStatus(I2C_MasterTransferBlocking(s_i2cBases[i2cMasterHandle->instance], &transfer));
291 }
292 
HAL_I2cMasterTransferInstallCallback(hal_i2c_master_handle_t handle,hal_i2c_master_transfer_callback_t callback,void * callbackParam)293 hal_i2c_status_t HAL_I2cMasterTransferInstallCallback(hal_i2c_master_handle_t handle,
294                                                       hal_i2c_master_transfer_callback_t callback,
295                                                       void *callbackParam)
296 {
297     hal_i2c_master_t *i2cMasterHandle;
298 
299     assert(handle);
300 
301     i2cMasterHandle = (hal_i2c_master_t *)handle;
302 
303     i2cMasterHandle->callback      = callback;
304     i2cMasterHandle->callbackParam = callbackParam;
305     I2C_MasterTransferCreateHandle(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle,
306                                    HAL_I2cMasterCallback, i2cMasterHandle);
307 
308     return kStatus_HAL_I2cSuccess;
309 }
310 
HAL_I2cMasterTransferNonBlocking(hal_i2c_master_handle_t handle,hal_i2c_master_transfer_t * xfer)311 hal_i2c_status_t HAL_I2cMasterTransferNonBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer)
312 {
313     hal_i2c_master_t *i2cMasterHandle;
314     i2c_master_transfer_t transfer;
315 
316     assert(handle);
317     assert(xfer);
318 
319     i2cMasterHandle = (hal_i2c_master_t *)handle;
320 
321     transfer.flags          = xfer->flags;
322     transfer.slaveAddress   = xfer->slaveAddress;
323     transfer.direction      = (kHAL_I2cRead == xfer->direction) ? kI2C_Read : kI2C_Write;
324     transfer.subaddress     = xfer->subaddress;
325     transfer.subaddressSize = xfer->subaddressSize;
326     transfer.data           = xfer->data;
327     transfer.dataSize       = xfer->dataSize;
328     return HAL_I2cGetStatus(I2C_MasterTransferNonBlocking(s_i2cBases[i2cMasterHandle->instance],
329                                                           &i2cMasterHandle->hardwareHandle, &transfer));
330 }
331 
HAL_I2cMasterTransferGetCount(hal_i2c_master_handle_t handle,size_t * count)332 hal_i2c_status_t HAL_I2cMasterTransferGetCount(hal_i2c_master_handle_t handle, size_t *count)
333 {
334     hal_i2c_master_t *i2cMasterHandle;
335 
336     assert(handle);
337     assert(count);
338 
339     i2cMasterHandle = (hal_i2c_master_t *)handle;
340     return HAL_I2cGetStatus(
341         I2C_MasterTransferGetCount(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle, count));
342 }
343 
HAL_I2cMasterTransferAbort(hal_i2c_master_handle_t handle)344 hal_i2c_status_t HAL_I2cMasterTransferAbort(hal_i2c_master_handle_t handle)
345 {
346     hal_i2c_master_t *i2cMasterHandle;
347 
348     assert(handle);
349 
350     i2cMasterHandle = (hal_i2c_master_t *)handle;
351     return HAL_I2cGetStatus(
352         I2C_MasterTransferAbort(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle));
353 }
354 
HAL_I2cSlaveTransferInstallCallback(hal_i2c_slave_handle_t handle,hal_i2c_slave_transfer_callback_t callback,void * callbackParam)355 hal_i2c_status_t HAL_I2cSlaveTransferInstallCallback(hal_i2c_slave_handle_t handle,
356                                                      hal_i2c_slave_transfer_callback_t callback,
357                                                      void *callbackParam)
358 {
359     hal_i2c_slave_t *i2cSlaveHandle;
360 
361     assert(handle);
362 
363     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
364 
365     i2cSlaveHandle->callback      = callback;
366     i2cSlaveHandle->callbackParam = callbackParam;
367     I2C_SlaveTransferCreateHandle(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle,
368                                   HAL_I2cSlaveCallback, i2cSlaveHandle);
369 
370     return kStatus_HAL_I2cSuccess;
371 }
372 
HAL_I2cSlaveTransferNonBlocking(hal_i2c_slave_handle_t handle,uint32_t eventMask)373 hal_i2c_status_t HAL_I2cSlaveTransferNonBlocking(hal_i2c_slave_handle_t handle, uint32_t eventMask)
374 {
375     hal_i2c_slave_t *i2cSlaveHandle;
376 
377     assert(handle);
378 
379     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
380 
381     return HAL_I2cGetStatus(
382         I2C_SlaveTransferNonBlocking(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle, eventMask));
383 }
384 
HAL_I2cSlaveTransferAbort(hal_i2c_slave_handle_t handle)385 hal_i2c_status_t HAL_I2cSlaveTransferAbort(hal_i2c_slave_handle_t handle)
386 {
387     hal_i2c_slave_t *i2cSlaveHandle;
388 
389     assert(handle);
390 
391     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
392 
393     I2C_SlaveTransferAbort(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle);
394 
395     return kStatus_HAL_I2cSuccess;
396 }
397 
HAL_I2cSlaveTransferGetCount(hal_i2c_slave_handle_t handle,size_t * count)398 hal_i2c_status_t HAL_I2cSlaveTransferGetCount(hal_i2c_slave_handle_t handle, size_t *count)
399 {
400     hal_i2c_slave_t *i2cSlaveHandle;
401 
402     assert(handle);
403     assert(count);
404 
405     i2cSlaveHandle = (hal_i2c_slave_t *)handle;
406 
407     return HAL_I2cGetStatus(
408         I2C_SlaveTransferGetCount(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle, count));
409 }
410