mrfioc2  2.3.0
spi.cpp
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2017 Michael Davidsaver
3 * mrfioc2 is distributed subject to a Software License Agreement found
4 * in file LICENSE that is included with this distribution.
5 \*************************************************************************/
6 
7 #include <map>
8 
9 #include "mrfCommon.h"
10 #include "mrf/spi.h"
11 
12 namespace mrf {
13 
15  :optimo(3.0)
16 {}
17 
19 
20 void
22 {}
23 
24 void
26  const Operation *ops)
27 {
28  for(size_t n=0; n<nops; n++)
29  {
30  const Operation& op = ops[n];
31 
32  for(size_t i=0; i<op.ncycles; i++) {
33  epicsUInt8 O = this->cycle(op.in ? op.in[i] : 0);
34  if(op.out)
35  op.out[i] = O;
36  }
37  }
38 }
39 
40 double
42 {
43  SCOPED_LOCK(mutex);
44  return optimo;
45 }
46 
47 void
49 {
50  SCOPED_LOCK(mutex);
51  optimo = t;
52 }
53 
54 namespace {
55 struct SPIRegistry {
56  epicsMutex mutex;
57  typedef std::map<std::string, SPIDevice> devices_t;
58  devices_t devices;
59 };
60 SPIRegistry * volatile registry;
61 
62 SPIRegistry *getReg()
63 {
64  SPIRegistry *ret = registry;
65  if(!ret) {
66  ret = new SPIRegistry;
67  registry = ret;
68  }
69  return ret;
70 }
71 
72 } //namespace
73 
74 bool SPIDevice::lookupDev(const std::string& name, SPIDevice *dev)
75 {
76  SPIRegistry *reg(getReg());
77  SCOPED_LOCK2(reg->mutex,G);
78  SPIRegistry::devices_t::const_iterator it = reg->devices.find(name);
79  if(it!=reg->devices.end()) {
80  *dev = it->second;
81  return true;
82  }
83  return false;
84 }
85 
86 void SPIDevice::registerDev(const std::string& name, const SPIDevice &dev)
87 {
88  SPIRegistry *reg(getReg());
89  SCOPED_LOCK2(reg->mutex,G);
90  reg->devices[name] = dev;
91 }
92 
93 void SPIDevice::unregisterDev(const std::string& name)
94 {
95  SPIRegistry *reg(getReg());
96  SCOPED_LOCK2(reg->mutex,G);
97  reg->devices.erase(name);
98 }
99 
100 } // namespace mrf
static void registerDev(const std::string &name, const SPIDevice &)
Definition: spi.cpp:86
double timeout() const
timeout in seconds for an individual cycle()
Definition: spi.cpp:41
virtual epicsUInt8 cycle(epicsUInt8 in)=0
virtual void cycles(size_t nops, const Operation *ops)
Definition: spi.cpp:25
virtual ~SPIInterface()
Definition: spi.cpp:18
epicsMutex mutex
Definition: spi.h:46
epicsUInt8 * out
Definition: spi.h:35
devices_t devices
Definition: spi.cpp:58
const epicsUInt8 * in
Definition: spi.h:34
static void unregisterDev(const std::string &name)
Definition: spi.cpp:93
void setTimeout(double t)
Definition: spi.cpp:48
static bool lookupDev(const std::string &name, SPIDevice *)
Definition: spi.cpp:74
virtual void select(unsigned id)=0
Select numbered device. 0 clears selection.
Definition: spi.cpp:21
Definition: flash.cpp:23