I2C.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2015 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_I2C_H
  17. #define MBED_I2C_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_I2C) || defined(DOXYGEN_ONLY)
  20. #include "hal/i2c_api.h"
  21. #include "platform/SingletonPtr.h"
  22. #include "platform/PlatformMutex.h"
  23. #include "platform/NonCopyable.h"
  24. #if DEVICE_I2C_ASYNCH
  25. #include "platform/CThunk.h"
  26. #include "hal/dma_api.h"
  27. #include "platform/FunctionPointer.h"
  28. #endif
  29. namespace mbed {
  30. /** \addtogroup drivers */
  31. /** An I2C Master, used for communicating with I2C slave devices
  32. *
  33. * @note Synchronization level: Thread safe
  34. *
  35. * Example:
  36. * @code
  37. * // Read from I2C slave at address 0x62
  38. *
  39. * #include "mbed.h"
  40. *
  41. * I2C i2c(p28, p27);
  42. *
  43. * int main() {
  44. * int address = 0x62;
  45. * char data[2];
  46. * i2c.read(address, data, 2);
  47. * }
  48. * @endcode
  49. * @ingroup drivers
  50. */
  51. class I2C : private NonCopyable<I2C> {
  52. public:
  53. enum RxStatus {
  54. NoData,
  55. MasterGeneralCall,
  56. MasterWrite,
  57. MasterRead
  58. };
  59. enum Acknowledge {
  60. NoACK = 0,
  61. ACK = 1
  62. };
  63. /** Create an I2C Master interface, connected to the specified pins
  64. *
  65. * @param sda I2C data line pin
  66. * @param scl I2C clock line pin
  67. */
  68. I2C(PinName sda, PinName scl);
  69. /** Set the frequency of the I2C interface
  70. *
  71. * @param hz The bus frequency in hertz
  72. */
  73. void frequency(int hz);
  74. /** Read from an I2C slave
  75. *
  76. * Performs a complete read transaction. The bottom bit of
  77. * the address is forced to 1 to indicate a read.
  78. *
  79. * @param address 8-bit I2C slave address [ addr | 1 ]
  80. * @param data Pointer to the byte-array to read data in to
  81. * @param length Number of bytes to read
  82. * @param repeated Repeated start, true - don't send stop at end
  83. *
  84. * @returns
  85. * 0 on success (ack),
  86. * non-0 on failure (nack)
  87. */
  88. int read(int address, char *data, int length, bool repeated = false);
  89. /** Read a single byte from the I2C bus
  90. *
  91. * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
  92. *
  93. * @returns
  94. * the byte read
  95. */
  96. int read(int ack);
  97. /** Write to an I2C slave
  98. *
  99. * Performs a complete write transaction. The bottom bit of
  100. * the address is forced to 0 to indicate a write.
  101. *
  102. * @param address 8-bit I2C slave address [ addr | 0 ]
  103. * @param data Pointer to the byte-array data to send
  104. * @param length Number of bytes to send
  105. * @param repeated Repeated start, true - do not send stop at end
  106. *
  107. * @returns
  108. * 0 on success (ack),
  109. * non-0 on failure (nack)
  110. */
  111. int write(int address, const char *data, int length, bool repeated = false);
  112. /** Write single byte out on the I2C bus
  113. *
  114. * @param data data to write out on bus
  115. *
  116. * @returns
  117. * '0' - NAK was received
  118. * '1' - ACK was received,
  119. * '2' - timeout
  120. */
  121. int write(int data);
  122. /** Creates a start condition on the I2C bus
  123. */
  124. void start(void);
  125. /** Creates a stop condition on the I2C bus
  126. */
  127. void stop(void);
  128. /** Acquire exclusive access to this I2C bus
  129. */
  130. virtual void lock(void);
  131. /** Release exclusive access to this I2C bus
  132. */
  133. virtual void unlock(void);
  134. virtual ~I2C()
  135. {
  136. // Do nothing
  137. }
  138. #if DEVICE_I2C_ASYNCH
  139. /** Start non-blocking I2C transfer.
  140. *
  141. * This function locks the deep sleep until any event has occurred
  142. *
  143. * @param address 8/10 bit I2C slave address
  144. * @param tx_buffer The TX buffer with data to be transfered
  145. * @param tx_length The length of TX buffer in bytes
  146. * @param rx_buffer The RX buffer which is used for received data
  147. * @param rx_length The length of RX buffer in bytes
  148. * @param event The logical OR of events to modify
  149. * @param callback The event callback function
  150. * @param repeated Repeated start, true - do not send stop at end
  151. * @return Zero if the transfer has started, or -1 if I2C peripheral is busy
  152. */
  153. int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
  154. /** Abort the on-going I2C transfer
  155. */
  156. void abort_transfer();
  157. protected:
  158. /** Lock deep sleep only if it is not yet locked */
  159. void lock_deep_sleep();
  160. /** Unlock deep sleep only if it has been locked */
  161. void unlock_deep_sleep();
  162. void irq_handler_asynch(void);
  163. event_callback_t _callback;
  164. CThunk<I2C> _irq;
  165. DMAUsage _usage;
  166. bool _deep_sleep_locked;
  167. #endif
  168. protected:
  169. void aquire();
  170. i2c_t _i2c;
  171. static I2C *_owner;
  172. int _hz;
  173. static SingletonPtr<PlatformMutex> _mutex;
  174. };
  175. } // namespace mbed
  176. #endif
  177. #endif