Unmanned Player
Unmanned Player

Reputation: 1209

why does i2cset send extra bytes?

I've been working on PIC18F55K42 chip for a while. The PIC is setup as a slave and it's receiving bytes correctly. But I encountered a few problems.

For example, when I do:

i2cset -y 1 0x54 0x80 0x01

It looks correct on the controller side and I can see the address 0x80(data address) and byte value 0x01.

When I send in block mode like:

i2cset -y 1 0x54 0x80 0x01 0x02 0x03 0x04 i

I see spurious bytes appearing on the controller. More precisely, it looks like this:

ADDRESS 80 6c 00 2f 01 02 03 04 STOP

Salae Logic Analyser trace

At first I thought this is something to do with my controller and even tried digging into it's clock settings. Used Salae logic analyser too. There's nothing wrong with the controller or it's set up. The only place I can think of is the complex onion driver layering done by Linux.

I'd like to know why Linux is sending the 3 extra bytes (6c 00 2f). Why does i2c_smbus_write_block_data send extra bytes and how can it be avoided?

Upvotes: 4

Views: 2729

Answers (2)

Reid Lindsay
Reid Lindsay

Reputation: 56

It's a bug in the i2cset implementation in Busybox. See miscutils/i2c_tools.c:

    /* Prepare the value(s) to be written according to current mode. */
    switch (mode) {
    case I2C_SMBUS_BYTE_DATA:
        val = xstrtou_range(argv[3], 0, 0, 0xff);
        break;
    case I2C_SMBUS_WORD_DATA:
        val = xstrtou_range(argv[3], 0, 0, 0xffff);
        break;
    case I2C_SMBUS_BLOCK_DATA:
    case I2C_SMBUS_I2C_BLOCK_DATA:
        for (blen = 3; blen < (argc - 1); blen++)
            block[blen] = xstrtou_range(argv[blen], 0, 0, 0xff);
        val = -1;
        break;
    default:
        val = -1;
        break;
    }

Should be block[blen - 3] = xstrtou_range(argv[blen], 0, 0, 0xff);. The bug results in 3 extra garbage bytes from stack being sent.

Upvotes: 4

ReAl
ReAl

Reputation: 1301

Use i2c_smbus_write_i2c_block_data for raw i2c transfers

i2c_smbus_write_block_data makes data transfer using SMBUS protocol

Upvotes: 0

Related Questions