fsl_i2c.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. * Variables
  244. ******************************************************************************/
  245. /*! @brief Pointers to i2c bases for each instance. */
  246. extern I2C_Type *const s_i2cBases[];
  247. /*******************************************************************************
  248. * API
  249. ******************************************************************************/
  250. #if defined(__cplusplus)
  251. extern "C" {
  252. #endif /*_cplusplus. */
  253. /*!
  254. * @name Initialization and deinitialization
  255. * @{
  256. */
  257. /*!
  258. * @brief Initializes the I2C peripheral. Call this API to ungate the I2C clock
  259. * and configure the I2C with master configuration.
  260. *
  261. * @note This API should be called at the beginning of the application.
  262. * Otherwise, any operation to the I2C module can cause a hard fault
  263. * because the clock is not enabled. The configuration structure can be custom filled
  264. * or it can be set with default values by using the I2C_MasterGetDefaultConfig().
  265. * After calling this API, the master is ready to transfer.
  266. * This is an example.
  267. * @code
  268. * i2c_master_config_t config = {
  269. * .enableMaster = true,
  270. * .enableStopHold = false,
  271. * .highDrive = false,
  272. * .baudRate_Bps = 100000,
  273. * .glitchFilterWidth = 0
  274. * };
  275. * I2C_MasterInit(I2C0, &config, 12000000U);
  276. * @endcode
  277. *
  278. * @param base I2C base pointer
  279. * @param masterConfig A pointer to the master configuration structure
  280. * @param srcClock_Hz I2C peripheral clock frequency in Hz
  281. */
  282. void I2C_MasterInit(I2C_Type *base, const i2c_master_config_t *masterConfig, uint32_t srcClock_Hz);
  283. /*!
  284. * @brief Initializes the I2C peripheral. Call this API to ungate the I2C clock
  285. * and initialize the I2C with the slave configuration.
  286. *
  287. * @note This API should be called at the beginning of the application.
  288. * Otherwise, any operation to the I2C module can cause a hard fault
  289. * because the clock is not enabled. The configuration structure can partly be set
  290. * with default values by I2C_SlaveGetDefaultConfig() or it can be custom filled by the user.
  291. * This is an example.
  292. * @code
  293. * i2c_slave_config_t config = {
  294. * .enableSlave = true,
  295. * .enableGeneralCall = false,
  296. * .addressingMode = kI2C_Address7bit,
  297. * .slaveAddress = 0x1DU,
  298. * .enableWakeUp = false,
  299. * .enablehighDrive = false,
  300. * .enableBaudRateCtl = false,
  301. * .sclStopHoldTime_ns = 4000
  302. * };
  303. * I2C_SlaveInit(I2C0, &config, 12000000U);
  304. * @endcode
  305. *
  306. * @param base I2C base pointer
  307. * @param slaveConfig A pointer to the slave configuration structure
  308. * @param srcClock_Hz I2C peripheral clock frequency in Hz
  309. */
  310. void I2C_SlaveInit(I2C_Type *base, const i2c_slave_config_t *slaveConfig, uint32_t srcClock_Hz);
  311. /*!
  312. * @brief De-initializes the I2C master peripheral. Call this API to gate the I2C clock.
  313. * The I2C master module can't work unless the I2C_MasterInit is called.
  314. * @param base I2C base pointer
  315. */
  316. void I2C_MasterDeinit(I2C_Type *base);
  317. /*!
  318. * @brief De-initializes the I2C slave peripheral. Calling this API gates the I2C clock.
  319. * The I2C slave module can't work unless the I2C_SlaveInit is called to enable the clock.
  320. * @param base I2C base pointer
  321. */
  322. void I2C_SlaveDeinit(I2C_Type *base);
  323. /*!
  324. * @brief Get instance number for I2C module.
  325. *
  326. * @param base I2C peripheral base address.
  327. */
  328. uint32_t I2C_GetInstance(I2C_Type *base);
  329. /*!
  330. * @brief Sets the I2C master configuration structure to default values.
  331. *
  332. * The purpose of this API is to get the configuration structure initialized for use in the I2C_MasterConfigure().
  333. * Use the initialized structure unchanged in the I2C_MasterConfigure() or modify
  334. * the structure before calling the I2C_MasterConfigure().
  335. * This is an example.
  336. * @code
  337. * i2c_master_config_t config;
  338. * I2C_MasterGetDefaultConfig(&config);
  339. * @endcode
  340. * @param masterConfig A pointer to the master configuration structure.
  341. */
  342. void I2C_MasterGetDefaultConfig(i2c_master_config_t *masterConfig);
  343. /*!
  344. * @brief Sets the I2C slave configuration structure to default values.
  345. *
  346. * The purpose of this API is to get the configuration structure initialized for use in the I2C_SlaveConfigure().
  347. * Modify fields of the structure before calling the I2C_SlaveConfigure().
  348. * This is an example.
  349. * @code
  350. * i2c_slave_config_t config;
  351. * I2C_SlaveGetDefaultConfig(&config);
  352. * @endcode
  353. * @param slaveConfig A pointer to the slave configuration structure.
  354. */
  355. void I2C_SlaveGetDefaultConfig(i2c_slave_config_t *slaveConfig);
  356. /*!
  357. * @brief Enables or disabless the I2C peripheral operation.
  358. *
  359. * @param base I2C base pointer
  360. * @param enable Pass true to enable and false to disable the module.
  361. */
  362. static inline void I2C_Enable(I2C_Type *base, bool enable)
  363. {
  364. if (enable)
  365. {
  366. base->C1 |= I2C_C1_IICEN_MASK;
  367. }
  368. else
  369. {
  370. base->C1 &= ~I2C_C1_IICEN_MASK;
  371. }
  372. }
  373. /* @} */
  374. /*!
  375. * @name Status
  376. * @{
  377. */
  378. /*!
  379. * @brief Gets the I2C status flags.
  380. *
  381. * @param base I2C base pointer
  382. * @return status flag, use status flag to AND #_i2c_flags to get the related status.
  383. */
  384. uint32_t I2C_MasterGetStatusFlags(I2C_Type *base);
  385. /*!
  386. * @brief Gets the I2C status flags.
  387. *
  388. * @param base I2C base pointer
  389. * @return status flag, use status flag to AND #_i2c_flags to get the related status.
  390. */
  391. static inline uint32_t I2C_SlaveGetStatusFlags(I2C_Type *base)
  392. {
  393. return I2C_MasterGetStatusFlags(base);
  394. }
  395. /*!
  396. * @brief Clears the I2C status flag state.
  397. *
  398. * The following status register flags can be cleared kI2C_ArbitrationLostFlag and kI2C_IntPendingFlag.
  399. *
  400. * @param base I2C base pointer
  401. * @param statusMask The status flag mask, defined in type i2c_status_flag_t.
  402. * The parameter can be any combination of the following values:
  403. * @arg kI2C_StartDetectFlag (if available)
  404. * @arg kI2C_StopDetectFlag (if available)
  405. * @arg kI2C_ArbitrationLostFlag
  406. * @arg kI2C_IntPendingFlagFlag
  407. */
  408. static inline void I2C_MasterClearStatusFlags(I2C_Type *base, uint32_t statusMask)
  409. {
  410. /* Must clear the STARTF / STOPF bits prior to clearing IICIF */
  411. #if defined(FSL_FEATURE_I2C_HAS_START_STOP_DETECT) && FSL_FEATURE_I2C_HAS_START_STOP_DETECT
  412. if (statusMask & kI2C_StartDetectFlag)
  413. {
  414. /* Shift the odd-ball flags back into place. */
  415. base->FLT |= (uint8_t)(statusMask >> 8U);
  416. }
  417. #endif
  418. #ifdef I2C_HAS_STOP_DETECT
  419. if (statusMask & kI2C_StopDetectFlag)
  420. {
  421. /* Shift the odd-ball flags back into place. */
  422. base->FLT |= (uint8_t)(statusMask >> 8U);
  423. }
  424. #endif
  425. base->S = (uint8_t)statusMask;
  426. }
  427. /*!
  428. * @brief Clears the I2C status flag state.
  429. *
  430. * The following status register flags can be cleared kI2C_ArbitrationLostFlag and kI2C_IntPendingFlag
  431. *
  432. * @param base I2C base pointer
  433. * @param statusMask The status flag mask, defined in type i2c_status_flag_t.
  434. * The parameter can be any combination of the following values:
  435. * @arg kI2C_StartDetectFlag (if available)
  436. * @arg kI2C_StopDetectFlag (if available)
  437. * @arg kI2C_ArbitrationLostFlag
  438. * @arg kI2C_IntPendingFlagFlag
  439. */
  440. static inline void I2C_SlaveClearStatusFlags(I2C_Type *base, uint32_t statusMask)
  441. {
  442. I2C_MasterClearStatusFlags(base, statusMask);
  443. }
  444. /* @} */
  445. /*!
  446. * @name Interrupts
  447. * @{
  448. */
  449. /*!
  450. * @brief Enables 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_EnableInterrupts(I2C_Type *base, uint32_t mask);
  460. /*!
  461. * @brief Disables I2C interrupt requests.
  462. *
  463. * @param base I2C base pointer
  464. * @param mask interrupt source
  465. * The parameter can be combination of the following source if defined:
  466. * @arg kI2C_GlobalInterruptEnable
  467. * @arg kI2C_StopDetectInterruptEnable/kI2C_StartDetectInterruptEnable
  468. * @arg kI2C_SdaTimeoutInterruptEnable
  469. */
  470. void I2C_DisableInterrupts(I2C_Type *base, uint32_t mask);
  471. /*!
  472. * @name DMA Control
  473. * @{
  474. */
  475. #if defined(FSL_FEATURE_I2C_HAS_DMA_SUPPORT) && FSL_FEATURE_I2C_HAS_DMA_SUPPORT
  476. /*!
  477. * @brief Enables/disables the I2C DMA interrupt.
  478. *
  479. * @param base I2C base pointer
  480. * @param enable true to enable, false to disable
  481. */
  482. static inline void I2C_EnableDMA(I2C_Type *base, bool enable)
  483. {
  484. if (enable)
  485. {
  486. base->C1 |= I2C_C1_DMAEN_MASK;
  487. }
  488. else
  489. {
  490. base->C1 &= ~I2C_C1_DMAEN_MASK;
  491. }
  492. }
  493. #endif /* FSL_FEATURE_I2C_HAS_DMA_SUPPORT */
  494. /*!
  495. * @brief Gets the I2C tx/rx data register address. This API is used to provide a transfer address
  496. * for I2C DMA transfer configuration.
  497. *
  498. * @param base I2C base pointer
  499. * @return data register address
  500. */
  501. static inline uint32_t I2C_GetDataRegAddr(I2C_Type *base)
  502. {
  503. return (uint32_t)(&(base->D));
  504. }
  505. /* @} */
  506. /*!
  507. * @name Bus Operations
  508. * @{
  509. */
  510. /*!
  511. * @brief Sets the I2C master transfer baud rate.
  512. *
  513. * @param base I2C base pointer
  514. * @param baudRate_Bps the baud rate value in bps
  515. * @param srcClock_Hz Source clock
  516. */
  517. void I2C_MasterSetBaudRate(I2C_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
  518. /*!
  519. * @brief Sends a START on the I2C bus.
  520. *
  521. * This function is used to initiate a new master mode transfer by sending the START signal.
  522. * The slave address is sent following the I2C START signal.
  523. *
  524. * @param base I2C peripheral base pointer
  525. * @param address 7-bit slave device address.
  526. * @param direction Master transfer directions(transmit/receive).
  527. * @retval kStatus_Success Successfully send the start signal.
  528. * @retval kStatus_I2C_Busy Current bus is busy.
  529. */
  530. status_t I2C_MasterStart(I2C_Type *base, uint8_t address, i2c_direction_t direction);
  531. /*!
  532. * @brief Sends a STOP signal on the I2C bus.
  533. *
  534. * @retval kStatus_Success Successfully send the stop signal.
  535. * @retval kStatus_I2C_Timeout Send stop signal failed, timeout.
  536. */
  537. status_t I2C_MasterStop(I2C_Type *base);
  538. /*!
  539. * @brief Sends a REPEATED START on the I2C bus.
  540. *
  541. * @param base I2C peripheral base pointer
  542. * @param address 7-bit slave device address.
  543. * @param direction Master transfer directions(transmit/receive).
  544. * @retval kStatus_Success Successfully send the start signal.
  545. * @retval kStatus_I2C_Busy Current bus is busy but not occupied by current I2C master.
  546. */
  547. status_t I2C_MasterRepeatedStart(I2C_Type *base, uint8_t address, i2c_direction_t direction);
  548. /*!
  549. * @brief Performs a polling send transaction on the I2C bus.
  550. *
  551. * @param base The I2C peripheral base pointer.
  552. * @param txBuff The pointer to the data to be transferred.
  553. * @param txSize The length in bytes of the data to be transferred.
  554. * @param flags Transfer control flag to decide whether need to send a stop, use kI2C_TransferDefaultFlag
  555. * to issue a stop and kI2C_TransferNoStop to not send a stop.
  556. * @retval kStatus_Success Successfully complete the data transmission.
  557. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  558. * @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
  559. */
  560. status_t I2C_MasterWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize, uint32_t flags);
  561. /*!
  562. * @brief Performs a polling receive transaction on the I2C bus.
  563. *
  564. * @note The I2C_MasterReadBlocking function stops the bus before reading the final byte.
  565. * Without stopping the bus prior for the final read, the bus issues another read, resulting
  566. * in garbage data being read into the data register.
  567. *
  568. * @param base I2C peripheral base pointer.
  569. * @param rxBuff The pointer to the data to store the received data.
  570. * @param rxSize The length in bytes of the data to be received.
  571. * @param flags Transfer control flag to decide whether need to send a stop, use kI2C_TransferDefaultFlag
  572. * to issue a stop and kI2C_TransferNoStop to not send a stop.
  573. * @retval kStatus_Success Successfully complete the data transmission.
  574. * @retval kStatus_I2C_Timeout Send stop signal failed, timeout.
  575. */
  576. status_t I2C_MasterReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize, uint32_t flags);
  577. /*!
  578. * @brief Performs a polling send transaction on the I2C bus.
  579. *
  580. * @param base The I2C peripheral base pointer.
  581. * @param txBuff The pointer to the data to be transferred.
  582. * @param txSize The length in bytes of the data to be transferred.
  583. * @retval kStatus_Success Successfully complete the data transmission.
  584. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  585. * @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
  586. */
  587. status_t I2C_SlaveWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize);
  588. /*!
  589. * @brief Performs a polling receive transaction on the I2C bus.
  590. *
  591. * @param base I2C peripheral base pointer.
  592. * @param rxBuff The pointer to the data to store the received data.
  593. * @param rxSize The length in bytes of the data to be received.
  594. * @retval kStatus_Success Successfully complete data receive.
  595. * @retval kStatus_I2C_Timeout Wait status flag timeout.
  596. */
  597. status_t I2C_SlaveReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize);
  598. /*!
  599. * @brief Performs a master polling transfer on the I2C bus.
  600. *
  601. * @note The API does not return until the transfer succeeds or fails due
  602. * to arbitration lost or receiving a NAK.
  603. *
  604. * @param base I2C peripheral base address.
  605. * @param xfer Pointer to the transfer structure.
  606. * @retval kStatus_Success Successfully complete the data transmission.
  607. * @retval kStatus_I2C_Busy Previous transmission still not finished.
  608. * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
  609. * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
  610. * @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
  611. */
  612. status_t I2C_MasterTransferBlocking(I2C_Type *base, i2c_master_transfer_t *xfer);
  613. /* @} */
  614. /*!
  615. * @name Transactional
  616. * @{
  617. */
  618. /*!
  619. * @brief Initializes the I2C handle which is used in transactional functions.
  620. *
  621. * @param base I2C base pointer.
  622. * @param handle pointer to i2c_master_handle_t structure to store the transfer state.
  623. * @param callback pointer to user callback function.
  624. * @param userData user parameter passed to the callback function.
  625. */
  626. void I2C_MasterTransferCreateHandle(I2C_Type *base,
  627. i2c_master_handle_t *handle,
  628. i2c_master_transfer_callback_t callback,
  629. void *userData);
  630. /*!
  631. * @brief Performs a master interrupt non-blocking transfer on the I2C bus.
  632. *
  633. * @note Calling the API returns immediately after transfer initiates. The user needs
  634. * to call I2C_MasterGetTransferCount to poll the transfer status to check whether
  635. * the transfer is finished. If the return status is not kStatus_I2C_Busy, the transfer
  636. * is finished.
  637. *
  638. * @param base I2C base pointer.
  639. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state.
  640. * @param xfer pointer to i2c_master_transfer_t structure.
  641. * @retval kStatus_Success Successfully start the data transmission.
  642. * @retval kStatus_I2C_Busy Previous transmission still not finished.
  643. * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
  644. */
  645. status_t I2C_MasterTransferNonBlocking(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer);
  646. /*!
  647. * @brief Gets the master transfer status during a interrupt non-blocking transfer.
  648. *
  649. * @param base I2C base pointer.
  650. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state.
  651. * @param count Number of bytes transferred so far by the non-blocking transaction.
  652. * @retval kStatus_InvalidArgument count is Invalid.
  653. * @retval kStatus_Success Successfully return the count.
  654. */
  655. status_t I2C_MasterTransferGetCount(I2C_Type *base, i2c_master_handle_t *handle, size_t *count);
  656. /*!
  657. * @brief Aborts an interrupt non-blocking transfer early.
  658. *
  659. * @note This API can be called at any time when an interrupt non-blocking transfer initiates
  660. * to abort the transfer early.
  661. *
  662. * @param base I2C base pointer.
  663. * @param handle pointer to i2c_master_handle_t structure which stores the transfer state
  664. * @retval kStatus_I2C_Timeout Timeout during polling flag.
  665. * @retval kStatus_Success Successfully abort the transfer.
  666. */
  667. status_t I2C_MasterTransferAbort(I2C_Type *base, i2c_master_handle_t *handle);
  668. /*!
  669. * @brief Master interrupt handler.
  670. *
  671. * @param base I2C base pointer.
  672. * @param i2cHandle pointer to i2c_master_handle_t structure.
  673. */
  674. void I2C_MasterTransferHandleIRQ(I2C_Type *base, void *i2cHandle);
  675. /*!
  676. * @brief Initializes the I2C handle which is used in transactional functions.
  677. *
  678. * @param base I2C base pointer.
  679. * @param handle pointer to i2c_slave_handle_t structure to store the transfer state.
  680. * @param callback pointer to user callback function.
  681. * @param userData user parameter passed to the callback function.
  682. */
  683. void I2C_SlaveTransferCreateHandle(I2C_Type *base,
  684. i2c_slave_handle_t *handle,
  685. i2c_slave_transfer_callback_t callback,
  686. void *userData);
  687. /*!
  688. * @brief Starts accepting slave transfers.
  689. *
  690. * Call this API after calling the I2C_SlaveInit() and I2C_SlaveTransferCreateHandle() to start processing
  691. * transactions driven by an I2C master. The slave monitors the I2C bus and passes events to the
  692. * callback that was passed into the call to I2C_SlaveTransferCreateHandle(). The callback is always invoked
  693. * from the interrupt context.
  694. *
  695. * The set of events received by the callback is customizable. To do so, set the @a eventMask parameter to
  696. * the OR'd combination of #i2c_slave_transfer_event_t enumerators for the events you wish to receive.
  697. * The #kI2C_SlaveTransmitEvent and #kLPI2C_SlaveReceiveEvent events are always enabled and do not need
  698. * to be included in the mask. Alternatively, pass 0 to get a default set of only the transmit and
  699. * receive events that are always enabled. In addition, the #kI2C_SlaveAllEvents constant is provided as
  700. * a convenient way to enable all events.
  701. *
  702. * @param base The I2C peripheral base address.
  703. * @param handle Pointer to #i2c_slave_handle_t structure which stores the transfer state.
  704. * @param eventMask Bit mask formed by OR'ing together #i2c_slave_transfer_event_t enumerators to specify
  705. * which events to send to the callback. Other accepted values are 0 to get a default set of
  706. * only the transmit and receive events, and #kI2C_SlaveAllEvents to enable all events.
  707. *
  708. * @retval #kStatus_Success Slave transfers were successfully started.
  709. * @retval #kStatus_I2C_Busy Slave transfers have already been started on this handle.
  710. */
  711. status_t I2C_SlaveTransferNonBlocking(I2C_Type *base, i2c_slave_handle_t *handle, uint32_t eventMask);
  712. /*!
  713. * @brief Aborts the slave transfer.
  714. *
  715. * @note This API can be called at any time to stop slave for handling the bus events.
  716. *
  717. * @param base I2C base pointer.
  718. * @param handle pointer to i2c_slave_handle_t structure which stores the transfer state.
  719. */
  720. void I2C_SlaveTransferAbort(I2C_Type *base, i2c_slave_handle_t *handle);
  721. /*!
  722. * @brief Gets the slave transfer remaining bytes during a interrupt non-blocking transfer.
  723. *
  724. * @param base I2C base pointer.
  725. * @param handle pointer to i2c_slave_handle_t structure.
  726. * @param count Number of bytes transferred so far by the non-blocking transaction.
  727. * @retval kStatus_InvalidArgument count is Invalid.
  728. * @retval kStatus_Success Successfully return the count.
  729. */
  730. status_t I2C_SlaveTransferGetCount(I2C_Type *base, i2c_slave_handle_t *handle, size_t *count);
  731. /*!
  732. * @brief Slave interrupt handler.
  733. *
  734. * @param base I2C base pointer.
  735. * @param i2cHandle pointer to i2c_slave_handle_t structure which stores the transfer state
  736. */
  737. void I2C_SlaveTransferHandleIRQ(I2C_Type *base, void *i2cHandle);
  738. /* @} */
  739. #if defined(__cplusplus)
  740. }
  741. #endif /*_cplusplus. */
  742. /*@}*/
  743. #endif /* _FSL_I2C_H_*/