libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
Spi.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef SPI_H_
32 #define SPI_H_
33 
34 #ifdef SPISUPPORT
35 
36 #include "../Exception.h"
37 
38 #include <type_traits>
39 #include <vector>
40 #include <mutex>
41 #include <memory>
42 
43 #include <linux/spi/spidev.h>
44 #include <sys/ioctl.h>
45 
46 namespace BaseLib
47 {
48 
49 class SharedObjects;
50 class FileDescriptor;
51 
52 namespace LowLevel
53 {
54 
60 class SpiException : public Exception
61 {
62 public:
63  SpiException(std::string message) : Exception(message) {}
64 };
65 
66 enum class SpiModes : uint8_t
67 {
68  none = 0,
69 
73  loop = SPI_LOOP,
74 
78  cpha = SPI_CPHA,
79 
83  cpol = SPI_CPOL,
84 
88  lsbFirst = SPI_LSB_FIRST,
89 
93  csHigh = SPI_CS_HIGH,
94 
98  threeWire = SPI_3WIRE,
99 
103  noCs = SPI_NO_CS,
104 
108  ready = SPI_READY
109 };
110 
111 inline SpiModes operator|(SpiModes lhs, SpiModes rhs)
112 {
113  return (SpiModes)(static_cast<std::underlying_type<SpiModes>::type>(lhs) | static_cast<std::underlying_type<SpiModes>::type>(rhs));
114 }
115 
116 inline SpiModes& operator|=(SpiModes& lhs, SpiModes rhs)
117 {
118  lhs = (SpiModes)(static_cast<std::underlying_type<SpiModes>::type>(lhs) | static_cast<std::underlying_type<SpiModes>::type>(rhs));
119  return lhs;
120 }
121 
122 inline SpiModes operator&(SpiModes lhs, SpiModes rhs)
123 {
124  return (SpiModes)(static_cast<std::underlying_type<SpiModes>::type>(lhs) & static_cast<std::underlying_type<SpiModes>::type>(rhs));
125 }
126 
127 inline SpiModes& operator&=(SpiModes& lhs, SpiModes rhs)
128 {
129  lhs = (SpiModes)(static_cast<std::underlying_type<SpiModes>::type>(lhs) & static_cast<std::underlying_type<SpiModes>::type>(rhs));
130  return lhs;
131 }
132 
133 class Spi
134 {
135 public:
145  Spi(BaseLib::SharedObjects* baseLib, std::string device, SpiModes mode, uint8_t bitsPerWord, uint32_t speed);
146 
147  virtual ~Spi();
148 
149  void open();
150  void close();
151  void readwrite(std::vector<uint8_t>& data);
152 
153  bool isOpen();
154 protected:
155  BaseLib::SharedObjects* _bl = nullptr;
156  std::shared_ptr<FileDescriptor> _fileDescriptor;
157  std::string _lockfile;
158  std::mutex _sendMutex;
159  struct spi_ioc_transfer _transfer;
160 
161  std::string _device;
162  SpiModes _mode = SpiModes::none;
163  uint8_t _bitsPerWord = 8;
164  uint32_t _speed = 1000000;
165 
166  void setup();
167 };
168 
169 }
170 }
171 #endif
172 #endif
This is the base library main class.
Definition: BaseLib.h:95
Definition: BaseLib.cpp:34