I2CSlave.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 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_SLAVE_H
  17. #define MBED_I2C_SLAVE_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_I2CSLAVE) || defined(DOXYGEN_ONLY)
  20. #include "hal/i2c_api.h"
  21. namespace mbed {
  22. /** \addtogroup drivers */
  23. /** An I2C Slave, used for communicating with an I2C Master device
  24. *
  25. * @note Synchronization level: Not protected
  26. *
  27. * Example:
  28. * @code
  29. * // Simple I2C responder
  30. * #include <mbed.h>
  31. *
  32. * I2CSlave slave(p9, p10);
  33. *
  34. * int main() {
  35. * char buf[10];
  36. * char msg[] = "Slave!";
  37. *
  38. * slave.address(0xA0);
  39. * while (1) {
  40. * int i = slave.receive();
  41. * switch (i) {
  42. * case I2CSlave::ReadAddressed:
  43. * slave.write(msg, strlen(msg) + 1); // Includes null char
  44. * break;
  45. * case I2CSlave::WriteGeneral:
  46. * slave.read(buf, 10);
  47. * printf("Read G: %s\n", buf);
  48. * break;
  49. * case I2CSlave::WriteAddressed:
  50. * slave.read(buf, 10);
  51. * printf("Read A: %s\n", buf);
  52. * break;
  53. * }
  54. * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
  55. * }
  56. * }
  57. * @endcode
  58. * @ingroup drivers
  59. */
  60. class I2CSlave {
  61. public:
  62. enum RxStatus {
  63. NoData = 0,
  64. ReadAddressed = 1,
  65. WriteGeneral = 2,
  66. WriteAddressed = 3
  67. };
  68. /** Create an I2C Slave interface, connected to the specified pins.
  69. *
  70. * @param sda I2C data line pin
  71. * @param scl I2C clock line pin
  72. */
  73. I2CSlave(PinName sda, PinName scl);
  74. /** Set the frequency of the I2C interface
  75. *
  76. * @param hz The bus frequency in hertz
  77. */
  78. void frequency(int hz);
  79. /** Checks to see if this I2C Slave has been addressed.
  80. *
  81. * @returns
  82. * A status indicating if the device has been addressed, and how
  83. * - NoData - the slave has not been addressed
  84. * - ReadAddressed - the master has requested a read from this slave
  85. * - WriteAddressed - the master is writing to this slave
  86. * - WriteGeneral - the master is writing to all slave
  87. */
  88. int receive(void);
  89. /** Read from an I2C master.
  90. *
  91. * @param data pointer to the byte array to read data in to
  92. * @param length maximum number of bytes to read
  93. *
  94. * @returns
  95. * 0 on success,
  96. * non-0 otherwise
  97. */
  98. int read(char *data, int length);
  99. /** Read a single byte from an I2C master.
  100. *
  101. * @returns
  102. * the byte read
  103. */
  104. int read(void);
  105. /** Write to an I2C master.
  106. *
  107. * @param data pointer to the byte array to be transmitted
  108. * @param length the number of bytes to transmite
  109. *
  110. * @returns
  111. * 0 on success,
  112. * non-0 otherwise
  113. */
  114. int write(const char *data, int length);
  115. /** Write a single byte to an I2C master.
  116. *
  117. * @param data the byte to write
  118. *
  119. * @returns
  120. * '1' if an ACK was received,
  121. * '0' otherwise
  122. */
  123. int write(int data);
  124. /** Sets the I2C slave address.
  125. *
  126. * @param address The address to set for the slave (ignoring the least
  127. * signifcant bit). If set to 0, the slave will only respond to the
  128. * general call address.
  129. */
  130. void address(int address);
  131. /** Reset the I2C slave back into the known ready receiving state.
  132. */
  133. void stop(void);
  134. protected:
  135. i2c_t _i2c;
  136. };
  137. } // namespace mbed
  138. #endif
  139. #endif