fsl_flexio_i2c_master.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_flexio_i2c_master.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /*! @brief FLEXIO I2C transfer state */
  35. enum _flexio_i2c_master_transfer_states
  36. {
  37. kFLEXIO_I2C_Idle = 0x0U, /*!< I2C bus idle */
  38. kFLEXIO_I2C_CheckAddress = 0x1U, /*!< 7-bit address check state */
  39. kFLEXIO_I2C_SendCommand = 0x2U, /*!< Send command byte phase */
  40. kFLEXIO_I2C_SendData = 0x3U, /*!< Send data transfer phase*/
  41. kFLEXIO_I2C_ReceiveDataBegin = 0x4U, /*!< Receive data begin transfer phase*/
  42. kFLEXIO_I2C_ReceiveData = 0x5U, /*!< Receive data transfer phase*/
  43. };
  44. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  45. extern const clock_ip_name_t s_flexioClocks[];
  46. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  47. extern FLEXIO_Type *const s_flexioBases[];
  48. /*******************************************************************************
  49. * Prototypes
  50. ******************************************************************************/
  51. extern uint32_t FLEXIO_GetInstance(FLEXIO_Type *base);
  52. /*!
  53. * @brief Set up master transfer, send slave address and decide the initial
  54. * transfer state.
  55. *
  56. * @param base pointer to FLEXIO_I2C_Type structure
  57. * @param handle pointer to flexio_i2c_master_handle_t structure which stores the transfer state
  58. * @param transfer pointer to flexio_i2c_master_transfer_t structure
  59. */
  60. static status_t FLEXIO_I2C_MasterTransferInitStateMachine(FLEXIO_I2C_Type *base,
  61. flexio_i2c_master_handle_t *handle,
  62. flexio_i2c_master_transfer_t *xfer);
  63. /*!
  64. * @brief Master run transfer state machine to perform a byte of transfer.
  65. *
  66. * @param base pointer to FLEXIO_I2C_Type structure
  67. * @param handle pointer to flexio_i2c_master_handle_t structure which stores the transfer state
  68. * @param statusFlags flexio i2c hardware status
  69. * @retval kStatus_Success Successfully run state machine
  70. * @retval kStatus_FLEXIO_I2C_Nak Receive Nak during transfer
  71. */
  72. static status_t FLEXIO_I2C_MasterTransferRunStateMachine(FLEXIO_I2C_Type *base,
  73. flexio_i2c_master_handle_t *handle,
  74. uint32_t statusFlags);
  75. /*!
  76. * @brief Complete transfer, disable interrupt and call callback.
  77. *
  78. * @param base pointer to FLEXIO_I2C_Type structure
  79. * @param handle pointer to flexio_i2c_master_handle_t structure which stores the transfer state
  80. * @param status flexio transfer status
  81. */
  82. static void FLEXIO_I2C_MasterTransferComplete(FLEXIO_I2C_Type *base,
  83. flexio_i2c_master_handle_t *handle,
  84. status_t status);
  85. /*******************************************************************************
  86. * Codes
  87. ******************************************************************************/
  88. uint32_t FLEXIO_I2C_GetInstance(FLEXIO_I2C_Type *base)
  89. {
  90. return FLEXIO_GetInstance(base->flexioBase);
  91. }
  92. static status_t FLEXIO_I2C_MasterTransferInitStateMachine(FLEXIO_I2C_Type *base,
  93. flexio_i2c_master_handle_t *handle,
  94. flexio_i2c_master_transfer_t *xfer)
  95. {
  96. bool needRestart;
  97. uint32_t byteCount;
  98. /* Init the handle member. */
  99. handle->transfer.slaveAddress = xfer->slaveAddress;
  100. handle->transfer.direction = xfer->direction;
  101. handle->transfer.subaddress = xfer->subaddress;
  102. handle->transfer.subaddressSize = xfer->subaddressSize;
  103. handle->transfer.data = xfer->data;
  104. handle->transfer.dataSize = xfer->dataSize;
  105. handle->transfer.flags = xfer->flags;
  106. handle->transferSize = xfer->dataSize;
  107. /* Initial state, i2c check address state. */
  108. handle->state = kFLEXIO_I2C_CheckAddress;
  109. /* Clear all status before transfer. */
  110. FLEXIO_I2C_MasterClearStatusFlags(base, kFLEXIO_I2C_ReceiveNakFlag);
  111. /* Calculate whether need to send re-start. */
  112. needRestart = (handle->transfer.subaddressSize != 0) && (handle->transfer.direction == kFLEXIO_I2C_Read);
  113. /* Calculate total byte count in a frame. */
  114. byteCount = 1;
  115. if (!needRestart)
  116. {
  117. byteCount += handle->transfer.dataSize;
  118. }
  119. if (handle->transfer.subaddressSize != 0)
  120. {
  121. byteCount += handle->transfer.subaddressSize;
  122. /* Next state, send command byte. */
  123. handle->state = kFLEXIO_I2C_SendCommand;
  124. }
  125. /* Configure data count. */
  126. if (FLEXIO_I2C_MasterSetTransferCount(base, byteCount) != kStatus_Success)
  127. {
  128. return kStatus_InvalidArgument;
  129. }
  130. while (!((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->shifterIndex[0]))))
  131. {
  132. }
  133. /* Send address byte first. */
  134. if (needRestart)
  135. {
  136. FLEXIO_I2C_MasterStart(base, handle->transfer.slaveAddress, kFLEXIO_I2C_Write);
  137. }
  138. else
  139. {
  140. FLEXIO_I2C_MasterStart(base, handle->transfer.slaveAddress, handle->transfer.direction);
  141. }
  142. return kStatus_Success;
  143. }
  144. static status_t FLEXIO_I2C_MasterTransferRunStateMachine(FLEXIO_I2C_Type *base,
  145. flexio_i2c_master_handle_t *handle,
  146. uint32_t statusFlags)
  147. {
  148. if (statusFlags & kFLEXIO_I2C_ReceiveNakFlag)
  149. {
  150. /* Clear receive nak flag. */
  151. FLEXIO_ClearShifterErrorFlags(base->flexioBase, 1U << base->shifterIndex[1]);
  152. if ((!((handle->state == kFLEXIO_I2C_SendData) && (handle->transfer.dataSize == 0U))) &&
  153. (!(((handle->state == kFLEXIO_I2C_ReceiveData) || (handle->state == kFLEXIO_I2C_ReceiveDataBegin)) &&
  154. (handle->transfer.dataSize == 1U))))
  155. {
  156. FLEXIO_I2C_MasterReadByte(base);
  157. FLEXIO_I2C_MasterAbortStop(base);
  158. handle->state = kFLEXIO_I2C_Idle;
  159. return kStatus_FLEXIO_I2C_Nak;
  160. }
  161. }
  162. if (handle->state == kFLEXIO_I2C_CheckAddress)
  163. {
  164. if (handle->transfer.direction == kFLEXIO_I2C_Write)
  165. {
  166. /* Next state, send data. */
  167. handle->state = kFLEXIO_I2C_SendData;
  168. }
  169. else
  170. {
  171. /* Next state, receive data begin. */
  172. handle->state = kFLEXIO_I2C_ReceiveDataBegin;
  173. }
  174. }
  175. if ((statusFlags & kFLEXIO_I2C_RxFullFlag) && (handle->state != kFLEXIO_I2C_ReceiveData))
  176. {
  177. FLEXIO_I2C_MasterReadByte(base);
  178. }
  179. switch (handle->state)
  180. {
  181. case kFLEXIO_I2C_SendCommand:
  182. if (statusFlags & kFLEXIO_I2C_TxEmptyFlag)
  183. {
  184. if (handle->transfer.subaddressSize > 0)
  185. {
  186. handle->transfer.subaddressSize--;
  187. FLEXIO_I2C_MasterWriteByte(
  188. base, ((handle->transfer.subaddress) >> (8 * handle->transfer.subaddressSize)));
  189. if (handle->transfer.subaddressSize == 0)
  190. {
  191. /* Load re-start in advance. */
  192. if (handle->transfer.direction == kFLEXIO_I2C_Read)
  193. {
  194. while (!((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->shifterIndex[0]))))
  195. {
  196. }
  197. FLEXIO_I2C_MasterRepeatedStart(base);
  198. }
  199. }
  200. }
  201. else
  202. {
  203. if (handle->transfer.direction == kFLEXIO_I2C_Write)
  204. {
  205. /* Next state, send data. */
  206. handle->state = kFLEXIO_I2C_SendData;
  207. /* Send first byte of data. */
  208. if (handle->transfer.dataSize > 0)
  209. {
  210. FLEXIO_I2C_MasterWriteByte(base, *handle->transfer.data);
  211. handle->transfer.data++;
  212. handle->transfer.dataSize--;
  213. }
  214. }
  215. else
  216. {
  217. FLEXIO_I2C_MasterSetTransferCount(base, (handle->transfer.dataSize + 1));
  218. FLEXIO_I2C_MasterStart(base, handle->transfer.slaveAddress, kFLEXIO_I2C_Read);
  219. /* Next state, receive data begin. */
  220. handle->state = kFLEXIO_I2C_ReceiveDataBegin;
  221. }
  222. }
  223. }
  224. break;
  225. /* Send command byte. */
  226. case kFLEXIO_I2C_SendData:
  227. if (statusFlags & kFLEXIO_I2C_TxEmptyFlag)
  228. {
  229. /* Send one byte of data. */
  230. if (handle->transfer.dataSize > 0)
  231. {
  232. FLEXIO_I2C_MasterWriteByte(base, *handle->transfer.data);
  233. handle->transfer.data++;
  234. handle->transfer.dataSize--;
  235. }
  236. else
  237. {
  238. FLEXIO_I2C_MasterStop(base);
  239. while (!(FLEXIO_I2C_MasterGetStatusFlags(base) & kFLEXIO_I2C_RxFullFlag))
  240. {
  241. }
  242. FLEXIO_I2C_MasterReadByte(base);
  243. handle->state = kFLEXIO_I2C_Idle;
  244. }
  245. }
  246. break;
  247. case kFLEXIO_I2C_ReceiveDataBegin:
  248. if (statusFlags & kFLEXIO_I2C_RxFullFlag)
  249. {
  250. handle->state = kFLEXIO_I2C_ReceiveData;
  251. /* Send nak at the last receive byte. */
  252. if (handle->transfer.dataSize == 1)
  253. {
  254. FLEXIO_I2C_MasterEnableAck(base, false);
  255. while (!((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->shifterIndex[0]))))
  256. {
  257. }
  258. FLEXIO_I2C_MasterStop(base);
  259. }
  260. else
  261. {
  262. FLEXIO_I2C_MasterEnableAck(base, true);
  263. }
  264. }
  265. else if (statusFlags & kFLEXIO_I2C_TxEmptyFlag)
  266. {
  267. /* Read one byte of data. */
  268. FLEXIO_I2C_MasterWriteByte(base, 0xFFFFFFFFU);
  269. }
  270. else
  271. {
  272. }
  273. break;
  274. case kFLEXIO_I2C_ReceiveData:
  275. if (statusFlags & kFLEXIO_I2C_RxFullFlag)
  276. {
  277. *handle->transfer.data = FLEXIO_I2C_MasterReadByte(base);
  278. handle->transfer.data++;
  279. if (handle->transfer.dataSize--)
  280. {
  281. if (handle->transfer.dataSize == 0)
  282. {
  283. FLEXIO_I2C_MasterDisableInterrupts(base, kFLEXIO_I2C_RxFullInterruptEnable);
  284. handle->state = kFLEXIO_I2C_Idle;
  285. }
  286. /* Send nak at the last receive byte. */
  287. if (handle->transfer.dataSize == 1)
  288. {
  289. FLEXIO_I2C_MasterEnableAck(base, false);
  290. while (!((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->shifterIndex[0]))))
  291. {
  292. }
  293. FLEXIO_I2C_MasterStop(base);
  294. }
  295. }
  296. }
  297. else if (statusFlags & kFLEXIO_I2C_TxEmptyFlag)
  298. {
  299. if (handle->transfer.dataSize > 1)
  300. {
  301. FLEXIO_I2C_MasterWriteByte(base, 0xFFFFFFFFU);
  302. }
  303. }
  304. else
  305. {
  306. }
  307. break;
  308. default:
  309. break;
  310. }
  311. return kStatus_Success;
  312. }
  313. static void FLEXIO_I2C_MasterTransferComplete(FLEXIO_I2C_Type *base,
  314. flexio_i2c_master_handle_t *handle,
  315. status_t status)
  316. {
  317. FLEXIO_I2C_MasterDisableInterrupts(base, kFLEXIO_I2C_TxEmptyInterruptEnable | kFLEXIO_I2C_RxFullInterruptEnable);
  318. if (handle->completionCallback)
  319. {
  320. handle->completionCallback(base, handle, status, handle->userData);
  321. }
  322. }
  323. status_t FLEXIO_I2C_MasterInit(FLEXIO_I2C_Type *base, flexio_i2c_master_config_t *masterConfig, uint32_t srcClock_Hz)
  324. {
  325. assert(base && masterConfig);
  326. flexio_shifter_config_t shifterConfig;
  327. flexio_timer_config_t timerConfig;
  328. uint32_t controlVal = 0;
  329. uint16_t timerDiv = 0;
  330. status_t result = kStatus_Success;
  331. memset(&shifterConfig, 0, sizeof(shifterConfig));
  332. memset(&timerConfig, 0, sizeof(timerConfig));
  333. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  334. /* Ungate flexio clock. */
  335. CLOCK_EnableClock(s_flexioClocks[FLEXIO_I2C_GetInstance(base)]);
  336. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  337. /* Do hardware configuration. */
  338. /* 1. Configure the shifter 0 for tx. */
  339. shifterConfig.timerSelect = base->timerIndex[1];
  340. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnPositive;
  341. shifterConfig.pinConfig = kFLEXIO_PinConfigOpenDrainOrBidirection;
  342. shifterConfig.pinSelect = base->SDAPinIndex;
  343. shifterConfig.pinPolarity = kFLEXIO_PinActiveLow;
  344. shifterConfig.shifterMode = kFLEXIO_ShifterModeTransmit;
  345. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  346. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitHigh;
  347. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitLow;
  348. FLEXIO_SetShifterConfig(base->flexioBase, base->shifterIndex[0], &shifterConfig);
  349. /* 2. Configure the shifter 1 for rx. */
  350. shifterConfig.timerSelect = base->timerIndex[1];
  351. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnNegitive;
  352. shifterConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  353. shifterConfig.pinSelect = base->SDAPinIndex;
  354. shifterConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  355. shifterConfig.shifterMode = kFLEXIO_ShifterModeReceive;
  356. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  357. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitLow;
  358. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
  359. FLEXIO_SetShifterConfig(base->flexioBase, base->shifterIndex[1], &shifterConfig);
  360. /*3. Configure the timer 0 for generating bit clock. */
  361. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_SHIFTnSTAT(base->shifterIndex[0]);
  362. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveLow;
  363. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  364. timerConfig.pinConfig = kFLEXIO_PinConfigOpenDrainOrBidirection;
  365. timerConfig.pinSelect = base->SCLPinIndex;
  366. timerConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  367. timerConfig.timerMode = kFLEXIO_TimerModeDual8BitBaudBit;
  368. timerConfig.timerOutput = kFLEXIO_TimerOutputZeroNotAffectedByReset;
  369. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
  370. timerConfig.timerReset = kFLEXIO_TimerResetOnTimerPinEqualToTimerOutput;
  371. timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompare;
  372. timerConfig.timerEnable = kFLEXIO_TimerEnableOnTriggerHigh;
  373. timerConfig.timerStop = kFLEXIO_TimerStopBitEnableOnTimerDisable;
  374. timerConfig.timerStart = kFLEXIO_TimerStartBitEnabled;
  375. /* Set TIMCMP[7:0] = (baud rate divider / 2) - 1. */
  376. timerDiv = (srcClock_Hz / masterConfig->baudRate_Bps) / 2 - 1;
  377. if (timerDiv > 0xFFU)
  378. {
  379. result = kStatus_InvalidArgument;
  380. return result;
  381. }
  382. timerConfig.timerCompare = timerDiv;
  383. FLEXIO_SetTimerConfig(base->flexioBase, base->timerIndex[0], &timerConfig);
  384. /* 4. Configure the timer 1 for controlling shifters. */
  385. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_SHIFTnSTAT(base->shifterIndex[0]);
  386. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveLow;
  387. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  388. timerConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  389. timerConfig.pinSelect = base->SCLPinIndex;
  390. timerConfig.pinPolarity = kFLEXIO_PinActiveLow;
  391. timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
  392. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  393. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnPinInputShiftPinInput;
  394. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  395. timerConfig.timerDisable = kFLEXIO_TimerDisableOnPreTimerDisable;
  396. timerConfig.timerEnable = kFLEXIO_TimerEnableOnPrevTimerEnable;
  397. timerConfig.timerStop = kFLEXIO_TimerStopBitEnableOnTimerCompare;
  398. timerConfig.timerStart = kFLEXIO_TimerStartBitEnabled;
  399. /* Set TIMCMP[15:0] = (number of bits x 2) - 1. */
  400. timerConfig.timerCompare = 8 * 2 - 1;
  401. FLEXIO_SetTimerConfig(base->flexioBase, base->timerIndex[1], &timerConfig);
  402. /* Configure FLEXIO I2C Master. */
  403. controlVal = base->flexioBase->CTRL;
  404. controlVal &=
  405. ~(FLEXIO_CTRL_DOZEN_MASK | FLEXIO_CTRL_DBGE_MASK | FLEXIO_CTRL_FASTACC_MASK | FLEXIO_CTRL_FLEXEN_MASK);
  406. controlVal |= (FLEXIO_CTRL_DBGE(masterConfig->enableInDebug) | FLEXIO_CTRL_FASTACC(masterConfig->enableFastAccess) |
  407. FLEXIO_CTRL_FLEXEN(masterConfig->enableMaster));
  408. if (!masterConfig->enableInDoze)
  409. {
  410. controlVal |= FLEXIO_CTRL_DOZEN_MASK;
  411. }
  412. base->flexioBase->CTRL = controlVal;
  413. return result;
  414. }
  415. void FLEXIO_I2C_MasterDeinit(FLEXIO_I2C_Type *base)
  416. {
  417. base->flexioBase->SHIFTCFG[base->shifterIndex[0]] = 0;
  418. base->flexioBase->SHIFTCTL[base->shifterIndex[0]] = 0;
  419. base->flexioBase->SHIFTCFG[base->shifterIndex[1]] = 0;
  420. base->flexioBase->SHIFTCTL[base->shifterIndex[1]] = 0;
  421. base->flexioBase->TIMCFG[base->timerIndex[0]] = 0;
  422. base->flexioBase->TIMCMP[base->timerIndex[0]] = 0;
  423. base->flexioBase->TIMCTL[base->timerIndex[0]] = 0;
  424. base->flexioBase->TIMCFG[base->timerIndex[1]] = 0;
  425. base->flexioBase->TIMCMP[base->timerIndex[1]] = 0;
  426. base->flexioBase->TIMCTL[base->timerIndex[1]] = 0;
  427. /* Clear the shifter flag. */
  428. base->flexioBase->SHIFTSTAT = (1U << base->shifterIndex[0]);
  429. base->flexioBase->SHIFTSTAT = (1U << base->shifterIndex[1]);
  430. /* Clear the timer flag. */
  431. base->flexioBase->TIMSTAT = (1U << base->timerIndex[0]);
  432. base->flexioBase->TIMSTAT = (1U << base->timerIndex[1]);
  433. }
  434. void FLEXIO_I2C_MasterGetDefaultConfig(flexio_i2c_master_config_t *masterConfig)
  435. {
  436. assert(masterConfig);
  437. masterConfig->enableMaster = true;
  438. masterConfig->enableInDoze = false;
  439. masterConfig->enableInDebug = true;
  440. masterConfig->enableFastAccess = false;
  441. /* Default baud rate at 100kbps. */
  442. masterConfig->baudRate_Bps = 100000U;
  443. }
  444. uint32_t FLEXIO_I2C_MasterGetStatusFlags(FLEXIO_I2C_Type *base)
  445. {
  446. uint32_t status = 0;
  447. status =
  448. ((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->shifterIndex[0])) >> base->shifterIndex[0]);
  449. status |=
  450. (((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->shifterIndex[1])) >> (base->shifterIndex[1]))
  451. << 1U);
  452. status |=
  453. (((FLEXIO_GetShifterErrorFlags(base->flexioBase) & (1U << base->shifterIndex[1])) >> (base->shifterIndex[1]))
  454. << 2U);
  455. return status;
  456. }
  457. void FLEXIO_I2C_MasterClearStatusFlags(FLEXIO_I2C_Type *base, uint32_t mask)
  458. {
  459. if (mask & kFLEXIO_I2C_TxEmptyFlag)
  460. {
  461. FLEXIO_ClearShifterStatusFlags(base->flexioBase, 1U << base->shifterIndex[0]);
  462. }
  463. if (mask & kFLEXIO_I2C_RxFullFlag)
  464. {
  465. FLEXIO_ClearShifterStatusFlags(base->flexioBase, 1U << base->shifterIndex[1]);
  466. }
  467. if (mask & kFLEXIO_I2C_ReceiveNakFlag)
  468. {
  469. FLEXIO_ClearShifterErrorFlags(base->flexioBase, 1U << base->shifterIndex[1]);
  470. }
  471. }
  472. void FLEXIO_I2C_MasterEnableInterrupts(FLEXIO_I2C_Type *base, uint32_t mask)
  473. {
  474. if (mask & kFLEXIO_I2C_TxEmptyInterruptEnable)
  475. {
  476. FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1U << base->shifterIndex[0]);
  477. }
  478. if (mask & kFLEXIO_I2C_RxFullInterruptEnable)
  479. {
  480. FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1U << base->shifterIndex[1]);
  481. }
  482. }
  483. void FLEXIO_I2C_MasterDisableInterrupts(FLEXIO_I2C_Type *base, uint32_t mask)
  484. {
  485. if (mask & kFLEXIO_I2C_TxEmptyInterruptEnable)
  486. {
  487. FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1U << base->shifterIndex[0]);
  488. }
  489. if (mask & kFLEXIO_I2C_RxFullInterruptEnable)
  490. {
  491. FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1U << base->shifterIndex[1]);
  492. }
  493. }
  494. void FLEXIO_I2C_MasterSetBaudRate(FLEXIO_I2C_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
  495. {
  496. uint16_t timerDiv = 0;
  497. uint16_t timerCmp = 0;
  498. FLEXIO_Type *flexioBase = base->flexioBase;
  499. /* Set TIMCMP[7:0] = (baud rate divider / 2) - 1.*/
  500. timerDiv = srcClock_Hz / baudRate_Bps;
  501. timerDiv = timerDiv / 2 - 1U;
  502. timerCmp = flexioBase->TIMCMP[base->timerIndex[0]];
  503. timerCmp &= 0xFF00;
  504. timerCmp |= timerDiv;
  505. flexioBase->TIMCMP[base->timerIndex[0]] = timerCmp;
  506. }
  507. status_t FLEXIO_I2C_MasterSetTransferCount(FLEXIO_I2C_Type *base, uint8_t count)
  508. {
  509. if (count > 14U)
  510. {
  511. return kStatus_InvalidArgument;
  512. }
  513. uint16_t timerCmp = 0;
  514. uint32_t timerConfig = 0;
  515. FLEXIO_Type *flexioBase = base->flexioBase;
  516. timerCmp = flexioBase->TIMCMP[base->timerIndex[0]];
  517. timerCmp &= 0x00FFU;
  518. timerCmp |= (count * 18 + 1U) << 8U;
  519. flexioBase->TIMCMP[base->timerIndex[0]] = timerCmp;
  520. timerConfig = flexioBase->TIMCFG[base->timerIndex[0]];
  521. timerConfig &= ~FLEXIO_TIMCFG_TIMDIS_MASK;
  522. timerConfig |= FLEXIO_TIMCFG_TIMDIS(kFLEXIO_TimerDisableOnTimerCompare);
  523. flexioBase->TIMCFG[base->timerIndex[0]] = timerConfig;
  524. return kStatus_Success;
  525. }
  526. void FLEXIO_I2C_MasterStart(FLEXIO_I2C_Type *base, uint8_t address, flexio_i2c_direction_t direction)
  527. {
  528. uint32_t data;
  529. data = ((uint32_t)address) << 1U | ((direction == kFLEXIO_I2C_Read) ? 1U : 0U);
  530. FLEXIO_I2C_MasterWriteByte(base, data);
  531. }
  532. void FLEXIO_I2C_MasterRepeatedStart(FLEXIO_I2C_Type *base)
  533. {
  534. /* Prepare for RESTART condition, no stop.*/
  535. FLEXIO_I2C_MasterWriteByte(base, 0xFFFFFFFFU);
  536. }
  537. void FLEXIO_I2C_MasterStop(FLEXIO_I2C_Type *base)
  538. {
  539. /* Prepare normal stop. */
  540. FLEXIO_I2C_MasterSetTransferCount(base, 0x0U);
  541. FLEXIO_I2C_MasterWriteByte(base, 0x0U);
  542. }
  543. void FLEXIO_I2C_MasterAbortStop(FLEXIO_I2C_Type *base)
  544. {
  545. uint32_t tmpConfig;
  546. /* Prepare abort stop. */
  547. tmpConfig = base->flexioBase->TIMCFG[base->timerIndex[0]];
  548. tmpConfig &= ~FLEXIO_TIMCFG_TIMDIS_MASK;
  549. tmpConfig |= FLEXIO_TIMCFG_TIMDIS(kFLEXIO_TimerDisableOnPinBothEdge);
  550. base->flexioBase->TIMCFG[base->timerIndex[0]] = tmpConfig;
  551. }
  552. void FLEXIO_I2C_MasterEnableAck(FLEXIO_I2C_Type *base, bool enable)
  553. {
  554. uint32_t tmpConfig = 0;
  555. tmpConfig = base->flexioBase->SHIFTCFG[base->shifterIndex[0]];
  556. tmpConfig &= ~FLEXIO_SHIFTCFG_SSTOP_MASK;
  557. if (enable)
  558. {
  559. tmpConfig |= FLEXIO_SHIFTCFG_SSTOP(kFLEXIO_ShifterStopBitLow);
  560. }
  561. else
  562. {
  563. tmpConfig |= FLEXIO_SHIFTCFG_SSTOP(kFLEXIO_ShifterStopBitHigh);
  564. }
  565. base->flexioBase->SHIFTCFG[base->shifterIndex[0]] = tmpConfig;
  566. }
  567. status_t FLEXIO_I2C_MasterWriteBlocking(FLEXIO_I2C_Type *base, const uint8_t *txBuff, uint8_t txSize)
  568. {
  569. assert(txBuff);
  570. assert(txSize);
  571. uint32_t status;
  572. while (txSize--)
  573. {
  574. FLEXIO_I2C_MasterWriteByte(base, *txBuff++);
  575. /* Wait until data transfer complete. */
  576. while (!((status = FLEXIO_I2C_MasterGetStatusFlags(base)) & kFLEXIO_I2C_RxFullFlag))
  577. {
  578. }
  579. if (status & kFLEXIO_I2C_ReceiveNakFlag)
  580. {
  581. FLEXIO_ClearShifterErrorFlags(base->flexioBase, 1U << base->shifterIndex[1]);
  582. return kStatus_FLEXIO_I2C_Nak;
  583. }
  584. }
  585. return kStatus_Success;
  586. }
  587. void FLEXIO_I2C_MasterReadBlocking(FLEXIO_I2C_Type *base, uint8_t *rxBuff, uint8_t rxSize)
  588. {
  589. assert(rxBuff);
  590. assert(rxSize);
  591. while (rxSize--)
  592. {
  593. /* Wait until data transfer complete. */
  594. while (!(FLEXIO_I2C_MasterGetStatusFlags(base) & kFLEXIO_I2C_RxFullFlag))
  595. {
  596. }
  597. *rxBuff++ = FLEXIO_I2C_MasterReadByte(base);
  598. }
  599. }
  600. status_t FLEXIO_I2C_MasterTransferBlocking(FLEXIO_I2C_Type *base, flexio_i2c_master_transfer_t *xfer)
  601. {
  602. assert(xfer);
  603. flexio_i2c_master_handle_t tmpHandle;
  604. uint32_t statusFlags;
  605. uint32_t result = kStatus_Success;
  606. /* Zero the handle. */
  607. memset(&tmpHandle, 0, sizeof(tmpHandle));
  608. /* Set up transfer machine. */
  609. FLEXIO_I2C_MasterTransferInitStateMachine(base, &tmpHandle, xfer);
  610. do
  611. {
  612. /* Wait either tx empty or rx full flag is asserted. */
  613. while (!((statusFlags = FLEXIO_I2C_MasterGetStatusFlags(base)) &
  614. (kFLEXIO_I2C_TxEmptyFlag | kFLEXIO_I2C_RxFullFlag)))
  615. {
  616. }
  617. result = FLEXIO_I2C_MasterTransferRunStateMachine(base, &tmpHandle, statusFlags);
  618. } while ((tmpHandle.state != kFLEXIO_I2C_Idle) && (result == kStatus_Success));
  619. return result;
  620. }
  621. status_t FLEXIO_I2C_MasterTransferCreateHandle(FLEXIO_I2C_Type *base,
  622. flexio_i2c_master_handle_t *handle,
  623. flexio_i2c_master_transfer_callback_t callback,
  624. void *userData)
  625. {
  626. assert(handle);
  627. IRQn_Type flexio_irqs[] = FLEXIO_IRQS;
  628. /* Zero the handle. */
  629. memset(handle, 0, sizeof(*handle));
  630. /* Register callback and userData. */
  631. handle->completionCallback = callback;
  632. handle->userData = userData;
  633. /* Enable interrupt in NVIC. */
  634. EnableIRQ(flexio_irqs[FLEXIO_I2C_GetInstance(base)]);
  635. /* Save the context in global variables to support the double weak mechanism. */
  636. return FLEXIO_RegisterHandleIRQ(base, handle, FLEXIO_I2C_MasterTransferHandleIRQ);
  637. }
  638. status_t FLEXIO_I2C_MasterTransferNonBlocking(FLEXIO_I2C_Type *base,
  639. flexio_i2c_master_handle_t *handle,
  640. flexio_i2c_master_transfer_t *xfer)
  641. {
  642. assert(handle);
  643. assert(xfer);
  644. if (handle->state != kFLEXIO_I2C_Idle)
  645. {
  646. return kStatus_FLEXIO_I2C_Busy;
  647. }
  648. else
  649. {
  650. /* Set up transfer machine. */
  651. FLEXIO_I2C_MasterTransferInitStateMachine(base, handle, xfer);
  652. /* Enable both tx empty and rxfull interrupt. */
  653. FLEXIO_I2C_MasterEnableInterrupts(base, kFLEXIO_I2C_TxEmptyInterruptEnable | kFLEXIO_I2C_RxFullInterruptEnable);
  654. return kStatus_Success;
  655. }
  656. }
  657. void FLEXIO_I2C_MasterTransferAbort(FLEXIO_I2C_Type *base, flexio_i2c_master_handle_t *handle)
  658. {
  659. assert(handle);
  660. /* Disable interrupts. */
  661. FLEXIO_I2C_MasterDisableInterrupts(base, kFLEXIO_I2C_TxEmptyInterruptEnable | kFLEXIO_I2C_RxFullInterruptEnable);
  662. /* Reset to idle state. */
  663. handle->state = kFLEXIO_I2C_Idle;
  664. }
  665. status_t FLEXIO_I2C_MasterTransferGetCount(FLEXIO_I2C_Type *base, flexio_i2c_master_handle_t *handle, size_t *count)
  666. {
  667. if (!count)
  668. {
  669. return kStatus_InvalidArgument;
  670. }
  671. *count = handle->transferSize - handle->transfer.dataSize;
  672. return kStatus_Success;
  673. }
  674. void FLEXIO_I2C_MasterTransferHandleIRQ(void *i2cType, void *i2cHandle)
  675. {
  676. FLEXIO_I2C_Type *base = (FLEXIO_I2C_Type *)i2cType;
  677. flexio_i2c_master_handle_t *handle = (flexio_i2c_master_handle_t *)i2cHandle;
  678. uint32_t statusFlags;
  679. status_t result;
  680. statusFlags = FLEXIO_I2C_MasterGetStatusFlags(base);
  681. result = FLEXIO_I2C_MasterTransferRunStateMachine(base, handle, statusFlags);
  682. if (handle->state == kFLEXIO_I2C_Idle)
  683. {
  684. FLEXIO_I2C_MasterTransferComplete(base, handle, result);
  685. }
  686. }