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.

858 line
20KB

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