Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

9 лет назад
9 лет назад
9 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. TwoWire.h - TWI/I2C library for Arduino & Wiring
  3. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
  16. */
  17. #ifndef TwoWire_h
  18. #define TwoWire_h
  19. #include <inttypes.h>
  20. #include "Arduino.h"
  21. #define BUFFER_LENGTH 32
  22. #define WIRE_HAS_END 1
  23. extern "C" void i2c0_isr(void);
  24. class TwoWire : public Stream
  25. {
  26. private:
  27. static uint8_t rxBuffer[];
  28. static uint8_t rxBufferIndex;
  29. static uint8_t rxBufferLength;
  30. static uint8_t txAddress;
  31. static uint8_t txBuffer[];
  32. static uint8_t txBufferIndex;
  33. static uint8_t txBufferLength;
  34. static uint8_t transmitting;
  35. static void onRequestService(void);
  36. static void onReceiveService(uint8_t*, int);
  37. static void (*user_onRequest)(void);
  38. static void (*user_onReceive)(int);
  39. static void sda_rising_isr(void);
  40. friend void i2c0_isr(void);
  41. public:
  42. TwoWire();
  43. void begin();
  44. void begin(uint8_t);
  45. void begin(int);
  46. void end();
  47. void setClock(uint32_t);
  48. void beginTransmission(uint8_t);
  49. void beginTransmission(int);
  50. uint8_t endTransmission(void);
  51. uint8_t endTransmission(uint8_t);
  52. uint8_t requestFrom(uint8_t, uint8_t);
  53. uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
  54. uint8_t requestFrom(int, int);
  55. uint8_t requestFrom(int, int, int);
  56. virtual size_t write(uint8_t);
  57. virtual size_t write(const uint8_t *, size_t);
  58. virtual int available(void);
  59. virtual int read(void);
  60. virtual int peek(void);
  61. virtual void flush(void);
  62. void onReceive( void (*)(int) );
  63. void onRequest( void (*)(void) );
  64. #ifdef CORE_TEENSY
  65. // added by Teensyduino installer, for compatibility
  66. // with pre-1.0 sketches and libraries
  67. void send(uint8_t b) { write(b); }
  68. void send(uint8_t *s, uint8_t n) { write(s, n); }
  69. void send(int n) { write((uint8_t)n); }
  70. void send(char *s) { write(s); }
  71. uint8_t receive(void) {
  72. int c = read();
  73. if (c < 0) return 0;
  74. return c;
  75. }
  76. #endif
  77. inline size_t write(unsigned long n) { return write((uint8_t)n); }
  78. inline size_t write(long n) { return write((uint8_t)n); }
  79. inline size_t write(unsigned int n) { return write((uint8_t)n); }
  80. inline size_t write(int n) { return write((uint8_t)n); }
  81. using Print::write;
  82. };
  83. extern TwoWire Wire;
  84. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  85. class TWBRemulation
  86. {
  87. public:
  88. inline TWBRemulation & operator = (int val) __attribute__((always_inline)) {
  89. if (val == 12 || val == ((F_CPU / 400000) - 16) / 2) { // 22, 52, 112
  90. I2C0_C1 = 0;
  91. #if F_BUS == 60000000
  92. I2C0_F = 0x1C; // 416 kHz
  93. #elif F_BUS == 56000000
  94. I2C0_F = 0x1C; // 389 kHz
  95. #elif F_BUS == 48000000
  96. I2C0_F = 0x1A; // 400 kHz
  97. #elif F_BUS == 40000000
  98. I2C0_F = 0x19; // 416 kHz
  99. #elif F_BUS == 36000000
  100. I2C0_F = 0x19; // 375 kHz
  101. #elif F_BUS == 24000000
  102. I2C0_F = 0x12; // 375 kHz
  103. #elif F_BUS == 16000000
  104. I2C0_F = 0x07; // 400 kHz
  105. #elif F_BUS == 8000000
  106. I2C0_F = 0x00; // 400 kHz
  107. #elif F_BUS == 4000000
  108. I2C0_F = 0x00; // 200 kHz
  109. #endif
  110. I2C0_C1 = I2C_C1_IICEN;
  111. } else if (val == 72 || val == ((F_CPU / 100000) - 16) / 2) { // 112, 232, 472
  112. I2C0_C1 = 0;
  113. #if F_BUS == 60000000
  114. I2C0_F = 0x2C; // 104 kHz
  115. #elif F_BUS == 56000000
  116. I2C0_F = 0x2B; // 109 kHz
  117. #elif F_BUS == 48000000
  118. I2C0_F = 0x27; // 100 kHz
  119. #elif F_BUS == 40000000
  120. I2C0_F = 0x29; // 104 kHz
  121. #elif F_BUS == 36000000
  122. I2C0_F = 0x28; // 113 kHz
  123. #elif F_BUS == 24000000
  124. I2C0_F = 0x1F; // 100 kHz
  125. #elif F_BUS == 16000000
  126. I2C0_F = 0x20; // 100 kHz
  127. #elif F_BUS == 8000000
  128. I2C0_F = 0x14; // 100 kHz
  129. #elif F_BUS == 4000000
  130. I2C0_F = 0x07; // 100 kHz
  131. #elif F_BUS == 2000000
  132. I2C0_F = 0x00; // 100 kHz
  133. #endif
  134. I2C0_C1 = I2C_C1_IICEN;
  135. }
  136. return *this;
  137. }
  138. inline operator int () const __attribute__((always_inline)) {
  139. #if F_BUS == 60000000
  140. if (I2C0_F == 0x1C) return 12;
  141. #elif F_BUS == 48000000
  142. if (I2C0_F == 0x1A) return 12;
  143. #elif F_BUS == 40000000
  144. if (I2C0_F == 0x19) return 12;
  145. #elif F_BUS == 36000000
  146. if (I2C0_F == 0x19) return 12;
  147. #elif F_BUS == 24000000
  148. if (I2C0_F == 0x12) return 12;
  149. #elif F_BUS == 16000000
  150. if (I2C0_F == 0x07) return 12;
  151. #elif F_BUS == 8000000
  152. if (I2C0_F == 0x00) return 12;
  153. #elif F_BUS == 4000000
  154. if (I2C0_F == 0x00) return 12;
  155. #endif
  156. return 72;
  157. }
  158. };
  159. extern TWBRemulation TWBR;
  160. #endif
  161. #endif