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.

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