i2c: add clean {write, read}data functions
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
@@ -78,7 +78,8 @@ mraa_i2c_context mraa_i2c_init_raw(unsigned int bus);
|
|||||||
mraa_result_t mraa_i2c_frequency(mraa_i2c_context dev, int hz);
|
mraa_result_t mraa_i2c_frequency(mraa_i2c_context dev, int hz);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read from an i2c context
|
* Simple bulk read from an i2c context, this will always begin with the i2c
|
||||||
|
* offset 0x0
|
||||||
*
|
*
|
||||||
* @param dev The i2c context
|
* @param dev The i2c context
|
||||||
* @param data pointer to the byte array to read data in to
|
* @param data pointer to the byte array to read data in to
|
||||||
@@ -88,7 +89,8 @@ mraa_result_t mraa_i2c_frequency(mraa_i2c_context dev, int hz);
|
|||||||
int mraa_i2c_read(mraa_i2c_context dev, uint8_t *data, int length);
|
int mraa_i2c_read(mraa_i2c_context dev, uint8_t *data, int length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a single byte from the i2c context
|
* Simple read for a single byte from the i2c context, this will always begin
|
||||||
|
* with the i2c offset 0x0
|
||||||
*
|
*
|
||||||
* @param dev The i2c context
|
* @param dev The i2c context
|
||||||
* @return The result of the read or -1 if failed
|
* @return The result of the read or -1 if failed
|
||||||
@@ -102,10 +104,19 @@ uint8_t mraa_i2c_read_byte(mraa_i2c_context dev);
|
|||||||
* @param command The register
|
* @param command The register
|
||||||
* @return The result of the read or -1 if failed
|
* @return The result of the read or -1 if failed
|
||||||
*/
|
*/
|
||||||
uint8_t mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command);
|
uint8_t mraa_i2c_read_byte_data(mraa_i2c_context dev, const uint8_t command);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write to an i2c context
|
* Read a single word from i2c context, from designated register
|
||||||
|
*
|
||||||
|
* @param dev The i2c context
|
||||||
|
* @param command The register
|
||||||
|
* @return The result of the read or -1 if failed
|
||||||
|
*/
|
||||||
|
uint16_t mraa_i2c_read_word_data(mraa_i2c_context dev, const uint8_t command);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform a simple write to an i2c context, always at offset 0x0
|
||||||
*
|
*
|
||||||
* @param dev The i2c context
|
* @param dev The i2c context
|
||||||
* @param data pointer to the byte array to be written
|
* @param data pointer to the byte array to be written
|
||||||
@@ -115,7 +126,7 @@ uint8_t mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command);
|
|||||||
mraa_result_t mraa_i2c_write(mraa_i2c_context dev, const uint8_t *data, int length);
|
mraa_result_t mraa_i2c_write(mraa_i2c_context dev, const uint8_t *data, int length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a single byte to an i2c context
|
* Write a single byte to an i2c context, always at offset 0x0
|
||||||
*
|
*
|
||||||
* @param dev The i2c context
|
* @param dev The i2c context
|
||||||
* @param data The byte to write
|
* @param data The byte to write
|
||||||
@@ -123,6 +134,26 @@ mraa_result_t mraa_i2c_write(mraa_i2c_context dev, const uint8_t *data, int leng
|
|||||||
*/
|
*/
|
||||||
mraa_result_t mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data);
|
mraa_result_t mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a single byte to an i2c context
|
||||||
|
*
|
||||||
|
* @param dev The i2c context
|
||||||
|
* @param data The byte to write
|
||||||
|
* @param command The register
|
||||||
|
* @return Result of operation
|
||||||
|
*/
|
||||||
|
mraa_result_t mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a single word to an i2c context
|
||||||
|
*
|
||||||
|
* @param dev The i2c context
|
||||||
|
* @param data The word to write
|
||||||
|
* @param command The register
|
||||||
|
* @return Result of operation
|
||||||
|
*/
|
||||||
|
mraa_result_t mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the i2c context address.
|
* Sets the i2c context address.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -128,18 +128,7 @@ class I2c {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write one byte to the bus
|
* Read byte from an i2c register
|
||||||
*
|
|
||||||
* @param data Buffer to send on the bus
|
|
||||||
* @param length Size of buffer to send
|
|
||||||
* @return Result of operation
|
|
||||||
*/
|
|
||||||
mraa_result_t write(char* data, size_t length) {
|
|
||||||
return mraa_i2c_write(m_i2c, (const unsigned char *)data, (int) length);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read an i2c register
|
|
||||||
*
|
*
|
||||||
* @param reg Register to read from
|
* @param reg Register to read from
|
||||||
* @return char read from register
|
* @return char read from register
|
||||||
@@ -148,6 +137,16 @@ class I2c {
|
|||||||
return mraa_i2c_read_byte_data(m_i2c, reg);
|
return mraa_i2c_read_byte_data(m_i2c, reg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read word from an i2c register
|
||||||
|
*
|
||||||
|
* @param reg Register to read from
|
||||||
|
* @return char read from register
|
||||||
|
*/
|
||||||
|
uint16_t readWordReg(uint8_t reg) {
|
||||||
|
return mraa_i2c_read_byte_data(m_i2c, reg);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a byte on the bus
|
* Write a byte on the bus
|
||||||
*
|
*
|
||||||
@@ -159,15 +158,36 @@ class I2c {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write to an i2c register
|
* Write length bytes to the bus
|
||||||
|
*
|
||||||
|
* @param data Buffer to send on the bus
|
||||||
|
* @param length Size of buffer to send
|
||||||
|
* @return Result of operation
|
||||||
|
*/
|
||||||
|
mraa_result_t write(char* data, size_t length) {
|
||||||
|
return mraa_i2c_write(m_i2c, (const unsigned char *)data, (int) length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a byte to an i2c register
|
||||||
*
|
*
|
||||||
* @param reg Register to write to
|
* @param reg Register to write to
|
||||||
* @param data Value to write to register
|
* @param data Value to write to register
|
||||||
* @return Result of operation
|
* @return Result of operation
|
||||||
*/
|
*/
|
||||||
mraa_result_t writeReg(uint8_t reg, uint8_t data) {
|
mraa_result_t writeReg(uint8_t reg, uint8_t data) {
|
||||||
const uint8_t buf[2] = {reg, data};
|
return mraa_i2c_write_byte_data(m_i2c, data, reg);
|
||||||
return mraa_i2c_write(m_i2c, buf, 2);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a word to an i2c register
|
||||||
|
*
|
||||||
|
* @param reg Register to write to
|
||||||
|
* @param data Value to write to register
|
||||||
|
* @return Result of operation
|
||||||
|
*/
|
||||||
|
mraa_result_t writeWordReg(uint8_t reg, uint16_t data) {
|
||||||
|
return mraa_i2c_write_word_data(m_i2c, data, reg);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
mraa_i2c_context m_i2c;
|
mraa_i2c_context m_i2c;
|
||||||
|
|||||||
@@ -121,8 +121,13 @@ mraa_i2c_read_byte(mraa_i2c_context dev)
|
|||||||
uint8_t
|
uint8_t
|
||||||
mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
|
mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
|
||||||
{
|
{
|
||||||
uint8_t byte = i2c_smbus_read_byte_data(dev->fh, command);
|
return (uint8_t) i2c_smbus_read_byte_data(dev->fh, command);
|
||||||
return byte;
|
}
|
||||||
|
|
||||||
|
uint16_t
|
||||||
|
mraa_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
|
||||||
|
{
|
||||||
|
return (uint16_t) i2c_smbus_read_word_data(dev->fh, command);
|
||||||
}
|
}
|
||||||
|
|
||||||
mraa_result_t
|
mraa_result_t
|
||||||
@@ -145,6 +150,26 @@ mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
|
|||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
|
||||||
|
{
|
||||||
|
if (i2c_smbus_write_byte_data(dev->fh, command, data) < 0) {
|
||||||
|
syslog(LOG_ERR, "i2c: Failed to write");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
|
||||||
|
{
|
||||||
|
if (i2c_smbus_write_word_data(dev->fh, command, data) < 0) {
|
||||||
|
syslog(LOG_ERR, "i2c: Failed to write");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_i2c_address(mraa_i2c_context dev, uint8_t addr)
|
mraa_i2c_address(mraa_i2c_context dev, uint8_t addr)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user