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.

874 lines
21KB

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