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.

359 lines
10.0KB

  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. #include <inttypes.h>
  30. #include "Arduino.h"
  31. #define BUFFER_LENGTH 32
  32. #define WIRE_HAS_END 1
  33. // Teensy LC
  34. #if defined(__MKL26Z64__)
  35. #define WIRE_IMPLEMENT_WIRE
  36. #define WIRE_IMPLEMENT_WIRE1
  37. #define WIRE_HAS_STOP_INTERRUPT
  38. // Teensy 3.0
  39. #elif defined(__MK20DX128__)
  40. #define WIRE_IMPLEMENT_WIRE
  41. // Teensy 3.1 & 3.2
  42. #elif defined(__MK20DX256__)
  43. #define WIRE_IMPLEMENT_WIRE
  44. #define WIRE_IMPLEMENT_WIRE1
  45. // Teensy 3.5
  46. #elif defined(__MK64FX512__)
  47. #define WIRE_IMPLEMENT_WIRE
  48. #define WIRE_IMPLEMENT_WIRE1
  49. #define WIRE_IMPLEMENT_WIRE2
  50. #define WIRE_HAS_START_INTERRUPT
  51. #define WIRE_HAS_STOP_INTERRUPT
  52. // Teensy 3.6
  53. #elif defined(__MK66FX1M0__)
  54. #define WIRE_IMPLEMENT_WIRE
  55. #define WIRE_IMPLEMENT_WIRE1
  56. #define WIRE_IMPLEMENT_WIRE2
  57. #define WIRE_IMPLEMENT_WIRE3
  58. #define WIRE_HAS_START_INTERRUPT
  59. #define WIRE_HAS_STOP_INTERRUPT
  60. #endif
  61. class TwoWire : public Stream
  62. {
  63. public:
  64. // Hardware description struct
  65. typedef struct {
  66. volatile uint32_t &clock_gate_register;
  67. uint32_t clock_gate_mask;
  68. uint8_t sda_pin[5];
  69. uint8_t sda_mux[5];
  70. uint8_t scl_pin[5];
  71. uint8_t scl_mux[5];
  72. IRQ_NUMBER_t irq;
  73. } I2C_Hardware_t;
  74. static const I2C_Hardware_t i2c0_hardware;
  75. static const I2C_Hardware_t i2c1_hardware;
  76. static const I2C_Hardware_t i2c2_hardware;
  77. static const I2C_Hardware_t i2c3_hardware;
  78. public:
  79. TwoWire(KINETIS_I2C_t &myport, const I2C_Hardware_t &myhardware)
  80. : port(myport), hardware(myhardware), sda_pin_index(0), scl_pin_index(0) {
  81. }
  82. void begin();
  83. void begin(uint8_t address);
  84. void begin(int address) {
  85. begin((uint8_t)address);
  86. }
  87. void end();
  88. void setClock(uint32_t frequency);
  89. void setSDA(uint8_t pin);
  90. void setSCL(uint8_t pin);
  91. void beginTransmission(uint8_t address) {
  92. txBuffer[0] = (address << 1);
  93. transmitting = 1;
  94. txBufferLength = 1;
  95. }
  96. void beginTransmission(int address) {
  97. beginTransmission((uint8_t)address);
  98. }
  99. uint8_t endTransmission(uint8_t sendStop);
  100. uint8_t endTransmission(void) {
  101. return endTransmission(1);
  102. }
  103. uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop);
  104. uint8_t requestFrom(uint8_t address, uint8_t quantity) {
  105. return requestFrom(address, quantity, (uint8_t)1);
  106. }
  107. uint8_t requestFrom(int address, int quantity, int sendStop) {
  108. return requestFrom((uint8_t)address, (uint8_t)quantity,
  109. (uint8_t)(sendStop ? 1 : 0));
  110. }
  111. uint8_t requestFrom(int address, int quantity) {
  112. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)1);
  113. }
  114. virtual size_t write(uint8_t data);
  115. virtual size_t write(const uint8_t *data, size_t quantity);
  116. virtual int available(void) {
  117. return rxBufferLength - rxBufferIndex;
  118. }
  119. virtual int read(void) {
  120. if (rxBufferIndex >= rxBufferLength) return -1;
  121. return rxBuffer[rxBufferIndex++];
  122. }
  123. virtual int peek(void) {
  124. if (rxBufferIndex >= rxBufferLength) return -1;
  125. return rxBuffer[rxBufferIndex];
  126. }
  127. virtual void flush(void) {
  128. }
  129. void onReceive(void (*function)(int numBytes)) {
  130. user_onReceive = function;
  131. }
  132. void onRequest(void (*function)(void)) {
  133. user_onRequest = function;
  134. }
  135. // send() for compatibility with very old sketches and libraries
  136. void send(uint8_t b) {
  137. write(b);
  138. }
  139. void send(uint8_t *s, uint8_t n) {
  140. write(s, n);
  141. }
  142. void send(int n) {
  143. write((uint8_t)n);
  144. }
  145. void send(char *s) {
  146. write(s);
  147. }
  148. uint8_t receive(void) {
  149. int c = read();
  150. if (c < 0) return 0;
  151. return c;
  152. }
  153. size_t write(unsigned long n) {
  154. return write((uint8_t)n);
  155. }
  156. size_t write(long n) {
  157. return write((uint8_t)n);
  158. }
  159. size_t write(unsigned int n) {
  160. return write((uint8_t)n);
  161. }
  162. size_t write(int n) {
  163. return write((uint8_t)n);
  164. }
  165. using Print::write;
  166. private:
  167. uint8_t i2c_status(void) {
  168. return port.S;
  169. }
  170. void i2c_wait(void) {
  171. while (!(port.S & I2C_S_IICIF)) ; // wait (TODO: timeout)
  172. port.S = I2C_S_IICIF;
  173. }
  174. void isr(void);
  175. KINETIS_I2C_t &port;
  176. const I2C_Hardware_t &hardware;
  177. uint8_t rxBuffer[BUFFER_LENGTH];
  178. uint8_t rxBufferIndex;
  179. uint8_t rxBufferLength;
  180. uint8_t txAddress;
  181. uint8_t txBuffer[BUFFER_LENGTH+1];
  182. uint8_t txBufferIndex;
  183. uint8_t txBufferLength;
  184. uint8_t transmitting;
  185. uint8_t slave_mode;
  186. uint8_t irqcount;
  187. uint8_t sda_pin_index;
  188. uint8_t scl_pin_index;
  189. void onRequestService(void);
  190. void onReceiveService(uint8_t*, int);
  191. void (*user_onRequest)(void);
  192. void (*user_onReceive)(int);
  193. void sda_rising_isr(void);
  194. friend void i2c0_isr(void);
  195. friend void i2c1_isr(void);
  196. friend void i2c2_isr(void);
  197. friend void i2c3_isr(void);
  198. friend void sda_rising_isr0(void);
  199. friend void sda_rising_isr1(void);
  200. };
  201. #ifdef WIRE_IMPLEMENT_WIRE
  202. extern TwoWire Wire;
  203. #endif
  204. #ifdef WIRE_IMPLEMENT_WIRE1
  205. extern TwoWire Wire1;
  206. #endif
  207. #ifdef WIRE_IMPLEMENT_WIRE2
  208. extern TwoWire Wire2;
  209. #endif
  210. #ifdef WIRE_IMPLEMENT_WIRE3
  211. extern TwoWire Wire3;
  212. #endif
  213. class TWBRemulation
  214. {
  215. public:
  216. inline TWBRemulation & operator = (int val) __attribute__((always_inline)) {
  217. if (val == 12 || val == ((F_CPU / 400000) - 16) / 2) { // 22, 52, 112
  218. I2C0_C1 = 0;
  219. #if F_BUS == 120000000
  220. I2C0_F = I2C_F_DIV288; // 416 kHz
  221. #elif F_BUS == 108000000
  222. I2C0_F = I2C_F_DIV256; // 422 kHz
  223. #elif F_BUS == 96000000
  224. I2C0_F = I2C_F_DIV240; // 400 kHz
  225. #elif F_BUS == 90000000
  226. I2C0_F = I2C_F_DIV224; // 402 kHz
  227. #elif F_BUS == 80000000
  228. I2C0_F = I2C_F_DIV192; // 416 kHz
  229. #elif F_BUS == 72000000
  230. I2C0_F = I2C_F_DIV192; // 375 kHz
  231. #elif F_BUS == 64000000
  232. I2C0_F = I2C_F_DIV160; // 400 kHz
  233. #elif F_BUS == 60000000
  234. I2C0_F = I2C_F_DIV144; // 416 kHz
  235. #elif F_BUS == 56000000
  236. I2C0_F = I2C_F_DIV144; // 389 kHz
  237. #elif F_BUS == 54000000
  238. I2C0_F = I2C_F_DIV128; // 422 kHz
  239. #elif F_BUS == 48000000
  240. I2C0_F = I2C_F_DIV112; // 400 kHz
  241. #elif F_BUS == 40000000
  242. I2C0_F = I2C_F_DIV96; // 416 kHz
  243. #elif F_BUS == 36000000
  244. I2C0_F = I2C_F_DIV96; // 375 kHz
  245. #elif F_BUS == 24000000
  246. I2C0_F = I2C_F_DIV64; // 375 kHz
  247. #elif F_BUS == 16000000
  248. I2C0_F = I2C_F_DIV40; // 400 kHz
  249. #elif F_BUS == 8000000
  250. I2C0_F = I2C_F_DIV20; // 400 kHz
  251. #elif F_BUS == 4000000
  252. I2C0_F = I2C_F_DIV20; // 200 kHz
  253. #elif F_BUS == 2000000
  254. I2C0_F = I2C_F_DIV20; // 100 kHz
  255. #endif
  256. I2C0_C1 = I2C_C1_IICEN;
  257. } else if (val == 72 || val == ((F_CPU / 100000) - 16) / 2) { // 112, 232, 472
  258. I2C0_C1 = 0;
  259. #if F_BUS == 120000000
  260. I2C0_F = I2C_F_DIV1152; // 104 kHz
  261. #elif F_BUS == 108000000
  262. I2C0_F = I2C_F_DIV1024; // 105 kHz
  263. #elif F_BUS == 96000000
  264. I2C0_F = I2C_F_DIV960; // 100 kHz
  265. #elif F_BUS == 90000000
  266. I2C0_F = I2C_F_DIV896; // 100 kHz
  267. #elif F_BUS == 80000000
  268. I2C0_F = I2C_F_DIV768; // 104 kHz
  269. #elif F_BUS == 72000000
  270. I2C0_F = I2C_F_DIV640; // 112 kHz
  271. #elif F_BUS == 64000000
  272. I2C0_F = I2C_F_DIV640; // 100 kHz
  273. #elif F_BUS == 60000000
  274. I2C0_F = I2C_F_DIV576; // 104 kHz
  275. #elif F_BUS == 56000000
  276. I2C0_F = I2C_F_DIV512; // 109 kHz
  277. #elif F_BUS == 54000000
  278. I2C0_F = I2C_F_DIV512; // 105 kHz
  279. #elif F_BUS == 48000000
  280. I2C0_F = I2C_F_DIV480; // 100 kHz
  281. #elif F_BUS == 40000000
  282. I2C0_F = I2C_F_DIV384; // 104 kHz
  283. #elif F_BUS == 36000000
  284. I2C0_F = I2C_F_DIV320; // 113 kHz
  285. #elif F_BUS == 24000000
  286. I2C0_F = I2C_F_DIV240; // 100 kHz
  287. #elif F_BUS == 16000000
  288. I2C0_F = I2C_F_DIV160; // 100 kHz
  289. #elif F_BUS == 8000000
  290. I2C0_F = I2C_F_DIV80; // 100 kHz
  291. #elif F_BUS == 4000000
  292. I2C0_F = I2C_F_DIV40; // 100 kHz
  293. #elif F_BUS == 2000000
  294. I2C0_F = I2C_F_DIV20; // 100 kHz
  295. #endif
  296. I2C0_C1 = I2C_C1_IICEN;
  297. }
  298. return *this;
  299. }
  300. inline operator int () const __attribute__((always_inline)) {
  301. #if F_BUS == 120000000
  302. if (I2C0_F == I2C_F_DIV288) return 12;
  303. #elif F_BUS == 108000000
  304. if (I2C0_F == I2C_F_DIV256) return 12;
  305. #elif F_BUS == 96000000
  306. if (I2C0_F == I2C_F_DIV240) return 12;
  307. #elif F_BUS == 90000000
  308. if (I2C0_F == I2C_F_DIV224) return 12;
  309. #elif F_BUS == 80000000
  310. if (I2C0_F == I2C_F_DIV192) return 12;
  311. #elif F_BUS == 72000000
  312. if (I2C0_F == I2C_F_DIV192) return 12;
  313. #elif F_BUS == 64000000
  314. if (I2C0_F == I2C_F_DIV160) return 12;
  315. #elif F_BUS == 60000000
  316. if (I2C0_F == I2C_F_DIV144) return 12;
  317. #elif F_BUS == 56000000
  318. if (I2C0_F == I2C_F_DIV144) return 12;
  319. #elif F_BUS == 54000000
  320. if (I2C0_F == I2C_F_DIV128) return 12;
  321. #elif F_BUS == 48000000
  322. if (I2C0_F == I2C_F_DIV112) return 12;
  323. #elif F_BUS == 40000000
  324. if (I2C0_F == I2C_F_DIV96) return 12;
  325. #elif F_BUS == 36000000
  326. if (I2C0_F == I2C_F_DIV96) return 12;
  327. #elif F_BUS == 24000000
  328. if (I2C0_F == I2C_F_DIV64) return 12;
  329. #elif F_BUS == 16000000
  330. if (I2C0_F == I2C_F_DIV40) return 12;
  331. #elif F_BUS == 8000000
  332. if (I2C0_F == I2C_F_DIV20) return 12;
  333. #elif F_BUS == 4000000
  334. if (I2C0_F == I2C_F_DIV20) return 12;
  335. #endif
  336. return 72;
  337. }
  338. };
  339. extern TWBRemulation TWBR;
  340. #endif
  341. #endif