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.

186 lines
5.3KB

  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. #if defined(__arm__) && defined(CORE_TEENSY)
  24. extern "C" void i2c0_isr(void);
  25. #endif
  26. class TwoWire : public Stream
  27. {
  28. private:
  29. static uint8_t rxBuffer[];
  30. static uint8_t rxBufferIndex;
  31. static uint8_t rxBufferLength;
  32. static uint8_t txAddress;
  33. static uint8_t txBuffer[];
  34. static uint8_t txBufferIndex;
  35. static uint8_t txBufferLength;
  36. static uint8_t transmitting;
  37. static void onRequestService(void);
  38. static void onReceiveService(uint8_t*, int);
  39. static void (*user_onRequest)(void);
  40. static void (*user_onReceive)(int);
  41. static void sda_rising_isr(void);
  42. #if defined(__arm__) && defined(CORE_TEENSY)
  43. static uint8_t sda_pin_num;
  44. static uint8_t scl_pin_num;
  45. friend void i2c0_isr(void);
  46. #endif
  47. public:
  48. TwoWire();
  49. void begin();
  50. void begin(uint8_t);
  51. void begin(int);
  52. void end();
  53. void setClock(uint32_t);
  54. void setSDA(uint8_t);
  55. void setSCL(uint8_t);
  56. void beginTransmission(uint8_t);
  57. void beginTransmission(int);
  58. uint8_t endTransmission(void);
  59. uint8_t endTransmission(uint8_t);
  60. uint8_t requestFrom(uint8_t, uint8_t);
  61. uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
  62. uint8_t requestFrom(int, int);
  63. uint8_t requestFrom(int, int, int);
  64. virtual size_t write(uint8_t);
  65. virtual size_t write(const uint8_t *, size_t);
  66. virtual int available(void);
  67. virtual int read(void);
  68. virtual int peek(void);
  69. virtual void flush(void);
  70. void onReceive( void (*)(int) );
  71. void onRequest( void (*)(void) );
  72. #ifdef CORE_TEENSY
  73. // added by Teensyduino installer, for compatibility
  74. // with pre-1.0 sketches and libraries
  75. void send(uint8_t b) { write(b); }
  76. void send(uint8_t *s, uint8_t n) { write(s, n); }
  77. void send(int n) { write((uint8_t)n); }
  78. void send(char *s) { write(s); }
  79. uint8_t receive(void) {
  80. int c = read();
  81. if (c < 0) return 0;
  82. return c;
  83. }
  84. #endif
  85. inline size_t write(unsigned long n) { return write((uint8_t)n); }
  86. inline size_t write(long n) { return write((uint8_t)n); }
  87. inline size_t write(unsigned int n) { return write((uint8_t)n); }
  88. inline size_t write(int n) { return write((uint8_t)n); }
  89. using Print::write;
  90. };
  91. extern TwoWire Wire;
  92. #if defined(__arm__) && defined(CORE_TEENSY)
  93. class TWBRemulation
  94. {
  95. public:
  96. inline TWBRemulation & operator = (int val) __attribute__((always_inline)) {
  97. if (val == 12 || val == ((F_CPU / 400000) - 16) / 2) { // 22, 52, 112
  98. I2C0_C1 = 0;
  99. #if F_BUS == 60000000
  100. I2C0_F = 0x1C; // 416 kHz
  101. #elif F_BUS == 56000000
  102. I2C0_F = 0x1C; // 389 kHz
  103. #elif F_BUS == 48000000
  104. I2C0_F = 0x1A; // 400 kHz
  105. #elif F_BUS == 40000000
  106. I2C0_F = 0x19; // 416 kHz
  107. #elif F_BUS == 36000000
  108. I2C0_F = 0x19; // 375 kHz
  109. #elif F_BUS == 24000000
  110. I2C0_F = 0x12; // 375 kHz
  111. #elif F_BUS == 16000000
  112. I2C0_F = 0x07; // 400 kHz
  113. #elif F_BUS == 8000000
  114. I2C0_F = 0x00; // 400 kHz
  115. #elif F_BUS == 4000000
  116. I2C0_F = 0x00; // 200 kHz
  117. #endif
  118. I2C0_C1 = I2C_C1_IICEN;
  119. } else if (val == 72 || val == ((F_CPU / 100000) - 16) / 2) { // 112, 232, 472
  120. I2C0_C1 = 0;
  121. #if F_BUS == 60000000
  122. I2C0_F = 0x2C; // 104 kHz
  123. #elif F_BUS == 56000000
  124. I2C0_F = 0x2B; // 109 kHz
  125. #elif F_BUS == 48000000
  126. I2C0_F = 0x27; // 100 kHz
  127. #elif F_BUS == 40000000
  128. I2C0_F = 0x29; // 104 kHz
  129. #elif F_BUS == 36000000
  130. I2C0_F = 0x28; // 113 kHz
  131. #elif F_BUS == 24000000
  132. I2C0_F = 0x1F; // 100 kHz
  133. #elif F_BUS == 16000000
  134. I2C0_F = 0x20; // 100 kHz
  135. #elif F_BUS == 8000000
  136. I2C0_F = 0x14; // 100 kHz
  137. #elif F_BUS == 4000000
  138. I2C0_F = 0x07; // 100 kHz
  139. #elif F_BUS == 2000000
  140. I2C0_F = 0x00; // 100 kHz
  141. #endif
  142. I2C0_C1 = I2C_C1_IICEN;
  143. }
  144. return *this;
  145. }
  146. inline operator int () const __attribute__((always_inline)) {
  147. #if F_BUS == 60000000
  148. if (I2C0_F == 0x1C) return 12;
  149. #elif F_BUS == 48000000
  150. if (I2C0_F == 0x1A) return 12;
  151. #elif F_BUS == 40000000
  152. if (I2C0_F == 0x19) return 12;
  153. #elif F_BUS == 36000000
  154. if (I2C0_F == 0x19) return 12;
  155. #elif F_BUS == 24000000
  156. if (I2C0_F == 0x12) return 12;
  157. #elif F_BUS == 16000000
  158. if (I2C0_F == 0x07) return 12;
  159. #elif F_BUS == 8000000
  160. if (I2C0_F == 0x00) return 12;
  161. #elif F_BUS == 4000000
  162. if (I2C0_F == 0x00) return 12;
  163. #endif
  164. return 72;
  165. }
  166. };
  167. extern TWBRemulation TWBR;
  168. #endif
  169. #endif