STM32-I2C_CheckEvent- flag is automatically cleared understand

STM32 in I2C_CheckEvent function we should be quite familiar with, after each data transmission we all need to examine the appropriate EVx (x = 0,1,2 ,,,) whether the event has sent.

Function is defined as follows:

 1 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
 2 {
 3   uint32_t lastevent = 0;
 4   uint32_t flag1 = 0, flag2 = 0;
 5   ErrorStatus status = ERROR;
 6 
 7   /* Check the parameters */
 8   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
 9   assert_param(IS_I2C_EVENT(I2C_EVENT));
10 
11   /* Read the I2Cx status register */
12   flag1 = I2Cx->SR1;
13   flag2 = I2Cx->SR2;
14   flag2 = flag2 << 16;
15 
16   /* Get the last event value from I2C status register */
17   lastevent = (flag1 | flag2) & FLAG_Mask;
18 
19   /* Check whether the last event contains the I2C_EVENT */
20   if ((lastevent & I2C_EVENT) == I2C_EVENT)
21   {
22     /* SUCCESS: last event is equal to I2C_EVENT */
23     status = SUCCESS;
24   }
25   else
26   {
27     /* ERROR: last event is different from I2C_EVENT */
28     status = ERROR;
29   }
30   /* Return status */
31   return status;
32 }

The first input argument of the function is to be checked I2Cx (x = 1,2,3,4,5) peripheral, a second event parameter is examined, as follows:

I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED :       EV1
I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED :         EV1
I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED :     EV1
I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED :       EV1
I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED :         EV1
I2C_EVENT_SLAVE_BYTE_RECEIVED :                 EV2
(I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF) :       EV2
(I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL) :       EV2
I2C_EVENT_SLAVE_BYTE_TRANSMITTED :               EV3
(I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF) :     EV3
(I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL) :      EV3
I2C_EVENT_SLAVE_ACK_FAILURE :                   EV3_2
I2C_EVENT_SLAVE_STOP_DETECTED :                 EV4
I2C_EVENT_MASTER_MODE_SELECT :                 EV5
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED :        EV6 
I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED :        EV6
I2C_EVENT_MASTER_BYTE_RECEIVED :              EV7
I2C_EVENT_MASTER_BYTE_TRANSMITTING :            EV8
I2C_EVENT_MASTER_BYTE_TRANSMITTED :             EV8_2
I2C_EVENT_MASTER_MODE_ADDRESS10 :               EV9

This article will give some common event which, for example, to analyze the function Why can automatically clean flag. It contains EV5, EV6, EV8 and EV7 events.

When using the I2C transmit data we will use EV5, EV6, EV8 event, the event name and are defined as follows:

/*I2C_EVENT_MASTER_MODE_SELECT                          : EV5*/
#define  I2C_EVENT_MASTER_MODE_SELECT                      ((uint32_t)0x00030001)  /* BUSY, MSL and SB flag */

/*I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED            : EV6 */
#define  I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED        ((uint32_t)0x00070082)  /* BUSY, MSL, ADDR and TRA flags */
/*I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED               : EV6*/
#define  I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED           ((uint32_t)0x00030002)  /* BUSY, MSL and ADDR flags */


/*I2C_EVENT_MASTER_BYTE_TRANSMITTING                    : EV8*/
#define I2C_EVENT_MASTER_BYTE_TRANSMITTING                 ((uint32_t)0x00070080) /* TRA, BUSY, MSL, TXE flags */
/*I2C_EVENT_MASTER_BYTE_TRANSMITTED                     : EV8_2*/
#define  I2C_EVENT_MASTER_BYTE_TRANSMITTED                 ((uint32_t)0x00070084)  /* TRA, BUSY, MSL, TXE and BTF flags */
  • According to the definition I2C_CheckEvent function, event 16 of a high peripheral I2C register SR2, the lower 16 bits of the register SR1 I2C peripherals.
  • Let's look I2C_EVENT_MASTER_MODE_SELECT, is defined as a macro, 0x00030001, the corresponding registers SR1 and SR2 are as follows:

Where MSL is a table master mode, the BUSY Bus Busy in Table 1,

The two are usually generated when the STOP signal is set to 0, other times 1.

See how SB bit is cleared,

We read registers SR1 inside Check_Event function, we need to send the device to access the address after the start signal is transmitted, the function of which we I2C_Send7bitAddress accessed data register SD, SB is detected then the next flag bit cleared before.

  • Let's look at two events I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED EV6 and I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED, this difference between the two is a mode for sending and one for receiving mode, the transmission mode is 0x00070082,

 

TRA data generating unit generally cleared after the STOP signal, as ADDR bits, in which I2C_CheckEvent function, we are sequentially read registers SR1 and SR2, so ADDR bit is cleared. For TxE position, we will be able to perform the next data transmission operation, which is used to access functions I2C_SendData data register DR, so TxE also cleared.

  • There is also a EV8 event I2C_EVENT_MASTER_BYTE_TRANSMITTED, defined as 0x00070084,

 

Here only discuss cleared BTF bit,

SR1 also register and access to read or write data register clears this bit, so the BTF bit is cleared.

As for the other events which will be cleared I2C_CheckEvent designed, I will not, for example, you can follow this line of thought, reference STM32F10x- Chinese reference manual to see themselves step by step.

I2C_ChencEvent better than I2C_GetFlagStatus function point is that it detects all register bits associated with the event, which only detect flag provided.

 

Guess you like

Origin www.cnblogs.com/jackis-tang/p/11224512.html