fsl_i2c.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_i2c.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.i2c"
  41. #endif
  42. /*! @brief i2c transfer state. */
  43. enum _i2c_transfer_states
  44. {
  45. kIdleState = 0x0U, /*!< I2C bus idle. */
  46. kCheckAddressState = 0x1U, /*!< 7-bit address check state. */
  47. kSendCommandState = 0x2U, /*!< Send command byte phase. */
  48. kSendDataState = 0x3U, /*!< Send data transfer phase. */
  49. kReceiveDataBeginState = 0x4U, /*!< Receive data transfer phase begin. */
  50. kReceiveDataState = 0x5U, /*!< Receive data transfer phase. */
  51. };
  52. /*! @brief Common sets of flags used by the driver. */
  53. enum _i2c_flag_constants
  54. {
  55. /*! All flags which are cleared by the driver upon starting a transfer. */
  56. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  57. kClearFlags = kI2C_ArbitrationLostFlag | kI2C_IntPendingFlag | kI2C_StartDetectFlag | kI2C_StopDetectFlag,
  58. kIrqFlags = kI2C_GlobalInterruptEnable | kI2C_StartStopDetectInterruptEnable,
  59. #elif defined(FSL_FEATURE_I2C_HAS_STOP_DETECT) && FSL_FEATURE_I2C_HAS_STOP_DETECT
  60. kClearFlags = kI2C_ArbitrationLostFlag | kI2C_IntPendingFlag | kI2C_StopDetectFlag,
  61. kIrqFlags = kI2C_GlobalInterruptEnable | kI2C_StopDetectInterruptEnable,
  62. #else
  63. kClearFlags = kI2C_ArbitrationLostFlag | kI2C_IntPendingFlag,
  64. kIrqFlags = kI2C_GlobalInterruptEnable,
  65. #endif
  66. };
  67. /*! @brief Typedef for interrupt handler. */
  68. typedef void (*i2c_isr_t)(I2C_Type *base, void *i2cHandle);
  69. /*******************************************************************************
  70. * Prototypes
  71. ******************************************************************************/
  72. /*!
  73. * @brief Set SCL/SDA hold time, this API receives SCL stop hold time, calculate the
  74. * closest SCL divider and MULT value for the SDA hold time, SCL start and SCL stop
  75. * hold time. To reduce the ROM size, SDA/SCL hold value mapping table is not provided,
  76. * assume SCL divider = SCL stop hold value *2 to get the closest SCL divider value and MULT
  77. * value, then the related SDA hold time, SCL start and SCL stop hold time is used.
  78. *
  79. * @param base I2C peripheral base address.
  80. * @param sourceClock_Hz I2C functional clock frequency in Hertz.
  81. * @param sclStopHoldTime_ns SCL stop hold time in ns.
  82. */
  83. static void I2C_SetHoldTime(I2C_Type *base, uint32_t sclStopHoldTime_ns, uint32_t sourceClock_Hz);
  84. /*!
  85. * @brief Set up master transfer, send slave address and decide the initial
  86. * transfer state.
  87. *
  88. * @param base I2C peripheral base address.
  89. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state.
  90. * @param xfer pointer to i2c_master_transfer_t structure.
  91. */
  92. static status_t I2C_InitTransferStateMachine(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer);
  93. /*!
  94. * @brief Check and clear status operation.
  95. *
  96. * @param base I2C peripheral base address.
  97. * @param status current i2c hardware status.
  98. * @retval kStatus_Success No error found.
  99. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  100. * @retval kStatus_I2C_Nak Received Nak error.
  101. */
  102. static status_t I2C_CheckAndClearError(I2C_Type *base, uint32_t status);
  103. /*!
  104. * @brief Master run transfer state machine to perform a byte of transfer.
  105. *
  106. * @param base I2C peripheral base address.
  107. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state
  108. * @param isDone input param to get whether the thing is done, true is done
  109. * @retval kStatus_Success No error found.
  110. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  111. * @retval kStatus_I2C_Nak Received Nak error.
  112. * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
  113. */
  114. static status_t I2C_MasterTransferRunStateMachine(I2C_Type *base, i2c_master_handle_t *handle, bool *isDone);
  115. /*!
  116. * @brief I2C common interrupt handler.
  117. *
  118. * @param base I2C peripheral base address.
  119. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state
  120. */
  121. static void I2C_TransferCommonIRQHandler(I2C_Type *base, void *handle);
  122. /*******************************************************************************
  123. * Variables
  124. ******************************************************************************/
  125. /*! @brief Pointers to i2c bases for each instance. */
  126. I2C_Type *const s_i2cBases[] = I2C_BASE_PTRS;
  127. /*! @brief Pointers to i2c handles for each instance. */
  128. static void *s_i2cHandle[FSL_FEATURE_SOC_I2C_COUNT] = {NULL};
  129. /*! @brief SCL clock divider used to calculate baudrate. */
  130. static const uint16_t s_i2cDividerTable[] = {
  131. 20, 22, 24, 26, 28, 30, 34, 40, 28, 32, 36, 40, 44, 48, 56, 68,
  132. 48, 56, 64, 72, 80, 88, 104, 128, 80, 96, 112, 128, 144, 160, 192, 240,
  133. 160, 192, 224, 256, 288, 320, 384, 480, 320, 384, 448, 512, 576, 640, 768, 960,
  134. 640, 768, 896, 1024, 1152, 1280, 1536, 1920, 1280, 1536, 1792, 2048, 2304, 2560, 3072, 3840};
  135. /*! @brief Pointers to i2c IRQ number for each instance. */
  136. static const IRQn_Type s_i2cIrqs[] = I2C_IRQS;
  137. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  138. /*! @brief Pointers to i2c clocks for each instance. */
  139. static const clock_ip_name_t s_i2cClocks[] = I2C_CLOCKS;
  140. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  141. /*! @brief Pointer to master IRQ handler for each instance. */
  142. static i2c_isr_t s_i2cMasterIsr;
  143. /*! @brief Pointer to slave IRQ handler for each instance. */
  144. static i2c_isr_t s_i2cSlaveIsr;
  145. /*******************************************************************************
  146. * Codes
  147. ******************************************************************************/
  148. uint32_t I2C_GetInstance(I2C_Type *base)
  149. {
  150. uint32_t instance;
  151. /* Find the instance index from base address mappings. */
  152. for (instance = 0; instance < ARRAY_SIZE(s_i2cBases); instance++)
  153. {
  154. if (s_i2cBases[instance] == base)
  155. {
  156. break;
  157. }
  158. }
  159. assert(instance < ARRAY_SIZE(s_i2cBases));
  160. return instance;
  161. }
  162. static void I2C_SetHoldTime(I2C_Type *base, uint32_t sclStopHoldTime_ns, uint32_t sourceClock_Hz)
  163. {
  164. uint32_t multiplier;
  165. uint32_t computedSclHoldTime;
  166. uint32_t absError;
  167. uint32_t bestError = UINT32_MAX;
  168. uint32_t bestMult = 0u;
  169. uint32_t bestIcr = 0u;
  170. uint8_t mult;
  171. uint8_t i;
  172. /* Search for the settings with the lowest error. Mult is the MULT field of the I2C_F register,
  173. * and ranges from 0-2. It selects the multiplier factor for the divider. */
  174. /* SDA hold time = bus period (s) * mul * SDA hold value. */
  175. /* SCL start hold time = bus period (s) * mul * SCL start hold value. */
  176. /* SCL stop hold time = bus period (s) * mul * SCL stop hold value. */
  177. for (mult = 0u; (mult <= 2u) && (bestError != 0); ++mult)
  178. {
  179. multiplier = 1u << mult;
  180. /* Scan table to find best match. */
  181. for (i = 0u; i < sizeof(s_i2cDividerTable) / sizeof(s_i2cDividerTable[0]); ++i)
  182. {
  183. /* Assume SCL hold(stop) value = s_i2cDividerTable[i]/2. */
  184. computedSclHoldTime = ((multiplier * s_i2cDividerTable[i]) * 500000U) / (sourceClock_Hz / 1000U);
  185. absError = sclStopHoldTime_ns > computedSclHoldTime ? (sclStopHoldTime_ns - computedSclHoldTime) :
  186. (computedSclHoldTime - sclStopHoldTime_ns);
  187. if (absError < bestError)
  188. {
  189. bestMult = mult;
  190. bestIcr = i;
  191. bestError = absError;
  192. /* If the error is 0, then we can stop searching because we won't find a better match. */
  193. if (absError == 0)
  194. {
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. /* Set frequency register based on best settings. */
  201. base->F = I2C_F_MULT(bestMult) | I2C_F_ICR(bestIcr);
  202. }
  203. static status_t I2C_InitTransferStateMachine(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer)
  204. {
  205. status_t result = kStatus_Success;
  206. i2c_direction_t direction = xfer->direction;
  207. /* Initialize the handle transfer information. */
  208. handle->transfer = *xfer;
  209. /* Save total transfer size. */
  210. handle->transferSize = xfer->dataSize;
  211. /* Initial transfer state. */
  212. if (handle->transfer.subaddressSize > 0)
  213. {
  214. if (xfer->direction == kI2C_Read)
  215. {
  216. direction = kI2C_Write;
  217. }
  218. }
  219. handle->state = kCheckAddressState;
  220. /* Clear all status before transfer. */
  221. I2C_MasterClearStatusFlags(base, kClearFlags);
  222. /* Handle no start option. */
  223. if (handle->transfer.flags & kI2C_TransferNoStartFlag)
  224. {
  225. /* No need to send start flag, directly go to send command or data */
  226. if (handle->transfer.subaddressSize > 0)
  227. {
  228. handle->state = kSendCommandState;
  229. }
  230. else
  231. {
  232. if (direction == kI2C_Write)
  233. {
  234. /* Next state, send data. */
  235. handle->state = kSendDataState;
  236. }
  237. else
  238. {
  239. /* Only support write with no stop signal. */
  240. return kStatus_InvalidArgument;
  241. }
  242. }
  243. /* Wait for TCF bit and manually trigger tx interrupt. */
  244. while (!(base->S & kI2C_TransferCompleteFlag))
  245. {
  246. }
  247. I2C_MasterTransferHandleIRQ(base, handle);
  248. }
  249. /* If repeated start is requested, send repeated start. */
  250. else if (handle->transfer.flags & kI2C_TransferRepeatedStartFlag)
  251. {
  252. result = I2C_MasterRepeatedStart(base, handle->transfer.slaveAddress, direction);
  253. }
  254. else /* For normal transfer, send start. */
  255. {
  256. result = I2C_MasterStart(base, handle->transfer.slaveAddress, direction);
  257. }
  258. return result;
  259. }
  260. static status_t I2C_CheckAndClearError(I2C_Type *base, uint32_t status)
  261. {
  262. status_t result = kStatus_Success;
  263. /* Check arbitration lost. */
  264. if (status & kI2C_ArbitrationLostFlag)
  265. {
  266. /* Clear arbitration lost flag. */
  267. base->S = kI2C_ArbitrationLostFlag;
  268. result = kStatus_I2C_ArbitrationLost;
  269. }
  270. /* Check NAK */
  271. else if (status & kI2C_ReceiveNakFlag)
  272. {
  273. result = kStatus_I2C_Nak;
  274. }
  275. else
  276. {
  277. }
  278. return result;
  279. }
  280. static status_t I2C_MasterTransferRunStateMachine(I2C_Type *base, i2c_master_handle_t *handle, bool *isDone)
  281. {
  282. status_t result = kStatus_Success;
  283. uint32_t statusFlags = base->S;
  284. *isDone = false;
  285. volatile uint8_t dummy = 0;
  286. bool ignoreNak = ((handle->state == kSendDataState) && (handle->transfer.dataSize == 0U)) ||
  287. ((handle->state == kReceiveDataState) && (handle->transfer.dataSize == 1U));
  288. /* Add this to avoid build warning. */
  289. dummy++;
  290. /* Check & clear error flags. */
  291. result = I2C_CheckAndClearError(base, statusFlags);
  292. /* Ignore Nak when it's appeared for last byte. */
  293. if ((result == kStatus_I2C_Nak) && ignoreNak)
  294. {
  295. result = kStatus_Success;
  296. }
  297. /* Handle Check address state to check the slave address is Acked in slave
  298. probe application. */
  299. if (handle->state == kCheckAddressState)
  300. {
  301. if (statusFlags & kI2C_ReceiveNakFlag)
  302. {
  303. result = kStatus_I2C_Addr_Nak;
  304. }
  305. else
  306. {
  307. if (handle->transfer.subaddressSize > 0)
  308. {
  309. handle->state = kSendCommandState;
  310. }
  311. else
  312. {
  313. if (handle->transfer.direction == kI2C_Write)
  314. {
  315. /* Next state, send data. */
  316. handle->state = kSendDataState;
  317. }
  318. else
  319. {
  320. /* Next state, receive data begin. */
  321. handle->state = kReceiveDataBeginState;
  322. }
  323. }
  324. }
  325. }
  326. if (result)
  327. {
  328. return result;
  329. }
  330. /* Run state machine. */
  331. switch (handle->state)
  332. {
  333. /* Send I2C command. */
  334. case kSendCommandState:
  335. if (handle->transfer.subaddressSize)
  336. {
  337. handle->transfer.subaddressSize--;
  338. base->D = ((handle->transfer.subaddress) >> (8 * handle->transfer.subaddressSize));
  339. }
  340. else
  341. {
  342. if (handle->transfer.direction == kI2C_Write)
  343. {
  344. /* Next state, send data. */
  345. handle->state = kSendDataState;
  346. /* Send first byte of data. */
  347. if (handle->transfer.dataSize > 0)
  348. {
  349. base->D = *handle->transfer.data;
  350. handle->transfer.data++;
  351. handle->transfer.dataSize--;
  352. }
  353. }
  354. else
  355. {
  356. /* Send repeated start and slave address. */
  357. result = I2C_MasterRepeatedStart(base, handle->transfer.slaveAddress, kI2C_Read);
  358. /* Next state, receive data begin. */
  359. handle->state = kReceiveDataBeginState;
  360. }
  361. }
  362. break;
  363. /* Send I2C data. */
  364. case kSendDataState:
  365. /* Send one byte of data. */
  366. if (handle->transfer.dataSize > 0)
  367. {
  368. base->D = *handle->transfer.data;
  369. handle->transfer.data++;
  370. handle->transfer.dataSize--;
  371. }
  372. else
  373. {
  374. *isDone = true;
  375. }
  376. break;
  377. /* Start I2C data receive. */
  378. case kReceiveDataBeginState:
  379. base->C1 &= ~(I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
  380. /* Send nak at the last receive byte. */
  381. if (handle->transfer.dataSize == 1)
  382. {
  383. base->C1 |= I2C_C1_TXAK_MASK;
  384. }
  385. /* Read dummy to release the bus. */
  386. dummy = base->D;
  387. /* Next state, receive data. */
  388. handle->state = kReceiveDataState;
  389. break;
  390. /* Receive I2C data. */
  391. case kReceiveDataState:
  392. /* Receive one byte of data. */
  393. if (handle->transfer.dataSize--)
  394. {
  395. if (handle->transfer.dataSize == 0)
  396. {
  397. *isDone = true;
  398. /* Send stop if kI2C_TransferNoStop is not asserted. */
  399. if (!(handle->transfer.flags & kI2C_TransferNoStopFlag))
  400. {
  401. result = I2C_MasterStop(base);
  402. }
  403. else
  404. {
  405. base->C1 |= I2C_C1_TX_MASK;
  406. }
  407. }
  408. /* Send NAK at the last receive byte. */
  409. if (handle->transfer.dataSize == 1)
  410. {
  411. base->C1 |= I2C_C1_TXAK_MASK;
  412. }
  413. /* Read the data byte into the transfer buffer. */
  414. *handle->transfer.data = base->D;
  415. handle->transfer.data++;
  416. }
  417. break;
  418. default:
  419. break;
  420. }
  421. return result;
  422. }
  423. static void I2C_TransferCommonIRQHandler(I2C_Type *base, void *handle)
  424. {
  425. /* Check if master interrupt. */
  426. if ((base->S & kI2C_ArbitrationLostFlag) || (base->C1 & I2C_C1_MST_MASK))
  427. {
  428. s_i2cMasterIsr(base, handle);
  429. }
  430. else
  431. {
  432. s_i2cSlaveIsr(base, handle);
  433. }
  434. __DSB();
  435. }
  436. void I2C_MasterInit(I2C_Type *base, const i2c_master_config_t *masterConfig, uint32_t srcClock_Hz)
  437. {
  438. assert(masterConfig && srcClock_Hz);
  439. /* Temporary register for filter read. */
  440. uint8_t fltReg;
  441. #if defined(FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE) && FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE
  442. uint8_t s2Reg;
  443. #endif
  444. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  445. /* Enable I2C clock. */
  446. CLOCK_EnableClock(s_i2cClocks[I2C_GetInstance(base)]);
  447. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  448. /* Reset the module. */
  449. base->A1 = 0;
  450. base->F = 0;
  451. base->C1 = 0;
  452. base->S = 0xFFU;
  453. base->C2 = 0;
  454. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  455. base->FLT = 0x50U;
  456. #elif defined(FSL_FEATURE_I2C_HAS_STOP_DETECT) && FSL_FEATURE_I2C_HAS_STOP_DETECT
  457. base->FLT = 0x40U;
  458. #endif
  459. base->RA = 0;
  460. /* Disable I2C prior to configuring it. */
  461. base->C1 &= ~(I2C_C1_IICEN_MASK);
  462. /* Clear all flags. */
  463. I2C_MasterClearStatusFlags(base, kClearFlags);
  464. /* Configure baud rate. */
  465. I2C_MasterSetBaudRate(base, masterConfig->baudRate_Bps, srcClock_Hz);
  466. /* Read out the FLT register. */
  467. fltReg = base->FLT;
  468. #if defined(FSL_FEATURE_I2C_HAS_STOP_HOLD_OFF) && FSL_FEATURE_I2C_HAS_STOP_HOLD_OFF
  469. /* Configure the stop / hold enable. */
  470. fltReg &= ~(I2C_FLT_SHEN_MASK);
  471. fltReg |= I2C_FLT_SHEN(masterConfig->enableStopHold);
  472. #endif
  473. /* Configure the glitch filter value. */
  474. fltReg &= ~(I2C_FLT_FLT_MASK);
  475. fltReg |= I2C_FLT_FLT(masterConfig->glitchFilterWidth);
  476. /* Write the register value back to the filter register. */
  477. base->FLT = fltReg;
  478. /* Enable/Disable double buffering. */
  479. #if defined(FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE) && FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE
  480. s2Reg = base->S2 & (~I2C_S2_DFEN_MASK);
  481. base->S2 = s2Reg | I2C_S2_DFEN(masterConfig->enableDoubleBuffering);
  482. #endif
  483. /* Enable the I2C peripheral based on the configuration. */
  484. base->C1 = I2C_C1_IICEN(masterConfig->enableMaster);
  485. }
  486. void I2C_MasterDeinit(I2C_Type *base)
  487. {
  488. /* Disable I2C module. */
  489. I2C_Enable(base, false);
  490. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  491. /* Disable I2C clock. */
  492. CLOCK_DisableClock(s_i2cClocks[I2C_GetInstance(base)]);
  493. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  494. }
  495. void I2C_MasterGetDefaultConfig(i2c_master_config_t *masterConfig)
  496. {
  497. assert(masterConfig);
  498. /* Default baud rate at 100kbps. */
  499. masterConfig->baudRate_Bps = 100000U;
  500. /* Default stop hold enable is disabled. */
  501. #if defined(FSL_FEATURE_I2C_HAS_STOP_HOLD_OFF) && FSL_FEATURE_I2C_HAS_STOP_HOLD_OFF
  502. masterConfig->enableStopHold = false;
  503. #endif
  504. /* Default glitch filter value is no filter. */
  505. masterConfig->glitchFilterWidth = 0U;
  506. /* Default enable double buffering. */
  507. #if defined(FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE) && FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE
  508. masterConfig->enableDoubleBuffering = true;
  509. #endif
  510. /* Enable the I2C peripheral. */
  511. masterConfig->enableMaster = true;
  512. }
  513. void I2C_EnableInterrupts(I2C_Type *base, uint32_t mask)
  514. {
  515. #ifdef I2C_HAS_STOP_DETECT
  516. uint8_t fltReg;
  517. #endif
  518. if (mask & kI2C_GlobalInterruptEnable)
  519. {
  520. base->C1 |= I2C_C1_IICIE_MASK;
  521. }
  522. #if defined(FSL_FEATURE_I2C_HAS_STOP_DETECT) && FSL_FEATURE_I2C_HAS_STOP_DETECT
  523. if (mask & kI2C_StopDetectInterruptEnable)
  524. {
  525. fltReg = base->FLT;
  526. /* Keep STOPF flag. */
  527. fltReg &= ~I2C_FLT_STOPF_MASK;
  528. /* Stop detect enable. */
  529. fltReg |= I2C_FLT_STOPIE_MASK;
  530. base->FLT = fltReg;
  531. }
  532. #endif /* FSL_FEATURE_I2C_HAS_STOP_DETECT */
  533. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  534. if (mask & kI2C_StartStopDetectInterruptEnable)
  535. {
  536. fltReg = base->FLT;
  537. /* Keep STARTF and STOPF flags. */
  538. fltReg &= ~(I2C_FLT_STOPF_MASK | I2C_FLT_STARTF_MASK);
  539. /* Start and stop detect enable. */
  540. fltReg |= I2C_FLT_SSIE_MASK;
  541. base->FLT = fltReg;
  542. }
  543. #endif /* FSL_FEATURE_I2C_HAS_START_STOP_DETECT */
  544. }
  545. void I2C_DisableInterrupts(I2C_Type *base, uint32_t mask)
  546. {
  547. if (mask & kI2C_GlobalInterruptEnable)
  548. {
  549. base->C1 &= ~I2C_C1_IICIE_MASK;
  550. }
  551. #if defined(FSL_FEATURE_I2C_HAS_STOP_DETECT) && FSL_FEATURE_I2C_HAS_STOP_DETECT
  552. if (mask & kI2C_StopDetectInterruptEnable)
  553. {
  554. base->FLT &= ~(I2C_FLT_STOPIE_MASK | I2C_FLT_STOPF_MASK);
  555. }
  556. #endif /* FSL_FEATURE_I2C_HAS_STOP_DETECT */
  557. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  558. if (mask & kI2C_StartStopDetectInterruptEnable)
  559. {
  560. base->FLT &= ~(I2C_FLT_SSIE_MASK | I2C_FLT_STOPF_MASK | I2C_FLT_STARTF_MASK);
  561. }
  562. #endif /* FSL_FEATURE_I2C_HAS_START_STOP_DETECT */
  563. }
  564. void I2C_MasterSetBaudRate(I2C_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
  565. {
  566. uint32_t multiplier;
  567. uint32_t computedRate;
  568. uint32_t absError;
  569. uint32_t bestError = UINT32_MAX;
  570. uint32_t bestMult = 0u;
  571. uint32_t bestIcr = 0u;
  572. uint8_t mult;
  573. uint8_t i;
  574. /* Search for the settings with the lowest error. Mult is the MULT field of the I2C_F register,
  575. * and ranges from 0-2. It selects the multiplier factor for the divider. */
  576. for (mult = 0u; (mult <= 2u) && (bestError != 0); ++mult)
  577. {
  578. multiplier = 1u << mult;
  579. /* Scan table to find best match. */
  580. for (i = 0u; i < sizeof(s_i2cDividerTable) / sizeof(uint16_t); ++i)
  581. {
  582. computedRate = srcClock_Hz / (multiplier * s_i2cDividerTable[i]);
  583. absError = baudRate_Bps > computedRate ? (baudRate_Bps - computedRate) : (computedRate - baudRate_Bps);
  584. if (absError < bestError)
  585. {
  586. bestMult = mult;
  587. bestIcr = i;
  588. bestError = absError;
  589. /* If the error is 0, then we can stop searching because we won't find a better match. */
  590. if (absError == 0)
  591. {
  592. break;
  593. }
  594. }
  595. }
  596. }
  597. /* Set frequency register based on best settings. */
  598. base->F = I2C_F_MULT(bestMult) | I2C_F_ICR(bestIcr);
  599. }
  600. status_t I2C_MasterStart(I2C_Type *base, uint8_t address, i2c_direction_t direction)
  601. {
  602. status_t result = kStatus_Success;
  603. uint32_t statusFlags = I2C_MasterGetStatusFlags(base);
  604. /* Return an error if the bus is already in use. */
  605. if (statusFlags & kI2C_BusBusyFlag)
  606. {
  607. result = kStatus_I2C_Busy;
  608. }
  609. else
  610. {
  611. /* Send the START signal. */
  612. base->C1 |= I2C_C1_MST_MASK | I2C_C1_TX_MASK;
  613. #if defined(FSL_FEATURE_I2C_HAS_DOUBLE_BUFFERING) && FSL_FEATURE_I2C_HAS_DOUBLE_BUFFERING
  614. #if I2C_WAIT_TIMEOUT
  615. uint32_t waitTimes = I2C_WAIT_TIMEOUT;
  616. while ((!(base->S2 & I2C_S2_EMPTY_MASK)) && (--waitTimes))
  617. {
  618. }
  619. if (waitTimes == 0)
  620. {
  621. return kStatus_I2C_Timeout;
  622. }
  623. #else
  624. while (!(base->S2 & I2C_S2_EMPTY_MASK))
  625. {
  626. }
  627. #endif
  628. #endif /* FSL_FEATURE_I2C_HAS_DOUBLE_BUFFERING */
  629. base->D = (((uint32_t)address) << 1U | ((direction == kI2C_Read) ? 1U : 0U));
  630. }
  631. return result;
  632. }
  633. status_t I2C_MasterRepeatedStart(I2C_Type *base, uint8_t address, i2c_direction_t direction)
  634. {
  635. status_t result = kStatus_Success;
  636. uint8_t savedMult;
  637. uint32_t statusFlags = I2C_MasterGetStatusFlags(base);
  638. uint8_t timeDelay = 6;
  639. /* Return an error if the bus is already in use, but not by us. */
  640. if ((statusFlags & kI2C_BusBusyFlag) && ((base->C1 & I2C_C1_MST_MASK) == 0))
  641. {
  642. result = kStatus_I2C_Busy;
  643. }
  644. else
  645. {
  646. savedMult = base->F;
  647. base->F = savedMult & (~I2C_F_MULT_MASK);
  648. /* We are already in a transfer, so send a repeated start. */
  649. base->C1 |= I2C_C1_RSTA_MASK | I2C_C1_TX_MASK;
  650. /* Restore the multiplier factor. */
  651. base->F = savedMult;
  652. /* Add some delay to wait the Re-Start signal. */
  653. while (timeDelay--)
  654. {
  655. __NOP();
  656. }
  657. #if defined(FSL_FEATURE_I2C_HAS_DOUBLE_BUFFERING) && FSL_FEATURE_I2C_HAS_DOUBLE_BUFFERING
  658. #if I2C_WAIT_TIMEOUT
  659. uint32_t waitTimes = I2C_WAIT_TIMEOUT;
  660. while ((!(base->S2 & I2C_S2_EMPTY_MASK)) && (--waitTimes))
  661. {
  662. }
  663. if (waitTimes == 0)
  664. {
  665. return kStatus_I2C_Timeout;
  666. }
  667. #else
  668. while (!(base->S2 & I2C_S2_EMPTY_MASK))
  669. {
  670. }
  671. #endif
  672. #endif /* FSL_FEATURE_I2C_HAS_DOUBLE_BUFFERING */
  673. base->D = (((uint32_t)address) << 1U | ((direction == kI2C_Read) ? 1U : 0U));
  674. }
  675. return result;
  676. }
  677. status_t I2C_MasterStop(I2C_Type *base)
  678. {
  679. status_t result = kStatus_Success;
  680. /* Issue the STOP command on the bus. */
  681. base->C1 &= ~(I2C_C1_MST_MASK | I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
  682. #if I2C_WAIT_TIMEOUT
  683. uint32_t waitTimes = I2C_WAIT_TIMEOUT;
  684. /* Wait until bus not busy. */
  685. while ((base->S & kI2C_BusBusyFlag) && (--waitTimes))
  686. {
  687. }
  688. if (waitTimes == 0)
  689. {
  690. result = kStatus_I2C_Timeout;
  691. }
  692. #else
  693. /* Wait until data transfer complete. */
  694. while (base->S & kI2C_BusBusyFlag)
  695. {
  696. }
  697. #endif
  698. return result;
  699. }
  700. uint32_t I2C_MasterGetStatusFlags(I2C_Type *base)
  701. {
  702. uint32_t statusFlags = base->S;
  703. #ifdef I2C_HAS_STOP_DETECT
  704. /* Look up the STOPF bit from the filter register. */
  705. if (base->FLT & I2C_FLT_STOPF_MASK)
  706. {
  707. statusFlags |= kI2C_StopDetectFlag;
  708. }
  709. #endif
  710. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  711. /* Look up the STARTF bit from the filter register. */
  712. if (base->FLT & I2C_FLT_STARTF_MASK)
  713. {
  714. statusFlags |= kI2C_StartDetectFlag;
  715. }
  716. #endif /* FSL_FEATURE_I2C_HAS_START_STOP_DETECT */
  717. return statusFlags;
  718. }
  719. status_t I2C_MasterWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize, uint32_t flags)
  720. {
  721. status_t result = kStatus_Success;
  722. uint8_t statusFlags = 0;
  723. #if I2C_WAIT_TIMEOUT
  724. uint32_t waitTimes = I2C_WAIT_TIMEOUT;
  725. /* Wait until the data register is ready for transmit. */
  726. while ((!(base->S & kI2C_TransferCompleteFlag)) && (--waitTimes))
  727. {
  728. }
  729. if (waitTimes == 0)
  730. {
  731. return kStatus_I2C_Timeout;
  732. }
  733. #else
  734. /* Wait until the data register is ready for transmit. */
  735. while (!(base->S & kI2C_TransferCompleteFlag))
  736. {
  737. }
  738. #endif
  739. /* Clear the IICIF flag. */
  740. base->S = kI2C_IntPendingFlag;
  741. /* Setup the I2C peripheral to transmit data. */
  742. base->C1 |= I2C_C1_TX_MASK;
  743. while (txSize--)
  744. {
  745. /* Send a byte of data. */
  746. base->D = *txBuff++;
  747. #if I2C_WAIT_TIMEOUT
  748. waitTimes = I2C_WAIT_TIMEOUT;
  749. /* Wait until data transfer complete. */
  750. while ((!(base->S & kI2C_IntPendingFlag)) && (--waitTimes))
  751. {
  752. }
  753. if (waitTimes == 0)
  754. {
  755. return kStatus_I2C_Timeout;
  756. }
  757. #else
  758. /* Wait until data transfer complete. */
  759. while (!(base->S & kI2C_IntPendingFlag))
  760. {
  761. }
  762. #endif
  763. statusFlags = base->S;
  764. /* Clear the IICIF flag. */
  765. base->S = kI2C_IntPendingFlag;
  766. /* Check if arbitration lost or no acknowledgement (NAK), return failure status. */
  767. if (statusFlags & kI2C_ArbitrationLostFlag)
  768. {
  769. base->S = kI2C_ArbitrationLostFlag;
  770. result = kStatus_I2C_ArbitrationLost;
  771. }
  772. if ((statusFlags & kI2C_ReceiveNakFlag) && txSize)
  773. {
  774. base->S = kI2C_ReceiveNakFlag;
  775. result = kStatus_I2C_Nak;
  776. }
  777. if (result != kStatus_Success)
  778. {
  779. /* Breaking out of the send loop. */
  780. break;
  781. }
  782. }
  783. if (((result == kStatus_Success) && (!(flags & kI2C_TransferNoStopFlag))) || (result == kStatus_I2C_Nak))
  784. {
  785. /* Clear the IICIF flag. */
  786. base->S = kI2C_IntPendingFlag;
  787. /* Send stop. */
  788. result = I2C_MasterStop(base);
  789. }
  790. return result;
  791. }
  792. status_t I2C_MasterReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize, uint32_t flags)
  793. {
  794. status_t result = kStatus_Success;
  795. volatile uint8_t dummy = 0;
  796. /* Add this to avoid build warning. */
  797. dummy++;
  798. #if I2C_WAIT_TIMEOUT
  799. uint32_t waitTimes = I2C_WAIT_TIMEOUT;
  800. /* Wait until the data register is ready for transmit. */
  801. while ((!(base->S & kI2C_TransferCompleteFlag)) && (--waitTimes))
  802. {
  803. }
  804. if (waitTimes == 0)
  805. {
  806. return kStatus_I2C_Timeout;
  807. }
  808. #else
  809. /* Wait until the data register is ready for transmit. */
  810. while (!(base->S & kI2C_TransferCompleteFlag))
  811. {
  812. }
  813. #endif
  814. /* Clear the IICIF flag. */
  815. base->S = kI2C_IntPendingFlag;
  816. /* Setup the I2C peripheral to receive data. */
  817. base->C1 &= ~(I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
  818. /* If rxSize equals 1, configure to send NAK. */
  819. if (rxSize == 1)
  820. {
  821. /* Issue NACK on read. */
  822. base->C1 |= I2C_C1_TXAK_MASK;
  823. }
  824. /* Do dummy read. */
  825. dummy = base->D;
  826. while ((rxSize--))
  827. {
  828. #if I2C_WAIT_TIMEOUT
  829. waitTimes = I2C_WAIT_TIMEOUT;
  830. /* Wait until data transfer complete. */
  831. while ((!(base->S & kI2C_IntPendingFlag)) && (--waitTimes))
  832. {
  833. }
  834. if (waitTimes == 0)
  835. {
  836. return kStatus_I2C_Timeout;
  837. }
  838. #else
  839. /* Wait until data transfer complete. */
  840. while (!(base->S & kI2C_IntPendingFlag))
  841. {
  842. }
  843. #endif
  844. /* Clear the IICIF flag. */
  845. base->S = kI2C_IntPendingFlag;
  846. /* Single byte use case. */
  847. if (rxSize == 0)
  848. {
  849. if (!(flags & kI2C_TransferNoStopFlag))
  850. {
  851. /* Issue STOP command before reading last byte. */
  852. result = I2C_MasterStop(base);
  853. }
  854. else
  855. {
  856. /* Change direction to Tx to avoid extra clocks. */
  857. base->C1 |= I2C_C1_TX_MASK;
  858. }
  859. }
  860. if (rxSize == 1)
  861. {
  862. /* Issue NACK on read. */
  863. base->C1 |= I2C_C1_TXAK_MASK;
  864. }
  865. /* Read from the data register. */
  866. *rxBuff++ = base->D;
  867. }
  868. return result;
  869. }
  870. status_t I2C_MasterTransferBlocking(I2C_Type *base, i2c_master_transfer_t *xfer)
  871. {
  872. assert(xfer);
  873. i2c_direction_t direction = xfer->direction;
  874. status_t result = kStatus_Success;
  875. /* Clear all status before transfer. */
  876. I2C_MasterClearStatusFlags(base, kClearFlags);
  877. #if I2C_WAIT_TIMEOUT
  878. uint32_t waitTimes = I2C_WAIT_TIMEOUT;
  879. /* Wait until the data register is ready for transmit. */
  880. while ((!(base->S & kI2C_TransferCompleteFlag)) && (--waitTimes))
  881. {
  882. }
  883. if (waitTimes == 0)
  884. {
  885. return kStatus_I2C_Timeout;
  886. }
  887. #else
  888. /* Wait until the data register is ready for transmit. */
  889. while (!(base->S & kI2C_TransferCompleteFlag))
  890. {
  891. }
  892. #endif
  893. /* Change to send write address when it's a read operation with command. */
  894. if ((xfer->subaddressSize > 0) && (xfer->direction == kI2C_Read))
  895. {
  896. direction = kI2C_Write;
  897. }
  898. /* Handle no start option, only support write with no start signal. */
  899. if (xfer->flags & kI2C_TransferNoStartFlag)
  900. {
  901. if (direction == kI2C_Read)
  902. {
  903. return kStatus_InvalidArgument;
  904. }
  905. }
  906. /* If repeated start is requested, send repeated start. */
  907. else if (xfer->flags & kI2C_TransferRepeatedStartFlag)
  908. {
  909. result = I2C_MasterRepeatedStart(base, xfer->slaveAddress, direction);
  910. }
  911. else /* For normal transfer, send start. */
  912. {
  913. result = I2C_MasterStart(base, xfer->slaveAddress, direction);
  914. }
  915. if (!(xfer->flags & kI2C_TransferNoStartFlag))
  916. {
  917. /* Return if error. */
  918. if (result)
  919. {
  920. return result;
  921. }
  922. #if I2C_WAIT_TIMEOUT
  923. waitTimes = I2C_WAIT_TIMEOUT;
  924. /* Wait until data transfer complete. */
  925. while ((!(base->S & kI2C_IntPendingFlag)) && (--waitTimes))
  926. {
  927. }
  928. if (waitTimes == 0)
  929. {
  930. return kStatus_I2C_Timeout;
  931. }
  932. #else
  933. /* Wait until data transfer complete. */
  934. while (!(base->S & kI2C_IntPendingFlag))
  935. {
  936. }
  937. #endif
  938. /* Check if there's transfer error. */
  939. result = I2C_CheckAndClearError(base, base->S);
  940. /* Return if error. */
  941. if (result)
  942. {
  943. if (result == kStatus_I2C_Nak)
  944. {
  945. result = kStatus_I2C_Addr_Nak;
  946. I2C_MasterStop(base);
  947. }
  948. return result;
  949. }
  950. }
  951. /* Send subaddress. */
  952. if (xfer->subaddressSize)
  953. {
  954. do
  955. {
  956. /* Clear interrupt pending flag. */
  957. base->S = kI2C_IntPendingFlag;
  958. xfer->subaddressSize--;
  959. base->D = ((xfer->subaddress) >> (8 * xfer->subaddressSize));
  960. #if I2C_WAIT_TIMEOUT
  961. waitTimes = I2C_WAIT_TIMEOUT;
  962. /* Wait until data transfer complete. */
  963. while ((!(base->S & kI2C_IntPendingFlag)) && (--waitTimes))
  964. {
  965. }
  966. if (waitTimes == 0)
  967. {
  968. return kStatus_I2C_Timeout;
  969. }
  970. #else
  971. /* Wait until data transfer complete. */
  972. while (!(base->S & kI2C_IntPendingFlag))
  973. {
  974. }
  975. #endif
  976. /* Check if there's transfer error. */
  977. result = I2C_CheckAndClearError(base, base->S);
  978. if (result)
  979. {
  980. if (result == kStatus_I2C_Nak)
  981. {
  982. I2C_MasterStop(base);
  983. }
  984. return result;
  985. }
  986. } while (xfer->subaddressSize > 0);
  987. if (xfer->direction == kI2C_Read)
  988. {
  989. /* Clear pending flag. */
  990. base->S = kI2C_IntPendingFlag;
  991. /* Send repeated start and slave address. */
  992. result = I2C_MasterRepeatedStart(base, xfer->slaveAddress, kI2C_Read);
  993. /* Return if error. */
  994. if (result)
  995. {
  996. return result;
  997. }
  998. #if I2C_WAIT_TIMEOUT
  999. waitTimes = I2C_WAIT_TIMEOUT;
  1000. /* Wait until data transfer complete. */
  1001. while ((!(base->S & kI2C_IntPendingFlag)) && (--waitTimes))
  1002. {
  1003. }
  1004. if (waitTimes == 0)
  1005. {
  1006. return kStatus_I2C_Timeout;
  1007. }
  1008. #else
  1009. /* Wait until data transfer complete. */
  1010. while (!(base->S & kI2C_IntPendingFlag))
  1011. {
  1012. }
  1013. #endif
  1014. /* Check if there's transfer error. */
  1015. result = I2C_CheckAndClearError(base, base->S);
  1016. if (result)
  1017. {
  1018. if (result == kStatus_I2C_Nak)
  1019. {
  1020. result = kStatus_I2C_Addr_Nak;
  1021. I2C_MasterStop(base);
  1022. }
  1023. return result;
  1024. }
  1025. }
  1026. }
  1027. /* Transmit data. */
  1028. if ((xfer->direction == kI2C_Write) && (xfer->dataSize > 0))
  1029. {
  1030. /* Send Data. */
  1031. result = I2C_MasterWriteBlocking(base, xfer->data, xfer->dataSize, xfer->flags);
  1032. }
  1033. /* Receive Data. */
  1034. if ((xfer->direction == kI2C_Read) && (xfer->dataSize > 0))
  1035. {
  1036. result = I2C_MasterReadBlocking(base, xfer->data, xfer->dataSize, xfer->flags);
  1037. }
  1038. return result;
  1039. }
  1040. void I2C_MasterTransferCreateHandle(I2C_Type *base,
  1041. i2c_master_handle_t *handle,
  1042. i2c_master_transfer_callback_t callback,
  1043. void *userData)
  1044. {
  1045. assert(handle);
  1046. uint32_t instance = I2C_GetInstance(base);
  1047. /* Zero handle. */
  1048. memset(handle, 0, sizeof(*handle));
  1049. /* Set callback and userData. */
  1050. handle->completionCallback = callback;
  1051. handle->userData = userData;
  1052. /* Save the context in global variables to support the double weak mechanism. */
  1053. s_i2cHandle[instance] = handle;
  1054. /* Save master interrupt handler. */
  1055. s_i2cMasterIsr = I2C_MasterTransferHandleIRQ;
  1056. /* Enable NVIC interrupt. */
  1057. EnableIRQ(s_i2cIrqs[instance]);
  1058. }
  1059. status_t I2C_MasterTransferNonBlocking(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer)
  1060. {
  1061. assert(handle);
  1062. assert(xfer);
  1063. status_t result = kStatus_Success;
  1064. /* Check if the I2C bus is idle - if not return busy status. */
  1065. if (handle->state != kIdleState)
  1066. {
  1067. result = kStatus_I2C_Busy;
  1068. }
  1069. else
  1070. {
  1071. /* Start up the master transfer state machine. */
  1072. result = I2C_InitTransferStateMachine(base, handle, xfer);
  1073. if (result == kStatus_Success)
  1074. {
  1075. /* Enable the I2C interrupts. */
  1076. I2C_EnableInterrupts(base, kI2C_GlobalInterruptEnable);
  1077. }
  1078. }
  1079. return result;
  1080. }
  1081. status_t I2C_MasterTransferAbort(I2C_Type *base, i2c_master_handle_t *handle)
  1082. {
  1083. assert(handle);
  1084. volatile uint8_t dummy = 0;
  1085. #if I2C_WAIT_TIMEOUT
  1086. uint32_t waitTimes = I2C_WAIT_TIMEOUT;
  1087. #endif
  1088. /* Add this to avoid build warning. */
  1089. dummy++;
  1090. /* Disable interrupt. */
  1091. I2C_DisableInterrupts(base, kI2C_GlobalInterruptEnable);
  1092. /* Reset the state to idle. */
  1093. handle->state = kIdleState;
  1094. /* If the bus is already in use, but not by us */
  1095. if (!(base->C1 & I2C_C1_MST_MASK))
  1096. {
  1097. return kStatus_I2C_Busy;
  1098. }
  1099. /* Send STOP signal. */
  1100. if (handle->transfer.direction == kI2C_Read)
  1101. {
  1102. base->C1 |= I2C_C1_TXAK_MASK;
  1103. #if I2C_WAIT_TIMEOUT
  1104. /* Wait until data transfer complete. */
  1105. while ((!(base->S & kI2C_IntPendingFlag)) && (--waitTimes))
  1106. {
  1107. }
  1108. if (waitTimes == 0)
  1109. {
  1110. return kStatus_I2C_Timeout;
  1111. }
  1112. #else
  1113. /* Wait until data transfer complete. */
  1114. while (!(base->S & kI2C_IntPendingFlag))
  1115. {
  1116. }
  1117. #endif
  1118. base->S = kI2C_IntPendingFlag;
  1119. base->C1 &= ~(I2C_C1_MST_MASK | I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
  1120. dummy = base->D;
  1121. }
  1122. else
  1123. {
  1124. #if I2C_WAIT_TIMEOUT
  1125. /* Wait until data transfer complete. */
  1126. while ((!(base->S & kI2C_IntPendingFlag)) && (--waitTimes))
  1127. {
  1128. }
  1129. if (waitTimes == 0)
  1130. {
  1131. return kStatus_I2C_Timeout;
  1132. }
  1133. #else
  1134. /* Wait until data transfer complete. */
  1135. while (!(base->S & kI2C_IntPendingFlag))
  1136. {
  1137. }
  1138. #endif
  1139. base->S = kI2C_IntPendingFlag;
  1140. base->C1 &= ~(I2C_C1_MST_MASK | I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
  1141. }
  1142. return kStatus_Success;
  1143. }
  1144. status_t I2C_MasterTransferGetCount(I2C_Type *base, i2c_master_handle_t *handle, size_t *count)
  1145. {
  1146. assert(handle);
  1147. if (!count)
  1148. {
  1149. return kStatus_InvalidArgument;
  1150. }
  1151. *count = handle->transferSize - handle->transfer.dataSize;
  1152. return kStatus_Success;
  1153. }
  1154. void I2C_MasterTransferHandleIRQ(I2C_Type *base, void *i2cHandle)
  1155. {
  1156. assert(i2cHandle);
  1157. i2c_master_handle_t *handle = (i2c_master_handle_t *)i2cHandle;
  1158. status_t result = kStatus_Success;
  1159. bool isDone;
  1160. /* Clear the interrupt flag. */
  1161. base->S = kI2C_IntPendingFlag;
  1162. /* Check transfer complete flag. */
  1163. result = I2C_MasterTransferRunStateMachine(base, handle, &isDone);
  1164. if (isDone || result)
  1165. {
  1166. /* Send stop command if transfer done or received Nak. */
  1167. if ((!(handle->transfer.flags & kI2C_TransferNoStopFlag)) || (result == kStatus_I2C_Nak) ||
  1168. (result == kStatus_I2C_Addr_Nak))
  1169. {
  1170. /* Ensure stop command is a need. */
  1171. if ((base->C1 & I2C_C1_MST_MASK))
  1172. {
  1173. if (I2C_MasterStop(base) != kStatus_Success)
  1174. {
  1175. result = kStatus_I2C_Timeout;
  1176. }
  1177. }
  1178. }
  1179. /* Restore handle to idle state. */
  1180. handle->state = kIdleState;
  1181. /* Disable interrupt. */
  1182. I2C_DisableInterrupts(base, kI2C_GlobalInterruptEnable);
  1183. /* Call the callback function after the function has completed. */
  1184. if (handle->completionCallback)
  1185. {
  1186. handle->completionCallback(base, handle, result, handle->userData);
  1187. }
  1188. }
  1189. }
  1190. void I2C_SlaveInit(I2C_Type *base, const i2c_slave_config_t *slaveConfig, uint32_t srcClock_Hz)
  1191. {
  1192. assert(slaveConfig);
  1193. uint8_t tmpReg;
  1194. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  1195. CLOCK_EnableClock(s_i2cClocks[I2C_GetInstance(base)]);
  1196. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  1197. /* Reset the module. */
  1198. base->A1 = 0;
  1199. base->F = 0;
  1200. base->C1 = 0;
  1201. base->S = 0xFFU;
  1202. base->C2 = 0;
  1203. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  1204. base->FLT = 0x50U;
  1205. #elif defined(FSL_FEATURE_I2C_HAS_STOP_DETECT) && FSL_FEATURE_I2C_HAS_STOP_DETECT
  1206. base->FLT = 0x40U;
  1207. #endif
  1208. base->RA = 0;
  1209. /* Configure addressing mode. */
  1210. switch (slaveConfig->addressingMode)
  1211. {
  1212. case kI2C_Address7bit:
  1213. base->A1 = ((uint32_t)(slaveConfig->slaveAddress)) << 1U;
  1214. break;
  1215. case kI2C_RangeMatch:
  1216. assert(slaveConfig->slaveAddress < slaveConfig->upperAddress);
  1217. base->A1 = ((uint32_t)(slaveConfig->slaveAddress)) << 1U;
  1218. base->RA = ((uint32_t)(slaveConfig->upperAddress)) << 1U;
  1219. base->C2 |= I2C_C2_RMEN_MASK;
  1220. break;
  1221. default:
  1222. break;
  1223. }
  1224. /* Configure low power wake up feature. */
  1225. tmpReg = base->C1;
  1226. tmpReg &= ~I2C_C1_WUEN_MASK;
  1227. base->C1 = tmpReg | I2C_C1_WUEN(slaveConfig->enableWakeUp) | I2C_C1_IICEN(slaveConfig->enableSlave);
  1228. /* Configure general call & baud rate control. */
  1229. tmpReg = base->C2;
  1230. tmpReg &= ~(I2C_C2_SBRC_MASK | I2C_C2_GCAEN_MASK);
  1231. tmpReg |= I2C_C2_SBRC(slaveConfig->enableBaudRateCtl) | I2C_C2_GCAEN(slaveConfig->enableGeneralCall);
  1232. base->C2 = tmpReg;
  1233. /* Enable/Disable double buffering. */
  1234. #if defined(FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE) && FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE
  1235. tmpReg = base->S2 & (~I2C_S2_DFEN_MASK);
  1236. base->S2 = tmpReg | I2C_S2_DFEN(slaveConfig->enableDoubleBuffering);
  1237. #endif
  1238. /* Set hold time. */
  1239. I2C_SetHoldTime(base, slaveConfig->sclStopHoldTime_ns, srcClock_Hz);
  1240. }
  1241. void I2C_SlaveDeinit(I2C_Type *base)
  1242. {
  1243. /* Disable I2C module. */
  1244. I2C_Enable(base, false);
  1245. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  1246. /* Disable I2C clock. */
  1247. CLOCK_DisableClock(s_i2cClocks[I2C_GetInstance(base)]);
  1248. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  1249. }
  1250. void I2C_SlaveGetDefaultConfig(i2c_slave_config_t *slaveConfig)
  1251. {
  1252. assert(slaveConfig);
  1253. /* By default slave is addressed with 7-bit address. */
  1254. slaveConfig->addressingMode = kI2C_Address7bit;
  1255. /* General call mode is disabled by default. */
  1256. slaveConfig->enableGeneralCall = false;
  1257. /* Slave address match waking up MCU from low power mode is disabled. */
  1258. slaveConfig->enableWakeUp = false;
  1259. /* Independent slave mode baud rate at maximum frequency is disabled. */
  1260. slaveConfig->enableBaudRateCtl = false;
  1261. /* Default enable double buffering. */
  1262. #if defined(FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE) && FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE
  1263. slaveConfig->enableDoubleBuffering = true;
  1264. #endif
  1265. /* Set default SCL stop hold time to 4us which is minimum requirement in I2C spec. */
  1266. slaveConfig->sclStopHoldTime_ns = 4000;
  1267. /* Enable the I2C peripheral. */
  1268. slaveConfig->enableSlave = true;
  1269. }
  1270. status_t I2C_SlaveWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize)
  1271. {
  1272. status_t result = kStatus_Success;
  1273. volatile uint8_t dummy = 0;
  1274. /* Add this to avoid build warning. */
  1275. dummy++;
  1276. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  1277. /* Check start flag. */
  1278. while (!(base->FLT & I2C_FLT_STARTF_MASK))
  1279. {
  1280. }
  1281. /* Clear STARTF flag. */
  1282. base->FLT |= I2C_FLT_STARTF_MASK;
  1283. /* Clear the IICIF flag. */
  1284. base->S = kI2C_IntPendingFlag;
  1285. #endif /* FSL_FEATURE_I2C_HAS_START_STOP_DETECT */
  1286. #if I2C_WAIT_TIMEOUT
  1287. uint32_t waitTimes = I2C_WAIT_TIMEOUT;
  1288. /* Wait until data transfer complete. */
  1289. while ((!(base->S & kI2C_AddressMatchFlag)) && (--waitTimes))
  1290. {
  1291. }
  1292. if (waitTimes == 0)
  1293. {
  1294. return kStatus_I2C_Timeout;
  1295. }
  1296. #else
  1297. /* Wait for address match flag. */
  1298. while (!(base->S & kI2C_AddressMatchFlag))
  1299. {
  1300. }
  1301. #endif
  1302. /* Read dummy to release bus. */
  1303. dummy = base->D;
  1304. result = I2C_MasterWriteBlocking(base, txBuff, txSize, kI2C_TransferDefaultFlag);
  1305. /* Switch to receive mode. */
  1306. base->C1 &= ~(I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
  1307. /* Read dummy to release bus. */
  1308. dummy = base->D;
  1309. return result;
  1310. }
  1311. status_t I2C_SlaveReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize)
  1312. {
  1313. status_t result = kStatus_Success;
  1314. volatile uint8_t dummy = 0;
  1315. /* Add this to avoid build warning. */
  1316. dummy++;
  1317. /* Wait until address match. */
  1318. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  1319. /* Check start flag. */
  1320. while (!(base->FLT & I2C_FLT_STARTF_MASK))
  1321. {
  1322. }
  1323. /* Clear STARTF flag. */
  1324. base->FLT |= I2C_FLT_STARTF_MASK;
  1325. /* Clear the IICIF flag. */
  1326. base->S = kI2C_IntPendingFlag;
  1327. #endif /* FSL_FEATURE_I2C_HAS_START_STOP_DETECT */
  1328. #if I2C_WAIT_TIMEOUT
  1329. uint32_t waitTimes = I2C_WAIT_TIMEOUT;
  1330. /* Wait for address match and int pending flag. */
  1331. while ((!(base->S & kI2C_AddressMatchFlag)) && (--waitTimes))
  1332. {
  1333. }
  1334. if (waitTimes == 0)
  1335. {
  1336. return kStatus_I2C_Timeout;
  1337. }
  1338. waitTimes = I2C_WAIT_TIMEOUT;
  1339. while ((!(base->S & kI2C_IntPendingFlag)) && (--waitTimes))
  1340. {
  1341. }
  1342. if (waitTimes == 0)
  1343. {
  1344. return kStatus_I2C_Timeout;
  1345. }
  1346. #else
  1347. /* Wait for address match and int pending flag. */
  1348. while (!(base->S & kI2C_AddressMatchFlag))
  1349. {
  1350. }
  1351. while (!(base->S & kI2C_IntPendingFlag))
  1352. {
  1353. }
  1354. #endif
  1355. /* Read dummy to release bus. */
  1356. dummy = base->D;
  1357. /* Clear the IICIF flag. */
  1358. base->S = kI2C_IntPendingFlag;
  1359. /* Setup the I2C peripheral to receive data. */
  1360. base->C1 &= ~(I2C_C1_TX_MASK);
  1361. while (rxSize--)
  1362. {
  1363. #if I2C_WAIT_TIMEOUT
  1364. waitTimes = I2C_WAIT_TIMEOUT;
  1365. /* Wait until data transfer complete. */
  1366. while ((!(base->S & kI2C_IntPendingFlag)) && (--waitTimes))
  1367. {
  1368. }
  1369. if (waitTimes == 0)
  1370. {
  1371. return kStatus_I2C_Timeout;
  1372. }
  1373. #else
  1374. /* Wait until data transfer complete. */
  1375. while (!(base->S & kI2C_IntPendingFlag))
  1376. {
  1377. }
  1378. #endif
  1379. /* Clear the IICIF flag. */
  1380. base->S = kI2C_IntPendingFlag;
  1381. /* Read from the data register. */
  1382. *rxBuff++ = base->D;
  1383. }
  1384. return result;
  1385. }
  1386. void I2C_SlaveTransferCreateHandle(I2C_Type *base,
  1387. i2c_slave_handle_t *handle,
  1388. i2c_slave_transfer_callback_t callback,
  1389. void *userData)
  1390. {
  1391. assert(handle);
  1392. uint32_t instance = I2C_GetInstance(base);
  1393. /* Zero handle. */
  1394. memset(handle, 0, sizeof(*handle));
  1395. /* Set callback and userData. */
  1396. handle->callback = callback;
  1397. handle->userData = userData;
  1398. /* Save the context in global variables to support the double weak mechanism. */
  1399. s_i2cHandle[instance] = handle;
  1400. /* Save slave interrupt handler. */
  1401. s_i2cSlaveIsr = I2C_SlaveTransferHandleIRQ;
  1402. /* Enable NVIC interrupt. */
  1403. EnableIRQ(s_i2cIrqs[instance]);
  1404. }
  1405. status_t I2C_SlaveTransferNonBlocking(I2C_Type *base, i2c_slave_handle_t *handle, uint32_t eventMask)
  1406. {
  1407. assert(handle);
  1408. /* Check if the I2C bus is idle - if not return busy status. */
  1409. if (handle->isBusy)
  1410. {
  1411. return kStatus_I2C_Busy;
  1412. }
  1413. else
  1414. {
  1415. /* Disable LPI2C IRQ sources while we configure stuff. */
  1416. I2C_DisableInterrupts(base, kIrqFlags);
  1417. /* Clear transfer in handle. */
  1418. memset(&handle->transfer, 0, sizeof(handle->transfer));
  1419. /* Record that we're busy. */
  1420. handle->isBusy = true;
  1421. /* Set up event mask. tx and rx are always enabled. */
  1422. handle->eventMask = eventMask | kI2C_SlaveTransmitEvent | kI2C_SlaveReceiveEvent | kI2C_SlaveGenaralcallEvent;
  1423. /* Clear all flags. */
  1424. I2C_SlaveClearStatusFlags(base, kClearFlags);
  1425. /* Enable I2C internal IRQ sources. NVIC IRQ was enabled in CreateHandle() */
  1426. I2C_EnableInterrupts(base, kIrqFlags);
  1427. }
  1428. return kStatus_Success;
  1429. }
  1430. void I2C_SlaveTransferAbort(I2C_Type *base, i2c_slave_handle_t *handle)
  1431. {
  1432. assert(handle);
  1433. if (handle->isBusy)
  1434. {
  1435. /* Disable interrupts. */
  1436. I2C_DisableInterrupts(base, kIrqFlags);
  1437. /* Reset transfer info. */
  1438. memset(&handle->transfer, 0, sizeof(handle->transfer));
  1439. /* Reset the state to idle. */
  1440. handle->isBusy = false;
  1441. }
  1442. }
  1443. status_t I2C_SlaveTransferGetCount(I2C_Type *base, i2c_slave_handle_t *handle, size_t *count)
  1444. {
  1445. assert(handle);
  1446. if (!count)
  1447. {
  1448. return kStatus_InvalidArgument;
  1449. }
  1450. /* Catch when there is not an active transfer. */
  1451. if (!handle->isBusy)
  1452. {
  1453. *count = 0;
  1454. return kStatus_NoTransferInProgress;
  1455. }
  1456. /* For an active transfer, just return the count from the handle. */
  1457. *count = handle->transfer.transferredCount;
  1458. return kStatus_Success;
  1459. }
  1460. void I2C_SlaveTransferHandleIRQ(I2C_Type *base, void *i2cHandle)
  1461. {
  1462. assert(i2cHandle);
  1463. uint16_t status;
  1464. bool doTransmit = false;
  1465. i2c_slave_handle_t *handle = (i2c_slave_handle_t *)i2cHandle;
  1466. i2c_slave_transfer_t *xfer;
  1467. volatile uint8_t dummy = 0;
  1468. /* Add this to avoid build warning. */
  1469. dummy++;
  1470. status = I2C_SlaveGetStatusFlags(base);
  1471. xfer = &(handle->transfer);
  1472. #ifdef I2C_HAS_STOP_DETECT
  1473. /* Check stop flag. */
  1474. if (status & kI2C_StopDetectFlag)
  1475. {
  1476. I2C_MasterClearStatusFlags(base, kI2C_StopDetectFlag);
  1477. /* Clear the interrupt flag. */
  1478. base->S = kI2C_IntPendingFlag;
  1479. /* Call slave callback if this is the STOP of the transfer. */
  1480. if (handle->isBusy)
  1481. {
  1482. xfer->event = kI2C_SlaveCompletionEvent;
  1483. xfer->completionStatus = kStatus_Success;
  1484. handle->isBusy = false;
  1485. if ((handle->eventMask & xfer->event) && (handle->callback))
  1486. {
  1487. handle->callback(base, xfer, handle->userData);
  1488. }
  1489. }
  1490. if (!(status & kI2C_AddressMatchFlag))
  1491. {
  1492. return;
  1493. }
  1494. }
  1495. #endif /* I2C_HAS_STOP_DETECT */
  1496. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  1497. /* Check start flag. */
  1498. if (status & kI2C_StartDetectFlag)
  1499. {
  1500. I2C_MasterClearStatusFlags(base, kI2C_StartDetectFlag);
  1501. /* Clear the interrupt flag. */
  1502. base->S = kI2C_IntPendingFlag;
  1503. xfer->event = kI2C_SlaveStartEvent;
  1504. if ((handle->eventMask & xfer->event) && (handle->callback))
  1505. {
  1506. handle->callback(base, xfer, handle->userData);
  1507. }
  1508. if (!(status & kI2C_AddressMatchFlag))
  1509. {
  1510. return;
  1511. }
  1512. }
  1513. #endif /* FSL_FEATURE_I2C_HAS_START_STOP_DETECT */
  1514. /* Clear the interrupt flag. */
  1515. base->S = kI2C_IntPendingFlag;
  1516. /* Check NAK */
  1517. if (status & kI2C_ReceiveNakFlag)
  1518. {
  1519. /* Set receive mode. */
  1520. base->C1 &= ~(I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
  1521. /* Read dummy. */
  1522. dummy = base->D;
  1523. if (handle->transfer.dataSize != 0)
  1524. {
  1525. xfer->event = kI2C_SlaveCompletionEvent;
  1526. xfer->completionStatus = kStatus_I2C_Nak;
  1527. handle->isBusy = false;
  1528. if ((handle->eventMask & xfer->event) && (handle->callback))
  1529. {
  1530. handle->callback(base, xfer, handle->userData);
  1531. }
  1532. }
  1533. else
  1534. {
  1535. #ifndef I2C_HAS_STOP_DETECT
  1536. xfer->event = kI2C_SlaveCompletionEvent;
  1537. xfer->completionStatus = kStatus_Success;
  1538. handle->isBusy = false;
  1539. if ((handle->eventMask & xfer->event) && (handle->callback))
  1540. {
  1541. handle->callback(base, xfer, handle->userData);
  1542. }
  1543. #endif /* !FSL_FEATURE_I2C_HAS_START_STOP_DETECT or !FSL_FEATURE_I2C_HAS_STOP_DETECT */
  1544. }
  1545. }
  1546. /* Check address match. */
  1547. else if (status & kI2C_AddressMatchFlag)
  1548. {
  1549. handle->isBusy = true;
  1550. xfer->event = kI2C_SlaveAddressMatchEvent;
  1551. /* Slave transmit, master reading from slave. */
  1552. if (status & kI2C_TransferDirectionFlag)
  1553. {
  1554. /* Change direction to send data. */
  1555. base->C1 |= I2C_C1_TX_MASK;
  1556. doTransmit = true;
  1557. }
  1558. else
  1559. {
  1560. /* Slave receive, master writing to slave. */
  1561. base->C1 &= ~(I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
  1562. /* Read dummy to release the bus. */
  1563. dummy = base->D;
  1564. if (dummy == 0)
  1565. {
  1566. xfer->event = kI2C_SlaveGenaralcallEvent;
  1567. }
  1568. }
  1569. if ((handle->eventMask & xfer->event) && (handle->callback))
  1570. {
  1571. handle->callback(base, xfer, handle->userData);
  1572. }
  1573. }
  1574. /* Check transfer complete flag. */
  1575. else if (status & kI2C_TransferCompleteFlag)
  1576. {
  1577. /* Slave transmit, master reading from slave. */
  1578. if (status & kI2C_TransferDirectionFlag)
  1579. {
  1580. doTransmit = true;
  1581. }
  1582. else
  1583. {
  1584. /* If we're out of data, invoke callback to get more. */
  1585. if ((!xfer->data) || (!xfer->dataSize))
  1586. {
  1587. xfer->event = kI2C_SlaveReceiveEvent;
  1588. if (handle->callback)
  1589. {
  1590. handle->callback(base, xfer, handle->userData);
  1591. }
  1592. /* Clear the transferred count now that we have a new buffer. */
  1593. xfer->transferredCount = 0;
  1594. }
  1595. /* Slave receive, master writing to slave. */
  1596. uint8_t data = base->D;
  1597. if (handle->transfer.dataSize)
  1598. {
  1599. /* Receive data. */
  1600. *handle->transfer.data++ = data;
  1601. handle->transfer.dataSize--;
  1602. xfer->transferredCount++;
  1603. if (!handle->transfer.dataSize)
  1604. {
  1605. #ifndef I2C_HAS_STOP_DETECT
  1606. xfer->event = kI2C_SlaveCompletionEvent;
  1607. xfer->completionStatus = kStatus_Success;
  1608. handle->isBusy = false;
  1609. /* Proceed receive complete event. */
  1610. if ((handle->eventMask & xfer->event) && (handle->callback))
  1611. {
  1612. handle->callback(base, xfer, handle->userData);
  1613. }
  1614. #endif /* !FSL_FEATURE_I2C_HAS_START_STOP_DETECT or !FSL_FEATURE_I2C_HAS_STOP_DETECT */
  1615. }
  1616. }
  1617. }
  1618. }
  1619. else
  1620. {
  1621. /* Read dummy to release bus. */
  1622. dummy = base->D;
  1623. }
  1624. /* Send data if there is the need. */
  1625. if (doTransmit)
  1626. {
  1627. /* If we're out of data, invoke callback to get more. */
  1628. if ((!xfer->data) || (!xfer->dataSize))
  1629. {
  1630. xfer->event = kI2C_SlaveTransmitEvent;
  1631. if (handle->callback)
  1632. {
  1633. handle->callback(base, xfer, handle->userData);
  1634. }
  1635. /* Clear the transferred count now that we have a new buffer. */
  1636. xfer->transferredCount = 0;
  1637. }
  1638. if (handle->transfer.dataSize)
  1639. {
  1640. /* Send data. */
  1641. base->D = *handle->transfer.data++;
  1642. handle->transfer.dataSize--;
  1643. xfer->transferredCount++;
  1644. }
  1645. else
  1646. {
  1647. /* Switch to receive mode. */
  1648. base->C1 &= ~(I2C_C1_TX_MASK | I2C_C1_TXAK_MASK);
  1649. /* Read dummy to release bus. */
  1650. dummy = base->D;
  1651. #ifndef I2C_HAS_STOP_DETECT
  1652. xfer->event = kI2C_SlaveCompletionEvent;
  1653. xfer->completionStatus = kStatus_Success;
  1654. handle->isBusy = false;
  1655. /* Proceed txdone event. */
  1656. if ((handle->eventMask & xfer->event) && (handle->callback))
  1657. {
  1658. handle->callback(base, xfer, handle->userData);
  1659. }
  1660. #endif /* !FSL_FEATURE_I2C_HAS_START_STOP_DETECT or !FSL_FEATURE_I2C_HAS_STOP_DETECT */
  1661. }
  1662. }
  1663. }
  1664. #if defined(I2C0)
  1665. void I2C0_DriverIRQHandler(void)
  1666. {
  1667. I2C_TransferCommonIRQHandler(I2C0, s_i2cHandle[0]);
  1668. }
  1669. #endif
  1670. #if defined(I2C1)
  1671. void I2C1_DriverIRQHandler(void)
  1672. {
  1673. I2C_TransferCommonIRQHandler(I2C1, s_i2cHandle[1]);
  1674. }
  1675. #endif
  1676. #if defined(I2C2)
  1677. void I2C2_DriverIRQHandler(void)
  1678. {
  1679. I2C_TransferCommonIRQHandler(I2C2, s_i2cHandle[2]);
  1680. }
  1681. #endif
  1682. #if defined(I2C3)
  1683. void I2C3_DriverIRQHandler(void)
  1684. {
  1685. I2C_TransferCommonIRQHandler(I2C3, s_i2cHandle[3]);
  1686. }
  1687. #endif