• AVR Freaks

Hot!How to recognize a master acknowledge in a dsPIC configured as an I2C slave?

Author
PICrukie
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2019/04/09 14:11:53
  • Location: 0
  • Status: offline
2019/04/22 09:39:35 (permalink)
0

How to recognize a master acknowledge in a dsPIC configured as an I2C slave?

Hi all,
 
I am configuring a dsPIC33F MCU as a slave for a communication protocol I'm developing. In this case, a Raspberry Pi was set up as a master. In the communication protocol, I'm writing; there are situations wherein the master reads 2-4 bytes of data after sending a command write. I am using a slave interrupt event for processing the reads and writes in my PIC. The slave is responding appropriately for single byte writes, double-byte writes (another requirement in the scheme), and single byte responses. But here's what is giving me an issue:
 
When the master has to read multiple bytes, the I2C read fails. I have shown the SDA and SCL signals for a multi-byte read scheme in the attachments. It appears that I am unable to write a code for a condition in the interrupt when the master acknowledges occurs. It is evident that the master is trying to read 4 bytes of data as one can see that it is sending the acknowledges but the SDA is remaining dead between the acknowledges. Does anyone have a clue about what one could do to add the code for the slave to pick up master acknowledges?
 
Thanks!

Attached Image(s)

#1

3 Replies Related Threads

    Mysil
    Super Member
    • Total Posts : 3354
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How to recognize a master acknowledge in a dsPIC configured as an I2C slave? 2019/04/22 11:54:37 (permalink)
    5 (1)
    Hi,
    Acknowledge from master to slave should be picked up in ACKSTAT bit in I2CxSTAT register.
     
    In code I use, adopted from MCC and modified, there are some macro definitions used:
    #define I2C_TRANSMIT_REG                I2C2TRN                    // Defines the transmit register used to send data.
    #define I2C_RECEIVE_REG                 I2C2RCV                    // Defines the receive register used to receive data.
    #define I2C_SCLREL                      I2C2CONbits.SCLREL = 1     // Release I2C clock stretch.
     
    #define I2C_ACKNOWLEDGE_STATUS_BIT      I2C2STATbits.ACKSTAT       // I2C ACK status bit. 

    And in code:
          case S_SLAVE_TRANSMIT_MODE:
            // this is the state where an ACK or NACK is expected
            // to occur after the slave has placed data to the
            // transmit register.

            // if the transaction was ACK'ed, more data needs to be sent
            // if the transaction was NACK'ed then we don't need to send
            // more data
            if (I2C_ACKNOWLEDGE_STATUS_BIT == 0)
            {
                // prepare next data
                i2c_data = I2C_RECEIVE_REG;
                I2C_TRANSMIT_REG = I2C2_SlaveCallback(I2C_SLAVE_TRANSMIT_REQUEST, i2c_data);
                I2C_SCLREL;    /* Release the clock */
            }
            else //if (I2C_ACKNOWLEDGE_STATUS_BIT == 1)
            {
                // no more data to be sent so we go to idle state
                I2C_Slave_State = S_SLAVE_IDLE;
            }
            break;  

     
    Debugging I2C Slave code when communicating with a Master running in another processor, may be tricky.
    If you stop in debugger to examine something, then master run away, making it difficult to proceed.
    I prefer to debug I2C slave code, with master code running in the same microcontroller, using another I2C peripheral,
    until I have code that work. Before trying testing with master running in another device.
     
        Mysil
    post edited by Mysil - 2019/04/22 12:06:50
    #2
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: How to recognize a master acknowledge in a dsPIC configured as an I2C slave? 2019/04/23 04:33:46 (permalink)
    0
    KARTIKEYA
    When the master has to read multiple bytes, the I2C read fails. I have shown the SDA and SCL signals for a multi-byte read scheme in the attachments. It appears that I am unable to write a code for a condition in the interrupt when the master acknowledges occurs. It is evident that the master is trying to read 4 bytes of data as one can see that it is sending the acknowledges but the SDA is remaining dead between the acknowledges. Does anyone have a clue about what one could do to add the code for the slave to pick up master acknowledges?
     

    You did not show any of your code, so we can't tell if you are aware that you should be reading the ACKSTAT bit as noted by Mysil.
    It is also unclear from your question of you are aware that a Master will always NAK the last byte of a read sequence.
     

    Nearly there...
    #3
    PICrukie
    New Member
    • Total Posts : 28
    • Reward points : 0
    • Joined: 2019/04/09 14:11:53
    • Location: 0
    • Status: offline
    Re: How to recognize a master acknowledge in a dsPIC configured as an I2C slave? 2019/04/24 12:50:22 (permalink)
    0
    Thank you all, for your valuable responses. The problem was indeed the ACKSTAT bit! The issue was that the interrupt was firing properly but there wasn't any code for the slave to trigger for conditions of master acknowledge. I have now added the code as suggested by Mysil and it works as expected.
    #4
    Jump to:
    © 2019 APG vNext Commercial Version 4.5