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

Need an equivalent for an arduino code

hello everyone I am trying to implement a Library from Arduino to nordic but I have a problem : 

this is the Arduino code :

Wire.beginTransmission(dev_id);
  Wire.write(reg_addr);   // Accès au registre
  Wire.endTransmission(false);
  Wire.requestFrom(dev_id, len);
  if (len<=Wire.available()){
    for(int i=0;i<len;i++){
        data[i] = Wire.read();        
    }
  }else{
    result=5;
  }

I replace by that :

uint8_t reg[1]={reg_addr};
  nrf_drv_twi_tx(&m_twi, dev_id, reg, 1,false);
  nrf_drv_twi_rx(&m_twi, dev_id, data, len);

But I Don't know how to do the control : len <= Wire.available().

Thank you have a good day.

Arnaud

  • Hello Arnaud,

    a Library from Arduino

    From the code I have to assume you are talking about the "wire" library, but it is ambiguous.
    I am not familiar with this library, or what the specific function does, but it seems that .available is used to retrieve the number of received bytes from the TWI slave. Judging by the reference it seems to me that Arduino here divides the operation into first requesting the bytes with .requestFrom, and placing the received bytes in a register. Then, you may read from that register with the .read function, or see if the register holds anything with the .available function.
    I may have this incorrectly, if so please let me know.

    But I Don't know how to do the control : len <= Wire.available().

    If you take a look at the TWI Sensor example you would do both .requestFrom and .read at the same time by using nrf_drv_twi_rx ( or nrfx_twim_xfer if you are using the recommended nrfx driver ) with a buffer the size of "len". You would then just need to add a check to see how many bytes you actually received from the slave ( since it may send fewer than requested ).

    Please let me know if anything still should be unclear.

    Thank you have a good day.

    Thank you, I hope you have a good day as well!

    Best regards,
    Karl

Related