|
DUDS
|
Distributed Update of Data from Something
|
An abstraction for the master side of a simple synchronous serial communication connection to some device. More...
#include <MasterSyncSerial.hpp>
Public Types | |
| typedef duds::general::BitFlags< struct MssFlags > | Flags |
| Configuration flags for various synchronous serial options. More... | |
Public Member Functions | |
| MasterSyncSerial () | |
| Builds a MasterSyncSerial with an invalid clock period and all configuration flags clear. More... | |
| MasterSyncSerial (Flags flags, int period) | |
| Builds a MasterSyncSerial object. More... | |
| virtual | ~MasterSyncSerial ()=0 |
| Derived class destructors should assure that communication has stopped by calling forceClose(). More... | |
| std::unique_ptr< MasterSyncSerialAccess > | access () |
| Obtain access for communication; transitions the object from the ready (MssReady) to the open (MssOpen) state. More... | |
| void | access (MasterSyncSerialAccess &acc) |
| Obtain access for communication; transitions the object from the ready (MssReady) to the open (MssOpen) state. More... | |
| std::unique_ptr< MasterSyncSerialAccess > | accessStart () |
| Obtain access for communication; transitions the object from the ready (MssReady) to the communicating (MssCommunicating) state. More... | |
| void | accessStart (MasterSyncSerialAccess &acc) |
| Obtain access for communication; transitions the object from the ready (MssReady) to the communicating (MssCommunicating) state. More... | |
| unsigned int | clockFrequency () const noexcept |
| Computes and returns the maximum clock frequency in Hertz. More... | |
| unsigned int | clockPeriod () const noexcept |
| Returns the minimum clock period in nanoseconds. More... | |
| Flags | configFlags () const noexcept |
| Returns the current set of configuration flags. More... | |
| virtual void | converse (Conversation &conv) |
| Has a half-duplex Conversation with the connected device. More... | |
| bool | inUse () const noexcept |
| Returns true when this serial interface is in use by checking for the existence of an access object. More... | |
Public Member Functions inherited from duds::hardware::interface::Conversationalist | |
| virtual | ~Conversationalist ()=0 |
| Allow proper destruction using a Conversationalist pointer. More... | |
Static Public Attributes | |
| static constexpr Flags | MssClockIdleHigh = Flags(2) |
| Before communication begins, and after it ends, the clock line should have a high logic level. More... | |
| static constexpr Flags | MssCommunicating = Flags(128) |
| Indicates that communication is underway. More... | |
| static constexpr Flags | MssConfigMask = Flags(31) |
| All the flags that are used for configuration rather than the current state. More... | |
| static constexpr Flags | MssFirstDerivedClassFlag = Flags(256) |
| The first flag that may be defined by a derived class. More... | |
| static constexpr Flags | MssFullDuplex = Flags(16) |
| Communication is full duplex. More... | |
| static constexpr Flags | MssMSbFirst = Flags(8) |
| Send data MSb first, little endian. More... | |
| static constexpr Flags | MssOpen = Flags(64) |
| Indicates that all required resources for communication have been acquired. More... | |
| static constexpr Flags | MssOutFallInRise = Flags(4) |
| Output on the falling edge of the clock and read on the rising edge. More... | |
| static constexpr Flags | MssReady = Flags(32) |
| Indictates that all required resources for communication have been identified and passed a validity check. More... | |
| static constexpr Flags | MssSpiMode0 |
| Flags for SPI mode 0. More... | |
| static constexpr Flags | MssSpiMode0LSb = MssOutFallInRise | MssFullDuplex |
| Flags for SPI mode 0 with the LSb transfered first. More... | |
| static constexpr Flags | MssSpiMode1 = MssMSbFirst | MssFullDuplex |
| Flags for SPI mode 1. More... | |
| static constexpr Flags | MssSpiMode1LSb = MssFullDuplex |
| Flags for SPI mode 1 with the LSb transfered first. More... | |
| static constexpr Flags | MssSpiMode2 |
| Flags for SPI mode 2. More... | |
| static constexpr Flags | MssSpiMode2LSb = MssClockIdleHigh | MssFullDuplex |
| Flags for SPI mode 2 with the LSb transfered first. More... | |
| static constexpr Flags | MssSpiMode3 |
| Flags for SPI mode 3. More... | |
| static constexpr Flags | MssSpiMode3LSb |
| Flags for SPI mode 3 with the LSb transfered first. More... | |
| static constexpr Flags | MssUseSelect = Flags(1) |
| Use a select line to tell a device to pay attention to the master. More... | |
Protected Member Functions | |
| void | clockFrequency (unsigned int freq) |
| Changes the maximum clock frequency. More... | |
| void | clockPeriod (unsigned int nanos) |
| Changes the minimum clock period. More... | |
| virtual void | close ()=0 |
| Transitions the object from the open (MssOpen) to the ready (MssReady) state. More... | |
| void | condStart () |
| Calls start() if not currently communicating (clear MssCommunicating flag). More... | |
| void | condStop () |
| Calls stop() if currently communicating (set MssCommunicating flag). More... | |
| void | converseAlreadyOpen (Conversation &conv) |
| Has a half-duplex Conversation with the connected device. More... | |
| void | forceClose () |
| Attempts to forcibly cease communications by calling condStop() and close(). More... | |
| virtual void | open ()=0 |
| Transitions the object from the ready (MssReady) to the open (MssOpen) state. More... | |
| virtual void | receive (std::uint8_t *buff, duds::general::Bits bits) |
| Receives bits of data. More... | |
| virtual void | start ()=0 |
| Denotes the start of a conversation; transitions from the open state to the communicating state. More... | |
| virtual void | stop ()=0 |
| Denotes the end of a conversation; transitions from the communicating state to the open state. More... | |
| virtual void | transfer (const std::uint8_t *__restrict__ out, std::uint8_t *__restrict__ in, duds::general::Bits bits)=0 |
| Sends and/or receives bits of data. More... | |
| virtual void | transmit (const std::uint8_t *buff, duds::general::Bits bits) |
| Sends bits of data. More... | |
Protected Attributes | |
| Flags | flags |
| Configuration flags. More... | |
| unsigned int | minHalfPeriod |
| The minimum time between changing the clock edge in nanoseconds. More... | |
Private Member Functions | |
| void | retire (MasterSyncSerialAccess *acc) |
| Removes the access object from use. More... | |
Private Attributes | |
| MasterSyncSerialAccess * | mssacc |
| A pointer to the current access object or nullptr. More... | |
Friends | |
| class | MasterSyncSerialAccess |
| The access object calls select(), deselect(), and retire(MasterSyncSerialAccess *). More... | |
An abstraction for the master side of a simple synchronous serial communication connection to some device.
The abstraction is only for working with a single device; it is expected that multiple such objects will be used for each device on a bus. This abstraction is not intended for use with more complex protocols like I2C.
The abstraction and any similar kind of bus object should not be used for a device if the bus is unimportant to accessing said device. For instance, if an SPI device with Linux kernel support for the device is used, the kernel's device driver should, and likely must, be used to work with the device. However, if the kernel is unaware of the device and the program supplies its own driver, a MasterSyncSerial object can handle the low-level communication.
Single MasterSyncSerial objects do not allow for thread-safe use because they are intended to communicate with a single device and such communication typically does not work well or make sense to implement with multiple threads. However, multiple MasterSyncSerial objects can be thread-safe. Even if resources are shared between MasterSyncSerial objects, they must use those resources in a thread-safe manner. DigitalPinMasterSyncSerial, for instance, must obtain the DigitalPinSetAccess objects for all of its pins in its open() function.
Definition at line 95 of file MasterSyncSerial.hpp.
| typedef duds::general::BitFlags<struct MssFlags> duds::hardware::interface::MasterSyncSerial::Flags |
Configuration flags for various synchronous serial options.
Definition at line 116 of file MasterSyncSerial.hpp.
|
inline |
Builds a MasterSyncSerial with an invalid clock period and all configuration flags clear.
Definition at line 385 of file MasterSyncSerial.hpp.
| duds::hardware::interface::MasterSyncSerial::MasterSyncSerial | ( | Flags | flags, |
| int | period | ||
| ) |
Builds a MasterSyncSerial object.
| flags | The initial set of configuration flags. |
| period | The minimum clock period in nanoseconds. |
Definition at line 36 of file MasterSyncSerial.cpp.
|
pure virtual |
Derived class destructors should assure that communication has stopped by calling forceClose().
This may be done unconditionally. forceClose() will call virtual functions. Those functions must be called before the destructor of the class that implements them has finished running; this base class cannot make the call.
Definition at line 40 of file MasterSyncSerial.cpp.
Referenced by MasterSyncSerial().
| std::unique_ptr< MasterSyncSerialAccess > duds::hardware::interface::MasterSyncSerial::access | ( | ) |
Obtain access for communication; transitions the object from the ready (MssReady) to the open (MssOpen) state.
Implementations handle the transition with their implementation of open(). If open() throws an exception, it will not be caught by this function and will prevent the state transition.
| SyncSerialNotReady | The object is not in the ready state. |
| SyncSerialInUse | The object is already in the open state. |
Definition at line 81 of file MasterSyncSerial.cpp.
Referenced by accessStart(), and clockPeriod().
| void duds::hardware::interface::MasterSyncSerial::access | ( | MasterSyncSerialAccess & | acc | ) |
Obtain access for communication; transitions the object from the ready (MssReady) to the open (MssOpen) state.
Implementations handle the transition with their implementation of open(). If open() throws an exception, it will not be caught by this function and will prevent the state transition.
| acc | The access object that will be used for controlling communications. It must not already be providing access. |
| SyncSerialNotReady | The object is not in the ready state. |
| SyncSerialInUse | The object is already in the open state. |
| SyncSerialAccessInUse | The access object, acc, is providing access to a serial interface. |
Definition at line 94 of file MasterSyncSerial.cpp.
| std::unique_ptr< MasterSyncSerialAccess > duds::hardware::interface::MasterSyncSerial::accessStart | ( | ) |
Obtain access for communication; transitions the object from the ready (MssReady) to the communicating (MssCommunicating) state.
Implementations handle the transition with their implementation of open(). If open() throws an exception, it will not be caught by this function and will prevent the state transition.
| SyncSerialNotReady | The object is not in the ready state. |
| SyncSerialInUse | The object is in the open or communicating state. |
Definition at line 110 of file MasterSyncSerial.cpp.
Referenced by clockPeriod().
| void duds::hardware::interface::MasterSyncSerial::accessStart | ( | MasterSyncSerialAccess & | acc | ) |
Obtain access for communication; transitions the object from the ready (MssReady) to the communicating (MssCommunicating) state.
Implementations handle the transition with their implementation of open(). If open() throws an exception, it will not be caught by this function and will prevent the state transition.
| acc | The access object that will be used for controlling communications. It must not already be providing access. |
| SyncSerialNotReady | The object is not in the ready state. |
| SyncSerialInUse | The object is in the open or communicating state. |
| SyncSerialAccessInUse | The access object, acc, is providing access to a serial interface. |
Definition at line 116 of file MasterSyncSerial.cpp.
|
protected |
Changes the maximum clock frequency.
This is an internal function to allow implementations control over the stored clock speed.
| freq | The requested maximum clock frequency in hertz. |
Definition at line 69 of file MasterSyncSerial.cpp.
|
noexcept |
Computes and returns the maximum clock frequency in Hertz.
The actual clock frequency may be lower.
Definition at line 65 of file MasterSyncSerial.cpp.
Referenced by duds::hardware::interface::DigitalPinMasterSyncSerial::clockFrequency(), clockPeriod(), duds::hardware::interface::linux::SpiMasterSyncSerial::setClockFrequency(), duds::hardware::interface::linux::SpiMasterSyncSerial::setClockPeriod(), and duds::hardware::interface::DigitalPinMasterSyncSerial::setPins().
|
protected |
Changes the minimum clock period.
This is an internal function to allow implementations control over the clock speed.
| nanos | The requested minimum clock period in nanoseconds. |
Definition at line 73 of file MasterSyncSerial.cpp.
|
inlinenoexcept |
Returns the minimum clock period in nanoseconds.
The actual clock period may be longer.
Definition at line 418 of file MasterSyncSerial.hpp.
Referenced by clockFrequency(), duds::hardware::interface::DigitalPinMasterSyncSerial::clockPeriod(), duds::hardware::interface::linux::SpiMasterSyncSerial::setClockPeriod(), and duds::hardware::interface::DigitalPinMasterSyncSerial::setPins().
|
protectedpure virtual |
Transitions the object from the open (MssOpen) to the ready (MssReady) state.
This should relinquish access to any required shared resources.
Implemented in duds::hardware::interface::DigitalPinMasterSyncSerial, and duds::hardware::interface::linux::SpiMasterSyncSerial.
Referenced by converse(), forceClose(), and retire().
|
protected |
Calls start() if not currently communicating (clear MssCommunicating flag).
| SyncSerialNotReady | The object is not in the ready state (MssReady). |
Definition at line 121 of file MasterSyncSerial.cpp.
Referenced by accessStart(), and converseAlreadyOpen().
|
protected |
Calls stop() if currently communicating (set MssCommunicating flag).
| SyncSerialNotReady | The object is not in the ready state (MssReady). |
Definition at line 136 of file MasterSyncSerial.cpp.
Referenced by converseAlreadyOpen(), forceClose(), and retire().
|
inlinenoexcept |
Returns the current set of configuration flags.
Definition at line 404 of file MasterSyncSerial.hpp.
|
virtual |
Has a half-duplex Conversation with the connected device.
An access object is not used to call this function, but one is acquired and released internally. The Conversation object defines all input and output parameters. On the ConversationPart objects, the MpfBreak flag is honored, but the MpfVarlen flag is ignored. The transmit() and receive() functions are called to move the data.
| conv | The conversation to have with the device on the other end. |
| SyncSerialInUse | The object is already in the open state. |
| SyncSerialIoError | An error prevented the communication. |
Implements duds::hardware::interface::Conversationalist.
Definition at line 185 of file MasterSyncSerial.cpp.
Referenced by clockPeriod().
|
protected |
Has a half-duplex Conversation with the connected device.
The Conversation object defines all input and output parameters. On the ConversationPart objects, the MpfBreak flag is honored, but the MpfVarlen flag is ignored. The transmit() and receive() functions are called to move the data.
| conv | The conversation to have with the device on the other end. |
| SyncSerialIoError | An error prevented the communication. |
Definition at line 162 of file MasterSyncSerial.cpp.
Referenced by converse().
|
protected |
Attempts to forcibly cease communications by calling condStop() and close().
It is intended to be called in the destructor of derived classes. This base class cannot make the call in its destructor because the calls invoke virtual functions. The destructor may call this function unconditionally.
Definition at line 42 of file MasterSyncSerial.cpp.
Referenced by converse(), duds::hardware::interface::DigitalPinMasterSyncSerial::~DigitalPinMasterSyncSerial(), and duds::hardware::interface::linux::SpiMasterSyncSerial::~SpiMasterSyncSerial().
|
inlinenoexcept |
Returns true when this serial interface is in use by checking for the existence of an access object.
Definition at line 411 of file MasterSyncSerial.hpp.
|
protectedpure virtual |
Transitions the object from the ready (MssReady) to the open (MssOpen) state.
This should acquire access to any required shared resources.
| boost::exception | Badness; state transision did not occur. |
Implemented in duds::hardware::interface::DigitalPinMasterSyncSerial, and duds::hardware::interface::linux::SpiMasterSyncSerial.
Referenced by access(), and converse().
|
protectedvirtual |
Receives bits of data.
If full duplex communication is used, transmitted data is undefined unless an implementation cares to have a definition. The implementation in this base class is:
| buff | The buffer that will receive the data. |
| bits | The number of bits to send. Implementations may impose limitations on this value, like requiring a multiple of 8. |
| SyncSerialNotCommunicating | This object is not in the communicating state. |
| SyncSerialUnsupported | An operation unsupported by the implementation was attempted. This may happen if (bits % 8) is non-zero. |
| SyncSerialIoError | An error prevented the communication. |
Definition at line 158 of file MasterSyncSerial.cpp.
Referenced by converseAlreadyOpen().
|
private |
Removes the access object from use.
| SyncSerialInvalidAccess | acc is not the current access object. |
Definition at line 49 of file MasterSyncSerial.cpp.
|
protectedpure virtual |
Denotes the start of a conversation; transitions from the open state to the communicating state.
Implemented in duds::hardware::interface::DigitalPinMasterSyncSerial, and duds::hardware::interface::linux::SpiMasterSyncSerial.
Referenced by condStart().
|
protectedpure virtual |
Denotes the end of a conversation; transitions from the communicating state to the open state.
Implemented in duds::hardware::interface::DigitalPinMasterSyncSerial, and duds::hardware::interface::linux::SpiMasterSyncSerial.
Referenced by condStop().
|
protectedpure virtual |
Sends and/or receives bits of data.
If full duplex communication is not supported, one of the buffers should be given a nullptr pointer. The buffers must not overlap.
| out | The data to transmit, or nullptr to not transmit. |
| in | The buffer that will receive data, or nullptr if nothing will be received. |
| bits | The number of bits to transfer. Implementations may impose limitations on this value, like requiring a multiple of 8. |
| SyncSerialNotFullDuplex | Neither out and in are nullptr, and the serial interface is half-duplex. |
| SyncSerialNotCommunicating | This object is not in the communicating state. |
| SyncSerialUnsupported | An operation unsupported by the implementation was attempted. This may happen if (bits % 8) is non-zero. |
| SyncSerialIoError | An error prevented the communication. |
Implemented in duds::hardware::interface::linux::SpiMasterSyncSerial.
Referenced by receive(), and transmit().
|
protectedvirtual |
Sends bits of data.
If full duplex communication is used, received data is lost. The implementation in this base class is:
| buff | The data to transmit. |
| bits | The number of bits to send. Implementations may impose limitations on this value, like requiring a multiple of 8. |
| SyncSerialNotCommunicating | This object is not in the communicating state. |
| SyncSerialUnsupported | An operation unsupported by the implementation was attempted. This may happen if (bits % 8) is non-zero. |
| SyncSerialIoError | An error prevented the communication. |
Definition at line 151 of file MasterSyncSerial.cpp.
Referenced by converseAlreadyOpen().
|
friend |
The access object calls select(), deselect(), and retire(MasterSyncSerialAccess *).
Definition at line 101 of file MasterSyncSerial.hpp.
Referenced by access().
|
protected |
Configuration flags.
Definition at line 222 of file MasterSyncSerial.hpp.
Referenced by access(), clockPeriod(), condStart(), condStop(), converse(), forceClose(), MasterSyncSerial(), duds::hardware::interface::linux::SpiMasterSyncSerial::open(), duds::hardware::interface::DigitalPinMasterSyncSerial::open(), retire(), duds::hardware::interface::DigitalPinMasterSyncSerial::setChipSelect(), duds::hardware::interface::linux::SpiMasterSyncSerial::setClockFrequency(), duds::hardware::interface::linux::SpiMasterSyncSerial::setClockPeriod(), duds::hardware::interface::DigitalPinMasterSyncSerial::setPins(), duds::hardware::interface::DigitalPinMasterSyncSerial::stop(), duds::hardware::interface::linux::SpiMasterSyncSerial::transfer(), and duds::hardware::interface::DigitalPinMasterSyncSerial::transfer().
|
protected |
The minimum time between changing the clock edge in nanoseconds.
This is a period instead of a frequency because software implementations need to sleep for half a period very often, whereas implementations that require a frequency are typically using hardware support and need the frequency just once.
Definition at line 218 of file MasterSyncSerial.hpp.
Referenced by clockFrequency(), clockPeriod(), duds::hardware::interface::linux::SpiMasterSyncSerial::setClockPeriod(), duds::hardware::interface::DigitalPinMasterSyncSerial::start(), duds::hardware::interface::DigitalPinMasterSyncSerial::stop(), and duds::hardware::interface::DigitalPinMasterSyncSerial::transfer().
|
private |
A pointer to the current access object or nullptr.
Definition at line 105 of file MasterSyncSerial.hpp.
Referenced by access(), converse(), and retire().
|
static |
Before communication begins, and after it ends, the clock line should have a high logic level.
Definition at line 125 of file MasterSyncSerial.hpp.
Referenced by duds::hardware::interface::linux::SpiMasterSyncSerial::open(), duds::hardware::interface::DigitalPinMasterSyncSerial::open(), and duds::hardware::interface::DigitalPinMasterSyncSerial::stop().
|
static |
Indicates that communication is underway.
If a select line is used, it will be in the selected state.
Definition at line 160 of file MasterSyncSerial.hpp.
Referenced by clockPeriod(), condStart(), condStop(), duds::hardware::interface::linux::SpiMasterSyncSerial::setClockFrequency(), duds::hardware::interface::linux::SpiMasterSyncSerial::setClockPeriod(), duds::hardware::interface::linux::SpiMasterSyncSerial::transfer(), and duds::hardware::interface::DigitalPinMasterSyncSerial::transfer().
|
static |
All the flags that are used for configuration rather than the current state.
Definition at line 142 of file MasterSyncSerial.hpp.
Referenced by configFlags(), and MasterSyncSerial().
|
static |
The first flag that may be defined by a derived class.
Definition at line 165 of file MasterSyncSerial.hpp.
|
static |
Communication is full duplex.
Definition at line 137 of file MasterSyncSerial.hpp.
Referenced by duds::hardware::interface::DigitalPinMasterSyncSerial::checkPins(), duds::hardware::interface::DigitalPinMasterSyncSerial::open(), duds::hardware::interface::DigitalPinMasterSyncSerial::setChipSelect(), duds::hardware::interface::DigitalPinMasterSyncSerial::setPins(), and duds::hardware::interface::DigitalPinMasterSyncSerial::transfer().
|
static |
Send data MSb first, little endian.
Definition at line 133 of file MasterSyncSerial.hpp.
Referenced by duds::hardware::interface::linux::SpiMasterSyncSerial::open(), and duds::hardware::interface::DigitalPinMasterSyncSerial::transfer().
|
static |
Indicates that all required resources for communication have been acquired.
These are initally put into a non-communicating state.
Definition at line 155 of file MasterSyncSerial.hpp.
Referenced by access(), condStart(), condStop(), converse(), forceClose(), duds::hardware::interface::linux::SpiMasterSyncSerial::open(), retire(), duds::hardware::interface::DigitalPinMasterSyncSerial::setChipSelect(), and duds::hardware::interface::DigitalPinMasterSyncSerial::setPins().
|
static |
Output on the falling edge of the clock and read on the rising edge.
Definition at line 129 of file MasterSyncSerial.hpp.
Referenced by duds::hardware::interface::linux::SpiMasterSyncSerial::open(), and duds::hardware::interface::DigitalPinMasterSyncSerial::transfer().
|
static |
Indictates that all required resources for communication have been identified and passed a validity check.
The MasterSyncSerial class clears this flag when constructed; after that, the flag is only changed by derived classes. Several functions in this base class use this flag to check for improper usage.
Definition at line 150 of file MasterSyncSerial.hpp.
Referenced by access(), converse(), duds::hardware::interface::linux::SpiMasterSyncSerial::open(), duds::hardware::interface::DigitalPinMasterSyncSerial::setChipSelect(), and duds::hardware::interface::DigitalPinMasterSyncSerial::setPins().
|
static |
Flags for SPI mode 0.
This does not include MssUseSelect; this should be set if needed.
Definition at line 170 of file MasterSyncSerial.hpp.
|
static |
Flags for SPI mode 0 with the LSb transfered first.
This does not include MssUseSelect; this should be set if needed.
Definition at line 193 of file MasterSyncSerial.hpp.
|
static |
Flags for SPI mode 1.
This does not include MssUseSelect; this should be set if needed.
Definition at line 176 of file MasterSyncSerial.hpp.
|
static |
Flags for SPI mode 1 with the LSb transfered first.
This does not include MssUseSelect; this should be set if needed.
Definition at line 198 of file MasterSyncSerial.hpp.
|
static |
Flags for SPI mode 2.
This does not include MssUseSelect; this should be set if needed.
Definition at line 181 of file MasterSyncSerial.hpp.
|
static |
Flags for SPI mode 2 with the LSb transfered first.
This does not include MssUseSelect; this should be set if needed.
Definition at line 203 of file MasterSyncSerial.hpp.
|
static |
Flags for SPI mode 3.
This does not include MssUseSelect; this should be set if needed.
Definition at line 187 of file MasterSyncSerial.hpp.
|
static |
Flags for SPI mode 3 with the LSb transfered first.
This does not include MssUseSelect; this should be set if needed.
Definition at line 208 of file MasterSyncSerial.hpp.
|
static |
Use a select line to tell a device to pay attention to the master.
Definition at line 120 of file MasterSyncSerial.hpp.
Referenced by duds::hardware::interface::DigitalPinMasterSyncSerial::setChipSelect(), and duds::hardware::interface::DigitalPinMasterSyncSerial::setPins().