DUDS
Distributed Update of Data from Something
MasterSyncSerial.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
10 #ifndef MASTERSYNCSERIAL_HPP
11 #define MASTERSYNCSERIAL_HPP
12 
13 #include <cstdint>
14 #include <memory>
15 #include <boost/noncopyable.hpp>
19 
20 namespace duds { namespace hardware { namespace interface {
21 
22 class MasterSyncSerialAccess;
23 
95 class MasterSyncSerial : public Conversationalist, boost::noncopyable,
96 public std::enable_shared_from_this<MasterSyncSerial> {
111  void retire(MasterSyncSerialAccess *acc);
112 public:
120  static constexpr Flags MssUseSelect = Flags(1);
125  static constexpr Flags MssClockIdleHigh = Flags(2);
129  static constexpr Flags MssOutFallInRise = Flags(4);
133  static constexpr Flags MssMSbFirst = Flags(8);
137  static constexpr Flags MssFullDuplex = Flags(16);
142  static constexpr Flags MssConfigMask = Flags(31);
150  static constexpr Flags MssReady = Flags(32);
155  static constexpr Flags MssOpen = Flags(64);
160  static constexpr Flags MssCommunicating = Flags(128);
165  static constexpr Flags MssFirstDerivedClassFlag = Flags(256);
170  static constexpr Flags MssSpiMode0 = MssMSbFirst | MssOutFallInRise
171  | MssFullDuplex;
176  static constexpr Flags MssSpiMode1 = MssMSbFirst | MssFullDuplex;
181  static constexpr Flags MssSpiMode2 = MssMSbFirst | MssClockIdleHigh
182  | MssFullDuplex;
187  static constexpr Flags MssSpiMode3 = MssMSbFirst | MssClockIdleHigh |
188  MssOutFallInRise | MssFullDuplex;
193  static constexpr Flags MssSpiMode0LSb = MssOutFallInRise | MssFullDuplex;
198  static constexpr Flags MssSpiMode1LSb = MssFullDuplex;
203  static constexpr Flags MssSpiMode2LSb = MssClockIdleHigh | MssFullDuplex;
208  static constexpr Flags MssSpiMode3LSb = MssClockIdleHigh
209  | MssOutFallInRise | MssFullDuplex;
210 protected:
218  unsigned int minHalfPeriod;
222  Flags flags;
230  void forceClose();
239  virtual void open() = 0;
247  virtual void close() = 0;
258  virtual void start() = 0;
268  virtual void stop() = 0;
278  void condStart();
286  void condStop();
294  void clockFrequency(unsigned int freq);
302  void clockPeriod(unsigned int nanos);
323  virtual void transfer(
324  const std::uint8_t * __restrict__ out,
325  std::uint8_t * __restrict__ in,
327  ) = 0;
346  virtual void transmit(const std::uint8_t *buff, duds::general::Bits bits);
366  virtual void receive(std::uint8_t *buff, duds::general::Bits bits);
380 public:
385  MasterSyncSerial() : minHalfPeriod(0), flags(0) { }
391  MasterSyncSerial(Flags flags, int period);
399  virtual ~MasterSyncSerial() = 0;
404  Flags configFlags() const noexcept {
405  return flags & MssConfigMask;
406  }
411  bool inUse() const noexcept {
412  return mssacc != nullptr;
413  }
418  unsigned int clockPeriod() const noexcept {
419  return minHalfPeriod << 1;
420  }
425  unsigned int clockFrequency() const noexcept;
438  std::unique_ptr<MasterSyncSerialAccess> access();
454  void access(MasterSyncSerialAccess &acc);
468  std::unique_ptr<MasterSyncSerialAccess> accessStart();
501  virtual void converse(Conversation &conv);
502 };
503 
504 } } } // namespaces
505 
506 #endif // #ifndef MASTERSYNCSERIAL_HPP
unsigned int clockFrequency() const noexcept
Computes and returns the maximum clock frequency in Hertz.
MasterSyncSerial()
Builds a MasterSyncSerial with an invalid clock period and all configuration flags clear...
static constexpr Flags MssSpiMode2LSb
Flags for SPI mode 2 with the LSb transfered first.
virtual void open()=0
Transitions the object from the ready (MssReady) to the open (MssOpen) state.
virtual void close()=0
Transitions the object from the open (MssOpen) to the ready (MssReady) state.
void condStop()
Calls stop() if currently communicating (set MssCommunicating flag).
static constexpr Flags MssSpiMode2
Flags for SPI mode 2.
bool inUse() const noexcept
Returns true when this serial interface is in use by checking for the existence of an access object...
static constexpr Flags MssMSbFirst
Send data MSb first, little endian.
static constexpr Flags MssFullDuplex
Communication is full duplex.
static constexpr Flags MssCommunicating
Indicates that communication is underway.
static constexpr Flags MssSpiMode1
Flags for SPI mode 1.
std::unique_ptr< MasterSyncSerialAccess > access()
Obtain access for communication; transitions the object from the ready (MssReady) to the open (MssOpe...
static constexpr Flags MssSpiMode1LSb
Flags for SPI mode 1 with the LSb transfered first.
duds::general::BitFlags< struct MssFlags > Flags
Configuration flags for various synchronous serial options.
unsigned int clockPeriod() const noexcept
Returns the minimum clock period in nanoseconds.
virtual void transmit(const std::uint8_t *buff, duds::general::Bits bits)
Sends bits of data.
static constexpr Flags MssConfigMask
All the flags that are used for configuration rather than the current state.
void converseAlreadyOpen(Conversation &conv)
Has a half-duplex Conversation with the connected device.
static constexpr Flags MssSpiMode3
Flags for SPI mode 3.
Allows a common interface for using Conversation objects for communication.
virtual void converse(Conversation &conv)
Has a half-duplex Conversation with the connected device.
static constexpr Flags MssUseSelect
Use a select line to tell a device to pay attention to the master.
unsigned int minHalfPeriod
The minimum time between changing the clock edge in nanoseconds.
virtual ~MasterSyncSerial()=0
Derived class destructors should assure that communication has stopped by calling forceClose()...
static constexpr Flags MssSpiMode0
Flags for SPI mode 0.
std::unique_ptr< MasterSyncSerialAccess > accessStart()
Obtain access for communication; transitions the object from the ready (MssReady) to the communicatin...
static constexpr Flags MssOpen
Indicates that all required resources for communication have been acquired.
static constexpr Flags MssSpiMode0LSb
Flags for SPI mode 0 with the LSb transfered first.
virtual void stop()=0
Denotes the end of a conversation; transitions from the communicating state to the open state...
static std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > conv
String converter; UTF-8 to/from UTF-32.
Definition: BppFont.cpp:165
static constexpr Flags MssSpiMode3LSb
Flags for SPI mode 3 with the LSb transfered first.
Flags configFlags() const noexcept
Returns the current set of configuration flags.
An abstraction for the master side of a simple synchronous serial communication connection to some de...
Provides access for communicating using a MasterSyncSerial object.
static constexpr Flags MssReady
Indictates that all required resources for communication have been identified and passed a validity c...
static constexpr Flags MssOutFallInRise
Output on the falling edge of the clock and read on the rising edge.
A class to assist with specifiying the sizes of data with scaling units, much like std::chrono::durat...
Definition: DataSize.hpp:106
void forceClose()
Attempts to forcibly cease communications by calling condStop() and close().
virtual void start()=0
Denotes the start of a conversation; transitions from the open state to the communicating state...
MasterSyncSerialAccess * mssacc
A pointer to the current access object or nullptr.
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.
static constexpr Flags MssClockIdleHigh
Before communication begins, and after it ends, the clock line should have a high logic level...
virtual void receive(std::uint8_t *buff, duds::general::Bits bits)
Receives bits of data.
static constexpr Flags MssFirstDerivedClassFlag
The first flag that may be defined by a derived class.
void condStart()
Calls start() if not currently communicating (clear MssCommunicating flag).
void retire(MasterSyncSerialAccess *acc)
Removes the access object from use.
Represents a two-way conversation with a device.