fsl_flexio_i2c_master.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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_FLEXIO_I2C_MASTER_H_
  9. #define _FSL_FLEXIO_I2C_MASTER_H_
  10. #include "fsl_common.h"
  11. #include "fsl_flexio.h"
  12. /*!
  13. * @addtogroup flexio_i2c_master
  14. * @{
  15. */
  16. /*******************************************************************************
  17. * Definitions
  18. ******************************************************************************/
  19. /*! @name Driver version */
  20. /*@{*/
  21. #define FSL_FLEXIO_I2C_MASTER_DRIVER_VERSION (MAKE_VERSION(2, 4, 0))
  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 FlexIO I2C transfer status*/
  28. enum
  29. {
  30. kStatus_FLEXIO_I2C_Busy = MAKE_STATUS(kStatusGroup_FLEXIO_I2C, 0), /*!< I2C is busy doing transfer. */
  31. kStatus_FLEXIO_I2C_Idle = MAKE_STATUS(kStatusGroup_FLEXIO_I2C, 1), /*!< I2C is busy doing transfer. */
  32. kStatus_FLEXIO_I2C_Nak = MAKE_STATUS(kStatusGroup_FLEXIO_I2C, 2), /*!< NAK received during transfer. */
  33. kStatus_FLEXIO_I2C_Timeout = MAKE_STATUS(kStatusGroup_FLEXIO_I2C, 3), /*!< Timeout polling status flags. */
  34. };
  35. /*! @brief Define FlexIO I2C master interrupt mask. */
  36. enum _flexio_i2c_master_interrupt
  37. {
  38. kFLEXIO_I2C_TxEmptyInterruptEnable = 0x1U, /*!< Tx buffer empty interrupt enable. */
  39. kFLEXIO_I2C_RxFullInterruptEnable = 0x2U, /*!< Rx buffer full interrupt enable. */
  40. };
  41. /*! @brief Define FlexIO I2C master status mask. */
  42. enum _flexio_i2c_master_status_flags
  43. {
  44. kFLEXIO_I2C_TxEmptyFlag = 0x1U, /*!< Tx shifter empty flag. */
  45. kFLEXIO_I2C_RxFullFlag = 0x2U, /*!< Rx shifter full/Transfer complete flag. */
  46. kFLEXIO_I2C_ReceiveNakFlag = 0x4U, /*!< Receive NAK flag. */
  47. };
  48. /*! @brief Direction of master transfer.*/
  49. typedef enum _flexio_i2c_direction
  50. {
  51. kFLEXIO_I2C_Write = 0x0U, /*!< Master send to slave. */
  52. kFLEXIO_I2C_Read = 0x1U, /*!< Master receive from slave. */
  53. } flexio_i2c_direction_t;
  54. /*! @brief Define FlexIO I2C master access structure typedef. */
  55. typedef struct _flexio_i2c_type
  56. {
  57. FLEXIO_Type *flexioBase; /*!< FlexIO base pointer. */
  58. uint8_t SDAPinIndex; /*!< Pin select for I2C SDA. */
  59. uint8_t SCLPinIndex; /*!< Pin select for I2C SCL. */
  60. uint8_t shifterIndex[2]; /*!< Shifter index used in FlexIO I2C. */
  61. uint8_t timerIndex[3]; /*!< Timer index used in FlexIO I2C. */
  62. uint32_t baudrate; /*!< Master transfer baudrate, used to calculate delay time. */
  63. } FLEXIO_I2C_Type;
  64. /*! @brief Define FlexIO I2C master user configuration structure. */
  65. typedef struct _flexio_i2c_master_config
  66. {
  67. bool enableMaster; /*!< Enables the FlexIO I2C peripheral at initialization time. */
  68. bool enableInDoze; /*!< Enable/disable FlexIO operation in doze mode. */
  69. bool enableInDebug; /*!< Enable/disable FlexIO operation in debug mode. */
  70. bool enableFastAccess; /*!< Enable/disable fast access to FlexIO registers, fast access requires
  71. the FlexIO clock to be at least twice the frequency of the bus clock. */
  72. uint32_t baudRate_Bps; /*!< Baud rate in Bps. */
  73. } flexio_i2c_master_config_t;
  74. /*! @brief Define FlexIO I2C master transfer structure. */
  75. typedef struct _flexio_i2c_master_transfer
  76. {
  77. uint32_t flags; /*!< Transfer flag which controls the transfer, reserved for FlexIO I2C. */
  78. uint8_t slaveAddress; /*!< 7-bit slave address. */
  79. flexio_i2c_direction_t direction; /*!< Transfer direction, read or write. */
  80. uint32_t subaddress; /*!< Sub address. Transferred MSB first. */
  81. uint8_t subaddressSize; /*!< Size of command buffer. */
  82. uint8_t volatile *data; /*!< Transfer buffer. */
  83. volatile size_t dataSize; /*!< Transfer size. */
  84. } flexio_i2c_master_transfer_t;
  85. /*! @brief FlexIO I2C master handle typedef. */
  86. typedef struct _flexio_i2c_master_handle flexio_i2c_master_handle_t;
  87. /*! @brief FlexIO I2C master transfer callback typedef. */
  88. typedef void (*flexio_i2c_master_transfer_callback_t)(FLEXIO_I2C_Type *base,
  89. flexio_i2c_master_handle_t *handle,
  90. status_t status,
  91. void *userData);
  92. /*! @brief Define FlexIO I2C master handle structure. */
  93. struct _flexio_i2c_master_handle
  94. {
  95. flexio_i2c_master_transfer_t transfer; /*!< FlexIO I2C master transfer copy. */
  96. size_t transferSize; /*!< Total bytes to be transferred. */
  97. uint8_t state; /*!< Transfer state maintained during transfer. */
  98. flexio_i2c_master_transfer_callback_t completionCallback; /*!< Callback function called at transfer event. */
  99. /*!< Callback function called at transfer event. */
  100. void *userData; /*!< Callback parameter passed to callback function. */
  101. bool needRestart; /*!< Whether master needs to send re-start signal. */
  102. };
  103. /*******************************************************************************
  104. * API
  105. ******************************************************************************/
  106. #if defined(__cplusplus)
  107. extern "C" {
  108. #endif /*_cplusplus*/
  109. /*!
  110. * @name Initialization and deinitialization
  111. * @{
  112. */
  113. #if defined(FSL_FEATURE_FLEXIO_HAS_PIN_STATUS) && FSL_FEATURE_FLEXIO_HAS_PIN_STATUS
  114. /*!
  115. * @brief Make sure the bus isn't already pulled down.
  116. *
  117. * Check the FLEXIO pin status to see whether either of SDA and SCL pin is pulled down.
  118. *
  119. * @param base Pointer to FLEXIO_I2C_Type structure..
  120. * @retval kStatus_Success
  121. * @retval kStatus_FLEXIO_I2C_Busy
  122. */
  123. status_t FLEXIO_I2C_CheckForBusyBus(FLEXIO_I2C_Type *base);
  124. #endif /*FSL_FEATURE_FLEXIO_HAS_PIN_STATUS*/
  125. /*!
  126. * @brief Ungates the FlexIO clock, resets the FlexIO module, and configures the FlexIO I2C
  127. * hardware configuration.
  128. *
  129. * Example
  130. @code
  131. FLEXIO_I2C_Type base = {
  132. .flexioBase = FLEXIO,
  133. .SDAPinIndex = 0,
  134. .SCLPinIndex = 1,
  135. .shifterIndex = {0,1},
  136. .timerIndex = {0,1}
  137. };
  138. flexio_i2c_master_config_t config = {
  139. .enableInDoze = false,
  140. .enableInDebug = true,
  141. .enableFastAccess = false,
  142. .baudRate_Bps = 100000
  143. };
  144. FLEXIO_I2C_MasterInit(base, &config, srcClock_Hz);
  145. @endcode
  146. *
  147. * @param base Pointer to FLEXIO_I2C_Type structure.
  148. * @param masterConfig Pointer to flexio_i2c_master_config_t structure.
  149. * @param srcClock_Hz FlexIO source clock in Hz.
  150. * @retval kStatus_Success Initialization successful
  151. * @retval kStatus_InvalidArgument The source clock exceed upper range limitation
  152. */
  153. status_t FLEXIO_I2C_MasterInit(FLEXIO_I2C_Type *base, flexio_i2c_master_config_t *masterConfig, uint32_t srcClock_Hz);
  154. /*!
  155. * @brief De-initializes the FlexIO I2C master peripheral. Calling this API Resets the FlexIO I2C master
  156. * shifer and timer config, module can't work unless the FLEXIO_I2C_MasterInit is called.
  157. *
  158. * @param base pointer to FLEXIO_I2C_Type structure.
  159. */
  160. void FLEXIO_I2C_MasterDeinit(FLEXIO_I2C_Type *base);
  161. /*!
  162. * @brief Gets the default configuration to configure the FlexIO module. The configuration
  163. * can be used directly for calling the FLEXIO_I2C_MasterInit().
  164. *
  165. * Example:
  166. @code
  167. flexio_i2c_master_config_t config;
  168. FLEXIO_I2C_MasterGetDefaultConfig(&config);
  169. @endcode
  170. * @param masterConfig Pointer to flexio_i2c_master_config_t structure.
  171. */
  172. void FLEXIO_I2C_MasterGetDefaultConfig(flexio_i2c_master_config_t *masterConfig);
  173. /*!
  174. * @brief Enables/disables the FlexIO module operation.
  175. *
  176. * @param base Pointer to FLEXIO_I2C_Type structure.
  177. * @param enable Pass true to enable module, false does not have any effect.
  178. */
  179. static inline void FLEXIO_I2C_MasterEnable(FLEXIO_I2C_Type *base, bool enable)
  180. {
  181. if (enable)
  182. {
  183. base->flexioBase->CTRL |= FLEXIO_CTRL_FLEXEN_MASK;
  184. }
  185. }
  186. /* @} */
  187. /*!
  188. * @name Status
  189. * @{
  190. */
  191. /*!
  192. * @brief Gets the FlexIO I2C master status flags.
  193. *
  194. * @param base Pointer to FLEXIO_I2C_Type structure
  195. * @return Status flag, use status flag to AND #_flexio_i2c_master_status_flags can get the related status.
  196. */
  197. uint32_t FLEXIO_I2C_MasterGetStatusFlags(FLEXIO_I2C_Type *base);
  198. /*!
  199. * @brief Clears the FlexIO I2C master status flags.
  200. *
  201. * @param base Pointer to FLEXIO_I2C_Type structure.
  202. * @param mask Status flag.
  203. * The parameter can be any combination of the following values:
  204. * @arg kFLEXIO_I2C_RxFullFlag
  205. * @arg kFLEXIO_I2C_ReceiveNakFlag
  206. */
  207. void FLEXIO_I2C_MasterClearStatusFlags(FLEXIO_I2C_Type *base, uint32_t mask);
  208. /*@}*/
  209. /*!
  210. * @name Interrupts
  211. * @{
  212. */
  213. /*!
  214. * @brief Enables the FlexIO i2c master interrupt requests.
  215. *
  216. * @param base Pointer to FLEXIO_I2C_Type structure.
  217. * @param mask Interrupt source.
  218. * Currently only one interrupt request source:
  219. * @arg kFLEXIO_I2C_TransferCompleteInterruptEnable
  220. */
  221. void FLEXIO_I2C_MasterEnableInterrupts(FLEXIO_I2C_Type *base, uint32_t mask);
  222. /*!
  223. * @brief Disables the FlexIO I2C master interrupt requests.
  224. *
  225. * @param base Pointer to FLEXIO_I2C_Type structure.
  226. * @param mask Interrupt source.
  227. */
  228. void FLEXIO_I2C_MasterDisableInterrupts(FLEXIO_I2C_Type *base, uint32_t mask);
  229. /*@}*/
  230. /*!
  231. * @name Bus Operations
  232. * @{
  233. */
  234. /*!
  235. * @brief Sets the FlexIO I2C master transfer baudrate.
  236. *
  237. * @param base Pointer to FLEXIO_I2C_Type structure
  238. * @param baudRate_Bps the baud rate value in HZ
  239. * @param srcClock_Hz source clock in HZ
  240. */
  241. void FLEXIO_I2C_MasterSetBaudRate(FLEXIO_I2C_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
  242. /*!
  243. * @brief Sends START + 7-bit address to the bus.
  244. *
  245. * @note This API should be called when the transfer configuration is ready to send a START signal
  246. * and 7-bit address to the bus. This is a non-blocking API, which returns directly after the address
  247. * is put into the data register but the address transfer is not finished on the bus. Ensure that
  248. * the kFLEXIO_I2C_RxFullFlag status is asserted before calling this API.
  249. * @param base Pointer to FLEXIO_I2C_Type structure.
  250. * @param address 7-bit address.
  251. * @param direction transfer direction.
  252. * This parameter is one of the values in flexio_i2c_direction_t:
  253. * @arg kFLEXIO_I2C_Write: Transmit
  254. * @arg kFLEXIO_I2C_Read: Receive
  255. */
  256. void FLEXIO_I2C_MasterStart(FLEXIO_I2C_Type *base, uint8_t address, flexio_i2c_direction_t direction);
  257. /*!
  258. * @brief Sends the stop signal on the bus.
  259. *
  260. * @param base Pointer to FLEXIO_I2C_Type structure.
  261. */
  262. void FLEXIO_I2C_MasterStop(FLEXIO_I2C_Type *base);
  263. /*!
  264. * @brief Sends the repeated start signal on the bus.
  265. *
  266. * @param base Pointer to FLEXIO_I2C_Type structure.
  267. */
  268. void FLEXIO_I2C_MasterRepeatedStart(FLEXIO_I2C_Type *base);
  269. /*!
  270. * @brief Sends the stop signal when transfer is still on-going.
  271. *
  272. * @param base Pointer to FLEXIO_I2C_Type structure.
  273. */
  274. void FLEXIO_I2C_MasterAbortStop(FLEXIO_I2C_Type *base);
  275. /*!
  276. * @brief Configures the sent ACK/NAK for the following byte.
  277. *
  278. * @param base Pointer to FLEXIO_I2C_Type structure.
  279. * @param enable True to configure send ACK, false configure to send NAK.
  280. */
  281. void FLEXIO_I2C_MasterEnableAck(FLEXIO_I2C_Type *base, bool enable);
  282. /*!
  283. * @brief Sets the number of bytes to be transferred from a start signal to a stop signal.
  284. *
  285. * @note Call this API before a transfer begins because the timer generates a number of clocks according
  286. * to the number of bytes that need to be transferred.
  287. *
  288. * @param base Pointer to FLEXIO_I2C_Type structure.
  289. * @param count Number of bytes need to be transferred from a start signal to a re-start/stop signal
  290. * @retval kStatus_Success Successfully configured the count.
  291. * @retval kStatus_InvalidArgument Input argument is invalid.
  292. */
  293. status_t FLEXIO_I2C_MasterSetTransferCount(FLEXIO_I2C_Type *base, uint16_t count);
  294. /*!
  295. * @brief Writes one byte of data to the I2C bus.
  296. *
  297. * @note This is a non-blocking API, which returns directly after the data is put into the
  298. * data register but the data transfer is not finished on the bus. Ensure that
  299. * the TxEmptyFlag is asserted before calling this API.
  300. *
  301. * @param base Pointer to FLEXIO_I2C_Type structure.
  302. * @param data a byte of data.
  303. */
  304. static inline void FLEXIO_I2C_MasterWriteByte(FLEXIO_I2C_Type *base, uint32_t data)
  305. {
  306. base->flexioBase->SHIFTBUFBBS[base->shifterIndex[0]] = data;
  307. }
  308. /*!
  309. * @brief Reads one byte of data from the I2C bus.
  310. *
  311. * @note This is a non-blocking API, which returns directly after the data is read from the
  312. * data register. Ensure that the data is ready in the register.
  313. *
  314. * @param base Pointer to FLEXIO_I2C_Type structure.
  315. * @return data byte read.
  316. */
  317. static inline uint8_t FLEXIO_I2C_MasterReadByte(FLEXIO_I2C_Type *base)
  318. {
  319. return (uint8_t)(base->flexioBase->SHIFTBUFBIS[base->shifterIndex[1]]);
  320. }
  321. /*!
  322. * @brief Sends a buffer of data in bytes.
  323. *
  324. * @note This function blocks via polling until all bytes have been sent.
  325. *
  326. * @param base Pointer to FLEXIO_I2C_Type structure.
  327. * @param txBuff The data bytes to send.
  328. * @param txSize The number of data bytes to send.
  329. * @retval kStatus_Success Successfully write data.
  330. * @retval kStatus_FLEXIO_I2C_Nak Receive NAK during writing data.
  331. * @retval kStatus_FLEXIO_I2C_Timeout Timeout polling status flags.
  332. */
  333. status_t FLEXIO_I2C_MasterWriteBlocking(FLEXIO_I2C_Type *base, const uint8_t *txBuff, uint8_t txSize);
  334. /*!
  335. * @brief Receives a buffer of bytes.
  336. *
  337. * @note This function blocks via polling until all bytes have been received.
  338. *
  339. * @param base Pointer to FLEXIO_I2C_Type structure.
  340. * @param rxBuff The buffer to store the received bytes.
  341. * @param rxSize The number of data bytes to be received.
  342. * @retval kStatus_Success Successfully read data.
  343. * @retval kStatus_FLEXIO_I2C_Timeout Timeout polling status flags.
  344. */
  345. status_t FLEXIO_I2C_MasterReadBlocking(FLEXIO_I2C_Type *base, uint8_t *rxBuff, uint8_t rxSize);
  346. /*!
  347. * @brief Performs a master polling transfer on the I2C bus.
  348. *
  349. * @note The API does not return until the transfer succeeds or fails due
  350. * to receiving NAK.
  351. *
  352. * @param base pointer to FLEXIO_I2C_Type structure.
  353. * @param xfer pointer to flexio_i2c_master_transfer_t structure.
  354. * @return status of status_t.
  355. */
  356. status_t FLEXIO_I2C_MasterTransferBlocking(FLEXIO_I2C_Type *base, flexio_i2c_master_transfer_t *xfer);
  357. /*@}*/
  358. /*Transactional APIs*/
  359. /*!
  360. * @name Transactional
  361. * @{
  362. */
  363. /*!
  364. * @brief Initializes the I2C handle which is used in transactional functions.
  365. *
  366. * @param base Pointer to FLEXIO_I2C_Type structure.
  367. * @param handle Pointer to flexio_i2c_master_handle_t structure to store the transfer state.
  368. * @param callback Pointer to user callback function.
  369. * @param userData User param passed to the callback function.
  370. * @retval kStatus_Success Successfully create the handle.
  371. * @retval kStatus_OutOfRange The FlexIO type/handle/isr table out of range.
  372. */
  373. status_t FLEXIO_I2C_MasterTransferCreateHandle(FLEXIO_I2C_Type *base,
  374. flexio_i2c_master_handle_t *handle,
  375. flexio_i2c_master_transfer_callback_t callback,
  376. void *userData);
  377. /*!
  378. * @brief Performs a master interrupt non-blocking transfer on the I2C bus.
  379. *
  380. * @note The API returns immediately after the transfer initiates.
  381. * Call FLEXIO_I2C_MasterTransferGetCount to poll the transfer status to check whether
  382. * the transfer is finished. If the return status is not kStatus_FLEXIO_I2C_Busy, the transfer
  383. * is finished.
  384. *
  385. * @param base Pointer to FLEXIO_I2C_Type structure
  386. * @param handle Pointer to flexio_i2c_master_handle_t structure which stores the transfer state
  387. * @param xfer pointer to flexio_i2c_master_transfer_t structure
  388. * @retval kStatus_Success Successfully start a transfer.
  389. * @retval kStatus_FLEXIO_I2C_Busy FlexIO I2C is not idle, is running another transfer.
  390. */
  391. status_t FLEXIO_I2C_MasterTransferNonBlocking(FLEXIO_I2C_Type *base,
  392. flexio_i2c_master_handle_t *handle,
  393. flexio_i2c_master_transfer_t *xfer);
  394. /*!
  395. * @brief Gets the master transfer status during a interrupt non-blocking transfer.
  396. *
  397. * @param base Pointer to FLEXIO_I2C_Type structure.
  398. * @param handle Pointer to flexio_i2c_master_handle_t structure which stores the transfer state.
  399. * @param count Number of bytes transferred so far by the non-blocking transaction.
  400. * @retval kStatus_InvalidArgument count is Invalid.
  401. * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  402. * @retval kStatus_Success Successfully return the count.
  403. */
  404. status_t FLEXIO_I2C_MasterTransferGetCount(FLEXIO_I2C_Type *base, flexio_i2c_master_handle_t *handle, size_t *count);
  405. /*!
  406. * @brief Aborts an interrupt non-blocking transfer early.
  407. *
  408. * @note This API can be called at any time when an interrupt non-blocking transfer initiates
  409. * to abort the transfer early.
  410. *
  411. * @param base Pointer to FLEXIO_I2C_Type structure
  412. * @param handle Pointer to flexio_i2c_master_handle_t structure which stores the transfer state
  413. */
  414. void FLEXIO_I2C_MasterTransferAbort(FLEXIO_I2C_Type *base, flexio_i2c_master_handle_t *handle);
  415. /*!
  416. * @brief Master interrupt handler.
  417. *
  418. * @param i2cType Pointer to FLEXIO_I2C_Type structure
  419. * @param i2cHandle Pointer to flexio_i2c_master_transfer_t structure
  420. */
  421. void FLEXIO_I2C_MasterTransferHandleIRQ(void *i2cType, void *i2cHandle);
  422. /*@}*/
  423. #if defined(__cplusplus)
  424. }
  425. #endif /*_cplusplus*/
  426. /*@}*/
  427. #endif /*_FSL_FLEXIO_I2C_MASTER_H_*/