Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

781 rinda
19KB

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