Skip to content

Commit bff2875

Browse files
committed
MCUXpresso: Update I2C SDK driver
Use the K66F driver that has the latest Signed-off-by: Mahesh Mahadevan <[email protected]>
1 parent 23bf7b7 commit bff2875

File tree

16 files changed

+1942
-1151
lines changed

16 files changed

+1942
-1151
lines changed

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/drivers/fsl_i2c.c

Lines changed: 250 additions & 126 deletions
Large diffs are not rendered by default.

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/drivers/fsl_i2c.h

Lines changed: 83 additions & 77 deletions
Large diffs are not rendered by default.

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/drivers/fsl_i2c_edma.c

Lines changed: 90 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2015, Freescale Semiconductor, Inc.
3-
* All rights reserved.
3+
* Copyright 2016-2017 NXP
44
*
55
* Redistribution and use in source and binary forms, with or without modification,
66
* are permitted provided that the following conditions are met:
@@ -12,7 +12,7 @@
1212
* list of conditions and the following disclaimer in the documentation and/or
1313
* other materials provided with the distribution.
1414
*
15-
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
15+
* o Neither the name of the copyright holder nor the names of its
1616
* contributors may be used to endorse or promote products derived from this
1717
* software without specific prior written permission.
1818
*
@@ -162,6 +162,26 @@ static void I2C_MasterTransferCallbackEDMA(edma_handle_t *handle, void *userData
162162
result = I2C_MasterStop(i2cPrivateHandle->base);
163163
}
164164
}
165+
else
166+
{
167+
if (i2cPrivateHandle->handle->transfer.direction == kI2C_Read)
168+
{
169+
/* Change to send NAK at the last byte. */
170+
i2cPrivateHandle->base->C1 |= I2C_C1_TXAK_MASK;
171+
172+
/* Wait the last data to be received. */
173+
while (!(i2cPrivateHandle->base->S & kI2C_TransferCompleteFlag))
174+
{
175+
}
176+
177+
/* Change direction to send. */
178+
i2cPrivateHandle->base->C1 |= I2C_C1_TX_MASK;
179+
180+
/* Read the last data byte. */
181+
*(i2cPrivateHandle->handle->transfer.data + i2cPrivateHandle->handle->transfer.dataSize - 1) =
182+
i2cPrivateHandle->base->D;
183+
}
184+
}
165185

166186
i2cPrivateHandle->handle->state = kIdleState;
167187

@@ -203,7 +223,6 @@ static status_t I2C_InitTransferStateMachineEDMA(I2C_Type *base,
203223
assert(xfer);
204224

205225
status_t result = kStatus_Success;
206-
uint16_t timeout = UINT16_MAX;
207226

208227
if (handle->state != kIdleState)
209228
{
@@ -221,16 +240,6 @@ static status_t I2C_InitTransferStateMachineEDMA(I2C_Type *base,
221240

222241
handle->state = kTransferDataState;
223242

224-
/* Wait until ready to complete. */
225-
while ((!(base->S & kI2C_TransferCompleteFlag)) && (--timeout))
226-
{
227-
}
228-
229-
/* Failed to start the transfer. */
230-
if (timeout == 0)
231-
{
232-
return kStatus_I2C_Timeout;
233-
}
234243
/* Clear all status before transfer. */
235244
I2C_MasterClearStatusFlags(base, kClearFlags);
236245

@@ -250,22 +259,55 @@ static status_t I2C_InitTransferStateMachineEDMA(I2C_Type *base,
250259
result = I2C_MasterStart(base, handle->transfer.slaveAddress, direction);
251260
}
252261

253-
/* Send subaddress. */
254-
if (handle->transfer.subaddressSize)
262+
if (result)
255263
{
256-
do
264+
return result;
265+
}
266+
267+
while (!(base->S & kI2C_IntPendingFlag))
268+
{
269+
}
270+
271+
/* Check if there's transfer error. */
272+
result = I2C_CheckAndClearError(base, base->S);
273+
274+
/* Return if error. */
275+
if (result)
276+
{
277+
if (result == kStatus_I2C_Nak)
257278
{
258-
/* Wait until data transfer complete. */
259-
while (!(base->S & kI2C_IntPendingFlag))
279+
result = kStatus_I2C_Addr_Nak;
280+
281+
if (I2C_MasterStop(base) != kStatus_Success)
260282
{
283+
result = kStatus_I2C_Timeout;
261284
}
262285

286+
if (handle->completionCallback)
287+
{
288+
(handle->completionCallback)(base, handle, result, handle->userData);
289+
}
290+
}
291+
292+
return result;
293+
}
294+
295+
/* Send subaddress. */
296+
if (handle->transfer.subaddressSize)
297+
{
298+
do
299+
{
263300
/* Clear interrupt pending flag. */
264301
base->S = kI2C_IntPendingFlag;
265302

266303
handle->transfer.subaddressSize--;
267304
base->D = ((handle->transfer.subaddress) >> (8 * handle->transfer.subaddressSize));
268305

306+
/* Wait until data transfer complete. */
307+
while (!(base->S & kI2C_IntPendingFlag))
308+
{
309+
}
310+
269311
/* Check if there's transfer error. */
270312
result = I2C_CheckAndClearError(base, base->S);
271313

@@ -278,34 +320,34 @@ static status_t I2C_InitTransferStateMachineEDMA(I2C_Type *base,
278320

279321
if (handle->transfer.direction == kI2C_Read)
280322
{
281-
/* Wait until data transfer complete. */
282-
while (!(base->S & kI2C_IntPendingFlag))
283-
{
284-
}
285-
286323
/* Clear pending flag. */
287324
base->S = kI2C_IntPendingFlag;
288325

289326
/* Send repeated start and slave address. */
290327
result = I2C_MasterRepeatedStart(base, handle->transfer.slaveAddress, kI2C_Read);
291-
}
292-
}
293328

294-
if (result)
295-
{
296-
return result;
297-
}
329+
if (result)
330+
{
331+
return result;
332+
}
298333

299-
/* Wait until data transfer complete. */
300-
while (!(base->S & kI2C_IntPendingFlag))
301-
{
334+
/* Wait until data transfer complete. */
335+
while (!(base->S & kI2C_IntPendingFlag))
336+
{
337+
}
338+
339+
/* Check if there's transfer error. */
340+
result = I2C_CheckAndClearError(base, base->S);
341+
342+
if (result)
343+
{
344+
return result;
345+
}
346+
}
302347
}
303348

304349
/* Clear pending flag. */
305350
base->S = kI2C_IntPendingFlag;
306-
307-
/* Check if there's transfer error. */
308-
result = I2C_CheckAndClearError(base, base->S);
309351
}
310352

311353
return result;
@@ -319,17 +361,7 @@ static void I2C_MasterTransferEDMAConfig(I2C_Type *base, i2c_master_edma_handle_
319361
{
320362
transfer_config.srcAddr = (uint32_t)I2C_GetDataRegAddr(base);
321363
transfer_config.destAddr = (uint32_t)(handle->transfer.data);
322-
323-
/* Send stop if kI2C_TransferNoStop flag is not asserted. */
324-
if (!(handle->transfer.flags & kI2C_TransferNoStopFlag))
325-
{
326-
transfer_config.majorLoopCounts = (handle->transfer.dataSize - 1);
327-
}
328-
else
329-
{
330-
transfer_config.majorLoopCounts = handle->transfer.dataSize;
331-
}
332-
364+
transfer_config.majorLoopCounts = (handle->transfer.dataSize - 1);
333365
transfer_config.srcTransferSize = kEDMA_TransferSize1Bytes;
334366
transfer_config.srcOffset = 0;
335367
transfer_config.destTransferSize = kEDMA_TransferSize1Bytes;
@@ -348,6 +380,9 @@ static void I2C_MasterTransferEDMAConfig(I2C_Type *base, i2c_master_edma_handle_
348380
transfer_config.minorLoopBytes = 1;
349381
}
350382

383+
/* Store the initially configured eDMA minor byte transfer count into the I2C handle */
384+
handle->nbytes = transfer_config.minorLoopBytes;
385+
351386
EDMA_SubmitTransfer(handle->dmaHandle, &transfer_config);
352387
EDMA_StartTransfer(handle->dmaHandle);
353388
}
@@ -427,7 +462,7 @@ status_t I2C_MasterTransferEDMA(I2C_Type *base, i2c_master_edma_handle_t *handle
427462
if (handle->transfer.direction == kI2C_Read)
428463
{
429464
/* Change direction for receive. */
430-
base->C1 &= ~I2C_C1_TX_MASK;
465+
base->C1 &= ~(I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
431466

432467
/* Read dummy to release the bus. */
433468
dummy = base->D;
@@ -479,6 +514,11 @@ status_t I2C_MasterTransferEDMA(I2C_Type *base, i2c_master_edma_handle_t *handle
479514
{
480515
result = I2C_MasterStop(base);
481516
}
517+
else
518+
{
519+
/* Change direction to send. */
520+
base->C1 |= I2C_C1_TX_MASK;
521+
}
482522

483523
/* Read the last byte of data. */
484524
if (handle->transfer.direction == kI2C_Read)
@@ -504,7 +544,9 @@ status_t I2C_MasterTransferGetCountEDMA(I2C_Type *base, i2c_master_edma_handle_t
504544

505545
if (kIdleState != handle->state)
506546
{
507-
*count = (handle->transferSize - EDMA_GetRemainingBytes(handle->dmaHandle->base, handle->dmaHandle->channel));
547+
*count = (handle->transferSize -
548+
(uint32_t)handle->nbytes *
549+
EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel));
508550
}
509551
else
510552
{

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/drivers/fsl_i2c_edma.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2015, Freescale Semiconductor, Inc.
3-
* All rights reserved.
3+
* Copyright 2016-2017 NXP
44
*
55
* Redistribution and use in source and binary forms, with or without modification,
66
* are permitted provided that the following conditions are met:
@@ -12,7 +12,7 @@
1212
* list of conditions and the following disclaimer in the documentation and/or
1313
* other materials provided with the distribution.
1414
*
15-
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
15+
* o Neither the name of the copyright holder nor the names of its
1616
* contributors may be used to endorse or promote products derived from this
1717
* software without specific prior written permission.
1818
*
@@ -39,7 +39,6 @@
3939
* @{
4040
*/
4141

42-
4342
/*******************************************************************************
4443
* Definitions
4544
******************************************************************************/
@@ -56,13 +55,14 @@ typedef void (*i2c_master_edma_transfer_callback_t)(I2C_Type *base,
5655
/*! @brief I2C master eDMA transfer structure. */
5756
struct _i2c_master_edma_handle
5857
{
59-
i2c_master_transfer_t transfer; /*!< I2C master transfer struct. */
58+
i2c_master_transfer_t transfer; /*!< I2C master transfer structure. */
6059
size_t transferSize; /*!< Total bytes to be transferred. */
60+
uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */
6161
uint8_t state; /*!< I2C master transfer status. */
6262
edma_handle_t *dmaHandle; /*!< The eDMA handler used. */
6363
i2c_master_edma_transfer_callback_t
64-
completionCallback; /*!< Callback function called after eDMA transfer finished. */
65-
void *userData; /*!< Callback parameter passed to callback function. */
64+
completionCallback; /*!< A callback function called after the eDMA transfer is finished. */
65+
void *userData; /*!< A callback parameter passed to the callback function. */
6666
};
6767

6868
/*******************************************************************************
@@ -79,12 +79,12 @@ extern "C" {
7979
*/
8080

8181
/*!
82-
* @brief Init the I2C handle which is used in transcational functions.
82+
* @brief Initializes the I2C handle which is used in transcational functions.
8383
*
8484
* @param base I2C peripheral base address.
85-
* @param handle pointer to i2c_master_edma_handle_t structure.
86-
* @param callback pointer to user callback function.
87-
* @param userData user param passed to the callback function.
85+
* @param handle A pointer to the i2c_master_edma_handle_t structure.
86+
* @param callback A pointer to the user callback function.
87+
* @param userData A user parameter passed to the callback function.
8888
* @param edmaHandle eDMA handle pointer.
8989
*/
9090
void I2C_MasterCreateEDMAHandle(I2C_Type *base,
@@ -97,30 +97,30 @@ void I2C_MasterCreateEDMAHandle(I2C_Type *base,
9797
* @brief Performs a master eDMA non-blocking transfer on the I2C bus.
9898
*
9999
* @param base I2C peripheral base address.
100-
* @param handle pointer to i2c_master_edma_handle_t structure.
101-
* @param xfer pointer to transfer structure of i2c_master_transfer_t.
102-
* @retval kStatus_Success Sucessully complete the data transmission.
103-
* @retval kStatus_I2C_Busy Previous transmission still not finished.
104-
* @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
100+
* @param handle A pointer to the i2c_master_edma_handle_t structure.
101+
* @param xfer A pointer to the transfer structure of i2c_master_transfer_t.
102+
* @retval kStatus_Success Sucessfully completed the data transmission.
103+
* @retval kStatus_I2C_Busy A previous transmission is still not finished.
104+
* @retval kStatus_I2C_Timeout Transfer error, waits for a signal timeout.
105105
* @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
106-
* @retval kStataus_I2C_Nak Transfer error, receive Nak during transfer.
106+
* @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
107107
*/
108108
status_t I2C_MasterTransferEDMA(I2C_Type *base, i2c_master_edma_handle_t *handle, i2c_master_transfer_t *xfer);
109109

110110
/*!
111-
* @brief Get master transfer status during a eDMA non-blocking transfer.
111+
* @brief Gets a master transfer status during the eDMA non-blocking transfer.
112112
*
113113
* @param base I2C peripheral base address.
114-
* @param handle pointer to i2c_master_edma_handle_t structure.
115-
* @param count Number of bytes transferred so far by the non-blocking transaction.
114+
* @param handle A pointer to the i2c_master_edma_handle_t structure.
115+
* @param count A number of bytes transferred by the non-blocking transaction.
116116
*/
117117
status_t I2C_MasterTransferGetCountEDMA(I2C_Type *base, i2c_master_edma_handle_t *handle, size_t *count);
118118

119119
/*!
120-
* @brief Abort a master eDMA non-blocking transfer in a early time.
120+
* @brief Aborts a master eDMA non-blocking transfer early.
121121
*
122122
* @param base I2C peripheral base address.
123-
* @param handle pointer to i2c_master_edma_handle_t structure.
123+
* @param handle A pointer to the i2c_master_edma_handle_t structure.
124124
*/
125125
void I2C_MasterTransferAbortEDMA(I2C_Type *base, i2c_master_edma_handle_t *handle);
126126

0 commit comments

Comments
 (0)