BlackI2C.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 BLACKI2C_H_
35 #define BLACKI2C_H_
36 
37 
38 #include "../BlackCore.h"
39 #include <iostream>
40 
41 #include <cstring>
42 #include <string>
43 #include <fstream>
44 #include <cstdint>
45 #include <unistd.h>
46 
47 #include <fcntl.h>
48 #include <sys/ioctl.h>
49 #include <linux/i2c.h>
50 #include <linux/i2c-dev.h>
51 
52 
53 namespace BlackLib
54 {
55 
59  enum i2cName { I2C_0 = 0,
60  I2C_1 = 1
61  };
62 
75  };
76 
77 
78 
79 
80 
81 
82 
83  // ########################################### BLACKI2C DECLARATION STARTS ############################################ //
84 
190  class BlackI2C : virtual private BlackCore
191  {
192  private:
195  unsigned int i2cDevAddress;
196  int i2cFD;
197  std::string i2cPortPath;
198  bool isOpenFlag;
208  bool loadDeviceTree();
209 
223  inline bool useSmbusIOCTL(direction rwMode, uint8_t registerAddr, transactionType smbusTransaction, i2c_smbus_data &data);
224 
231  inline bool setSlave();
232 
233 
234 
235  public:
239  enum flags { openErr = 0,
240  closeErr = 1,
242  readErr = 3,
243  writeErr = 4
244  };
245 
266  BlackI2C(i2cName i2c, unsigned int i2cDeviceAddress);
267 
272  virtual ~BlackI2C();
273 
274 
296  bool open(uint openMode);
297 
315  bool close();
316 
361  bool writeByte(uint8_t registerAddr, uint8_t value);
362 
435  bool writeWord(uint8_t registerAddr, uint16_t value);
436 
501  bool writeBlock(uint8_t registerAddr, uint8_t *writeBuffer, size_t bufferSize);
502 
564  bool writeLine(uint8_t *writeBuffer, size_t bufferSize);
565 
577  uint8_t readByte(uint8_t registerAddr);
578 
590  uint16_t readWord(uint8_t registerAddr);
591 
605  uint8_t readBlock(uint8_t registerAddr, uint8_t *readBuffer, size_t bufferSize);
606 
621  bool readLine(uint8_t *readBuffer, size_t bufferSize);
622 
649  void setDeviceAddress(unsigned int newDeviceAddr);
650 
659  int getDeviceAddress();
660 
682  std::string getPortName();
683 
708  bool isOpen();
709 
734  bool isClose();
735 
764  bool fail();
765 
837  bool fail(BlackI2C::flags f);
838  };
839 
840  // ########################################### BLACKI2C DECLARATION ENDS ############################################## //
841 
842 
843 } /* namespace BlackLib */
844 
845 #endif /* BLACKI2C_H_ */
Definition: BlackI2C.h:74
Definition: BlackI2C.h:242
int i2cFD
is used to hold the i2c's tty file's file descriptor
Definition: BlackI2C.h:196
bool writeByte(uint8_t registerAddr, uint8_t value)
Writes byte value to i2c smbus.
Definition: BlackI2C.cpp:151
Holds BlackI2C errors.
Definition: BlackErr.h:1071
bool writeLine(uint8_t *writeBuffer, size_t bufferSize)
Writes data block to i2c line.
Definition: BlackI2C.cpp:284
Definition: BlackI2C.h:240
Definition: BlackI2C.h:60
unsigned int i2cDevAddress
is used to hold the i2c's device address
Definition: BlackI2C.h:195
Definition: BlackI2C.h:59
Definition: BlackI2C.h:243
bool isClose()
Checks i2c's tty file's close state.
Definition: BlackI2C.cpp:341
Definition: BlackI2C.h:68
Definition: BlackI2C.h:67
bool writeWord(uint8_t registerAddr, uint16_t value)
Writes word value to i2c smbus.
Definition: BlackI2C.cpp:189
BlackI2C(i2cName i2c, unsigned int i2cDeviceAddress)
Constructor of BlackI2C class.
Definition: BlackI2C.cpp:38
openMode
Definition: BlackDef.h:72
Definition: BlackI2C.h:73
bool fail()
Is used for general debugging.
Definition: BlackI2C.cpp:347
errorI2C * i2cErrors
is used to hold the errors of BlackI2C class
Definition: BlackI2C.h:193
transactionType
Definition: BlackI2C.h:66
void setDeviceAddress(unsigned int newDeviceAddr)
Changes device address of slave device.
Definition: BlackI2C.cpp:316
i2cName
Definition: BlackI2C.h:59
Definition: BlackI2C.h:239
uint8_t readByte(uint8_t registerAddr)
Read byte value from i2c smbus.
Definition: BlackI2C.cpp:170
Definition: BlackI2C.h:70
bool loadDeviceTree()
Device tree loading is not necessary for using I2C feature.
Definition: BlackI2C.cpp:92
bool open(uint openMode)
Opens TTY file of i2c.
Definition: BlackI2C.cpp:101
Definition: BlackI2C.h:71
std::string getPortName()
Exports i2c's port path.
Definition: BlackI2C.cpp:331
std::string i2cPortPath
is used to hold the i2c's tty port path
Definition: BlackI2C.h:197
int getDeviceAddress()
Exports device address of slave device.
Definition: BlackI2C.cpp:322
bool writeBlock(uint8_t registerAddr, uint8_t *writeBuffer, size_t bufferSize)
Writes block of data to i2c smbus.
Definition: BlackI2C.cpp:229
direction
Definition: BlackDef.h:44
uint16_t readWord(uint8_t registerAddr)
Read word value from i2c smbus.
Definition: BlackI2C.cpp:208
bool close()
Closes TTY file of i2c.
Definition: BlackI2C.cpp:131
flags
Definition: BlackI2C.h:239
bool setSlave()
Sets slave to device.
Definition: BlackI2C.cpp:78
uint8_t readBlock(uint8_t registerAddr, uint8_t *readBuffer, size_t bufferSize)
Read data block from i2c smbus.
Definition: BlackI2C.cpp:257
bool useSmbusIOCTL(direction rwMode, uint8_t registerAddr, transactionType smbusTransaction, i2c_smbus_data &data)
Does SMBUS kernel requests.
Definition: BlackI2C.cpp:56
Interacts with end user, to use I2C.
Definition: BlackI2C.h:190
Definition: BlackI2C.h:69
bool isOpen()
Checks i2c's tty file's open state.
Definition: BlackI2C.cpp:336
Definition: BlackI2C.h:241
virtual ~BlackI2C()
Destructor of BlackI2C class.
Definition: BlackI2C.cpp:49
Definition: BlackADC.cpp:36
Definition: BlackI2C.h:72
bool readLine(uint8_t *readBuffer, size_t bufferSize)
Read data block from i2c line.
Definition: BlackI2C.cpp:299
bool isOpenFlag
is used to hold the i2c's tty file's state
Definition: BlackI2C.h:198
Definition: BlackI2C.h:66
Base class of the other classes.
Definition: BlackCore.h:80