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.

795 lines
21KB

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