Private
Public Access
2
0

i2c: remove smbus helper library & clean up command use in API

This commit changes the mraa_i2c_read() API call behaviour, a register should
now always be set when using this call. The smbus helper library is no longer
required since the code is now all contained from i2c.c which avoids multiple
function calls

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-11-18 16:09:08 +00:00
parent e9de25d797
commit 19d7dad364
8 changed files with 146 additions and 546 deletions

View File

@@ -116,7 +116,8 @@ uint8_t mraa_i2c_read_byte_data(mraa_i2c_context dev, const uint8_t command);
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
* Write length bytes to the bus, the first byte in the array is the
* command/register to write
*
* @param dev The i2c context
* @param data pointer to the byte array to be written

View File

@@ -106,6 +106,7 @@ class I2c {
*
* @param data Buffer to write into
* @param length Size of read
* @param command The i2c command
* @return length of the read or 0 if failed
*/
uint8_t read(char *data, size_t length) {
@@ -116,13 +117,13 @@ class I2c {
* Read length bytes from the bus, and return as a std::string note
* that this is not a null terminated string
*
* @param length Size of read to make
* @param length Size of read in bytes to make
* @return pointer to std::string
*/
std::string read(size_t length) {
char* data = (char*) malloc(sizeof(char) * length);
mraa_i2c_read(m_i2c, (uint8_t*) data, (int) length);
std::string str(data, (int) length);
std::string read(int length, uint8_t command) {
uint8_t* data = (uint8_t*) malloc(sizeof(uint8_t) * length);
mraa_i2c_read(m_i2c, data, command, length);
std::string str((char*)data, length);
free(data);
return str;
}
@@ -144,7 +145,7 @@ class I2c {
* @return char read from register
*/
uint16_t readWordReg(uint8_t reg) {
return mraa_i2c_read_byte_data(m_i2c, reg);
return mraa_i2c_read_word_data(m_i2c, reg);
}
/**
@@ -158,9 +159,10 @@ class I2c {
}
/**
* Write length bytes to the bus
* Write length bytes to the bus, the first byte in the array is the
* command/register to write
*
* @param data Buffer to send on the bus
* @param data Buffer to send on the bus, first byte is i2c command
* @param length Size of buffer to send
* @return Result of operation
*/

View File

@@ -99,11 +99,24 @@ main(int argc, char **argv)
mraa_i2c_write(i2c, rx_tx_buf, 2);
for(;;) {
#if 0
int i = 0;
//alternative, equivalent method which helps to understand exactly what
//the below does
mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
for (i = 0; i < DATA_REG_SIZE; i++) {
mraa_i2c_read_byte_data(i2c, HMC5883L_DATA_REG+i);
}
#endif
// first 'select' the register we want to read from
mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
mraa_i2c_write_byte(i2c, HMC5883L_DATA_REG);
// then we read from that register incrementing with every read the
// chosen register
mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
mraa_i2c_read(i2c, rx_tx_buf, DATA_REG_SIZE);
// this call behaves very similarly to the Wire receive() call
mraa_i2c_read(i2c, rx_tx_buf, HMC5883L_DATA_REG, DATA_REG_SIZE);
x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_X_LSB_REG] ;
z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Z_LSB_REG] ;

View File

@@ -1,101 +0,0 @@
/*
* Author: Robin Knight (robin.knight@roadnarrows.com)
*
* Copyright © 2009 RoadNarrows LLC.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that
* (1) The above copyright notice and the following two paragraphs
* appear in all copies of the source code and (2) redistributions
* including binaries reproduces these notices in the supporting
* documentation. Substantial modifications to this software may be
* copyrighted by their authors and need not follow the licensing terms
* described here, provided that the new terms are clearly indicated in
* all files where they apply.
*
* IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
* OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
* PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
* DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
* EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
* "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include "linux/i2c-dev.h"
typedef union i2c_smbus_data_union
{
uint8_t byte; ///< data byte
unsigned short word; ///< data short word
uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
///< block[0] is used for length and one more for PEC
} i2c_smbus_data_t;
typedef struct i2c_smbus_ioctl_data_struct
{
uint8_t read_write; ///< operation direction
uint8_t command; ///< ioctl command
int size; ///< data size
i2c_smbus_data_t *data; ///< data
} i2c_smbus_ioctl_data_t;
// ---------------------------------------------------------------------------
// Prototypes
// ---------------------------------------------------------------------------
extern int i2c_smbus_access(int fd, uint8_t read_write, uint8_t command,
int size, i2c_smbus_data_t *data);
extern int i2c_smbus_write_quick(int fd, uint8_t value);
extern int i2c_smbus_read_byte(int fd);
extern int i2c_smbus_write_byte(int fd, uint8_t value);
extern int i2c_smbus_read_byte_data(int fd, uint8_t command);
extern int i2c_smbus_write_byte_data(int fd, uint8_t command, uint8_t value);
extern int i2c_smbus_read_word_data(int fd, uint8_t command);
extern int i2c_smbus_write_word_data(int fd, uint8_t command, unsigned short value);
extern int i2c_smbus_process_call(int fd, uint8_t command, unsigned short value);
extern int i2c_smbus_read_block_data(int fd, uint8_t command, uint8_t *values);
extern int i2c_smbus_write_block_data(int fd, uint8_t command, uint8_t length,
const uint8_t *values);
extern int i2c_smbus_read_i2c_block_data(int fd, uint8_t command,
uint8_t *values);
extern int i2c_smbus_write_i2c_block_data(int fd, uint8_t command, uint8_t length,
const uint8_t *values);
extern int i2c_smbus_block_process_call(int fd, uint8_t command, uint8_t length,
uint8_t *values);
#ifdef __cplusplus
}
#endif

View File

@@ -8,7 +8,6 @@ set (mraa_LIB_SRCS
${PROJECT_SOURCE_DIR}/src/mraa.c
${PROJECT_SOURCE_DIR}/src/gpio/gpio.c
${PROJECT_SOURCE_DIR}/src/i2c/i2c.c
${PROJECT_SOURCE_DIR}/src/i2c/smbus.c
${PROJECT_SOURCE_DIR}/src/pwm/pwm.c
${PROJECT_SOURCE_DIR}/src/spi/spi.c
${PROJECT_SOURCE_DIR}/src/aio/aio.c

22
src/i2c/LICENSE Normal file
View File

@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2005-2014 RoadNarrows LLC.
htpt://roadnarrows.com
All Rights Reserved
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -2,6 +2,9 @@
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Code is modified from the RoadNarrows-robotics i2c library (distributed under
* the MIT license, license is included verbatim under src/i2c/LICENSE)
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
@@ -23,9 +26,47 @@
*/
#include "i2c.h"
#include "smbus.h"
#include "mraa_internal.h"
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include "linux/i2c-dev.h"
typedef union i2c_smbus_data_union
{
uint8_t byte; ///< data byte
unsigned short word; ///< data short word
uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
///< block[0] is used for length and one more for PEC
} i2c_smbus_data_t;
typedef struct i2c_smbus_ioctl_data_struct
{
uint8_t read_write; ///< operation direction
uint8_t command; ///< ioctl command
int size; ///< data size
i2c_smbus_data_t *data; ///< data
} i2c_smbus_ioctl_data_t;
int
mraa_i2c_smbus_access(int fh, uint8_t read_write, uint8_t command, int size,
i2c_smbus_data_t *data)
{
i2c_smbus_ioctl_data_t args;
args.read_write = read_write;
args.command = command;
args.size = size;
args.data = data;
return ioctl(fh, I2C_SMBUS, &args);
}
mraa_i2c_context
mraa_i2c_init(int bus)
{
@@ -114,36 +155,69 @@ mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
uint8_t
mraa_i2c_read_byte(mraa_i2c_context dev)
{
uint8_t byte = i2c_smbus_read_byte(dev->fh);
return byte;
i2c_smbus_data_t d;
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, I2C_NOCMD,
I2C_SMBUS_BYTE, &d) < 0) {
syslog(LOG_ERR, "i2c: Failed to write");
return 0;
}
return 0x0FF & d.byte;
}
uint8_t
mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
{
return (uint8_t) i2c_smbus_read_byte_data(dev->fh, command);
i2c_smbus_data_t d;
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command,
I2C_SMBUS_BYTE_DATA, &d) < 0) {
syslog(LOG_ERR, "i2c: Failed to write");
return 0;
}
return 0x0FF & d.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);
i2c_smbus_data_t d;
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command,
I2C_SMBUS_WORD_DATA, &d) < 0) {
syslog(LOG_ERR, "i2c: Failed to write");
return 0;
}
return 0xFFFF & d.byte;
}
mraa_result_t
mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
{
if (i2c_smbus_write_i2c_block_data(dev->fh, data[0], length-1, (uint8_t*) data+1) < 0) {
syslog(LOG_ERR, "i2c: Failed to write");
return MRAA_ERROR_INVALID_HANDLE;
i2c_smbus_data_t d;
int i;
uint8_t command = data[0];
data = &data[1];
length = length-1;
if (length > I2C_SMBUS_I2C_BLOCK_MAX) {
length = I2C_SMBUS_I2C_BLOCK_MAX;
}
return MRAA_SUCCESS;
for (i=1; i<=length; i++) {
d.block[i] = data[i-1];
}
d.block[0] = length;
return mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
I2C_SMBUS_I2C_BLOCK_DATA, &d);
}
mraa_result_t
mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
{
if (i2c_smbus_write_byte(dev->fh, data) < 0) {
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, data,
I2C_SMBUS_BYTE, NULL) < 0) {
syslog(LOG_ERR, "i2c: Failed to write");
return MRAA_ERROR_INVALID_HANDLE;
}
@@ -151,9 +225,14 @@ mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
}
mraa_result_t
mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
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) {
i2c_smbus_data_t d;
d.byte = data;
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
I2C_SMBUS_BYTE_DATA, &d) < 0) {
syslog(LOG_ERR, "i2c: Failed to write");
return MRAA_ERROR_INVALID_HANDLE;
}
@@ -161,9 +240,14 @@ mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t
}
mraa_result_t
mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
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) {
i2c_smbus_data_t d;
d.word = data;
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
I2C_SMBUS_WORD_DATA, &d) < 0) {
syslog(LOG_ERR, "i2c: Failed to write");
return MRAA_ERROR_INVALID_HANDLE;
}

View File

@@ -1,420 +0,0 @@
// Library: libi2c API
// File: smbus.c
/*! \file
*
* $LastChangedDate: 2009-09-09 09:44:12 -0600 (Wed, 09 Sep 2009) $
* $Rev: 130 $
*
* \brief System Management Bus (SMBus) over I<sup>2</sup>C communication
* interface.
*
* \author Robin Knight (robin.knight@roadnarrows.com)
*
* \par Copyright:
* (C) 2009. RoadNarrows LLC.
* (http://www.roadnarrows.com) \n
* All Rights Reserved
*
* <hr>
* \par Original Source and Copyright:
* See i2c-dev.h.
*
* <hr>
*/
// Permission is hereby granted, without written agreement and without
// license or royalty fees, to use, copy, modify, and distribute this
// software and its documentation for any purpose, provided that
// (1) The above copyright notice and the following two paragraphs
// appear in all copies of the source code and (2) redistributions
// including binaries reproduces these notices in the supporting
// documentation. Substantial modifications to this software may be
// copyrighted by their authors and need not follow the licensing terms
// described here, provided that the new terms are clearly indicated in
// all files where they apply.
//
// IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
// OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
// PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
// EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
// "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
// PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
//
////////////////////////////////////////////////////////////////////////////////
#include "smbus.h"
#ifdef __cplusplus
extern "C" {
#endif
/*!
* \brief Execute an SMBus IOCTL.
*
* \param fd File descriptor to opened SMBus device.
* \param read_write Operation access type.
* \param command Operation command or immediate data.
* \param size Data size.
* \param [in,out] data Read/write/control data
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_access(int fd, uint8_t read_write, uint8_t command,
int size, i2c_smbus_data_t *data)
{
i2c_smbus_ioctl_data_t args;
args.read_write = read_write;
args.command = command;
args.size = size;
args.data = data;
return ioctl(fd, I2C_SMBUS, &args);
}
/*!
* \brief Write a quick value to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param value Value to write
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_quick(int fd, uint8_t value)
{
return i2c_smbus_access(fd, value, I2C_NOCMD, I2C_SMBUS_QUICK, NULL);
}
/*!
* \brief Read an immediate byte from the SMBus.
*
* \param fd File descriptor to opened SMBus device.
*
* \return
* Returns read byte on on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_read_byte(int fd)
{
i2c_smbus_data_t data;
int rc;
rc = i2c_smbus_access(fd, I2C_SMBUS_READ, I2C_NOCMD, I2C_SMBUS_BYTE, &data);
return rc>=0? 0x0FF & data.byte: -1;
}
/*!
* \brief Write an immediate byte to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param value Byte value to write.
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_byte(int fd, uint8_t value)
{
return i2c_smbus_access(fd, I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
}
/*!
* \brief Read a data byte from the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
*
* \return
* Returns read byte on on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_read_byte_data(int fd, uint8_t command)
{
i2c_smbus_data_t data;
int rc;
rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_BYTE_DATA,
&data);
return rc>=0? 0x0FF & data.byte: -1;
}
/*!
* \brief Write a data byte to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param value Byte value to write.
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_byte_data(int fd, uint8_t command, uint8_t value)
{
i2c_smbus_data_t data;
data.byte = value;
return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
I2C_SMBUS_BYTE_DATA, &data);
}
/*!
* \brief Read a data 2-byte word from the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
*
* \return
* Returns read 2-byte word on on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_read_word_data(int fd, uint8_t command)
{
i2c_smbus_data_t data;
int rc;
rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_WORD_DATA,
&data);
return rc>=0? 0x0FFFF & data.word: -1;
}
/*!
* \brief Write a data 2-byte word to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param value Word value to write.
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_word_data(int fd, uint8_t command, unsigned short value)
{
i2c_smbus_data_t data;
data.word = value;
return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
I2C_SMBUS_WORD_DATA, &data);
}
/*!
* \brief Issue a 2-byte word process call (write/read) to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param value Word value to write.
*
* \return
* Returns read 2-byte word on on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_process_call(int fd, uint8_t command, unsigned short value)
{
i2c_smbus_data_t data;
int rc;
data.word = value;
rc = i2c_smbus_access(fd, I2C_SMBUS_WRITE, command, I2C_SMBUS_PROC_CALL,
&data);
return rc>=0? 0x0FFFF & data.word: -1;
}
/*!
* \brief Read a block of data from the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param [out] values Buffer to hold the block of read byte values.\n
* Must be large enough to receive the data.
*
* \return
* On success, returns \h_ge 0 the number of bytes read, excluding any header
* fields. Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_read_block_data(int fd, uint8_t command, uint8_t *values)
{
i2c_smbus_data_t data;
int i;
int rc;
rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_BLOCK_DATA,
&data);
if( rc >= 0 )
{
for(i=1; i<=data.block[0]; ++i)
{
values[i-1] = data.block[i];
}
rc = data.block[0];
}
return rc;
}
/*!
* \brief Write a data block to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param length Length of buffer (bytes) to write.
* \param [in] values Buffer of data to write.
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_block_data(int fd, uint8_t command, uint8_t length,
const uint8_t *values)
{
i2c_smbus_data_t data;
int i;
if( length > I2C_SMBUS_BLOCK_MAX )
{
length = I2C_SMBUS_BLOCK_MAX;
}
for(i=1; i<=length; i++)
{
data.block[i] = values[i-1];
}
data.block[0] = length;
return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
I2C_SMBUS_BLOCK_DATA, &data);
}
/*!
* \brief Read a block of data from the SMBus via low-level I<sup>2</sup>C.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param [out] values Buffer to hold the block of read byte values.\n
* Must be large enough to receive the data.
*
* \return
* On success, returns \h_ge 0 the number of bytes read, excluding any header
* fields. Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_read_i2c_block_data(int fd, uint8_t command, uint8_t *values)
{
i2c_smbus_data_t data;
int i;
int rc;
rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_I2C_BLOCK_DATA,
&data);
if( rc >= 0 )
{
for(i=1; i<=data.block[0]; i++)
{
values[i-1] = data.block[i];
}
rc = data.block[0];
}
return rc;
}
/*!
* \brief Write a block of data to the SMBus via low-level I<sup>2</sup>C.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param length Length of buffer (bytes) to write.
* \param [in] values Buffer of data to write.
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_i2c_block_data(int fd, uint8_t command, uint8_t length,
const uint8_t *values)
{
i2c_smbus_data_t data;
int i;
if( length > I2C_SMBUS_I2C_BLOCK_MAX )
{
length = I2C_SMBUS_I2C_BLOCK_MAX;
}
for(i=1; i<=length; i++)
{
data.block[i] = values[i-1];
}
data.block[0] = length;
return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
I2C_SMBUS_I2C_BLOCK_DATA, &data);
}
/*!
* \brief Issue a block process call (write/read) to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param length Length of buffer (bytes) to write.
* \param [in,out] values Buffer of data to write and to hold the block of
* read byte values.\n
* Must be large enough to receive the data.
*
* \return
* On success, returns \h_ge 0 the number of bytes read, excluding any header
* fields. Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_block_process_call(int fd, uint8_t command, uint8_t length,
uint8_t *values)
{
i2c_smbus_data_t data;
int i;
int rc;
if( length > I2C_SMBUS_BLOCK_MAX )
{
length = I2C_SMBUS_BLOCK_MAX;
}
for(i=1; i<=length; i++)
{
data.block[i] = values[i-1];
}
data.block[0] = length;
rc = i2c_smbus_access(fd, I2C_SMBUS_WRITE, command, I2C_SMBUS_BLOCK_PROC_CALL,
&data);
if( rc >= 0 )
{
for(i=1; i<=data.block[0]; i++)
{
values[i-1] = data.block[i];
}
rc = data.block[0];
}
return rc;
}
#ifdef __cplusplus
}
#endif