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

NRF51 twi scanner: getting registers of i2c devices on bus

I have a small collection of i2c devices on the bus that can be jumpered in (sda/scl disconnected). I am currently using SDK v10 and Keil v4. 

I wrote this code to namely sweep through all addresses and read out the first few registers of each. But I run this code with the twi_scanner example program and I end up getting error codes of 0, and readResults of 0 as well. How should I fix my code to successfully get the registers of the i2c devices?

//==========START example==================

#<included everything needed for this thing to compile>

void twi_init (void){
    ret_code_t err_code;
    

    //mma7660 is from the example, won't bother changing variable names.
    const nrf_drv_twi_config_t twi_mma_7660_config = {
       .scl                = 16,
       .sda                = 15,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH
    };
    
    err_code = nrf_drv_twi_init(&m_twi_mma_7660, &twi_mma_7660_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);
    
    nrf_drv_twi_enable(&m_twi_mma_7660);
}

int main(void){
        uart_config();
        twi_init();
        uint8_t readResult = 0x00;
        ret_code_t rt;
        for(int adr = 0; adr< 128; ++adr){
            for(int i = 0; i<0xF; ++i){
                rt = nrf_drv_twi_rx(&m_twi_mma_7660, adr, &readResult, 1, false);
                nrf_delay_ms(100);
                printf("%d>>0x%d:%x\t%d\r\n",adr,i,readResult,rt);
            
            }
        }
    
    
    while(true){
                printf("done\r\n");
                nrf_delay_ms(500);
    }
}

//================END of example======================

  • Hello,

    So you do not have any error codes (rt) != 0?

    Have you tried the unmodified example, to see if that works? I see that there aren't any twi_scanner examples in SDK10, but maybe you can test the one in SDK12.3.0 just to see that all the HW is working as expected?

    BR,

    Edvin

  • Hi I do not get any error codes that are not 0.

    When I try running the unmodified TWI_Scanner example from SDK v12 with two i2c items on the NRF51 DK sda/scl lines (one at address 0x40, and one at 0x38) the scanning gets stuck on address 0x1.

    If I plug in any single wire (SDA/SCL) into either P0.07 or P0.30, I get:

    APP:INFO:TWI scanner.
    APP:INFO:scanning: 0x1.

    The 0x1 line doesn't print if I don't force a flush immediately after printing that line. I also tried using other pins and also different ways of powering the i2c items. 

    I debugged the unmodified twi_scanner code and it got stuck on a while loop in nrf_drv_twi.c (starting on line 615). I then traced the while loop calls to 'return true' being returned by the if statement on line 518 of the same nrf_drv_twi.c file. 

    @Edvin I will be getting a logic analyzer to do a further trace of the original twi_scanner code, but do you have any suggestions as to why the code I wrote does not read the registers of the i2c devices on the bus? I am still confused as to why it only returns error codes of 0 and readResults of 0. Thanks!

Related