先上一段STM32官方代码:
/**
* @brief Communication events
*
* If a communication is established (START condition generated and slave address
* acknowledged) then the master has to check on one of the following events for
* communication procedures:
*
* 1) Master Receiver mode: The master has to wait on the event EV7 then to read
* the data received from the slave (I2C_ReceiveData() function).
*
* 2) Master Transmitter mode: The master has to send data (I2C_SendData()
* function) then to wait on event EV8 or EV8_2.
* These two events are similar:
* - EV8 means that the data has been written in the data register and is
* being shifted out.
* - EV8_2 means that the data has been physically shifted out and output
* on the bus.
* In most cases, using EV8 is sufficient for the application.
* Using EV8_2 leads to a slower communication but ensure more reliable test.
* EV8_2 is also more suitable than EV8 for testing on the last data transmission
* (before Stop condition generation).
*
* @note In case the user software does not guarantee that this event EV7 is
* managed before the current byte end of transfer, then user may check on EV7
* and BTF flag at the same time (ie. (I2C_EVENT_MASTER_BYTE_RECEIVED | I2C_FLAG_BTF)).
* In this case the communication may be slower.
*
*/
/* Master RECEIVER mode -----------------------------*/
/* --EV7 */
#define I2C_EVENT_MASTER_BYTE_RECEIVED ((uint32_t)0x00030040) /* BUSY, MSL and RXNE flags */
/* Master TRANSMITTER mode --------------------------*/
/* --EV8 */
#define I2C_EVENT_MASTER_BYTE_TRANSMITTING ((uint32_t)0x00070080) /* TRA, BUSY, MSL, TXE flags */
/* --EV8_2 */
#define I2C_EVENT_MASTER_BYTE_TRANSMITTED ((uint32_t)0x00070084) /* TRA, BUSY, MSL, TXE and BTF flags */
这里定义了 EV7/EV8 两个事件,注意观察 后面的注释 /* BUSY, MSL and RXNE flags */
/* Master RECEIVER mode -----------------------------*/
/* --EV7 */
#define I2C_EVENT_MASTER_BYTE_RECEIVED ((uint32_t)0x00030040) /* BUSY, MSL and RXNE flags */
这代表 EV7这个事件对应的是 BUSY + MSL + RXNE 三个 flag 的组合。
具体是怎么对应的呢?我们先看 EV7事件这个宏定义的值为 ((uint32_t)0x00030040)
再对照SR1 和 SR2两个状态寄存器 和 上述值 可以知道:
0x0003 其实对应的是SR2的值 也就是 BUSY 和 MSL被置为1
后面的 0x0040 对应的是 SR1的值 对应的是RxNE位 被置为1.
再对照参考手册中对这三个位的描述:
就知道 EV7代表的意思是,总线上正在通讯,STM32 I2C设备处于主模式,数据寄存器(receiver接收模式)非空(有其它设备的数据通过I2C总线发送到了数据寄存器)。