i2c_master.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /****************************************************************************
  2. * include/nuttx/i2c/i2c_master.h
  3. *
  4. * Copyright(C) 2009-2012, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __INCLUDE_NUTTX_I2C_I2C_MASTER_H
  36. #define __INCLUDE_NUTTX_I2C_I2C_MASTER_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <sys/types.h>
  42. #include <stdint.h>
  43. #include <nuttx/fs/ioctl.h>
  44. /****************************************************************************
  45. * Pre-processor Definitions
  46. ****************************************************************************/
  47. /* I2C address calculation. Convert 7- and 10-bit address to 8-bit and
  48. * 16-bit read/write address
  49. */
  50. #define I2C_READBIT 0x01
  51. /* Convert 7- to 8-bit address */
  52. #define I2C_ADDR8(a) ((a) << 1)
  53. #define I2C_WRITEADDR8(a) I2C_ADDR8(a)
  54. #define I2C_READADDR8(a) (I2C_ADDR8(a) | I2C_READBIT)
  55. /* Convert 10- to 16-bit address */
  56. #define I2C_ADDR10H(a) (0xf0 | (((a) >> 7) & 0x06))
  57. #define I2C_ADDR10L(a) ((a) & 0xff)
  58. #define I2C_WRITEADDR10H(a) I2C_ADDR10H(a)
  59. #define I2C_WRITEADDR10L(a) I2C_ADDR10L(a)
  60. #define I2C_READADDR10H(a) (I2C_ADDR10H(a) | I2C_READBIT)
  61. #define I2C_READADDR10L(a) I2C_ADDR10L(a)
  62. /* Bit definitions for the flags field in struct i2c_msg_s
  63. *
  64. * START/STOP Rules:
  65. *
  66. * 1. The lower half I2C driver will always issue the START condition at the
  67. * beginning of a message unless I2C_M_NOSTART flag is set in the
  68. * message.
  69. *
  70. * 2. The lower half I2C driver will always issue the STOP condition at the
  71. * end of the messages unless:
  72. *
  73. * a. The I2C_M_NOSTOP flag is set in the message, OR
  74. * b. The following message has the I2C_M_NOSTART flag set (meaning
  75. * that following message is simply a continuation of the transfer).
  76. *
  77. * A proper I2C repeated start would then have I2C_M_NOSTOP set on msg[n]
  78. * and I2C_M_NOSTART *not* set on msg[n+1]. See the following table:
  79. *
  80. * msg[n].flags msg[n+1].flags Behavior
  81. * ------------ --------------- -----------------------------------------
  82. * 0 0 Two normal, separate messages with STOP
  83. * on msg[n] then START on msg[n+1]
  84. * 0* I2C_M_NOSTART Continuation of the same transfer (must
  85. * be the same direction). See NOTE below.
  86. * NO_STOP 0 No STOP on msg[n]; repeated START on
  87. * msg[n+1].
  88. *
  89. * * NOTE: NO_STOP is implied in this case and may or not be explicitly
  90. * included in the msg[n] flags
  91. */
  92. #define I2C_M_READ 0x0001 /* Read data, from slave to master */
  93. #define I2C_M_TEN 0x0002 /* Ten bit address */
  94. #define I2C_M_NOSTOP 0x0040 /* Message should not end with a STOP */
  95. #define I2C_M_NOSTART 0x0080 /* Message should not begin with a START */
  96. /* I2c bus speed */
  97. #define I2C_SPEED_STANDARD 100000 /* Standard speed (100Khz) */
  98. #define I2C_SPEED_FAST 400000 /* Fast speed (400Khz) */
  99. #define I2C_SPEED_FAST_PLUS 1000000 /* Fast+ speed ( 1Mhz) */
  100. #define I2C_SPEED_HIGH 3400000 /* High speed (3.4Mhz) */
  101. /* I2C Character Driver IOCTL Commands **************************************/
  102. /* The I2C driver is intended to support application testing of the I2C bus.
  103. * The I2C driver simply wraps an instance of struct i2c_dev_s and then
  104. * provides the following IOCTL commands to access each method of the I2c
  105. * interface.
  106. */
  107. /* Command: I2CIOC_TRANSFER
  108. * Description: Perform an I2C transfer
  109. * Argument: A reference to an instance of struct i2c_transfer_s.
  110. * Dependencies: CONFIG_I2C_DRIVER
  111. */
  112. #define I2CIOC_TRANSFER _I2CIOC(0x0001)
  113. /* Command: I2CIOC_RESET
  114. * Description: Perform an I2C bus reset in an attempt to break loose stuck
  115. * I2C devices.
  116. * Argument: None
  117. * Dependencies: CONFIG_I2C_DRIVER && CONFIG_I2C_RESET
  118. */
  119. #define I2CIOC_RESET _I2CIOC(0x0002)
  120. /* Access macros ************************************************************/
  121. /****************************************************************************
  122. * Name: I2C_TRANSFER
  123. *
  124. * Description:
  125. * Perform a sequence of I2C transfers, each transfer is started with a
  126. * START and the final transfer is completed with a STOP. Each sequence
  127. * will be an 'atomic' operation in the sense that any other I2C actions
  128. * will be serialized and pend until this sequence of transfers completes.
  129. *
  130. * Input Parameters:
  131. * dev - Device-specific state data
  132. * msgs - A pointer to a set of message descriptors
  133. * count - The number of transfers to perform
  134. *
  135. * Returned Value:
  136. * Zero (OK) or positive on success; a negated errno value on failure.
  137. *
  138. * Note : some implementations of this interface return the number of
  139. * transfers completed, but others return OK on success.
  140. *
  141. ****************************************************************************/
  142. #define I2C_TRANSFER(d,m,c) ((d)->ops->transfer(d,m,c))
  143. /************************************************************************************
  144. * Name: I2C_RESET
  145. *
  146. * Description:
  147. * Perform an I2C bus reset in an attempt to break loose stuck I2C devices.
  148. *
  149. * Input Parameters:
  150. * dev - Device-specific state data
  151. *
  152. * Returned Value:
  153. * Zero (OK) on success; a negated errno value on failure.
  154. *
  155. ************************************************************************************/
  156. #ifdef CONFIG_I2C_RESET
  157. # define I2C_RESET(d) ((d)->ops->reset(d))
  158. #endif
  159. /****************************************************************************
  160. * Public Types
  161. ****************************************************************************/
  162. /* The I2C lower half driver interface */
  163. struct i2c_master_s;
  164. struct i2c_msg_s;
  165. struct i2c_ops_s
  166. {
  167. CODE int (*transfer)(FAR struct i2c_master_s *dev,
  168. FAR struct i2c_msg_s *msgs, int count);
  169. #ifdef CONFIG_I2C_RESET
  170. CODE int (*reset)(FAR struct i2c_master_s *dev);
  171. #endif
  172. };
  173. /* This structure contains the full state of I2C as needed for a specific
  174. * transfer. It is passed to I2C methods so that I2C transfer may be
  175. * performed in a thread safe manner.
  176. */
  177. struct i2c_config_s
  178. {
  179. uint32_t frequency; /* I2C frequency */
  180. uint16_t address; /* I2C address (7- or 10-bit) */
  181. uint8_t addrlen; /* I2C address length (7 or 10 bits) */
  182. };
  183. /* I2C transaction segment beginning with a START. A number of these can
  184. * be transferred together to form an arbitrary sequence of write/read transfer
  185. * to an I2C slave device.
  186. */
  187. struct i2c_msg_s
  188. {
  189. uint32_t frequency; /* I2C frequency */
  190. uint16_t addr; /* Slave address (7- or 10-bit) */
  191. uint16_t flags; /* See I2C_M_* definitions */
  192. FAR uint8_t *buffer; /* Buffer to be transferred */
  193. ssize_t length; /* Length of the buffer in bytes */
  194. };
  195. /* I2C private data. This structure only defines the initial fields of the
  196. * structure visible to the I2C client. The specific implementation may
  197. * add additional, device specific fields after the vtable.
  198. */
  199. struct i2c_master_s
  200. {
  201. FAR const struct i2c_ops_s *ops; /* I2C vtable */
  202. };
  203. /* This structure is used to communicate with the I2C character driver in
  204. * order to perform IOCTL transfers.
  205. */
  206. struct i2c_transfer_s
  207. {
  208. FAR struct i2c_msg_s *msgv; /* Array of I2C messages for the transfer */
  209. size_t msgc; /* Number of messages in the array. */
  210. };
  211. /****************************************************************************
  212. * Public Functions
  213. ****************************************************************************/
  214. #undef EXTERN
  215. #if defined(__cplusplus)
  216. #define EXTERN extern "C"
  217. extern "C"
  218. {
  219. #else
  220. #define EXTERN extern
  221. #endif
  222. /****************************************************************************
  223. * Name: i2c_register
  224. *
  225. * Description:
  226. * Create and register the I2C character driver.
  227. *
  228. * The I2C character driver is a simple character driver that supports I2C
  229. * transfers. The intent of this driver is to support I2C testing. It is
  230. * not suitable for use in any real driver application.
  231. *
  232. * Input Parameters:
  233. * i2c - An instance of the lower half I2C driver
  234. * bus - The I2C bus number. This will be used as the I2C device minor
  235. * number. The I2C character device will be registered as /dev/i2cN
  236. * where N is the minor number
  237. *
  238. * Returned Value:
  239. * OK if the driver was successfully register; A negated errno value is
  240. * returned on any failure.
  241. *
  242. ****************************************************************************/
  243. #ifdef CONFIG_I2C_DRIVER
  244. int i2c_register(FAR struct i2c_master_s *i2c, int bus);
  245. #endif
  246. /****************************************************************************
  247. * Name: i2c_writeread
  248. *
  249. * Description:
  250. * Send a block of data on I2C followed by restarted read access. This
  251. * provides a convenient wrapper to the transfer function.
  252. *
  253. * Input Parameters:
  254. * dev - Device-specific state data
  255. * config - Described the I2C configuration
  256. * wbuffer - A pointer to the read-only buffer of data to be written to device
  257. * wbuflen - The number of bytes to send from the buffer
  258. * rbuffer - A pointer to a buffer of data to receive the data from the device
  259. * rbuflen - The requested number of bytes to be read
  260. *
  261. * Returned Value:
  262. * 0: success, <0: A negated errno
  263. *
  264. ****************************************************************************/
  265. int i2c_writeread(FAR struct i2c_master_s *dev,
  266. FAR const struct i2c_config_s *config,
  267. FAR const uint8_t *wbuffer, int wbuflen,
  268. FAR uint8_t *rbuffer, int rbuflen);
  269. /****************************************************************************
  270. * Name: i2c_write
  271. *
  272. * Description:
  273. * Send a block of data on I2C. Each write operation will be an 'atomic'
  274. * operation in the sense that any other I2C actions will be serialized
  275. * and pend until this write completes.
  276. *
  277. * Input Parameters:
  278. * dev - Device-specific state data
  279. * config - Described the I2C configuration
  280. * buffer - A pointer to the read-only buffer of data to be written to device
  281. * buflen - The number of bytes to send from the buffer
  282. *
  283. * Returned Value:
  284. * 0: success, <0: A negated errno
  285. *
  286. ****************************************************************************/
  287. int i2c_write(FAR struct i2c_master_s *dev,
  288. FAR const struct i2c_config_s *config,
  289. FAR const uint8_t *buffer, int buflen);
  290. /****************************************************************************
  291. * Name: i2c_read
  292. *
  293. * Description:
  294. * Receive a block of data from I2C. Each read operation will be an
  295. * 'atomic' operation in the sense that any other I2C actions will be
  296. * serialized and pend until this read completes.
  297. *
  298. * Input Parameters:
  299. * dev - Device-specific state data
  300. * buffer - A pointer to a buffer of data to receive the data from the device
  301. * buflen - The requested number of bytes to be read
  302. *
  303. * Returned Value:
  304. * 0: success, <0: A negated errno
  305. *
  306. ****************************************************************************/
  307. int i2c_read(FAR struct i2c_master_s *dev,
  308. FAR const struct i2c_config_s *config,
  309. FAR uint8_t *buffer, int buflen);
  310. #undef EXTERN
  311. #if defined(__cplusplus)
  312. }
  313. #endif
  314. #endif /* __INCLUDE_NUTTX_I2C_I2C_MASTER_H */