imx_i2c.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2011-2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  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 Freescale Semiconductor, Inc. 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 __IMX_I2C_H__
  31. #define __IMX_I2C_H__
  32. #include "sdk_types.h"
  33. //! @addtogroup diag_i2c
  34. //! @{
  35. ////////////////////////////////////////////////////////////////////////////////
  36. // Definitions
  37. ////////////////////////////////////////////////////////////////////////////////
  38. //! @brief Read/write address bits
  39. //!
  40. //! Bit 0 of the i2c device address cycle to indicate r/w. 0 is for write, 1 is for read.
  41. enum _i2c_rq {
  42. I2C_WRITE = 0,
  43. I2C_READ = 1
  44. };
  45. //! @brief I2C Error Codes
  46. enum _i2c_err {
  47. ERR_TX = -1,
  48. ERR_RX = -2,
  49. ERR_ARB_LOST = -3,
  50. ERR_NO_ACK = -4,
  51. ERR_XFER = -5,
  52. ERR_RX_ACK = -6,
  53. ERR_NO_ACK_ON_START = -7,
  54. ERR_INVALID_REQUEST = -8
  55. };
  56. //! Default slave address used for the MX6.
  57. enum _i2c_slave_id {
  58. IMX6_DEFAULT_SLAVE_ID = 0x60
  59. };
  60. //! @brief Info required to talk to an I2C device.
  61. //!
  62. //! Pairs an I2C port number with a device address.
  63. //!
  64. //! While the device address is often fixed and known in advance by the driver,
  65. //! some devices have configurable addresses that can be changed with pin
  66. //! settings. Thus, the same device may have different adresses on different
  67. //! boards depending on how these pins are tied.
  68. //!
  69. //! Note that the @a address member's value is @i not pre-shifted. The 7-bit
  70. //! address is right aligned within the byte, and the top bit is always set to 0.
  71. typedef struct i2c_device_info {
  72. uint8_t port; //!< I2C controller instance to which the device is connected. Starts at 1.
  73. uint8_t address; //!< I2C device address in lower 7 bits.
  74. uint32_t freq; //!< Maximum transfer speed in bits per second.
  75. } i2c_device_info_t;
  76. /*!
  77. * @brief An I2C transfer descriptor.
  78. *
  79. * To perform an I2C transfer, the caller first fills in an instance of this struct. Then
  80. * i2c_xfer() is called, passing a pointer to the #imx_i2c_request_t struct.
  81. *
  82. * @a ctl_addr should be set to either a valid controller instance number from 1 through
  83. * the number of I2C instances on the chip, or the base address of the controller.
  84. *
  85. * If @a device is set to a non-NULL value, it is a pointer to an #i2c_device_info_t struct
  86. * to use instead of the @a ctl_addr and @a dev_addr members of this struct.
  87. */
  88. typedef struct imx_i2c_request {
  89. uint32_t ctl_addr; //!< Either the I2C controller base address or instance number starting at 1.
  90. uint32_t dev_addr; //!< The I2C device address.
  91. uint32_t reg_addr; //!< The register address within the target device.
  92. uint32_t reg_addr_sz; //!< Number of bytes for the address of I2C device register.
  93. uint8_t *buffer; //!< Buffer to hold the data.
  94. uint32_t buffer_sz; //!< The number of bytes for read/write.
  95. int32_t (*slave_receive) (const struct imx_i2c_request *rq); //!< Function for slave to receive data from master.
  96. int32_t (*slave_transmit) (const struct imx_i2c_request *rq); //!< Function for slave to transmit data to master.
  97. const i2c_device_info_t * device; //!< Optional pointer to device info struct. Overrides @a ctl_addr and @a dev_addr if set.
  98. } imx_i2c_request_t;
  99. ////////////////////////////////////////////////////////////////////////////////
  100. // API
  101. ////////////////////////////////////////////////////////////////////////////////
  102. #if defined(__cplusplus)
  103. extern "C" {
  104. #endif
  105. /*!
  106. * @brief Initialize the I2C module
  107. *
  108. * Mainly enable the I2C clock, module itself and the I2C clock prescaler.
  109. *
  110. * @param base Either the base address of I2C module or the module's instance number. (also assigned for I2Cx_CLK)
  111. * @param baud The desired data rate in bits per second.
  112. *
  113. * @return 0 if successful; non-zero otherwise
  114. */
  115. int i2c_init(uint32_t base, uint32_t baud);
  116. /*!
  117. * @brief Perform a single I2C transfer in the selected direction.
  118. *
  119. * This is a rather simple function that can be used for most I2C devices.
  120. *
  121. * Common steps for both READ and WRITE:
  122. * - step 1: issue start signal
  123. * - step 2: put I2C device addr on the bus (always 1 byte write. the dir always I2C_WRITE)
  124. * - step 3: offset of the I2C device write (offset within the device. can be 1-4 bytes)
  125. *
  126. * For READ:
  127. * - step 4: do repeat-start
  128. * - step 5: send slave address again, but indicate a READ operation by setting LSB bit
  129. * - Step 6: change to receive mode
  130. * - Step 7: dummy read
  131. * - Step 8: reading
  132. *
  133. * For WRITE:
  134. * - Step 4: do data write
  135. * - Step 5: generate STOP by clearing MSTA bit
  136. *
  137. * @param rq Pointer to #imx_i2c_request_t.
  138. * @param dir #I2C_READ or #I2C_WRITE
  139. *
  140. * @return 0 on success; non-zero otherwise
  141. */
  142. int i2c_xfer(const imx_i2c_request_t *rq, int dir);
  143. /*!
  144. * @brief Perform I2C read transfer.
  145. *
  146. * @param rq Pointer to #imx_i2c_request_t.
  147. */
  148. int i2c_read(const imx_i2c_request_t *rq);
  149. /*!
  150. * @brief Perform I2C write transfer.
  151. *
  152. * @param rq Pointer to #imx_i2c_request_t.
  153. */
  154. int i2c_write(const imx_i2c_request_t *rq);
  155. /*!
  156. * @brief I2C handler for the slave mode.
  157. *
  158. * The function is based on the flow chart for typical I2C polling routine described in the
  159. * I2C controller chapter of the reference manual.
  160. *
  161. * @param rq Pointer to #imx_i2c_request_t.
  162. */
  163. void i2c_slave_handler(const imx_i2c_request_t *rq);
  164. /*!
  165. * @brief Handle the I2C transfers in slave mode.
  166. *
  167. * The slave mode behaves like any device with g_addr_cycle of address + g_data_cycle of data.
  168. * Master read =
  169. * START - SLAVE_ID/W - ACK - MEM_ADDR - ACK - START - SLAVE_ID/R - ACK - DATAx - NACK - STOP
  170. *
  171. * Example for a 16-bit address access:
  172. * - 1st IRQ - receive the slave address and Write flag from master.
  173. * - 2nd IRQ - receive the lower byte of the requested 16-bit address.
  174. * - 3rd IRQ - receive the higher byte of the requested 16-bit address.
  175. * - 4th IRQ - receive the slave address and Read flag from master.
  176. * - 5th and next IRQ - transmit the data as long as NACK and STOP is not asserted.
  177. *
  178. * Master write =
  179. * START - SLAVE_ID/W - ACK - MEM_ADDR - ACK - DATAx - NACK - STOP
  180. *
  181. * - 1st IRQ - receive the slave address and Write flag from master.
  182. * - 2nd IRQ - receive the lower byte of the requested 16-bit address.
  183. * - 3rd IRQ - receive the higher byte of the requested 16-bit address.
  184. * - 4th and next IRQ - receive the data as long STOP is not asserted.
  185. *
  186. * @param port Pointer to the I2C module structure.
  187. * @param rq Pointer to #imx_i2c_request_t.
  188. */
  189. void i2c_slave_xfer(imx_i2c_request_t *rq);
  190. ////////////////////////////////////////////////////////////////////////////////
  191. // Board support
  192. ////////////////////////////////////////////////////////////////////////////////
  193. //! @name Board support functions
  194. //!
  195. //! These functions are called by the driver in order to factor out board
  196. //! specific functionality. They must be defined by the board support
  197. //! library or the application.
  198. //@{
  199. //! @brief Configure IOMUX for the I2C driver.
  200. void i2c_iomux_config(int instance);
  201. //@}
  202. #if defined(__cplusplus)
  203. }
  204. #endif
  205. //! @}
  206. #endif /* __IMX_I2C_H__ */
  207. ////////////////////////////////////////////////////////////////////////////////
  208. // EOF
  209. ////////////////////////////////////////////////////////////////////////////////