瀏覽代碼

Add setSDA(pin) & setSCK(pin)

main
PaulStoffregen 8 年之前
父節點
當前提交
1c77cdd974
共有 3 個文件被更改,包括 71 次插入6 次删除
  1. +59
    -5
      Wire.cpp
  2. +9
    -1
      Wire.h
  3. +3
    -0
      keywords.txt

+ 59
- 5
Wire.cpp 查看文件

@@ -19,7 +19,9 @@
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 <string.h> // for memcpy
@@ -30,11 +32,12 @@
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+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;

@@ -62,8 +65,16 @@ void TwoWire::begin(void)
// would enable pullup resistors. However, there seems
// to be a bug in chip while I2C is enabled, where setting
// 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);
I2C0_C2 = I2C_C2_HDRS;
I2C0_C1 = I2C_C1_IICEN;
@@ -158,6 +169,42 @@ void TwoWire::setClock(uint32_t frequency)
#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)
{
begin();
@@ -588,7 +635,6 @@ extern "C" {
#include "twi.h"
}

#include "Wire.h"

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

@@ -649,6 +695,14 @@ 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)
{
// clamp to buffer length

+ 9
- 1
Wire.h 查看文件

@@ -28,7 +28,9 @@
#define BUFFER_LENGTH 32
#define WIRE_HAS_END 1

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

class TwoWire : public Stream
{
@@ -48,7 +50,11 @@ class TwoWire : public Stream
static void (*user_onRequest)(void);
static void (*user_onReceive)(int);
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);
#endif
public:
TwoWire();
void begin();
@@ -56,6 +62,8 @@ class TwoWire : public Stream
void begin(int);
void end();
void setClock(uint32_t);
void setSDA(uint8_t);
void setSCL(uint8_t);
void beginTransmission(uint8_t);
void beginTransmission(int);
uint8_t endTransmission(void);
@@ -95,7 +103,7 @@ class TwoWire : public Stream

extern TwoWire Wire;

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

+ 3
- 0
keywords.txt 查看文件

@@ -18,6 +18,9 @@ send KEYWORD2
receive KEYWORD2
onReceive KEYWORD2
onRequest KEYWORD2
setClock KEYWORD2
setSDA KEYWORD2
setSCL KEYWORD2

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

Loading…
取消
儲存