fsl_flexio_i2c_master.h 18 KB

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