Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
spi.hpp
1 /*
2  Title --- driver/spi.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_DRIVER_SPI_HPP_
23 #define _RPI_HW_DRIVER_SPI_HPP_
24 
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #include <linux/spi/spidev.h>
29 #include <cstring>
30 
31 #include <rpi-hw/rpi.hpp>
32 
33 #include <rpi-hw/types.hpp>
34 #include <rpi-hw/exception.hpp>
35 #include <rpi-hw/utils.hpp>
36 
37 namespace rpihw { // Begin main namespace
38 
39 namespace driver { // Begin drivers namespace
40 
41 /*!
42  @class spi
43  @brief Serial Peripheral Interface.
44 */
45 class spi {
46 
47 public:
48 
49  //! The SPI modes (see: https://www.kernel.org/doc/Documentation/spi/spi-summary).
50  enum Modes {
51 
52  MODE_0 = SPI_MODE_0, //!< Sets CPOL=0, SPI_CPHA=0.
53  MODE_1 = SPI_MODE_1, //!< Sets CPOL=0, SPI_CPHA=1.
54  MODE_2 = SPI_MODE_2, //!< Sets CPOL=1, SPI_CPHA=0.
55  MODE_3 = SPI_MODE_3 //!< Sets CPOL=1, SPI_CPHA=1.
56  };
57 
58  /*!
59  @brief Constructor method.
60  @param[in] dev_path The device path.
61  @param[in] mode The SPI mode.
62  @param[in] word_len The device word length.
63  @param[in] speed The device max speed (hz).
64  */
65  spi( const std::string &dev_path, Modes mode, uint8_t word_len, uint32_t speed );
66 
67  //! Destructor method.
68  virtual ~spi();
69 
70  /*!
71  @brief Sets the SPI mode.
72  @param[in] mode The new SPI mode.
73  */
74  void setMode( uint8_t mode );
75 
76  /*!
77  @brief Returns the SPI mode.
78  @return The SPI mode.
79  */
80  uint8_t getMode() const;
81 
82  /*!
83  @brief Sets the device word length.
84  @param[in] word_len The bits per word.
85  */
86  void setWordLen( uint8_t word_len );
87 
88  /*!
89  @brief Returns the device word length.
90  @return The bits per word.
91  */
92  uint8_t getWordLen() const;
93 
94  /*!
95  @brief Sets the device max speed.
96  @param[in] speed The new device speed.
97  */
98  void setSpeed( uint32_t speed );
99 
100  /*!
101  @brief Returns the device max speed.
102  @return The new device speed.
103  */
104  uint32_t getSpeed() const;
105 
106  //! Sends/receives a data buffer.
107  void transfer( uint8_t *data, size_t size );
108 
109 private:
110 
111  //! The device path.
112  std::string m_dev_path;
113 
114  //! SPI mode.
115  uint8_t m_mode;
116 
117  //! Device word length.
118  uint8_t m_word_len;
119 
120  //! Device max speed (hz).
121  uint32_t m_speed;
122 
123  //! File descriptor of the device.
124  int m_dev_fd;
125 };
126 
127 } // End of drivers namespace
128 
129 } // End of main namespace
130 
131 
132 // Include inline methods
133 #include <rpi-hw/driver/spi-inl.hpp>
134 
135 #endif /* _RPI_HW_DRIVER_SPI_HPP_ */
void setMode(uint8_t mode)
Sets the SPI mode.
int m_dev_fd
File descriptor of the device.
Definition: spi.hpp:124
std::string m_dev_path
The device path.
Definition: spi.hpp:112
spi(const std::string &dev_path, Modes mode, uint8_t word_len, uint32_t speed)
Constructor method.
Sets CPOL=1, SPI_CPHA=0.
Definition: spi.hpp:54
uint32_t m_speed
Device max speed (hz).
Definition: spi.hpp:121
void setSpeed(uint32_t speed)
Sets the device max speed.
virtual ~spi()
Destructor method.
Sets CPOL=1, SPI_CPHA=1.
Definition: spi.hpp:55
uint8_t m_mode
SPI mode.
Definition: spi.hpp:115
uint32_t getSpeed() const
Returns the device max speed.
Definition: spi-inl.hpp:44
Sets CPOL=0, SPI_CPHA=1.
Definition: spi.hpp:53
uint8_t getWordLen() const
Returns the device word length.
Definition: spi-inl.hpp:37
void setWordLen(uint8_t word_len)
Sets the device word length.
void transfer(uint8_t *data, size_t size)
Sends/receives a data buffer.
Serial Peripheral Interface.
Definition: spi.hpp:45
Modes
The SPI modes (see: https://www.kernel.org/doc/Documentation/spi/spi-summary).
Definition: spi.hpp:50
uint8_t m_word_len
Device word length.
Definition: spi.hpp:118
uint8_t getMode() const
Returns the SPI mode.
Definition: spi-inl.hpp:30
Sets CPOL=0, SPI_CPHA=0.
Definition: spi.hpp:52