選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

701 行
18KB

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