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.

928 lines
22KB

  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(CORE_TEENSY)
  19. #include "kinetis.h"
  20. #include <string.h> // for memcpy
  21. #include "core_pins.h"
  22. //#include "HardwareSerial.h"
  23. #include "Wire.h"
  24. uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
  25. uint8_t TwoWire::rxBufferIndex = 0;
  26. uint8_t TwoWire::rxBufferLength = 0;
  27. uint8_t TwoWire::txBuffer[BUFFER_LENGTH+1];
  28. uint8_t TwoWire::txBufferIndex = 0;
  29. uint8_t TwoWire::txBufferLength = 0;
  30. uint8_t TwoWire::transmitting = 0;
  31. uint8_t TwoWire::sda_pin_num = 18;
  32. uint8_t TwoWire::scl_pin_num = 19;
  33. void (*TwoWire::user_onRequest)(void) = NULL;
  34. void (*TwoWire::user_onReceive)(int) = NULL;
  35. TwoWire::TwoWire()
  36. {
  37. }
  38. static uint8_t slave_mode = 0;
  39. static uint8_t irqcount=0;
  40. void TwoWire::begin(void)
  41. {
  42. //serial_begin(BAUD2DIV(115200));
  43. //serial_print("\nWire Begin\n");
  44. slave_mode = 0;
  45. SIM_SCGC4 |= SIM_SCGC4_I2C0; // TODO: use bitband
  46. I2C0_C1 = 0;
  47. // On Teensy 3.0 external pullup resistors *MUST* be used
  48. // the PORT_PCR_PE bit is ignored when in I2C mode
  49. // I2C will not work at all without pullup resistors
  50. // It might seem like setting PORT_PCR_PE & PORT_PCR_PS
  51. // would enable pullup resistors. However, there seems
  52. // to be a bug in chip while I2C is enabled, where setting
  53. // those causes the port to be driven strongly high.
  54. if (sda_pin_num == 18) {
  55. CORE_PIN18_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  56. } else if (sda_pin_num == 17) {
  57. CORE_PIN17_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  58. }
  59. if (scl_pin_num == 19) {
  60. CORE_PIN19_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  61. } else if (scl_pin_num == 16) {
  62. CORE_PIN16_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  63. }
  64. setClock(100000);
  65. I2C0_C2 = I2C_C2_HDRS;
  66. I2C0_C1 = I2C_C1_IICEN;
  67. //pinMode(3, OUTPUT);
  68. //pinMode(4, OUTPUT);
  69. }
  70. void TwoWire::setClock(uint32_t frequency)
  71. {
  72. #if F_BUS == 60000000
  73. if (frequency < 400000) {
  74. I2C0_F = 0x2C; // 104 kHz
  75. } else if (frequency < 1000000) {
  76. I2C0_F = 0x1C; // 416 kHz
  77. } else {
  78. I2C0_F = 0x12; // 938 kHz
  79. }
  80. I2C0_FLT = 4;
  81. #elif F_BUS == 56000000
  82. if (frequency < 400000) {
  83. I2C0_F = 0x2B; // 109 kHz
  84. } else if (frequency < 1000000) {
  85. I2C0_F = 0x1C; // 389 kHz
  86. } else {
  87. I2C0_F = 0x0E; // 1 MHz
  88. }
  89. I2C0_FLT = 4;
  90. #elif F_BUS == 48000000
  91. if (frequency < 400000) {
  92. I2C0_F = 0x27; // 100 kHz
  93. } else if (frequency < 1000000) {
  94. I2C0_F = 0x1A; // 400 kHz
  95. } else {
  96. I2C0_F = 0x0D; // 1 MHz
  97. }
  98. I2C0_FLT = 4;
  99. #elif F_BUS == 40000000
  100. if (frequency < 400000) {
  101. I2C0_F = 0x29; // 104 kHz
  102. } else if (frequency < 1000000) {
  103. I2C0_F = 0x19; // 416 kHz
  104. } else {
  105. I2C0_F = 0x0B; // 1 MHz
  106. }
  107. I2C0_FLT = 3;
  108. #elif F_BUS == 36000000
  109. if (frequency < 400000) {
  110. I2C0_F = 0x28; // 113 kHz
  111. } else if (frequency < 1000000) {
  112. I2C0_F = 0x19; // 375 kHz
  113. } else {
  114. I2C0_F = 0x0A; // 1 MHz
  115. }
  116. I2C0_FLT = 3;
  117. #elif F_BUS == 24000000
  118. if (frequency < 400000) {
  119. I2C0_F = 0x1F; // 100 kHz
  120. } else if (frequency < 1000000) {
  121. I2C0_F = 0x12; // 375 kHz
  122. } else {
  123. I2C0_F = 0x02; // 1 MHz
  124. }
  125. I2C0_FLT = 2;
  126. #elif F_BUS == 16000000
  127. if (frequency < 400000) {
  128. I2C0_F = 0x20; // 100 kHz
  129. } else if (frequency < 1000000) {
  130. I2C0_F = 0x07; // 400 kHz
  131. } else {
  132. I2C0_F = 0x00; // 800 MHz
  133. }
  134. I2C0_FLT = 1;
  135. #elif F_BUS == 8000000
  136. if (frequency < 400000) {
  137. I2C0_F = 0x14; // 100 kHz
  138. } else {
  139. I2C0_F = 0x00; // 400 kHz
  140. }
  141. I2C0_FLT = 1;
  142. #elif F_BUS == 4000000
  143. if (frequency < 400000) {
  144. I2C0_F = 0x07; // 100 kHz
  145. } else {
  146. I2C0_F = 0x00; // 200 kHz
  147. }
  148. I2C0_FLT = 1;
  149. #elif F_BUS == 2000000
  150. I2C0_F = 0x00; // 100 kHz
  151. I2C0_FLT = 1;
  152. #else
  153. #error "F_BUS must be 60, 56, 48, 40, 36, 24, 16, 8, 4 or 2 MHz"
  154. #endif
  155. }
  156. void TwoWire::setSDA(uint8_t pin)
  157. {
  158. if (pin == sda_pin_num) return;
  159. if ((SIM_SCGC4 & SIM_SCGC4_I2C0)) {
  160. if (sda_pin_num == 18) {
  161. CORE_PIN18_CONFIG = 0;
  162. } else if (sda_pin_num == 17) {
  163. CORE_PIN17_CONFIG = 0;
  164. }
  165. if (pin == 18) {
  166. CORE_PIN18_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  167. } else if (pin == 17) {
  168. CORE_PIN17_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  169. }
  170. }
  171. sda_pin_num = pin;
  172. }
  173. void TwoWire::setSCL(uint8_t pin)
  174. {
  175. if (pin == scl_pin_num) return;
  176. if ((SIM_SCGC4 & SIM_SCGC4_I2C0)) {
  177. if (scl_pin_num == 19) {
  178. CORE_PIN19_CONFIG = 0;
  179. } else if (scl_pin_num == 16) {
  180. CORE_PIN16_CONFIG = 0;
  181. }
  182. if (pin == 19) {
  183. CORE_PIN19_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  184. } else if (pin == 16) {
  185. CORE_PIN16_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  186. }
  187. }
  188. scl_pin_num = pin;
  189. }
  190. void TwoWire::begin(uint8_t address)
  191. {
  192. begin();
  193. I2C0_A1 = address << 1;
  194. slave_mode = 1;
  195. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  196. NVIC_ENABLE_IRQ(IRQ_I2C0);
  197. }
  198. void TwoWire::end()
  199. {
  200. NVIC_DISABLE_IRQ(IRQ_I2C0);
  201. I2C0_C1 = 0;
  202. CORE_PIN18_CONFIG = 0;
  203. CORE_PIN19_CONFIG = 0;
  204. SIM_SCGC4 &= ~SIM_SCGC4_I2C0; // TODO: use bitband
  205. }
  206. void i2c0_isr(void)
  207. {
  208. uint8_t status, c1, data;
  209. static uint8_t receiving=0;
  210. status = I2C0_S;
  211. //serial_print(".");
  212. if (status & I2C_S_ARBL) {
  213. // Arbitration Lost
  214. I2C0_S = I2C_S_ARBL;
  215. //serial_print("a");
  216. if (receiving && TwoWire::rxBufferLength > 0) {
  217. // TODO: does this detect the STOP condition in slave receive mode?
  218. }
  219. if (!(status & I2C_S_IAAS)) return;
  220. }
  221. if (status & I2C_S_IAAS) {
  222. //serial_print("\n");
  223. // Addressed As A Slave
  224. if (status & I2C_S_SRW) {
  225. //serial_print("T");
  226. // Begin Slave Transmit
  227. receiving = 0;
  228. TwoWire::txBufferLength = 0;
  229. if (TwoWire::user_onRequest != NULL) {
  230. TwoWire::user_onRequest();
  231. }
  232. if (TwoWire::txBufferLength == 0) {
  233. // is this correct, transmitting a single zero
  234. // when we should send nothing? Arduino's AVR
  235. // implementation does this, but is it ok?
  236. TwoWire::txBufferLength = 1;
  237. TwoWire::txBuffer[0] = 0;
  238. }
  239. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
  240. I2C0_D = TwoWire::txBuffer[0];
  241. TwoWire::txBufferIndex = 1;
  242. } else {
  243. // Begin Slave Receive
  244. //serial_print("R");
  245. receiving = 1;
  246. TwoWire::rxBufferLength = 0;
  247. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  248. data = I2C0_D;
  249. }
  250. I2C0_S = I2C_S_IICIF;
  251. return;
  252. }
  253. #if defined(KINETISL)
  254. c1 = I2C0_FLT;
  255. if ((c1 & I2C_FLT_STOPF) && (c1 & I2C_FLT_STOPIE)) {
  256. I2C0_FLT = c1 & ~I2C_FLT_STOPIE;
  257. if (TwoWire::user_onReceive != NULL) {
  258. TwoWire::rxBufferIndex = 0;
  259. TwoWire::user_onReceive(TwoWire::rxBufferLength);
  260. }
  261. }
  262. #endif
  263. c1 = I2C0_C1;
  264. if (c1 & I2C_C1_TX) {
  265. // Continue Slave Transmit
  266. //serial_print("t");
  267. if ((status & I2C_S_RXAK) == 0) {
  268. //serial_print(".");
  269. // Master ACK'd previous byte
  270. if (TwoWire::txBufferIndex < TwoWire::txBufferLength) {
  271. I2C0_D = TwoWire::txBuffer[TwoWire::txBufferIndex++];
  272. } else {
  273. I2C0_D = 0;
  274. }
  275. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
  276. } else {
  277. //serial_print("*");
  278. // Master did not ACK previous byte
  279. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  280. data = I2C0_D;
  281. }
  282. } else {
  283. // Continue Slave Receive
  284. irqcount = 0;
  285. #if defined(KINETISK)
  286. attachInterrupt(18, TwoWire::sda_rising_isr, RISING);
  287. #elif defined(KINETISL)
  288. I2C0_FLT |= I2C_FLT_STOPIE;
  289. #endif
  290. //digitalWriteFast(4, HIGH);
  291. data = I2C0_D;
  292. //serial_phex(data);
  293. if (TwoWire::rxBufferLength < BUFFER_LENGTH && receiving) {
  294. TwoWire::rxBuffer[TwoWire::rxBufferLength++] = data;
  295. }
  296. //digitalWriteFast(4, LOW);
  297. }
  298. I2C0_S = I2C_S_IICIF;
  299. }
  300. // Detects the stop condition that terminates a slave receive transfer.
  301. // Sadly, the I2C in Kinetis K series lacks the stop detect interrupt
  302. // This pin change interrupt hack is needed to detect the stop condition
  303. void TwoWire::sda_rising_isr(void)
  304. {
  305. //digitalWrite(3, HIGH);
  306. if (!(I2C0_S & I2C_S_BUSY)) {
  307. detachInterrupt(18);
  308. if (user_onReceive != NULL) {
  309. rxBufferIndex = 0;
  310. user_onReceive(rxBufferLength);
  311. }
  312. //delayMicroseconds(100);
  313. } else {
  314. if (++irqcount >= 2 || !slave_mode) {
  315. detachInterrupt(18);
  316. }
  317. }
  318. //digitalWrite(3, LOW);
  319. }
  320. // Chapter 44: Inter-Integrated Circuit (I2C) - Page 1012
  321. // I2C0_A1 // I2C Address Register 1
  322. // I2C0_F // I2C Frequency Divider register
  323. // I2C0_C1 // I2C Control Register 1
  324. // I2C0_S // I2C Status register
  325. // I2C0_D // I2C Data I/O register
  326. // I2C0_C2 // I2C Control Register 2
  327. // I2C0_FLT // I2C Programmable Input Glitch Filter register
  328. static uint8_t i2c_status(void)
  329. {
  330. static uint32_t p=0xFFFF;
  331. uint32_t s = I2C0_S;
  332. if (s != p) {
  333. //Serial.printf("(%02X)", s);
  334. p = s;
  335. }
  336. return s;
  337. }
  338. static void i2c_wait(void)
  339. {
  340. #if 0
  341. while (!(I2C0_S & I2C_S_IICIF)) ; // wait
  342. I2C0_S = I2C_S_IICIF;
  343. #endif
  344. //Serial.write('^');
  345. while (1) {
  346. if ((i2c_status() & I2C_S_IICIF)) break;
  347. }
  348. I2C0_S = I2C_S_IICIF;
  349. }
  350. void TwoWire::beginTransmission(uint8_t address)
  351. {
  352. txBuffer[0] = (address << 1);
  353. transmitting = 1;
  354. txBufferLength = 1;
  355. }
  356. size_t TwoWire::write(uint8_t data)
  357. {
  358. if (transmitting || slave_mode) {
  359. if (txBufferLength >= BUFFER_LENGTH+1) {
  360. setWriteError();
  361. return 0;
  362. }
  363. txBuffer[txBufferLength++] = data;
  364. return 1;
  365. }
  366. return 0;
  367. }
  368. size_t TwoWire::write(const uint8_t *data, size_t quantity)
  369. {
  370. if (transmitting || slave_mode) {
  371. size_t avail = BUFFER_LENGTH+1 - txBufferLength;
  372. if (quantity > avail) {
  373. quantity = avail;
  374. setWriteError();
  375. }
  376. memcpy(txBuffer + txBufferLength, data, quantity);
  377. txBufferLength += quantity;
  378. return quantity;
  379. }
  380. return 0;
  381. }
  382. void TwoWire::flush(void)
  383. {
  384. }
  385. uint8_t TwoWire::endTransmission(uint8_t sendStop)
  386. {
  387. uint8_t i, status, ret=0;
  388. // clear the status flags
  389. I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
  390. // now take control of the bus...
  391. if (I2C0_C1 & I2C_C1_MST) {
  392. // we are already the bus master, so send a repeated start
  393. //Serial.print("rstart:");
  394. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  395. } else {
  396. // we are not currently the bus master, so wait for bus ready
  397. //Serial.print("busy:");
  398. uint32_t wait_begin = millis();
  399. while (i2c_status() & I2C_S_BUSY) {
  400. //Serial.write('.') ;
  401. if (millis() - wait_begin > 15) {
  402. // bus stuck busy too long
  403. I2C0_C1 = 0;
  404. I2C0_C1 = I2C_C1_IICEN;
  405. //Serial.println("abort");
  406. return 4;
  407. }
  408. }
  409. // become the bus master in transmit mode (send start)
  410. slave_mode = 0;
  411. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  412. }
  413. // wait until start condition establishes control of the bus
  414. while (1) {
  415. status = i2c_status();
  416. if ((status & I2C_S_BUSY)) break;
  417. }
  418. // transmit the address and data
  419. for (i=0; i < txBufferLength; i++) {
  420. I2C0_D = txBuffer[i];
  421. //Serial.write('^');
  422. while (1) {
  423. status = i2c_status();
  424. if ((status & I2C_S_IICIF)) break;
  425. if (!(status & I2C_S_BUSY)) break;
  426. }
  427. I2C0_S = I2C_S_IICIF;
  428. //Serial.write('$');
  429. status = i2c_status();
  430. if ((status & I2C_S_ARBL)) {
  431. // we lost bus arbitration to another master
  432. // TODO: what is the proper thing to do here??
  433. //Serial.printf(" c1=%02X ", I2C0_C1);
  434. I2C0_C1 = I2C_C1_IICEN;
  435. ret = 4; // 4:other error
  436. break;
  437. }
  438. if (!(status & I2C_S_BUSY)) {
  439. // suddenly lost control of the bus!
  440. I2C0_C1 = I2C_C1_IICEN;
  441. ret = 4; // 4:other error
  442. break;
  443. }
  444. if (status & I2C_S_RXAK) {
  445. // the slave device did not acknowledge
  446. if (i == 0) {
  447. ret = 2; // 2:received NACK on transmit of address
  448. } else {
  449. ret = 3; // 3:received NACK on transmit of data
  450. }
  451. sendStop = 1;
  452. break;
  453. }
  454. }
  455. if (sendStop) {
  456. // send the stop condition
  457. I2C0_C1 = I2C_C1_IICEN;
  458. // TODO: do we wait for this somehow?
  459. }
  460. transmitting = 0;
  461. //Serial.print(" ret=");
  462. //Serial.println(ret);
  463. return ret;
  464. }
  465. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t length, uint8_t sendStop)
  466. {
  467. uint8_t tmp __attribute__((unused));
  468. uint8_t status, count=0;
  469. rxBufferIndex = 0;
  470. rxBufferLength = 0;
  471. //serial_print("requestFrom\n");
  472. // clear the status flags
  473. I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
  474. // now take control of the bus...
  475. if (I2C0_C1 & I2C_C1_MST) {
  476. // we are already the bus master, so send a repeated start
  477. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  478. } else {
  479. // we are not currently the bus master, so wait for bus ready
  480. while (i2c_status() & I2C_S_BUSY) ;
  481. // become the bus master in transmit mode (send start)
  482. slave_mode = 0;
  483. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  484. }
  485. // send the address
  486. I2C0_D = (address << 1) | 1;
  487. i2c_wait();
  488. status = i2c_status();
  489. if ((status & I2C_S_RXAK) || (status & I2C_S_ARBL)) {
  490. // the slave device did not acknowledge
  491. // or we lost bus arbitration to another master
  492. I2C0_C1 = I2C_C1_IICEN;
  493. return 0;
  494. }
  495. if (length == 0) {
  496. // TODO: does anybody really do zero length reads?
  497. // if so, does this code really work?
  498. I2C0_C1 = I2C_C1_IICEN | (sendStop ? 0 : I2C_C1_MST);
  499. return 0;
  500. } else if (length == 1) {
  501. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
  502. } else {
  503. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST;
  504. }
  505. tmp = I2C0_D; // initiate the first receive
  506. while (length > 1) {
  507. i2c_wait();
  508. length--;
  509. if (length == 1) I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
  510. rxBuffer[count++] = I2C0_D;
  511. }
  512. i2c_wait();
  513. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  514. rxBuffer[count++] = I2C0_D;
  515. if (sendStop) I2C0_C1 = I2C_C1_IICEN;
  516. rxBufferLength = count;
  517. return count;
  518. }
  519. int TwoWire::available(void)
  520. {
  521. return rxBufferLength - rxBufferIndex;
  522. }
  523. int TwoWire::read(void)
  524. {
  525. if (rxBufferIndex >= rxBufferLength) return -1;
  526. return rxBuffer[rxBufferIndex++];
  527. }
  528. int TwoWire::peek(void)
  529. {
  530. if (rxBufferIndex >= rxBufferLength) return -1;
  531. return rxBuffer[rxBufferIndex];
  532. }
  533. // alternate function prototypes
  534. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
  535. {
  536. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  537. }
  538. uint8_t TwoWire::requestFrom(int address, int quantity)
  539. {
  540. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  541. }
  542. uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
  543. {
  544. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
  545. }
  546. void TwoWire::beginTransmission(int address)
  547. {
  548. beginTransmission((uint8_t)address);
  549. }
  550. uint8_t TwoWire::endTransmission(void)
  551. {
  552. return endTransmission(true);
  553. }
  554. void TwoWire::begin(int address)
  555. {
  556. begin((uint8_t)address);
  557. }
  558. void TwoWire::onReceive( void (*function)(int) )
  559. {
  560. user_onReceive = function;
  561. }
  562. void TwoWire::onRequest( void (*function)(void) )
  563. {
  564. user_onRequest = function;
  565. }
  566. //TwoWire Wire = TwoWire();
  567. TwoWire Wire;
  568. #endif // __MK20DX128__ || __MK20DX256__
  569. #if defined(__AVR__)
  570. extern "C" {
  571. #include <stdlib.h>
  572. #include <string.h>
  573. #include <inttypes.h>
  574. #include "twi.h"
  575. }
  576. // Initialize Class Variables //////////////////////////////////////////////////
  577. uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
  578. uint8_t TwoWire::rxBufferIndex = 0;
  579. uint8_t TwoWire::rxBufferLength = 0;
  580. uint8_t TwoWire::txAddress = 0;
  581. uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
  582. uint8_t TwoWire::txBufferIndex = 0;
  583. uint8_t TwoWire::txBufferLength = 0;
  584. uint8_t TwoWire::transmitting = 0;
  585. void (*TwoWire::user_onRequest)(void);
  586. void (*TwoWire::user_onReceive)(int);
  587. // Constructors ////////////////////////////////////////////////////////////////
  588. TwoWire::TwoWire()
  589. {
  590. }
  591. // Public Methods //////////////////////////////////////////////////////////////
  592. void TwoWire::begin(void)
  593. {
  594. rxBufferIndex = 0;
  595. rxBufferLength = 0;
  596. txBufferIndex = 0;
  597. txBufferLength = 0;
  598. twi_init();
  599. }
  600. void TwoWire::begin(uint8_t address)
  601. {
  602. twi_setAddress(address);
  603. twi_attachSlaveTxEvent(onRequestService);
  604. twi_attachSlaveRxEvent(onReceiveService);
  605. begin();
  606. }
  607. void TwoWire::begin(int address)
  608. {
  609. begin((uint8_t)address);
  610. }
  611. void TwoWire::end()
  612. {
  613. TWCR &= ~(_BV(TWEN) | _BV(TWIE) | _BV(TWEA));
  614. digitalWrite(SDA, 0);
  615. digitalWrite(SCL, 0);
  616. }
  617. void TwoWire::setClock(uint32_t frequency)
  618. {
  619. TWBR = ((F_CPU / frequency) - 16) / 2;
  620. }
  621. void TwoWire::setSDA(uint8_t pin)
  622. {
  623. }
  624. void TwoWire::setSCL(uint8_t pin)
  625. {
  626. }
  627. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
  628. {
  629. // clamp to buffer length
  630. if(quantity > BUFFER_LENGTH){
  631. quantity = BUFFER_LENGTH;
  632. }
  633. // perform blocking read into buffer
  634. uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
  635. // set rx buffer iterator vars
  636. rxBufferIndex = 0;
  637. rxBufferLength = read;
  638. return read;
  639. }
  640. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
  641. {
  642. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  643. }
  644. uint8_t TwoWire::requestFrom(int address, int quantity)
  645. {
  646. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  647. }
  648. uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
  649. {
  650. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
  651. }
  652. void TwoWire::beginTransmission(uint8_t address)
  653. {
  654. // indicate that we are transmitting
  655. transmitting = 1;
  656. // set address of targeted slave
  657. txAddress = address;
  658. // reset tx buffer iterator vars
  659. txBufferIndex = 0;
  660. txBufferLength = 0;
  661. }
  662. void TwoWire::beginTransmission(int address)
  663. {
  664. beginTransmission((uint8_t)address);
  665. }
  666. //
  667. // Originally, 'endTransmission' was an f(void) function.
  668. // It has been modified to take one parameter indicating
  669. // whether or not a STOP should be performed on the bus.
  670. // Calling endTransmission(false) allows a sketch to
  671. // perform a repeated start.
  672. //
  673. // WARNING: Nothing in the library keeps track of whether
  674. // the bus tenure has been properly ended with a STOP. It
  675. // is very possible to leave the bus in a hung state if
  676. // no call to endTransmission(true) is made. Some I2C
  677. // devices will behave oddly if they do not see a STOP.
  678. //
  679. uint8_t TwoWire::endTransmission(uint8_t sendStop)
  680. {
  681. // transmit buffer (blocking)
  682. int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
  683. // reset tx buffer iterator vars
  684. txBufferIndex = 0;
  685. txBufferLength = 0;
  686. // indicate that we are done transmitting
  687. transmitting = 0;
  688. return ret;
  689. }
  690. // This provides backwards compatibility with the original
  691. // definition, and expected behaviour, of endTransmission
  692. //
  693. uint8_t TwoWire::endTransmission(void)
  694. {
  695. return endTransmission(true);
  696. }
  697. // must be called in:
  698. // slave tx event callback
  699. // or after beginTransmission(address)
  700. size_t TwoWire::write(uint8_t data)
  701. {
  702. if(transmitting){
  703. // in master transmitter mode
  704. // don't bother if buffer is full
  705. if(txBufferLength >= BUFFER_LENGTH){
  706. setWriteError();
  707. return 0;
  708. }
  709. // put byte in tx buffer
  710. txBuffer[txBufferIndex] = data;
  711. ++txBufferIndex;
  712. // update amount in buffer
  713. txBufferLength = txBufferIndex;
  714. }else{
  715. // in slave send mode
  716. // reply to master
  717. twi_transmit(&data, 1);
  718. }
  719. return 1;
  720. }
  721. // must be called in:
  722. // slave tx event callback
  723. // or after beginTransmission(address)
  724. size_t TwoWire::write(const uint8_t *data, size_t quantity)
  725. {
  726. if(transmitting){
  727. // in master transmitter mode
  728. for(size_t i = 0; i < quantity; ++i){
  729. write(data[i]);
  730. }
  731. }else{
  732. // in slave send mode
  733. // reply to master
  734. twi_transmit(data, quantity);
  735. }
  736. return quantity;
  737. }
  738. // must be called in:
  739. // slave rx event callback
  740. // or after requestFrom(address, numBytes)
  741. int TwoWire::available(void)
  742. {
  743. return rxBufferLength - rxBufferIndex;
  744. }
  745. // must be called in:
  746. // slave rx event callback
  747. // or after requestFrom(address, numBytes)
  748. int TwoWire::read(void)
  749. {
  750. int value = -1;
  751. // get each successive byte on each call
  752. if(rxBufferIndex < rxBufferLength){
  753. value = rxBuffer[rxBufferIndex];
  754. ++rxBufferIndex;
  755. }
  756. return value;
  757. }
  758. // must be called in:
  759. // slave rx event callback
  760. // or after requestFrom(address, numBytes)
  761. int TwoWire::peek(void)
  762. {
  763. int value = -1;
  764. if(rxBufferIndex < rxBufferLength){
  765. value = rxBuffer[rxBufferIndex];
  766. }
  767. return value;
  768. }
  769. void TwoWire::flush(void)
  770. {
  771. // XXX: to be implemented.
  772. }
  773. // behind the scenes function that is called when data is received
  774. void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
  775. {
  776. // don't bother if user hasn't registered a callback
  777. if(!user_onReceive){
  778. return;
  779. }
  780. // don't bother if rx buffer is in use by a master requestFrom() op
  781. // i know this drops data, but it allows for slight stupidity
  782. // meaning, they may not have read all the master requestFrom() data yet
  783. if(rxBufferIndex < rxBufferLength){
  784. return;
  785. }
  786. // copy twi rx buffer into local read buffer
  787. // this enables new reads to happen in parallel
  788. for(uint8_t i = 0; i < numBytes; ++i){
  789. rxBuffer[i] = inBytes[i];
  790. }
  791. // set rx iterator vars
  792. rxBufferIndex = 0;
  793. rxBufferLength = numBytes;
  794. // alert user program
  795. user_onReceive(numBytes);
  796. }
  797. // behind the scenes function that is called when data is requested
  798. void TwoWire::onRequestService(void)
  799. {
  800. // don't bother if user hasn't registered a callback
  801. if(!user_onRequest){
  802. return;
  803. }
  804. // reset tx buffer iterator vars
  805. // !!! this will kill any pending pre-master sendTo() activity
  806. txBufferIndex = 0;
  807. txBufferLength = 0;
  808. // alert user program
  809. user_onRequest();
  810. }
  811. // sets function called on slave write
  812. void TwoWire::onReceive( void (*function)(int) )
  813. {
  814. user_onReceive = function;
  815. }
  816. // sets function called on slave read
  817. void TwoWire::onRequest( void (*function)(void) )
  818. {
  819. user_onRequest = function;
  820. }
  821. // Preinstantiate Objects //////////////////////////////////////////////////////
  822. TwoWire Wire = TwoWire();
  823. #endif // __AVR__