Gurkan
Gurkan

Reputation: 13

I'm trying to use I2C with beaglebone but I cannot write more than 1 byte

I'm trying to control DAC5571 with Beaglebone Black. My kernel version is:

Linux beaglebone 4.14.108-ti-r137 #1stretch SMP PREEMPT Tue Aug 25 01:48:39 UTC 2020 armv7l GNU/Linux

I can partially the control the DAC IC. As you can see here, you need to send 3 bytes; Slave Address, CTRL/MSB and LSB. The IC recognizes Slave address bytes and CTRL/MSB. I can read and confirm the output of the output pin. But when I start increasing the voltage value slowly as Vout += 0.05, output increasing as 0.2, 0.4, 0.6, etc...

I've checked with my oscilloscope and I can confirm that the third byte is being transmitted as 0x00 no matter what its the actual value is.

Here is my source code:

int DAC5571::writeI2CDeviceByte(int value)
{

  cout << "Starting DAC5571 I2C sensor state write" << endl;

  char namebuf[MAX_BUS];
  snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", I2CBus);
  int file;

  if ((file = open(namebuf, O_RDWR)) < 0) {
        cout << "Failed to open DAC5571 Sensor on " << namebuf << " I2C Bus" << endl;
        return(1);
  }

  if (ioctl(file, I2C_SLAVE, I2CAddress) < 0) {
        cout << "I2C_SLAVE address " << I2CAddress << " failed..." << endl;
        return(2);
  }

  int buffer[2];

    buffer[0] = value>>8;
    buffer[1] = value & 0xFF;

    cout << "buffer [0] is " << buffer[0] << endl;
    cout << "buffer [1] is " << buffer[1] << endl;
  if (write(file, buffer, 2) != 2) {
      cout << "Failure to write values to I2C Device address." << endl;
      return(3);
  }
  close(file);
  cout << "Finished DAC5571 I2C sensor state write" << endl;
  return 0;
}

Here is the console output:

Starting DAC5571 I2C sensor state write
buffer [0] is 3
buffer [1] is 128
Finished DAC5571 I2C sensor state write

I've seen on my research there is a header file called as "i2c-core.h" but I could not include into my projects which has a block write function. Not sure if that would help in my situation.

Can anyone please help me to solve my issue about not being able to transmit LSB part of the data?

Thank you.

Upvotes: 1

Views: 236

Answers (1)

Nate Eldredge
Nate Eldredge

Reputation: 58132

    int buffer[2];
    buffer[0] = value>>8;
    buffer[1] = value & 0xFF;
    if ( write(file, buffer, 2) != 2) { ... }

The elements of buffer are of type int, which is 4 bytes long. So when you write buffer with a length of 2, you write 2 bytes, which are the first two bytes of the integer buffer[0]. In your example buffer[0] is 3, so since this machine is little-endian, it consists of the bytes 03 00 00 00 and you write out 03 00.

You probably want unsigned char buffer[2]; or maybe uint8_t buffer[2];, so that each element is a byte.

Upvotes: 1

Related Questions