distortos  v0.5.0
object-oriented C++ RTOS for microcontrollers
SpiDevice.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_DEVICES_COMMUNICATION_SPIDEVICE_HPP_
13 #define INCLUDE_DISTORTOS_DEVICES_COMMUNICATION_SPIDEVICE_HPP_
14 
17 
19 #include "distortos/Mutex.hpp"
20 
21 namespace distortos
22 {
23 
24 class Thread;
25 
26 namespace devices
27 {
28 
29 class OutputPin;
30 class SpiMaster;
31 
38 class SpiDevice
39 {
40 public:
41 
54  constexpr SpiDevice(SpiMaster& spiMaster, OutputPin& slaveSelectPin, const SpiMode mode,
55  const uint32_t maxClockFrequency, const uint8_t wordLength, const bool lsbFirst) :
58  maxClockFrequency_{maxClockFrequency},
59  owner_{},
60  slaveSelectPin_{slaveSelectPin},
61  spiMaster_{spiMaster},
62  lsbFirst_{lsbFirst},
63  mode_{mode},
64  openCount_{},
65  wordLength_{wordLength}
66  {
67 
68  }
69 
78  ~SpiDevice();
79 
92  int close();
93 
110  std::pair<int, size_t> executeTransaction(SpiMasterOperationRange operationRange);
111 
117  bool getLsbFirst() const
118  {
119  return lsbFirst_;
120  }
121 
126  uint32_t getMaxClockFrequency() const
127  {
128  return maxClockFrequency_;
129  }
130 
135  SpiMode getMode() const
136  {
137  return mode_;
138  }
139 
145  {
146  return slaveSelectPin_;
147  }
148 
153  uint8_t getWordLength() const
154  {
155  return wordLength_;
156  }
157 
172  bool lock();
173 
186  int open();
187 
200  void unlock(bool previousLockState);
201 
202 private:
203 
211  bool lockInternal();
212 
221  void unlockInternal(bool previousLockState);
222 
225 
228 
231 
233  const Thread* owner_;
234 
237 
240 
243  bool lsbFirst_;
244 
247 
249  uint8_t openCount_;
250 
252  uint8_t wordLength_;
253 };
254 
255 } // namespace devices
256 
257 } // namespace distortos
258 
259 #endif // INCLUDE_DISTORTOS_DEVICES_COMMUNICATION_SPIDEVICE_HPP_
uint32_t maxClockFrequency_
max clock frequency supported by SPI slave device, Hz
Definition: SpiDevice.hpp:230
priority inheritance protocol, similar to PTHREAD_PRIO_INHERIT
normal mutex, similar to PTHREAD_MUTEX_NORMAL
ConditionVariable is an advanced synchronization primitive.
Definition: ConditionVariable.hpp:35
SpiMode getMode() const
Definition: SpiDevice.hpp:135
constexpr SpiDevice(SpiMaster &spiMaster, OutputPin &slaveSelectPin, const SpiMode mode, const uint32_t maxClockFrequency, const uint8_t wordLength, const bool lsbFirst)
SpiDevice&#39;s constructor.
Definition: SpiDevice.hpp:54
OutputPin & slaveSelectPin_
reference to slave select pin of this SPI slave device
Definition: SpiDevice.hpp:236
std::pair< int, size_t > executeTransaction(SpiMasterOperationRange operationRange)
Executes series of operations as a single atomic transaction.
Definition: SpiDevice.cpp:80
Mutex mutex_
mutex used to serialize access to this object
Definition: SpiDevice.hpp:227
uint32_t getMaxClockFrequency() const
Definition: SpiDevice.hpp:126
SpiMaster & spiMaster_
reference to SPI master to which this SPI slave device is connected
Definition: SpiDevice.hpp:239
SpiMode enum class header.
ConditionVariable class header.
bool getLsbFirst() const
Definition: SpiDevice.hpp:117
Definition: OutputPin.hpp:29
SpiMode
Definition: SpiMode.hpp:29
bool lsbFirst_
Definition: SpiDevice.hpp:243
int close()
Closes SPI device.
Definition: SpiDevice.cpp:55
SpiMode mode_
SPI mode used by SPI slave device.
Definition: SpiDevice.hpp:246
Mutex class header.
Definition: SpiDevice.hpp:38
void unlockInternal(bool previousLockState)
Internal version of unlock() - without locking the mutex.
Definition: SpiDevice.cpp:172
uint8_t openCount_
number of times this device was opened but not yet closed
Definition: SpiDevice.hpp:249
OutputPin & getSlaveSelectPin() const
Definition: SpiDevice.hpp:144
void unlock(bool previousLockState)
Unlocks the object that was previously locked by current thread.
Definition: SpiDevice.cpp:139
Top-level namespace of distortos project.
Mutex is the basic synchronization primitive.
Definition: Mutex.hpp:30
const Thread * owner_
pointer to thread that locked this object
Definition: SpiDevice.hpp:233
bool lockInternal()
Internal version of lock() - without locking the mutex.
Definition: SpiDevice.cpp:155
ConditionVariable conditionVariable_
condition variable used for locking access to this object
Definition: SpiDevice.hpp:224
bool lock()
Locks the object for exclusive use by current thread using condition variable and mutex...
Definition: SpiDevice.cpp:102
uint8_t wordLength_
word length used by SPI slave device, bits
Definition: SpiDevice.hpp:252
Definition: SpiMaster.hpp:37
int open()
Opens SPI device.
Definition: SpiDevice.cpp:114
uint8_t getWordLength() const
Definition: SpiDevice.hpp:153
Thread class is a pure abstract interface for threads.
Definition: Thread.hpp:32
SpiMasterOperationRange type alias header.
~SpiDevice()
SpiDevice&#39;s destructor.
Definition: SpiDevice.cpp:36