i2c.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * @file i2c.h
  3. * @brief Inter-integrated circuit (I2C) communications interface driver.
  4. */
  5. /* ****************************************************************************
  6. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  22. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Except as contained in this notice, the name of Maxim Integrated
  27. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  28. * Products, Inc. Branding Policy.
  29. *
  30. * The mere transfer of this software does not imply any licenses
  31. * of trade secrets, proprietary technology, copyrights, patents,
  32. * trademarks, maskwork rights, or any other form of intellectual
  33. * property whatsoever. Maxim Integrated Products, Inc. retains all
  34. * ownership rights.
  35. *
  36. * $Date: 2019-06-28 09:42:42 -0500 (Fri, 28 Jun 2019) $
  37. * $Revision: 44330 $
  38. *
  39. *************************************************************************** */
  40. #ifndef _I2C_H_
  41. #define _I2C_H_
  42. #include <stdint.h>
  43. #include "i2c_regs.h"
  44. #include "mxc_sys.h"
  45. /**
  46. * @defgroup i2c I2C
  47. * @ingroup periphlibs
  48. * @{
  49. */
  50. /***** Definitions *****/
  51. /// @brief I2C Speed Modes
  52. typedef enum {
  53. I2C_STD_MODE = 100000, //!< 100KHz Bus Speed
  54. I2C_FAST_MODE = 400000, //!< 400KHz Bus Speed
  55. I2C_FASTPLUS_MODE = 1000000, //!< 1MHz Bus Speed
  56. I2C_HS_MODE = 3400000 //!< 3.4MHz Bus Speed
  57. } i2c_speed_t;
  58. //State for Master
  59. typedef enum {
  60. I2C_STATE_READING = 0,
  61. I2C_STATE_WRITING = 1
  62. } i2c_state_t;
  63. // @brief Enable/Disable TXFIFO Autoflush mode
  64. typedef enum {
  65. I2C_AUTOFLUSH_ENABLE = 0,
  66. I2C_AUTOFLUSH_DISABLE = 1
  67. } i2c_autoflush_disable_t;
  68. // @brief I2C Transaction request.
  69. typedef struct i2c_req i2c_req_t;
  70. struct i2c_req {
  71. uint8_t addr; /**< @parblock I2C 7-bit Address left aligned, bit 7 to bit 1.
  72. * Only supports 7-bit addressing. LSb of the given address
  73. * will be used as the read/write bit, the @p addr <b>will
  74. * not be shifted</b>. Used for <em>both master</em> and
  75. * @em slave transactions. @endparblock
  76. */
  77. const uint8_t *tx_data; ///< Data for mater write/slave read.
  78. uint8_t *rx_data; ///< Data for master read/slave write.
  79. unsigned tx_len; ///< Length of tx data.
  80. unsigned rx_len; ///< Length of rx.
  81. unsigned tx_num; ///< Number of tx bytes sent.
  82. unsigned rx_num; ///< Number of rx bytes sent.
  83. i2c_state_t state; ///< Read or Write.
  84. /**
  85. * @details 0 to send a stop bit at the end of the transaction,
  86. otherwise send a restart. Only used in master trasnactions.
  87. */
  88. int restart; /**< @parblock Restart or stop bit indicator.
  89. * @arg 0 to send a stop bit at the end of the transaction
  90. * @arg Non-zero to send a restart at end of the transaction
  91. * @note Only used for Master transactions.
  92. * @endparblock
  93. */
  94. i2c_autoflush_disable_t sw_autoflush_disable; ///< Enable/Disable autoflush.
  95. /**
  96. * @brief Callback for asynchronous request.
  97. * @param i2c_req_t* Pointer to the transaction request.
  98. * @param int Error code.
  99. */
  100. void (*callback)(i2c_req_t*, int);
  101. };
  102. /***** Function Prototypes *****/
  103. /**
  104. * @brief Initialize and enable I2C.
  105. * @param i2c Pointer to I2C peripheral registers.
  106. * @param i2cspeed desired speed (I2C mode)
  107. * @param sys_cfg System configuration object
  108. * @returns \c #E_NO_ERROR if everything is successful,
  109. * @ref MXC_Error_Codes if an error occurred.
  110. */
  111. int I2C_Init(mxc_i2c_regs_t * i2c, i2c_speed_t i2cspeed, const sys_cfg_i2c_t* sys_cfg);
  112. /**
  113. * @brief Shutdown I2C module.
  114. * @param i2c Pointer to the I2C registers.
  115. * @returns #E_NO_ERROR I2C shutdown successfully, @ref MXC_Error_Codes "error" if
  116. * unsuccessful.
  117. */
  118. int I2C_Shutdown(mxc_i2c_regs_t *i2c);
  119. /**
  120. * @brief Master write data. Will block until transaction is complete.
  121. * @param i2c Pointer to I2C regs.
  122. * @param addr @parblock I2C 7-bit Address left aligned, bit 7 to bit 1.
  123. * Only supports 7-bit addressing. LSb of the given address
  124. * will be used as the read/write bit, the \p addr <b>will
  125. * not be shifted</b>. Used for <em>both master</em> and
  126. * @em slave transactions. @endparblock
  127. * @param data Data to be written.
  128. * @param len Number of bytes to Write.
  129. * @param restart 0 to send a stop bit at the end of the transaction,
  130. otherwise send a restart.
  131. * @returns Bytes transacted if everything is successful,
  132. * @ref MXC_Error_Codes if an error occurred.
  133. */
  134. int I2C_MasterWrite(mxc_i2c_regs_t *i2c, uint8_t addr, const uint8_t* data, int len, int restart);
  135. /**
  136. * @brief Master read data. Will block until transaction is complete.
  137. * @param i2c Pointer to I2C regs.
  138. * @param addr @parblock I2C 7-bit Address left aligned, bit 7 to bit 1.
  139. * Only supports 7-bit addressing. LSb of the given address
  140. * will be used as the read/write bit, the @p addr <b>will
  141. * not be shifted</b>. Used for <em>both master</em> and
  142. * @em slave transactions. @endparblock
  143. * @param data Data to be written.
  144. * @param len Number of bytes to Write.
  145. * @param restart 0 to send a stop bit at the end of the transaction,
  146. otherwise send a restart.
  147. * @returns Bytes transacted if everything is successful, @ref MXC_Error_Codes if an error occurred.
  148. */
  149. int I2C_MasterRead(mxc_i2c_regs_t *i2c, uint8_t addr, uint8_t* data, int len, int restart);
  150. /**
  151. * @brief Slave read data. Will block until transaction is complete.
  152. * @param i2c Pointer to I2C regs.
  153. * @param addr @parblock I2C 7-bit Address left aligned, bit 7 to bit 1.
  154. * Only supports 7-bit addressing. LSb of the given address
  155. * will be used as the read/write bit, the @p addr <b>will
  156. * not be shifted</b>. Used for <em>both master</em> and
  157. * @em slave transactions. @endparblock
  158. * @param read_data Buffer that the master will read from.
  159. * @param read_len Number of bytes the master can read.
  160. * @param write_data Buffer that the master will write to.
  161. * @param write_len Number of bytes the master can write.
  162. * @param tx_num Number of bytes transmitted by the slave.
  163. * @param rx_num Number of bytes received by the slave.
  164. * @param sw_autoflush_disable TX Autoflush enabled by default.Set this bit to disable autoflush manually.
  165. * @returns #E_NO_ERROR if everything is successful, @ref MXC_Error_Codes if an error occurred.
  166. */
  167. int I2C_Slave(mxc_i2c_regs_t *i2c, uint8_t addr, const uint8_t* read_data,
  168. int read_len, uint8_t* write_data, int write_len, int* tx_num,
  169. int* rx_num, i2c_autoflush_disable_t sw_autoflush_disable);
  170. /**
  171. * @brief Master Read and Write Asynchronous.
  172. * @param i2c Pointer to I2C regs.
  173. * @param req Request for an I2C transaction.
  174. * @returns #E_NO_ERROR if everything is successful, @ref MXC_Error_Codes if an error occurred.
  175. */
  176. int I2C_MasterAsync(mxc_i2c_regs_t *i2c, i2c_req_t *req);
  177. /**
  178. * @brief Slave Read and Write Asynchronous.
  179. * @param i2c Pointer to I2C regs.
  180. * @param req Request for an I2C transaction.
  181. * @returns #E_NO_ERROR if everything is successful, @ref MXC_Error_Codes if an error occurred.
  182. */
  183. int I2C_SlaveAsync(mxc_i2c_regs_t *i2c, i2c_req_t *req);
  184. /**
  185. * @brief I2C interrupt handler.
  186. * @details This function should be called by the application from the interrupt
  187. * handler if I2C interrupts are enabled. Alternately, this function
  188. * can be periodically called by the application if I2C interrupts are
  189. * disabled.
  190. * @param i2c Base address of the I2C module.
  191. */
  192. void I2C_Handler(mxc_i2c_regs_t *i2c);
  193. /**
  194. * @brief Drain all of the data in the RXFIFO.
  195. * @param i2c Pointer to I2C regs.
  196. */
  197. void I2C_DrainRX(mxc_i2c_regs_t *i2c);
  198. /**
  199. * @brief Drain all of the data in the TXFIFO.
  200. * @param i2c Pointer to I2C regs.
  201. */
  202. void I2C_DrainTX(mxc_i2c_regs_t *i2c);
  203. /**
  204. * @brief Abort Async request based on the request you want to abort.
  205. * @param req Pointer to I2C Transaction.
  206. */
  207. int I2C_AbortAsync(i2c_req_t *req);
  208. /**
  209. * @brief Enable and Set Timeout
  210. *
  211. * @param i2c pointer to I2C regs
  212. * @param[in] us micro seconds to delay
  213. *
  214. * @return E_NO_ERROR or E_BAD_PARAM if delay is to long.
  215. */
  216. int I2C_SetTimeout(mxc_i2c_regs_t *i2c, int us);
  217. /**
  218. * @brief clear and disable timeout
  219. *
  220. * @param i2c pointer to I2C regs
  221. */
  222. void I2C_ClearTimeout(mxc_i2c_regs_t *i2c);
  223. /**@} end of group i2c */
  224. #endif /* _I2C_H_ */