Steefmaester

[classe/driver] I2C linux

Sep 8th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. //By Steef
  2.  
  3. //I2C.h
  4. #ifndef I2C_H
  5. #define I2C_H
  6.  
  7. #include <fcntl.h>
  8. #include <iostream>
  9. #include <linux/i2c-dev.h>
  10. #include <string>
  11. #include <sys/ioctl.h>
  12. #include <unistd.h>
  13. #include <vector>
  14.  
  15.  
  16. using namespace std;
  17.  
  18. class I2C {
  19.  
  20. private:
  21.         const string DEFAULTDEVICE = "/dev/i2c-1";
  22.     int slave_address;
  23.     string devicefile;
  24.         int fd;
  25.         void close_fd();
  26.         int open_fd();
  27.     void init(string DeviceFile, int Address);
  28.        
  29. public:
  30.     I2C();
  31.     I2C(string DeviceFile);
  32.     ~I2C();
  33.        
  34.         vector<int> detect();
  35.     int setAddress(int Address);
  36.     int getAddress() const;
  37.     int setDevicefile(string filename);
  38.        
  39.     int receive(vector <unsigned char> &RxBuf, int length);
  40.     int receive(unsigned char RegisterAddress, vector <unsigned char> &RxBuf, int length);
  41.        
  42.     int send(vector <unsigned char> &TxBuf);
  43.     int send(unsigned char Slave_address, vector<unsigned char> &TxBuf);
  44.     int send(unsigned char value);
  45. };
  46.  
  47.  
  48. #endif
  49.  
  50.  
  51. // I2C.cpp
  52. #include "I2C.h"
  53.  
  54. I2C::I2C()
  55. {
  56.     init(DEFAULTDEVICE, -1);
  57. }
  58.  
  59.  
  60. I2C::I2C(string Devicefile)
  61. {
  62.     init(Devicefile, -1);
  63. }
  64.  
  65. I2C::~I2C()
  66. {
  67.     close_fd();
  68. }
  69.  
  70. void I2C::init(string Devicefile, int Address)
  71. {
  72.     devicefile=Devicefile;
  73.         slave_address = Address;
  74.         fd = 0;
  75. }
  76.  
  77. vector<int> I2C::detect()
  78. {
  79.     vector<int> allDevices;
  80.     char buf[5];
  81.    
  82.     for(int allAddress = 0x03;allAddress < 0x78 ;allAddress ++ )
  83.     {
  84.         setAddress(allAddress);
  85.        
  86.         if(read(fd, buf, 1) == 1)
  87.         {
  88.             allDevices.push_back(allAddress);
  89.         }
  90.     }
  91.    
  92.     return allDevices;
  93. }
  94.  
  95.  
  96. void I2C::close_fd()
  97. {
  98.     if (fd) {
  99.         close(fd);
  100.         fd = 0;
  101.     }
  102. }
  103.  
  104. int I2C::open_fd()
  105. {
  106.  
  107.     if (fd) {
  108.         close_fd();
  109.         fd = 0;
  110.     }
  111.  
  112.     if (slave_address == -1) {
  113.         cout<<"Erreur l'adresse de l'esclave non déclaré"<<endl;
  114.                 return -1;
  115.         }
  116.  
  117.     if ((fd = open(devicefile.c_str(), O_RDWR)) < 0) {
  118.         cout<<"Erreur d'ouverture: " + devicefile <<endl;
  119.                 return -1;
  120.         }
  121.  
  122.     if (ioctl(fd, I2C_SLAVE, slave_address) < 0)
  123.         {
  124.         cout<<"Erreur d'adresse: " + slave_address <<endl;
  125.                 return -1;
  126.         }
  127.  
  128.     return 0;
  129. }
  130.  
  131. int I2C::setAddress(int Address){
  132.     slave_address = Address;
  133.     return open_fd();
  134. }
  135.  
  136. int I2C::getAddress() const{
  137.     return slave_address;
  138. }
  139.  
  140. int I2C::setDevicefile(string filename){
  141.     devicefile = filename;
  142.     return open_fd();
  143. }
  144.  
  145. int I2C::receive(vector <unsigned char> &RxBuf, int length){
  146.  
  147.     RxBuf.clear();
  148.    
  149.     if (length < 1)
  150.     cout<<"Receive method received an invalid buffer length";
  151.    
  152.     for(int i=0 ; i<length ; i++)
  153.         RxBuf.push_back(0);
  154.    
  155.     if (!fd)
  156.          if (open_fd() == -1)
  157.               return -1;
  158.  
  159.  
  160.        
  161.     if (read(fd, RxBuf.data(), RxBuf.size()) != length)
  162.         cout<<"Erreur de lecture I2C ! Address: " + slave_address << " dev file: " + devicefile <<endl;    
  163.  
  164.     return 1;
  165. }
  166.  
  167. int I2C::receive(unsigned char RegisterAddress, vector <unsigned char> &RxBuf, int length){
  168.  
  169.         RxBuf.clear();
  170.        
  171.     if (length < 1)
  172.         cout<<"Receive method received an invalid buffer length";
  173.        
  174.         for(int i=0 ; i<length ; i++)
  175.             RxBuf.push_back(0);
  176.  
  177.     if (!fd)
  178.         if (open_fd() == -1)
  179.               return -1;
  180.  
  181.     if (write(fd, &RegisterAddress, 1) != 1)
  182.         cout<<"Erreur d'ecriture I2C !";
  183.        
  184.  
  185.        
  186.     if (read(fd, RxBuf.data(), RxBuf.size()) != length)
  187.         cout<<"Erreur de lecture I2C ! Address: " + slave_address << " dev file: " + devicefile <<endl;
  188.  
  189.     return 1;
  190. }
  191.  
  192. int I2C::send(vector<unsigned char> &TxBuf){
  193.  
  194.     if (TxBuf.size() == 0)
  195.         cout<<"Erreur TxBuf est vide"<<endl;
  196.  
  197.     if (!fd)
  198.         if (open_fd() == -1)
  199.               return -1;
  200.  
  201.     if(write(fd, TxBuf.data(), TxBuf.size()) != TxBuf.size())
  202.         cout<<"Erreur d'ecriture I2C !"<<endl;
  203.        
  204.         TxBuf.clear();
  205.  
  206.     return 1;
  207. }
  208.  
  209. int I2C::send(unsigned char Slave_address, vector<unsigned char> &TxBuf){
  210.  
  211.     setAddress(static_cast<int>(Slave_address));
  212.  
  213.     if (TxBuf.size() == 0)
  214.         cout<<"Erreur TxBuf est vide"<<endl;
  215.  
  216.     if (!fd)
  217.         if (open_fd() == -1)
  218.               return -1;
  219.  
  220.     if(write(fd, TxBuf.data(), TxBuf.size()) != TxBuf.size())
  221.         cout<<"Erreur d'ecriture I2C !"<<endl;
  222.  
  223.         TxBuf.clear();
  224.     return 1;
  225. }
  226.  
  227. int I2C::send(unsigned char value){
  228.  
  229.     if (!fd)
  230.         if (open_fd() == -1)
  231.               return -1;
  232.  
  233.     if(write(fd, &value, 1) != 1)
  234.         cout<<"Erreur d'ecriture I2C! "<<endl;
  235.  
  236.     return 1;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment