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

How to access to the slave address and to manage successive nrf_drv_twi_tx() ?

Hello everyone,

I have a program which set configurations by using write (nrf_drv_twi_tx) in the registers of a sensor. I use the SDK 8 in the nRF51.

Here is the code:

// *************************************** Temperature cfg *************************************** //
// Write cfg to reg 0x06
for(slave_address=0x76; slave_address < 0x78; slave_address++) 
{				
ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, &temp_reg, 1, true);
nrf_delay_ms(200);
                ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, temp, sizeof(temp), false);
nrf_delay_ms(200);
 }	
	
// ****************************************** Pressure cfg *************************************** //
// Write cfg to reg 0x07
for(slave_address=0x76; slave_address < 0x78; slave_address++) 
{				
ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, &press_reg, 1, true);
nrf_delay_ms(200);
                ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, press, sizeof(press), false);
nrf_delay_ms(200);		
 }	
	
// ***************************************** Interrupt and FIFO ********************************** //
// Write cfg to reg 0x09
for(slave_address=0x76; slave_address < 0x78; slave_address++) 
{				
ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, &interr_reg, 1, true);
nrf_delay_ms(200);
                ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, interr, sizeof(interr), false);
nrf_delay_ms(200);	
}

Could you help me to respond to those questions and solve the problem please?

  1. The sensor (slave) address is 0x77. When I use it directly, I get an ERROR event. Otherwise, I have to use the “for” loop to get a TXSENT event. Could you explain me please?

  2. Can functionalities of debugger be affected by the program as 2 successive writes? With the debugger I can see that until the end of the first write, everything works properly. But when I go on to the second write, it seems like the debugger no longer reacts. However, when I begin to put a breakpoint at the end of the 2nd write, the debugger answers and shows that it works well.

Maybe the 2 subjects are linked, maybe not. In any case, thank you in advance for your answers.


Hello,

Let me give more details about what I have done step by step.

  1. As I said, the slave address is 0x77. At the beginning, I tried to transmit the byte 0x05 to the register 0x08 of the slave. Here is the code :

    uint8_t slave_address = 0x77; uint8_t register_address = 0x08; uint8_t data = 0x05;

    ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, &register_address, 1, true); nrf_delay_ms(1);
    ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, &data, 1, false); nrf_delay_ms(1);

But it does not work. Here is what I observed with the logical analyzer: image description

The first call to the slave delivers a NACK (non-acknoledge) at the 9th clock pulse.

  1. Then I noticed that there were times, passing by the slave address 0x76 solved the problem.

Here is the code :

uint8_t slave_address_0 = 0x76;   
uint8_t slave_address = 0x77;
uint8_t register_address = 0x08;
uint8_t data = 0x05; 
    
ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address_0, &register_address, 1, true);
nrf_delay_ms(1);    
ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address_0, &data, 1, false);
nrf_delay_ms(1);
                 
ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, &register_address, 1, true);
nrf_delay_ms(1);    
ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, &data, 1, false);
nrf_delay_ms(1);

The third call to the slave delivered an ACK at the 9th clock pulse and it was directly followed by the fourth call to the slave which transmitted the register address 0x08. That seems like it is necessary to wake up the slave with the address 0x76 before calling it with the address 0x77.

  1. Finally, I tried to follow the Phillips I²C bus specification using a START byte to help synchronizing those chipset. I have succeeded to produce the same chronogram as below which is in the specification: image description

with this code :

uint8_t start_byte = 0x00;
uint8_t slave_address = 0x77;
uint8_t register_address = 0x08;
uint8_t data = 0x05; 
             
ret_code = nrf_drv_twi_rx(&p_twi_instance, start_byte, &start_byte, 1, false);
nrf_delay_ms(1); 
ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, &register_address, 1, true);
nrf_delay_ms(1);    
ret_code = nrf_drv_twi_tx(&p_twi_instance, slave_address, &data, 1, false);
nrf_delay_ms(1);

But it does not work yet, as the logical analyzer shows: image description

I come to you again to know if you could help me to find a solution please.

Thank you in advance, Best regards,

Parents
  • Could you post the code where you initialize the TWI driver, i.e. nrf_drv_twi_init?1

    1) You mean that you cannot call nrf_drv_twi_txthe following way

    nrf_drv_twi_tx(&p_twi_instance, 0x77, &press_reg, 1, true);
    

    but you have to call it like this

    uint8_t address = 0x77;
    nrf_drv_twi_tx(&p_twi_instance, address, &press_reg, 1, true);
    

    You could try to type cast it, i.e.

    nrf_drv_twi_tx(&p_twi_instance, (uint8_t)0x77, &press_reg, 1, true);
    

    2) If you have provided it with a event handler,i.e. that the driver is event-driven and not blocking, then halting and starting the nRF51 could interfere with the event generation and handling.

  • Thank you for your answer. Yes, I am using that but it does not solve my problem. Please look above, I edited the original post to give more details.

Reply Children
No Data
Related