这个我清楚的。可能我没描述清楚,我把完整的I2C中断代码贴出来
void I2C1_IRQHandler(void)
{
uint32_t I2C_InterruptStatus = I2C1->ISR; /* Get interrupt status */
if((I2C_InterruptStatus & I2C_ISR_ADDR) == I2C_ISR_ADDR) /* Check address match */
{
I2C1->ICR |= I2C_ICR_ADDRCF; /* Clear address match flag*/
if((I2C1->ISR & I2C_ISR_DIR) == I2C_ISR_DIR) /* Check if transfer direction is read (slave transmitter) */
{
I2C1->CR1 |= I2C_CR1_TXIE; /* Set transmit IT /status*/
i2c_event=EVENT_OPCOD_SEND; /* Set I2C entor transmit mode*/
}
else /*Write operation, slave receive status*/
{
I2C1->CR1 |= I2C_CR1_RXIE; /* Set receive IT /status*/
i2c_event=EVENT_OPCOD_BUSY_RECEIVE; /* Set I2C entor receive mode*/
}
}
else if((I2C_InterruptStatus & I2C_ISR_TXIS) == I2C_ISR_TXIS)
{
//add some application code in this place
}
/*check RXDR is not empty*/
else if((I2C_InterruptStatus & I2C_ISR_RXNE) == I2C_ISR_RXNE)
{
//I2C_ISR_RXNE add you code in this place Tomas Li add
Receive_Buffer[I2C_Receive_Counter++]= I2C1->RXDR;
i2c_event=EVENT_OPCOD_BUSY_RECEIVE;//slave is busy for receive data
但是按照协议,NACK应该由主机来发送,表示不需要从机再发数据了啊
这个我清楚的。可能我没描述清楚,我把完整的I2C中断代码贴出来
void I2C1_IRQHandler(void)
{
uint32_t I2C_InterruptStatus = I2C1->ISR; /* Get interrupt status */
if((I2C_InterruptStatus & I2C_ISR_ADDR) == I2C_ISR_ADDR) /* Check address match */
{
I2C1->ICR |= I2C_ICR_ADDRCF; /* Clear address match flag*/
if((I2C1->ISR & I2C_ISR_DIR) == I2C_ISR_DIR) /* Check if transfer direction is read (slave transmitter) */
{
I2C1->CR1 |= I2C_CR1_TXIE; /* Set transmit IT /status*/
i2c_event=EVENT_OPCOD_SEND; /* Set I2C entor transmit mode*/
}
else /*Write operation, slave receive status*/
{
I2C1->CR1 |= I2C_CR1_RXIE; /* Set receive IT /status*/
i2c_event=EVENT_OPCOD_BUSY_RECEIVE; /* Set I2C entor receive mode*/
}
I2C1->CR1 |=I2C_CR1_STOPIE;//Enable STOP interrupt
}
else if((I2C_InterruptStatus & I2C_ISR_TXIS) == I2C_ISR_TXIS)
{
//add some application code in this place
}
/*check RXDR is not empty*/
else if((I2C_InterruptStatus & I2C_ISR_RXNE) == I2C_ISR_RXNE)
{
//I2C_ISR_RXNE add you code in this place Tomas Li add
Receive_Buffer[I2C_Receive_Counter++]= I2C1->RXDR;
i2c_event=EVENT_OPCOD_BUSY_RECEIVE;//slave is busy for receive data
}
else if((I2C_InterruptStatus & I2C_ISR_NACKF) == I2C_ISR_NACKF)
{
}
/*check Stop event happen */
if((I2C_InterruptStatus & I2C_ISR_STOPF) == I2C_ISR_STOPF)
{
I2C1->ICR |=I2C_ICR_STOPCF;//clear the STOP interrupt Flag
#if 1
switch(i2c_event){
case EVENT_OPCOD_BUSY_RECEIVE://slave receive status Stop flag
I2C_Receive_Counter=0;
i2c_event = EVENT_OPCOD_NOTYET_READ;
I2C1->CR1 &= ~(I2C_CR1_STOPIE); //Disable all interrupt.except Error interrupt
I2C1->CR2 |= I2C_CR2_NACK;//set feedback Nack in next event
I2C1->CR1 |= I2C_CR1_ADDRIE;
break;
case EVENT_OPCOD_SEND: //slave send stop
I2C1->ICR |=I2C_ICR_STOPCF | I2C_ICR_NACKCF |I2C_ICR_BERRCF;//clear the STOP interrupt Flag
I2C1->CR1 |=I2C_CR1_ADDRIE;
i2c_event = NOEVENT;
break;
default:
break;
}
#endif
}
else
{
error = ERROR_I2C; /* Report an error */
}
}