Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
i2c.hpp
1 /*
2  Title --- driver/i2c.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_I2C_HPP_
23 #define _RPI_HW_DRIVER_I2C_HPP_
24 
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #include <linux/i2c.h>
29 #include <linux/i2c-dev.h>
30 #include <cstring>
31 
32 #include <rpi-hw/rpi.hpp>
33 
34 #include <rpi-hw/types.hpp>
35 #include <rpi-hw/exception.hpp>
36 #include <rpi-hw/utils.hpp>
37 
38 namespace rpihw { // Begin main namespace
39 
40 namespace driver { // Begin drivers namespace
41 
42 /*!
43  @class i2c
44  @brief Inter Integrated Circuit.
45 */
46 class i2c {
47 
48 public:
49 
50  /*!
51  @brief Constructor method.
52  @param[in] dev_path The device path.
53  @param[in] addr The I2C slave address.
54  */
55  i2c( const std::string &dev_path, uint8_t addr );
56 
57  //! Destructor method.
58  virtual ~i2c();
59 
60  /*!
61  @brief Writes data on the device.
62  @param[in] data The data to write on the device.
63  @param[in] size Size of the data to write.
64  */
65  void write( uint8_t *data, uint8_t size );
66 
67  /*!
68  @brief Reads data from the device.
69  @param[in] data The buffer to store the data.
70  @param[in] size Size of the data to read.
71  */
72  void read( uint8_t *data, uint8_t size );
73 
74  /*!
75  @brief Writes a byte data to a register on the device.
76  @param[in] reg The device register.
77  @param[in] data The data to write to the register.
78  */
79  void writeReg8( uint8_t reg, uint8_t data );
80 
81  /*!
82  @brief Reads a byte from a register on the device.
83  @param[in] reg The device register.
84  @return The data read from the register.
85  */
86  uint8_t readReg8( uint8_t reg );
87 
88  /*!
89  @brief Writes a word data to a register on the device.
90  @param[in] reg The device register.
91  @param[in] data The data to write to the register.
92  */
93  void writeReg16( uint8_t reg, uint16_t data );
94 
95  /*!
96  @brief Reads a word from a register on the device.
97  @param[in] reg The device register.
98  @return The data read from the register.
99  */
100  uint16_t readReg16( uint8_t reg );
101 
102 private:
103 
104  //! The device path.
105  std::string m_dev_path;
106 
107  //! The I2C slave address.
108  uint32_t m_addr;
109 
110  //! File descriptor of the device.
111  int m_dev_fd;
112 
113  //! Data buffer used for I2C transmission.
114  uint8_t m_buffer[3];
115 };
116 
117 } // End of drivers namespace
118 
119 } // End of main namespace
120 
121 
122 // Include inline methods
123 #include <rpi-hw/driver/i2c-inl.hpp>
124 
125 #endif /* _RPI_HW_DRIVER_I2C_HPP_ */
uint32_t m_addr
The I2C slave address.
Definition: i2c.hpp:108
uint8_t readReg8(uint8_t reg)
Reads a byte from a register on the device.
void write(uint8_t *data, uint8_t size)
Writes data on the device.
uint16_t readReg16(uint8_t reg)
Reads a word from a register on the device.
void writeReg8(uint8_t reg, uint8_t data)
Writes a byte data to a register on the device.
virtual ~i2c()
Destructor method.
uint8_t m_buffer[3]
Data buffer used for I2C transmission.
Definition: i2c.hpp:114
std::string m_dev_path
The device path.
Definition: i2c.hpp:105
void writeReg16(uint8_t reg, uint16_t data)
Writes a word data to a register on the device.
i2c(const std::string &dev_path, uint8_t addr)
Constructor method.
Inter Integrated Circuit.
Definition: i2c.hpp:46
int m_dev_fd
File descriptor of the device.
Definition: i2c.hpp:111
void read(uint8_t *data, uint8_t size)
Reads data from the device.