You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

618 lines
20 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2019 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_i2c_edma.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /* Component ID definition, used by tools. */
  13. #ifndef FSL_COMPONENT_ID
  14. #define FSL_COMPONENT_ID "platform.drivers.i2c_edma"
  15. #endif
  16. /*<! @breif Structure definition for i2c_master_edma_private_handle_t. The structure is private. */
  17. typedef struct _i2c_master_edma_private_handle
  18. {
  19. I2C_Type *base;
  20. i2c_master_edma_handle_t *handle;
  21. } i2c_master_edma_private_handle_t;
  22. /*! @brief i2c master DMA transfer state. */
  23. enum _i2c_master_dma_transfer_states
  24. {
  25. kIdleState = 0x0U, /*!< I2C bus idle. */
  26. kTransferDataState = 0x1U, /*!< 7-bit address check state. */
  27. };
  28. /*******************************************************************************
  29. * Prototypes
  30. ******************************************************************************/
  31. /*!
  32. * @brief EDMA callback for I2C master EDMA driver.
  33. *
  34. * @param handle EDMA handler for I2C master EDMA driver
  35. * @param userData user param passed to the callback function
  36. */
  37. static void I2C_MasterTransferCallbackEDMA(edma_handle_t *handle, void *userData, bool transferDone, uint32_t tcds);
  38. /*!
  39. * @brief Check and clear status operation.
  40. *
  41. * @param base I2C peripheral base address.
  42. * @param status current i2c hardware status.
  43. * @retval kStatus_Success No error found.
  44. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  45. * @retval kStatus_I2C_Nak Received Nak error.
  46. */
  47. static status_t I2C_CheckAndClearError(I2C_Type *base, uint32_t status);
  48. /*!
  49. * @brief EDMA config for I2C master driver.
  50. *
  51. * @param base I2C peripheral base address.
  52. * @param handle pointer to i2c_master_edma_handle_t structure which stores the transfer state
  53. */
  54. static void I2C_MasterTransferEDMAConfig(I2C_Type *base, i2c_master_edma_handle_t *handle);
  55. /*!
  56. * @brief Set up master transfer, send slave address and sub address(if any), wait until the
  57. * wait until address sent status return.
  58. *
  59. * @param base I2C peripheral base address.
  60. * @param handle pointer to i2c_master_edma_handle_t structure which stores the transfer state
  61. * @param xfer pointer to i2c_master_transfer_t structure
  62. */
  63. static status_t I2C_InitTransferStateMachineEDMA(I2C_Type *base,
  64. i2c_master_edma_handle_t *handle,
  65. i2c_master_transfer_t *xfer);
  66. /*******************************************************************************
  67. * Variables
  68. ******************************************************************************/
  69. /*<! Private handle only used for internally. */
  70. static i2c_master_edma_private_handle_t s_i2cEdmaPrivateHandle[FSL_FEATURE_SOC_I2C_COUNT];
  71. /*******************************************************************************
  72. * Codes
  73. ******************************************************************************/
  74. static void I2C_MasterTransferCallbackEDMA(edma_handle_t *handle, void *userData, bool transferDone, uint32_t tcds)
  75. {
  76. i2c_master_edma_private_handle_t *i2cPrivateHandle = (i2c_master_edma_private_handle_t *)userData;
  77. status_t result = kStatus_Success;
  78. uint8_t tmpReg;
  79. size_t tmpdataSize;
  80. /* Disable DMA. */
  81. I2C_EnableDMA(i2cPrivateHandle->base, false);
  82. /* Send stop if kI2C_TransferNoStop flag is not asserted. */
  83. if (0U == (i2cPrivateHandle->handle->transfer.flags & (uint32_t)kI2C_TransferNoStopFlag))
  84. {
  85. if (i2cPrivateHandle->handle->transfer.direction == kI2C_Read)
  86. {
  87. /* Change to send NAK at the last byte. */
  88. i2cPrivateHandle->base->C1 |= I2C_C1_TXAK_MASK;
  89. /* Wait the last data to be received. */
  90. while (0U == (i2cPrivateHandle->base->S & (uint8_t)kI2C_TransferCompleteFlag))
  91. {
  92. }
  93. /* Send stop signal. */
  94. result = I2C_MasterStop(i2cPrivateHandle->base);
  95. /* Read the last data byte. */
  96. tmpReg = i2cPrivateHandle->base->D;
  97. tmpdataSize = i2cPrivateHandle->handle->transfer.dataSize;
  98. *(i2cPrivateHandle->handle->transfer.data + tmpdataSize - 1U) = tmpReg;
  99. }
  100. else
  101. {
  102. /* Wait the last data to be sent. */
  103. while (0U == (i2cPrivateHandle->base->S & (uint8_t)kI2C_TransferCompleteFlag))
  104. {
  105. }
  106. /* Send stop signal. */
  107. result = I2C_MasterStop(i2cPrivateHandle->base);
  108. }
  109. }
  110. else
  111. {
  112. if (i2cPrivateHandle->handle->transfer.direction == kI2C_Read)
  113. {
  114. /* Change to send NAK at the last byte. */
  115. i2cPrivateHandle->base->C1 |= I2C_C1_TXAK_MASK;
  116. /* Wait the last data to be received. */
  117. while (0U == (i2cPrivateHandle->base->S & (uint8_t)kI2C_TransferCompleteFlag))
  118. {
  119. }
  120. /* Change direction to send. */
  121. i2cPrivateHandle->base->C1 |= I2C_C1_TX_MASK;
  122. /* Read the last data byte. */
  123. tmpReg = i2cPrivateHandle->base->D;
  124. tmpdataSize = i2cPrivateHandle->handle->transfer.dataSize;
  125. *(i2cPrivateHandle->handle->transfer.data + tmpdataSize - 1U) = tmpReg;
  126. }
  127. }
  128. i2cPrivateHandle->handle->state = (uint8_t)kIdleState;
  129. if (NULL != i2cPrivateHandle->handle->completionCallback)
  130. {
  131. i2cPrivateHandle->handle->completionCallback(i2cPrivateHandle->base, i2cPrivateHandle->handle, result,
  132. i2cPrivateHandle->handle->userData);
  133. }
  134. }
  135. static status_t I2C_CheckAndClearError(I2C_Type *base, uint32_t status)
  136. {
  137. status_t result = kStatus_Success;
  138. /* Check arbitration lost. */
  139. if (0U != (status & (uint32_t)kI2C_ArbitrationLostFlag))
  140. {
  141. /* Clear arbitration lost flag. */
  142. base->S = (uint8_t)kI2C_ArbitrationLostFlag;
  143. result = kStatus_I2C_ArbitrationLost;
  144. }
  145. /* Check NAK */
  146. else if (0U != (status & (uint32_t)kI2C_ReceiveNakFlag))
  147. {
  148. result = kStatus_I2C_Nak;
  149. }
  150. else
  151. {
  152. /* Add this to fix MISRA C2012 rule15.7 issue: Empty else without comment. */
  153. }
  154. return result;
  155. }
  156. static status_t I2C_InitTransferStateMachineEDMA(I2C_Type *base,
  157. i2c_master_edma_handle_t *handle,
  158. i2c_master_transfer_t *xfer)
  159. {
  160. assert(NULL != handle);
  161. assert(NULL != xfer);
  162. status_t result = kStatus_Success;
  163. #if I2C_RETRY_TIMES
  164. uint32_t waitTimes = I2C_RETRY_TIMES;
  165. #endif
  166. if (handle->state != (uint8_t)kIdleState)
  167. {
  168. return kStatus_I2C_Busy;
  169. }
  170. else
  171. {
  172. i2c_direction_t direction = xfer->direction;
  173. /* Init the handle member. */
  174. handle->transfer = *xfer;
  175. /* Save total transfer size. */
  176. handle->transferSize = xfer->dataSize;
  177. handle->state = (uint8_t)kTransferDataState;
  178. /* Clear all status before transfer. */
  179. I2C_MasterClearStatusFlags(base, (uint32_t)kClearFlags);
  180. /* Change to send write address when it's a read operation with command. */
  181. if ((xfer->subaddressSize > 0U) && (0U != (uint8_t)(xfer->direction == kI2C_Read)))
  182. {
  183. direction = kI2C_Write;
  184. }
  185. /* If repeated start is requested, send repeated start. */
  186. if (0U != (handle->transfer.flags & (uint32_t)kI2C_TransferRepeatedStartFlag))
  187. {
  188. result = I2C_MasterRepeatedStart(base, handle->transfer.slaveAddress, direction);
  189. }
  190. else /* For normal transfer, send start. */
  191. {
  192. result = I2C_MasterStart(base, handle->transfer.slaveAddress, direction);
  193. }
  194. if (kStatus_Success != result)
  195. {
  196. return result;
  197. }
  198. #if I2C_RETRY_TIMES
  199. while ((0U == (base->S & (uint8_t)kI2C_IntPendingFlag)) && (0U != --waitTimes))
  200. {
  201. }
  202. if (waitTimes == 0)
  203. {
  204. return kStatus_I2C_Timeout;
  205. }
  206. #else
  207. while (0U == (base->S & (uint8_t)kI2C_IntPendingFlag))
  208. {
  209. }
  210. #endif
  211. /* Check if there's transfer error. */
  212. result = I2C_CheckAndClearError(base, base->S);
  213. /* Return if error. */
  214. if (kStatus_Success != result)
  215. {
  216. if (result == kStatus_I2C_Nak)
  217. {
  218. result = kStatus_I2C_Addr_Nak;
  219. if (I2C_MasterStop(base) != kStatus_Success)
  220. {
  221. result = kStatus_I2C_Timeout;
  222. }
  223. if (NULL != handle->completionCallback)
  224. {
  225. (handle->completionCallback)(base, handle, result, handle->userData);
  226. }
  227. }
  228. return result;
  229. }
  230. /* Send subaddress. */
  231. if (0U != handle->transfer.subaddressSize)
  232. {
  233. do
  234. {
  235. /* Clear interrupt pending flag. */
  236. base->S = (uint8_t)kI2C_IntPendingFlag;
  237. handle->transfer.subaddressSize--;
  238. base->D = (uint8_t)((handle->transfer.subaddress) >> (8U * handle->transfer.subaddressSize));
  239. /* Wait until data transfer complete. */
  240. #if I2C_RETRY_TIMES
  241. waitTimes = I2C_RETRY_TIMES;
  242. while ((0U == (base->S & (uint8_t)kI2C_IntPendingFlag)) && (0U != --waitTimes))
  243. {
  244. }
  245. if (waitTimes == 0)
  246. {
  247. return kStatus_I2C_Timeout;
  248. }
  249. #else
  250. while (0U == (base->S & (uint8_t)kI2C_IntPendingFlag))
  251. {
  252. }
  253. #endif
  254. /* Check if there's transfer error. */
  255. result = I2C_CheckAndClearError(base, base->S);
  256. if (0 != result)
  257. {
  258. return result;
  259. }
  260. } while (handle->transfer.subaddressSize > 0U);
  261. if (handle->transfer.direction == kI2C_Read)
  262. {
  263. /* Clear pending flag. */
  264. base->S = (uint8_t)kI2C_IntPendingFlag;
  265. /* Send repeated start and slave address. */
  266. result = I2C_MasterRepeatedStart(base, handle->transfer.slaveAddress, kI2C_Read);
  267. if (0 != result)
  268. {
  269. return result;
  270. }
  271. /* Wait until data transfer complete. */
  272. #if I2C_RETRY_TIMES
  273. waitTimes = I2C_RETRY_TIMES;
  274. while ((0U == (base->S & (uint8_t)kI2C_IntPendingFlag)) && (0U != --waitTimes))
  275. {
  276. }
  277. if (waitTimes == 0)
  278. {
  279. return kStatus_I2C_Timeout;
  280. }
  281. #else
  282. while (0U == (base->S & (uint8_t)kI2C_IntPendingFlag))
  283. {
  284. }
  285. #endif
  286. /* Check if there's transfer error. */
  287. result = I2C_CheckAndClearError(base, base->S);
  288. if (0 != result)
  289. {
  290. return result;
  291. }
  292. }
  293. }
  294. /* Clear pending flag. */
  295. base->S = (uint8_t)kI2C_IntPendingFlag;
  296. }
  297. return result;
  298. }
  299. static void I2C_MasterTransferEDMAConfig(I2C_Type *base, i2c_master_edma_handle_t *handle)
  300. {
  301. edma_transfer_config_t transfer_config = {0};
  302. if (handle->transfer.direction == kI2C_Read)
  303. {
  304. transfer_config.srcAddr = (uint32_t)I2C_GetDataRegAddr(base);
  305. transfer_config.destAddr = (uint32_t)(handle->transfer.data);
  306. transfer_config.majorLoopCounts = (handle->transfer.dataSize - 1U);
  307. transfer_config.srcTransferSize = kEDMA_TransferSize1Bytes;
  308. transfer_config.srcOffset = 0;
  309. transfer_config.destTransferSize = kEDMA_TransferSize1Bytes;
  310. transfer_config.destOffset = 1;
  311. transfer_config.minorLoopBytes = 1;
  312. }
  313. else
  314. {
  315. transfer_config.srcAddr = ((uint32_t)handle->transfer.data + 1U);
  316. transfer_config.destAddr = (uint32_t)I2C_GetDataRegAddr(base);
  317. transfer_config.majorLoopCounts = (handle->transfer.dataSize - 1U);
  318. transfer_config.srcTransferSize = kEDMA_TransferSize1Bytes;
  319. transfer_config.srcOffset = 1;
  320. transfer_config.destTransferSize = kEDMA_TransferSize1Bytes;
  321. transfer_config.destOffset = 0;
  322. transfer_config.minorLoopBytes = 1;
  323. }
  324. /* Store the initially configured eDMA minor byte transfer count into the I2C handle */
  325. handle->nbytes = (uint8_t)(transfer_config.minorLoopBytes);
  326. (void)EDMA_SubmitTransfer(handle->dmaHandle, (const edma_transfer_config_t *)(uint32_t)&transfer_config);
  327. EDMA_StartTransfer(handle->dmaHandle);
  328. }
  329. /*!
  330. * brief Initializes the I2C handle which is used in transactional functions.
  331. *
  332. * param base I2C peripheral base address.
  333. * param handle A pointer to the i2c_master_edma_handle_t structure.
  334. * param callback A pointer to the user callback function.
  335. * param userData A user parameter passed to the callback function.
  336. * param edmaHandle eDMA handle pointer.
  337. */
  338. void I2C_MasterCreateEDMAHandle(I2C_Type *base,
  339. i2c_master_edma_handle_t *handle,
  340. i2c_master_edma_transfer_callback_t callback,
  341. void *userData,
  342. edma_handle_t *edmaHandle)
  343. {
  344. assert(NULL != handle);
  345. assert(NULL != edmaHandle);
  346. uint32_t instance = I2C_GetInstance(base);
  347. /* Zero handle. */
  348. (void)memset(handle, 0, sizeof(*handle));
  349. /* Set the user callback and userData. */
  350. handle->completionCallback = callback;
  351. handle->userData = userData;
  352. /* Set the base for the handle. */
  353. base = base;
  354. /* Set the handle for EDMA. */
  355. handle->dmaHandle = edmaHandle;
  356. s_i2cEdmaPrivateHandle[instance].base = base;
  357. s_i2cEdmaPrivateHandle[instance].handle = handle;
  358. EDMA_SetCallback(edmaHandle, (edma_callback)I2C_MasterTransferCallbackEDMA, &s_i2cEdmaPrivateHandle[instance]);
  359. }
  360. /*!
  361. * brief Performs a master eDMA non-blocking transfer on the I2C bus.
  362. *
  363. * param base I2C peripheral base address.
  364. * param handle A pointer to the i2c_master_edma_handle_t structure.
  365. * param xfer A pointer to the transfer structure of i2c_master_transfer_t.
  366. * retval kStatus_Success Successfully completed the data transmission.
  367. * retval kStatus_I2C_Busy A previous transmission is still not finished.
  368. * retval kStatus_I2C_Timeout Transfer error, waits for a signal timeout.
  369. * retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  370. * retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
  371. */
  372. status_t I2C_MasterTransferEDMA(I2C_Type *base, i2c_master_edma_handle_t *handle, i2c_master_transfer_t *xfer)
  373. {
  374. assert(NULL != handle);
  375. assert(NULL != xfer);
  376. status_t result;
  377. uint8_t tmpReg;
  378. volatile uint8_t dummy = 0;
  379. /* Add this to avoid build warning. */
  380. dummy++;
  381. /* Disable dma xfer. */
  382. I2C_EnableDMA(base, false);
  383. /* Send address and command buffer(if there is), until senddata phase or receive data phase. */
  384. result = I2C_InitTransferStateMachineEDMA(base, handle, xfer);
  385. if (0 != result)
  386. {
  387. /* Send stop if received Nak. */
  388. if (result == kStatus_I2C_Nak)
  389. {
  390. if (I2C_MasterStop(base) != kStatus_Success)
  391. {
  392. result = kStatus_I2C_Timeout;
  393. }
  394. }
  395. /* Reset the state to idle state. */
  396. handle->state = (uint8_t)kIdleState;
  397. return result;
  398. }
  399. /* Configure dma transfer. */
  400. /* For i2c send, need to send 1 byte first to trigger the dma, for i2c read,
  401. need to send stop before reading the last byte, so the dma transfer size should
  402. be (xSize - 1). */
  403. if (handle->transfer.dataSize > 1U)
  404. {
  405. I2C_MasterTransferEDMAConfig(base, handle);
  406. if (handle->transfer.direction == kI2C_Read)
  407. {
  408. /* Change direction for receive. */
  409. base->C1 &= ~(uint8_t)(I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
  410. /* Read dummy to release the bus. */
  411. dummy = base->D;
  412. /* Enabe dma transfer. */
  413. I2C_EnableDMA(base, true);
  414. }
  415. else
  416. {
  417. /* Enabe dma transfer. */
  418. I2C_EnableDMA(base, true);
  419. /* Send the first data. */
  420. base->D = *handle->transfer.data;
  421. }
  422. }
  423. else /* If transfer size is 1, use polling method. */
  424. {
  425. if (handle->transfer.direction == kI2C_Read)
  426. {
  427. tmpReg = base->C1;
  428. /* Change direction to Rx. */
  429. tmpReg &= ~(uint8_t)I2C_C1_TX_MASK;
  430. /* Configure send NAK */
  431. tmpReg |= I2C_C1_TXAK_MASK;
  432. base->C1 = tmpReg;
  433. /* Read dummy to release the bus. */
  434. dummy = base->D;
  435. }
  436. else
  437. {
  438. base->D = *handle->transfer.data;
  439. }
  440. /* Wait until data transfer complete. */
  441. #if I2C_RETRY_TIMES
  442. uint32_t waitTimes = I2C_RETRY_TIMES;
  443. while ((0U == (base->S & (uint8_t)kI2C_IntPendingFlag)) && (0U != --waitTimes))
  444. {
  445. }
  446. if (waitTimes == 0U)
  447. {
  448. return kStatus_I2C_Timeout;
  449. }
  450. #else
  451. while (0U == (base->S & (uint8_t)kI2C_IntPendingFlag))
  452. {
  453. }
  454. #endif
  455. /* Clear pending flag. */
  456. base->S = (uint8_t)kI2C_IntPendingFlag;
  457. /* Send stop if kI2C_TransferNoStop flag is not asserted. */
  458. if (0U == (handle->transfer.flags & (uint32_t)kI2C_TransferNoStopFlag))
  459. {
  460. result = I2C_MasterStop(base);
  461. }
  462. else
  463. {
  464. /* Change direction to send. */
  465. base->C1 |= I2C_C1_TX_MASK;
  466. }
  467. /* Read the last byte of data. */
  468. if (handle->transfer.direction == kI2C_Read)
  469. {
  470. tmpReg = base->D;
  471. *handle->transfer.data = tmpReg;
  472. }
  473. /* Reset the state to idle. */
  474. handle->state = (uint8_t)kIdleState;
  475. }
  476. return result;
  477. }
  478. /*!
  479. * brief Gets a master transfer status during the eDMA non-blocking transfer.
  480. *
  481. * param base I2C peripheral base address.
  482. * param handle A pointer to the i2c_master_edma_handle_t structure.
  483. * param count A number of bytes transferred by the non-blocking transaction.
  484. */
  485. status_t I2C_MasterTransferGetCountEDMA(I2C_Type *base, i2c_master_edma_handle_t *handle, size_t *count)
  486. {
  487. assert(NULL != handle->dmaHandle);
  488. if (NULL == count)
  489. {
  490. return kStatus_InvalidArgument;
  491. }
  492. if ((uint8_t)kIdleState != handle->state)
  493. {
  494. *count = (handle->transferSize -
  495. (uint32_t)handle->nbytes *
  496. EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel));
  497. }
  498. else
  499. {
  500. *count = handle->transferSize;
  501. }
  502. return kStatus_Success;
  503. }
  504. /*!
  505. * brief Aborts a master eDMA non-blocking transfer early.
  506. *
  507. * param base I2C peripheral base address.
  508. * param handle A pointer to the i2c_master_edma_handle_t structure.
  509. */
  510. void I2C_MasterTransferAbortEDMA(I2C_Type *base, i2c_master_edma_handle_t *handle)
  511. {
  512. EDMA_AbortTransfer(handle->dmaHandle);
  513. /* Disable dma transfer. */
  514. I2C_EnableDMA(base, false);
  515. /* Reset the state to idle. */
  516. handle->state = (uint8_t)kIdleState;
  517. }