fsl_flexio_i2c_master.h 18 KB

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