1 Reply Latest reply on Jun 12, 2015 12:50 PM by user_1377889

    I2C

    mengxuan.ma

      I'm trying to let one master board read one byte from a slave board and display the read result in master's LCD.The result doesn't correct. Unfortunately, I don't know how to correct it.  Could you please give me some suggestions? I really appreciate any help from you. You can read the programs and some explanations below. The project is also attached. 

         

      In my project, one PSoC 5 board used as a master is supposed to read one byte from a slave PSoC 5 board.  Slave board's address is set to 8 in both topdesign and program.  I also use a LCD to display the value which is read by master.  

         

      The program of master is here:

         

      int main()

         

      {

         

          uint8 temp1,temp2;

         

          uint8 buffer = 0x00;

         

       

         

          LCD_Char_Start();

         

          

         

          LCD_Char_PrintString("Start I2C");

         

          I2CM_Start();

         

          CyDelay(200u);

         

          LCD_Char_ClearDisplay();

         

          

         

          LCD_Char_PrintString("Enable Intrp");

         

          /* Enable global interrupts - required for I2C */

         

          CyGlobalIntEnable;

         

          CyDelay(200u/*ms*/);

         

          LCD_Char_ClearDisplay();

         

          

         

          LCD_Char_PrintString("Clr I2C status");

         

          I2CM_MasterClearStatus(); /* Clear any previous status */

         

          CyDelay(200u/*ms*/);

         

         

         

          while(1){

         

          

         

              LCD_Char_ClearDisplay(); LCD_Char_PrintString("Sending data");

         

       

         

              do{

         

                  temp1=I2CM_MasterReadBuf(I2C_SLAVE_ADDRESS, (uint8 *)&buffer, MRD_BUFFER_SIZE, I2CM_MODE_COMPLETE_XFER);

         

              }while(temp1!=I2CM_MSTR_NO_ERROR);

         

                  

         

              LCD_Char_ClearDisplay(); LCD_Char_PrintString("Waiting for completion"); CyDelay(200u/*ms*/);

         

       

         

              while(I2CM_MasterStatus() == I2CM_MSTAT_XFER_INP);

         

              

         

              if(I2CM_MasterStatus()!=I2CM_MSTAT_RD_CMPLT){    

         

               

         

               LCD_Char_ClearDisplay(); LCD_Char_PrintInt8(I2CM_MasterStatus());

         

      //when I run the project, LCD shows return value of I2CM_MasterStatus() is A1. What does it mean? 

         

               CyDelay(200u/*ms*/);

         

       

         

              } 

         

              temp2=I2CM_MasterClearStatus();

         

       

         

              LCD_Char_ClearDisplay();  

         

              if(temp2==I2CM_MSTAT_ERR_XFER){

         

                      LCD_Char_PrintString("I2C Error");

         

              }else{

         

                      LCD_Char_PrintInt8(buffer);

         

              }

         

              /* Delay introduced for ease of reading LCD */

         

              CyDelay(800u/*ms*/); 

         

              LCD_Char_ClearDisplay();  

         

              LCD_Char_PrintString("I2C Loop");

         

              CyDelay(800u/*ms*/);    

         

       

         

          }      

         

      } /* End main */

         

      /* [] END OF FILE */

         

       

         

      Here is the program of slave:

         

      int main()

         

      {

         

          

         

          uint8 buffer=0x01;

         

          

         

          I2CS_SlaveSetAddress(8);

         

          

         

          I2CS_SlaveInitReadBuf((uint8 *)&buffer, RD_BUFFER_SIZE);

         

          

         

          /*start I2C Slave operation*/

         

          I2CS_Start();

         

          /* Enable global interrupts - needed for I2C operation */

         

          CyGlobalIntEnable;

         

          

         

          while(1){

         

              while( I2CS_SlaveGetReadBufSize()< 1 );

         

              buffer++;

         

              I2CS_SlaveClearReadStatus();

         

              I2CS_SlaveClearReadBuf();

         

          }

         

          

         

          return 0;

         

      } /* End of main */

         

       

         

      From my understanding, if the project works well, master can read slave's buffer where 0x01 stores and the buffer in master will be changed to 0x01 from 0x00. So that I can see 01 in LCD.  However, when I run the project, I2CM_MasterStatus() return A1 and the buffer in master is still 0X00.  

         

      It is interesting that why the return value of I2CM_MasterStatus() is A1. I guess A1 shows me I2C_MSTAT_RD_CMPLT, I2C_MSTAT_ERR_ADDR_NAK and I2C_MSTAT_ERR_XFER . But why there is something wrong with address. 

         

      I'm so confused. I really appreciate any suggestions and help from you. Thank you so much!

        • 1. Re: I2C
          user_1377889

          Look into I2CM.h, search for "error".

             

          Then you see the bit-codes for your returned error:

             

              #define I2CM_MSTAT_ERR_ADDR_NAK     (0x20u) /* Slave did not ACK */
              #define I2CM_MSTAT_ERR_XFER         (0x80u) /* Error during transfer */

              /* Master API returns */
              #define I2CM_MSTR_BUS_BUSY          (0x01u) /* Bus is busy, process not started */
           

             

          since in the case of one error you re-try reading (which I would NOT do) there might be some codes mixed.

             

           

             

          Bob