vsergeev / lua-periphery

A Lua library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
MIT License
186 stars 39 forks source link

Can i2c support write operation ? #14

Closed ghost closed 7 years ago

ghost commented 7 years ago

Hello

I would like to use this library to handle eeprom with Lua. I would like to know how to write data into eeprom via Lua api ? I did not see any write operation I2C Message Flags. Thanks

vsergeev commented 7 years ago

The default behavior for a message in an I2C transaction is writing. A read message is specified with the I2C.I2C_M_RD flag. See this example https://github.com/vsergeev/lua-periphery/blob/master/docs/i2c.md#example:

--- Read byte at address 0x100 of EEPROM at 0x50
local msgs = { {0x01, 0x00}, {0x00, flags = I2C.I2C_M_RD} }
i2c:transfer(0x50, msgs)
print(string.format("0x100: 0x%02x", msgs[2][1]))

The first message is writing 2 bytes, the second message is writing 1 byte, within one transaction. These are the actual I2C transfers.

To write/read an EEPROM, you'll have to implement the EEPROM's protocol in your code, and then you can expose it as read/write operations on the EEPROM.