* THE SOFTWARE. | * THE SOFTWARE. | ||||
*/ | */ | ||||
#include <Arduino.h> | |||||
#include "Wire.h" | #include "Wire.h" | ||||
#if defined(__arm__) && defined(TEENSYDUINO) | #if defined(__arm__) && defined(TEENSYDUINO) | ||||
{ | { | ||||
if (!(hardware.clock_gate_register & hardware.clock_gate_mask)) return; | if (!(hardware.clock_gate_register & hardware.clock_gate_mask)) return; | ||||
#if F_BUS == 120000000 | |||||
#if F_BUS == 128000000 | |||||
if (frequency < 400000) { | |||||
port().F = I2C_F_DIV1280; // 100 kHz | |||||
} else if (frequency < 1000000) { | |||||
port().F = I2C_F_DIV320; // 400 kHz | |||||
} else { | |||||
port().F = I2C_F_DIV128; // 1 MHz | |||||
} | |||||
port().FLT = 4; | |||||
#elif F_BUS == 120000000 | |||||
if (frequency < 400000) { | if (frequency < 400000) { | ||||
port().F = I2C_F_DIV1152; // 104 kHz | port().F = I2C_F_DIV1152; // 104 kHz | ||||
} else if (frequency < 1000000) { | } else if (frequency < 1000000) { | ||||
port().F = 0x00; // 100 kHz | port().F = 0x00; // 100 kHz | ||||
port().FLT = 1; | port().FLT = 1; | ||||
#else | #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" | |||||
#error "F_BUS must be 128, 120, 108, 96, 90, 80, 72, 64, 60, 56, 54, 48, 40, 36, 24, 16, 8, 4 or 2 MHz" | |||||
#endif | #endif | ||||
} | } | ||||
#if defined(__arm__) && defined(TEENSYDUINO) | #if defined(__arm__) && defined(TEENSYDUINO) | ||||
#include <inttypes.h> | |||||
#include "Arduino.h" | |||||
#include <Arduino.h> | |||||
#include <stdint.h> | |||||
#define BUFFER_LENGTH 32 | #define BUFFER_LENGTH 32 | ||||
#define WIRE_HAS_END 1 | #define WIRE_HAS_END 1 | ||||
inline TWBRemulation & operator = (int val) __attribute__((always_inline)) { | inline TWBRemulation & operator = (int val) __attribute__((always_inline)) { | ||||
if (val == 12 || val == ((F_CPU / 400000) - 16) / 2) { // 22, 52, 112 | if (val == 12 || val == ((F_CPU / 400000) - 16) / 2) { // 22, 52, 112 | ||||
I2C0_C1 = 0; | I2C0_C1 = 0; | ||||
#if F_BUS == 120000000 | |||||
#if F_BUS == 128000000 | |||||
I2C0_F = I2C_F_DIV320; // 400 kHz | |||||
#elif F_BUS == 120000000 | |||||
I2C0_F = I2C_F_DIV288; // 416 kHz | I2C0_F = I2C_F_DIV288; // 416 kHz | ||||
#elif F_BUS == 108000000 | #elif F_BUS == 108000000 | ||||
I2C0_F = I2C_F_DIV256; // 422 kHz | I2C0_F = I2C_F_DIV256; // 422 kHz | ||||
I2C0_C1 = I2C_C1_IICEN; | I2C0_C1 = I2C_C1_IICEN; | ||||
} else if (val == 72 || val == ((F_CPU / 100000) - 16) / 2) { // 112, 232, 472 | } else if (val == 72 || val == ((F_CPU / 100000) - 16) / 2) { // 112, 232, 472 | ||||
I2C0_C1 = 0; | I2C0_C1 = 0; | ||||
#if F_BUS == 120000000 | |||||
#if F_BUS == 128000000 | |||||
I2C0_F = I2C_F_DIV1280; // 100 kHz | |||||
#elif F_BUS == 120000000 | |||||
I2C0_F = I2C_F_DIV1152; // 104 kHz | I2C0_F = I2C_F_DIV1152; // 104 kHz | ||||
#elif F_BUS == 108000000 | #elif F_BUS == 108000000 | ||||
I2C0_F = I2C_F_DIV1024; // 105 kHz | I2C0_F = I2C_F_DIV1024; // 105 kHz | ||||
return *this; | return *this; | ||||
} | } | ||||
inline operator int () const __attribute__((always_inline)) { | inline operator int () const __attribute__((always_inline)) { | ||||
#if F_BUS == 120000000 | |||||
#if F_BUS == 128000000 | |||||
if (I2C0_F == I2C_F_DIV320) return 12; | |||||
#elif F_BUS == 120000000 | |||||
if (I2C0_F == I2C_F_DIV288) return 12; | if (I2C0_F == I2C_F_DIV288) return 12; | ||||
#elif F_BUS == 108000000 | #elif F_BUS == 108000000 | ||||
if (I2C0_F == I2C_F_DIV256) return 12; | if (I2C0_F == I2C_F_DIV256) return 12; |