Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7719687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:28:43+00:00 2026-06-01T03:28:43+00:00

I try to implement a i2c slave receiver interrupt service routine on a stm32f4.

  • 0

I try to implement a i2c slave receiver interrupt service routine on a stm32f4.
Here is my smart piece of code.

void I2C2_EV_IRQHandler()
  {
    switch (I2C_GetLastEvent(I2C2))
    {
    //The address sent by the master matches the own address of the peripheral
    case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
        //The slave stretches SCL low until ADDR is
        //cleared and DR filled with the data to be sent
        I2C_ClearFlag(I2C2,I2C_FLAG_ADDR);
        break;

    //The application is expecting a data byte to be received
    case I2C_EVENT_SLAVE_BYTE_RECEIVED:
        I2C_ReceiveData(I2C2);
        break;

    //The application is expecting the end of the communication
    //Make sure that both ADDR and STOPF flags are cleared
    //if both are found set.
    case I2C_EVENT_SLAVE_STOP_DETECTED:
        if(I2C_GetFlagStatus(I2C2,I2C_FLAG_ADDR) == SET)
            I2C_ClearFlag(I2C2,I2C_FLAG_ADDR);
        if(I2C_GetFlagStatus(I2C2,I2C_FLAG_STOPF) == SET)
            I2C_ClearFlag(I2C2,I2C_FLAG_STOPF);
}

}

The interrupt becomes called and I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED case is entered.
The SCL is low now. The reference manual says if I clear the address flag, the clock will continue and data will be sent (Page 579 – Slave receiver). In my opinion the interrupt always becomes called if any data arrives and next state will be I2C_EVENT_SLAVE_BYTE_RECEIVED.

I can not find any example from stm or via google. Can anybody help me or show me an example.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:28:44+00:00Added an answer on June 1, 2026 at 3:28 am

    now it works. My problem was that I was not able to reset the ADDR and the STOPF register with the given commands out of reference manual. But if do it in a loop it works fine for me. Here my working Interrupt Routine.

     void I2C3_EV_IRQHandler()
     {
         switch (I2C_GetLastEvent(I2C3))
         {
    
        case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
            STM_EVAL_LEDOn(LED3);
            STM_EVAL_LEDOff(LED5);
            break;
    
        case I2C_EVENT_SLAVE_BYTE_RECEIVED:
            STM_EVAL_LEDToggle(LED4);
            STM_EVAL_LEDOff(LED3);
            I2C_InputBuffer[I2C_InputBufferIndex++] = I2C_ReceiveData(I2C3);
            break;
    
        case I2C_EVENT_SLAVE_STOP_DETECTED:
            STM_EVAL_LEDOn(LED6);
            STM_EVAL_LEDOff(LED4);
            break;
        }
    
        I2C_CleanADDRandSTOPF();
    
        if(I2C_InputBufferIndex > MOTOR_PACKAGE_SIZE-1)
        {
          motorHandleEvent(I2C_InputBuffer);
          I2C_InputBufferIndex = 0;
          uint8_t resetIndex;
          for(resetIndex = 0; resetIndex < MOTOR_PACKAGE_SIZE; resetIndex ++)
            I2C_InputBuffer[resetIndex] = 0;
        }
    }
    
    inline void I2C_CleanADDRandSTOPF()
    {
      while ((I2C3->SR1 & I2C_SR1_ADDR) == I2C_SR1_ADDR)
      {
        volatile uint32_t temp;
        temp=I2C3->SR1;
        temp=I2C3->SR2;
      }
      while ((I2C3->SR1&I2C_SR1_STOPF) == I2C_SR1_STOPF)
      {
        volatile uint32_t temp;
        temp=I2C3->SR1;
        I2C3->CR1 |= 0x1;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to implement php image verification code, here is in test.php file: <form
I try to implement the following code: <div class=clearid=usernameline>username:<input type=text name=username id=username style=border:1px solid
I get a NetworkOnMainThreadException when I try to implement the following code: public class
I try implement advance on my question form here: send parameter from view-model to
When I try to implement the below code: $('.touchelement').bind({'touchstart':function(e){ console.info(e.touches.item(0)); }}); It shows the
Try to implement left and right gestures in my application. Here is my layout:
I try to implement the word count example by myself, here's my implementation of
I try to implement the 'AsyncPattern' within a WCF data service. I define the
I try to implement the following code, this page will display when survey is
I try to implement the following code: <!DOCTYPE html> <html lang=en> <head> <meta http-equiv=Content-Type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.