fsl_i2c.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_I2C_H_
  31. #define _FSL_I2C_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup i2c_driver
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief I2C driver version 2.0.0. */
  43. #define FSL_I2C_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  44. /*@}*/
  45. /*! @brief I2C status return codes. */
  46. enum _i2c_status
  47. {
  48. kStatus_I2C_Busy = MAKE_STATUS(kStatusGroup_I2C, 0), /*!< I2C is busy with current transfer. */
  49. kStatus_I2C_Idle = MAKE_STATUS(kStatusGroup_I2C, 1), /*!< Bus is Idle. */
  50. kStatus_I2C_Nak = MAKE_STATUS(kStatusGroup_I2C, 2), /*!< NAK received during transfer. */
  51. kStatus_I2C_ArbitrationLost = MAKE_STATUS(kStatusGroup_I2C, 3), /*!< Arbitration lost during transfer. */
  52. kStatus_I2C_Timeout = MAKE_STATUS(kStatusGroup_I2C, 4), /*!< Wait event timeout. */
  53. kStatus_I2C_Addr_Nak = MAKE_STATUS(kStatusGroup_I2C, 5), /*!< NAK received during the address probe. */
  54. };
  55. /*!
  56. * @brief I2C peripheral flags
  57. *
  58. * The following status register flags can be cleared:
  59. * - #kI2C_ArbitrationLostFlag
  60. * - #kI2C_IntPendingFlag
  61. *
  62. * @note These enumerations are meant to be OR'd together to form a bit mask.
  63. *
  64. */
  65. enum _i2c_flags
  66. {
  67. kI2C_ReceiveNakFlag = I2C_I2SR_RXAK_MASK, /*!< I2C receive NAK flag. */
  68. kI2C_IntPendingFlag = I2C_I2SR_IIF_MASK, /*!< I2C interrupt pending flag. */
  69. kI2C_TransferDirectionFlag = I2C_I2SR_SRW_MASK, /*!< I2C transfer direction flag. */
  70. kI2C_ArbitrationLostFlag = I2C_I2SR_IAL_MASK, /*!< I2C arbitration lost flag. */
  71. kI2C_BusBusyFlag = I2C_I2SR_IBB_MASK, /*!< I2C bus busy flag. */
  72. kI2C_AddressMatchFlag = I2C_I2SR_IAAS_MASK, /*!< I2C address match flag. */
  73. kI2C_TransferCompleteFlag = I2C_I2SR_ICF_MASK,
  74. /*!< I2C transfer complete flag. */
  75. };
  76. /*! @brief I2C feature interrupt source. */
  77. enum _i2c_interrupt_enable
  78. {
  79. kI2C_GlobalInterruptEnable = I2C_I2CR_IIEN_MASK, /*!< I2C global interrupt. */
  80. };
  81. /*! @brief The direction of master and slave transfers. */
  82. typedef enum _i2c_direction
  83. {
  84. kI2C_Write = 0x0U, /*!< Master transmits to the slave. */
  85. kI2C_Read = 0x1U, /*!< Master receives from the slave. */
  86. } i2c_direction_t;
  87. /*! @brief I2C transfer control flag. */
  88. enum _i2c_master_transfer_flags
  89. {
  90. kI2C_TransferDefaultFlag = 0x0U, /*!< A transfer starts with a start signal, stops with a stop signal. */
  91. kI2C_TransferNoStartFlag = 0x1U, /*!< A transfer starts without a start signal. */
  92. kI2C_TransferRepeatedStartFlag = 0x2U, /*!< A transfer starts with a repeated start signal. */
  93. kI2C_TransferNoStopFlag = 0x4U, /*!< A transfer ends without a stop signal. */
  94. };
  95. /*! @brief I2C master user configuration. */
  96. typedef struct _i2c_master_config
  97. {
  98. bool enableMaster; /*!< Enables the I2C peripheral at initialization time. */
  99. uint32_t baudRate_Bps; /*!< Baud rate configuration of I2C peripheral. */
  100. } i2c_master_config_t;
  101. /*! @brief I2C master handle typedef. */
  102. typedef struct _i2c_master_handle i2c_master_handle_t;
  103. /*! @brief I2C master transfer callback typedef. */
  104. typedef void (*i2c_master_transfer_callback_t)(I2C_Type *base,
  105. i2c_master_handle_t *handle,
  106. status_t status,
  107. void *userData);
  108. /*! @brief I2C master transfer structure. */
  109. typedef struct _i2c_master_transfer
  110. {
  111. uint32_t flags; /*!< A transfer flag which controls the transfer. */
  112. uint8_t slaveAddress; /*!< 7-bit slave address. */
  113. i2c_direction_t direction; /*!< A transfer direction, read or write. */
  114. uint32_t subaddress; /*!< A sub address. Transferred MSB first. */
  115. uint8_t subaddressSize; /*!< A size of the command buffer. */
  116. uint8_t *volatile data; /*!< A transfer buffer. */
  117. volatile size_t dataSize; /*!< A transfer size. */
  118. } i2c_master_transfer_t;
  119. /*! @brief I2C master handle structure. */
  120. struct _i2c_master_handle
  121. {
  122. i2c_master_transfer_t transfer; /*!< I2C master transfer copy. */
  123. size_t transferSize; /*!< Total bytes to be transferred. */
  124. uint8_t state; /*!< A transfer state maintained during transfer. */
  125. i2c_master_transfer_callback_t completionCallback; /*!< A callback function called when the transfer is finished. */
  126. void *userData; /*!< A callback parameter passed to the callback function. */
  127. };
  128. /*!
  129. * @brief Set of events sent to the callback for nonblocking slave transfers.
  130. *
  131. * These event enumerations are used for two related purposes. First, a bit mask created by OR'ing together
  132. * events is passed to I2C_SlaveTransferNonBlocking() to specify which events to enable.
  133. * Then, when the slave callback is invoked, it is passed the current event through its @a transfer
  134. * parameter.
  135. *
  136. * @note These enumerations are meant to be OR'd together to form a bit mask of events.
  137. */
  138. typedef enum _i2c_slave_transfer_event
  139. {
  140. kI2C_SlaveAddressMatchEvent = 0x01U, /*!< Received the slave address after a start or repeated start. */
  141. kI2C_SlaveTransmitEvent = 0x02U, /*!< A callback is requested to provide data to transmit
  142. (slave-transmitter role). */
  143. kI2C_SlaveReceiveEvent = 0x04U, /*!< A callback is requested to provide a buffer in which to place received
  144. data (slave-receiver role). */
  145. kI2C_SlaveTransmitAckEvent = 0x08U, /*!< A callback needs to either transmit an ACK or NACK. */
  146. kI2C_SlaveCompletionEvent = 0x20U, /*!< A stop was detected or finished transfer, completing the transfer. */
  147. /*! A bit mask of all available events. */
  148. kI2C_SlaveAllEvents =
  149. kI2C_SlaveAddressMatchEvent | kI2C_SlaveTransmitEvent | kI2C_SlaveReceiveEvent | kI2C_SlaveCompletionEvent,
  150. } i2c_slave_transfer_event_t;
  151. /*! @brief I2C slave handle typedef. */
  152. typedef struct _i2c_slave_handle i2c_slave_handle_t;
  153. /*! @brief I2C slave user configuration. */
  154. typedef struct _i2c_slave_config
  155. {
  156. bool enableSlave; /*!< Enables the I2C peripheral at initialization time. */
  157. uint16_t slaveAddress; /*!< A slave address configuration. */
  158. } i2c_slave_config_t;
  159. /*! @brief I2C slave transfer structure. */
  160. typedef struct _i2c_slave_transfer
  161. {
  162. i2c_slave_transfer_event_t event; /*!< A reason that the callback is invoked. */
  163. uint8_t *volatile data; /*!< A transfer buffer. */
  164. volatile size_t dataSize; /*!< A transfer size. */
  165. status_t completionStatus; /*!< Success or error code describing how the transfer completed. Only applies for
  166. #kI2C_SlaveCompletionEvent. */
  167. size_t transferredCount; /*!< A number of bytes actually transferred since the start or since the last repeated
  168. start. */
  169. } i2c_slave_transfer_t;
  170. /*! @brief I2C slave transfer callback typedef. */
  171. typedef void (*i2c_slave_transfer_callback_t)(I2C_Type *base, i2c_slave_transfer_t *xfer, void *userData);
  172. /*! @brief I2C slave handle structure. */
  173. struct _i2c_slave_handle
  174. {
  175. volatile uint8_t state; /*!< A transfer state maintained during transfer. */
  176. i2c_slave_transfer_t transfer; /*!< I2C slave transfer copy. */
  177. uint32_t eventMask; /*!< A mask of enabled events. */
  178. i2c_slave_transfer_callback_t callback; /*!< A callback function called at the transfer event. */
  179. void *userData; /*!< A callback parameter passed to the callback. */
  180. };
  181. /*******************************************************************************
  182. * API
  183. ******************************************************************************/
  184. #if defined(__cplusplus)
  185. extern "C" {
  186. #endif /*_cplusplus. */
  187. /*!
  188. * @name Initialization and deinitialization
  189. * @{
  190. */
  191. /*!
  192. * @brief Initializes the I2C peripheral. Call this API to ungate the I2C clock
  193. * and configure the I2C with master configuration.
  194. *
  195. * @note This API should be called at the beginning of the application.
  196. * Otherwise, any operation to the I2C module can cause a hard fault
  197. * because the clock is not enabled. The configuration structure can be custom filled
  198. * or it can be set with default values by using the I2C_MasterGetDefaultConfig().
  199. * After calling this API, the master is ready to transfer.
  200. * This is an example.
  201. * @code
  202. * i2c_master_config_t config = {
  203. * .enableMaster = true,
  204. * .baudRate_Bps = 100000
  205. * };
  206. * I2C_MasterInit(I2C0, &config, 12000000U);
  207. * @endcode
  208. *
  209. * @param base I2C base pointer
  210. * @param masterConfig A pointer to the master configuration structure
  211. * @param srcClock_Hz I2C peripheral clock frequency in Hz
  212. */
  213. void I2C_MasterInit(I2C_Type *base, const i2c_master_config_t *masterConfig, uint32_t srcClock_Hz);
  214. /*!
  215. * @brief De-initializes the I2C master peripheral. Call this API to gate the I2C clock.
  216. * The I2C master module can't work unless the I2C_MasterInit is called.
  217. * @param base I2C base pointer
  218. */
  219. void I2C_MasterDeinit(I2C_Type *base);
  220. /*!
  221. * @brief Sets the I2C master configuration structure to default values.
  222. *
  223. * The purpose of this API is to get the configuration structure initialized for use in the I2C_MasterInit().
  224. * Use the initialized structure unchanged in the I2C_MasterInit() or modify
  225. * the structure before calling the I2C_MasterInit().
  226. * This is an example.
  227. * @code
  228. * i2c_master_config_t config;
  229. * I2C_MasterGetDefaultConfig(&config);
  230. * @endcode
  231. * @param masterConfig A pointer to the master configuration structure.
  232. */
  233. void I2C_MasterGetDefaultConfig(i2c_master_config_t *masterConfig);
  234. /*!
  235. * @brief Initializes the I2C peripheral. Call this API to ungate the I2C clock
  236. * and initialize the I2C with the slave configuration.
  237. *
  238. * @note This API should be called at the beginning of the application.
  239. * Otherwise, any operation to the I2C module can cause a hard fault
  240. * because the clock is not enabled. The configuration structure can partly be set
  241. * with default values by I2C_SlaveGetDefaultConfig() or it can be custom filled by the user.
  242. * This is an example.
  243. * @code
  244. * i2c_slave_config_t config = {
  245. * .enableSlave = true,
  246. * .slaveAddress = 0x1DU,
  247. * };
  248. * I2C_SlaveInit(I2C0, &config);
  249. * @endcode
  250. *
  251. * @param base I2C base pointer
  252. * @param slaveConfig A pointer to the slave configuration structure
  253. * @param srcClock_Hz I2C peripheral clock frequency in Hz
  254. */
  255. void I2C_SlaveInit(I2C_Type *base, const i2c_slave_config_t *slaveConfig);
  256. /*!
  257. * @brief De-initializes the I2C slave peripheral. Calling this API gates the I2C clock.
  258. * The I2C slave module can't work unless the I2C_SlaveInit is called to enable the clock.
  259. * @param base I2C base pointer
  260. */
  261. void I2C_SlaveDeinit(I2C_Type *base);
  262. /*!
  263. * @brief Sets the I2C slave configuration structure to default values.
  264. *
  265. * The purpose of this API is to get the configuration structure initialized for use in the I2C_SlaveInit().
  266. * Modify fields of the structure before calling the I2C_SlaveInit().
  267. * This is an example.
  268. * @code
  269. * i2c_slave_config_t config;
  270. * I2C_SlaveGetDefaultConfig(&config);
  271. * @endcode
  272. * @param slaveConfig A pointer to the slave configuration structure.
  273. */
  274. void I2C_SlaveGetDefaultConfig(i2c_slave_config_t *slaveConfig);
  275. /*!
  276. * @brief Enables or disabless the I2C peripheral operation.
  277. *
  278. * @param base I2C base pointer
  279. * @param enable Pass true to enable and false to disable the module.
  280. */
  281. static inline void I2C_Enable(I2C_Type *base, bool enable)
  282. {
  283. if (enable)
  284. {
  285. base->I2CR |= I2C_I2CR_IEN_MASK;
  286. }
  287. else
  288. {
  289. base->I2CR &= ~I2C_I2CR_IEN_MASK;
  290. }
  291. }
  292. /* @} */
  293. /*!
  294. * @name Status
  295. * @{
  296. */
  297. /*!
  298. * @brief Gets the I2C status flags.
  299. *
  300. * @param base I2C base pointer
  301. * @return status flag, use status flag to AND #_i2c_flags to get the related status.
  302. */
  303. static inline uint32_t I2C_MasterGetStatusFlags(I2C_Type *base)
  304. {
  305. return base->I2SR;
  306. }
  307. /*!
  308. * @brief Clears the I2C status flag state.
  309. *
  310. * The following status register flags can be cleared kI2C_ArbitrationLostFlag and kI2C_IntPendingFlag.
  311. *
  312. * @param base I2C base pointer
  313. * @param statusMask The status flag mask, defined in type i2c_status_flag_t.
  314. * The parameter can be any combination of the following values:
  315. * @arg kI2C_ArbitrationLostFlag
  316. * @arg kI2C_IntPendingFlagFlag
  317. */
  318. static inline void I2C_MasterClearStatusFlags(I2C_Type *base, uint32_t statusMask)
  319. {
  320. base->I2SR = (uint8_t)statusMask;
  321. }
  322. /*!
  323. * @brief Gets the I2C status flags.
  324. *
  325. * @param base I2C base pointer
  326. * @return status flag, use status flag to AND #_i2c_flags to get the related status.
  327. */
  328. static inline uint32_t I2C_SlaveGetStatusFlags(I2C_Type *base)
  329. {
  330. return I2C_MasterGetStatusFlags(base);
  331. }
  332. /*!
  333. * @brief Clears the I2C status flag state.
  334. *
  335. * The following status register flags can be cleared kI2C_ArbitrationLostFlag and kI2C_IntPendingFlag
  336. *
  337. * @param base I2C base pointer
  338. * @param statusMask The status flag mask, defined in type i2c_status_flag_t.
  339. * The parameter can be any combination of the following values:
  340. * @arg kI2C_IntPendingFlagFlag
  341. */
  342. static inline void I2C_SlaveClearStatusFlags(I2C_Type *base, uint32_t statusMask)
  343. {
  344. I2C_MasterClearStatusFlags(base, statusMask);
  345. }
  346. /* @} */
  347. /*!
  348. * @name Interrupts
  349. * @{
  350. */
  351. /*!
  352. * @brief Enables I2C interrupt requests.
  353. *
  354. * @param base I2C base pointer
  355. * @param mask interrupt source
  356. * The parameter can be combination of the following source if defined:
  357. * @arg kI2C_GlobalInterruptEnable
  358. * @arg kI2C_StopDetectInterruptEnable/kI2C_StartDetectInterruptEnable
  359. * @arg kI2C_SdaTimeoutInterruptEnable
  360. */
  361. void I2C_EnableInterrupts(I2C_Type *base, uint32_t mask);
  362. /*!
  363. * @brief Disables I2C interrupt requests.
  364. *
  365. * @param base I2C base pointer
  366. * @param mask interrupt source
  367. * The parameter can be combination of the following source if defined:
  368. * @arg kI2C_GlobalInterruptEnable
  369. * @arg kI2C_StopDetectInterruptEnable/kI2C_StartDetectInterruptEnable
  370. * @arg kI2C_SdaTimeoutInterruptEnable
  371. */
  372. void I2C_DisableInterrupts(I2C_Type *base, uint32_t mask);
  373. /* @} */
  374. /*!
  375. * @name Bus Operations
  376. * @{
  377. */
  378. /*!
  379. * @brief Sets the I2C master transfer baud rate.
  380. *
  381. * @param base I2C base pointer
  382. * @param baudRate_Bps the baud rate value in bps
  383. * @param srcClock_Hz Source clock
  384. */
  385. void I2C_MasterSetBaudRate(I2C_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
  386. /*!
  387. * @brief Sends a START on the I2C bus.
  388. *
  389. * This function is used to initiate a new master mode transfer by sending the START signal.
  390. * The slave address is sent following the I2C START signal.
  391. *
  392. * @param base I2C peripheral base pointer
  393. * @param address 7-bit slave device address.
  394. * @param direction Master transfer directions(transmit/receive).
  395. * @retval kStatus_Success Successfully send the start signal.
  396. * @retval kStatus_I2C_Busy Current bus is busy.
  397. */
  398. status_t I2C_MasterStart(I2C_Type *base, uint8_t address, i2c_direction_t direction);
  399. /*!
  400. * @brief Sends a STOP signal on the I2C bus.
  401. *
  402. * @retval kStatus_Success Successfully send the stop signal.
  403. * @retval kStatus_I2C_Timeout Send stop signal failed, timeout.
  404. */
  405. status_t I2C_MasterStop(I2C_Type *base);
  406. /*!
  407. * @brief Sends a REPEATED START on the I2C bus.
  408. *
  409. * @param base I2C peripheral base pointer
  410. * @param address 7-bit slave device address.
  411. * @param direction Master transfer directions(transmit/receive).
  412. * @retval kStatus_Success Successfully send the start signal.
  413. * @retval kStatus_I2C_Busy Current bus is busy but not occupied by current I2C master.
  414. */
  415. status_t I2C_MasterRepeatedStart(I2C_Type *base, uint8_t address, i2c_direction_t direction);
  416. /*!
  417. * @brief Performs a polling send transaction on the I2C bus.
  418. *
  419. * @param base The I2C peripheral base pointer.
  420. * @param txBuff The pointer to the data to be transferred.
  421. * @param txSize The length in bytes of the data to be transferred.
  422. * @param flags Transfer control flag to decide whether need to send a stop, use kI2C_TransferDefaultFlag
  423. * to issue a stop and kI2C_TransferNoStop to not send a stop.
  424. * @retval kStatus_Success Successfully complete the data transmission.
  425. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  426. * @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
  427. */
  428. status_t I2C_MasterWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize, uint32_t flags);
  429. /*!
  430. * @brief Performs a polling receive transaction on the I2C bus.
  431. *
  432. * @note The I2C_MasterReadBlocking function stops the bus before reading the final byte.
  433. * Without stopping the bus prior for the final read, the bus issues another read, resulting
  434. * in garbage data being read into the data register.
  435. *
  436. * @param base I2C peripheral base pointer.
  437. * @param rxBuff The pointer to the data to store the received data.
  438. * @param rxSize The length in bytes of the data to be received.
  439. * @param flags Transfer control flag to decide whether need to send a stop, use kI2C_TransferDefaultFlag
  440. * to issue a stop and kI2C_TransferNoStop to not send a stop.
  441. * @retval kStatus_Success Successfully complete the data transmission.
  442. * @retval kStatus_I2C_Timeout Send stop signal failed, timeout.
  443. */
  444. status_t I2C_MasterReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize, uint32_t flags);
  445. /*!
  446. * @brief Performs a polling send transaction on the I2C bus.
  447. *
  448. * @param base The I2C peripheral base pointer.
  449. * @param txBuff The pointer to the data to be transferred.
  450. * @param txSize The length in bytes of the data to be transferred.
  451. * @retval kStatus_Success Successfully complete the data transmission.
  452. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  453. * @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
  454. */
  455. status_t I2C_SlaveWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize);
  456. /*!
  457. * @brief Performs a polling receive transaction on the I2C bus.
  458. *
  459. * @param base I2C peripheral base pointer.
  460. * @param rxBuff The pointer to the data to store the received data.
  461. * @param rxSize The length in bytes of the data to be received.
  462. */
  463. void I2C_SlaveReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize);
  464. /*!
  465. * @brief Performs a master polling transfer on the I2C bus.
  466. *
  467. * @note The API does not return until the transfer succeeds or fails due
  468. * to arbitration lost or receiving a NAK.
  469. *
  470. * @param base I2C peripheral base address.
  471. * @param xfer Pointer to the transfer structure.
  472. * @retval kStatus_Success Successfully complete the data transmission.
  473. * @retval kStatus_I2C_Busy Previous transmission still not finished.
  474. * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
  475. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  476. * @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
  477. */
  478. status_t I2C_MasterTransferBlocking(I2C_Type *base, i2c_master_transfer_t *xfer);
  479. /* @} */
  480. /*!
  481. * @name Transactional
  482. * @{
  483. */
  484. /*!
  485. * @brief Initializes the I2C handle which is used in transactional functions.
  486. *
  487. * @param base I2C base pointer.
  488. * @param handle pointer to i2c_master_handle_t structure to store the transfer state.
  489. * @param callback pointer to user callback function.
  490. * @param userData user parameter passed to the callback function.
  491. */
  492. void I2C_MasterTransferCreateHandle(I2C_Type *base,
  493. i2c_master_handle_t *handle,
  494. i2c_master_transfer_callback_t callback,
  495. void *userData);
  496. /*!
  497. * @brief Performs a master interrupt non-blocking transfer on the I2C bus.
  498. *
  499. * @note Calling the API returns immediately after transfer initiates. The user needs
  500. * to call I2C_MasterGetTransferCount to poll the transfer status to check whether
  501. * the transfer is finished. If the return status is not kStatus_I2C_Busy, the transfer
  502. * is finished.
  503. *
  504. * @param base I2C base pointer.
  505. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state.
  506. * @param xfer pointer to i2c_master_transfer_t structure.
  507. * @retval kStatus_Success Successfully start the data transmission.
  508. * @retval kStatus_I2C_Busy Previous transmission still not finished.
  509. * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
  510. */
  511. status_t I2C_MasterTransferNonBlocking(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer);
  512. /*!
  513. * @brief Gets the master transfer status during a interrupt non-blocking transfer.
  514. *
  515. * @param base I2C base pointer.
  516. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state.
  517. * @param count Number of bytes transferred so far by the non-blocking transaction.
  518. * @retval kStatus_InvalidArgument count is Invalid.
  519. * @retval kStatus_Success Successfully return the count.
  520. */
  521. status_t I2C_MasterTransferGetCount(I2C_Type *base, i2c_master_handle_t *handle, size_t *count);
  522. /*!
  523. * @brief Aborts an interrupt non-blocking transfer early.
  524. *
  525. * @note This API can be called at any time when an interrupt non-blocking transfer initiates
  526. * to abort the transfer early.
  527. *
  528. * @param base I2C base pointer.
  529. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state
  530. */
  531. void I2C_MasterTransferAbort(I2C_Type *base, i2c_master_handle_t *handle);
  532. /*!
  533. * @brief Master interrupt handler.
  534. *
  535. * @param base I2C base pointer.
  536. * @param i2cHandle pointer to i2c_master_handle_t structure.
  537. */
  538. void I2C_MasterTransferHandleIRQ(I2C_Type *base, void *i2cHandle);
  539. /*!
  540. * @brief Initializes the I2C handle which is used in transactional functions.
  541. *
  542. * @param base I2C base pointer.
  543. * @param handle pointer to i2c_slave_handle_t structure to store the transfer state.
  544. * @param callback pointer to user callback function.
  545. * @param userData user parameter passed to the callback function.
  546. */
  547. void I2C_SlaveTransferCreateHandle(I2C_Type *base,
  548. i2c_slave_handle_t *handle,
  549. i2c_slave_transfer_callback_t callback,
  550. void *userData);
  551. /*!
  552. * @brief Starts accepting slave transfers.
  553. *
  554. * Call this API after calling the I2C_SlaveInit() and I2C_SlaveTransferCreateHandle() to start processing
  555. * transactions driven by an I2C master. The slave monitors the I2C bus and passes events to the
  556. * callback that was passed into the call to I2C_SlaveTransferCreateHandle(). The callback is always invoked
  557. * from the interrupt context.
  558. *
  559. * The set of events received by the callback is customizable. To do so, set the @a eventMask parameter to
  560. * the OR'd combination of #i2c_slave_transfer_event_t enumerators for the events you wish to receive.
  561. * The #kI2C_SlaveTransmitEvent and #kLPI2C_SlaveReceiveEvent events are always enabled and do not need
  562. * to be included in the mask. Alternatively, pass 0 to get a default set of only the transmit and
  563. * receive events that are always enabled. In addition, the #kI2C_SlaveAllEvents constant is provided as
  564. * a convenient way to enable all events.
  565. *
  566. * @param base The I2C peripheral base address.
  567. * @param handle Pointer to #i2c_slave_handle_t structure which stores the transfer state.
  568. * @param eventMask Bit mask formed by OR'ing together #i2c_slave_transfer_event_t enumerators to specify
  569. * which events to send to the callback. Other accepted values are 0 to get a default set of
  570. * only the transmit and receive events, and #kI2C_SlaveAllEvents to enable all events.
  571. *
  572. * @retval #kStatus_Success Slave transfers were successfully started.
  573. * @retval #kStatus_I2C_Busy Slave transfers have already been started on this handle.
  574. */
  575. status_t I2C_SlaveTransferNonBlocking(I2C_Type *base, i2c_slave_handle_t *handle, uint32_t eventMask);
  576. /*!
  577. * @brief Aborts the slave transfer.
  578. *
  579. * @note This API can be called at any time to stop slave for handling the bus events.
  580. *
  581. * @param base I2C base pointer.
  582. * @param handle pointer to i2c_slave_handle_t structure which stores the transfer state.
  583. */
  584. void I2C_SlaveTransferAbort(I2C_Type *base, i2c_slave_handle_t *handle);
  585. /*!
  586. * @brief Gets the slave transfer remaining bytes during a interrupt non-blocking transfer.
  587. *
  588. * @param base I2C base pointer.
  589. * @param handle pointer to i2c_slave_handle_t structure.
  590. * @param count Number of bytes transferred so far by the non-blocking transaction.
  591. * @retval kStatus_InvalidArgument count is Invalid.
  592. * @retval kStatus_Success Successfully return the count.
  593. */
  594. status_t I2C_SlaveTransferGetCount(I2C_Type *base, i2c_slave_handle_t *handle, size_t *count);
  595. /*!
  596. * @brief Slave interrupt handler.
  597. *
  598. * @param base I2C base pointer.
  599. * @param i2cHandle pointer to i2c_slave_handle_t structure which stores the transfer state
  600. */
  601. void I2C_SlaveTransferHandleIRQ(I2C_Type *base, void *i2cHandle);
  602. /* @} */
  603. #if defined(__cplusplus)
  604. }
  605. #endif /*_cplusplus. */
  606. /*@}*/
  607. #endif /* _FSL_I2C_H_*/