sensor reading from address using nrf52 TWIM

hi, Im trying to read from a sensor register using TWIM peripheral on nrf52840 in blocking mode, but it wont work , doesnt freeze or anything .. just doesnt work

below you will find a code snippet of my read function. if any information needed is missing please tell me

to illustrate what im trying to do in the snippet, i first enable short cuts between lasttx and startrx and the short cut between lastrx and stop. then i enter the slave address in the address register

then put the register address in the tx register to be sent out and the buffer in the rx register

what im hopping happens is that the register address is sent out then startrx would be triggered reading bytes coming from the sensor triggering stop task when lastrx event is generated

after that i disable shorts and clear the events.. can someone tell me what am i missing?

thank you,

void user_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len)
{
  
    nrf_twim_shorts_enable(I2C0_add,NRF_TWIM_SHORT_LASTTX_STARTRX_MASK);
    nrf_twim_shorts_enable(I2C0_add,NRF_TWIM_SHORT_LASTRX_STOP_MASK );

    // 7-bit address of sensor
    nrf_twim_address_set(I2C0_add,0x76);
    
    // data length i want to read
     nrf_twim_rx_buffer_set(I2C0_add,reg_data,len);
     
     // register address sent n write operation
    nrf_twim_tx_buffer_set(I2C0_add,&reg_addr,1);
    
    nrf_twim_task_trigger(I2C0_add,NRF_TWIM_TASK_STARTTX );

    while(nrf_twim_event_check(I2C0_add,NRF_TWIM_EVENT_STOPPED) == 0 );

    nrf_twim_shorts_disable(I2C0_add,NRF_TWIM_SHORT_LASTTX_STARTRX_MASK);
    nrf_twim_shorts_disable(I2C0_add,NRF_TWIM_SHORT_LASTRX_STOP_MASK );
    
    nrf_twim_event_clear(I2C0_add,NRF_TWIM_EVENT_LASTTX);
    nrf_twim_event_clear(I2C0_add,NRF_TWIM_EVENT_LASTRX);

  

}

  • Hi,

    Your logic looks correct to me. 

    Have you configured the GPIOs and pin config for the TWIM prior to this function? 

    What is I2C0_add defined as?

    Have you checked the TWIM registers in a debug session to verify that the correct values have been written before triggering the STARTTX task?

    Have you checked the TWI bus with a logic analyzer, to see if anything (address, data, etc) is output?

    Best regards,
    Jørgen

Related