Browse Source

Add setSDA(pin) & setSCK(pin)

main
PaulStoffregen 8 years ago
parent
commit
1c77cdd974
3 changed files with 71 additions and 6 deletions
  1. +59
    -5
      Wire.cpp
  2. +9
    -1
      Wire.h
  3. +3
    -0
      keywords.txt

+ 59
- 5
Wire.cpp View File

Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
*/ */


#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
#include "Wire.h"

#if defined(__arm__) && defined(CORE_TEENSY)


#include "kinetis.h" #include "kinetis.h"
#include <string.h> // for memcpy #include <string.h> // for memcpy
uint8_t TwoWire::rxBuffer[BUFFER_LENGTH]; uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
uint8_t TwoWire::rxBufferIndex = 0; uint8_t TwoWire::rxBufferIndex = 0;
uint8_t TwoWire::rxBufferLength = 0; uint8_t TwoWire::rxBufferLength = 0;
//uint8_t TwoWire::txAddress = 0;
uint8_t TwoWire::txBuffer[BUFFER_LENGTH+1]; uint8_t TwoWire::txBuffer[BUFFER_LENGTH+1];
uint8_t TwoWire::txBufferIndex = 0; uint8_t TwoWire::txBufferIndex = 0;
uint8_t TwoWire::txBufferLength = 0; uint8_t TwoWire::txBufferLength = 0;
uint8_t TwoWire::transmitting = 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_onRequest)(void) = NULL;
void (*TwoWire::user_onReceive)(int) = NULL; void (*TwoWire::user_onReceive)(int) = NULL;


// would enable pullup resistors. However, there seems // would enable pullup resistors. However, there seems
// to be a bug in chip while I2C is enabled, where setting // to be a bug in chip while I2C is enabled, where setting
// those causes the port to be driven strongly high. // those causes the port to be driven strongly high.
CORE_PIN18_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
CORE_PIN19_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
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 (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;
}
setClock(100000); setClock(100000);
I2C0_C2 = I2C_C2_HDRS; I2C0_C2 = I2C_C2_HDRS;
I2C0_C1 = I2C_C1_IICEN; I2C0_C1 = I2C_C1_IICEN;
#endif #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 (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;
}
}
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 (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;
}
}
scl_pin_num = pin;
}

void TwoWire::begin(uint8_t address) void TwoWire::begin(uint8_t address)
{ {
begin(); begin();
#include "twi.h" #include "twi.h"
} }


#include "Wire.h"


// Initialize Class Variables ////////////////////////////////////////////////// // Initialize Class Variables //////////////////////////////////////////////////


TWBR = ((F_CPU / frequency) - 16) / 2; 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) uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{ {
// clamp to buffer length // clamp to buffer length

+ 9
- 1
Wire.h View File

#define BUFFER_LENGTH 32 #define BUFFER_LENGTH 32
#define WIRE_HAS_END 1 #define WIRE_HAS_END 1


#if defined(__arm__) && defined(CORE_TEENSY)
extern "C" void i2c0_isr(void); extern "C" void i2c0_isr(void);
#endif


class TwoWire : public Stream class TwoWire : public Stream
{ {
static void (*user_onRequest)(void); static void (*user_onRequest)(void);
static void (*user_onReceive)(int); static void (*user_onReceive)(int);
static void sda_rising_isr(void); static void sda_rising_isr(void);
#if defined(__arm__) && defined(CORE_TEENSY)
static uint8_t sda_pin_num;
static uint8_t scl_pin_num;
friend void i2c0_isr(void); friend void i2c0_isr(void);
#endif
public: public:
TwoWire(); TwoWire();
void begin(); void begin();
void begin(int); void begin(int);
void end(); void end();
void setClock(uint32_t); void setClock(uint32_t);
void setSDA(uint8_t);
void setSCL(uint8_t);
void beginTransmission(uint8_t); void beginTransmission(uint8_t);
void beginTransmission(int); void beginTransmission(int);
uint8_t endTransmission(void); uint8_t endTransmission(void);


extern TwoWire Wire; extern TwoWire Wire;


#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
#if defined(__arm__) && defined(CORE_TEENSY)
class TWBRemulation class TWBRemulation
{ {
public: public:

+ 3
- 0
keywords.txt View File

receive KEYWORD2 receive KEYWORD2
onReceive KEYWORD2 onReceive KEYWORD2
onRequest KEYWORD2 onRequest KEYWORD2
setClock KEYWORD2
setSDA KEYWORD2
setSCL KEYWORD2


####################################### #######################################
# Instances (KEYWORD2) # Instances (KEYWORD2)

Loading…
Cancel
Save