6 #include "DrvI2cMaster.hpp"
7 #include "RtosInclude.hpp"
9 #if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL)
10 DrvI2cMaster* DrvI2cMaster::sInstances[2];
12 DrvI2cMaster* DrvI2cMaster::sInstances[1];
15 DrvI2cMaster::DrvI2cMaster() :
19 void DrvI2cMaster::init(DrvTypes::I2C port,
int baudrate) {
24 case DrvTypes::DRV_I2C1:
27 irqTypeEv = I2C1_EV_IRQn;
28 irqTypeEr = I2C1_ER_IRQn;
30 #if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL)
31 case DrvTypes::DRV_I2C2:
34 irqTypeEv = I2C2_EV_IRQn;
35 irqTypeEr = I2C2_ER_IRQn;
42 mI2c->CR2 = I2C_CR2_ITBUFEN | I2C_CR2_ITEVTEN | I2C_CR2_ITERREN | (DRV_CFG_PCLK1_FREQ / 1000000);
43 mI2c->CCR = DRV_CFG_PCLK1_FREQ / baudrate;
45 NVIC ->IP[irqTypeEv] = RTOS_INT_PRI;
46 NVIC ->IP[irqTypeEr] = RTOS_INT_PRI;
47 NVIC_EnableIRQ(irqTypeEv);
48 NVIC_EnableIRQ(irqTypeEr);
51 void DrvI2cMaster::setSlaveAddr(uint16_t slaveAddr) {
52 mSlaveAddr = slaveAddr;
55 bool DrvI2cMaster::readWrite(
const uint8_t* src,
int srcLen, uint8_t* dst,
int dstLen) {
60 mI2c->CR1 = I2C_CR1_PE;
61 mI2c->CR1 |= I2C_CR1_START;
70 void DrvI2cMaster::onEvent() {
71 uint16_t status = mI2c->SR1;
73 if (status & I2C_SR1_SB ) {
77 mI2c->DR = mSlaveAddr;
79 else if (mDstLen > 0) {
80 mI2c->DR = mSlaveAddr | 0x01;
83 mI2c->CR1 |= I2C_CR1_STOP;
86 else if (status & I2C_SR1_ADDR ) {
90 else if (mDstLen > 0) {
92 mI2c->CR1 |= I2C_CR1_ACK;
95 mI2c->CR1 &= ~I2C_CR1_ACK;
99 mI2c->CR1 |= I2C_CR1_STOP;
101 volatile uint16_t dummy = mI2c->SR2;
104 else if (status & I2C_SR1_STOPF ) {
107 else if (status & I2C_SR1_RXNE ) {
109 mI2c->CR1 |= I2C_CR1_ACK;
112 else if (mDstLen > 0) {
113 mI2c->CR1 &= ~I2C_CR1_ACK;
117 mI2c->CR1 |= I2C_CR1_STOP;
121 else if (status & I2C_SR1_TXE ) {
125 else if (status & I2C_SR1_BTF ) {
126 mI2c->CR1 |= I2C_CR1_STOP;
131 void DrvI2cMaster::onError() {
133 uint16_t status = mI2c->SR1;
137 if (status & I2C_SR1_BERR ) {
138 mI2c->SR1 |= I2C_SR1_BERR;
142 else if (status & I2C_SR1_ARLO ) {
146 else if (status & I2C_SR1_AF ) {
147 mI2c->CR1 |= I2C_CR1_STOP;
149 else if (status & I2C_SR1_OVR ) {
154 else if (status & I2C_SR1_PECERR ) {
159 else if (status & I2C_SR1_TIMEOUT ) {
160 mI2c->CR1 |= I2C_CR1_STOP;
162 else if (status & I2C_SR1_SMBALERT ) {
169 void RtosInterrupt::IRQ_I2C1_EV() {
170 DrvI2cMaster::sInstances[0]->onEvent();
173 void RtosInterrupt::IRQ_I2C1_ER() {
174 DrvI2cMaster::sInstances[0]->onError();
177 #if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL)
178 void RtosInterrupt::IRQ_I2C2_EV() {
179 DrvI2cMaster::sInstances[1]->onEvent();
182 void RtosInterrupt::IRQ_I2C2_ER() {
183 DrvI2cMaster::sInstances[1]->onError();