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

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

#include <DevSmbus.hpp>

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

Public Member Functions

 DevSmbus (const std::string &devname, int devaddr, bool pec=true)
 Opens the device file for the bus. More...
 
 DevSmbus (const std::string &devname, int devaddr, UsePec)
 Opens the device file for the bus and specifies that Packet Error Checking (PEC) will be used. More...
 
 DevSmbus (const std::string &devname, int devaddr, NoPec)
 Opens the device file for the bus and specifies that Packet Error Checking (PEC) will not be used. More...
 
 ~DevSmbus ()
 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 std::uint16_t call (std::uint8_t cmd, std::uint16_t word)
 Does a process call operation. More...
 
virtual void call (std::uint8_t cmd, const std::vector< std::uint8_t > &out, std::vector< std::uint8_t > &in)
 Does a block process call operation. More...
 
virtual int receive (std::uint8_t cmd, std::uint8_t *in, const int maxlen)
 Sends a command byte, then reads a block of data from the device. More...
 
virtual void receive (std::uint8_t cmd, std::vector< std::uint8_t > &in)
 Sends a command byte, then reads a block of data from the device. More...
 
virtual std::uint8_t receiveByte ()
 Read a single byte from the device without sending a command/register byte first. More...
 
virtual std::uint8_t receiveByte (std::uint8_t cmd)
 Sends a command byte, then reads a single byte from the device. More...
 
virtual std::uint16_t receiveWord (std::uint8_t cmd)
 Sends a command byte, then reads a word, two bytes, from the device. More...
 
virtual void transmit (std::uint8_t cmd, const std::uint8_t *out, const int len)
 Sends a command byte and a block of data to the device. More...
 
virtual void transmitBool (bool out)
 Sends a single bit to the device. More...
 
virtual void transmitByte (std::uint8_t byte)
 Sends a single byte to the device. More...
 
virtual void transmitByte (std::uint8_t cmd, std::uint8_t byte)
 Sends a command byte and a data byte to the device. More...
 
virtual void transmitWord (std::uint8_t cmd, std::uint16_t word)
 Sends a command byte and a data word to the device. More...
 
- Public Member Functions inherited from duds::hardware::interface::Smbus
virtual ~Smbus ()=0
 
std::uint16_t receiveWordBe (std::uint8_t cmd)
 Sends a command byte, then reads a big-endian word from the device. More...
 
void transmit (std::uint8_t cmd, const std::vector< std::uint8_t > &out)
 Sends a command byte and a block of data to the device. More...
 
void transmitWordBe (std::uint8_t cmd, std::uint16_t word)
 Sends a command byte and a big-endian data word to the device. More...
 

Private Member Functions

void io (i2c_smbus_ioctl_data &sdat)
 Sends I/O requests to the kernel, then checks for an error and if found throws the appropriate exception. More...
 

Private Attributes

int addr
 The device (slave) address; used for error reporting. 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 Smbus interface using the Linux kernel's user-space support.

This requires that the kernel be built with support for the SMBus or 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 SMBus 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 SmbusDeviceAddr.

The name follows SysFsGpio in naming the kernel interface, and it avoids using the same name as the base class which I figured might lessen confusion.

Author
Jeff Jackowski
Examples:
apds9301test.cpp, and ina219test.cpp.

Definition at line 40 of file DevSmbus.hpp.

Constructor & Destructor Documentation

◆ DevSmbus() [1/3]

duds::hardware::interface::linux::DevSmbus::DevSmbus ( const std::string &  devname,
int  devaddr,
bool  pec = true 
)

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.
pecTrue (default) to enable use of Packet Error Checking. If the device supports PEC, this should be used to help prevent bad data over the bus from causing trouble.
Exceptions
SmbusErrorUnsupportedEither PEC requested but not supported, or a 10-bit address was requested but is not supported by the kernel's driver.
SmbusErrorNoBusThe device file could not be opened or failed to accept the device address.

Definition at line 24 of file DevSmbus.cpp.

◆ DevSmbus() [2/3]

duds::hardware::interface::linux::DevSmbus::DevSmbus ( const std::string &  devname,
int  devaddr,
UsePec   
)
inline

Opens the device file for the bus and specifies that Packet Error Checking (PEC) will be used.

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
SmbusErrorUnsupportedEither PEC requested but not supported, or a 10-bit address was requested but is not supported by the kernel's driver.
SmbusErrorThe device file could not be opened or failed to accept the device address.

Definition at line 88 of file DevSmbus.hpp.

◆ DevSmbus() [3/3]

duds::hardware::interface::linux::DevSmbus::DevSmbus ( const std::string &  devname,
int  devaddr,
NoPec   
)
inline

Opens the device file for the bus and specifies that Packet Error Checking (PEC) will not be used.

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
SmbusErrorUnsupportedA 10-bit address was requested but is not supported by the kernel's driver.
SmbusErrorThe device file could not be opened or failed to accept the device address.

Definition at line 102 of file DevSmbus.hpp.

◆ ~DevSmbus()

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

Closes the device file.

Definition at line 53 of file DevSmbus.cpp.

Referenced by DevSmbus().

Member Function Documentation

◆ address()

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

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

Implements duds::hardware::interface::Smbus.

Definition at line 285 of file DevSmbus.cpp.

Referenced by DevSmbus().

◆ call() [1/2]

std::uint16_t duds::hardware::interface::linux::DevSmbus::call ( std::uint8_t  cmd,
std::uint16_t  word 
)
virtual

Does a process call operation.

Sends a command byte and a word to the device, then receives a word.

Parameters
cmdThe command or register byte.
wordThe word to send.
Returns
The word received from the device.
Exceptions
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 248 of file DevSmbus.cpp.

Referenced by DevSmbus().

◆ call() [2/2]

void duds::hardware::interface::linux::DevSmbus::call ( std::uint8_t  cmd,
const std::vector< std::uint8_t > &  out,
std::vector< std::uint8_t > &  in 
)
virtual

Does a block process call operation.

Sends a command byte and block of data to the device, then receives a block of data.

Parameters
cmdThe command or register byte.
outThe data block to send. It must not have more than 32 bytes.
inThe data block received from the device.
Exceptions
SmbusErrorMessageLengthA request was made to send a block of more than 32 bytes.
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 261 of file DevSmbus.cpp.

◆ io()

void duds::hardware::interface::linux::DevSmbus::io ( i2c_smbus_ioctl_data &  sdat)
private

Sends I/O requests to the kernel, then checks for an error and if found throws the appropriate exception.

Definition at line 57 of file DevSmbus.cpp.

Referenced by call(), receive(), receiveByte(), receiveWord(), transmit(), transmitBool(), transmitByte(), and transmitWord().

◆ receive() [1/2]

int duds::hardware::interface::linux::DevSmbus::receive ( std::uint8_t  cmd,
std::uint8_t *  in,
const int  maxlen 
)
virtual

Sends a command byte, then reads a block of data from the device.

Parameters
cmdThe command or register byte.
inThe input buffer.
maxlenThe maximum amount of data to write into in.
Returns
The number of bytes received from the device.
Exceptions
SmbusErrorMessageLengthThe buffer in could not hold all the received data.
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 194 of file DevSmbus.cpp.

Referenced by DevSmbus().

◆ receive() [2/2]

void duds::hardware::interface::linux::DevSmbus::receive ( std::uint8_t  cmd,
std::vector< std::uint8_t > &  in 
)
virtual

Sends a command byte, then reads a block of data from the device.

Parameters
cmdThe command or register byte.
inThe input vector. Upon success, the vector will be resized to match the received data.
Exceptions
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 213 of file DevSmbus.cpp.

◆ receiveByte() [1/2]

std::uint8_t duds::hardware::interface::linux::DevSmbus::receiveByte ( )
virtual

Read a single byte from the device without sending a command/register byte first.

Returns
The byte from the device.
Exceptions
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 122 of file DevSmbus.cpp.

Referenced by DevSmbus().

◆ receiveByte() [2/2]

std::uint8_t duds::hardware::interface::linux::DevSmbus::receiveByte ( std::uint8_t  cmd)
virtual

Sends a command byte, then reads a single byte from the device.

Parameters
cmdThe command or register byte.
Returns
The byte from the device.
Exceptions
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 146 of file DevSmbus.cpp.

◆ receiveWord()

std::uint16_t duds::hardware::interface::linux::DevSmbus::receiveWord ( std::uint8_t  cmd)
virtual

Sends a command byte, then reads a word, two bytes, from the device.

Parameters
cmdThe command or register byte.
Returns
The word from the device.
Exceptions
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 170 of file DevSmbus.cpp.

Referenced by DevSmbus().

◆ transmit()

void duds::hardware::interface::linux::DevSmbus::transmit ( std::uint8_t  cmd,
const std::uint8_t *  out,
const int  len 
)
virtual

Sends a command byte and a block of data to the device.

Parameters
cmdThe command or register byte.
outThe data to send.
lenThe number of bytes in the buffer out to send. It must be between 1 and 32, inclusive.
Exceptions
SmbusErrorMessageLengthA request was made to send a block of less than 1 or more than 32 bytes.
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 226 of file DevSmbus.cpp.

Referenced by DevSmbus().

◆ transmitBool()

void duds::hardware::interface::linux::DevSmbus::transmitBool ( bool  out)
virtual

Sends a single bit to the device.

Exceptions
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 112 of file DevSmbus.cpp.

Referenced by DevSmbus().

◆ transmitByte() [1/2]

void duds::hardware::interface::linux::DevSmbus::transmitByte ( std::uint8_t  byte)
virtual

Sends a single byte to the device.

Parameters
byteThe byte to send.
Exceptions
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 134 of file DevSmbus.cpp.

Referenced by DevSmbus().

◆ transmitByte() [2/2]

void duds::hardware::interface::linux::DevSmbus::transmitByte ( std::uint8_t  cmd,
std::uint8_t  byte 
)
virtual

Sends a command byte and a data byte to the device.

Parameters
cmdThe command or register byte.
byteThe data byte to send.
Exceptions
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 158 of file DevSmbus.cpp.

◆ transmitWord()

void duds::hardware::interface::linux::DevSmbus::transmitWord ( std::uint8_t  cmd,
std::uint16_t  word 
)
virtual

Sends a command byte and a data word to the device.

Parameters
cmdThe command or register byte.
wordThe data word to send.
Exceptions
SmbusErrorPecThe PEC checksum was not valid for the data.
SmbusErrorBusyThe bus was in use for an inordinate length of time. This is not caused by scheduling on the same host computer.
SmbusErrorNoDeviceThe device did not respond to its address.
SmbusErrorUnsupportedThis operation is unsupported by the master.
SmbusErrorProtocolData from the device does not conform to the SMBus protocol.
SmbusErrorTimeoutThe operation took too long resulting in a bus timeout.
SmbusErrorA general error that doesn't fit one of the other exceptions.

Implements duds::hardware::interface::Smbus.

Definition at line 182 of file DevSmbus.cpp.

Referenced by DevSmbus().

Member Data Documentation

◆ addr

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

The device (slave) address; used for error reporting.

Definition at line 52 of file DevSmbus.hpp.

Referenced by address(), call(), DevSmbus(), io(), receive(), and transmit().

◆ dev

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

Stores the device file name for later error reporting.

Definition at line 44 of file DevSmbus.hpp.

Referenced by call(), DevSmbus(), io(), receive(), and transmit().

◆ fd

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

The file descriptor for the open device.

Definition at line 48 of file DevSmbus.hpp.

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


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