1 /*
2  * Copyright 2019 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_common.h"
10 #include "fsl_i3c.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 #if 0
22     hal_i2c_master_transfer_callback_t callback;
23     void *callbackParam;
24 #endif
25     i3c_master_handle_t hardwareHandle;
26     uint8_t instance;
27 } hal_i3c_master_t;
28 
29 #if 0
30 /*! @brief i2c slave state structure. */
31 typedef struct _hal_i2c_slave
32 {
33     hal_i2c_slave_transfer_callback_t callback;
34     void *callbackParam;
35     hal_i3c_slave_transfer_t transfer;
36     i3c_slave_handle_t hardwareHandle;
37     uint8_t instance;
38 } hal_i3c_slave_t;
39 #endif
40 #ifdef HAL_I2C_MASTER_HANDLE_SIZE
41 #undef HAL_I2C_MASTER_HANDLE_SIZE
42 #endif
43 
44 #define HAL_I2C_MASTER_HANDLE_SIZE (400U)
45 /*******************************************************************************
46  * Prototypes
47  ******************************************************************************/
48 
49 /*******************************************************************************
50  * Variables
51  ******************************************************************************/
52 
53 /*! @brief Pointers to i2c bases for each instance. */
54 static I3C_Type *const s_i3cBases[] = I3C_BASE_PTRS;
55 
56 /*******************************************************************************
57  * Code
58  ******************************************************************************/
59 
HAL_I2cGetStatus(status_t status)60 static hal_i2c_status_t HAL_I2cGetStatus(status_t status)
61 {
62     hal_i2c_status_t returnStatus;
63     switch (status)
64     {
65         case kStatus_Success:
66         {
67             returnStatus = kStatus_HAL_I2cSuccess;
68             break;
69         }
70         case kStatus_I3C_Busy:
71         {
72             returnStatus = kStatus_HAL_I2cBusy;
73             break;
74         }
75         case kStatus_I3C_Idle:
76         {
77             returnStatus = kStatus_HAL_I2cIdle;
78             break;
79         }
80         case kStatus_I3C_Nak:
81         {
82             returnStatus = kStatus_HAL_I2cNak;
83             break;
84         }
85         case kStatus_I3C_Timeout:
86         {
87             returnStatus = kStatus_HAL_I2cTimeout;
88             break;
89         }
90         default:
91         {
92             returnStatus = kStatus_HAL_I2cError;
93             break;
94         }
95     }
96     return returnStatus;
97 }
98 
99 #if 0
100 static void HAL_I2cMasterCallback(I3C_Type *base, i3c_master_handle_t *handle, status_t status, void *callbackParam)
101 {
102     hal_i3c_master_t *i3cMasterHandle;
103     assert(callbackParam);
104 
105     i3cMasterHandle = (hal_i3c_master_t *)callbackParam;
106 
107     if (i3cMasterHandle->callback)
108     {
109         i3cMasterHandle->callback(i3cMasterHandle, HAL_I2cGetStatus(status), i3cMasterHandle->callbackParam);
110     }
111 }
112 
113 static void HAL_I2cSlaveCallback(I3C_Type *base, i3c_slave_transfer_t *xfer, void *callbackParam)
114 {
115     hal_i3c_slave_t *i2cSlaveHandle;
116     assert(callbackParam);
117 
118     i2cSlaveHandle = (hal_i3c_slave_t *)callbackParam;
119 
120     if (i2cSlaveHandle->callback)
121     {
122         i2cSlaveHandle->transfer.event            = (hal_i3c_slave_transfer_event_t)xfer->event;
123         i2cSlaveHandle->transfer.data             = xfer->data;
124         i2cSlaveHandle->transfer.dataSize         = xfer->dataSize;
125         i2cSlaveHandle->transfer.completionStatus = HAL_I2cGetStatus(xfer->completionStatus);
126         i2cSlaveHandle->transfer.transferredCount = xfer->transferredCount;
127         i2cSlaveHandle->callback(i2cSlaveHandle, &i2cSlaveHandle->transfer, i2cSlaveHandle->callbackParam);
128         xfer->data     = i2cSlaveHandle->transfer.data;
129         xfer->dataSize = i2cSlaveHandle->transfer.dataSize;
130     }
131 }
132 #endif
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_i3c_master_t *i3cMasterHandle;
137     i3c_master_config_t i3cConfig;
138 
139     assert(handle);
140     assert(halI2cConfig);
141 
142     assert(HAL_I2C_MASTER_HANDLE_SIZE >= sizeof(hal_i3c_master_t));
143 
144     i3cMasterHandle = (hal_i3c_master_t *)handle;
145 
146     I3C_MasterGetDefaultConfig(&i3cConfig);
147     i3cConfig.enableMaster        = (halI2cConfig->enableMaster ? kI3C_MasterOn : kI3C_MasterOff);
148     i3cConfig.baudRate_Hz.i2cBaud = halI2cConfig->baudRate_Bps;
149     i3cMasterHandle->instance     = halI2cConfig->instance;
150 
151     I3C_MasterInit(s_i3cBases[i3cMasterHandle->instance], &i3cConfig, halI2cConfig->srcClock_Hz);
152 
153     return kStatus_HAL_I2cSuccess;
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 #if 0
158     hal_i3c_slave_t *i2cSlaveHandle;
159     i3c_slave_config_t i3cConfig;
160 
161     assert(handle);
162     assert(halI2cConfig);
163 
164     if (HAL_I2C_SLAVE_HANDLE_SIZE < sizeof(hal_i3c_slave_t))
165     {
166         return kStatus_HAL_I2cError;
167     }
168 
169     i2cSlaveHandle = (hal_i3c_slave_t *)handle;
170 
171     I3C_SlaveGetDefaultConfig(&i3cConfig);
172     i3cConfig.enableSlave    = halI2cConfig->enableSlave;
173     i3cConfig.address0       = halI2cConfig->slaveAddress;
174     i2cSlaveHandle->instance = halI2cConfig->instance;
175 
176     I3C_SlaveInit(s_i3cBases[i2cSlaveHandle->instance], &i3cConfig, halI2cConfig->srcClock_Hz);
177 
178     return kStatus_HAL_I2cSuccess;
179 #else
180     return kStatus_HAL_I2cError;
181 #endif
182 }
183 
HAL_I2cMasterDeinit(hal_i2c_master_handle_t handle)184 hal_i2c_status_t HAL_I2cMasterDeinit(hal_i2c_master_handle_t handle)
185 {
186     hal_i3c_master_t *i3cMasterHandle;
187 
188     assert(handle);
189 
190     i3cMasterHandle = (hal_i3c_master_t *)handle;
191 
192     I3C_MasterDeinit(s_i3cBases[i3cMasterHandle->instance]);
193 
194     return kStatus_HAL_I2cSuccess;
195 }
196 
HAL_I2cSlaveDeinit(hal_i2c_slave_handle_t handle)197 hal_i2c_status_t HAL_I2cSlaveDeinit(hal_i2c_slave_handle_t handle)
198 {
199 #if 0
200     hal_i3c_slave_t *i2cSlaveHandle;
201 
202     assert(handle);
203 
204     i2cSlaveHandle = (hal_i3c_slave_t *)handle;
205 
206     I3C_SlaveDeinit(s_i3cBases[i2cSlaveHandle->instance]);
207 
208     return kStatus_HAL_I2cSuccess;
209 #else
210     return kStatus_HAL_I2cError;
211 #endif
212 }
213 
HAL_I2cMasterWriteBlocking(hal_i2c_master_handle_t handle,const uint8_t * txBuff,size_t txSize,uint32_t flags)214 hal_i2c_status_t HAL_I2cMasterWriteBlocking(hal_i2c_master_handle_t handle,
215                                             const uint8_t *txBuff,
216                                             size_t txSize,
217                                             uint32_t flags)
218 {
219     return kStatus_HAL_I2cError;
220 }
221 
HAL_I2cMasterReadBlocking(hal_i2c_master_handle_t handle,uint8_t * rxBuff,size_t rxSize,uint32_t flags)222 hal_i2c_status_t HAL_I2cMasterReadBlocking(hal_i2c_master_handle_t handle,
223                                            uint8_t *rxBuff,
224                                            size_t rxSize,
225                                            uint32_t flags)
226 {
227     return kStatus_HAL_I2cError;
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     return kStatus_HAL_I2cError;
233 }
234 
HAL_I2cSlaveReadBlocking(hal_i2c_slave_handle_t handle,uint8_t * rxBuff,size_t rxSize)235 hal_i2c_status_t HAL_I2cSlaveReadBlocking(hal_i2c_slave_handle_t handle, uint8_t *rxBuff, size_t rxSize)
236 {
237     return kStatus_HAL_I2cError;
238 }
239 
HAL_I2cMasterTransferBlocking(hal_i2c_master_handle_t handle,hal_i2c_master_transfer_t * xfer)240 hal_i2c_status_t HAL_I2cMasterTransferBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer)
241 {
242     hal_i3c_master_t *i3cMasterHandle;
243     i3c_master_transfer_t transfer;
244 
245     assert(handle);
246     assert(xfer);
247 
248     i3cMasterHandle = (hal_i3c_master_t *)handle;
249 
250     transfer.flags          = xfer->flags;
251     transfer.slaveAddress   = xfer->slaveAddress;
252     transfer.busType        = kI3C_TypeI2C;
253     transfer.direction      = (kHAL_I2cRead == xfer->direction) ? kI3C_Read : kI3C_Write;
254     transfer.subaddress     = xfer->subaddress;
255     transfer.subaddressSize = xfer->subaddressSize;
256     transfer.data           = xfer->data;
257     transfer.dataSize       = xfer->dataSize;
258 
259     return HAL_I2cGetStatus(I3C_MasterTransferBlocking(s_i3cBases[i3cMasterHandle->instance], &transfer));
260 }
261 
HAL_I2cMasterTransferInstallCallback(hal_i2c_master_handle_t handle,hal_i2c_master_transfer_callback_t callback,void * callbackParam)262 hal_i2c_status_t HAL_I2cMasterTransferInstallCallback(hal_i2c_master_handle_t handle,
263                                                       hal_i2c_master_transfer_callback_t callback,
264                                                       void *callbackParam)
265 {
266 #if 0
267     hal_i3c_master_t *i3cMasterHandle;
268 
269     assert(handle);
270 
271     i3cMasterHandle = (hal_i3c_master_t *)handle;
272 
273     i3cMasterHandle->callback      = callback;
274     i3cMasterHandle->callbackParam = callbackParam;
275     I3C_MasterTransferCreateHandle(s_i3cBases[i3cMasterHandle->instance], &i3cMasterHandle->hardwareHandle,
276                                      HAL_I2cMasterCallback, i3cMasterHandle);
277 
278     return kStatus_HAL_I2cSuccess;
279 #else
280     return kStatus_HAL_I2cError;
281 #endif
282 }
283 
HAL_I2cMasterTransferNonBlocking(hal_i2c_master_handle_t handle,hal_i2c_master_transfer_t * xfer)284 hal_i2c_status_t HAL_I2cMasterTransferNonBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer)
285 {
286 #if 0
287     hal_i3c_master_t *i3cMasterHandle;
288     i3c_master_transfer_t transfer;
289 
290     assert(handle);
291     assert(xfer);
292 
293     i3cMasterHandle = (hal_i3c_master_t *)handle;
294 
295     transfer.flags          = xfer->flags;
296     transfer.slaveAddress   = xfer->slaveAddress;
297     transfer.direction      = (kHAL_I2cRead == xfer->direction) ? kI3C_Read : kI3C_Write;
298     transfer.subaddress     = xfer->subaddress;
299     transfer.subaddressSize = xfer->subaddressSize;
300     transfer.data           = xfer->data;
301     transfer.dataSize       = xfer->dataSize;
302     return HAL_I2cGetStatus(I3C_MasterTransferNonBlocking(s_i3cBases[i3cMasterHandle->instance],
303                                                             &i3cMasterHandle->hardwareHandle, &transfer));
304 #else
305     return kStatus_HAL_I2cError;
306 #endif
307 }
308 
HAL_I2cMasterTransferGetCount(hal_i2c_master_handle_t handle,size_t * count)309 hal_i2c_status_t HAL_I2cMasterTransferGetCount(hal_i2c_master_handle_t handle, size_t *count)
310 {
311 #if 0
312     hal_i3c_master_t *i3cMasterHandle;
313 
314     assert(handle);
315     assert(count);
316 
317     i3cMasterHandle = (hal_i3c_master_t *)handle;
318     return HAL_I2cGetStatus(
319         I3C_MasterTransferGetCount(s_i3cBases[i3cMasterHandle->instance], &i3cMasterHandle->hardwareHandle, count));
320 #else
321     return kStatus_HAL_I2cError;
322 #endif
323 }
324 
HAL_I2cMasterTransferAbort(hal_i2c_master_handle_t handle)325 hal_i2c_status_t HAL_I2cMasterTransferAbort(hal_i2c_master_handle_t handle)
326 {
327 #if 0
328     hal_i3c_master_t *i3cMasterHandle;
329 
330     assert(handle);
331 
332     i3cMasterHandle = (hal_i3c_master_t *)handle;
333     I3C_MasterTransferAbort(s_i3cBases[i3cMasterHandle->instance], &i3cMasterHandle->hardwareHandle);
334 
335     return kStatus_HAL_I2cSuccess;
336 #else
337     return kStatus_HAL_I2cError;
338 #endif
339 }
340 
HAL_I2cSlaveTransferInstallCallback(hal_i2c_slave_handle_t handle,hal_i2c_slave_transfer_callback_t callback,void * callbackParam)341 hal_i2c_status_t HAL_I2cSlaveTransferInstallCallback(hal_i2c_slave_handle_t handle,
342                                                      hal_i2c_slave_transfer_callback_t callback,
343                                                      void *callbackParam)
344 {
345 #if 0
346     hal_i3c_slave_t *i2cSlaveHandle;
347 
348     assert(handle);
349 
350     i2cSlaveHandle = (hal_i3c_slave_t *)handle;
351 
352     i2cSlaveHandle->callback      = callback;
353     i2cSlaveHandle->callbackParam = callbackParam;
354     I3C_SlaveTransferCreateHandle(s_i3cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle,
355                                     HAL_I2cSlaveCallback, i2cSlaveHandle);
356 
357     return kStatus_HAL_I2cSuccess;
358 #else
359     return kStatus_HAL_I2cError;
360 #endif
361 }
362 
HAL_I2cSlaveTransferNonBlocking(hal_i2c_slave_handle_t handle,uint32_t eventMask)363 hal_i2c_status_t HAL_I2cSlaveTransferNonBlocking(hal_i2c_slave_handle_t handle, uint32_t eventMask)
364 {
365 #if 0
366     hal_i3c_slave_t *i2cSlaveHandle;
367 
368     assert(handle);
369 
370     i2cSlaveHandle = (hal_i3c_slave_t *)handle;
371 
372     return HAL_I2cGetStatus(I3C_SlaveTransferNonBlocking(s_i3cBases[i2cSlaveHandle->instance],
373                                                            &i2cSlaveHandle->hardwareHandle, eventMask));
374 #else
375     return kStatus_HAL_I2cError;
376 #endif
377 }
378 
HAL_I2cSlaveTransferAbort(hal_i2c_slave_handle_t handle)379 hal_i2c_status_t HAL_I2cSlaveTransferAbort(hal_i2c_slave_handle_t handle)
380 {
381 #if 0
382     hal_i3c_slave_t *i2cSlaveHandle;
383 
384     assert(handle);
385 
386     i2cSlaveHandle = (hal_i3c_slave_t *)handle;
387 
388     I3C_SlaveTransferAbort(s_i3cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle);
389 
390     return kStatus_HAL_I2cSuccess;
391 #else
392     return kStatus_HAL_I2cError;
393 #endif
394 }
395 
HAL_I2cSlaveTransferGetCount(hal_i2c_slave_handle_t handle,size_t * count)396 hal_i2c_status_t HAL_I2cSlaveTransferGetCount(hal_i2c_slave_handle_t handle, size_t *count)
397 {
398 #if 0
399     hal_i3c_slave_t *i2cSlaveHandle;
400 
401     assert(handle);
402     assert(count);
403 
404     i2cSlaveHandle = (hal_i3c_slave_t *)handle;
405 
406     return HAL_I2cGetStatus(
407         I3C_SlaveTransferGetCount(s_i3cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle, count));
408 #else
409     return kStatus_HAL_I2cError;
410 #endif
411 }
412