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:
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 {
0 commit comments