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.

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