fsl_i2c.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_i2c.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /*! @brief i2c transfer state. */
  35. enum _i2c_transfer_states
  36. {
  37. kIdleState = 0x0U, /*!< I2C bus idle. */
  38. kCheckAddressState = 0x1U, /*!< 7-bit address check state. */
  39. kSendCommandState = 0x2U, /*!< Send command byte phase. */
  40. kSendDataState = 0x3U, /*!< Send data transfer phase. */
  41. kReceiveDataBeginState = 0x4U, /*!< Receive data transfer phase begin. */
  42. kReceiveDataState = 0x5U, /*!< Receive data transfer phase. */
  43. };
  44. /*! @brief Common sets of flags used by the driver. */
  45. enum _i2c_flag_constants
  46. {
  47. kClearFlags = kI2C_ArbitrationLostFlag | kI2C_IntPendingFlag,
  48. kIrqFlags = kI2C_GlobalInterruptEnable,
  49. };
  50. /*! @brief Typedef for interrupt handler. */
  51. typedef void (*i2c_isr_t)(I2C_Type *base, void *i2cHandle);
  52. /*******************************************************************************
  53. * Prototypes
  54. ******************************************************************************/
  55. /*!
  56. * @brief Get instance number for I2C module.
  57. *
  58. * @param base I2C peripheral base address.
  59. */
  60. uint32_t I2C_GetInstance(I2C_Type *base);
  61. /*!
  62. * @brief Set up master transfer, send slave address and decide the initial
  63. * transfer state.
  64. *
  65. * @param base I2C peripheral base address.
  66. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state.
  67. * @param xfer pointer to i2c_master_transfer_t structure.
  68. */
  69. static status_t I2C_InitTransferStateMachine(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer);
  70. /*!
  71. * @brief Check and clear status operation.
  72. *
  73. * @param base I2C peripheral base address.
  74. * @param status current i2c hardware status.
  75. * @retval kStatus_Success No error found.
  76. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  77. * @retval kStatus_I2C_Nak Received Nak error.
  78. */
  79. static status_t I2C_CheckAndClearError(I2C_Type *base, uint32_t status);
  80. /*!
  81. * @brief Master run transfer state machine to perform a byte of transfer.
  82. *
  83. * @param base I2C peripheral base address.
  84. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state
  85. * @param isDone input param to get whether the thing is done, true is done
  86. * @retval kStatus_Success No error found.
  87. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  88. * @retval kStatus_I2C_Nak Received Nak error.
  89. * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
  90. */
  91. static status_t I2C_MasterTransferRunStateMachine(I2C_Type *base, i2c_master_handle_t *handle, bool *isDone);
  92. /*!
  93. * @brief I2C common interrupt handler.
  94. *
  95. * @param base I2C peripheral base address.
  96. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state
  97. */
  98. static void I2C_TransferCommonIRQHandler(I2C_Type *base, void *handle);
  99. /*******************************************************************************
  100. * Variables
  101. ******************************************************************************/
  102. /*! @brief SCL clock divider used to calculate baudrate. */
  103. static const uint16_t s_i2cDividerTable[] = {
  104. 30, 32, 36, 42, 48, 52, 60, 72, 80, 88, 104, 128, 144, 160, 192, 240,
  105. 288, 320, 384, 480, 576, 640, 768, 960, 1152, 1280, 1536, 1920, 2304, 2560, 3072, 3840,
  106. 22, 24, 26, 28, 32, 36, 40, 44, 48, 56, 64, 72, 80, 96, 112, 128,
  107. 160, 192, 224, 256, 320, 384, 448, 512, 640, 768, 896, 1024, 1280, 1536, 1792, 2048};
  108. /*! @brief Pointers to i2c bases for each instance. */
  109. static I2C_Type *const s_i2cBases[] = I2C_BASE_PTRS;
  110. /*! @brief Pointers to i2c IRQ number for each instance. */
  111. static const IRQn_Type s_i2cIrqs[] = I2C_IRQS;
  112. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  113. /*! @brief Pointers to i2c clocks for each instance. */
  114. static const clock_ip_name_t s_i2cClocks[] = I2C_CLOCKS;
  115. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  116. /*! @brief Pointers to i2c handles for each instance. */
  117. static void *s_i2cHandle[ARRAY_SIZE(s_i2cBases)] = {NULL};
  118. /*! @brief Pointer to master IRQ handler for each instance. */
  119. static i2c_isr_t s_i2cMasterIsr;
  120. /*! @brief Pointer to slave IRQ handler for each instance. */
  121. static i2c_isr_t s_i2cSlaveIsr;
  122. /*******************************************************************************
  123. * Codes
  124. ******************************************************************************/
  125. uint32_t I2C_GetInstance(I2C_Type *base)
  126. {
  127. uint32_t instance;
  128. /* Find the instance index from base address mappings. */
  129. for (instance = 0; instance < ARRAY_SIZE(s_i2cBases); instance++)
  130. {
  131. if (s_i2cBases[instance] == base)
  132. {
  133. break;
  134. }
  135. }
  136. assert(instance < ARRAY_SIZE(s_i2cBases));
  137. return instance;
  138. }
  139. static status_t I2C_InitTransferStateMachine(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer)
  140. {
  141. status_t result = kStatus_Success;
  142. i2c_direction_t direction = xfer->direction;
  143. /* Initialize the handle transfer information. */
  144. handle->transfer = *xfer;
  145. /* Save total transfer size. */
  146. handle->transferSize = xfer->dataSize;
  147. /* Initial transfer state. */
  148. if (handle->transfer.subaddressSize > 0)
  149. {
  150. if (xfer->direction == kI2C_Read)
  151. {
  152. direction = kI2C_Write;
  153. }
  154. }
  155. handle->state = kCheckAddressState;
  156. /* Clear all status before transfer. */
  157. I2C_MasterClearStatusFlags(base, kClearFlags);
  158. /* If repeated start is requested, send repeated start. */
  159. if (handle->transfer.flags & kI2C_TransferRepeatedStartFlag)
  160. {
  161. result = I2C_MasterRepeatedStart(base, handle->transfer.slaveAddress, direction);
  162. }
  163. else /* For normal transfer, send start. */
  164. {
  165. result = I2C_MasterStart(base, handle->transfer.slaveAddress, direction);
  166. }
  167. return result;
  168. }
  169. static status_t I2C_CheckAndClearError(I2C_Type *base, uint32_t status)
  170. {
  171. status_t result = kStatus_Success;
  172. /* Check arbitration lost. */
  173. if (status & kI2C_ArbitrationLostFlag)
  174. {
  175. /* Clear arbitration lost flag. */
  176. base->I2SR &= (uint8_t)(~kI2C_ArbitrationLostFlag);
  177. /* Reset I2C controller*/
  178. base->I2CR &= ~I2C_I2CR_IEN_MASK;
  179. base->I2CR |= I2C_I2CR_IEN_MASK;
  180. result = kStatus_I2C_ArbitrationLost;
  181. }
  182. /* Check NAK */
  183. else if (status & kI2C_ReceiveNakFlag)
  184. {
  185. result = kStatus_I2C_Nak;
  186. }
  187. else
  188. {
  189. }
  190. return result;
  191. }
  192. static status_t I2C_MasterTransferRunStateMachine(I2C_Type *base, i2c_master_handle_t *handle, bool *isDone)
  193. {
  194. status_t result = kStatus_Success;
  195. uint32_t statusFlags = base->I2SR;
  196. *isDone = false;
  197. volatile uint8_t dummy = 0;
  198. bool ignoreNak = ((handle->state == kSendDataState) && (handle->transfer.dataSize == 0U)) ||
  199. ((handle->state == kReceiveDataState) && (handle->transfer.dataSize == 1U));
  200. /* Add this to avoid build warning. */
  201. dummy++;
  202. /* Check & clear error flags. */
  203. result = I2C_CheckAndClearError(base, statusFlags);
  204. /* Ignore Nak when it's appeared for last byte. */
  205. if ((result == kStatus_I2C_Nak) && ignoreNak)
  206. {
  207. result = kStatus_Success;
  208. }
  209. /* Handle Check address state to check the slave address is Acked in slave
  210. probe application. */
  211. if (handle->state == kCheckAddressState)
  212. {
  213. if (statusFlags & kI2C_ReceiveNakFlag)
  214. {
  215. result = kStatus_I2C_Addr_Nak;
  216. }
  217. else
  218. {
  219. if (handle->transfer.subaddressSize > 0)
  220. {
  221. handle->state = kSendCommandState;
  222. }
  223. else
  224. {
  225. if (handle->transfer.direction == kI2C_Write)
  226. {
  227. /* Next state, send data. */
  228. handle->state = kSendDataState;
  229. }
  230. else
  231. {
  232. /* Next state, receive data begin. */
  233. handle->state = kReceiveDataBeginState;
  234. }
  235. }
  236. }
  237. }
  238. if (result)
  239. {
  240. return result;
  241. }
  242. /* Run state machine. */
  243. switch (handle->state)
  244. {
  245. /* Send I2C command. */
  246. case kSendCommandState:
  247. if (handle->transfer.subaddressSize)
  248. {
  249. handle->transfer.subaddressSize--;
  250. base->I2DR = ((handle->transfer.subaddress) >> (8 * handle->transfer.subaddressSize));
  251. }
  252. else
  253. {
  254. if (handle->transfer.direction == kI2C_Write)
  255. {
  256. /* Next state, send data. */
  257. handle->state = kSendDataState;
  258. /* Send first byte of data. */
  259. if (handle->transfer.dataSize > 0)
  260. {
  261. base->I2DR = *handle->transfer.data;
  262. handle->transfer.data++;
  263. handle->transfer.dataSize--;
  264. }
  265. }
  266. else
  267. {
  268. /* Send repeated start and slave address. */
  269. result = I2C_MasterRepeatedStart(base, handle->transfer.slaveAddress, kI2C_Read);
  270. /* Next state, receive data begin. */
  271. handle->state = kReceiveDataBeginState;
  272. }
  273. }
  274. break;
  275. /* Send I2C data. */
  276. case kSendDataState:
  277. /* Send one byte of data. */
  278. if (handle->transfer.dataSize > 0)
  279. {
  280. base->I2DR = *handle->transfer.data;
  281. handle->transfer.data++;
  282. handle->transfer.dataSize--;
  283. }
  284. else
  285. {
  286. *isDone = true;
  287. }
  288. break;
  289. /* Start I2C data receive. */
  290. case kReceiveDataBeginState:
  291. base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
  292. /* Send nak at the last receive byte. */
  293. if (handle->transfer.dataSize == 1)
  294. {
  295. base->I2CR |= I2C_I2CR_TXAK_MASK;
  296. }
  297. /* Read dummy to release the bus. */
  298. dummy = base->I2DR;
  299. /* Next state, receive data. */
  300. handle->state = kReceiveDataState;
  301. break;
  302. /* Receive I2C data. */
  303. case kReceiveDataState:
  304. /* Receive one byte of data. */
  305. if (handle->transfer.dataSize--)
  306. {
  307. if (handle->transfer.dataSize == 0)
  308. {
  309. *isDone = true;
  310. /* Send stop if kI2C_TransferNoStop is not asserted. */
  311. if (!(handle->transfer.flags & kI2C_TransferNoStopFlag))
  312. {
  313. result = I2C_MasterStop(base);
  314. }
  315. else
  316. {
  317. base->I2CR |= I2C_I2CR_MTX_MASK;
  318. }
  319. }
  320. /* Send NAK at the last receive byte. */
  321. if (handle->transfer.dataSize == 1)
  322. {
  323. base->I2CR |= I2C_I2CR_TXAK_MASK;
  324. }
  325. /* Read the data byte into the transfer buffer. */
  326. *handle->transfer.data = base->I2DR;
  327. handle->transfer.data++;
  328. }
  329. break;
  330. default:
  331. break;
  332. }
  333. return result;
  334. }
  335. static void I2C_TransferCommonIRQHandler(I2C_Type *base, void *handle)
  336. {
  337. /* Check if master interrupt. */
  338. if ((base->I2SR & kI2C_ArbitrationLostFlag) || (base->I2CR & I2C_I2CR_MSTA_MASK))
  339. {
  340. s_i2cMasterIsr(base, handle);
  341. }
  342. else
  343. {
  344. s_i2cSlaveIsr(base, handle);
  345. }
  346. __DSB();
  347. }
  348. void I2C_MasterInit(I2C_Type *base, const i2c_master_config_t *masterConfig, uint32_t srcClock_Hz)
  349. {
  350. assert(masterConfig && srcClock_Hz);
  351. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  352. /* Enable I2C clock. */
  353. CLOCK_EnableClock(s_i2cClocks[I2C_GetInstance(base)]);
  354. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  355. /* Disable I2C prior to configuring it. */
  356. base->I2CR &= ~(I2C_I2CR_IEN_MASK);
  357. /* Clear all flags. */
  358. I2C_MasterClearStatusFlags(base, kClearFlags);
  359. /* Configure baud rate. */
  360. I2C_MasterSetBaudRate(base, masterConfig->baudRate_Bps, srcClock_Hz);
  361. /* Enable the I2C peripheral based on the configuration. */
  362. base->I2CR = I2C_I2CR_IEN(masterConfig->enableMaster);
  363. }
  364. void I2C_MasterDeinit(I2C_Type *base)
  365. {
  366. /* Disable I2C module. */
  367. I2C_Enable(base, false);
  368. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  369. /* Disable I2C clock. */
  370. CLOCK_DisableClock(s_i2cClocks[I2C_GetInstance(base)]);
  371. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  372. }
  373. void I2C_MasterGetDefaultConfig(i2c_master_config_t *masterConfig)
  374. {
  375. assert(masterConfig);
  376. /* Default baud rate at 100kbps. */
  377. masterConfig->baudRate_Bps = 100000U;
  378. /* Enable the I2C peripheral. */
  379. masterConfig->enableMaster = true;
  380. }
  381. void I2C_EnableInterrupts(I2C_Type *base, uint32_t mask)
  382. {
  383. if (mask & kI2C_GlobalInterruptEnable)
  384. {
  385. base->I2CR |= I2C_I2CR_IIEN_MASK;
  386. }
  387. }
  388. void I2C_DisableInterrupts(I2C_Type *base, uint32_t mask)
  389. {
  390. if (mask & kI2C_GlobalInterruptEnable)
  391. {
  392. base->I2CR &= ~I2C_I2CR_IIEN_MASK;
  393. }
  394. }
  395. void I2C_MasterSetBaudRate(I2C_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
  396. {
  397. uint32_t computedRate;
  398. uint32_t absError;
  399. uint32_t bestError = UINT32_MAX;
  400. uint32_t bestIcr = 0u;
  401. uint8_t i;
  402. /* Scan table to find best match. */
  403. for (i = 0u; i < sizeof(s_i2cDividerTable) / sizeof(s_i2cDividerTable[0]); ++i)
  404. {
  405. computedRate = srcClock_Hz / s_i2cDividerTable[i];
  406. absError = baudRate_Bps > computedRate ? (baudRate_Bps - computedRate) : (computedRate - baudRate_Bps);
  407. if (absError < bestError)
  408. {
  409. bestIcr = i;
  410. bestError = absError;
  411. /* If the error is 0, then we can stop searching because we won't find a better match. */
  412. if (absError == 0)
  413. {
  414. break;
  415. }
  416. }
  417. }
  418. /* Set frequency register based on best settings. */
  419. base->IFDR = I2C_IFDR_IC(bestIcr);
  420. }
  421. status_t I2C_MasterStart(I2C_Type *base, uint8_t address, i2c_direction_t direction)
  422. {
  423. status_t result = kStatus_Success;
  424. uint32_t statusFlags = I2C_MasterGetStatusFlags(base);
  425. /* Return an error if the bus is already in use. */
  426. if (statusFlags & kI2C_BusBusyFlag)
  427. {
  428. result = kStatus_I2C_Busy;
  429. }
  430. else
  431. {
  432. /* Send the START signal. */
  433. base->I2CR |= I2C_I2CR_MSTA_MASK | I2C_I2CR_MTX_MASK;
  434. base->I2DR = (((uint32_t)address) << 1U | ((direction == kI2C_Read) ? 1U : 0U));
  435. }
  436. return result;
  437. }
  438. status_t I2C_MasterRepeatedStart(I2C_Type *base, uint8_t address, i2c_direction_t direction)
  439. {
  440. status_t result = kStatus_Success;
  441. uint32_t statusFlags = I2C_MasterGetStatusFlags(base);
  442. /* Return an error if the bus is already in use, but not by us. */
  443. if ((statusFlags & kI2C_BusBusyFlag) && ((base->I2CR & I2C_I2CR_MSTA_MASK) == 0))
  444. {
  445. result = kStatus_I2C_Busy;
  446. }
  447. else
  448. {
  449. /* We are already in a transfer, so send a repeated start. */
  450. base->I2CR |= I2C_I2CR_RSTA_MASK | I2C_I2CR_MTX_MASK;
  451. base->I2DR = (((uint32_t)address) << 1U | ((direction == kI2C_Read) ? 1U : 0U));
  452. }
  453. return result;
  454. }
  455. status_t I2C_MasterStop(I2C_Type *base)
  456. {
  457. status_t result = kStatus_Success;
  458. uint16_t timeout = UINT16_MAX;
  459. /* Issue the STOP command on the bus. */
  460. base->I2CR &= ~(I2C_I2CR_MSTA_MASK | I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
  461. /* Wait until data transfer complete. */
  462. while ((base->I2SR & kI2C_BusBusyFlag) && (--timeout))
  463. {
  464. }
  465. if (timeout == 0)
  466. {
  467. result = kStatus_I2C_Timeout;
  468. }
  469. return result;
  470. }
  471. status_t I2C_MasterWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize, uint32_t flags)
  472. {
  473. status_t result = kStatus_Success;
  474. uint8_t statusFlags = 0;
  475. /* Wait until the data register is ready for transmit. */
  476. while (!(base->I2SR & kI2C_TransferCompleteFlag))
  477. {
  478. }
  479. /* Clear the IICIF flag. */
  480. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  481. /* Setup the I2C peripheral to transmit data. */
  482. base->I2CR |= I2C_I2CR_MTX_MASK;
  483. while (txSize--)
  484. {
  485. /* Send a byte of data. */
  486. base->I2DR = *txBuff++;
  487. /* Wait until data transfer complete. */
  488. while (!(base->I2SR & kI2C_IntPendingFlag))
  489. {
  490. }
  491. statusFlags = base->I2SR;
  492. /* Clear the IICIF flag. */
  493. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  494. /* Check if arbitration lost or no acknowledgement (NAK), return failure status. */
  495. if (statusFlags & kI2C_ArbitrationLostFlag)
  496. {
  497. base->I2SR = kI2C_ArbitrationLostFlag;
  498. result = kStatus_I2C_ArbitrationLost;
  499. }
  500. if ((statusFlags & kI2C_ReceiveNakFlag) && txSize)
  501. {
  502. base->I2SR = kI2C_ReceiveNakFlag;
  503. result = kStatus_I2C_Nak;
  504. }
  505. if (result != kStatus_Success)
  506. {
  507. /* Breaking out of the send loop. */
  508. break;
  509. }
  510. }
  511. if (((result == kStatus_Success) && (!(flags & kI2C_TransferNoStopFlag))) || (result == kStatus_I2C_Nak))
  512. {
  513. /* Clear the IICIF flag. */
  514. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  515. /* Send stop. */
  516. result = I2C_MasterStop(base);
  517. }
  518. return result;
  519. }
  520. status_t I2C_MasterReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize, uint32_t flags)
  521. {
  522. status_t result = kStatus_Success;
  523. volatile uint8_t dummy = 0;
  524. /* Add this to avoid build warning. */
  525. dummy++;
  526. /* Wait until the data register is ready for transmit. */
  527. while (!(base->I2SR & kI2C_TransferCompleteFlag))
  528. {
  529. }
  530. /* Clear the IICIF flag. */
  531. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  532. /* Setup the I2C peripheral to receive data. */
  533. base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
  534. /* If rxSize equals 1, configure to send NAK. */
  535. if (rxSize == 1)
  536. {
  537. /* Issue NACK on read. */
  538. base->I2CR |= I2C_I2CR_TXAK_MASK;
  539. }
  540. /* Do dummy read. */
  541. dummy = base->I2DR;
  542. while ((rxSize--))
  543. {
  544. /* Wait until data transfer complete. */
  545. while (!(base->I2SR & kI2C_IntPendingFlag))
  546. {
  547. }
  548. /* Clear the IICIF flag. */
  549. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  550. /* Single byte use case. */
  551. if (rxSize == 0)
  552. {
  553. if (!(flags & kI2C_TransferNoStopFlag))
  554. {
  555. /* Issue STOP command before reading last byte. */
  556. result = I2C_MasterStop(base);
  557. }
  558. else
  559. {
  560. /* Change direction to Tx to avoid extra clocks. */
  561. base->I2CR |= I2C_I2CR_MTX_MASK;
  562. }
  563. }
  564. if (rxSize == 1)
  565. {
  566. /* Issue NACK on read. */
  567. base->I2CR |= I2C_I2CR_TXAK_MASK;
  568. }
  569. /* Read from the data register. */
  570. *rxBuff++ = base->I2DR;
  571. }
  572. return result;
  573. }
  574. status_t I2C_MasterTransferBlocking(I2C_Type *base, i2c_master_transfer_t *xfer)
  575. {
  576. assert(xfer);
  577. i2c_direction_t direction = xfer->direction;
  578. status_t result = kStatus_Success;
  579. /* Clear all status before transfer. */
  580. I2C_MasterClearStatusFlags(base, kClearFlags);
  581. /* Wait until ready to complete. */
  582. while (!(base->I2SR & kI2C_TransferCompleteFlag))
  583. {
  584. }
  585. /* Change to send write address when it's a read operation with command. */
  586. if ((xfer->subaddressSize > 0) && (xfer->direction == kI2C_Read))
  587. {
  588. direction = kI2C_Write;
  589. }
  590. /* If repeated start is requested, send repeated start. */
  591. if (xfer->flags & kI2C_TransferRepeatedStartFlag)
  592. {
  593. result = I2C_MasterRepeatedStart(base, xfer->slaveAddress, direction);
  594. }
  595. else /* For normal transfer, send start. */
  596. {
  597. result = I2C_MasterStart(base, xfer->slaveAddress, direction);
  598. }
  599. /* Return if error. */
  600. if (result)
  601. {
  602. return result;
  603. }
  604. while (!(base->I2SR & kI2C_IntPendingFlag))
  605. {
  606. }
  607. /* Check if there's transfer error. */
  608. result = I2C_CheckAndClearError(base, base->I2SR);
  609. /* Return if error. */
  610. if (result)
  611. {
  612. if (result == kStatus_I2C_Nak)
  613. {
  614. result = kStatus_I2C_Addr_Nak;
  615. I2C_MasterStop(base);
  616. }
  617. return result;
  618. }
  619. /* Send subaddress. */
  620. if (xfer->subaddressSize)
  621. {
  622. do
  623. {
  624. /* Clear interrupt pending flag. */
  625. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  626. xfer->subaddressSize--;
  627. base->I2DR = ((xfer->subaddress) >> (8 * xfer->subaddressSize));
  628. /* Wait until data transfer complete. */
  629. while (!(base->I2SR & kI2C_IntPendingFlag))
  630. {
  631. }
  632. /* Check if there's transfer error. */
  633. result = I2C_CheckAndClearError(base, base->I2SR);
  634. if (result)
  635. {
  636. if (result == kStatus_I2C_Nak)
  637. {
  638. I2C_MasterStop(base);
  639. }
  640. return result;
  641. }
  642. } while ((xfer->subaddressSize > 0) && (result == kStatus_Success));
  643. if (xfer->direction == kI2C_Read)
  644. {
  645. /* Clear pending flag. */
  646. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  647. /* Send repeated start and slave address. */
  648. result = I2C_MasterRepeatedStart(base, xfer->slaveAddress, kI2C_Read);
  649. /* Return if error. */
  650. if (result)
  651. {
  652. return result;
  653. }
  654. /* Wait until data transfer complete. */
  655. while (!(base->I2SR & kI2C_IntPendingFlag))
  656. {
  657. }
  658. /* Check if there's transfer error. */
  659. result = I2C_CheckAndClearError(base, base->I2SR);
  660. if (result)
  661. {
  662. if (result == kStatus_I2C_Nak)
  663. {
  664. result = kStatus_I2C_Addr_Nak;
  665. I2C_MasterStop(base);
  666. }
  667. return result;
  668. }
  669. }
  670. }
  671. /* Transmit data. */
  672. if ((xfer->direction == kI2C_Write) && (xfer->dataSize > 0))
  673. {
  674. /* Send Data. */
  675. result = I2C_MasterWriteBlocking(base, xfer->data, xfer->dataSize, xfer->flags);
  676. }
  677. /* Receive Data. */
  678. if ((xfer->direction == kI2C_Read) && (xfer->dataSize > 0))
  679. {
  680. result = I2C_MasterReadBlocking(base, xfer->data, xfer->dataSize, xfer->flags);
  681. }
  682. return result;
  683. }
  684. void I2C_MasterTransferCreateHandle(I2C_Type *base,
  685. i2c_master_handle_t *handle,
  686. i2c_master_transfer_callback_t callback,
  687. void *userData)
  688. {
  689. assert(handle);
  690. uint32_t instance = I2C_GetInstance(base);
  691. /* Zero handle. */
  692. memset(handle, 0, sizeof(*handle));
  693. /* Set callback and userData. */
  694. handle->completionCallback = callback;
  695. handle->userData = userData;
  696. /* Save the context in global variables to support the double weak mechanism. */
  697. s_i2cHandle[instance] = handle;
  698. /* Save master interrupt handler. */
  699. s_i2cMasterIsr = I2C_MasterTransferHandleIRQ;
  700. /* Enable NVIC interrupt. */
  701. EnableIRQ(s_i2cIrqs[instance]);
  702. }
  703. status_t I2C_MasterTransferNonBlocking(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer)
  704. {
  705. assert(handle);
  706. assert(xfer);
  707. status_t result = kStatus_Success;
  708. /* Check if the I2C bus is idle - if not return busy status. */
  709. if (handle->state != kIdleState)
  710. {
  711. result = kStatus_I2C_Busy;
  712. }
  713. else
  714. {
  715. /* Start up the master transfer state machine. */
  716. result = I2C_InitTransferStateMachine(base, handle, xfer);
  717. if (result == kStatus_Success)
  718. {
  719. /* Enable the I2C interrupts. */
  720. I2C_EnableInterrupts(base, kI2C_GlobalInterruptEnable);
  721. }
  722. }
  723. return result;
  724. }
  725. void I2C_MasterTransferAbort(I2C_Type *base, i2c_master_handle_t *handle)
  726. {
  727. assert(handle);
  728. volatile uint8_t dummy = 0;
  729. /* Add this to avoid build warning. */
  730. dummy++;
  731. /* Disable interrupt. */
  732. I2C_DisableInterrupts(base, kI2C_GlobalInterruptEnable);
  733. /* Reset the state to idle. */
  734. handle->state = kIdleState;
  735. /* Send STOP signal. */
  736. if (handle->transfer.direction == kI2C_Read)
  737. {
  738. base->I2CR |= I2C_I2CR_TXAK_MASK;
  739. while (!(base->I2SR & kI2C_IntPendingFlag))
  740. {
  741. }
  742. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  743. base->I2CR &= ~(I2C_I2CR_MSTA_MASK | I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
  744. dummy = base->I2DR;
  745. }
  746. else
  747. {
  748. while (!(base->I2SR & kI2C_IntPendingFlag))
  749. {
  750. }
  751. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  752. base->I2CR &= ~(I2C_I2CR_MSTA_MASK | I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
  753. }
  754. }
  755. status_t I2C_MasterTransferGetCount(I2C_Type *base, i2c_master_handle_t *handle, size_t *count)
  756. {
  757. assert(handle);
  758. if (!count)
  759. {
  760. return kStatus_InvalidArgument;
  761. }
  762. *count = handle->transferSize - handle->transfer.dataSize;
  763. return kStatus_Success;
  764. }
  765. void I2C_MasterTransferHandleIRQ(I2C_Type *base, void *i2cHandle)
  766. {
  767. assert(i2cHandle);
  768. i2c_master_handle_t *handle = (i2c_master_handle_t *)i2cHandle;
  769. status_t result = kStatus_Success;
  770. bool isDone;
  771. /* Clear the interrupt flag. */
  772. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  773. /* Check transfer complete flag. */
  774. result = I2C_MasterTransferRunStateMachine(base, handle, &isDone);
  775. if (isDone || result)
  776. {
  777. /* Send stop command if transfer done or received Nak. */
  778. if ((!(handle->transfer.flags & kI2C_TransferNoStopFlag)) || (result == kStatus_I2C_Nak) ||
  779. (result == kStatus_I2C_Addr_Nak))
  780. {
  781. /* Ensure stop command is a need. */
  782. if ((base->I2CR & I2C_I2CR_MSTA_MASK))
  783. {
  784. if (I2C_MasterStop(base) != kStatus_Success)
  785. {
  786. result = kStatus_I2C_Timeout;
  787. }
  788. }
  789. }
  790. /* Restore handle to idle state. */
  791. handle->state = kIdleState;
  792. /* Disable interrupt. */
  793. I2C_DisableInterrupts(base, kI2C_GlobalInterruptEnable);
  794. /* Call the callback function after the function has completed. */
  795. if (handle->completionCallback)
  796. {
  797. handle->completionCallback(base, handle, result, handle->userData);
  798. }
  799. }
  800. }
  801. void I2C_SlaveInit(I2C_Type *base, const i2c_slave_config_t *slaveConfig)
  802. {
  803. assert(slaveConfig);
  804. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  805. /* Enable I2C clock. */
  806. CLOCK_EnableClock(s_i2cClocks[I2C_GetInstance(base)]);
  807. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  808. base->IADR = ((uint32_t)(slaveConfig->slaveAddress)) << 1U;
  809. base->I2CR = I2C_I2CR_IEN(slaveConfig->enableSlave);
  810. }
  811. void I2C_SlaveDeinit(I2C_Type *base)
  812. {
  813. /* Disable I2C module. */
  814. I2C_Enable(base, false);
  815. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  816. /* Disable I2C clock. */
  817. CLOCK_DisableClock(s_i2cClocks[I2C_GetInstance(base)]);
  818. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  819. }
  820. void I2C_SlaveGetDefaultConfig(i2c_slave_config_t *slaveConfig)
  821. {
  822. assert(slaveConfig);
  823. /* Enable the I2C peripheral. */
  824. slaveConfig->enableSlave = true;
  825. }
  826. status_t I2C_SlaveWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize)
  827. {
  828. status_t result = kStatus_Success;
  829. volatile uint8_t dummy = 0;
  830. /* Add this to avoid build warning. */
  831. dummy++;
  832. /* Wait for address match flag. */
  833. while (!(base->I2SR & kI2C_AddressMatchFlag))
  834. {
  835. }
  836. /* Read dummy to release bus. */
  837. dummy = base->I2DR;
  838. result = I2C_MasterWriteBlocking(base, txBuff, txSize, kI2C_TransferDefaultFlag);
  839. /* Switch to receive mode. */
  840. base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
  841. /* Read dummy to release bus. */
  842. dummy = base->I2DR;
  843. return result;
  844. }
  845. void I2C_SlaveReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize)
  846. {
  847. volatile uint8_t dummy = 0;
  848. /* Add this to avoid build warning. */
  849. dummy++;
  850. /* Wait for address match and int pending flag. */
  851. while (!(base->I2SR & kI2C_AddressMatchFlag))
  852. {
  853. }
  854. /* Read dummy to release bus. */
  855. dummy = base->I2DR;
  856. /* Clear the IICIF flag. */
  857. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  858. /* Setup the I2C peripheral to receive data. */
  859. base->I2CR &= ~(I2C_I2CR_MTX_MASK);
  860. while (rxSize--)
  861. {
  862. /* Wait until data transfer complete. */
  863. while (!(base->I2SR & kI2C_IntPendingFlag))
  864. {
  865. }
  866. /* Clear the IICIF flag. */
  867. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  868. /* Read from the data register. */
  869. *rxBuff++ = base->I2DR;
  870. }
  871. }
  872. void I2C_SlaveTransferCreateHandle(I2C_Type *base,
  873. i2c_slave_handle_t *handle,
  874. i2c_slave_transfer_callback_t callback,
  875. void *userData)
  876. {
  877. assert(handle);
  878. uint32_t instance = I2C_GetInstance(base);
  879. /* Zero handle. */
  880. memset(handle, 0, sizeof(*handle));
  881. /* Set callback and userData. */
  882. handle->callback = callback;
  883. handle->userData = userData;
  884. /* Save the context in global variables to support the double weak mechanism. */
  885. s_i2cHandle[instance] = handle;
  886. /* Save slave interrupt handler. */
  887. s_i2cSlaveIsr = I2C_SlaveTransferHandleIRQ;
  888. /* Enable NVIC interrupt. */
  889. EnableIRQ(s_i2cIrqs[instance]);
  890. }
  891. status_t I2C_SlaveTransferNonBlocking(I2C_Type *base, i2c_slave_handle_t *handle, uint32_t eventMask)
  892. {
  893. assert(handle);
  894. /* Check if the I2C bus is idle - if not return busy status. */
  895. if (handle->state != kIdleState)
  896. {
  897. return kStatus_I2C_Busy;
  898. }
  899. else
  900. {
  901. /* Disable LPI2C IRQ sources while we configure stuff. */
  902. I2C_DisableInterrupts(base, kIrqFlags);
  903. /* Clear transfer in handle. */
  904. memset(&handle->transfer, 0, sizeof(handle->transfer));
  905. /* Record that we're busy. */
  906. handle->state = kCheckAddressState;
  907. /* Set up event mask. tx and rx are always enabled. */
  908. handle->eventMask = eventMask | kI2C_SlaveTransmitEvent | kI2C_SlaveReceiveEvent;
  909. /* Clear all flags. */
  910. I2C_SlaveClearStatusFlags(base, kClearFlags);
  911. /* Enable I2C internal IRQ sources. NVIC IRQ was enabled in CreateHandle() */
  912. I2C_EnableInterrupts(base, kIrqFlags);
  913. }
  914. return kStatus_Success;
  915. }
  916. void I2C_SlaveTransferAbort(I2C_Type *base, i2c_slave_handle_t *handle)
  917. {
  918. assert(handle);
  919. if (handle->state != kIdleState)
  920. {
  921. /* Disable interrupts. */
  922. I2C_DisableInterrupts(base, kIrqFlags);
  923. /* Reset transfer info. */
  924. memset(&handle->transfer, 0, sizeof(handle->transfer));
  925. /* Reset the state to idle. */
  926. handle->state = kIdleState;
  927. }
  928. }
  929. status_t I2C_SlaveTransferGetCount(I2C_Type *base, i2c_slave_handle_t *handle, size_t *count)
  930. {
  931. assert(handle);
  932. if (!count)
  933. {
  934. return kStatus_InvalidArgument;
  935. }
  936. /* Catch when there is not an active transfer. */
  937. if (handle->state == kIdleState)
  938. {
  939. *count = 0;
  940. return kStatus_NoTransferInProgress;
  941. }
  942. /* For an active transfer, just return the count from the handle. */
  943. *count = handle->transfer.transferredCount;
  944. return kStatus_Success;
  945. }
  946. void I2C_SlaveTransferHandleIRQ(I2C_Type *base, void *i2cHandle)
  947. {
  948. assert(i2cHandle);
  949. uint16_t status;
  950. bool doTransmit = false;
  951. i2c_slave_handle_t *handle = (i2c_slave_handle_t *)i2cHandle;
  952. i2c_slave_transfer_t *xfer;
  953. volatile uint8_t dummy = 0;
  954. /* Add this to avoid build warning. */
  955. dummy++;
  956. status = I2C_SlaveGetStatusFlags(base);
  957. xfer = &(handle->transfer);
  958. /* Clear the interrupt flag. */
  959. base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
  960. /* Check NAK */
  961. if (status & kI2C_ReceiveNakFlag)
  962. {
  963. /* Set receive mode. */
  964. base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
  965. /* Read dummy. */
  966. dummy = base->I2DR;
  967. if (handle->transfer.dataSize != 0)
  968. {
  969. xfer->event = kI2C_SlaveCompletionEvent;
  970. xfer->completionStatus = kStatus_I2C_Nak;
  971. handle->state = kIdleState;
  972. if ((handle->eventMask & xfer->event) && (handle->callback))
  973. {
  974. handle->callback(base, xfer, handle->userData);
  975. }
  976. }
  977. else
  978. {
  979. xfer->event = kI2C_SlaveCompletionEvent;
  980. xfer->completionStatus = kStatus_Success;
  981. handle->state = kIdleState;
  982. if ((handle->eventMask & xfer->event) && (handle->callback))
  983. {
  984. handle->callback(base, xfer, handle->userData);
  985. }
  986. }
  987. }
  988. /* Check address match. */
  989. else if (status & kI2C_AddressMatchFlag)
  990. {
  991. xfer->event = kI2C_SlaveAddressMatchEvent;
  992. /* Slave transmit, master reading from slave. */
  993. if (status & kI2C_TransferDirectionFlag)
  994. {
  995. handle->state = kSendDataState;
  996. /* Change direction to send data. */
  997. base->I2CR |= I2C_I2CR_MTX_MASK;
  998. doTransmit = true;
  999. }
  1000. else
  1001. {
  1002. handle->state = kReceiveDataState;
  1003. /* Slave receive, master writing to slave. */
  1004. base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
  1005. /* Read dummy to release the bus. */
  1006. dummy = base->I2DR;
  1007. }
  1008. if ((handle->eventMask & xfer->event) && (handle->callback))
  1009. {
  1010. handle->callback(base, xfer, handle->userData);
  1011. }
  1012. }
  1013. /* Check transfer complete flag. */
  1014. else if (status & kI2C_TransferCompleteFlag)
  1015. {
  1016. /* Slave transmit, master reading from slave. */
  1017. if (handle->state == kSendDataState)
  1018. {
  1019. doTransmit = true;
  1020. }
  1021. else
  1022. {
  1023. /* If we're out of data, invoke callback to get more. */
  1024. if ((!xfer->data) || (!xfer->dataSize))
  1025. {
  1026. xfer->event = kI2C_SlaveReceiveEvent;
  1027. if (handle->callback)
  1028. {
  1029. handle->callback(base, xfer, handle->userData);
  1030. }
  1031. /* Clear the transferred count now that we have a new buffer. */
  1032. xfer->transferredCount = 0;
  1033. }
  1034. /* Slave receive, master writing to slave. */
  1035. uint8_t data = base->I2DR;
  1036. if (handle->transfer.dataSize)
  1037. {
  1038. /* Receive data. */
  1039. *handle->transfer.data++ = data;
  1040. handle->transfer.dataSize--;
  1041. xfer->transferredCount++;
  1042. if (!handle->transfer.dataSize)
  1043. {
  1044. xfer->event = kI2C_SlaveCompletionEvent;
  1045. xfer->completionStatus = kStatus_Success;
  1046. handle->state = kIdleState;
  1047. /* Proceed receive complete event. */
  1048. if ((handle->eventMask & xfer->event) && (handle->callback))
  1049. {
  1050. handle->callback(base, xfer, handle->userData);
  1051. }
  1052. }
  1053. }
  1054. }
  1055. }
  1056. else
  1057. {
  1058. /* Read dummy to release bus. */
  1059. dummy = base->I2DR;
  1060. }
  1061. /* Send data if there is the need. */
  1062. if (doTransmit)
  1063. {
  1064. /* If we're out of data, invoke callback to get more. */
  1065. if ((!xfer->data) || (!xfer->dataSize))
  1066. {
  1067. xfer->event = kI2C_SlaveTransmitEvent;
  1068. if (handle->callback)
  1069. {
  1070. handle->callback(base, xfer, handle->userData);
  1071. }
  1072. /* Clear the transferred count now that we have a new buffer. */
  1073. xfer->transferredCount = 0;
  1074. }
  1075. if (handle->transfer.dataSize)
  1076. {
  1077. /* Send data. */
  1078. base->I2DR = *handle->transfer.data++;
  1079. handle->transfer.dataSize--;
  1080. xfer->transferredCount++;
  1081. }
  1082. else
  1083. {
  1084. /* Switch to receive mode. */
  1085. base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
  1086. /* Read dummy to release bus. */
  1087. dummy = base->I2DR;
  1088. xfer->event = kI2C_SlaveCompletionEvent;
  1089. xfer->completionStatus = kStatus_Success;
  1090. handle->state = kIdleState;
  1091. /* Proceed txdone event. */
  1092. if ((handle->eventMask & xfer->event) && (handle->callback))
  1093. {
  1094. handle->callback(base, xfer, handle->userData);
  1095. }
  1096. }
  1097. }
  1098. }
  1099. #if defined(I2C1)
  1100. void I2C1_DriverIRQHandler(void)
  1101. {
  1102. I2C_TransferCommonIRQHandler(I2C1, s_i2cHandle[1]);
  1103. }
  1104. #endif
  1105. #if defined(I2C2)
  1106. void I2C2_DriverIRQHandler(void)
  1107. {
  1108. I2C_TransferCommonIRQHandler(I2C2, s_i2cHandle[2]);
  1109. }
  1110. #endif
  1111. #if defined(I2C3)
  1112. void I2C3_DriverIRQHandler(void)
  1113. {
  1114. I2C_TransferCommonIRQHandler(I2C3, s_i2cHandle[3]);
  1115. }
  1116. #endif
  1117. #if defined(I2C4)
  1118. void I2C4_DriverIRQHandler(void)
  1119. {
  1120. I2C_TransferCommonIRQHandler(I2C4, s_i2cHandle[4]);
  1121. }
  1122. #endif
  1123. #include <drv_i2c.h>
  1124. void I2C_DriverIRQHandler(int irq, void *base)
  1125. {
  1126. struct imx6ull_i2c_config *config;
  1127. uint32_t i2c_instance = 0;
  1128. config = (struct imx6ull_i2c_config*)base;
  1129. i2c_instance = I2C_GetInstance(config->hw_base);
  1130. I2C_TransferCommonIRQHandler(config->I2C, s_i2cHandle[i2c_instance]);
  1131. }