Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

694 lines
17KB

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