BlackSPI.h
Go to the documentation of this file.
1  /*
2 
3  ####################################################################################
4  # BlackLib Library controls Beaglebone Black's inputs and outputs. #
5  # Copyright (C) 2013-2015 by Yigit YUCE #
6  ####################################################################################
7  # #
8  # This file is part of BlackLib library. #
9  # #
10  # BlackLib library is free software: you can redistribute it and/or modify #
11  # it under the terms of the GNU Lesser General Public License as published by #
12  # the Free Software Foundation, either version 3 of the License, or #
13  # (at your option) any later version. #
14  # #
15  # BlackLib library is distributed in the hope that it will be useful, #
16  # but WITHOUT ANY WARRANTY; without even the implied warranty of #
17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
18  # GNU Lesser General Public License for more details. #
19  # #
20  # You should have received a copy of the GNU Lesser General Public License #
21  # along with this program. If not, see <http://www.gnu.org/licenses/>. #
22  # #
23  # For any comment or suggestion please contact the creator of BlackLib Library #
24  # at ygtyce@gmail.com #
25  # #
26  ####################################################################################
27 
28  */
29 
30 
31 
32 
33 
34 #ifndef BLACKSPI_H_
35 #define BLACKSPI_H_
36 
37 
38 #include "../BlackCore.h"
39 
40 #include <cstring>
41 #include <string>
42 #include <fstream>
43 #include <cstdint>
44 #include <unistd.h>
45 
46 #include <fcntl.h>
47 #include <sys/ioctl.h>
48 #include <linux/spi/spidev.h>
49 
50 
51 using std::string;
52 
53 namespace BlackLib
54 {
55 
59  enum spiName { SPI0_0 = 0,
60  SPI0_1 = 1,
61  SPI1_0 = 2,
62  SPI1_1 = 3
63  };
64 
68  enum spiMode { SpiDefault = 0x00,
69  SpiPhase = 0x01,
70  SpiPolarity = 0x02,
72  SpiLsbFirst = 0x08,
73  Spi3Wire = 0x10,
74  SpiLoop = 0x20,
76  SpiReady = 0x80,
77 
78  SpiMode0 = (0|0),
82  };
83 
84 
85 
86 
87 
88 
89 
90 
91 
92  // ######################################### BLACKSPIPROPERTIES DECLARATION STARTS ######################################### //
93 
100  {
101  uint8_t spiBitsPerWord;
102  uint8_t spiMode;
103  uint32_t spiSpeed;
110  {
111  spiBitsPerWord = 0;
112  spiMode = 0;
113  spiSpeed = 0;
114  }
115 
120  BlackSpiProperties(uint8_t bits, uint8_t mode, uint32_t speed)
121  {
122  spiBitsPerWord = bits;
123  spiMode = mode;
124  spiSpeed = speed;
125  }
126 
133  {
134  spiBitsPerWord = self->spiBitsPerWord;
135  spiMode = self->spiMode;
136  spiSpeed = self->spiSpeed;
137  }
138 
144  {
145  spiBitsPerWord = equal.spiBitsPerWord;
146  spiMode = equal.spiMode;
147  spiSpeed = equal.spiSpeed;
148  return *this;
149  }
150  };
151  // ########################################## BLACKSPIPROPERTIES DECLARATION ENDS ########################################### //
152 
153 
154 
155 
156 
157  // ########################################### BLACKSPI DECLARATION STARTS ############################################ //
158 
273  class BlackSPI : virtual private BlackCore
274  {
275  private:
282  std::string spiPortPath;
283  std::string dtSpiFilename;
285  int spiFD;
289  bool isOpenFlag;
299  bool loadDeviceTree();
300 
310  bool findPortPath();
311 
312 
313  public:
317  enum flags { dtErr = 1,
318  openErr = 2,
319  closeErr = 3,
322  modeErr = 6,
323  speedErr = 7,
325  };
326 
349  BlackSPI(spiName spi);
350 
377  BlackSPI(spiName spi, BlackSpiProperties spiProperties);
378 
407  BlackSPI(spiName spi, uint8_t spiBitsPerWord, uint8_t spiMode, uint32_t spiSpeed);
408 
413  virtual ~BlackSPI();
414 
438  bool open(uint openMode);
439 
457  bool close();
458 
488  uint8_t transfer(uint8_t writeByte, uint16_t wait_us = 10);
489 
529  bool transfer(uint8_t *writeBuffer, uint8_t *readBuffer, size_t bufferSize, uint16_t wait_us = 10);
530 
531 
559  bool setBitsPerWord(uint8_t newBitSize);
560 
588  bool setMaximumSpeed(uint32_t newSpeed);
589 
623  bool setMode(uint8_t newMode);
624 
667  bool setProperties(BlackSpiProperties &newProperties);
668 
690  string getPortName();
691 
701  uint8_t getBitsPerWord();
702 
712  uint32_t getMaximumSpeed();
713 
724  uint8_t getMode();
725 
739 
765  bool isOpen();
766 
792  bool isClose();
793 
821  bool fail();
822 
887  bool fail(BlackSPI::flags f);
888 
889  };
890  // ########################################### BLACKSPI DECLARATION ENDS ############################################## //
891 
892 
893 } /* namespace BlackLib */
894 
895 #endif /* BLACKSPI_H_ */
Definition: BlackSPI.h:59
uint8_t spiBitsPerWord
is used to hold the bits per word size of SPI
Definition: BlackSPI.h:101
Definition: BlackSPI.h:62
uint32_t getMaximumSpeed()
Exports maximum speed of spi.
Definition: BlackSPI.cpp:282
uint32_t spiSpeed
is used to hold the speed of SPI
Definition: BlackSPI.h:103
bool loadDeviceTree()
Loads SPI overlay to device tree.
Definition: BlackSPI.cpp:106
BlackSpiProperties & operator=(BlackSpiProperties equal)
Overloaded assign operator of BlackSpiProperties struct.
Definition: BlackSPI.h:143
uint8_t transfer(uint8_t writeByte, uint16_t wait_us=10)
Transfers byte to/from slave.
Definition: BlackSPI.cpp:353
bool open(uint openMode)
Opens TTY file of spi.
Definition: BlackSPI.cpp:166
BlackSpiProperties(BlackSpiProperties *self)
Overloaded constructor of BlackSpiProperties struct.
Definition: BlackSPI.h:132
bool fail()
Is used for general debugging.
Definition: BlackSPI.cpp:448
Definition: BlackSPI.h:60
uint8_t spiMode
is used to hold the mode of SPI
Definition: BlackSPI.h:102
bool isOpenFlag
is used to hold the spi's tty file's state
Definition: BlackSPI.h:289
Definition: BlackSPI.h:74
Definition: BlackSPI.h:75
virtual ~BlackSPI()
Destructor of BlackSPI class.
Definition: BlackSPI.cpp:95
BlackSpiProperties constructorProperties
is used to hold the user specified properties of spi
Definition: BlackSPI.h:276
Definition: BlackSPI.h:322
Interacts with end user, to use SPI.
Definition: BlackSPI.h:273
Definition: BlackSPI.h:61
bool setBitsPerWord(uint8_t newBitSize)
Changes word size of spi.
Definition: BlackSPI.cpp:301
BlackSpiProperties()
Default constructor of BlackSpiProperties struct.
Definition: BlackSPI.h:109
openMode
Definition: BlackDef.h:72
Holds BlackSPI errors.
Definition: BlackErr.h:903
uint8_t getMode()
Exports mode of spi.
Definition: BlackSPI.cpp:248
bool setProperties(BlackSpiProperties &newProperties)
Changes properties of spi.
Definition: BlackSPI.cpp:335
spiName
Definition: BlackSPI.h:59
bool setMode(uint8_t newMode)
Changes mode of spi.
Definition: BlackSPI.cpp:233
Definition: BlackSPI.h:78
Definition: BlackSPI.h:80
string getPortName()
Exports spi's port path.
Definition: BlackSPI.cpp:432
flags
Definition: BlackSPI.h:317
std::string dtSpiFilename
is used to hold the spi's device tree overlay name
Definition: BlackSPI.h:283
bool findPortPath()
Finds SPI's port path and bus number.
Definition: BlackSPI.cpp:127
BlackSpiProperties defaultProperties
is used to hold the default properties of spi
Definition: BlackSPI.h:277
Definition: BlackSPI.h:319
Definition: BlackSPI.h:71
errorSPI * spiErrors
is used to hold the errors of BlackSPI class
Definition: BlackSPI.h:280
int spiChipNumber
is used to hold the spi's chip number
Definition: BlackSPI.h:287
int spiBusNumber
is used to hold the spi's bus number
Definition: BlackSPI.h:286
Definition: BlackSPI.h:320
Definition: BlackSPI.h:324
bool isOpen()
Checks spi's tty file's open state.
Definition: BlackSPI.cpp:437
BlackSPI(spiName spi)
Constructor of BlackSPI class.
Definition: BlackSPI.cpp:40
Holds properties of SPI.
Definition: BlackSPI.h:99
BlackSpiProperties getProperties()
Exports properties of spi.
Definition: BlackSPI.cpp:343
Definition: BlackSPI.h:81
int spiFD
is used to hold the spi's tty file's file descriptor
Definition: BlackSPI.h:285
bool setMaximumSpeed(uint32_t newSpeed)
Changes maximum speed of spi.
Definition: BlackSPI.cpp:267
Definition: BlackSPI.h:72
Definition: BlackSPI.h:317
Definition: BlackSPI.h:70
Definition: BlackSPI.h:69
Definition: BlackSPI.h:318
Definition: BlackADC.cpp:36
Definition: BlackSPI.h:321
bool isCurrentEqDefault
is used to hold the properties of spi is equal to default properties
Definition: BlackSPI.h:288
Definition: BlackSPI.h:79
Definition: BlackSPI.h:68
Definition: BlackSPI.h:76
Definition: BlackSPI.h:323
spiMode
Definition: BlackSPI.h:68
bool close()
Closes TTY file of spi.
Definition: BlackSPI.cpp:211
bool isClose()
Checks spi's tty file's close state.
Definition: BlackSPI.cpp:442
std::string spiPortPath
is used to hold the spi's tty port path
Definition: BlackSPI.h:282
uint8_t getBitsPerWord()
Exports word size of spi.
Definition: BlackSPI.cpp:316
BlackSpiProperties currentProperties
is used to hold the current properties of spi
Definition: BlackSPI.h:278
BlackSpiProperties(uint8_t bits, uint8_t mode, uint32_t speed)
Overloaded constructor of BlackSpiProperties struct.
Definition: BlackSPI.h:120
Definition: BlackSPI.h:73
Base class of the other classes.
Definition: BlackCore.h:80