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.

176 lines
5.1KB

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