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.

315 lines
9.1KB

  1. /* Wire Library for Teensy LC & 3.X
  2. * Copyright (c) 2014-2017, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this I2C library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and related products. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef TwoWireKinetis_h
  27. #define TwoWireKinetis_h
  28. #if defined(__arm__) && defined(TEENSYDUINO)
  29. extern "C" void i2c0_isr(void);
  30. #include <inttypes.h>
  31. #include "Arduino.h"
  32. #define BUFFER_LENGTH 32
  33. #define WIRE_HAS_END 1
  34. #if defined(__MKL26Z64__)
  35. #define WIRE_HAS_STOP_INTERRUPT 1
  36. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  37. #define WIRE_HAS_START_INTERRUPT 1
  38. #define WIRE_HAS_STOP_INTERRUPT 1
  39. #endif
  40. class TwoWire : public Stream
  41. {
  42. public:
  43. // Hardware description struct
  44. typedef struct {
  45. volatile uint32_t &clock_gate_register;
  46. uint32_t clock_gate_mask;
  47. uint8_t sda_pin[5];
  48. uint8_t sda_mux[5];
  49. uint8_t scl_pin[5];
  50. uint8_t scl_mux[5];
  51. } I2C_Hardware_t;
  52. static const I2C_Hardware_t i2c0_hardware;
  53. static const I2C_Hardware_t i2c1_hardware;
  54. static const I2C_Hardware_t i2c2_hardware;
  55. static const I2C_Hardware_t i2c3_hardware;
  56. public:
  57. TwoWire(KINETIS_I2C_t &myport, const I2C_Hardware_t &myhardware)
  58. : port(myport), hardware(myhardware), sda_pin_index(0), scl_pin_index(0) {
  59. }
  60. void begin();
  61. void begin(uint8_t address);
  62. void begin(int address) {
  63. begin((uint8_t)address);
  64. }
  65. void end();
  66. void setClock(uint32_t frequency);
  67. void setSDA(uint8_t pin);
  68. void setSCL(uint8_t pin);
  69. void beginTransmission(uint8_t address) {
  70. txBuffer[0] = (address << 1);
  71. transmitting = 1;
  72. txBufferLength = 1;
  73. }
  74. void beginTransmission(int address) {
  75. beginTransmission((uint8_t)address);
  76. }
  77. uint8_t endTransmission(uint8_t sendStop);
  78. uint8_t endTransmission(void) {
  79. return endTransmission(1);
  80. }
  81. uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop);
  82. uint8_t requestFrom(uint8_t address, uint8_t quantity) {
  83. return requestFrom(address, quantity, (uint8_t)1);
  84. }
  85. uint8_t requestFrom(int address, int quantity, int sendStop) {
  86. return requestFrom((uint8_t)address, (uint8_t)quantity,
  87. (uint8_t)(sendStop ? 1 : 0));
  88. }
  89. uint8_t requestFrom(int address, int quantity) {
  90. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)1);
  91. }
  92. virtual size_t write(uint8_t data);
  93. virtual size_t write(const uint8_t *data, size_t quantity);
  94. virtual int available(void) {
  95. return rxBufferLength - rxBufferIndex;
  96. }
  97. virtual int read(void) {
  98. if (rxBufferIndex >= rxBufferLength) return -1;
  99. return rxBuffer[rxBufferIndex++];
  100. }
  101. virtual int peek(void) {
  102. if (rxBufferIndex >= rxBufferLength) return -1;
  103. return rxBuffer[rxBufferIndex];
  104. }
  105. virtual void flush(void) {
  106. }
  107. void onReceive(void (*function)(int numBytes)) {
  108. user_onReceive = function;
  109. }
  110. void onRequest(void (*function)(void)) {
  111. user_onRequest = function;
  112. }
  113. // send() for compatibility with very old sketches and libraries
  114. void send(uint8_t b) {
  115. write(b);
  116. }
  117. void send(uint8_t *s, uint8_t n) {
  118. write(s, n);
  119. }
  120. void send(int n) {
  121. write((uint8_t)n);
  122. }
  123. void send(char *s) {
  124. write(s);
  125. }
  126. uint8_t receive(void) {
  127. int c = read();
  128. if (c < 0) return 0;
  129. return c;
  130. }
  131. size_t write(unsigned long n) {
  132. return write((uint8_t)n);
  133. }
  134. size_t write(long n) {
  135. return write((uint8_t)n);
  136. }
  137. size_t write(unsigned int n) {
  138. return write((uint8_t)n);
  139. }
  140. size_t write(int n) {
  141. return write((uint8_t)n);
  142. }
  143. using Print::write;
  144. private:
  145. uint8_t i2c_status(void) {
  146. return port.S;
  147. }
  148. void i2c_wait(void) {
  149. while (!(port.S & I2C_S_IICIF)) ; // wait (TODO: timeout)
  150. port.S = I2C_S_IICIF;
  151. }
  152. void isr(void);
  153. KINETIS_I2C_t &port;
  154. const I2C_Hardware_t &hardware;
  155. uint8_t rxBuffer[BUFFER_LENGTH];
  156. uint8_t rxBufferIndex;
  157. uint8_t rxBufferLength;
  158. uint8_t txAddress;
  159. uint8_t txBuffer[BUFFER_LENGTH+1];
  160. uint8_t txBufferIndex;
  161. uint8_t txBufferLength;
  162. uint8_t transmitting;
  163. uint8_t slave_mode;
  164. uint8_t irqcount;
  165. uint8_t sda_pin_index;
  166. uint8_t scl_pin_index;
  167. void onRequestService(void);
  168. void onReceiveService(uint8_t*, int);
  169. void (*user_onRequest)(void);
  170. void (*user_onReceive)(int);
  171. void sda_rising_isr(void);
  172. friend void i2c0_isr(void);
  173. friend void sda_rising_isr(void);
  174. };
  175. extern TwoWire Wire;
  176. class TWBRemulation
  177. {
  178. public:
  179. inline TWBRemulation & operator = (int val) __attribute__((always_inline)) {
  180. if (val == 12 || val == ((F_CPU / 400000) - 16) / 2) { // 22, 52, 112
  181. I2C0_C1 = 0;
  182. #if F_BUS == 120000000
  183. I2C0_F = I2C_F_DIV288; // 416 kHz
  184. #elif F_BUS == 108000000
  185. I2C0_F = I2C_F_DIV256; // 422 kHz
  186. #elif F_BUS == 96000000
  187. I2C0_F = I2C_F_DIV240; // 400 kHz
  188. #elif F_BUS == 90000000
  189. I2C0_F = I2C_F_DIV224; // 402 kHz
  190. #elif F_BUS == 80000000
  191. I2C0_F = I2C_F_DIV192; // 416 kHz
  192. #elif F_BUS == 72000000
  193. I2C0_F = I2C_F_DIV192; // 375 kHz
  194. #elif F_BUS == 64000000
  195. I2C0_F = I2C_F_DIV160; // 400 kHz
  196. #elif F_BUS == 60000000
  197. I2C0_F = I2C_F_DIV144; // 416 kHz
  198. #elif F_BUS == 56000000
  199. I2C0_F = I2C_F_DIV144; // 389 kHz
  200. #elif F_BUS == 54000000
  201. I2C0_F = I2C_F_DIV128; // 422 kHz
  202. #elif F_BUS == 48000000
  203. I2C0_F = I2C_F_DIV112; // 400 kHz
  204. #elif F_BUS == 40000000
  205. I2C0_F = I2C_F_DIV96; // 416 kHz
  206. #elif F_BUS == 36000000
  207. I2C0_F = I2C_F_DIV96; // 375 kHz
  208. #elif F_BUS == 24000000
  209. I2C0_F = I2C_F_DIV64; // 375 kHz
  210. #elif F_BUS == 16000000
  211. I2C0_F = I2C_F_DIV40; // 400 kHz
  212. #elif F_BUS == 8000000
  213. I2C0_F = I2C_F_DIV20; // 400 kHz
  214. #elif F_BUS == 4000000
  215. I2C0_F = I2C_F_DIV20; // 200 kHz
  216. #elif F_BUS == 2000000
  217. I2C0_F = I2C_F_DIV20; // 100 kHz
  218. #endif
  219. I2C0_C1 = I2C_C1_IICEN;
  220. } else if (val == 72 || val == ((F_CPU / 100000) - 16) / 2) { // 112, 232, 472
  221. I2C0_C1 = 0;
  222. #if F_BUS == 120000000
  223. I2C0_F = I2C_F_DIV1152; // 104 kHz
  224. #elif F_BUS == 108000000
  225. I2C0_F = I2C_F_DIV1024; // 105 kHz
  226. #elif F_BUS == 96000000
  227. I2C0_F = I2C_F_DIV960; // 100 kHz
  228. #elif F_BUS == 90000000
  229. I2C0_F = I2C_F_DIV896; // 100 kHz
  230. #elif F_BUS == 80000000
  231. I2C0_F = I2C_F_DIV768; // 104 kHz
  232. #elif F_BUS == 72000000
  233. I2C0_F = I2C_F_DIV640; // 112 kHz
  234. #elif F_BUS == 64000000
  235. I2C0_F = I2C_F_DIV640; // 100 kHz
  236. #elif F_BUS == 60000000
  237. I2C0_F = I2C_F_DIV576; // 104 kHz
  238. #elif F_BUS == 56000000
  239. I2C0_F = I2C_F_DIV512; // 109 kHz
  240. #elif F_BUS == 54000000
  241. I2C0_F = I2C_F_DIV512; // 105 kHz
  242. #elif F_BUS == 48000000
  243. I2C0_F = I2C_F_DIV480; // 100 kHz
  244. #elif F_BUS == 40000000
  245. I2C0_F = I2C_F_DIV384; // 104 kHz
  246. #elif F_BUS == 36000000
  247. I2C0_F = I2C_F_DIV320; // 113 kHz
  248. #elif F_BUS == 24000000
  249. I2C0_F = I2C_F_DIV240; // 100 kHz
  250. #elif F_BUS == 16000000
  251. I2C0_F = I2C_F_DIV160; // 100 kHz
  252. #elif F_BUS == 8000000
  253. I2C0_F = I2C_F_DIV80; // 100 kHz
  254. #elif F_BUS == 4000000
  255. I2C0_F = I2C_F_DIV40; // 100 kHz
  256. #elif F_BUS == 2000000
  257. I2C0_F = I2C_F_DIV20; // 100 kHz
  258. #endif
  259. I2C0_C1 = I2C_C1_IICEN;
  260. }
  261. return *this;
  262. }
  263. inline operator int () const __attribute__((always_inline)) {
  264. #if F_BUS == 120000000
  265. if (I2C0_F == I2C_F_DIV288) return 12;
  266. #elif F_BUS == 108000000
  267. if (I2C0_F == I2C_F_DIV256) return 12;
  268. #elif F_BUS == 96000000
  269. if (I2C0_F == I2C_F_DIV240) return 12;
  270. #elif F_BUS == 90000000
  271. if (I2C0_F == I2C_F_DIV224) return 12;
  272. #elif F_BUS == 80000000
  273. if (I2C0_F == I2C_F_DIV192) return 12;
  274. #elif F_BUS == 72000000
  275. if (I2C0_F == I2C_F_DIV192) return 12;
  276. #elif F_BUS == 64000000
  277. if (I2C0_F == I2C_F_DIV160) return 12;
  278. #elif F_BUS == 60000000
  279. if (I2C0_F == I2C_F_DIV144) return 12;
  280. #elif F_BUS == 56000000
  281. if (I2C0_F == I2C_F_DIV144) return 12;
  282. #elif F_BUS == 54000000
  283. if (I2C0_F == I2C_F_DIV128) return 12;
  284. #elif F_BUS == 48000000
  285. if (I2C0_F == I2C_F_DIV112) return 12;
  286. #elif F_BUS == 40000000
  287. if (I2C0_F == I2C_F_DIV96) return 12;
  288. #elif F_BUS == 36000000
  289. if (I2C0_F == I2C_F_DIV96) return 12;
  290. #elif F_BUS == 24000000
  291. if (I2C0_F == I2C_F_DIV64) return 12;
  292. #elif F_BUS == 16000000
  293. if (I2C0_F == I2C_F_DIV40) return 12;
  294. #elif F_BUS == 8000000
  295. if (I2C0_F == I2C_F_DIV20) return 12;
  296. #elif F_BUS == 4000000
  297. if (I2C0_F == I2C_F_DIV20) return 12;
  298. #endif
  299. return 72;
  300. }
  301. };
  302. extern TWBRemulation TWBR;
  303. #endif
  304. #endif