i2c  1.4.2
RoadNarrows Robotics I2C Package
smbus.c File Reference

System Management Bus (SMBus) over I2C communication interface. More...

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include "rnr/rnrconfig.h"
#include "rnr/i2c-dev.h"
#include "rnr/smbus.h"

Go to the source code of this file.

Functions

int i2c_smbus_access (int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
 Execute an SMBus IOCTL. More...
 
int i2c_smbus_write_quick (int fd, byte_t value)
 Write a quick value to the SMBus. More...
 
int i2c_smbus_read_byte (int fd)
 Read an immediate byte from the SMBus. More...
 
int i2c_smbus_write_byte (int fd, byte_t value)
 Write an immediate byte to the SMBus. More...
 
int i2c_smbus_read_byte_data (int fd, byte_t command)
 Read a data byte from the SMBus. More...
 
int i2c_smbus_write_byte_data (int fd, byte_t command, byte_t value)
 Write a data byte to the SMBus. More...
 
int i2c_smbus_read_word_data (int fd, byte_t command)
 Read a data 2-byte word from the SMBus. More...
 
int i2c_smbus_write_word_data (int fd, byte_t command, ushort_t value)
 Write a data 2-byte word to the SMBus. More...
 
int i2c_smbus_process_call (int fd, byte_t command, ushort_t value)
 Issue a 2-byte word process call (write/read) to the SMBus. More...
 
int i2c_smbus_read_block_data (int fd, byte_t command, byte_t *values)
 Read a block of data from the SMBus. More...
 
int i2c_smbus_write_block_data (int fd, byte_t command, byte_t length, const byte_t *values)
 Write a data block to the SMBus. More...
 
int i2c_smbus_read_i2c_block_data (int fd, byte_t command, byte_t *values)
 Read a block of data from the SMBus via low-level I2C. More...
 
int i2c_smbus_write_i2c_block_data (int fd, byte_t command, byte_t length, const byte_t *values)
 Write a block of data to the SMBus via low-level I2C. More...
 
int i2c_smbus_block_process_call (int fd, byte_t command, byte_t length, byte_t *values)
 Issue a block process call (write/read) to the SMBus. More...
 

Detailed Description

System Management Bus (SMBus) over I2C communication interface.

LastChangedDate
2009-09-09 09:44:12 -0600 (Wed, 09 Sep 2009)
Rev
130
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Original Source and Copyright:
See i2c-dev.h.

Definition in file smbus.c.

Function Documentation

int i2c_smbus_access ( int  fd,
byte_t  read_write,
byte_t  command,
int  size,
i2c_smbus_data_t data 
)

Execute an SMBus IOCTL.

Parameters
fdFile descriptor to opened SMBus device.
read_writeOperation access type.
commandOperation command or immediate data.
sizeData size.
[in,out]dataRead/write/control data
Returns
Returns ≥ 0 on success. Else errno is set appropriately and -1 is returned.

Definition at line 81 of file smbus.c.

References i2c_smbus_ioctl_data_struct::command, i2c_smbus_ioctl_data_struct::data, I2C_SMBUS, i2c_smbus_ioctl_data_struct::read_write, and i2c_smbus_ioctl_data_struct::size.

Referenced by i2c_smbus_block_process_call(), i2c_smbus_process_call(), i2c_smbus_read_block_data(), i2c_smbus_read_byte(), i2c_smbus_read_byte_data(), i2c_smbus_read_i2c_block_data(), i2c_smbus_read_word_data(), i2c_smbus_write_block_data(), i2c_smbus_write_byte(), i2c_smbus_write_byte_data(), i2c_smbus_write_i2c_block_data(), i2c_smbus_write_quick(), and i2c_smbus_write_word_data().

83 {
85 
86  args.read_write = read_write;
87  args.command = command;
88  args.size = size;
89  args.data = data;
90 
91  return ioctl(fd, I2C_SMBUS, &args);
92 }
I2C SMBus IOCTL Call Structure.
Definition: i2c-dev.h:239
#define I2C_SMBUS
SMBus-level access.
Definition: i2c-dev.h:231
byte_t read_write
operation direction
Definition: i2c-dev.h:241
i2c_smbus_data_t * data
data
Definition: i2c-dev.h:244
byte_t command
ioctl command
Definition: i2c-dev.h:242
int i2c_smbus_block_process_call ( int  fd,
byte_t  command,
byte_t  length,
byte_t *  values 
)

Issue a block process call (write/read) to the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
commandCommand to SMBus device.
lengthLength of buffer (bytes) to write.
[in,out]valuesBuffer of data to write and to hold the block of read byte values.
Must be large enough to receive the data.
Returns
On success, returns ≥ 0 the number of bytes read, excluding any header fields. Else errno is set appropriately and -1 is returned.

Definition at line 396 of file smbus.c.

References i2c_smbus_data_union::block, i2c_smbus_access(), I2C_SMBUS_BLOCK_MAX, I2C_SMBUS_BLOCK_PROC_CALL, and I2C_SMBUS_WRITE.

398 {
399  i2c_smbus_data_t data;
400  int i;
401  int rc;
402 
403  if( length > I2C_SMBUS_BLOCK_MAX )
404  {
405  length = I2C_SMBUS_BLOCK_MAX;
406  }
407 
408  for(i=1; i<=length; i++)
409  {
410  data.block[i] = values[i-1];
411  }
412  data.block[0] = length;
413 
415  &data);
416 
417  if( rc >= 0 )
418  {
419  for(i=1; i<=data.block[0]; i++)
420  {
421  values[i-1] = data.block[i];
422  }
423  rc = data.block[0];
424  }
425 
426  return rc;
427 }
byte_t block[32+2]
block[0] is used for length and one more for PEC
Definition: i2c-dev.h:188
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
#define I2C_SMBUS_BLOCK_MAX
As specified in SMBus standard.
Definition: i2c-dev.h:178
#define I2C_SMBUS_BLOCK_PROC_CALL
SMBus 2.0: issue block process call.
Definition: i2c-dev.h:210
#define I2C_SMBUS_WRITE
write
Definition: i2c-dev.h:196
int i2c_smbus_process_call ( int  fd,
byte_t  command,
ushort_t  value 
)

Issue a 2-byte word process call (write/read) to the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
commandCommand to SMBus device.
valueWord value to write.
Returns
Returns read 2-byte word on on success. Else errno is set appropriately and -1 is returned.

Definition at line 238 of file smbus.c.

References i2c_smbus_access(), I2C_SMBUS_PROC_CALL, I2C_SMBUS_WRITE, and i2c_smbus_data_union::word.

239 {
240  i2c_smbus_data_t data;
241  int rc;
242 
243  data.word = value;
244 
246  &data);
247 
248  return rc>=0? 0x0FFFF & data.word: -1;
249 }
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
ushort_t word
data short word
Definition: i2c-dev.h:187
#define I2C_SMBUS_PROC_CALL
issue word process call
Definition: i2c-dev.h:207
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
#define I2C_SMBUS_WRITE
write
Definition: i2c-dev.h:196
int i2c_smbus_read_block_data ( int  fd,
byte_t  command,
byte_t *  values 
)

Read a block of data from the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
commandCommand to SMBus device.
[out]valuesBuffer to hold the block of read byte values.
Must be large enough to receive the data.
Returns
On success, returns ≥ 0 the number of bytes read, excluding any header fields. Else errno is set appropriately and -1 is returned.

Definition at line 264 of file smbus.c.

References i2c_smbus_data_union::block, i2c_smbus_access(), I2C_SMBUS_BLOCK_DATA, and I2C_SMBUS_READ.

265 {
266  i2c_smbus_data_t data;
267  int i;
268  int rc;
269 
271  &data);
272 
273  if( rc >= 0 )
274  {
275  for(i=1; i<=data.block[0]; ++i)
276  {
277  values[i-1] = data.block[i];
278  }
279  rc = data.block[0];
280  }
281 
282  return rc;
283 }
byte_t block[32+2]
block[0] is used for length and one more for PEC
Definition: i2c-dev.h:188
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_READ
read
Definition: i2c-dev.h:195
#define I2C_SMBUS_BLOCK_DATA
data block r/w operation
Definition: i2c-dev.h:208
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
int i2c_smbus_read_byte ( int  fd)

Read an immediate byte from the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
Returns
Returns read byte on on success. Else errno is set appropriately and -1 is returned.

Definition at line 118 of file smbus.c.

References i2c_smbus_data_union::byte, I2C_NOCMD, i2c_smbus_access(), I2C_SMBUS_BYTE, and I2C_SMBUS_READ.

119 {
120  i2c_smbus_data_t data;
121  int rc;
122 
124 
125  return rc>=0? 0x0FF & data.byte: -1;
126 }
#define I2C_NOCMD
no command
Definition: i2c-dev.h:220
byte_t byte
data byte
Definition: i2c-dev.h:186
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_READ
read
Definition: i2c-dev.h:195
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
#define I2C_SMBUS_BYTE
immediate r/w byte operation
Definition: i2c-dev.h:204
int i2c_smbus_read_byte_data ( int  fd,
byte_t  command 
)

Read a data byte from the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
commandCommand to SMBus device.
Returns
Returns read byte on on success. Else errno is set appropriately and -1 is returned.

Definition at line 153 of file smbus.c.

References i2c_smbus_data_union::byte, i2c_smbus_access(), I2C_SMBUS_BYTE_DATA, and I2C_SMBUS_READ.

154 {
155  i2c_smbus_data_t data;
156  int rc;
157 
159  &data);
160 
161  return rc>=0? 0x0FF & data.byte: -1;
162 }
byte_t byte
data byte
Definition: i2c-dev.h:186
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_READ
read
Definition: i2c-dev.h:195
#define I2C_SMBUS_BYTE_DATA
data byte r/w operation
Definition: i2c-dev.h:205
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
int i2c_smbus_read_i2c_block_data ( int  fd,
byte_t  command,
byte_t *  values 
)

Read a block of data from the SMBus via low-level I2C.

Parameters
fdFile descriptor to opened SMBus device.
commandCommand to SMBus device.
[out]valuesBuffer to hold the block of read byte values.
Must be large enough to receive the data.
Returns
On success, returns ≥ 0 the number of bytes read, excluding any header fields. Else errno is set appropriately and -1 is returned.

Definition at line 330 of file smbus.c.

References i2c_smbus_data_union::block, i2c_smbus_access(), I2C_SMBUS_I2C_BLOCK_DATA, and I2C_SMBUS_READ.

331 {
332  i2c_smbus_data_t data;
333  int i;
334  int rc;
335 
337  &data);
338  if( rc >= 0 )
339  {
340  for(i=1; i<=data.block[0]; i++)
341  {
342  values[i-1] = data.block[i];
343  }
344  rc = data.block[0];
345  }
346  return rc;
347 }
byte_t block[32+2]
block[0] is used for length and one more for PEC
Definition: i2c-dev.h:188
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_READ
read
Definition: i2c-dev.h:195
#define I2C_SMBUS_I2C_BLOCK_DATA
i2c format block r/w/ operation
Definition: i2c-dev.h:209
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
int i2c_smbus_read_word_data ( int  fd,
byte_t  command 
)

Read a data 2-byte word from the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
commandCommand to SMBus device.
Returns
Returns read 2-byte word on on success. Else errno is set appropriately and -1 is returned.

Definition at line 195 of file smbus.c.

References i2c_smbus_access(), I2C_SMBUS_READ, I2C_SMBUS_WORD_DATA, and i2c_smbus_data_union::word.

196 {
197  i2c_smbus_data_t data;
198  int rc;
199 
201  &data);
202 
203  return rc>=0? 0x0FFFF & data.word: -1;
204 }
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_READ
read
Definition: i2c-dev.h:195
#define I2C_SMBUS_WORD_DATA
data word r/w operation
Definition: i2c-dev.h:206
ushort_t word
data short word
Definition: i2c-dev.h:187
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
int i2c_smbus_write_block_data ( int  fd,
byte_t  command,
byte_t  length,
const byte_t *  values 
)

Write a data block to the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
commandCommand to SMBus device.
lengthLength of buffer (bytes) to write.
[in]valuesBuffer of data to write.
Returns
Returns ≥ 0 on success. Else errno is set appropriately and -1 is returned.

Definition at line 297 of file smbus.c.

References i2c_smbus_data_union::block, i2c_smbus_access(), I2C_SMBUS_BLOCK_DATA, I2C_SMBUS_BLOCK_MAX, and I2C_SMBUS_WRITE.

299 {
300  i2c_smbus_data_t data;
301  int i;
302 
303  if( length > I2C_SMBUS_BLOCK_MAX )
304  {
305  length = I2C_SMBUS_BLOCK_MAX;
306  }
307 
308  for(i=1; i<=length; i++)
309  {
310  data.block[i] = values[i-1];
311  }
312  data.block[0] = length;
313 
314  return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
315  I2C_SMBUS_BLOCK_DATA, &data);
316 }
byte_t block[32+2]
block[0] is used for length and one more for PEC
Definition: i2c-dev.h:188
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_BLOCK_DATA
data block r/w operation
Definition: i2c-dev.h:208
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
#define I2C_SMBUS_BLOCK_MAX
As specified in SMBus standard.
Definition: i2c-dev.h:178
#define I2C_SMBUS_WRITE
write
Definition: i2c-dev.h:196
int i2c_smbus_write_byte ( int  fd,
byte_t  value 
)

Write an immediate byte to the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
valueByte value to write.
Returns
Returns ≥ 0 on success. Else errno is set appropriately and -1 is returned.

Definition at line 138 of file smbus.c.

References i2c_smbus_access(), I2C_SMBUS_BYTE, and I2C_SMBUS_WRITE.

139 {
140  return i2c_smbus_access(fd, I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
141 }
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_BYTE
immediate r/w byte operation
Definition: i2c-dev.h:204
#define I2C_SMBUS_WRITE
write
Definition: i2c-dev.h:196
int i2c_smbus_write_byte_data ( int  fd,
byte_t  command,
byte_t  value 
)

Write a data byte to the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
commandCommand to SMBus device.
valueByte value to write.
Returns
Returns ≥ 0 on success. Else errno is set appropriately and -1 is returned.

Definition at line 175 of file smbus.c.

References i2c_smbus_data_union::byte, i2c_smbus_access(), I2C_SMBUS_BYTE_DATA, and I2C_SMBUS_WRITE.

176 {
177  i2c_smbus_data_t data;
178 
179  data.byte = value;
180 
181  return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
182  I2C_SMBUS_BYTE_DATA, &data);
183 }
byte_t byte
data byte
Definition: i2c-dev.h:186
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_BYTE_DATA
data byte r/w operation
Definition: i2c-dev.h:205
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
#define I2C_SMBUS_WRITE
write
Definition: i2c-dev.h:196
int i2c_smbus_write_i2c_block_data ( int  fd,
byte_t  command,
byte_t  length,
const byte_t *  values 
)

Write a block of data to the SMBus via low-level I2C.

Parameters
fdFile descriptor to opened SMBus device.
commandCommand to SMBus device.
lengthLength of buffer (bytes) to write.
[in]valuesBuffer of data to write.
Returns
Returns ≥ 0 on success. Else errno is set appropriately and -1 is returned.

Definition at line 361 of file smbus.c.

References i2c_smbus_data_union::block, i2c_smbus_access(), I2C_SMBUS_I2C_BLOCK_DATA, I2C_SMBUS_I2C_BLOCK_MAX, and I2C_SMBUS_WRITE.

363 {
364  i2c_smbus_data_t data;
365  int i;
366 
367  if( length > I2C_SMBUS_I2C_BLOCK_MAX )
368  {
369  length = I2C_SMBUS_I2C_BLOCK_MAX;
370  }
371 
372  for(i=1; i<=length; i++)
373  {
374  data.block[i] = values[i-1];
375  }
376  data.block[0] = length;
377 
378  return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
379  I2C_SMBUS_I2C_BLOCK_DATA, &data);
380 }
byte_t block[32+2]
block[0] is used for length and one more for PEC
Definition: i2c-dev.h:188
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_I2C_BLOCK_DATA
i2c format block r/w/ operation
Definition: i2c-dev.h:209
#define I2C_SMBUS_I2C_BLOCK_MAX
Not specified - use same structure.
Definition: i2c-dev.h:179
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
#define I2C_SMBUS_WRITE
write
Definition: i2c-dev.h:196
int i2c_smbus_write_quick ( int  fd,
byte_t  value 
)

Write a quick value to the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
valueValue to write
Returns
Returns ≥ 0 on success. Else errno is set appropriately and -1 is returned.

Definition at line 104 of file smbus.c.

References I2C_NOCMD, i2c_smbus_access(), and I2C_SMBUS_QUICK.

105 {
106  return i2c_smbus_access(fd, value, I2C_NOCMD, I2C_SMBUS_QUICK, NULL);
107 }
#define I2C_NOCMD
no command
Definition: i2c-dev.h:220
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_QUICK
quick SMBus ioctl operation
Definition: i2c-dev.h:203
int i2c_smbus_write_word_data ( int  fd,
byte_t  command,
ushort_t  value 
)

Write a data 2-byte word to the SMBus.

Parameters
fdFile descriptor to opened SMBus device.
commandCommand to SMBus device.
valueWord value to write.
Returns
Returns ≥ 0 on success. Else errno is set appropriately and -1 is returned.

Definition at line 217 of file smbus.c.

References i2c_smbus_access(), I2C_SMBUS_WORD_DATA, I2C_SMBUS_WRITE, and i2c_smbus_data_union::word.

218 {
219  i2c_smbus_data_t data;
220 
221  data.word = value;
222 
223  return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
224  I2C_SMBUS_WORD_DATA, &data);
225 }
int i2c_smbus_access(int fd, byte_t read_write, byte_t command, int size, i2c_smbus_data_t *data)
Execute an SMBus IOCTL.
Definition: smbus.c:81
#define I2C_SMBUS_WORD_DATA
data word r/w operation
Definition: i2c-dev.h:206
ushort_t word
data short word
Definition: i2c-dev.h:187
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
#define I2C_SMBUS_WRITE
write
Definition: i2c-dev.h:196