DUDS
Distributed Update of Data from Something
PinConfiguration.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) 2018 Jeff Jackowski
9  */
10 #include <boost/property_tree/ptree_fwd.hpp>
11 #include <boost/multi_index_container.hpp>
12 #include <boost/multi_index/ordered_index.hpp>
13 #include <boost/multi_index/sequenced_index.hpp>
14 #include <boost/multi_index/hashed_index.hpp>
15 #include <boost/multi_index/member.hpp>
19 #include <unordered_map>
20 #include <set>
21 
22 namespace duds { namespace hardware { namespace interface {
23 
24 class ChipSelectManager;
25 class DigitalPort;
26 
33 struct PinConfigurationError : virtual std::exception, virtual boost::exception { };
111 
115 typedef boost::error_info<struct Info_PortName, std::string>
120 typedef boost::error_info<struct Info_PortId, std::string>
125 typedef boost::error_info<struct Info_PortPinId, unsigned int>
130 typedef boost::error_info<struct Info_SelectManagerName, std::string>
135 typedef boost::error_info<struct Info_SelectName, std::string>
140 typedef boost::error_info<struct Info_SelectBadState, std::string>
145 typedef boost::error_info<struct Info_SelectBadType, std::string>
150 typedef boost::error_info<struct Info_SetName, std::string>
152 
188 public:
189  struct Port;
193  struct Pin {
201  std::string name;
205  unsigned int gid;
209  unsigned int pid;
213  static constexpr unsigned int NoPin = -1;
218  static constexpr unsigned int NoIdSpecified = -2;
223  void parse(
224  const std::pair<const std::string, boost::property_tree::ptree> &item,
225  Port *owner
226  );
227  Pin();
233  Pin(
234  const std::pair<const std::string, boost::property_tree::ptree> &item,
235  Port *owner
236  );
237  };
243  struct index_gid { };
250  struct index_pid { };
256  struct index_name { };
261  struct index_seq { };
266  typedef boost::multi_index::multi_index_container<
267  Pin,
268  boost::multi_index::indexed_by<
269  boost::multi_index::ordered_non_unique<
270  boost::multi_index::tag<index_gid>,
271  boost::multi_index::member<Pin, unsigned int, &Pin::gid>
272  >,
273  boost::multi_index::ordered_non_unique<
274  boost::multi_index::tag<index_pid>,
275  boost::multi_index::member<Pin, unsigned int, &Pin::pid>
276  >,
277  boost::multi_index::hashed_non_unique<
278  boost::multi_index::tag<index_name>,
279  boost::multi_index::member<Pin, std::string, &Pin::name>
280  >,
281  boost::multi_index::sequenced<
282  boost::multi_index::tag<index_seq>
283  >
284  >
285  > Pins;
289  struct Port {
294  std::shared_ptr<DigitalPort> dport;
309  std::string typeval;
314  unsigned int idOffset;
315  Port();
316  void parse(
317  const std::pair<const std::string, boost::property_tree::ptree> &item
318  );
323  const PinConfiguration::Pins::index<index_gid>::type &gidIndex() const {
324  return pins.get<index_gid>();
325  }
330  const PinConfiguration::Pins::index<index_pid>::type &pidIndex() const {
331  return pins.get<index_pid>();
332  }
337  const PinConfiguration::Pins::index<index_seq>::type &seqIndex() const {
338  return pins.get<index_seq>();
339  }
340  };
341  typedef std::unordered_map<std::string, Port> PortMap;
345  struct SelMgr {
350  std::shared_ptr<ChipSelectManager> csm;
355  std::vector<unsigned int> pins;
360  std::unordered_map<std::string, unsigned int> selNames;
368  enum MgrType {
389  };
394  union {
398  std::uint32_t selStates;
404  };
405  SelMgr();
406  SelMgr(
407  const std::pair<const std::string, boost::property_tree::ptree> &item,
408  const PinConfiguration *pinconf
409  );
410  };
411  typedef std::unordered_map<std::string, SelMgr> SelMgrMap;
415  struct ChipSel {
429  int chipId;
430  ChipSel() = default;
431  ChipSel(SelMgr *m, int id);
432  };
433  typedef std::unordered_map<std::string, ChipSel> ChipSelMap;
437  struct PinSet {
452  std::string selName;
457  PinSet() = default;
458  PinSet(
459  const std::pair<const std::string, boost::property_tree::ptree> &item,
460  const PinConfiguration *pinconf
461  );
466  const PinConfiguration::Pins::index<index_seq>::type &seqIndex() const {
467  return pins.get<index_seq>();
468  }
469  PinConfiguration::Pins::index<index_seq>::type::iterator begin() {
470  return pins.get<index_seq>().begin();
471  }
472  PinConfiguration::Pins::index<index_seq>::type::iterator end() {
473  return pins.get<index_seq>().end();
474  }
475  };
476  typedef std::unordered_map<std::string, PinSet> PinSetMap;
477 private:
485  PortMap ports;
489  SelMgrMap selMgrs;
493  ChipSelMap chipSels;
497  PinSetMap pinSets;
498 public:
502  PinConfiguration() = default;
528  PinConfiguration(const boost::property_tree::ptree &pt);
553  void parse(const boost::property_tree::ptree &pt);
570  void attachPort(
571  const std::shared_ptr<DigitalPort> &dp,
572  const std::string &name = "default"
573  );
582  const Pin &pin(const std::string &str) const;
592  unsigned int pinGlobalId(const std::string &str) const {
593  return pin(str).gid;
594  }
601  const Port &port(const std::string &name = "default") const;
611  const PinSet &pinSet(const std::string &name) const;
621  const ChipSel &chipSelect(const std::string &name) const;
632  const SelMgr &selectManager(const std::string &name) const;
659  void getPinSetAndSelect(
660  DigitalPinSet &dpset,
661  ChipSelect &sel,
662  const std::string &setName
663  ) const;
676  const DigitalPinSet &getPinSet(const std::string &setName) const;
690  const ChipSelect &getSelect(const std::string &selName) const;
704  DigitalPin getPin(const std::string &pinName) const;
715  bool getPin(DigitalPin &dest, const std::string &pinName) const;
722  bool havePin(const std::string &pinName) const;
729  bool haveChipSelect(const std::string &name) const {
730  return chipSels.count(name) > 0;
731  }
735  PortMap::const_iterator portsBegin() const {
736  return ports.cbegin();
737  }
741  PortMap::const_iterator portsEnd() const {
742  return ports.cend();
743  }
747  SelMgrMap::const_iterator selectManagersBegin() const {
748  return selMgrs.cbegin();
749  }
753  SelMgrMap::const_iterator selectManagersEnd() const {
754  return selMgrs.cend();
755  }
759  ChipSelMap::const_iterator selectsBegin() const {
760  return chipSels.cbegin();
761  }
765  ChipSelMap::const_iterator selectsEnd() const {
766  return chipSels.cend();
767  }
771  PinSetMap::const_iterator pinSetsBegin() const {
772  return pinSets.cbegin();
773  }
777  PinSetMap::const_iterator pinSetsEnd() const {
778  return pinSets.cend();
779  }
780 };
781 
782 } } }
PinConfiguration::Pins::index< index_seq >::type::iterator end()
PortMap::const_iterator portsEnd() const
Ending iterator to inspect the ports in the configuration.
boost::error_info< struct Info_PortId, std::string > PinBadId
A pin name or number from the configuration that could not be used.
const PinConfiguration::Pins::index< index_gid >::type & gidIndex() const
Convenience function that provides the pin global ID index for the port&#39;s pins.
std::unordered_map< std::string, SelMgr > SelMgrMap
PortMap ports
Port configurations stored by name.
Holds configuration data for a single chip select manager.
std::unordered_map< std::string, ChipSel > ChipSelMap
Represents a single pin on a DigitalPort.
Definition: DigitalPin.hpp:22
boost::error_info< struct Info_PortPinId, unsigned int > PortPinId
The global ID of a pin from a configuration that is involved in the error.
A pin set was defined with pins from more than one port.
A required chip select manager has not yet been created most likely because the port providing its pi...
const PinConfiguration::Pins::index< index_seq >::type & seqIndex() const
Convenience function that provides the sequential index for the sets&#39;s pins.
std::uint32_t selStates
Selection state bitmap for pin set manager.
unsigned int idOffset
The pin ID offset for the port; used to translate between global and port pin IDs.
A selection logic state in the configuration could not be parsed.
ChipSelect sel
The chip select object for this configuration.
PinSetMap pinSets
Pin set configurations stored by name.
int chipId
The chip ID that is selected by this chip select.
Port * usePort
The port that will provide the pins for this select manager.
SelMgrMap::const_iterator selectManagersEnd() const
Ending iterator to inspect the select managers in the configuration.
Holds configuration data for a single digital port.
boost::error_info< struct Info_SetName, std::string > SetName
The name of a pin set from a configuration.
A required pin set has not yet been created most likely because the port providing its pins has not y...
const PinConfiguration::Pins::index< index_seq >::type & seqIndex() const
Convenience function that provides the sequential index for the port&#39;s pins.
Base for all errors directly thrown by PinConfiguration.
bool haveChipSelect(const std::string &name) const
True if the named chip select has been found in the already parsed configuration. ...
ChipSelMap::const_iterator selectsBegin() const
Begining iterator to inspect the chip selects in the configuration.
A given pin ID cannot be used with the port, such as an ID that is less than the port&#39;s ID offset...
std::shared_ptr< DigitalPort > dport
The attached DigitalPort.
Port * usePort
The port that will provide the pins for this set.
SelMgrMap selMgrs
Select manager configurations stored by name.
std::unordered_map< std::string, PinSet > PinSetMap
Pins pins
The pins described by the configuration file.
MgrType type
The type of chip select manager requested.
PinConfiguration::Pins::index< index_seq >::type::iterator begin()
boost::multi_index::multi_index_container< Pin, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< boost::multi_index::tag< index_gid >, boost::multi_index::member< Pin, unsigned int, &Pin::gid > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< index_pid >, boost::multi_index::member< Pin, unsigned int, &Pin::pid > >, boost::multi_index::hashed_non_unique< boost::multi_index::tag< index_name >, boost::multi_index::member< Pin, std::string, &Pin::name > >, boost::multi_index::sequenced< boost::multi_index::tag< index_seq > > > > Pins
Holds the configuration data for digital pins indexed by global ID, port ID, arbitrary name...
Index in type Pins that is maintained by parsing order.
MgrType
The type of chip select manager requested.
The configuration gives the same name to more than one chip select.
boost::error_info< struct Info_SelectBadState, std::string > SelectBadState
The string with a select logic state that could not be parsed.
std::string typeval
A hint as to what DigitalPort implementation should be used.
The configuration gives the same name to more than one chip select manager.
An object to wrap together a ChipSelectManager and chip ID to simplify code that needs to repeatedly ...
Definition: ChipSelect.hpp:24
boost::error_info< struct Info_PortName, std::string > PortName
The name of the port as it appears in the configuration.
bool initSelHigh
True for high selection state with binary manager, or for initially selected state with pin manager...
Index in type Pins that is sorted by the optional pin name.
A chip selection manager was defined with pins from more than one port.
Port * parent
The Port object that supplies the pin.
ChipSelMap chipSels
Chip select configurations stored by name.
Represents a set of pins on a single DigitalPort.
PinSetMap::const_iterator pinSetsBegin() const
Begining iterator to inspect the pin sets in the configuration.
The configuration gives the same name to more than one port.
A select manager was given an unknown type, or no type.
std::shared_ptr< ChipSelectManager > csm
The select manager based on this configuration.
Parses configuration data for DigitalPort, DigitalPin, DigitalPinSet, ChipSelectManager, and ChipSelect objects.
DigitalPinSet dpSet
The DigitalPinSet for this configuration.
Index in type Pins that is sorted by pin global ID.
Index in type Pins that is sorted by pin port ID.
std::unordered_map< std::string, Port > PortMap
const PinConfiguration::Pins::index< index_pid >::type & pidIndex() const
Convenience function that provides the pin local ID index for the port&#39;s pins.
A chip selection manager was defined without any pins.
The same pin global ID is used for more than one pin.
SelMgr * mgr
The chip select manager configuration for this object.
Holds configuration data for a single digital I/O pin.
PortMap::const_iterator portsBegin() const
Begining iterator to inspect the ports in the configuration.
PinSetMap::const_iterator pinSetsEnd() const
Ending iterator to inspect the pin sets in the configuration.
SelMgrMap::const_iterator selectManagersBegin() const
Begining iterator to inspect the select managers in the configuration.
boost::error_info< struct Info_SelectManagerName, std::string > SelectManagerName
The name of a chip select manager from a configuration.
std::string selName
The name of an optional chip select that has been associated with this pin set.
std::unordered_map< std::string, unsigned int > selNames
A mapping of a name for a chip select to the chip ID used by the select manager.
The requested chip select does not exist.
The requested chip select manager is not named in the configuration.
unsigned int pinGlobalId(const std::string &str) const
Finds the global ID of the given pin according to this pin configuration.
ChipSelMap::const_iterator selectsEnd() const
Ending iterator to inspect the chip selects in the configuration.
Holds configuration data for a single chip select.
boost::error_info< struct Info_SelectBadType, std::string > SelectBadType
The string with a chip selection manager type that is not valid.
Pins allpins
All pins mentioned in the confiuration across all ports.
Holds configuration data for a single digital pin set.
The configuration gives the same name to more than one pin set.
A given pin ID could not be parsed.
The requested pin set is not defined by the configuration.
boost::error_info< struct Info_SelectName, std::string > SelectName
The name of a chip select from a configuration.
std::vector< unsigned int > pins
The global IDs of the pins that this select manager should use.
The requested port is not named in the configuration.