i2c_driver.cpp
Go to the documentation of this file.
1 #include <fcntl.h>
2 #include <iostream>
3 #include <linux/i2c-dev.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <unistd.h>
7 
8 #ifndef I2C_FUNC_I2C
9 #include <linux/i2c.h>
10 #define I2C_MSG_FMT __u8
11 #endif
12 
13 #include <sys/ioctl.h>
14 
16 
18 
19 void I2CDriver::init() {}
20 
22  this->write(0x33, 0x66, 0x00);
23  return 0;
24 }
25 
26 int I2CDriver::read(uint8_t slaveAddr, uint16_t startAddress,
27  uint16_t nMemAddressRead, uint16_t *data) {
28  if (!this->i2c_fd) {
29  this->i2c_fd = open(this->i2c_device, 02);
30  }
31 
32  int result;
33  char cmd[2] = {(char)(startAddress >> 8), (char)(startAddress & 0xFF)};
34  char buf[1664];
35  uint16_t *p = data;
36  struct i2c_msg i2c_messages[2];
37  struct i2c_rdwr_ioctl_data i2c_messageset[1];
38 
39  i2c_messages[0].addr = slaveAddr;
40  i2c_messages[0].flags = 0;
41  i2c_messages[0].len = 2;
42  i2c_messages[0].buf = (I2C_MSG_FMT *)cmd;
43  i2c_messages[1].addr = slaveAddr;
44  i2c_messages[1].flags = I2C_M_RD | I2C_M_NOSTART;
45  i2c_messages[1].len = nMemAddressRead * 2;
46  i2c_messages[1].buf = (I2C_MSG_FMT *)buf;
47  i2c_messageset[0].msgs = i2c_messages;
48  i2c_messageset[0].nmsgs = 2;
49 
50  memset(buf, 0, nMemAddressRead * 2);
51  if (ioctl(this->i2c_fd, I2C_RDWR, &i2c_messageset) < 0) {
52  ROS_ERROR("I2C read error!");
53  return -1;
54  }
55 
56  for (int count = 0; count < nMemAddressRead; count++) {
57  int i = count << 1;
58  *p++ = ((uint16_t)buf[i] << 8) | buf[i + 1];
59  }
60 
61  return 0;
62 }
63 
64 int I2CDriver::write(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data) {
65  char cmd[4] = {(char)(writeAddress >> 8), (char)(writeAddress & 0x00FF),
66  (char)(data >> 8), (char)(data & 0x00FF)};
67  int result;
68 
69  struct i2c_msg i2c_messages[1];
70  struct i2c_rdwr_ioctl_data i2c_messageset[1];
71 
72  i2c_messages[0].addr = slaveAddr;
73  i2c_messages[0].flags = 0;
74  i2c_messages[0].len = 4;
75  i2c_messages[0].buf = (I2C_MSG_FMT *)cmd;
76  i2c_messageset[0].msgs = i2c_messages;
77  i2c_messageset[0].nmsgs = 1;
78 
79  if (ioctl(this->i2c_fd, I2C_RDWR, &i2c_messageset) < 0) {
80  ROS_ERROR("I2C write error!");
81  return -1;
82  }
83 
84  return 0;
85 }
I2CDriver::generalReset
int generalReset(void)
Definition: i2c_driver.cpp:21
I2CDriver::I2CDriver
I2CDriver()
Definition: i2c_driver.cpp:17
I2C_MSG_FMT
#define I2C_MSG_FMT
Definition: i2c_driver.cpp:10
I2CDriver::i2c_fd
int i2c_fd
Definition: i2c_driver.h:9
I2CDriver::init
void init(void)
Definition: i2c_driver.cpp:19
I2CDriver::write
int write(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data)
Definition: i2c_driver.cpp:64
I2CDriver::i2c_device
const char * i2c_device
Definition: i2c_driver.h:10
i2c_driver.h
ROS_ERROR
#define ROS_ERROR(...)
I2CDriver::read
int read(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data)
Definition: i2c_driver.cpp:26


mlx90640_thermal_camera
Author(s):
autogenerated on Sat Sep 16 2023 02:13:29