i2cbus.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef DEV_I2CBUS_H
  2. #define DEV_I2CBUS_H
  3. /*
  4. * Copyright (C) 2012 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software 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 LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file include/dev/i2cbus.h
  38. * \brief I2C bus declarations.
  39. *
  40. * \verbatim
  41. * $Id$
  42. * \endverbatim
  43. */
  44. #include <stdint.h>
  45. #include <sys/types.h>
  46. /*!
  47. * \addtogroup xgI2cBus
  48. */
  49. /*@{*/
  50. #define I2C_CURRENT_RATE -1L
  51. #define I2C_CURRENT_TIMEOUT ((uint32_t) -1)
  52. #define I2C_SLA_NONE -1
  53. #define I2C_BF_INITIALIZED 0x01
  54. /*!
  55. * \brief I2C bus structure type.
  56. */
  57. typedef struct _NUTI2C_BUS NUTI2C_BUS;
  58. /*!
  59. * \brief I2C slave structure type.
  60. */
  61. typedef struct _NUTI2C_SLAVE NUTI2C_SLAVE;
  62. /*!
  63. * \brief I2C message structure type.
  64. */
  65. typedef struct _NUTI2C_MSG NUTI2C_MSG;
  66. /*!
  67. * \brief I2C message structure.
  68. *
  69. * This structure is used by platform specific implementations of an I2C
  70. * bus driver, not by application code.
  71. *
  72. * While it is more convenient for an application to pass the data buffers
  73. * and related information as function parameters when calling
  74. * NutI2cMasterTransceive(), it is easier to implement the hardware specific
  75. * driver when all parameters are kept in a structure.
  76. */
  77. struct _NUTI2C_MSG {
  78. /*! \brief Data to write to slave. */
  79. const uint8_t *msg_wdat;
  80. /*! \brief Number of bytes to write to slave. */
  81. int msg_wlen;
  82. /*! \brief Number of bytes written. */
  83. volatile int msg_widx;
  84. /*! \brief Data to read from slave. */
  85. uint8_t *msg_rdat;
  86. /*! \brief Maximum number of bytes to read from slave. */
  87. int msg_rsiz;
  88. /*! \brief Number of bytes read. */
  89. volatile int msg_ridx;
  90. };
  91. /*!
  92. * \brief I2C bus structure.
  93. *
  94. * Each hardware specific bus driver offers a global variable of this
  95. * type, which applications must pass to NutRegisterI2cSlave() to
  96. * attach a specific device to a specific bus.
  97. */
  98. struct _NUTI2C_BUS {
  99. /*! \brief Private data of the hardware specific implementation. */
  100. void *bus_icb;
  101. /*! \brief Hardware initialization. */
  102. int (*bus_init)(NUTI2C_BUS *);
  103. /*! \brief Hardware configuration. */
  104. int (*bus_configure)(NUTI2C_BUS *);
  105. /*! \brief Hardware specific probe routine. */
  106. int (*bus_probe)(NUTI2C_BUS *, int sla);
  107. /*! \brief Hardware specific transfer routine. */
  108. int (*bus_transceive)(NUTI2C_SLAVE *, NUTI2C_MSG *);
  109. /*! \brief Bus access timeout. */
  110. uint32_t bus_timeout;
  111. /*! \brief Bus speed. */
  112. long bus_rate;
  113. /*! \brief Miscellaneous status flags, see I2C_BF_XXX. */
  114. uint_fast8_t bus_flags;
  115. /*! \brief Bus access queue. */
  116. HANDLE bus_mutex;
  117. };
  118. /*!
  119. * \brief I2C slave structure.
  120. *
  121. * Each hardware specific driver offers a global variable of this type,
  122. * which applications must pass to NutRegisterI2cSlave() to attach
  123. * a specific device to a specific bus.
  124. */
  125. struct _NUTI2C_SLAVE {
  126. /*! \brief Pointer to the bus driver. */
  127. NUTI2C_BUS *slave_bus;
  128. /*! \brief Device's 7 bit slave address. */
  129. int slave_address;
  130. /*! \brief Slave access timeout. */
  131. uint32_t slave_timeout;
  132. /*! \brief Transfer message. */
  133. NUTI2C_MSG *slave_msg;
  134. };
  135. /*@}*/
  136. extern int NutRegisterI2cSlave(NUTI2C_SLAVE *slave, NUTI2C_BUS *bus);
  137. extern long NutI2cBusRate(NUTI2C_BUS *bus, long rate);
  138. extern uint32_t NutI2cBusTimeout(NUTI2C_BUS *bus, uint32_t tmo);
  139. extern int NutI2cBusScan(NUTI2C_BUS *bus, int first, int last);
  140. extern int NutI2cSlaveAddress(NUTI2C_SLAVE *slave, int sla);
  141. extern uint32_t NutI2cSlaveTimeout(NUTI2C_SLAVE *slave, uint32_t tmo);
  142. extern int NutI2cMasterTransceive(NUTI2C_SLAVE *slave, const void *wdat, int wlen, void *rdat, int rsiz);
  143. #endif