6 #include "DrvI2cMaster.hpp"
7 #include "RtosInclude.hpp"
9 DrvI2cMaster* DrvI2cMaster::sInstances[2];
11 DrvI2cMaster::DrvI2cMaster() :
15 void DrvI2cMaster::init(DrvTypes::I2C port) {
20 case DrvTypes::DRV_I2C1:
23 irqTypeEv = I2C1_EV_IRQn;
24 irqTypeEr = I2C1_ER_IRQn;
26 case DrvTypes::DRV_I2C2:
29 irqTypeEv = I2C2_EV_IRQn;
30 irqTypeEr = I2C2_ER_IRQn;
36 mI2c->TIMINGR = 0x00902025;
38 NVIC ->IP[irqTypeEv] = RTOS_INT_PRI;
39 NVIC ->IP[irqTypeEr] = RTOS_INT_PRI;
40 NVIC_EnableIRQ(irqTypeEv);
41 NVIC_EnableIRQ(irqTypeEr);
44 void DrvI2cMaster::setSlaveAddr(uint16_t slaveAddr) {
45 mSlaveAddr = slaveAddr;
48 bool DrvI2cMaster::readWrite(
const uint8_t* src,
int srcLen, uint8_t* dst,
int dstLen) {
54 mI2c->CR1 = I2C_CR1_TCIE | I2C_CR1_ERRIE | I2C_CR1_TXIE | I2C_CR1_RXIE | I2C_CR1_NACKIE | I2C_CR1_PE;
56 mI2c->CR2 = I2C_CR2_START | ((srcLen << 16) & I2C_CR2_NBYTES) | (mSlaveAddr & I2C_CR2_SADD);
58 else if (dstLen > 0) {
59 mI2c->CR2 = I2C_CR2_START | I2C_CR2_RD_WRN | ((dstLen << 16) & I2C_CR2_NBYTES) | (mSlaveAddr & I2C_CR2_SADD);
73 void DrvI2cMaster::onEvent() {
74 uint16_t status = mI2c->ISR;
76 if (status & I2C_ISR_TXE) {
79 if (status & I2C_ISR_TXIS) {
83 if (status & I2C_ISR_RXNE) {
89 if (status & I2C_ISR_ADDR) {
92 if (status & I2C_ISR_NACKF) {
95 if (status & I2C_ISR_STOPF) {
98 if (status & I2C_ISR_TC) {
100 mI2c->CR2 = I2C_CR2_START | I2C_CR2_RD_WRN | ((mDstLen << 16) & I2C_CR2_NBYTES) | (mSlaveAddr & I2C_CR2_SADD);
103 mI2c->CR2 = I2C_CR2_STOP;
108 if (status & I2C_ISR_TCR) {
111 if (status & I2C_ISR_BUSY) {
114 if (status & I2C_ISR_DIR) {
117 if (status & I2C_ISR_ADDCODE) {
123 void DrvI2cMaster::onError() {
126 uint16_t status = mI2c->ISR;
127 if (status & I2C_ISR_BERR) {
130 if (status & I2C_ISR_ARLO) {
133 if (status & I2C_ISR_OVR) {
136 if (status & I2C_ISR_TIMEOUT) {
139 if (status & I2C_ISR_PECERR) {
142 if (status & I2C_ISR_ALERT) {
147 void RtosInterrupt::IRQ_I2C1_EV() {
148 DrvI2cMaster::sInstances[0]->onEvent();
151 void RtosInterrupt::IRQ_I2C1_ER() {
152 DrvI2cMaster::sInstances[0]->onError();
155 void RtosInterrupt::IRQ_I2C2_EV() {
156 DrvI2cMaster::sInstances[1]->onEvent();
159 void RtosInterrupt::IRQ_I2C2_ER() {
160 DrvI2cMaster::sInstances[1]->onError();