diff options
Diffstat (limited to 'hal/i2c.cpp')
-rw-r--r-- | hal/i2c.cpp | 149 |
1 files changed, 48 insertions, 101 deletions
diff --git a/hal/i2c.cpp b/hal/i2c.cpp index 867597a..db9c919 100644 --- a/hal/i2c.cpp +++ b/hal/i2c.cpp @@ -1,53 +1,49 @@ #include "i2c.h" #include "rcc.h" -#include "stm32.h" #include "thread.h" -I2C* I2C::self; +#if defined(STM32F1) +I2C_t I2C1(0x40005400, 36000000, Interrupt::I2C1_EV, Interrupt::I2C1_ER); +I2C_t I2C2(0x40005800, 36000000, Interrupt::I2C2_EV, Interrupt::I2C2_ER); +#elif defined(STM32F4) +I2C_t I2C1(0x40005400, 42000000, Interrupt::I2C1_EV, Interrupt::I2C1_ER); +I2C_t I2C2(0x40005800, 42000000, Interrupt::I2C2_EV, Interrupt::I2C2_ER); +//I2C_t I2C3(0x40005c00, 42000000, Interrupt::I2C3_EV, Interrupt::I2C3_ER); +#endif -template <> -void interrupt<Interrupt::I2C1_EV>() { - I2C::self->irq_ev(); -} - -template <> -void interrupt<Interrupt::I2C1_ER>() { - I2C::self->irq_er(); -} - -void I2C::irq_ev() { - uint32_t sr1 = I2C1.SR1; - I2C1.SR2; +void I2C_t::irq_ev() { + uint32_t sr1 = reg.SR1; + reg.SR2; // EV5, SB = 1: Start condition sent. if(sr1 & 0x01) { // Send address. - I2C1.DR = (addr << 1) | (writing ? 0 : 1); + reg.DR = (addr << 1) | (writing ? 0 : 1); } // EV6, ADDR = 1: Address sent. if(sr1 & 0x02) { if(writing) { - I2C1.DR = *write_p++; + reg.DR = *write_p++; writing--; } else { if(reading > 1) { - I2C1.CR1 |= 0x400; // Set ACK. + reg.CR1 |= 0x400; // Set ACK. } else { - I2C1.CR1 |= 0x200; // Set STOP. + reg.CR1 |= 0x200; // Set STOP. } } } // EV7, RxNE = 1: Receive buffer not empty. if(sr1 & 0x40) { - *read_p++ = I2C1.DR; + *read_p++ = reg.DR; reading--; if(reading == 1) { // Unset ACK, set STOP. - I2C1.CR1 = (I2C1.CR1 & ~0x400) | 0x200; + reg.CR1 = (reg.CR1 & ~0x400) | 0x200; } if(reading == 0) { @@ -55,141 +51,92 @@ void I2C::irq_ev() { } } - //I2C1.CR1 &= ~0x400; + //reg.CR1 &= ~0x400; // EV8, TxE = 1, BTF = 0: Transmit buffer empty, still writing. if(sr1 & 0x80 && !(sr1 & 0x04)) { if(writing) { // Send data. - I2C1.DR = *write_p++; + reg.DR = *write_p++; writing--; } else { // All data sent. if(reading) { // Send repeat start. - I2C1.CR1 |= 0x100; + reg.CR1 |= 0x100; } else { // Send stop. - I2C1.CR1 |= 0x200; + reg.CR1 |= 0x200; busy = 0; } } } } -void I2C::irq_er() { +void I2C_t::irq_er() { handle_error(); } -void I2C::handle_error() { - I2C1.SR1; - I2C1.SR2; +void I2C_t::handle_error() { + reg.SR1; + reg.SR2; //while(1); - I2C1.CR1 |= 0x200; + reg.CR1 |= 0x200; busy = 0; } -void I2C::enable() { +void I2C_t::enable(Pin& scl, Pin& sda) { RCC.enable(RCC.I2C1); asm volatile("nop"); - I2C1.CR1 = 0x8000; - I2C1.CR1 = 0; + scl.set_af(4); + sda.set_af(4); + scl.set_type(Pin::OpenDrain); + sda.set_type(Pin::OpenDrain); + scl.set_mode(Pin::AF); + sda.set_mode(Pin::AF); + + reg.CR1 = 0x8000; + reg.CR1 = 0; - I2C1.CR2 = 0x700 | 36; - I2C1.TRISE = 37; - I2C1.CCR = 180; + reg.CR2 = 0x700 | (clk / 1000000); + reg.TRISE = clk / 1000000 + 1; + reg.CCR = clk / 2 / 100000; - Interrupt::enable(Interrupt::I2C1_EV); - Interrupt::enable(Interrupt::I2C1_ER); + Interrupt::enable(irq_ev_n, &I2C_t::irq_ev, this); + Interrupt::enable(irq_er_n, &I2C_t::irq_er, this); - I2C1.CR1 = 1; + reg.CR1 = 1; } -void I2C::write_reg(uint8_t addr_, uint8_t reg, uint8_t data) { +void I2C_t::write_reg(uint8_t addr_, uint8_t reg_, uint8_t data) { addr = addr_; writing = 2; reading = 0; - volatile uint8_t buf[] = {reg, data}; + volatile uint8_t buf[] = {reg_, data}; write_p = buf; busy = 1; - I2C1.CR1 |= 0x100; + reg.CR1 |= 0x100; while(busy) { Thread::yield(); } - - /* - while(!(I2C1.SR1 & 0x01)); // Wait for SB. - - I2C1.DR = (addr << 1) | 0; - while (!(I2C1.SR1 & 0x02)); // Wait for ADDR. - I2C1.SR2; - - I2C1.DR = reg; - while (!(I2C1.SR1 & 0x80)); // Wait for TxE. - - I2C1.DR = data; - while (!(I2C1.SR1 & 0x04)); // Wait for BTF. - - I2C1.CR1 |= 0x200;*/ } -void I2C::read_reg(uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf) { +void I2C_t::read_reg(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf) { addr = addr_; writing = 1; reading = len; - write_p = ® + write_p = ®_; read_p = buf; busy = 1; - I2C1.CR1 |= 0x100; + reg.CR1 |= 0x100; while(busy) { Thread::yield(); } - - /* - I2C1.CR1 |= 0x100; - while(!(I2C1.SR1 & 0x01)); // Wait for SB. - - I2C1.DR = (addr << 1) | 0; - while (!(I2C1.SR1 & 0x02)); // Wait for ADDR. - I2C1.SR2; - - I2C1.DR = reg; - while (!(I2C1.SR1 & 0x80)); // Wait for TxE. - - I2C1.CR1 |= 0x100; - while(!(I2C1.SR1 & 0x01)); // Wait for SB. - - I2C1.DR = (addr << 1) | 1; - while (!(I2C1.SR1 & 0x02)); // Wait for ADDR. - I2C1.SR2; - - I2C1.CR1 |= 0x400; // Set ACK. - - while(len) { - if(len == 3) { - while (!(I2C1.SR1 & 0x04)); // Wait for BTF. - - I2C1.CR1 &= ~0x400; // Clear ACK. - *buf++ = I2C1.DR; - len--; - - I2C1.CR1 |= 0x200; // Set STOP. - *buf++ = I2C1.DR; - len--; - - } else { - while (!(I2C1.SR1 & 0x40)); // Wait for RxNE. - - *buf++ = I2C1.DR; - len--; - } - } - */ } |