Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

806 wiersze
32 KiB

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