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.

706 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. Wire.isr();
  304. }
  305. void TwoWire::isr(void)
  306. {
  307. uint8_t status, c1, data;
  308. static uint8_t receiving=0;
  309. status = port.S;
  310. //serial_print(".");
  311. if (status & I2C_S_ARBL) {
  312. // Arbitration Lost
  313. port.S = I2C_S_ARBL;
  314. //serial_print("a");
  315. if (receiving && rxBufferLength > 0) {
  316. // TODO: does this detect the STOP condition in slave receive mode?
  317. }
  318. if (!(status & I2C_S_IAAS)) return;
  319. }
  320. if (status & I2C_S_IAAS) {
  321. //serial_print("\n");
  322. // Addressed As A Slave
  323. if (status & I2C_S_SRW) {
  324. //serial_print("T");
  325. // Begin Slave Transmit
  326. receiving = 0;
  327. txBufferLength = 0;
  328. if (user_onRequest != NULL) {
  329. user_onRequest();
  330. }
  331. if (txBufferLength == 0) {
  332. // is this correct, transmitting a single zero
  333. // when we should send nothing? Arduino's AVR
  334. // implementation does this, but is it ok?
  335. txBufferLength = 1;
  336. txBuffer[0] = 0;
  337. }
  338. port.C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
  339. port.D = txBuffer[0];
  340. txBufferIndex = 1;
  341. } else {
  342. // Begin Slave Receive
  343. //serial_print("R");
  344. receiving = 1;
  345. rxBufferLength = 0;
  346. port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  347. data = port.D;
  348. }
  349. port.S = I2C_S_IICIF;
  350. return;
  351. }
  352. #if defined(KINETISL)
  353. c1 = port.FLT;
  354. if ((c1 & I2C_FLT_STOPF) && (c1 & I2C_FLT_STOPIE)) {
  355. port.FLT = c1 & ~I2C_FLT_STOPIE;
  356. if (user_onReceive != NULL) {
  357. rxBufferIndex = 0;
  358. user_onReceive(rxBufferLength);
  359. }
  360. }
  361. #endif
  362. c1 = port.C1;
  363. if (c1 & I2C_C1_TX) {
  364. // Continue Slave Transmit
  365. //serial_print("t");
  366. if ((status & I2C_S_RXAK) == 0) {
  367. //serial_print(".");
  368. // Master ACK'd previous byte
  369. if (txBufferIndex < txBufferLength) {
  370. port.D = txBuffer[txBufferIndex++];
  371. } else {
  372. port.D = 0;
  373. }
  374. port.C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
  375. } else {
  376. //serial_print("*");
  377. // Master did not ACK previous byte
  378. port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  379. data = port.D;
  380. }
  381. } else {
  382. // Continue Slave Receive
  383. irqcount = 0;
  384. #if defined(KINETISK)
  385. attachInterrupt(hardware.sda_pin[sda_pin_index], sda_rising_isr, RISING);
  386. #elif defined(KINETISL)
  387. port.FLT |= I2C_FLT_STOPIE;
  388. #endif
  389. //digitalWriteFast(4, HIGH);
  390. data = port.D;
  391. //serial_phex(data);
  392. if (rxBufferLength < BUFFER_LENGTH && receiving) {
  393. rxBuffer[rxBufferLength++] = data;
  394. }
  395. //digitalWriteFast(4, LOW);
  396. }
  397. port.S = I2C_S_IICIF;
  398. }
  399. // Detects the stop condition that terminates a slave receive transfer.
  400. // Sadly, the I2C in Kinetis K series lacks the stop detect interrupt
  401. // This pin change interrupt hack is needed to detect the stop condition
  402. void sda_rising_isr(void)
  403. {
  404. //digitalWrite(3, HIGH);
  405. if (!(Wire.port.S & I2C_S_BUSY)) {
  406. detachInterrupt(Wire.hardware.sda_pin[Wire.sda_pin_index]);
  407. if (Wire.user_onReceive != NULL) {
  408. Wire.rxBufferIndex = 0;
  409. Wire.user_onReceive(Wire.rxBufferLength);
  410. }
  411. //delayMicroseconds(100);
  412. } else {
  413. if (++Wire.irqcount >= 2 || !Wire.slave_mode) {
  414. detachInterrupt(Wire.hardware.sda_pin[Wire.sda_pin_index]);
  415. }
  416. }
  417. //digitalWrite(3, LOW);
  418. }
  419. // Chapter 44: Inter-Integrated Circuit (I2C) - Page 1012
  420. // I2C0_A1 // I2C Address Register 1
  421. // I2C0_F // I2C Frequency Divider register
  422. // I2C0_C1 // I2C Control Register 1
  423. // I2C0_S // I2C Status register
  424. // I2C0_D // I2C Data I/O register
  425. // I2C0_C2 // I2C Control Register 2
  426. // I2C0_FLT // I2C Programmable Input Glitch Filter register
  427. size_t TwoWire::write(uint8_t data)
  428. {
  429. if (transmitting || slave_mode) {
  430. if (txBufferLength >= BUFFER_LENGTH+1) {
  431. setWriteError();
  432. return 0;
  433. }
  434. txBuffer[txBufferLength++] = data;
  435. return 1;
  436. }
  437. return 0;
  438. }
  439. size_t TwoWire::write(const uint8_t *data, size_t quantity)
  440. {
  441. if (transmitting || slave_mode) {
  442. size_t avail = BUFFER_LENGTH+1 - txBufferLength;
  443. if (quantity > avail) {
  444. quantity = avail;
  445. setWriteError();
  446. }
  447. memcpy(txBuffer + txBufferLength, data, quantity);
  448. txBufferLength += quantity;
  449. return quantity;
  450. }
  451. return 0;
  452. }
  453. uint8_t TwoWire::endTransmission(uint8_t sendStop)
  454. {
  455. uint8_t i, status, ret=0;
  456. // clear the status flags
  457. port.S = I2C_S_IICIF | I2C_S_ARBL;
  458. // now take control of the bus...
  459. if (port.C1 & I2C_C1_MST) {
  460. // we are already the bus master, so send a repeated start
  461. //Serial.print("rstart:");
  462. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  463. } else {
  464. // we are not currently the bus master, so wait for bus ready
  465. //Serial.print("busy:");
  466. uint32_t wait_begin = millis();
  467. while (i2c_status() & I2C_S_BUSY) {
  468. //Serial.write('.') ;
  469. if (millis() - wait_begin > 15) {
  470. // bus stuck busy too long
  471. port.C1 = 0;
  472. port.C1 = I2C_C1_IICEN;
  473. //Serial.println("abort");
  474. return 4;
  475. }
  476. }
  477. // become the bus master in transmit mode (send start)
  478. slave_mode = 0;
  479. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  480. }
  481. // wait until start condition establishes control of the bus
  482. while (1) {
  483. status = i2c_status();
  484. if ((status & I2C_S_BUSY)) break;
  485. }
  486. // transmit the address and data
  487. for (i=0; i < txBufferLength; i++) {
  488. port.D = txBuffer[i];
  489. //Serial.write('^');
  490. while (1) {
  491. status = i2c_status();
  492. if ((status & I2C_S_IICIF)) break;
  493. if (!(status & I2C_S_BUSY)) break;
  494. }
  495. port.S = I2C_S_IICIF;
  496. //Serial.write('$');
  497. status = i2c_status();
  498. if ((status & I2C_S_ARBL)) {
  499. // we lost bus arbitration to another master
  500. // TODO: what is the proper thing to do here??
  501. //Serial.printf(" c1=%02X ", port.C1);
  502. port.C1 = I2C_C1_IICEN;
  503. ret = 4; // 4:other error
  504. break;
  505. }
  506. if (!(status & I2C_S_BUSY)) {
  507. // suddenly lost control of the bus!
  508. port.C1 = I2C_C1_IICEN;
  509. ret = 4; // 4:other error
  510. break;
  511. }
  512. if (status & I2C_S_RXAK) {
  513. // the slave device did not acknowledge
  514. if (i == 0) {
  515. ret = 2; // 2:received NACK on transmit of address
  516. } else {
  517. ret = 3; // 3:received NACK on transmit of data
  518. }
  519. sendStop = 1;
  520. break;
  521. }
  522. }
  523. if (sendStop) {
  524. // send the stop condition
  525. port.C1 = I2C_C1_IICEN;
  526. // TODO: do we wait for this somehow?
  527. }
  528. transmitting = 0;
  529. //Serial.print(" ret=");
  530. //Serial.println(ret);
  531. return ret;
  532. }
  533. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t length, uint8_t sendStop)
  534. {
  535. uint8_t tmp __attribute__((unused));
  536. uint8_t status, count=0;
  537. rxBufferIndex = 0;
  538. rxBufferLength = 0;
  539. //serial_print("requestFrom\n");
  540. // clear the status flags
  541. port.S = I2C_S_IICIF | I2C_S_ARBL;
  542. // now take control of the bus...
  543. if (port.C1 & I2C_C1_MST) {
  544. // we are already the bus master, so send a repeated start
  545. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  546. } else {
  547. // we are not currently the bus master, so wait for bus ready
  548. while (i2c_status() & I2C_S_BUSY) ;
  549. // become the bus master in transmit mode (send start)
  550. slave_mode = 0;
  551. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  552. }
  553. // send the address
  554. port.D = (address << 1) | 1;
  555. i2c_wait();
  556. status = i2c_status();
  557. if ((status & I2C_S_RXAK) || (status & I2C_S_ARBL)) {
  558. // the slave device did not acknowledge
  559. // or we lost bus arbitration to another master
  560. port.C1 = I2C_C1_IICEN;
  561. return 0;
  562. }
  563. if (length == 0) {
  564. // TODO: does anybody really do zero length reads?
  565. // if so, does this code really work?
  566. port.C1 = I2C_C1_IICEN | (sendStop ? 0 : I2C_C1_MST);
  567. return 0;
  568. } else if (length == 1) {
  569. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
  570. } else {
  571. port.C1 = I2C_C1_IICEN | I2C_C1_MST;
  572. }
  573. tmp = port.D; // initiate the first receive
  574. while (length > 1) {
  575. i2c_wait();
  576. length--;
  577. if (length == 1) port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
  578. if (count < BUFFER_LENGTH) {
  579. rxBuffer[count++] = port.D;
  580. } else {
  581. tmp = port.D;
  582. }
  583. }
  584. i2c_wait();
  585. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  586. if (count < BUFFER_LENGTH) {
  587. rxBuffer[count++] = port.D;
  588. } else {
  589. tmp = port.D;
  590. }
  591. if (sendStop) port.C1 = I2C_C1_IICEN;
  592. rxBufferLength = count;
  593. return count;
  594. }
  595. #ifdef WIRE_IMPLEMENT_WIRE
  596. const TwoWire::I2C_Hardware_t TwoWire::i2c0_hardware = {
  597. SIM_SCGC4, SIM_SCGC4_I2C0,
  598. #if defined(__MKL26Z64__) || defined(__MK20DX128__) || defined(__MK20DX256__)
  599. 18, 17, 255, 255, 255,
  600. 2, 2, 0, 0, 0,
  601. 19, 16, 255, 255, 255,
  602. 2, 2, 0, 0, 0,
  603. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  604. 18, 17, 34, 8, 48,
  605. 2, 2, 5, 7, 2,
  606. 19, 16, 33, 7, 47,
  607. 2, 2, 5, 7, 2,
  608. #endif
  609. };
  610. TwoWire Wire(KINETIS_I2C0, TwoWire::i2c0_hardware);
  611. #endif // WIRE_IMPLEMENT_WIRE
  612. #ifdef WIRE_IMPLEMENT_WIRE1
  613. const TwoWire::I2C_Hardware_t TwoWire::i2c1_hardware = {
  614. SIM_SCGC4, SIM_SCGC4_I2C1,
  615. #if defined(__MKL26Z64__)
  616. 23, 255, 255, 255, 255,
  617. 2, 0, 0, 0, 0,
  618. 22, 255, 255, 255, 255,
  619. 2, 0, 0, 0, 0,
  620. #elif defined(__MK20DX256__)
  621. 30, 255, 255, 255, 255,
  622. 2, 0, 0, 0, 0,
  623. 29, 255, 255, 255, 255,
  624. 2, 0, 0, 0, 0,
  625. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  626. 38, 255, 255, 255, 255,
  627. 2, 0, 0, 0, 0,
  628. 37, 255, 255, 255, 255,
  629. 2, 0, 0, 0, 0,
  630. #endif
  631. };
  632. TwoWire Wire1(KINETIS_I2C1, TwoWire::i2c1_hardware);
  633. #endif // WIRE_IMPLEMENT_WIRE1
  634. #ifdef WIRE_IMPLEMENT_WIRE2
  635. const TwoWire::I2C_Hardware_t TwoWire::i2c2_hardware = {
  636. SIM_SCGC1, SIM_SCGC1_I2C2,
  637. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  638. 4, 255, 255, 255, 255,
  639. 5, 0, 0, 0, 0,
  640. 3, 26, 255, 255, 255,
  641. 5, 5, 0, 0, 0,
  642. #endif
  643. };
  644. #endif // WIRE_IMPLEMENT_WIRE2
  645. #ifdef WIRE_IMPLEMENT_WIRE3
  646. const TwoWire::I2C_Hardware_t TwoWire::i2c3_hardware = {
  647. SIM_SCGC1, SIM_SCGC1_I2C3,
  648. #if defined(__MK66FX1M0__)
  649. 56, 255, 255, 255, 255,
  650. 2, 0, 0, 0, 0,
  651. 57, 255, 255, 255, 255,
  652. 2, 0, 0, 0, 0,
  653. #endif
  654. };
  655. #endif // WIRE_IMPLEMENT_WIRE3
  656. #endif // __arm__ && TEENSYDUINO