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.

699 lines
18KB

  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. #include "Wire.h"
  27. #if defined(__arm__) && defined(TEENSYDUINO)
  28. #include "kinetis.h"
  29. #include <string.h> // for memcpy
  30. #include "core_pins.h"
  31. #include "Wire.h"
  32. // undefine these, so we can't accidentally access the hardware directly.
  33. #undef I2C0_A1
  34. #undef I2C0_F
  35. #undef I2C0_C1
  36. #undef I2C0_S
  37. #undef I2C0_D
  38. #undef I2C0_C2
  39. #undef I2C0_FLT
  40. #undef I2C0_RA
  41. #undef I2C0_SMB
  42. #undef I2C0_A2
  43. #undef I2C0_SLTH
  44. #undef I2C0_SLTL
  45. void sda_rising_isr(void);
  46. void TwoWire::begin(void)
  47. {
  48. //serial_begin(BAUD2DIV(115200));
  49. //serial_print("\nWire Begin\n");
  50. rxBufferIndex = 0;
  51. rxBufferLength = 0;
  52. txBufferIndex = 0;
  53. txBufferLength = 0;
  54. transmitting = 0;
  55. user_onRequest = NULL;
  56. user_onReceive = NULL;
  57. slave_mode = 0;
  58. hardware.clock_gate_register |= hardware.clock_gate_mask;
  59. port.C1 = 0;
  60. // On Teensy 3.0 external pullup resistors *MUST* be used
  61. // the PORT_PCR_PE bit is ignored when in I2C mode
  62. // I2C will not work at all without pullup resistors
  63. // It might seem like setting PORT_PCR_PE & PORT_PCR_PS
  64. // would enable pullup resistors. However, there seems
  65. // to be a bug in chip while I2C is enabled, where setting
  66. // those causes the port to be driven strongly high.
  67. uint32_t mux;
  68. volatile uint32_t *reg;
  69. reg = portConfigRegister(hardware.sda_pin[sda_pin_index]);
  70. mux = PORT_PCR_MUX(hardware.sda_mux[sda_pin_index]);
  71. *reg = mux|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  72. reg = portConfigRegister(hardware.scl_pin[scl_pin_index]);
  73. mux = PORT_PCR_MUX(hardware.scl_mux[scl_pin_index]);
  74. *reg = mux|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  75. setClock(100000);
  76. port.C2 = I2C_C2_HDRS;
  77. port.C1 = I2C_C1_IICEN;
  78. //pinMode(3, OUTPUT);
  79. //pinMode(4, OUTPUT);
  80. }
  81. void TwoWire::setClock(uint32_t frequency)
  82. {
  83. if (!(hardware.clock_gate_register & hardware.clock_gate_mask)) return;
  84. #if F_BUS == 120000000
  85. if (frequency < 400000) {
  86. port.F = I2C_F_DIV1152; // 104 kHz
  87. } else if (frequency < 1000000) {
  88. port.F = I2C_F_DIV288; // 416 kHz
  89. } else {
  90. port.F = I2C_F_DIV128; // 0.94 MHz
  91. }
  92. port.FLT = 4;
  93. #elif F_BUS == 108000000
  94. if (frequency < 400000) {
  95. port.F = I2C_F_DIV1024; // 105 kHz
  96. } else if (frequency < 1000000) {
  97. port.F = I2C_F_DIV256; // 422 kHz
  98. } else {
  99. port.F = I2C_F_DIV112; // 0.96 MHz
  100. }
  101. port.FLT = 4;
  102. #elif F_BUS == 96000000
  103. if (frequency < 400000) {
  104. port.F = I2C_F_DIV960; // 100 kHz
  105. } else if (frequency < 1000000) {
  106. port.F = I2C_F_DIV240; // 400 kHz
  107. } else {
  108. port.F = I2C_F_DIV96; // 1.0 MHz
  109. }
  110. port.FLT = 4;
  111. #elif F_BUS == 90000000
  112. if (frequency < 400000) {
  113. port.F = I2C_F_DIV896; // 100 kHz
  114. } else if (frequency < 1000000) {
  115. port.F = I2C_F_DIV224; // 402 kHz
  116. } else {
  117. port.F = I2C_F_DIV88; // 1.02 MHz
  118. }
  119. port.FLT = 4;
  120. #elif F_BUS == 80000000
  121. if (frequency < 400000) {
  122. port.F = I2C_F_DIV768; // 104 kHz
  123. } else if (frequency < 1000000) {
  124. port.F = I2C_F_DIV192; // 416 kHz
  125. } else {
  126. port.F = I2C_F_DIV80; // 1.0 MHz
  127. }
  128. port.FLT = 4;
  129. #elif F_BUS == 72000000
  130. if (frequency < 400000) {
  131. port.F = I2C_F_DIV640; // 112 kHz
  132. } else if (frequency < 1000000) {
  133. port.F = I2C_F_DIV192; // 375 kHz
  134. } else {
  135. port.F = I2C_F_DIV72; // 1.0 MHz
  136. }
  137. port.FLT = 4;
  138. #elif F_BUS == 64000000
  139. if (frequency < 400000) {
  140. port.F = I2C_F_DIV640; // 100 kHz
  141. } else if (frequency < 1000000) {
  142. port.F = I2C_F_DIV160; // 400 kHz
  143. } else {
  144. port.F = I2C_F_DIV64; // 1.0 MHz
  145. }
  146. port.FLT = 4;
  147. #elif F_BUS == 60000000
  148. if (frequency < 400000) {
  149. port.F = 0x2C; // 104 kHz
  150. } else if (frequency < 1000000) {
  151. port.F = 0x1C; // 416 kHz
  152. } else {
  153. port.F = 0x12; // 938 kHz
  154. }
  155. port.FLT = 4;
  156. #elif F_BUS == 56000000
  157. if (frequency < 400000) {
  158. port.F = 0x2B; // 109 kHz
  159. } else if (frequency < 1000000) {
  160. port.F = 0x1C; // 389 kHz
  161. } else {
  162. port.F = 0x0E; // 1 MHz
  163. }
  164. port.FLT = 4;
  165. #elif F_BUS == 54000000
  166. if (frequency < 400000) {
  167. port.F = I2C_F_DIV512; // 105 kHz
  168. } else if (frequency < 1000000) {
  169. port.F = I2C_F_DIV128; // 422 kHz
  170. } else {
  171. port.F = I2C_F_DIV56; // 0.96 MHz
  172. }
  173. port.FLT = 4;
  174. #elif F_BUS == 48000000
  175. if (frequency < 400000) {
  176. port.F = 0x27; // 100 kHz
  177. } else if (frequency < 1000000) {
  178. port.F = 0x1A; // 400 kHz
  179. } else {
  180. port.F = 0x0D; // 1 MHz
  181. }
  182. port.FLT = 4;
  183. #elif F_BUS == 40000000
  184. if (frequency < 400000) {
  185. port.F = 0x29; // 104 kHz
  186. } else if (frequency < 1000000) {
  187. port.F = 0x19; // 416 kHz
  188. } else {
  189. port.F = 0x0B; // 1 MHz
  190. }
  191. port.FLT = 3;
  192. #elif F_BUS == 36000000
  193. if (frequency < 400000) {
  194. port.F = 0x28; // 113 kHz
  195. } else if (frequency < 1000000) {
  196. port.F = 0x19; // 375 kHz
  197. } else {
  198. port.F = 0x0A; // 1 MHz
  199. }
  200. port.FLT = 3;
  201. #elif F_BUS == 24000000
  202. if (frequency < 400000) {
  203. port.F = 0x1F; // 100 kHz
  204. } else if (frequency < 1000000) {
  205. port.F = 0x12; // 375 kHz
  206. } else {
  207. port.F = 0x02; // 1 MHz
  208. }
  209. port.FLT = 2;
  210. #elif F_BUS == 16000000
  211. if (frequency < 400000) {
  212. port.F = 0x20; // 100 kHz
  213. } else if (frequency < 1000000) {
  214. port.F = 0x07; // 400 kHz
  215. } else {
  216. port.F = 0x00; // 800 MHz
  217. }
  218. port.FLT = 1;
  219. #elif F_BUS == 8000000
  220. if (frequency < 400000) {
  221. port.F = 0x14; // 100 kHz
  222. } else {
  223. port.F = 0x00; // 400 kHz
  224. }
  225. port.FLT = 1;
  226. #elif F_BUS == 4000000
  227. if (frequency < 400000) {
  228. port.F = 0x07; // 100 kHz
  229. } else {
  230. port.F = 0x00; // 200 kHz
  231. }
  232. port.FLT = 1;
  233. #elif F_BUS == 2000000
  234. port.F = 0x00; // 100 kHz
  235. port.FLT = 1;
  236. #else
  237. #error "F_BUS must be 120, 108, 96, 90, 80, 72, 64, 60, 56, 54, 48, 40, 36, 24, 16, 8, 4 or 2 MHz"
  238. #endif
  239. }
  240. void TwoWire::setSDA(uint8_t pin)
  241. {
  242. if (pin == hardware.sda_pin[sda_pin_index]) return;
  243. uint32_t newindex=0;
  244. while (1) {
  245. uint32_t sda_pin = hardware.sda_pin[newindex];
  246. if (sda_pin == 255) return;
  247. if (sda_pin == pin) break;
  248. if (++newindex >= sizeof(hardware.sda_pin)) return;
  249. }
  250. if ((hardware.clock_gate_register & hardware.clock_gate_mask)) {
  251. volatile uint32_t *reg;
  252. reg = portConfigRegister(hardware.sda_pin[sda_pin_index]);
  253. *reg = 0;
  254. reg = portConfigRegister(hardware.sda_pin[newindex]);
  255. uint32_t mux = PORT_PCR_MUX(hardware.sda_mux[newindex]);
  256. *reg = mux|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  257. }
  258. sda_pin_index = newindex;
  259. }
  260. void TwoWire::setSCL(uint8_t pin)
  261. {
  262. if (pin == hardware.scl_pin[scl_pin_index]) return;
  263. uint32_t newindex=0;
  264. while (1) {
  265. uint32_t scl_pin = hardware.scl_pin[newindex];
  266. if (scl_pin == 255) return;
  267. if (scl_pin == pin) break;
  268. if (++newindex >= sizeof(hardware.scl_pin)) return;
  269. }
  270. if ((hardware.clock_gate_register & hardware.clock_gate_mask)) {
  271. volatile uint32_t *reg;
  272. reg = portConfigRegister(hardware.scl_pin[scl_pin_index]);
  273. *reg = 0;
  274. reg = portConfigRegister(hardware.scl_pin[newindex]);
  275. uint32_t mux = PORT_PCR_MUX(hardware.scl_mux[newindex]);
  276. *reg = mux|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  277. }
  278. scl_pin_index = newindex;
  279. }
  280. void TwoWire::begin(uint8_t address)
  281. {
  282. begin();
  283. port.A1 = address << 1;
  284. slave_mode = 1;
  285. port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  286. NVIC_ENABLE_IRQ(IRQ_I2C0);
  287. }
  288. void TwoWire::end()
  289. {
  290. if (!(hardware.clock_gate_register & hardware.clock_gate_mask)) return;
  291. NVIC_DISABLE_IRQ(IRQ_I2C0);
  292. // TODO: should this try to create a stop condition??
  293. port.C1 = 0;
  294. volatile uint32_t *reg;
  295. reg = portConfigRegister(hardware.scl_pin[scl_pin_index]);
  296. *reg = 0;
  297. reg = portConfigRegister(hardware.sda_pin[sda_pin_index]);
  298. *reg = 0;
  299. hardware.clock_gate_register &= ~hardware.clock_gate_mask;
  300. }
  301. void i2c0_isr(void)
  302. {
  303. uint8_t status, c1, data;
  304. static uint8_t receiving=0;
  305. status = Wire.port.S;
  306. //serial_print(".");
  307. if (status & I2C_S_ARBL) {
  308. // Arbitration Lost
  309. Wire.port.S = I2C_S_ARBL;
  310. //serial_print("a");
  311. if (receiving && Wire.rxBufferLength > 0) {
  312. // TODO: does this detect the STOP condition in slave receive mode?
  313. }
  314. if (!(status & I2C_S_IAAS)) return;
  315. }
  316. if (status & I2C_S_IAAS) {
  317. //serial_print("\n");
  318. // Addressed As A Slave
  319. if (status & I2C_S_SRW) {
  320. //serial_print("T");
  321. // Begin Slave Transmit
  322. receiving = 0;
  323. Wire.txBufferLength = 0;
  324. if (Wire.user_onRequest != NULL) {
  325. Wire.user_onRequest();
  326. }
  327. if (Wire.txBufferLength == 0) {
  328. // is this correct, transmitting a single zero
  329. // when we should send nothing? Arduino's AVR
  330. // implementation does this, but is it ok?
  331. Wire.txBufferLength = 1;
  332. Wire.txBuffer[0] = 0;
  333. }
  334. Wire.port.C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
  335. Wire.port.D = Wire.txBuffer[0];
  336. Wire.txBufferIndex = 1;
  337. } else {
  338. // Begin Slave Receive
  339. //serial_print("R");
  340. receiving = 1;
  341. Wire.rxBufferLength = 0;
  342. Wire.port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  343. data = Wire.port.D;
  344. }
  345. Wire.port.S = I2C_S_IICIF;
  346. return;
  347. }
  348. #if defined(KINETISL)
  349. c1 = Wire.port.FLT;
  350. if ((c1 & I2C_FLT_STOPF) && (c1 & I2C_FLT_STOPIE)) {
  351. Wire.port.FLT = c1 & ~I2C_FLT_STOPIE;
  352. if (Wire.user_onReceive != NULL) {
  353. Wire.rxBufferIndex = 0;
  354. Wire.user_onReceive(Wire.rxBufferLength);
  355. }
  356. }
  357. #endif
  358. c1 = Wire.port.C1;
  359. if (c1 & I2C_C1_TX) {
  360. // Continue Slave Transmit
  361. //serial_print("t");
  362. if ((status & I2C_S_RXAK) == 0) {
  363. //serial_print(".");
  364. // Master ACK'd previous byte
  365. if (Wire.txBufferIndex < Wire.txBufferLength) {
  366. Wire.port.D = Wire.txBuffer[Wire.txBufferIndex++];
  367. } else {
  368. Wire.port.D = 0;
  369. }
  370. Wire.port.C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
  371. } else {
  372. //serial_print("*");
  373. // Master did not ACK previous byte
  374. Wire.port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  375. data = Wire.port.D;
  376. }
  377. } else {
  378. // Continue Slave Receive
  379. Wire.irqcount = 0;
  380. #if defined(KINETISK)
  381. attachInterrupt(18, sda_rising_isr, RISING);
  382. #elif defined(KINETISL)
  383. Wire.port.FLT |= I2C_FLT_STOPIE;
  384. #endif
  385. //digitalWriteFast(4, HIGH);
  386. data = Wire.port.D;
  387. //serial_phex(data);
  388. if (Wire.rxBufferLength < BUFFER_LENGTH && receiving) {
  389. Wire.rxBuffer[Wire.rxBufferLength++] = data;
  390. }
  391. //digitalWriteFast(4, LOW);
  392. }
  393. Wire.port.S = I2C_S_IICIF;
  394. }
  395. // Detects the stop condition that terminates a slave receive transfer.
  396. // Sadly, the I2C in Kinetis K series lacks the stop detect interrupt
  397. // This pin change interrupt hack is needed to detect the stop condition
  398. void sda_rising_isr(void)
  399. {
  400. //digitalWrite(3, HIGH);
  401. if (!(Wire.port.S & I2C_S_BUSY)) {
  402. detachInterrupt(18);
  403. if (Wire.user_onReceive != NULL) {
  404. Wire.rxBufferIndex = 0;
  405. Wire.user_onReceive(Wire.rxBufferLength);
  406. }
  407. //delayMicroseconds(100);
  408. } else {
  409. if (++Wire.irqcount >= 2 || !Wire.slave_mode) {
  410. detachInterrupt(18);
  411. }
  412. }
  413. //digitalWrite(3, LOW);
  414. }
  415. // Chapter 44: Inter-Integrated Circuit (I2C) - Page 1012
  416. // I2C0_A1 // I2C Address Register 1
  417. // I2C0_F // I2C Frequency Divider register
  418. // I2C0_C1 // I2C Control Register 1
  419. // I2C0_S // I2C Status register
  420. // I2C0_D // I2C Data I/O register
  421. // I2C0_C2 // I2C Control Register 2
  422. // I2C0_FLT // I2C Programmable Input Glitch Filter register
  423. size_t TwoWire::write(uint8_t data)
  424. {
  425. if (transmitting || slave_mode) {
  426. if (txBufferLength >= BUFFER_LENGTH+1) {
  427. setWriteError();
  428. return 0;
  429. }
  430. txBuffer[txBufferLength++] = data;
  431. return 1;
  432. }
  433. return 0;
  434. }
  435. size_t TwoWire::write(const uint8_t *data, size_t quantity)
  436. {
  437. if (transmitting || slave_mode) {
  438. size_t avail = BUFFER_LENGTH+1 - txBufferLength;
  439. if (quantity > avail) {
  440. quantity = avail;
  441. setWriteError();
  442. }
  443. memcpy(txBuffer + txBufferLength, data, quantity);
  444. txBufferLength += quantity;
  445. return quantity;
  446. }
  447. return 0;
  448. }
  449. uint8_t TwoWire::endTransmission(uint8_t sendStop)
  450. {
  451. uint8_t i, status, ret=0;
  452. // clear the status flags
  453. port.S = I2C_S_IICIF | I2C_S_ARBL;
  454. // now take control of the bus...
  455. if (port.C1 & I2C_C1_MST) {
  456. // we are already the bus master, so send a repeated start
  457. //Serial.print("rstart:");
  458. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  459. } else {
  460. // we are not currently the bus master, so wait for bus ready
  461. //Serial.print("busy:");
  462. uint32_t wait_begin = millis();
  463. while (i2c_status() & I2C_S_BUSY) {
  464. //Serial.write('.') ;
  465. if (millis() - wait_begin > 15) {
  466. // bus stuck busy too long
  467. port.C1 = 0;
  468. port.C1 = I2C_C1_IICEN;
  469. //Serial.println("abort");
  470. return 4;
  471. }
  472. }
  473. // become the bus master in transmit mode (send start)
  474. slave_mode = 0;
  475. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  476. }
  477. // wait until start condition establishes control of the bus
  478. while (1) {
  479. status = i2c_status();
  480. if ((status & I2C_S_BUSY)) break;
  481. }
  482. // transmit the address and data
  483. for (i=0; i < txBufferLength; i++) {
  484. port.D = txBuffer[i];
  485. //Serial.write('^');
  486. while (1) {
  487. status = i2c_status();
  488. if ((status & I2C_S_IICIF)) break;
  489. if (!(status & I2C_S_BUSY)) break;
  490. }
  491. port.S = I2C_S_IICIF;
  492. //Serial.write('$');
  493. status = i2c_status();
  494. if ((status & I2C_S_ARBL)) {
  495. // we lost bus arbitration to another master
  496. // TODO: what is the proper thing to do here??
  497. //Serial.printf(" c1=%02X ", port.C1);
  498. port.C1 = I2C_C1_IICEN;
  499. ret = 4; // 4:other error
  500. break;
  501. }
  502. if (!(status & I2C_S_BUSY)) {
  503. // suddenly lost control of the bus!
  504. port.C1 = I2C_C1_IICEN;
  505. ret = 4; // 4:other error
  506. break;
  507. }
  508. if (status & I2C_S_RXAK) {
  509. // the slave device did not acknowledge
  510. if (i == 0) {
  511. ret = 2; // 2:received NACK on transmit of address
  512. } else {
  513. ret = 3; // 3:received NACK on transmit of data
  514. }
  515. sendStop = 1;
  516. break;
  517. }
  518. }
  519. if (sendStop) {
  520. // send the stop condition
  521. port.C1 = I2C_C1_IICEN;
  522. // TODO: do we wait for this somehow?
  523. }
  524. transmitting = 0;
  525. //Serial.print(" ret=");
  526. //Serial.println(ret);
  527. return ret;
  528. }
  529. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t length, uint8_t sendStop)
  530. {
  531. uint8_t tmp __attribute__((unused));
  532. uint8_t status, count=0;
  533. rxBufferIndex = 0;
  534. rxBufferLength = 0;
  535. //serial_print("requestFrom\n");
  536. // clear the status flags
  537. port.S = I2C_S_IICIF | I2C_S_ARBL;
  538. // now take control of the bus...
  539. if (port.C1 & I2C_C1_MST) {
  540. // we are already the bus master, so send a repeated start
  541. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  542. } else {
  543. // we are not currently the bus master, so wait for bus ready
  544. while (i2c_status() & I2C_S_BUSY) ;
  545. // become the bus master in transmit mode (send start)
  546. slave_mode = 0;
  547. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  548. }
  549. // send the address
  550. port.D = (address << 1) | 1;
  551. i2c_wait();
  552. status = i2c_status();
  553. if ((status & I2C_S_RXAK) || (status & I2C_S_ARBL)) {
  554. // the slave device did not acknowledge
  555. // or we lost bus arbitration to another master
  556. port.C1 = I2C_C1_IICEN;
  557. return 0;
  558. }
  559. if (length == 0) {
  560. // TODO: does anybody really do zero length reads?
  561. // if so, does this code really work?
  562. port.C1 = I2C_C1_IICEN | (sendStop ? 0 : I2C_C1_MST);
  563. return 0;
  564. } else if (length == 1) {
  565. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
  566. } else {
  567. port.C1 = I2C_C1_IICEN | I2C_C1_MST;
  568. }
  569. tmp = port.D; // initiate the first receive
  570. while (length > 1) {
  571. i2c_wait();
  572. length--;
  573. if (length == 1) port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
  574. if (count < BUFFER_LENGTH) {
  575. rxBuffer[count++] = port.D;
  576. } else {
  577. tmp = port.D;
  578. }
  579. }
  580. i2c_wait();
  581. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  582. if (count < BUFFER_LENGTH) {
  583. rxBuffer[count++] = port.D;
  584. } else {
  585. tmp = port.D;
  586. }
  587. if (sendStop) port.C1 = I2C_C1_IICEN;
  588. rxBufferLength = count;
  589. return count;
  590. }
  591. const TwoWire::I2C_Hardware_t TwoWire::i2c0_hardware = {
  592. SIM_SCGC4, SIM_SCGC4_I2C0,
  593. #if defined(__MKL26Z64__) || defined(__MK20DX128__) || defined(__MK20DX256__)
  594. 18, 17, 255, 255, 255,
  595. 2, 2, 0, 0, 0,
  596. 19, 16, 255, 255, 255,
  597. 2, 2, 0, 0, 0,
  598. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  599. 18, 17, 34, 8, 48,
  600. 2, 2, 5, 7, 2,
  601. 19, 16, 33, 7, 47,
  602. 2, 2, 5, 7, 2,
  603. #endif
  604. };
  605. const TwoWire::I2C_Hardware_t TwoWire::i2c1_hardware = {
  606. SIM_SCGC4, SIM_SCGC4_I2C1,
  607. #if defined(__MKL26Z64__)
  608. 23, 255, 255, 255, 255,
  609. 2, 0, 0, 0, 0,
  610. 22, 255, 255, 255, 255,
  611. 2, 0, 0, 0, 0,
  612. #elif defined(__MK20DX256__)
  613. 30, 255, 255, 255, 255,
  614. 2, 0, 0, 0, 0,
  615. 29, 255, 255, 255, 255,
  616. 2, 0, 0, 0, 0,
  617. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  618. 38, 255, 255, 255, 255,
  619. 2, 0, 0, 0, 0,
  620. 37, 255, 255, 255, 255,
  621. 2, 0, 0, 0, 0,
  622. #else
  623. 255, 255, 255, 255, 255,
  624. 0, 0, 0, 0, 0,
  625. 255, 255, 255, 255, 255,
  626. 0, 0, 0, 0, 0,
  627. #endif
  628. };
  629. const TwoWire::I2C_Hardware_t TwoWire::i2c2_hardware = {
  630. SIM_SCGC1, SIM_SCGC1_I2C2,
  631. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  632. 4, 255, 255, 255, 255,
  633. 5, 0, 0, 0, 0,
  634. 3, 26, 255, 255, 255,
  635. 5, 5, 0, 0, 0,
  636. #else
  637. 255, 255, 255, 255, 255,
  638. 0, 0, 0, 0, 0,
  639. 255, 255, 255, 255, 255,
  640. 0, 0, 0, 0, 0,
  641. #endif
  642. };
  643. const TwoWire::I2C_Hardware_t TwoWire::i2c3_hardware = {
  644. SIM_SCGC1, SIM_SCGC1_I2C3,
  645. #if defined(__MK66FX1M0__)
  646. 56, 255, 255, 255, 255,
  647. 2, 0, 0, 0, 0,
  648. 57, 255, 255, 255, 255,
  649. 2, 0, 0, 0, 0,
  650. #else
  651. 255, 255, 255, 255, 255,
  652. 0, 0, 0, 0, 0,
  653. 255, 255, 255, 255, 255,
  654. 0, 0, 0, 0, 0,
  655. #endif
  656. };
  657. TwoWire Wire(KINETIS_I2C0, TwoWire::i2c0_hardware);
  658. TwoWire Wire1(KINETIS_I2C1, TwoWire::i2c1_hardware);
  659. #endif // __arm__ && TEENSYDUINO