i2cbus.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2012 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file dev/i2cbus.c
  36. * \brief I2C bus driver.
  37. *
  38. * \verbatim
  39. * $Id$
  40. * \endverbatim
  41. */
  42. #include <stdint.h>
  43. #include <errno.h>
  44. #include <sys/timer.h>
  45. #include <sys/event.h>
  46. #include <dev/i2cbus.h>
  47. /*!
  48. * \addtogroup xgI2cBus
  49. */
  50. /*@{*/
  51. /*!
  52. * \brief Register and initialize an I2C device attached to a specified bus.
  53. *
  54. * Calls the bus controller initialization.
  55. *
  56. * Applications should call this function during initialization for each
  57. * I2C slave device they intend to use.
  58. *
  59. * \param slave Pointer to the \ref NUTI2C_SLAVE structure, which is provided
  60. * by the I2C slave device driver.
  61. * \param bus Pointer to the \ref NUTI2C_BUS structure, which is provided
  62. * by the I2C bus driver.
  63. *
  64. * \return 0 on success, or -1 if the bus initialization failed.
  65. */
  66. int NutRegisterI2cSlave(NUTI2C_SLAVE *slave, NUTI2C_BUS *bus)
  67. {
  68. int rc = 0;
  69. /* Check if initialization is required. */
  70. if ((bus->bus_flags & I2C_BF_INITIALIZED) == 0) {
  71. rc = bus->bus_init(bus);
  72. if (rc == 0) {
  73. bus->bus_flags |= I2C_BF_INITIALIZED;
  74. /* Initialize mutex semaphore. */
  75. NutEventPost(&bus->bus_mutex);
  76. }
  77. }
  78. if (rc == 0) {
  79. slave->slave_bus = bus;
  80. }
  81. return rc;
  82. }
  83. /*!
  84. * \brief Set or get the clock rate.
  85. *
  86. * When setting a new value, then the caller must make sure that all
  87. * bus transfers had been completed before calling this function.
  88. *
  89. * \param bus Specifies the I2C bus.
  90. * \param rate New clock rate, given in bits per second. If the value is
  91. * I2C_CURRENT_RATE, then the current rate is kept. If it is
  92. * zero, then the default rate will be set.
  93. *
  94. * \return Old clock rate or I2C_CURRENT_RATE in case of an error.
  95. */
  96. long NutI2cBusRate(NUTI2C_BUS *bus, long rate)
  97. {
  98. long rc = bus->bus_rate;
  99. if (rate != I2C_CURRENT_RATE) {
  100. /* Configure a new rate. */
  101. bus->bus_rate = rate;
  102. if (bus->bus_configure(bus)) {
  103. /* If failed, restore the previous value. */
  104. bus->bus_rate = rc;
  105. rc = I2C_CURRENT_RATE;
  106. }
  107. }
  108. return rc;
  109. }
  110. /*!
  111. * \brief Set or get a bus access timeout value.
  112. *
  113. * \param bus Specifies the I2C bus.
  114. * \param tmo New timeout value, given in milliseconds. If the value is
  115. * I2C_CURRENT_TIMEOUT, then the current rate is kept. If it
  116. * is zero, then time monitoring is disabled and a data
  117. * transfer call may never return.
  118. *
  119. * \return Old timeout value.
  120. */
  121. uint32_t NutI2cBusTimeout(NUTI2C_BUS *bus, uint32_t tmo)
  122. {
  123. uint32_t rc = bus->bus_timeout;
  124. if (tmo != I2C_CURRENT_TIMEOUT) {
  125. bus->bus_timeout = tmo;
  126. }
  127. return rc;
  128. }
  129. /*!
  130. * \brief Scan bus for I2C slaves.
  131. *
  132. * If not initialized, the bus will be configured with the current
  133. * settings. To change these, applications may call NutI2cBusRate()
  134. * and NutI2cBusTimeout() prior to this call.
  135. *
  136. * \param bus Specifies the I2C bus.
  137. * \param first First slave address to probe.
  138. * \param last Last slave address to probe.
  139. *
  140. * \return First slave address found in the given range. If no slave was
  141. * detected, then I2C_SLA_NONE is returned.
  142. */
  143. int NutI2cBusScan(NUTI2C_BUS *bus, int first, int last)
  144. {
  145. uint_fast8_t initialized = (bus->bus_flags & I2C_BF_INITIALIZED) != 0;
  146. int e;
  147. /* If not initialized, try to configure the bus. */
  148. if (initialized || bus->bus_configure(bus) == 0) {
  149. while (first <= last) {
  150. /* Try to get mutex access to an initialized bus. */
  151. if (initialized && NutEventWait(&bus->bus_mutex, bus->bus_timeout)) {
  152. break;
  153. }
  154. /* Call low level bus probing. */
  155. e = bus->bus_probe(bus, first);
  156. /* Release mutex access to an initialized bus. */
  157. if (initialized) {
  158. NutEventPost(&bus->bus_mutex);
  159. }
  160. if (e == 0) {
  161. /* Slave found. */
  162. return first;
  163. }
  164. /* No slave, try next address. */
  165. first++;
  166. }
  167. }
  168. return I2C_SLA_NONE;
  169. }
  170. /*!
  171. * \brief Set or get a slave's I2C address.
  172. *
  173. * \param slave Specifies the slave device.
  174. * \param tmo New slave address. If the value is I2C_SLA_NONE, then
  175. * the current address is kept.
  176. *
  177. * \return Old slave address.
  178. */
  179. int NutI2cSlaveAddress(NUTI2C_SLAVE *slave, int sla)
  180. {
  181. int rc = slave->slave_address;
  182. if (sla != I2C_SLA_NONE) {
  183. slave->slave_address = sla;
  184. }
  185. return rc;
  186. }
  187. /*!
  188. * \brief Set or get a slave's timeout value.
  189. *
  190. * \param slave Specifies the slave device.
  191. * \param tmo New timeout value, given in milliseconds. If the value is
  192. * I2C_CURRENT_TIMEOUT, then the current rate is kept. If it
  193. * is zero, then time monitoring is disabled and a data
  194. * transfer call may never return.
  195. *
  196. * \return Old timeout value.
  197. */
  198. uint32_t NutI2cSlaveTimeout(NUTI2C_SLAVE *slave, uint32_t tmo)
  199. {
  200. uint32_t rc = slave->slave_timeout;
  201. if (tmo != I2C_CURRENT_TIMEOUT) {
  202. slave->slave_timeout = tmo;
  203. }
  204. return rc;
  205. }
  206. /*!
  207. * \brief Communicate with an I2C slave.
  208. *
  209. * \param slave Pointer to a slave device structure, which had been
  210. * registered by a previous call to NutRegisterI2cSlave().
  211. * \param wdat Points to the data to write. Ignored, if the number of
  212. * data bytes to write is zero.
  213. * \param wlen Number of data bytes to write to the device. If zero,
  214. * then the interface will only read data from the device.
  215. * However, this is quite unlikely. Most I2C slaves require
  216. * to write an internal register address before reading.
  217. * \param rdat Points to a buffer, where the received data will be
  218. * stored. Ignored, if the maximum number of bytes to
  219. * read is zero.
  220. * \param rsiz Maximum number of bytes to read from the device. Set
  221. * this to zero, if no bytes are expected from the slave.
  222. *
  223. * \return The number of bytes received, -1 in case of an error or timeout.
  224. */
  225. int NutI2cMasterTransceive(NUTI2C_SLAVE *slave, const void *wdat, int wlen, void *rdat, int rsiz)
  226. {
  227. int rc;
  228. NUTI2C_BUS *bus;
  229. /* Allocate the bus. */
  230. bus = slave->slave_bus;
  231. rc = NutEventWait(&bus->bus_mutex, bus->bus_timeout);
  232. if (rc) {
  233. errno = EIO;
  234. } else {
  235. NUTI2C_MSG msg;
  236. /* Setup a message structure to be passed to the low level driver. */
  237. msg.msg_wdat = (uint8_t *) wdat;
  238. msg.msg_wlen = wlen;
  239. msg.msg_widx = 0;
  240. msg.msg_rdat = (uint8_t *) rdat;
  241. msg.msg_rsiz = rsiz;
  242. msg.msg_ridx = 0;
  243. /* Call the low level driver. */
  244. (*bus->bus_transceive) (slave, &msg);
  245. /* Release the bus. */
  246. NutEventPost(&bus->bus_mutex);
  247. /* Return the number of bytes received. */
  248. rc = msg.msg_ridx;
  249. }
  250. return rc;
  251. }
  252. /*@}*/