Naze32 clone with Frysky receiver
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

I2C.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.h"
  19. #if DEVICE_I2C
  20. #include "i2c_api.h"
  21. #if DEVICE_I2C_ASYNCH
  22. #include "CThunk.h"
  23. #include "dma_api.h"
  24. #include "FunctionPointer.h"
  25. #endif
  26. namespace mbed {
  27. /** An I2C Master, used for communicating with I2C slave devices
  28. *
  29. * Example:
  30. * @code
  31. * // Read from I2C slave at address 0x62
  32. *
  33. * #include "mbed.h"
  34. *
  35. * I2C i2c(p28, p27);
  36. *
  37. * int main() {
  38. * int address = 0x62;
  39. * char data[2];
  40. * i2c.read(address, data, 2);
  41. * }
  42. * @endcode
  43. */
  44. class I2C {
  45. public:
  46. enum RxStatus {
  47. NoData,
  48. MasterGeneralCall,
  49. MasterWrite,
  50. MasterRead
  51. };
  52. enum Acknowledge {
  53. NoACK = 0,
  54. ACK = 1
  55. };
  56. /** Create an I2C Master interface, connected to the specified pins
  57. *
  58. * @param sda I2C data line pin
  59. * @param scl I2C clock line pin
  60. */
  61. I2C(PinName sda, PinName scl);
  62. /** Set the frequency of the I2C interface
  63. *
  64. * @param hz The bus frequency in hertz
  65. */
  66. void frequency(int hz);
  67. /** Read from an I2C slave
  68. *
  69. * Performs a complete read transaction. The bottom bit of
  70. * the address is forced to 1 to indicate a read.
  71. *
  72. * @param address 8-bit I2C slave address [ addr | 1 ]
  73. * @param data Pointer to the byte-array to read data in to
  74. * @param length Number of bytes to read
  75. * @param repeated Repeated start, true - don't send stop at end
  76. *
  77. * @returns
  78. * 0 on success (ack),
  79. * non-0 on failure (nack)
  80. */
  81. int read(int address, char *data, int length, bool repeated = false);
  82. /** Read a single byte from the I2C bus
  83. *
  84. * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
  85. *
  86. * @returns
  87. * the byte read
  88. */
  89. int read(int ack);
  90. /** Write to an I2C slave
  91. *
  92. * Performs a complete write transaction. The bottom bit of
  93. * the address is forced to 0 to indicate a write.
  94. *
  95. * @param address 8-bit I2C slave address [ addr | 0 ]
  96. * @param data Pointer to the byte-array data to send
  97. * @param length Number of bytes to send
  98. * @param repeated Repeated start, true - do not send stop at end
  99. *
  100. * @returns
  101. * 0 on success (ack),
  102. * non-0 on failure (nack)
  103. */
  104. int write(int address, const char *data, int length, bool repeated = false);
  105. /** Write single byte out on the I2C bus
  106. *
  107. * @param data data to write out on bus
  108. *
  109. * @returns
  110. * '1' if an ACK was received,
  111. * '0' otherwise
  112. */
  113. int write(int data);
  114. /** Creates a start condition on the I2C bus
  115. */
  116. void start(void);
  117. /** Creates a stop condition on the I2C bus
  118. */
  119. void stop(void);
  120. #if DEVICE_I2C_ASYNCH
  121. /** Start non-blocking I2C transfer.
  122. *
  123. * @param address 8/10 bit I2c slave address
  124. * @param tx_buffer The TX buffer with data to be transfered
  125. * @param tx_length The length of TX buffer in bytes
  126. * @param rx_buffer The RX buffer which is used for received data
  127. * @param rx_length The length of RX buffer in bytes
  128. * @param event The logical OR of events to modify
  129. * @param callback The event callback function
  130. * @param repeated Repeated start, true - do not send stop at end
  131. * @return Zero if the transfer has started, or -1 if I2C peripheral is busy
  132. */
  133. 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);
  134. /** Abort the on-going I2C transfer
  135. */
  136. void abort_transfer();
  137. protected:
  138. void irq_handler_asynch(void);
  139. event_callback_t _callback;
  140. CThunk<I2C> _irq;
  141. DMAUsage _usage;
  142. #endif
  143. protected:
  144. void aquire();
  145. i2c_t _i2c;
  146. static I2C *_owner;
  147. int _hz;
  148. };
  149. } // namespace mbed
  150. #endif
  151. #endif