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.

238 lines
7.2KB

  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 == 120000000
  100. I2C0_F = I2C_F_DIV288; // 416 kHz
  101. #elif F_BUS == 108000000
  102. I2C0_F = I2C_F_DIV256; // 422 kHz
  103. #elif F_BUS == 96000000
  104. I2C0_F = I2C_F_DIV240; // 400 kHz
  105. #elif F_BUS == 90000000
  106. I2C0_F = I2C_F_DIV224; // 402 kHz
  107. #elif F_BUS == 80000000
  108. I2C0_F = I2C_F_DIV192; // 416 kHz
  109. #elif F_BUS == 72000000
  110. I2C0_F = I2C_F_DIV192; // 375 kHz
  111. #elif F_BUS == 64000000
  112. I2C0_F = I2C_F_DIV160; // 400 kHz
  113. #elif F_BUS == 60000000
  114. I2C0_F = I2C_F_DIV144; // 416 kHz
  115. #elif F_BUS == 56000000
  116. I2C0_F = I2C_F_DIV144; // 389 kHz
  117. #elif F_BUS == 54000000
  118. I2C0_F = I2C_F_DIV128; // 422 kHz
  119. #elif F_BUS == 48000000
  120. I2C0_F = I2C_F_DIV112; // 400 kHz
  121. #elif F_BUS == 40000000
  122. I2C0_F = I2C_F_DIV96; // 416 kHz
  123. #elif F_BUS == 36000000
  124. I2C0_F = I2C_F_DIV96; // 375 kHz
  125. #elif F_BUS == 24000000
  126. I2C0_F = I2C_F_DIV64; // 375 kHz
  127. #elif F_BUS == 16000000
  128. I2C0_F = I2C_F_DIV40; // 400 kHz
  129. #elif F_BUS == 8000000
  130. I2C0_F = I2C_F_DIV20; // 400 kHz
  131. #elif F_BUS == 4000000
  132. I2C0_F = I2C_F_DIV20; // 200 kHz
  133. #elif F_BUS == 2000000
  134. I2C0_F = I2C_F_DIV20; // 100 kHz
  135. #endif
  136. I2C0_C1 = I2C_C1_IICEN;
  137. } else if (val == 72 || val == ((F_CPU / 100000) - 16) / 2) { // 112, 232, 472
  138. I2C0_C1 = 0;
  139. #if F_BUS == 120000000
  140. I2C0_F = I2C_F_DIV1152; // 104 kHz
  141. #elif F_BUS == 108000000
  142. I2C0_F = I2C_F_DIV1024; // 105 kHz
  143. #elif F_BUS == 96000000
  144. I2C0_F = I2C_F_DIV960; // 100 kHz
  145. #elif F_BUS == 90000000
  146. I2C0_F = I2C_F_DIV896; // 100 kHz
  147. #elif F_BUS == 80000000
  148. I2C0_F = I2C_F_DIV768; // 104 kHz
  149. #elif F_BUS == 72000000
  150. I2C0_F = I2C_F_DIV640; // 112 kHz
  151. #elif F_BUS == 64000000
  152. I2C0_F = I2C_F_DIV640; // 100 kHz
  153. #elif F_BUS == 60000000
  154. I2C0_F = I2C_F_DIV576; // 104 kHz
  155. #elif F_BUS == 56000000
  156. I2C0_F = I2C_F_DIV512; // 109 kHz
  157. #elif F_BUS == 54000000
  158. I2C0_F = I2C_F_DIV512; // 105 kHz
  159. #elif F_BUS == 48000000
  160. I2C0_F = I2C_F_DIV480; // 100 kHz
  161. #elif F_BUS == 40000000
  162. I2C0_F = I2C_F_DIV384; // 104 kHz
  163. #elif F_BUS == 36000000
  164. I2C0_F = I2C_F_DIV320; // 113 kHz
  165. #elif F_BUS == 24000000
  166. I2C0_F = I2C_F_DIV240; // 100 kHz
  167. #elif F_BUS == 16000000
  168. I2C0_F = I2C_F_DIV160; // 100 kHz
  169. #elif F_BUS == 8000000
  170. I2C0_F = I2C_F_DIV80; // 100 kHz
  171. #elif F_BUS == 4000000
  172. I2C0_F = I2C_F_DIV40; // 100 kHz
  173. #elif F_BUS == 2000000
  174. I2C0_F = I2C_F_DIV20; // 100 kHz
  175. #endif
  176. I2C0_C1 = I2C_C1_IICEN;
  177. }
  178. return *this;
  179. }
  180. inline operator int () const __attribute__((always_inline)) {
  181. #if F_BUS == 120000000
  182. if (I2C0_F == I2C_F_DIV288) return 12;
  183. #elif F_BUS == 108000000
  184. if (I2C0_F == I2C_F_DIV256) return 12;
  185. #elif F_BUS == 96000000
  186. if (I2C0_F == I2C_F_DIV240) return 12;
  187. #elif F_BUS == 90000000
  188. if (I2C0_F == I2C_F_DIV224) return 12;
  189. #elif F_BUS == 80000000
  190. if (I2C0_F == I2C_F_DIV192) return 12;
  191. #elif F_BUS == 72000000
  192. if (I2C0_F == I2C_F_DIV192) return 12;
  193. #elif F_BUS == 64000000
  194. if (I2C0_F == I2C_F_DIV160) return 12;
  195. #elif F_BUS == 60000000
  196. if (I2C0_F == I2C_F_DIV144) return 12;
  197. #elif F_BUS == 56000000
  198. if (I2C0_F == I2C_F_DIV144) return 12;
  199. #elif F_BUS == 54000000
  200. if (I2C0_F == I2C_F_DIV128) return 12;
  201. #elif F_BUS == 48000000
  202. if (I2C0_F == I2C_F_DIV112) return 12;
  203. #elif F_BUS == 40000000
  204. if (I2C0_F == I2C_F_DIV96) return 12;
  205. #elif F_BUS == 36000000
  206. if (I2C0_F == I2C_F_DIV96) return 12;
  207. #elif F_BUS == 24000000
  208. if (I2C0_F == I2C_F_DIV64) return 12;
  209. #elif F_BUS == 16000000
  210. if (I2C0_F == I2C_F_DIV40) return 12;
  211. #elif F_BUS == 8000000
  212. if (I2C0_F == I2C_F_DIV20) return 12;
  213. #elif F_BUS == 4000000
  214. if (I2C0_F == I2C_F_DIV20) return 12;
  215. #endif
  216. return 72;
  217. }
  218. };
  219. extern TWBRemulation TWBR;
  220. #endif
  221. #endif