fsl_i2c.c 58 KB

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