Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

I2CSlave.h 4.0KB

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