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.

932 lines
25KB

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