Pergunta

I want to use I2C C library for 'byte paradigm' I2C express device. This was sample API present in byte paradigm website.

//
// Executing I2C write transfer    
//
pDataWr[0] = 0x12;
pDataWr[1] = 0x34;
printf("\nWriting 0x%02X to address 0x%02x...\n", pDataWr[1], pDataWr[0]);
i2c_CmdBufAppend(2, 0x50, 1, 2, (char *)pDataWr, true, true); // Cmd = 2       : I2C write
                                                             // Address = 0x50
                                                             // AddrType = 1  : 7-bit address
                                                             // Length = 2    : 2 byte payload
                                                             // pDataWr       : buffer with 2 bytes payload (0x1234)
                                                             // STA = true    : generate start
                                                             // STO = true    : generate stop

i2c_RunMaster();
i2c_CmdBufDeleteAll();

What does this mean? What and Where its writing? How it differs from the API given below?

//
// Executing dummy write to set EEPROM address
//
printf("\nExecuting dummy write to set EEPROM address...\n");
pDataWr[0] = 0x12;
i2c_CmdBufAppend(2, 0x50, 1, 1, (char *)pDataWr, true, true); // Cmd = 2       : I2C write
                                                             // Address = 0x50
                                                             // AddrType = 1  : 7-bit address
                                                             // Length = 1    : 1 byte payload
                                                             // pDataWr       : buffer with 1 byte payload (0x12)
                                                             // STA = true    : generate start
                                                             // STO = true    : generate stop
i2c_RunMaster();
i2c_CmdBufDeleteAll();

I have gone through the byte paradigm library documentation, but information is very limited there. Kindly help, thanks in advance :)

Foi útil?

Solução

A quick google search for I2C specification resulted in this: http://i2c2p.twibright.com/spec/i2c.pdf

It's been a long time since I've played with i2c devices (I had to write my own driver for a specialized chip-set).

The chip that is executing the code is the I2C master.

i2c_CmdBufAppend(2, 0x50, 1, 2, (char *)pDataWr, true, true); // Cmd = 2       : I2C write
                                                         // Address = 0x50
                                                         // AddrType = 1  : 7-bit address
                                                         // Length = 2    : 2 byte payload
                                                         // pDataWr       : buffer with 2 bytes payload (0x1234)
                                                         // STA = true    : generate start
                                                         // STO = true    : generate stop

I don't know anything about the library you are using but the comments fully explain that you are addressing the device at address 0x50 (using 7 bit addressing) and are writing 2 bytes of data, and also generating a Start and Stop condition.

You need to refer to the I2C specification and EEPROM that you are interacting with.

It's really not difficult once you understand the specification. Which by the way has full of examples of reading / writing data to I2C devices.

Some devices are set up that you have to write a special value to them (such as a register or command value) and then read back data. This is setup by transmitting a write command and then a read command without a STOP between commands.

The device you are interacting with should have the details on special commands and such. The library I2C library WON'T have these details.

Outras dicas

To add to Freds post

The I2C spec is going to give you the physical layer, the start and stop patterns, where the ack lands and who drives what on the wire.

To understand the protocol that rides on that you usually have to look at the documentation for the device itself (which often also includes drawings of the physical layer).

To understand someone elses I2C code you need the documentation for the device in front of you. To write your own I2C code you need the documentation for the device in front of you. If the device is something generic like an eeprom for example that a number of vendors make compatible parts it is probably a good idea to have the documentation for several of the devices in front of you in case there are some vendor specific things that you want to support or avoid.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top