cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 StdPeriphLib I2C Slave

dman
Associate II
Posted on April 05, 2016 at 12:57

Hi,

I am trying to build a slave using a STM32F103. I've got the ''Optimized I2C Examples'' project to use it as a base and I get the idea of what is happening.

The bit I am stuck at is finding a way to check if master is trying to read or write. I cannot seem to find any info in the reference manual.

In my I2C2_EV_IRQHandler I've implemented the I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED and I can see it being matched if I try to read or write from master, but cannot find a way to let slave identify what master wants.

(The my.st forum won't let me add code properly as the site seems to have an error)

void I2C2_EV_IRQHandler(void)

{

    __IO uint32_t SR1Register =0;

    __IO uint32_t SR2Register =0;

    

    /* Read the I2C2 SR1 and SR2 status registers */

    SR1Register = I2C2->SR1;

    SR2Register = I2C2->SR2;

    /* If I2C2 is slave (MSL flag = 0) */

    if (I2C_GetFlagStatus(I2C2,I2C_FLAG_MSL) == RESET)

    {

        /* If ADDR = 1: EV1 */

        if (I2C_CheckEvent(I2C2,I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED))

        {

regVal = I2C_ReadRegister(I2C2,I2C_Register_OAR1);

            /* Clear SR1Register SR2Register variables to prepare for next IT*/

            SR1Register = 0;

            SR2Register = 0;

            /* Initialize the transmit/receive counters for next transmission/reception

            using Interrupt  */

            Tx_Idx2 = 0;

            Rx_Idx2 = 0;

fs = I2C_GetFlagStatus(I2C2,I2C_FLAG_TRA);

        }

        /* If TXE = 1: EV3 */

        if ((SR1Register & 0x0080) == 0x0080)

        {

            /* Write data in data register */

            I2C2->DR = Buffer_Tx2[Tx_Idx2++];

            SR1Register = 0;

            SR2Register = 0;

        }

        /* If RXNE = 1: EV2 */

        if (I2C_CheckEvent(I2C2,I2C_EVENT_SLAVE_BYTE_RECEIVED))

        {

            /* Read data from data register */

            Buffer_Rx2[Rx_Idx2++] = I2C2->DR;

            SR1Register = 0;

            SR2Register = 0;

        }

        /* If STOPF =1: EV4 (Slave has detected a STOP condition on the bus */

        if (( SR1Register & 0x0010) == 0x0010)

        {

            I2C2->CR1 |= CR1_PE_Set;

            SR1Register = 0;

            SR2Register = 0;

        }

    } /* End slave mode */

/* If SB = 1, I2C1 master sent a START on the bus: EV5) */

    if ((SR1Register &0x0001) == 0x0001)

    {

        /* Send the slave address for transmssion or for reception (according to the configured value

            in the write master write routine */

        I2C1->DR = Address;

        SR1Register = 0;

        SR2Register = 0;

    }

}

#i2c-slave-stm32f1
0 REPLIES 0