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

Problem in twi/i2c communication receiving data

Hi
I'm trying to communicate between nrf51-DK and arduino using twi. I was able to send data from nrf51-DK to arduino using nrf_drv_twi_tx but when I'm trying to send the data from arduino to DK, I'm unable to receive the data. Debugging on arduino side, I'm able to see that the data is being requested on the twi bus and arduino is sending it, but that data is not being displayed on DK side. I'm using the following code to receive the data.

uint32_t err_code;
nrf_drv_twi_config_t p_twi_config = NRF_DRV_TWI_DEFAULT_CONFIG(1);

err_code = nrf_drv_twi_init(&p_twi, &p_twi_config, twi_event_handler);
APP_ERROR_CHECK(err_code);

nrf_drv_twi_enable(&p_twi);

uint8_t tx_data[1] = {0x0D};
uint8_t* rx_data;
while(1)
{    
	err_code = nrf_drv_twi_rx(&p_twi, 0x08, rx_data, 1, false);
	APP_ERROR_CHECK(err_code);
	
	SEGGER_RTT_printf(0, "rx_data is %x\r\n", rx_data[0]);
}

While debugging in Keil on DK side, I can see that EVENTS_RXDREADY is being set but there is no option to see the RXD buffer in the Keil debug window.


One odd thing is that I am using TI's TM75 temp sensor which uses twi and I'm able to read and write without any problem whereas in case of arduino i'm receiving only 0. Also I've tried different pullups with no success. Where can I see the RXD buffer in Keil debug window? How can I view the err_code variale, debug window says "not in scope"? Also any ideas what I might be doing wrong.


Here is the arduino code for reference
bool state = 0;
#include <Wire.h>

void setup()
{
Serial.begin(9600);
Wire.begin(0x08);                // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
pinMode(8, OUTPUT);
}

void loop()
{
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  Wire.write("abcd"); // respond with message of 6 bytes
  digitalWrite(8, state);
  state = !state;
  // as expected by master
}

For those who don't know what arduino is, it is a board based on atmega328, this code is given as an example code with twi library, so I' think that the code should be working without fail.


  • The array is not allocated, change to

    uint8_t rx_data[1];
    
    err_code = nrf_drv_twi_rx(&p_twi, 0x08, rx_data, sizeof(rx_data), false);
    

    To get all bytes you should make the array of size 4.

    Usually you can see all registers in View->System viewer->TWI->TWI0, but I see that the RXD register is not there. I do not know quite why. To see the variables, try to turn down the optimization in project->options for target->c/c++ tab.

  • Got the code working, what a silly mistake i was doing, Thanks :). For the RXD register, I tried changing optimization to all the options and recompiled the project, but RXD is still not showing in the debug window. Also can you tell me about the process to view err_code, it says "out of scope" in debug window?

  • I guess the reason why you can not see RXD in TWI->TWI0 is that to show it to you in debug window Keil schould really read it from the chip, but for this there is no matter who is reading it's registers, it will behave as if YOUR CODE has read the register and react correspondingly. You can still see the registers in View->Memory Window->Window# (you know the address), but don't be surprized by this "strange" behaviour.

Related