DUDS
Distributed Update of Data from Something
duds::hardware::interface::linux::DevI2c Class Reference

Implementation of the I2c interface using the Linux kernel's user-space support. More...

#include <DevI2c.hpp>

Inheritance diagram for duds::hardware::interface::linux::DevI2c:
Collaboration diagram for duds::hardware::interface::linux::DevI2c:

Public Member Functions

 DevI2c (const std::string &devname, int devaddr)
 Opens the device file for the bus. More...
 
 ~DevI2c ()
 Closes the device file. More...
 
virtual int address () const
 Returns the address of the device that this object will attempt to communicate with. More...
 
virtual void converse (Conversation &conv)
 Conducts I2C communication with a device using the Linux i2c-dev driver. More...
 
- Public Member Functions inherited from duds::hardware::interface::Conversationalist
virtual ~Conversationalist ()=0
 Allow proper destruction using a Conversationalist pointer. More...
 

Private Member Functions

void io (i2c_rdwr_ioctl_data &idat)
 Calls ioctl to request the kernel do the I2C communication, the check for error conditions and throw corresponding exception on error. More...
 

Private Attributes

int addr
 The device (slave) address. More...
 
std::string dev
 Stores the device file name for later error reporting. More...
 
int fd
 The file descriptor for the open device. More...
 

Detailed Description

Implementation of the I2c interface using the Linux kernel's user-space support.

This requires that the kernel be built with support for the I2C master, and support for user-space I2C access. If the support is in kernel modules, they must be loaded along with the i2c-dev module. Using the kernel's i2c-gpio driver should be more efficient than implementing the I2C protocol with user-space GPIO support.

All thrown exceptions will include an attribute of boost::errinfo_file_name with the device file name, along with I2cDeviceAddr.

Author
Jeff Jackowski
Examples:
am2320test.cpp, amg88xxtest.cpp, and tsl2591test.cpp.

Definition at line 36 of file DevI2c.hpp.

Constructor & Destructor Documentation

◆ DevI2c()

duds::hardware::interface::linux::DevI2c::DevI2c ( const std::string &  devname,
int  devaddr 
)

Opens the device file for the bus.

Parameters
devnameThe path to the device file, usually /dev/i2c-N where N is the number assigned to the bus.
devaddrThe device, or slave, address used as the destination of communications.
Exceptions
I2cErrorUnsupportedA 10-bit address was requested but is not supported by the kernel's driver.
I2cErrorNoBusThe device file could not be opened.

Definition at line 23 of file DevI2c.cpp.

◆ ~DevI2c()

duds::hardware::interface::linux::DevI2c::~DevI2c ( )

Closes the device file.

Definition at line 39 of file DevI2c.cpp.

Member Function Documentation

◆ address()

int duds::hardware::interface::linux::DevI2c::address ( ) const
virtual

Returns the address of the device that this object will attempt to communicate with.

Implements duds::hardware::interface::I2c.

Definition at line 164 of file DevI2c.cpp.

◆ converse()

void duds::hardware::interface::linux::DevI2c::converse ( Conversation conv)
virtual

Conducts I2C communication with a device using the Linux i2c-dev driver.

The MpfVarlen flag of ConversationPart is honored. Input parts using this must have a buffer longer than 32 bytes; 32 for data, and 1 for the length.

The MpfBreak flag of ConversationPart objects is honored by separating the conversation into multiple ioctl() calls. The combination of the kernel's scheduling and other running software will determine if any other I2C communication from the same master will occur between the ioctl() calls here.

Parameters
convThe conversation to have with the device on the other end.
Exceptions
I2cErrorConversationLengthThe conversation has too many parts for the implementation to handle.
I2cErrorPartLengthA variable length input part had a buffer that was not longer than 32 bytes.
I2cErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer. It can be caused by another I2C master on a mulit-master bus.
I2cErrorNoDeviceThe device did not respond to its address.
I2cErrorUnsupportedAn operation is unsupported by the master.
I2cErrorProtocolData from the device does not conform to the I2C protocol.
I2cErrorTimeoutThe operation took too long resulting in a bus timeout.
I2cErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::I2c.

Definition at line 98 of file DevI2c.cpp.

◆ io()

void duds::hardware::interface::linux::DevI2c::io ( i2c_rdwr_ioctl_data &  idat)
private

Calls ioctl to request the kernel do the I2C communication, the check for error conditions and throw corresponding exception on error.

Definition at line 43 of file DevI2c.cpp.

Referenced by converse().

Member Data Documentation

◆ addr

int duds::hardware::interface::linux::DevI2c::addr
private

The device (slave) address.

Definition at line 48 of file DevI2c.hpp.

Referenced by address(), converse(), DevI2c(), and io().

◆ dev

std::string duds::hardware::interface::linux::DevI2c::dev
private

Stores the device file name for later error reporting.

Definition at line 40 of file DevI2c.hpp.

Referenced by converse(), DevI2c(), and io().

◆ fd

int duds::hardware::interface::linux::DevI2c::fd
private

The file descriptor for the open device.

Definition at line 44 of file DevI2c.hpp.

Referenced by DevI2c(), io(), and ~DevI2c().


The documentation for this class was generated from the following files: