|
-
-
- #include "Wire.h"
-
- #if defined(__arm__) && defined(TEENSYDUINO)
-
- #include "kinetis.h"
- #include <string.h> // for memcpy
- #include "core_pins.h"
- #include "Wire.h"
-
-
- #undef I2C0_A1
- #undef I2C0_F
- #undef I2C0_C1
- #undef I2C0_S
- #undef I2C0_D
- #undef I2C0_C2
- #undef I2C0_FLT
- #undef I2C0_RA
- #undef I2C0_SMB
- #undef I2C0_A2
- #undef I2C0_SLTH
- #undef I2C0_SLTL
-
- void sda_rising_isr0(void);
- void sda_rising_isr1(void);
-
- void TwoWire::begin(void)
- {
-
-
-
- rxBufferIndex = 0;
- rxBufferLength = 0;
- txBufferIndex = 0;
- txBufferLength = 0;
- transmitting = 0;
- user_onRequest = NULL;
- user_onReceive = NULL;
- slave_mode = 0;
- hardware.clock_gate_register |= hardware.clock_gate_mask;
- port.C1 = 0;
-
-
-
-
-
-
-
- uint32_t mux;
- volatile uint32_t *reg;
- reg = portConfigRegister(hardware.sda_pin[sda_pin_index]);
- mux = PORT_PCR_MUX(hardware.sda_mux[sda_pin_index]);
- *reg = mux|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- reg = portConfigRegister(hardware.scl_pin[scl_pin_index]);
- mux = PORT_PCR_MUX(hardware.scl_mux[scl_pin_index]);
- *reg = mux|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- setClock(100000);
- port.C2 = I2C_C2_HDRS;
- port.C1 = I2C_C1_IICEN;
-
-
- }
-
- void TwoWire::setClock(uint32_t frequency)
- {
- if (!(hardware.clock_gate_register & hardware.clock_gate_mask)) return;
-
- #if F_BUS == 120000000
- if (frequency < 400000) {
- port.F = I2C_F_DIV1152;
- } else if (frequency < 1000000) {
- port.F = I2C_F_DIV288;
- } else {
- port.F = I2C_F_DIV128;
- }
- port.FLT = 4;
- #elif F_BUS == 108000000
- if (frequency < 400000) {
- port.F = I2C_F_DIV1024;
- } else if (frequency < 1000000) {
- port.F = I2C_F_DIV256;
- } else {
- port.F = I2C_F_DIV112;
- }
- port.FLT = 4;
- #elif F_BUS == 96000000
- if (frequency < 400000) {
- port.F = I2C_F_DIV960;
- } else if (frequency < 1000000) {
- port.F = I2C_F_DIV240;
- } else {
- port.F = I2C_F_DIV96;
- }
- port.FLT = 4;
- #elif F_BUS == 90000000
- if (frequency < 400000) {
- port.F = I2C_F_DIV896;
- } else if (frequency < 1000000) {
- port.F = I2C_F_DIV224;
- } else {
- port.F = I2C_F_DIV88;
- }
- port.FLT = 4;
- #elif F_BUS == 80000000
- if (frequency < 400000) {
- port.F = I2C_F_DIV768;
- } else if (frequency < 1000000) {
- port.F = I2C_F_DIV192;
- } else {
- port.F = I2C_F_DIV80;
- }
- port.FLT = 4;
- #elif F_BUS == 72000000
- if (frequency < 400000) {
- port.F = I2C_F_DIV640;
- } else if (frequency < 1000000) {
- port.F = I2C_F_DIV192;
- } else {
- port.F = I2C_F_DIV72;
- }
- port.FLT = 4;
- #elif F_BUS == 64000000
- if (frequency < 400000) {
- port.F = I2C_F_DIV640;
- } else if (frequency < 1000000) {
- port.F = I2C_F_DIV160;
- } else {
- port.F = I2C_F_DIV64;
- }
- port.FLT = 4;
- #elif F_BUS == 60000000
- if (frequency < 400000) {
- port.F = 0x2C;
- } else if (frequency < 1000000) {
- port.F = 0x1C;
- } else {
- port.F = 0x12;
- }
- port.FLT = 4;
- #elif F_BUS == 56000000
- if (frequency < 400000) {
- port.F = 0x2B;
- } else if (frequency < 1000000) {
- port.F = 0x1C;
- } else {
- port.F = 0x0E;
- }
- port.FLT = 4;
- #elif F_BUS == 54000000
- if (frequency < 400000) {
- port.F = I2C_F_DIV512;
- } else if (frequency < 1000000) {
- port.F = I2C_F_DIV128;
- } else {
- port.F = I2C_F_DIV56;
- }
- port.FLT = 4;
- #elif F_BUS == 48000000
- if (frequency < 400000) {
- port.F = 0x27;
- } else if (frequency < 1000000) {
- port.F = 0x1A;
- } else {
- port.F = 0x0D;
- }
- port.FLT = 4;
- #elif F_BUS == 40000000
- if (frequency < 400000) {
- port.F = 0x29;
- } else if (frequency < 1000000) {
- port.F = 0x19;
- } else {
- port.F = 0x0B;
- }
- port.FLT = 3;
- #elif F_BUS == 36000000
- if (frequency < 400000) {
- port.F = 0x28;
- } else if (frequency < 1000000) {
- port.F = 0x19;
- } else {
- port.F = 0x0A;
- }
- port.FLT = 3;
- #elif F_BUS == 24000000
- if (frequency < 400000) {
- port.F = 0x1F;
- } else if (frequency < 1000000) {
- port.F = 0x12;
- } else {
- port.F = 0x02;
- }
- port.FLT = 2;
- #elif F_BUS == 16000000
- if (frequency < 400000) {
- port.F = 0x20;
- } else if (frequency < 1000000) {
- port.F = 0x07;
- } else {
- port.F = 0x00;
- }
- port.FLT = 1;
- #elif F_BUS == 8000000
- if (frequency < 400000) {
- port.F = 0x14;
- } else {
- port.F = 0x00;
- }
- port.FLT = 1;
- #elif F_BUS == 4000000
- if (frequency < 400000) {
- port.F = 0x07;
- } else {
- port.F = 0x00;
- }
- port.FLT = 1;
- #elif F_BUS == 2000000
- port.F = 0x00;
- port.FLT = 1;
- #else
- #error "F_BUS must be 120, 108, 96, 90, 80, 72, 64, 60, 56, 54, 48, 40, 36, 24, 16, 8, 4 or 2 MHz"
- #endif
- }
-
- void TwoWire::setSDA(uint8_t pin)
- {
- if (pin == hardware.sda_pin[sda_pin_index]) return;
- uint32_t newindex=0;
- while (1) {
- uint32_t sda_pin = hardware.sda_pin[newindex];
- if (sda_pin == 255) return;
- if (sda_pin == pin) break;
- if (++newindex >= sizeof(hardware.sda_pin)) return;
- }
- if ((hardware.clock_gate_register & hardware.clock_gate_mask)) {
- volatile uint32_t *reg;
- reg = portConfigRegister(hardware.sda_pin[sda_pin_index]);
- *reg = 0;
- reg = portConfigRegister(hardware.sda_pin[newindex]);
- uint32_t mux = PORT_PCR_MUX(hardware.sda_mux[newindex]);
- *reg = mux|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- }
- sda_pin_index = newindex;
- }
-
- void TwoWire::setSCL(uint8_t pin)
- {
- if (pin == hardware.scl_pin[scl_pin_index]) return;
- uint32_t newindex=0;
- while (1) {
- uint32_t scl_pin = hardware.scl_pin[newindex];
- if (scl_pin == 255) return;
- if (scl_pin == pin) break;
- if (++newindex >= sizeof(hardware.scl_pin)) return;
- }
- if ((hardware.clock_gate_register & hardware.clock_gate_mask)) {
- volatile uint32_t *reg;
- reg = portConfigRegister(hardware.scl_pin[scl_pin_index]);
- *reg = 0;
- reg = portConfigRegister(hardware.scl_pin[newindex]);
- uint32_t mux = PORT_PCR_MUX(hardware.scl_mux[newindex]);
- *reg = mux|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
- }
- scl_pin_index = newindex;
- }
-
- void TwoWire::begin(uint8_t address)
- {
- begin();
- port.A1 = address << 1;
- slave_mode = 1;
- port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
- NVIC_ENABLE_IRQ(hardware.irq);
- }
-
- void TwoWire::end()
- {
- if (!(hardware.clock_gate_register & hardware.clock_gate_mask)) return;
- NVIC_DISABLE_IRQ(hardware.irq);
-
- port.C1 = 0;
- volatile uint32_t *reg;
- reg = portConfigRegister(hardware.scl_pin[scl_pin_index]);
- *reg = 0;
- reg = portConfigRegister(hardware.sda_pin[sda_pin_index]);
- *reg = 0;
- hardware.clock_gate_register &= ~hardware.clock_gate_mask;
- }
-
- #ifdef WIRE_IMPLEMENT_WIRE
- void i2c0_isr(void)
- {
- Wire.isr();
- }
- #endif
-
- #ifdef WIRE_IMPLEMENT_WIRE1
- void i2c1_isr(void)
- {
- Wire1.isr();
- }
- #endif
-
- #ifdef WIRE_IMPLEMENT_WIRE2
- void i2c2_isr(void)
- {
- Wire2.isr();
- }
- #endif
-
- #ifdef WIRE_IMPLEMENT_WIRE3
- void i2c3_isr(void)
- {
- Wire3.isr();
- }
- #endif
-
-
- void TwoWire::isr(void)
- {
- uint8_t status, c1, data;
- static uint8_t receiving=0;
-
- status = port.S;
-
- if (status & I2C_S_ARBL) {
-
- port.S = I2C_S_ARBL;
-
- if (receiving && rxBufferLength > 0) {
-
-
-
- }
- if (!(status & I2C_S_IAAS)) return;
- }
- if (status & I2C_S_IAAS) {
-
-
- if (status & I2C_S_SRW) {
-
-
- receiving = 0;
- txBufferLength = 0;
- if (user_onRequest != NULL) {
- user_onRequest();
- }
- if (txBufferLength == 0) {
-
-
-
- txBufferLength = 1;
- txBuffer[0] = 0;
- }
- port.C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
- port.D = txBuffer[0];
- txBufferIndex = 1;
- } else {
-
-
- receiving = 1;
- rxBufferLength = 0;
- port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
- data = port.D;
- }
- port.S = I2C_S_IICIF;
- return;
- }
- #if defined(WIRE_HAS_STOP_INTERRUPT)
- c1 = port.FLT;
- if ((c1 & I2C_FLT_STOPF) && (c1 & I2C_FLT_STOPIE)) {
- port.FLT = c1 & ~I2C_FLT_STOPIE;
- if (user_onReceive != NULL) {
- rxBufferIndex = 0;
- user_onReceive(rxBufferLength);
- }
- }
- #endif
- c1 = port.C1;
- if (c1 & I2C_C1_TX) {
-
-
- if ((status & I2C_S_RXAK) == 0) {
-
-
- if (txBufferIndex < txBufferLength) {
- port.D = txBuffer[txBufferIndex++];
- } else {
- port.D = 0;
- }
- port.C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
- } else {
-
-
- port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
- data = port.D;
- }
- } else {
-
- irqcount = 0;
- #ifdef WIRE_HAS_STOP_INTERRUPT
- port.FLT |= I2C_FLT_STOPIE;
- #else
- #if defined(WIRE_IMPLEMENT_WIRE) && !defined(WIRE_IMPLEMENT_WIRE1)
- attachInterrupt(hardware.sda_pin[sda_pin_index], sda_rising_isr0, RISING);
- #elif !defined(WIRE_IMPLEMENT_WIRE) && defined(WIRE_IMPLEMENT_WIRE1)
- attachInterrupt(hardware.sda_pin[sda_pin_index], sda_rising_isr1, RISING);
- #elif defined(WIRE_IMPLEMENT_WIRE) && defined(WIRE_IMPLEMENT_WIRE1)
- if (this == &Wire) {
- attachInterrupt(hardware.sda_pin[sda_pin_index], sda_rising_isr0, RISING);
- } else if (this == &Wire1) {
- attachInterrupt(hardware.sda_pin[sda_pin_index], sda_rising_isr1, RISING);
- }
- #endif
- #endif
-
- data = port.D;
-
- if (rxBufferLength < BUFFER_LENGTH && receiving) {
- rxBuffer[rxBufferLength++] = data;
- }
-
- }
- port.S = I2C_S_IICIF;
- }
-
-
-
-
-
- #if !defined(WIRE_HAS_STOP_INTERRUPT)
-
- #if defined(WIRE_IMPLEMENT_WIRE)
- void sda_rising_isr0(void)
- {
- Wire.sda_rising_isr();
- }
- #endif
- #if defined(WIRE_IMPLEMENT_WIRE1)
- void sda_rising_isr1(void)
- {
- Wire1.sda_rising_isr();
- }
- #endif
-
- void TwoWire::sda_rising_isr(void)
- {
-
- if (!(port.S & I2C_S_BUSY)) {
- detachInterrupt(hardware.sda_pin[sda_pin_index]);
- if (user_onReceive != NULL) {
- rxBufferIndex = 0;
- user_onReceive(rxBufferLength);
- }
-
- } else {
- if (++irqcount >= 2 || !slave_mode) {
- detachInterrupt(hardware.sda_pin[sda_pin_index]);
- }
- }
-
- }
- #endif
-
-
-
-
-
-
-
-
-
-
-
- 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;
- }
-
-
- uint8_t TwoWire::endTransmission(uint8_t sendStop)
- {
- uint8_t i, status, ret=0;
- uint32_t wait_begin;
-
-
- port.S = I2C_S_IICIF | I2C_S_ARBL;
-
- if (port.C1 & I2C_C1_MST) {
-
-
- port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
- } else {
-
-
- wait_begin = millis();
- while (i2c_status() & I2C_S_BUSY) {
-
- if (millis() - wait_begin > 15) {
-
- port.C1 = 0;
- port.C1 = I2C_C1_IICEN;
-
- return 4;
- }
- }
-
- slave_mode = 0;
- port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
- }
-
- wait_begin = millis();
- while (1) {
- status = i2c_status();
- if ((status & I2C_S_BUSY)) break;
-
- if (millis() - wait_begin > 4) {
- port.C1 = 0;
- port.C1 = I2C_C1_IICEN;
-
- return 4;
- }
- }
-
- for (i=0; i < txBufferLength; i++) {
- port.D = txBuffer[i];
-
- wait_begin = millis();
- while (1) {
- status = i2c_status();
- if ((status & I2C_S_IICIF)) break;
- if (!(status & I2C_S_BUSY)) break;
- if (millis() - wait_begin > 5) {
- port.C1 = 0;
- port.C1 = I2C_C1_IICEN;
-
- return 4;
- }
- }
- port.S = I2C_S_IICIF;
-
- status = i2c_status();
- if ((status & I2C_S_ARBL)) {
-
-
-
- port.C1 = I2C_C1_IICEN;
- ret = 4;
- break;
- }
- if (!(status & I2C_S_BUSY)) {
-
- port.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) {
-
- port.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;
-
-
- port.S = I2C_S_IICIF | I2C_S_ARBL;
-
- if (port.C1 & I2C_C1_MST) {
-
- port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
- } else {
-
- while (i2c_status() & I2C_S_BUSY) ;
-
- slave_mode = 0;
- port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
- }
-
- port.D = (address << 1) | 1;
- i2c_wait();
- status = i2c_status();
- if ((status & I2C_S_RXAK) || (status & I2C_S_ARBL)) {
-
-
- port.C1 = I2C_C1_IICEN;
- return 0;
- }
- if (length == 0) {
-
-
- port.C1 = I2C_C1_IICEN | (sendStop ? 0 : I2C_C1_MST);
- return 0;
- } else if (length == 1) {
- port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
- } else {
- port.C1 = I2C_C1_IICEN | I2C_C1_MST;
- }
- tmp = port.D;
- while (length > 1) {
- i2c_wait();
- length--;
- if (length == 1) port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
- if (count < BUFFER_LENGTH) {
- rxBuffer[count++] = port.D;
- } else {
- tmp = port.D;
- }
- }
- i2c_wait();
- port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
- if (count < BUFFER_LENGTH) {
- rxBuffer[count++] = port.D;
- } else {
- tmp = port.D;
- }
- if (sendStop) port.C1 = I2C_C1_IICEN;
- rxBufferLength = count;
- return count;
- }
-
- #ifdef WIRE_IMPLEMENT_WIRE
-
- const TwoWire::I2C_Hardware_t TwoWire::i2c0_hardware = {
- SIM_SCGC4, SIM_SCGC4_I2C0,
- #if defined(__MKL26Z64__) || defined(__MK20DX128__) || defined(__MK20DX256__)
- 18, 17, 255, 255, 255,
- 2, 2, 0, 0, 0,
- 19, 16, 255, 255, 255,
- 2, 2, 0, 0, 0,
- #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
- 18, 17, 34, 8, 48,
- 2, 2, 5, 7, 2,
- 19, 16, 33, 7, 47,
- 2, 2, 5, 7, 2,
- #endif
- IRQ_I2C0
- };
-
- TwoWire Wire(KINETIS_I2C0, TwoWire::i2c0_hardware);
-
- #endif
-
-
- #ifdef WIRE_IMPLEMENT_WIRE1
-
- const TwoWire::I2C_Hardware_t TwoWire::i2c1_hardware = {
- SIM_SCGC4, SIM_SCGC4_I2C1,
- #if defined(__MKL26Z64__)
- 23, 255, 255, 255, 255,
- 2, 0, 0, 0, 0,
- 22, 255, 255, 255, 255,
- 2, 0, 0, 0, 0,
- #elif defined(__MK20DX256__)
- 30, 255, 255, 255, 255,
- 2, 0, 0, 0, 0,
- 29, 255, 255, 255, 255,
- 2, 0, 0, 0, 0,
- #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
- 38, 255, 255, 255, 255,
- 2, 0, 0, 0, 0,
- 37, 255, 255, 255, 255,
- 2, 0, 0, 0, 0,
- #endif
- IRQ_I2C1
- };
-
- TwoWire Wire1(KINETIS_I2C1, TwoWire::i2c1_hardware);
-
- #endif
-
-
- #ifdef WIRE_IMPLEMENT_WIRE2
-
- const TwoWire::I2C_Hardware_t TwoWire::i2c2_hardware = {
- SIM_SCGC1, SIM_SCGC1_I2C2,
- #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
- 4, 255, 255, 255, 255,
- 5, 0, 0, 0, 0,
- 3, 26, 255, 255, 255,
- 5, 5, 0, 0, 0,
- #endif
- IRQ_I2C2
- };
-
- TwoWire Wire2(KINETIS_I2C2, TwoWire::i2c2_hardware);
-
- #endif
-
-
- #ifdef WIRE_IMPLEMENT_WIRE3
-
- const TwoWire::I2C_Hardware_t TwoWire::i2c3_hardware = {
- SIM_SCGC1, SIM_SCGC1_I2C3,
- #if defined(__MK66FX1M0__)
- 56, 255, 255, 255, 255,
- 2, 0, 0, 0, 0,
- 57, 255, 255, 255, 255,
- 2, 0, 0, 0, 0,
- #endif
- IRQ_I2C3
- };
-
- TwoWire Wire3(KINETIS_I2C3, TwoWire::i2c3_hardware);
-
- #endif
-
-
- #endif
|