This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

TWI master does not send NACK

For some reason it seems that the TWI master does not generate a NACK. As a consequence, the TWI slave keeps sending data. I have managed to create a very simple function that should generate a NACK:

// ----------------------------------------------------------------------------------------------------
// I2C Read
// ----------------------------------------------------------------------------------------------------
uint8_t i2c_read (uint8_t ack)
{
    // ------------------------------------------------------------------------------------------------
    // Auxiliary variables
    // ------------------------------------------------------------------------------------------------
    uint8_t data;
    
    // ------------------------------------------------------------------------------------------------
    // Wait for data
    // ------------------------------------------------------------------------------------------------
    while (NRF_TWI1->EVENTS_RXDREADY == 0) { }

    // ------------------------------------------------------------------------------------------------
    // In case of the last byte, generate a stop condition before reading the byte
    // ------------------------------------------------------------------------------------------------
    if (ack == 0) {
        NRF_TWI1->TASKS_STOP;
    }

    // ------------------------------------------------------------------------------------------------
    // Read the byte
    // ------------------------------------------------------------------------------------------------
    data = NRF_TWI1->RXD;

    // ------------------------------------------------------------------------------------------------
    // Return data
    // ------------------------------------------------------------------------------------------------
    return data;
}

The function should generate a NACK is the parameter of the function is zero. Then the function creates a STOP task before it reads the data. According to the product specification, the implementation should be right.

I am using nRF52-DK + nRF5 (16.0.0) + SES.

Parents Reply
  • I have actually found the problem after several hours of debugging. The problem was related to line 20:

    NRF_TWI1->TASKS_STOP;

    The line should be as follows:

    NRF_TWI1->TASKS_STOP = 1;

    Then the master generates a NACK before a stop condition.

    For some reason, SES does not generate any kind of warning about an empty statement. As a consequence, it took some time before I finally found the root cause of the problem.

Children
No Data
Related