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