|
-
-
- #include "Wire.h"
-
- #if defined(__arm__) && defined(CORE_TEENSY)
-
- #include "kinetis.h"
- #include <string.h> // for memcpy
- #include "core_pins.h"
-
- #include "Wire.h"
-
- uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
- uint8_t TwoWire::rxBufferIndex = 0;
- uint8_t TwoWire::rxBufferLength = 0;
- uint8_t TwoWire::txBuffer[BUFFER_LENGTH+1];
- uint8_t TwoWire::txBufferIndex = 0;
- uint8_t TwoWire::txBufferLength = 0;
- uint8_t TwoWire::transmitting = 0;
- uint8_t TwoWire::sda_pin_num = 18;
- uint8_t TwoWire::scl_pin_num = 19;
- void (*TwoWire::user_onRequest)(void) = NULL;
- void (*TwoWire::user_onReceive)(int) = NULL;
-
-
- TwoWire::TwoWire()
- {
- }
-
- static uint8_t slave_mode = 0;
- static uint8_t irqcount=0;
-
-
- void TwoWire::begin(void)
- {
-
-
-
- slave_mode = 0;
- SIM_SCGC4 |= SIM_SCGC4_I2C0;
- I2C0_C1 = 0;
-
-
-
-
-
-
-
- if (sda_pin_num == 18) {
- CORE_PIN18_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (sda_pin_num == 17) {
- CORE_PIN17_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
- } else if (sda_pin_num == 34) {
- CORE_PIN34_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (sda_pin_num == 8) {
- CORE_PIN8_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (sda_pin_num == 48) {
- CORE_PIN48_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- #endif
- }
- if (scl_pin_num == 19) {
- CORE_PIN19_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (scl_pin_num == 16) {
- CORE_PIN16_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
- } else if (scl_pin_num == 33) {
- CORE_PIN33_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (scl_pin_num == 7) {
- CORE_PIN7_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (scl_pin_num == 47) {
- CORE_PIN47_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- #endif
- }
- setClock(100000);
- I2C0_C2 = I2C_C2_HDRS;
- I2C0_C1 = I2C_C1_IICEN;
-
-
- }
-
- void TwoWire::setClock(uint32_t frequency)
- {
- if (!(SIM_SCGC4 & SIM_SCGC4_I2C0)) return;
-
- #if F_BUS == 120000000
- if (frequency < 400000) {
- I2C0_F = I2C_F_DIV1152;
- } else if (frequency < 1000000) {
- I2C0_F = I2C_F_DIV288;
- } else {
- I2C0_F = I2C_F_DIV128;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 108000000
- if (frequency < 400000) {
- I2C0_F = I2C_F_DIV1024;
- } else if (frequency < 1000000) {
- I2C0_F = I2C_F_DIV256;
- } else {
- I2C0_F = I2C_F_DIV112;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 96000000
- if (frequency < 400000) {
- I2C0_F = I2C_F_DIV960;
- } else if (frequency < 1000000) {
- I2C0_F = I2C_F_DIV240;
- } else {
- I2C0_F = I2C_F_DIV96;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 90000000
- if (frequency < 400000) {
- I2C0_F = I2C_F_DIV896;
- } else if (frequency < 1000000) {
- I2C0_F = I2C_F_DIV224;
- } else {
- I2C0_F = I2C_F_DIV88;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 80000000
- if (frequency < 400000) {
- I2C0_F = I2C_F_DIV768;
- } else if (frequency < 1000000) {
- I2C0_F = I2C_F_DIV192;
- } else {
- I2C0_F = I2C_F_DIV80;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 72000000
- if (frequency < 400000) {
- I2C0_F = I2C_F_DIV640;
- } else if (frequency < 1000000) {
- I2C0_F = I2C_F_DIV192;
- } else {
- I2C0_F = I2C_F_DIV72;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 64000000
- if (frequency < 400000) {
- I2C0_F = I2C_F_DIV640;
- } else if (frequency < 1000000) {
- I2C0_F = I2C_F_DIV160;
- } else {
- I2C0_F = I2C_F_DIV64;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 60000000
- if (frequency < 400000) {
- I2C0_F = 0x2C;
- } else if (frequency < 1000000) {
- I2C0_F = 0x1C;
- } else {
- I2C0_F = 0x12;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 56000000
- if (frequency < 400000) {
- I2C0_F = 0x2B;
- } else if (frequency < 1000000) {
- I2C0_F = 0x1C;
- } else {
- I2C0_F = 0x0E;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 54000000
- if (frequency < 400000) {
- I2C0_F = I2C_F_DIV512;
- } else if (frequency < 1000000) {
- I2C0_F = I2C_F_DIV128;
- } else {
- I2C0_F = I2C_F_DIV56;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 48000000
- if (frequency < 400000) {
- I2C0_F = 0x27;
- } else if (frequency < 1000000) {
- I2C0_F = 0x1A;
- } else {
- I2C0_F = 0x0D;
- }
- I2C0_FLT = 4;
- #elif F_BUS == 40000000
- if (frequency < 400000) {
- I2C0_F = 0x29;
- } else if (frequency < 1000000) {
- I2C0_F = 0x19;
- } else {
- I2C0_F = 0x0B;
- }
- I2C0_FLT = 3;
- #elif F_BUS == 36000000
- if (frequency < 400000) {
- I2C0_F = 0x28;
- } else if (frequency < 1000000) {
- I2C0_F = 0x19;
- } else {
- I2C0_F = 0x0A;
- }
- I2C0_FLT = 3;
- #elif F_BUS == 24000000
- if (frequency < 400000) {
- I2C0_F = 0x1F;
- } else if (frequency < 1000000) {
- I2C0_F = 0x12;
- } else {
- I2C0_F = 0x02;
- }
- I2C0_FLT = 2;
- #elif F_BUS == 16000000
- if (frequency < 400000) {
- I2C0_F = 0x20;
- } else if (frequency < 1000000) {
- I2C0_F = 0x07;
- } else {
- I2C0_F = 0x00;
- }
- I2C0_FLT = 1;
- #elif F_BUS == 8000000
- if (frequency < 400000) {
- I2C0_F = 0x14;
- } else {
- I2C0_F = 0x00;
- }
- I2C0_FLT = 1;
- #elif F_BUS == 4000000
- if (frequency < 400000) {
- I2C0_F = 0x07;
- } else {
- I2C0_F = 0x00;
- }
- I2C0_FLT = 1;
- #elif F_BUS == 2000000
- I2C0_F = 0x00;
- I2C0_FLT = 1;
- #else
- #error "F_BUS must be 120, 108, 96, 9, 80, 72, 64, 60, 56, 54, 48, 40, 36, 24, 16, 8, 4 or 2 MHz"
- #endif
- }
-
- void TwoWire::setSDA(uint8_t pin)
- {
- if (pin == sda_pin_num) return;
- if ((SIM_SCGC4 & SIM_SCGC4_I2C0)) {
- if (sda_pin_num == 18) {
- CORE_PIN18_CONFIG = 0;
- } else if (sda_pin_num == 17) {
- CORE_PIN17_CONFIG = 0;
- #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
- } else if (sda_pin_num == 34) {
- CORE_PIN34_CONFIG = 0;
- } else if (sda_pin_num == 8) {
- CORE_PIN8_CONFIG = 0;
- } else if (sda_pin_num == 48) {
- CORE_PIN48_CONFIG = 0;
- #endif
- }
-
- if (pin == 18) {
- CORE_PIN18_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (pin == 17) {
- CORE_PIN17_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
- } else if (pin == 34) {
- CORE_PIN34_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (pin == 8) {
- CORE_PIN8_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (pin == 48) {
- CORE_PIN48_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- #endif
- }
- }
- sda_pin_num = pin;
- }
-
- void TwoWire::setSCL(uint8_t pin)
- {
- if (pin == scl_pin_num) return;
- if ((SIM_SCGC4 & SIM_SCGC4_I2C0)) {
- if (scl_pin_num == 19) {
- CORE_PIN19_CONFIG = 0;
- } else if (scl_pin_num == 16) {
- CORE_PIN16_CONFIG = 0;
- #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
- } else if (scl_pin_num == 33) {
- CORE_PIN33_CONFIG = 0;
- } else if (scl_pin_num == 7) {
- CORE_PIN7_CONFIG = 0;
- } else if (scl_pin_num == 47) {
- CORE_PIN47_CONFIG = 0;
- #endif
- }
-
- if (pin == 19) {
- CORE_PIN19_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (pin == 16) {
- CORE_PIN16_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
- } else if (pin == 33) {
- CORE_PIN33_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (pin == 7) {
- CORE_PIN7_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- } else if (pin == 47) {
- CORE_PIN47_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- #endif
- }
- }
- scl_pin_num = pin;
- }
-
- void TwoWire::begin(uint8_t address)
- {
- begin();
- I2C0_A1 = address << 1;
- slave_mode = 1;
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE;
- NVIC_ENABLE_IRQ(IRQ_I2C0);
- }
-
- void TwoWire::end()
- {
- if (!(SIM_SCGC4 & SIM_SCGC4_I2C0)) return;
- NVIC_DISABLE_IRQ(IRQ_I2C0);
- I2C0_C1 = 0;
- if (sda_pin_num == 18) {
- CORE_PIN18_CONFIG = 0;
- } else if (sda_pin_num == 17) {
- CORE_PIN17_CONFIG = 0;
- #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
- } else if (sda_pin_num == 34) {
- CORE_PIN34_CONFIG = 0;
- } else if (sda_pin_num == 8) {
- CORE_PIN8_CONFIG = 0;
- } else if (sda_pin_num == 48) {
- CORE_PIN48_CONFIG = 0;
- #endif
- }
- if (scl_pin_num == 19) {
- CORE_PIN19_CONFIG = 0;
- } else if (scl_pin_num == 16) {
- CORE_PIN16_CONFIG = 0;
- #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
- } else if (scl_pin_num == 33) {
- CORE_PIN33_CONFIG = 0;
- } else if (scl_pin_num == 7) {
- CORE_PIN7_CONFIG = 0;
- } else if (scl_pin_num == 47) {
- CORE_PIN47_CONFIG = 0;
- #endif
- }
- SIM_SCGC4 &= ~SIM_SCGC4_I2C0;
- }
-
- void i2c0_isr(void)
- {
- uint8_t status, c1, data;
- static uint8_t receiving=0;
-
- status = I2C0_S;
-
- if (status & I2C_S_ARBL) {
-
- I2C0_S = I2C_S_ARBL;
-
- if (receiving && TwoWire::rxBufferLength > 0) {
-
-
-
- }
- if (!(status & I2C_S_IAAS)) return;
- }
- if (status & I2C_S_IAAS) {
-
-
- if (status & I2C_S_SRW) {
-
-
- receiving = 0;
- TwoWire::txBufferLength = 0;
- if (TwoWire::user_onRequest != NULL) {
- TwoWire::user_onRequest();
- }
- if (TwoWire::txBufferLength == 0) {
-
-
-
- TwoWire::txBufferLength = 1;
- TwoWire::txBuffer[0] = 0;
- }
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
- I2C0_D = TwoWire::txBuffer[0];
- TwoWire::txBufferIndex = 1;
- } else {
-
-
- receiving = 1;
- TwoWire::rxBufferLength = 0;
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE;
- data = I2C0_D;
- }
- I2C0_S = I2C_S_IICIF;
- return;
- }
- #if defined(KINETISL)
- c1 = I2C0_FLT;
- if ((c1 & I2C_FLT_STOPF) && (c1 & I2C_FLT_STOPIE)) {
- I2C0_FLT = c1 & ~I2C_FLT_STOPIE;
- if (TwoWire::user_onReceive != NULL) {
- TwoWire::rxBufferIndex = 0;
- TwoWire::user_onReceive(TwoWire::rxBufferLength);
- }
- }
- #endif
- c1 = I2C0_C1;
- if (c1 & I2C_C1_TX) {
-
-
- if ((status & I2C_S_RXAK) == 0) {
-
-
- if (TwoWire::txBufferIndex < TwoWire::txBufferLength) {
- I2C0_D = TwoWire::txBuffer[TwoWire::txBufferIndex++];
- } else {
- I2C0_D = 0;
- }
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
- } else {
-
-
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE;
- data = I2C0_D;
- }
- } else {
-
- irqcount = 0;
- #if defined(KINETISK)
- attachInterrupt(18, TwoWire::sda_rising_isr, RISING);
- #elif defined(KINETISL)
- I2C0_FLT |= I2C_FLT_STOPIE;
- #endif
-
- data = I2C0_D;
-
- if (TwoWire::rxBufferLength < BUFFER_LENGTH && receiving) {
- TwoWire::rxBuffer[TwoWire::rxBufferLength++] = data;
- }
-
- }
- I2C0_S = I2C_S_IICIF;
- }
-
-
-
-
- void TwoWire::sda_rising_isr(void)
- {
-
- if (!(I2C0_S & I2C_S_BUSY)) {
- detachInterrupt(18);
- if (user_onReceive != NULL) {
- rxBufferIndex = 0;
- user_onReceive(rxBufferLength);
- }
-
- } else {
- if (++irqcount >= 2 || !slave_mode) {
- detachInterrupt(18);
- }
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
- static uint8_t i2c_status(void)
- {
- static uint32_t p=0xFFFF;
- uint32_t s = I2C0_S;
- if (s != p) {
-
- p = s;
- }
- return s;
- }
-
- static void i2c_wait(void)
- {
- #if 0
- while (!(I2C0_S & I2C_S_IICIF)) ;
- I2C0_S = I2C_S_IICIF;
- #endif
-
- while (1) {
- if ((i2c_status() & I2C_S_IICIF)) break;
- }
- I2C0_S = I2C_S_IICIF;
- }
-
- void TwoWire::beginTransmission(uint8_t address)
- {
- txBuffer[0] = (address << 1);
- transmitting = 1;
- txBufferLength = 1;
- }
-
- size_t TwoWire::write(uint8_t data)
- {
- if (transmitting || slave_mode) {
- if (txBufferLength >= BUFFER_LENGTH+1) {
- setWriteError();
- return 0;
- }
- txBuffer[txBufferLength++] = data;
- return 1;
- }
- return 0;
- }
-
- size_t TwoWire::write(const uint8_t *data, size_t quantity)
- {
- if (transmitting || slave_mode) {
- size_t avail = BUFFER_LENGTH+1 - txBufferLength;
- if (quantity > avail) {
- quantity = avail;
- setWriteError();
- }
- memcpy(txBuffer + txBufferLength, data, quantity);
- txBufferLength += quantity;
- return quantity;
- }
- return 0;
- }
-
- void TwoWire::flush(void)
- {
- }
-
-
- uint8_t TwoWire::endTransmission(uint8_t sendStop)
- {
- uint8_t i, status, ret=0;
-
-
- I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
-
- if (I2C0_C1 & I2C_C1_MST) {
-
-
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
- } else {
-
-
- uint32_t wait_begin = millis();
- while (i2c_status() & I2C_S_BUSY) {
-
- if (millis() - wait_begin > 15) {
-
- I2C0_C1 = 0;
- I2C0_C1 = I2C_C1_IICEN;
-
- return 4;
- }
- }
-
- slave_mode = 0;
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
- }
-
- while (1) {
- status = i2c_status();
- if ((status & I2C_S_BUSY)) break;
- }
-
- for (i=0; i < txBufferLength; i++) {
- I2C0_D = txBuffer[i];
-
- while (1) {
- status = i2c_status();
- if ((status & I2C_S_IICIF)) break;
- if (!(status & I2C_S_BUSY)) break;
- }
- I2C0_S = I2C_S_IICIF;
-
- status = i2c_status();
- if ((status & I2C_S_ARBL)) {
-
-
-
- I2C0_C1 = I2C_C1_IICEN;
- ret = 4;
- break;
- }
- if (!(status & I2C_S_BUSY)) {
-
- I2C0_C1 = I2C_C1_IICEN;
- ret = 4;
- break;
- }
- if (status & I2C_S_RXAK) {
-
- if (i == 0) {
- ret = 2;
- } else {
- ret = 3;
- }
- sendStop = 1;
- break;
- }
- }
- if (sendStop) {
-
- I2C0_C1 = I2C_C1_IICEN;
-
- }
- transmitting = 0;
-
-
- return ret;
- }
-
-
- uint8_t TwoWire::requestFrom(uint8_t address, uint8_t length, uint8_t sendStop)
- {
- uint8_t tmp __attribute__((unused));
- uint8_t status, count=0;
-
- rxBufferIndex = 0;
- rxBufferLength = 0;
-
-
- I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
-
- if (I2C0_C1 & I2C_C1_MST) {
-
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
- } else {
-
- while (i2c_status() & I2C_S_BUSY) ;
-
- slave_mode = 0;
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
- }
-
- I2C0_D = (address << 1) | 1;
- i2c_wait();
- status = i2c_status();
- if ((status & I2C_S_RXAK) || (status & I2C_S_ARBL)) {
-
-
- I2C0_C1 = I2C_C1_IICEN;
- return 0;
- }
- if (length == 0) {
-
-
- I2C0_C1 = I2C_C1_IICEN | (sendStop ? 0 : I2C_C1_MST);
- return 0;
- } else if (length == 1) {
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
- } else {
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST;
- }
- tmp = I2C0_D;
- while (length > 1) {
- i2c_wait();
- length--;
- if (length == 1) I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
- if (count < BUFFER_LENGTH) {
- rxBuffer[count++] = I2C0_D;
- } else {
- tmp = I2C0_D;
- }
- }
- i2c_wait();
- I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
- if (count < BUFFER_LENGTH) {
- rxBuffer[count++] = I2C0_D;
- } else {
- tmp = I2C0_D;
- }
- if (sendStop) I2C0_C1 = I2C_C1_IICEN;
- rxBufferLength = count;
- return count;
- }
-
- int TwoWire::available(void)
- {
- return rxBufferLength - rxBufferIndex;
- }
-
- int TwoWire::read(void)
- {
- if (rxBufferIndex >= rxBufferLength) return -1;
- return rxBuffer[rxBufferIndex++];
- }
-
- int TwoWire::peek(void)
- {
- if (rxBufferIndex >= rxBufferLength) return -1;
- return rxBuffer[rxBufferIndex];
- }
-
-
-
-
-
-
-
- uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
- {
- return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
- }
-
- uint8_t TwoWire::requestFrom(int address, int quantity)
- {
- return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
- }
-
- uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
- {
- return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
- }
-
- void TwoWire::beginTransmission(int address)
- {
- beginTransmission((uint8_t)address);
- }
-
- uint8_t TwoWire::endTransmission(void)
- {
- return endTransmission(true);
- }
-
- void TwoWire::begin(int address)
- {
- begin((uint8_t)address);
- }
-
- void TwoWire::onReceive( void (*function)(int) )
- {
- user_onReceive = function;
- }
-
- void TwoWire::onRequest( void (*function)(void) )
- {
- user_onRequest = function;
- }
-
-
- TwoWire Wire;
-
-
- #endif
-
-
-
- #if defined(__AVR__)
-
- extern "C" {
- #include <stdlib.h>
- #include <string.h>
- #include <inttypes.h>
- #include "twi.h"
- }
-
-
-
-
- uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
- uint8_t TwoWire::rxBufferIndex = 0;
- uint8_t TwoWire::rxBufferLength = 0;
-
- uint8_t TwoWire::txAddress = 0;
- uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
- uint8_t TwoWire::txBufferIndex = 0;
- uint8_t TwoWire::txBufferLength = 0;
-
- uint8_t TwoWire::transmitting = 0;
- void (*TwoWire::user_onRequest)(void);
- void (*TwoWire::user_onReceive)(int);
-
-
-
- TwoWire::TwoWire()
- {
- }
-
-
-
- void TwoWire::begin(void)
- {
- rxBufferIndex = 0;
- rxBufferLength = 0;
-
- txBufferIndex = 0;
- txBufferLength = 0;
-
- twi_init();
- }
-
- void TwoWire::begin(uint8_t address)
- {
- twi_setAddress(address);
- twi_attachSlaveTxEvent(onRequestService);
- twi_attachSlaveRxEvent(onReceiveService);
- begin();
- }
-
- void TwoWire::begin(int address)
- {
- begin((uint8_t)address);
- }
-
- void TwoWire::end()
- {
- TWCR &= ~(_BV(TWEN) | _BV(TWIE) | _BV(TWEA));
- digitalWrite(SDA, 0);
- digitalWrite(SCL, 0);
- }
-
- void TwoWire::setClock(uint32_t frequency)
- {
- TWBR = ((F_CPU / frequency) - 16) / 2;
- }
-
- void TwoWire::setSDA(uint8_t pin)
- {
- }
-
- void TwoWire::setSCL(uint8_t pin)
- {
- }
-
- uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
- {
-
- if(quantity > BUFFER_LENGTH){
- quantity = BUFFER_LENGTH;
- }
-
- uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
-
- rxBufferIndex = 0;
- rxBufferLength = read;
-
- return read;
- }
-
- uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
- {
- return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
- }
-
- uint8_t TwoWire::requestFrom(int address, int quantity)
- {
- return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
- }
-
- uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
- {
- return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
- }
-
- void TwoWire::beginTransmission(uint8_t address)
- {
-
- transmitting = 1;
-
- txAddress = address;
-
- txBufferIndex = 0;
- txBufferLength = 0;
- }
-
- void TwoWire::beginTransmission(int address)
- {
- beginTransmission((uint8_t)address);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- uint8_t TwoWire::endTransmission(uint8_t sendStop)
- {
-
- int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
-
- txBufferIndex = 0;
- txBufferLength = 0;
-
- transmitting = 0;
- return ret;
- }
-
-
-
-
- uint8_t TwoWire::endTransmission(void)
- {
- return endTransmission(true);
- }
-
-
-
-
- size_t TwoWire::write(uint8_t data)
- {
- if(transmitting){
-
-
- if(txBufferLength >= BUFFER_LENGTH){
- setWriteError();
- return 0;
- }
-
- txBuffer[txBufferIndex] = data;
- ++txBufferIndex;
-
- txBufferLength = txBufferIndex;
- }else{
-
-
- twi_transmit(&data, 1);
- }
- return 1;
- }
-
-
-
-
- size_t TwoWire::write(const uint8_t *data, size_t quantity)
- {
- if(transmitting){
-
- for(size_t i = 0; i < quantity; ++i){
- write(data[i]);
- }
- }else{
-
-
- twi_transmit(data, quantity);
- }
- return quantity;
- }
-
-
-
-
- int TwoWire::available(void)
- {
- return rxBufferLength - rxBufferIndex;
- }
-
-
-
-
- int TwoWire::read(void)
- {
- int value = -1;
-
-
- if(rxBufferIndex < rxBufferLength){
- value = rxBuffer[rxBufferIndex];
- ++rxBufferIndex;
- }
-
- return value;
- }
-
-
-
-
- int TwoWire::peek(void)
- {
- int value = -1;
-
- if(rxBufferIndex < rxBufferLength){
- value = rxBuffer[rxBufferIndex];
- }
-
- return value;
- }
-
- void TwoWire::flush(void)
- {
-
- }
-
-
- void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
- {
-
- if(!user_onReceive){
- return;
- }
-
-
-
- if(rxBufferIndex < rxBufferLength){
- return;
- }
-
-
- for(uint8_t i = 0; i < numBytes; ++i){
- rxBuffer[i] = inBytes[i];
- }
-
- rxBufferIndex = 0;
- rxBufferLength = numBytes;
-
- user_onReceive(numBytes);
- }
-
-
- void TwoWire::onRequestService(void)
- {
-
- if(!user_onRequest){
- return;
- }
-
-
- txBufferIndex = 0;
- txBufferLength = 0;
-
- user_onRequest();
- }
-
-
- void TwoWire::onReceive( void (*function)(int) )
- {
- user_onReceive = function;
- }
-
-
- void TwoWire::onRequest( void (*function)(void) )
- {
- user_onRequest = function;
- }
-
-
-
- TwoWire Wire = TwoWire();
-
- #endif
|