浏览代码

Add hardware description for clock gate control

main
PaulStoffregen 8 年前
父节点
当前提交
81cd07d106
共有 2 个文件被更改,包括 24 次插入9 次删除
  1. +15
    -8
      WireKinetis.cpp
  2. +9
    -1
      WireKinetis.h

+ 15
- 8
WireKinetis.cpp 查看文件



void sda_rising_isr(void); void sda_rising_isr(void);


TwoWire::TwoWire(KINETIS_I2C_t &myport) : port(myport)
TwoWire::TwoWire(KINETIS_I2C_t &myport, const I2C_Hardware_t &myhardware)
: port(myport), hardware(myhardware)
{ {
rxBufferIndex = 0; rxBufferIndex = 0;
rxBufferLength = 0; rxBufferLength = 0;
//serial_print("\nWire Begin\n"); //serial_print("\nWire Begin\n");


slave_mode = 0; slave_mode = 0;
SIM_SCGC4 |= SIM_SCGC4_I2C0; // TODO: use bitband
hardware.clock_gate_register |= hardware.clock_gate_mask;
port.C1 = 0; port.C1 = 0;
// On Teensy 3.0 external pullup resistors *MUST* be used // On Teensy 3.0 external pullup resistors *MUST* be used
// the PORT_PCR_PE bit is ignored when in I2C mode // the PORT_PCR_PE bit is ignored when in I2C mode


void TwoWire::setClock(uint32_t frequency) void TwoWire::setClock(uint32_t frequency)
{ {
if (!(SIM_SCGC4 & SIM_SCGC4_I2C0)) return;
if (!(hardware.clock_gate_register & hardware.clock_gate_mask)) return;


#if F_BUS == 120000000 #if F_BUS == 120000000
if (frequency < 400000) { if (frequency < 400000) {
void TwoWire::setSDA(uint8_t pin) void TwoWire::setSDA(uint8_t pin)
{ {
if (pin == sda_pin_num) return; if (pin == sda_pin_num) return;
if ((SIM_SCGC4 & SIM_SCGC4_I2C0)) {
if ((hardware.clock_gate_register & hardware.clock_gate_mask)) {
if (sda_pin_num == 18) { if (sda_pin_num == 18) {
CORE_PIN18_CONFIG = 0; CORE_PIN18_CONFIG = 0;
} else if (sda_pin_num == 17) { } else if (sda_pin_num == 17) {
void TwoWire::setSCL(uint8_t pin) void TwoWire::setSCL(uint8_t pin)
{ {
if (pin == scl_pin_num) return; if (pin == scl_pin_num) return;
if ((SIM_SCGC4 & SIM_SCGC4_I2C0)) {
if ((hardware.clock_gate_register & hardware.clock_gate_mask)) {
if (scl_pin_num == 19) { if (scl_pin_num == 19) {
CORE_PIN19_CONFIG = 0; CORE_PIN19_CONFIG = 0;
} else if (scl_pin_num == 16) { } else if (scl_pin_num == 16) {


void TwoWire::end() void TwoWire::end()
{ {
if (!(SIM_SCGC4 & SIM_SCGC4_I2C0)) return;
if (!(hardware.clock_gate_register & hardware.clock_gate_mask)) return;
NVIC_DISABLE_IRQ(IRQ_I2C0); NVIC_DISABLE_IRQ(IRQ_I2C0);
port.C1 = 0; port.C1 = 0;
if (sda_pin_num == 18) { if (sda_pin_num == 18) {
CORE_PIN47_CONFIG = 0; CORE_PIN47_CONFIG = 0;
#endif #endif
} }
SIM_SCGC4 &= ~SIM_SCGC4_I2C0; // TODO: use bitband
hardware.clock_gate_register &= ~hardware.clock_gate_mask;
} }


void i2c0_isr(void) void i2c0_isr(void)






TwoWire Wire(KINETIS_I2C0);

const TwoWire::I2C_Hardware_t TwoWire::i2c0_hardware = {
SIM_SCGC4, SIM_SCGC4_I2C0
};


TwoWire Wire(KINETIS_I2C0, TwoWire::i2c0_hardware);







+ 9
- 1
WireKinetis.h 查看文件

class TwoWire : public Stream class TwoWire : public Stream
{ {
public: public:
TwoWire(KINETIS_I2C_t &myport);
// Hardware description struct
typedef struct {
volatile uint32_t &clock_gate_register;
uint32_t clock_gate_mask;
} I2C_Hardware_t;
static const I2C_Hardware_t i2c0_hardware;
public:
TwoWire(KINETIS_I2C_t &myport, const I2C_Hardware_t &myhardware);
void begin(); void begin();
void begin(uint8_t address); void begin(uint8_t address);
void begin(int address) { void begin(int address) {
port.S = I2C_S_IICIF; port.S = I2C_S_IICIF;
} }
KINETIS_I2C_t &port; KINETIS_I2C_t &port;
const I2C_Hardware_t &hardware;
uint8_t rxBuffer[BUFFER_LENGTH]; uint8_t rxBuffer[BUFFER_LENGTH];
uint8_t rxBufferIndex; uint8_t rxBufferIndex;
uint8_t rxBufferLength; uint8_t rxBufferLength;

正在加载...
取消
保存