fsl_i2c.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. #ifndef _FSL_I2C_H_
  35. #define _FSL_I2C_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup i2c_driver
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. /*! @brief I2C driver version 2.0.5. */
  47. #define FSL_I2C_DRIVER_VERSION (MAKE_VERSION(2, 0, 5))
  48. /*@}*/
  49. /*! @brief Timeout times for waiting flag. */
  50. #ifndef I2C_WAIT_TIMEOUT
  51. #define I2C_WAIT_TIMEOUT 0U /* Define to zero means keep waiting until the flag is assert/deassert. */
  52. #endif
  53. #if (defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT || \
  54. defined(FSL_FEATURE_I2C_HAS_STOP_DETECT) && FSL_FEATURE_I2C_HAS_STOP_DETECT)
  55. #define I2C_HAS_STOP_DETECT
  56. #endif /* FSL_FEATURE_I2C_HAS_START_STOP_DETECT / FSL_FEATURE_I2C_HAS_STOP_DETECT */
  57. /*! @brief I2C status return codes. */
  58. enum _i2c_status
  59. {
  60. kStatus_I2C_Busy = MAKE_STATUS(kStatusGroup_I2C, 0), /*!< I2C is busy with current transfer. */
  61. kStatus_I2C_Idle = MAKE_STATUS(kStatusGroup_I2C, 1), /*!< Bus is Idle. */
  62. kStatus_I2C_Nak = MAKE_STATUS(kStatusGroup_I2C, 2), /*!< NAK received during transfer. */
  63. kStatus_I2C_ArbitrationLost = MAKE_STATUS(kStatusGroup_I2C, 3), /*!< Arbitration lost during transfer. */
  64. kStatus_I2C_Timeout = MAKE_STATUS(kStatusGroup_I2C, 4), /*!< Timeout poling status flags. */
  65. kStatus_I2C_Addr_Nak = MAKE_STATUS(kStatusGroup_I2C, 5), /*!< NAK received during the address probe. */
  66. };
  67. /*!
  68. * @brief I2C peripheral flags
  69. *
  70. * The following status register flags can be cleared:
  71. * - #kI2C_ArbitrationLostFlag
  72. * - #kI2C_IntPendingFlag
  73. * - #kI2C_StartDetectFlag
  74. * - #kI2C_StopDetectFlag
  75. *
  76. * @note These enumerations are meant to be OR'd together to form a bit mask.
  77. *
  78. */
  79. enum _i2c_flags
  80. {
  81. kI2C_ReceiveNakFlag = I2C_S_RXAK_MASK, /*!< I2C receive NAK flag. */
  82. kI2C_IntPendingFlag = I2C_S_IICIF_MASK, /*!< I2C interrupt pending flag. */
  83. kI2C_TransferDirectionFlag = I2C_S_SRW_MASK, /*!< I2C transfer direction flag. */
  84. kI2C_RangeAddressMatchFlag = I2C_S_RAM_MASK, /*!< I2C range address match flag. */
  85. kI2C_ArbitrationLostFlag = I2C_S_ARBL_MASK, /*!< I2C arbitration lost flag. */
  86. kI2C_BusBusyFlag = I2C_S_BUSY_MASK, /*!< I2C bus busy flag. */
  87. kI2C_AddressMatchFlag = I2C_S_IAAS_MASK, /*!< I2C address match flag. */
  88. kI2C_TransferCompleteFlag = I2C_S_TCF_MASK, /*!< I2C transfer complete flag. */
  89. #ifdef I2C_HAS_STOP_DETECT
  90. kI2C_StopDetectFlag = I2C_FLT_STOPF_MASK << 8, /*!< I2C stop detect flag. */
  91. #endif /* FSL_FEATURE_I2C_HAS_START_STOP_DETECT / FSL_FEATURE_I2C_HAS_STOP_DETECT */
  92. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  93. kI2C_StartDetectFlag = I2C_FLT_STARTF_MASK << 8, /*!< I2C start detect flag. */
  94. #endif /* FSL_FEATURE_I2C_HAS_START_STOP_DETECT */
  95. };
  96. /*! @brief I2C feature interrupt source. */
  97. enum _i2c_interrupt_enable
  98. {
  99. kI2C_GlobalInterruptEnable = I2C_C1_IICIE_MASK, /*!< I2C global interrupt. */
  100. #if defined(FSL_FEATURE_I2C_HAS_STOP_DETECT) && FSL_FEATURE_I2C_HAS_STOP_DETECT
  101. kI2C_StopDetectInterruptEnable = I2C_FLT_STOPIE_MASK, /*!< I2C stop detect interrupt. */
  102. #endif /* FSL_FEATURE_I2C_HAS_STOP_DETECT */
  103. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  104. kI2C_StartStopDetectInterruptEnable = I2C_FLT_SSIE_MASK, /*!< I2C start&stop detect interrupt. */
  105. #endif /* FSL_FEATURE_I2C_HAS_START_STOP_DETECT */
  106. };
  107. /*! @brief The direction of master and slave transfers. */
  108. typedef enum _i2c_direction
  109. {
  110. kI2C_Write = 0x0U, /*!< Master transmits to the slave. */
  111. kI2C_Read = 0x1U, /*!< Master receives from the slave. */
  112. } i2c_direction_t;
  113. /*! @brief Addressing mode. */
  114. typedef enum _i2c_slave_address_mode
  115. {
  116. kI2C_Address7bit = 0x0U, /*!< 7-bit addressing mode. */
  117. kI2C_RangeMatch = 0X2U, /*!< Range address match addressing mode. */
  118. } i2c_slave_address_mode_t;
  119. /*! @brief I2C transfer control flag. */
  120. enum _i2c_master_transfer_flags
  121. {
  122. kI2C_TransferDefaultFlag = 0x0U, /*!< A transfer starts with a start signal, stops with a stop signal. */
  123. kI2C_TransferNoStartFlag = 0x1U, /*!< A transfer starts without a start signal, only support write only or
  124. write+read with no start flag, do not support read only with no start flag. */
  125. kI2C_TransferRepeatedStartFlag = 0x2U, /*!< A transfer starts with a repeated start signal. */
  126. kI2C_TransferNoStopFlag = 0x4U, /*!< A transfer ends without a stop signal. */
  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. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  147. kI2C_SlaveStartEvent = 0x10U, /*!< A start/repeated start was detected. */
  148. #endif
  149. kI2C_SlaveCompletionEvent = 0x20U, /*!< A stop was detected or finished transfer, completing the transfer. */
  150. kI2C_SlaveGenaralcallEvent = 0x40U, /*!< Received the general call address after a start or repeated start. */
  151. /*! A bit mask of all available events. */
  152. kI2C_SlaveAllEvents = kI2C_SlaveAddressMatchEvent | kI2C_SlaveTransmitEvent | kI2C_SlaveReceiveEvent |
  153. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  154. kI2C_SlaveStartEvent |
  155. #endif
  156. kI2C_SlaveCompletionEvent | kI2C_SlaveGenaralcallEvent,
  157. } i2c_slave_transfer_event_t;
  158. /*! @brief I2C master user configuration. */
  159. typedef struct _i2c_master_config
  160. {
  161. bool enableMaster; /*!< Enables the I2C peripheral at initialization time. */
  162. #if defined(FSL_FEATURE_I2C_HAS_STOP_HOLD_OFF) && FSL_FEATURE_I2C_HAS_STOP_HOLD_OFF
  163. bool enableStopHold; /*!< Controls the stop hold enable. */
  164. #endif
  165. #if defined(FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE) && FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE
  166. bool enableDoubleBuffering; /*!< Controls double buffer enable; notice that
  167. enabling the double buffer disables the clock stretch. */
  168. #endif
  169. uint32_t baudRate_Bps; /*!< Baud rate configuration of I2C peripheral. */
  170. uint8_t glitchFilterWidth; /*!< Controls the width of the glitch. */
  171. } i2c_master_config_t;
  172. /*! @brief I2C slave user configuration. */
  173. typedef struct _i2c_slave_config
  174. {
  175. bool enableSlave; /*!< Enables the I2C peripheral at initialization time. */
  176. bool enableGeneralCall; /*!< Enables the general call addressing mode. */
  177. bool enableWakeUp; /*!< Enables/disables waking up MCU from low-power mode. */
  178. #if defined(FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE) && FSL_FEATURE_I2C_HAS_DOUBLE_BUFFER_ENABLE
  179. bool enableDoubleBuffering; /*!< Controls a double buffer enable; notice that
  180. enabling the double buffer disables the clock stretch. */
  181. #endif
  182. bool enableBaudRateCtl; /*!< Enables/disables independent slave baud rate on SCL in very fast I2C modes. */
  183. uint16_t slaveAddress; /*!< A slave address configuration. */
  184. uint16_t upperAddress; /*!< A maximum boundary slave address used in a range matching mode. */
  185. i2c_slave_address_mode_t
  186. addressingMode; /*!< An addressing mode configuration of i2c_slave_address_mode_config_t. */
  187. uint32_t sclStopHoldTime_ns; /*!< the delay from the rising edge of SCL (I2C clock) to the rising edge of SDA (I2C
  188. data) while SCL is high (stop condition), SDA hold time and SCL start hold time
  189. are also configured according to the SCL stop hold time. */
  190. } i2c_slave_config_t;
  191. /*! @brief I2C master handle typedef. */
  192. typedef struct _i2c_master_handle i2c_master_handle_t;
  193. /*! @brief I2C master transfer callback typedef. */
  194. typedef void (*i2c_master_transfer_callback_t)(I2C_Type *base,
  195. i2c_master_handle_t *handle,
  196. status_t status,
  197. void *userData);
  198. /*! @brief I2C slave handle typedef. */
  199. typedef struct _i2c_slave_handle i2c_slave_handle_t;
  200. /*! @brief I2C master transfer structure. */
  201. typedef struct _i2c_master_transfer
  202. {
  203. uint32_t flags; /*!< A transfer flag which controls the transfer. */
  204. uint8_t slaveAddress; /*!< 7-bit slave address. */
  205. i2c_direction_t direction; /*!< A transfer direction, read or write. */
  206. uint32_t subaddress; /*!< A sub address. Transferred MSB first. */
  207. uint8_t subaddressSize; /*!< A size of the command buffer. */
  208. uint8_t *volatile data; /*!< A transfer buffer. */
  209. volatile size_t dataSize; /*!< A transfer size. */
  210. } i2c_master_transfer_t;
  211. /*! @brief I2C master handle structure. */
  212. struct _i2c_master_handle
  213. {
  214. i2c_master_transfer_t transfer; /*!< I2C master transfer copy. */
  215. size_t transferSize; /*!< Total bytes to be transferred. */
  216. uint8_t state; /*!< A transfer state maintained during transfer. */
  217. i2c_master_transfer_callback_t completionCallback; /*!< A callback function called when the transfer is finished. */
  218. void *userData; /*!< A callback parameter passed to the callback function. */
  219. };
  220. /*! @brief I2C slave transfer structure. */
  221. typedef struct _i2c_slave_transfer
  222. {
  223. i2c_slave_transfer_event_t event; /*!< A reason that the callback is invoked. */
  224. uint8_t *volatile data; /*!< A transfer buffer. */
  225. volatile size_t dataSize; /*!< A transfer size. */
  226. status_t completionStatus; /*!< Success or error code describing how the transfer completed. Only applies for
  227. #kI2C_SlaveCompletionEvent. */
  228. size_t transferredCount; /*!< A number of bytes actually transferred since the start or since the last repeated
  229. start. */
  230. } i2c_slave_transfer_t;
  231. /*! @brief I2C slave transfer callback typedef. */
  232. typedef void (*i2c_slave_transfer_callback_t)(I2C_Type *base, i2c_slave_transfer_t *xfer, void *userData);
  233. /*! @brief I2C slave handle structure. */
  234. struct _i2c_slave_handle
  235. {
  236. volatile bool isBusy; /*!< Indicates whether a transfer is busy. */
  237. i2c_slave_transfer_t transfer; /*!< I2C slave transfer copy. */
  238. uint32_t eventMask; /*!< A mask of enabled events. */
  239. i2c_slave_transfer_callback_t callback; /*!< A callback function called at the transfer event. */
  240. void *userData; /*!< A callback parameter passed to the callback. */
  241. };
  242. /*******************************************************************************
  243. * API
  244. ******************************************************************************/
  245. #if defined(__cplusplus)
  246. extern "C" {
  247. #endif /*_cplusplus. */
  248. /*!
  249. * @name Initialization and deinitialization
  250. * @{
  251. */
  252. /*!
  253. * @brief Initializes the I2C peripheral. Call this API to ungate the I2C clock
  254. * and configure the I2C with master configuration.
  255. *
  256. * @note This API should be called at the beginning of the application.
  257. * Otherwise, any operation to the I2C module can cause a hard fault
  258. * because the clock is not enabled. The configuration structure can be custom filled
  259. * or it can be set with default values by using the I2C_MasterGetDefaultConfig().
  260. * After calling this API, the master is ready to transfer.
  261. * This is an example.
  262. * @code
  263. * i2c_master_config_t config = {
  264. * .enableMaster = true,
  265. * .enableStopHold = false,
  266. * .highDrive = false,
  267. * .baudRate_Bps = 100000,
  268. * .glitchFilterWidth = 0
  269. * };
  270. * I2C_MasterInit(I2C0, &config, 12000000U);
  271. * @endcode
  272. *
  273. * @param base I2C base pointer
  274. * @param masterConfig A pointer to the master configuration structure
  275. * @param srcClock_Hz I2C peripheral clock frequency in Hz
  276. */
  277. void I2C_MasterInit(I2C_Type *base, const i2c_master_config_t *masterConfig, uint32_t srcClock_Hz);
  278. /*!
  279. * @brief Initializes the I2C peripheral. Call this API to ungate the I2C clock
  280. * and initialize the I2C with the slave configuration.
  281. *
  282. * @note This API should be called at the beginning of the application.
  283. * Otherwise, any operation to the I2C module can cause a hard fault
  284. * because the clock is not enabled. The configuration structure can partly be set
  285. * with default values by I2C_SlaveGetDefaultConfig() or it can be custom filled by the user.
  286. * This is an example.
  287. * @code
  288. * i2c_slave_config_t config = {
  289. * .enableSlave = true,
  290. * .enableGeneralCall = false,
  291. * .addressingMode = kI2C_Address7bit,
  292. * .slaveAddress = 0x1DU,
  293. * .enableWakeUp = false,
  294. * .enablehighDrive = false,
  295. * .enableBaudRateCtl = false,
  296. * .sclStopHoldTime_ns = 4000
  297. * };
  298. * I2C_SlaveInit(I2C0, &config, 12000000U);
  299. * @endcode
  300. *
  301. * @param base I2C base pointer
  302. * @param slaveConfig A pointer to the slave configuration structure
  303. * @param srcClock_Hz I2C peripheral clock frequency in Hz
  304. */
  305. void I2C_SlaveInit(I2C_Type *base, const i2c_slave_config_t *slaveConfig, uint32_t srcClock_Hz);
  306. /*!
  307. * @brief De-initializes the I2C master peripheral. Call this API to gate the I2C clock.
  308. * The I2C master module can't work unless the I2C_MasterInit is called.
  309. * @param base I2C base pointer
  310. */
  311. void I2C_MasterDeinit(I2C_Type *base);
  312. /*!
  313. * @brief De-initializes the I2C slave peripheral. Calling this API gates the I2C clock.
  314. * The I2C slave module can't work unless the I2C_SlaveInit is called to enable the clock.
  315. * @param base I2C base pointer
  316. */
  317. void I2C_SlaveDeinit(I2C_Type *base);
  318. /*!
  319. * @brief Sets the I2C master configuration structure to default values.
  320. *
  321. * The purpose of this API is to get the configuration structure initialized for use in the I2C_MasterConfigure().
  322. * Use the initialized structure unchanged in the I2C_MasterConfigure() or modify
  323. * the structure before calling the I2C_MasterConfigure().
  324. * This is an example.
  325. * @code
  326. * i2c_master_config_t config;
  327. * I2C_MasterGetDefaultConfig(&config);
  328. * @endcode
  329. * @param masterConfig A pointer to the master configuration structure.
  330. */
  331. void I2C_MasterGetDefaultConfig(i2c_master_config_t *masterConfig);
  332. /*!
  333. * @brief Sets the I2C slave configuration structure to default values.
  334. *
  335. * The purpose of this API is to get the configuration structure initialized for use in the I2C_SlaveConfigure().
  336. * Modify fields of the structure before calling the I2C_SlaveConfigure().
  337. * This is an example.
  338. * @code
  339. * i2c_slave_config_t config;
  340. * I2C_SlaveGetDefaultConfig(&config);
  341. * @endcode
  342. * @param slaveConfig A pointer to the slave configuration structure.
  343. */
  344. void I2C_SlaveGetDefaultConfig(i2c_slave_config_t *slaveConfig);
  345. /*!
  346. * @brief Enables or disabless the I2C peripheral operation.
  347. *
  348. * @param base I2C base pointer
  349. * @param enable Pass true to enable and false to disable the module.
  350. */
  351. static inline void I2C_Enable(I2C_Type *base, bool enable)
  352. {
  353. if (enable)
  354. {
  355. base->C1 |= I2C_C1_IICEN_MASK;
  356. }
  357. else
  358. {
  359. base->C1 &= ~I2C_C1_IICEN_MASK;
  360. }
  361. }
  362. /* @} */
  363. /*!
  364. * @name Status
  365. * @{
  366. */
  367. /*!
  368. * @brief Gets the I2C status flags.
  369. *
  370. * @param base I2C base pointer
  371. * @return status flag, use status flag to AND #_i2c_flags to get the related status.
  372. */
  373. uint32_t I2C_MasterGetStatusFlags(I2C_Type *base);
  374. /*!
  375. * @brief Gets the I2C status flags.
  376. *
  377. * @param base I2C base pointer
  378. * @return status flag, use status flag to AND #_i2c_flags to get the related status.
  379. */
  380. static inline uint32_t I2C_SlaveGetStatusFlags(I2C_Type *base)
  381. {
  382. return I2C_MasterGetStatusFlags(base);
  383. }
  384. /*!
  385. * @brief Clears the I2C status flag state.
  386. *
  387. * The following status register flags can be cleared kI2C_ArbitrationLostFlag and kI2C_IntPendingFlag.
  388. *
  389. * @param base I2C base pointer
  390. * @param statusMask The status flag mask, defined in type i2c_status_flag_t.
  391. * The parameter can be any combination of the following values:
  392. * @arg kI2C_StartDetectFlag (if available)
  393. * @arg kI2C_StopDetectFlag (if available)
  394. * @arg kI2C_ArbitrationLostFlag
  395. * @arg kI2C_IntPendingFlagFlag
  396. */
  397. static inline void I2C_MasterClearStatusFlags(I2C_Type *base, uint32_t statusMask)
  398. {
  399. /* Must clear the STARTF / STOPF bits prior to clearing IICIF */
  400. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  401. if (statusMask & kI2C_StartDetectFlag)
  402. {
  403. /* Shift the odd-ball flags back into place. */
  404. base->FLT |= (uint8_t)(statusMask >> 8U);
  405. }
  406. #endif
  407. #ifdef I2C_HAS_STOP_DETECT
  408. if (statusMask & kI2C_StopDetectFlag)
  409. {
  410. /* Shift the odd-ball flags back into place. */
  411. base->FLT |= (uint8_t)(statusMask >> 8U);
  412. }
  413. #endif
  414. base->S = (uint8_t)statusMask;
  415. }
  416. /*!
  417. * @brief Clears the I2C status flag state.
  418. *
  419. * The following status register flags can be cleared kI2C_ArbitrationLostFlag and kI2C_IntPendingFlag
  420. *
  421. * @param base I2C base pointer
  422. * @param statusMask The status flag mask, defined in type i2c_status_flag_t.
  423. * The parameter can be any combination of the following values:
  424. * @arg kI2C_StartDetectFlag (if available)
  425. * @arg kI2C_StopDetectFlag (if available)
  426. * @arg kI2C_ArbitrationLostFlag
  427. * @arg kI2C_IntPendingFlagFlag
  428. */
  429. static inline void I2C_SlaveClearStatusFlags(I2C_Type *base, uint32_t statusMask)
  430. {
  431. I2C_MasterClearStatusFlags(base, statusMask);
  432. }
  433. /* @} */
  434. /*!
  435. * @name Interrupts
  436. * @{
  437. */
  438. /*!
  439. * @brief Enables I2C interrupt requests.
  440. *
  441. * @param base I2C base pointer
  442. * @param mask interrupt source
  443. * The parameter can be combination of the following source if defined:
  444. * @arg kI2C_GlobalInterruptEnable
  445. * @arg kI2C_StopDetectInterruptEnable/kI2C_StartDetectInterruptEnable
  446. * @arg kI2C_SdaTimeoutInterruptEnable
  447. */
  448. void I2C_EnableInterrupts(I2C_Type *base, uint32_t mask);
  449. /*!
  450. * @brief Disables I2C interrupt requests.
  451. *
  452. * @param base I2C base pointer
  453. * @param mask interrupt source
  454. * The parameter can be combination of the following source if defined:
  455. * @arg kI2C_GlobalInterruptEnable
  456. * @arg kI2C_StopDetectInterruptEnable/kI2C_StartDetectInterruptEnable
  457. * @arg kI2C_SdaTimeoutInterruptEnable
  458. */
  459. void I2C_DisableInterrupts(I2C_Type *base, uint32_t mask);
  460. /*!
  461. * @name DMA Control
  462. * @{
  463. */
  464. #if defined(FSL_FEATURE_I2C_HAS_DMA_SUPPORT) && FSL_FEATURE_I2C_HAS_DMA_SUPPORT
  465. /*!
  466. * @brief Enables/disables the I2C DMA interrupt.
  467. *
  468. * @param base I2C base pointer
  469. * @param enable true to enable, false to disable
  470. */
  471. static inline void I2C_EnableDMA(I2C_Type *base, bool enable)
  472. {
  473. if (enable)
  474. {
  475. base->C1 |= I2C_C1_DMAEN_MASK;
  476. }
  477. else
  478. {
  479. base->C1 &= ~I2C_C1_DMAEN_MASK;
  480. }
  481. }
  482. #endif /* FSL_FEATURE_I2C_HAS_DMA_SUPPORT */
  483. /*!
  484. * @brief Gets the I2C tx/rx data register address. This API is used to provide a transfer address
  485. * for I2C DMA transfer configuration.
  486. *
  487. * @param base I2C base pointer
  488. * @return data register address
  489. */
  490. static inline uint32_t I2C_GetDataRegAddr(I2C_Type *base)
  491. {
  492. return (uint32_t)(&(base->D));
  493. }
  494. /* @} */
  495. /*!
  496. * @name Bus Operations
  497. * @{
  498. */
  499. /*!
  500. * @brief Sets the I2C master transfer baud rate.
  501. *
  502. * @param base I2C base pointer
  503. * @param baudRate_Bps the baud rate value in bps
  504. * @param srcClock_Hz Source clock
  505. */
  506. void I2C_MasterSetBaudRate(I2C_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
  507. /*!
  508. * @brief Sends a START on the I2C bus.
  509. *
  510. * This function is used to initiate a new master mode transfer by sending the START signal.
  511. * The slave address is sent following the I2C START signal.
  512. *
  513. * @param base I2C peripheral base pointer
  514. * @param address 7-bit slave device address.
  515. * @param direction Master transfer directions(transmit/receive).
  516. * @retval kStatus_Success Successfully send the start signal.
  517. * @retval kStatus_I2C_Busy Current bus is busy.
  518. */
  519. status_t I2C_MasterStart(I2C_Type *base, uint8_t address, i2c_direction_t direction);
  520. /*!
  521. * @brief Sends a STOP signal on the I2C bus.
  522. *
  523. * @retval kStatus_Success Successfully send the stop signal.
  524. * @retval kStatus_I2C_Timeout Send stop signal failed, timeout.
  525. */
  526. status_t I2C_MasterStop(I2C_Type *base);
  527. /*!
  528. * @brief Sends a REPEATED START on the I2C bus.
  529. *
  530. * @param base I2C peripheral base pointer
  531. * @param address 7-bit slave device address.
  532. * @param direction Master transfer directions(transmit/receive).
  533. * @retval kStatus_Success Successfully send the start signal.
  534. * @retval kStatus_I2C_Busy Current bus is busy but not occupied by current I2C master.
  535. */
  536. status_t I2C_MasterRepeatedStart(I2C_Type *base, uint8_t address, i2c_direction_t direction);
  537. /*!
  538. * @brief Performs a polling send transaction on the I2C bus.
  539. *
  540. * @param base The I2C peripheral base pointer.
  541. * @param txBuff The pointer to the data to be transferred.
  542. * @param txSize The length in bytes of the data to be transferred.
  543. * @param flags Transfer control flag to decide whether need to send a stop, use kI2C_TransferDefaultFlag
  544. * to issue a stop and kI2C_TransferNoStop to not send a stop.
  545. * @retval kStatus_Success Successfully complete the data transmission.
  546. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  547. * @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
  548. */
  549. status_t I2C_MasterWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize, uint32_t flags);
  550. /*!
  551. * @brief Performs a polling receive transaction on the I2C bus.
  552. *
  553. * @note The I2C_MasterReadBlocking function stops the bus before reading the final byte.
  554. * Without stopping the bus prior for the final read, the bus issues another read, resulting
  555. * in garbage data being read into the data register.
  556. *
  557. * @param base I2C peripheral base pointer.
  558. * @param rxBuff The pointer to the data to store the received data.
  559. * @param rxSize The length in bytes of the data to be received.
  560. * @param flags Transfer control flag to decide whether need to send a stop, use kI2C_TransferDefaultFlag
  561. * to issue a stop and kI2C_TransferNoStop to not send a stop.
  562. * @retval kStatus_Success Successfully complete the data transmission.
  563. * @retval kStatus_I2C_Timeout Send stop signal failed, timeout.
  564. */
  565. status_t I2C_MasterReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize, uint32_t flags);
  566. /*!
  567. * @brief Performs a polling send transaction on the I2C bus.
  568. *
  569. * @param base The I2C peripheral base pointer.
  570. * @param txBuff The pointer to the data to be transferred.
  571. * @param txSize The length in bytes of the data to be transferred.
  572. * @retval kStatus_Success Successfully complete the data transmission.
  573. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  574. * @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
  575. */
  576. status_t I2C_SlaveWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize);
  577. /*!
  578. * @brief Performs a polling receive transaction on the I2C bus.
  579. *
  580. * @param base I2C peripheral base pointer.
  581. * @param rxBuff The pointer to the data to store the received data.
  582. * @param rxSize The length in bytes of the data to be received.
  583. * @retval kStatus_Success Successfully complete data receive.
  584. * @retval kStatus_I2C_Timeout Wait status flag timeout.
  585. */
  586. status_t I2C_SlaveReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize);
  587. /*!
  588. * @brief Performs a master polling transfer on the I2C bus.
  589. *
  590. * @note The API does not return until the transfer succeeds or fails due
  591. * to arbitration lost or receiving a NAK.
  592. *
  593. * @param base I2C peripheral base address.
  594. * @param xfer Pointer to the transfer structure.
  595. * @retval kStatus_Success Successfully complete the data transmission.
  596. * @retval kStatus_I2C_Busy Previous transmission still not finished.
  597. * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
  598. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  599. * @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
  600. */
  601. status_t I2C_MasterTransferBlocking(I2C_Type *base, i2c_master_transfer_t *xfer);
  602. /* @} */
  603. /*!
  604. * @name Transactional
  605. * @{
  606. */
  607. /*!
  608. * @brief Initializes the I2C handle which is used in transactional functions.
  609. *
  610. * @param base I2C base pointer.
  611. * @param handle pointer to i2c_master_handle_t structure to store the transfer state.
  612. * @param callback pointer to user callback function.
  613. * @param userData user parameter passed to the callback function.
  614. */
  615. void I2C_MasterTransferCreateHandle(I2C_Type *base,
  616. i2c_master_handle_t *handle,
  617. i2c_master_transfer_callback_t callback,
  618. void *userData);
  619. /*!
  620. * @brief Performs a master interrupt non-blocking transfer on the I2C bus.
  621. *
  622. * @note Calling the API returns immediately after transfer initiates. The user needs
  623. * to call I2C_MasterGetTransferCount to poll the transfer status to check whether
  624. * the transfer is finished. If the return status is not kStatus_I2C_Busy, the transfer
  625. * is finished.
  626. *
  627. * @param base I2C base pointer.
  628. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state.
  629. * @param xfer pointer to i2c_master_transfer_t structure.
  630. * @retval kStatus_Success Successfully start the data transmission.
  631. * @retval kStatus_I2C_Busy Previous transmission still not finished.
  632. * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
  633. */
  634. status_t I2C_MasterTransferNonBlocking(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer);
  635. /*!
  636. * @brief Gets the master transfer status during a interrupt non-blocking transfer.
  637. *
  638. * @param base I2C base pointer.
  639. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state.
  640. * @param count Number of bytes transferred so far by the non-blocking transaction.
  641. * @retval kStatus_InvalidArgument count is Invalid.
  642. * @retval kStatus_Success Successfully return the count.
  643. */
  644. status_t I2C_MasterTransferGetCount(I2C_Type *base, i2c_master_handle_t *handle, size_t *count);
  645. /*!
  646. * @brief Aborts an interrupt non-blocking transfer early.
  647. *
  648. * @note This API can be called at any time when an interrupt non-blocking transfer initiates
  649. * to abort the transfer early.
  650. *
  651. * @param base I2C base pointer.
  652. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state
  653. * @retval kStatus_I2C_Timeout Timeout during polling flag.
  654. * @retval kStatus_Success Successfully abort the transfer.
  655. */
  656. status_t I2C_MasterTransferAbort(I2C_Type *base, i2c_master_handle_t *handle);
  657. /*!
  658. * @brief Master interrupt handler.
  659. *
  660. * @param base I2C base pointer.
  661. * @param i2cHandle pointer to i2c_master_handle_t structure.
  662. */
  663. void I2C_MasterTransferHandleIRQ(I2C_Type *base, void *i2cHandle);
  664. /*!
  665. * @brief Initializes the I2C handle which is used in transactional functions.
  666. *
  667. * @param base I2C base pointer.
  668. * @param handle pointer to i2c_slave_handle_t structure to store the transfer state.
  669. * @param callback pointer to user callback function.
  670. * @param userData user parameter passed to the callback function.
  671. */
  672. void I2C_SlaveTransferCreateHandle(I2C_Type *base,
  673. i2c_slave_handle_t *handle,
  674. i2c_slave_transfer_callback_t callback,
  675. void *userData);
  676. /*!
  677. * @brief Starts accepting slave transfers.
  678. *
  679. * Call this API after calling the I2C_SlaveInit() and I2C_SlaveTransferCreateHandle() to start processing
  680. * transactions driven by an I2C master. The slave monitors the I2C bus and passes events to the
  681. * callback that was passed into the call to I2C_SlaveTransferCreateHandle(). The callback is always invoked
  682. * from the interrupt context.
  683. *
  684. * The set of events received by the callback is customizable. To do so, set the @a eventMask parameter to
  685. * the OR'd combination of #i2c_slave_transfer_event_t enumerators for the events you wish to receive.
  686. * The #kI2C_SlaveTransmitEvent and #kLPI2C_SlaveReceiveEvent events are always enabled and do not need
  687. * to be included in the mask. Alternatively, pass 0 to get a default set of only the transmit and
  688. * receive events that are always enabled. In addition, the #kI2C_SlaveAllEvents constant is provided as
  689. * a convenient way to enable all events.
  690. *
  691. * @param base The I2C peripheral base address.
  692. * @param handle Pointer to #i2c_slave_handle_t structure which stores the transfer state.
  693. * @param eventMask Bit mask formed by OR'ing together #i2c_slave_transfer_event_t enumerators to specify
  694. * which events to send to the callback. Other accepted values are 0 to get a default set of
  695. * only the transmit and receive events, and #kI2C_SlaveAllEvents to enable all events.
  696. *
  697. * @retval #kStatus_Success Slave transfers were successfully started.
  698. * @retval #kStatus_I2C_Busy Slave transfers have already been started on this handle.
  699. */
  700. status_t I2C_SlaveTransferNonBlocking(I2C_Type *base, i2c_slave_handle_t *handle, uint32_t eventMask);
  701. /*!
  702. * @brief Aborts the slave transfer.
  703. *
  704. * @note This API can be called at any time to stop slave for handling the bus events.
  705. *
  706. * @param base I2C base pointer.
  707. * @param handle pointer to i2c_slave_handle_t structure which stores the transfer state.
  708. */
  709. void I2C_SlaveTransferAbort(I2C_Type *base, i2c_slave_handle_t *handle);
  710. /*!
  711. * @brief Gets the slave transfer remaining bytes during a interrupt non-blocking transfer.
  712. *
  713. * @param base I2C base pointer.
  714. * @param handle pointer to i2c_slave_handle_t structure.
  715. * @param count Number of bytes transferred so far by the non-blocking transaction.
  716. * @retval kStatus_InvalidArgument count is Invalid.
  717. * @retval kStatus_Success Successfully return the count.
  718. */
  719. status_t I2C_SlaveTransferGetCount(I2C_Type *base, i2c_slave_handle_t *handle, size_t *count);
  720. /*!
  721. * @brief Slave interrupt handler.
  722. *
  723. * @param base I2C base pointer.
  724. * @param i2cHandle pointer to i2c_slave_handle_t structure which stores the transfer state
  725. */
  726. void I2C_SlaveTransferHandleIRQ(I2C_Type *base, void *i2cHandle);
  727. /* @} */
  728. #if defined(__cplusplus)
  729. }
  730. #endif /*_cplusplus. */
  731. /*@}*/
  732. #endif /* _FSL_I2C_H_*/