F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
LinuxI2cDriver.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title LinuxI2cDriverComponentImpl.cpp
3 // \author tcanham
4 // \brief cpp file for LinuxI2cDriver component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2015, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #include "Fw/Types/Assert.hpp"
14 #include <FpConfig.hpp>
16 #include <Fw/Logger/Logger.hpp>
17 
18 #include <unistd.h> // required for I2C device access
19 #include <fcntl.h> // required for I2C device configuration
20 #include <sys/ioctl.h> // required for I2C device usage
21 #include <linux/i2c.h> // required for struct / constant definitions
22 #include <linux/i2c-dev.h> // required for constant definitions
23 #include <cerrno>
24 
25 
26 namespace Drv {
27 
28  // ----------------------------------------------------------------------
29  // Construction, initialization, and destruction
30  // ----------------------------------------------------------------------
31 
34  const char *const compName
35  ) : LinuxI2cDriverComponentBase(compName),
36  m_fd(-1)
37  {
38 
39  }
40 
43  {
44  if (-1 != this->m_fd) { // check if file is open
45  ::close(this->m_fd);
46  }
47  }
48 
49  bool LinuxI2cDriver::open(const char* device) {
50  FW_ASSERT(device);
51  this->m_fd = ::open(device, O_RDWR);
52  return (-1 != this->m_fd);
53  }
54 
55 
56  // ----------------------------------------------------------------------
57  // Handler implementations for user-defined typed input ports
58  // ----------------------------------------------------------------------
59 
60  // Note this port handler is guarded, so we can make the ioctl call
61 
62  Drv::I2cStatus LinuxI2cDriver ::
63  write_handler(
64  const NATIVE_INT_TYPE portNum,
65  U32 addr,
66  Fw::Buffer &serBuffer
67  )
68  {
69  // Make sure file has been opened
70  if (-1 == this->m_fd) {
72  }
73 
74  // select slave address
75  int stat = ioctl(this->m_fd, I2C_SLAVE, addr);
76  if (stat == -1) {
78  }
79  // make sure it isn't a null pointer
80  FW_ASSERT(serBuffer.getData());
81  // write data
82  stat = static_cast<int>(write(this->m_fd, serBuffer.getData(), serBuffer.getSize()));
83  if (stat == -1) {
85  }
86  return I2cStatus::I2C_OK;
87  }
88 
89  Drv::I2cStatus LinuxI2cDriver ::
90  read_handler(
91  const NATIVE_INT_TYPE portNum,
92  U32 addr,
93  Fw::Buffer &serBuffer
94  )
95  {
96  // Make sure file has been opened
97  if (-1 == this->m_fd) {
99  }
100 
101  // select slave address
102  int stat = ioctl(this->m_fd, I2C_SLAVE, addr);
103  if (stat == -1) {
105  }
106  // make sure it isn't a null pointer
107  FW_ASSERT(serBuffer.getData());
108  // read data
109  stat = static_cast<int>(read(this->m_fd, serBuffer.getData(), serBuffer.getSize()));
110  if (stat == -1) {
112  }
113  return I2cStatus::I2C_OK;
114  }
115 
116  Drv::I2cStatus LinuxI2cDriver ::
117  writeRead_handler(
118  const NATIVE_INT_TYPE portNum,
119  U32 addr,
120  Fw::Buffer &writeBuffer,
121  Fw::Buffer &readBuffer
122  ){
123 
124  // Make sure file has been opened
125  if (-1 == this->m_fd) {
127  }
128  FW_ASSERT(-1 != this->m_fd);
129 
130  // make sure they are not null pointers
131  FW_ASSERT(writeBuffer.getData());
132  FW_ASSERT(readBuffer.getData());
133 
134  struct i2c_msg rdwr_msgs[2];
135 
136  // Start address
137  rdwr_msgs[0].addr = static_cast<U16>(addr);
138  rdwr_msgs[0].flags = 0; // write
139  rdwr_msgs[0].len = static_cast<U16>(writeBuffer.getSize());
140  rdwr_msgs[0].buf = writeBuffer.getData();
141 
142  // Read buffer
143  rdwr_msgs[1].addr = static_cast<U16>(addr);
144  rdwr_msgs[1].flags = I2C_M_RD; // read
145  rdwr_msgs[1].len = static_cast<U16>(readBuffer.getSize());
146  rdwr_msgs[1].buf = readBuffer.getData();
147 
148  struct i2c_rdwr_ioctl_data rdwr_data;
149  rdwr_data.msgs = rdwr_msgs;
150  rdwr_data.nmsgs = 2;
151 
152  //Use ioctl to perform the combined write/read transaction
153  NATIVE_INT_TYPE stat = ioctl(this->m_fd, I2C_RDWR, &rdwr_data);
154 
155  if(stat == -1){
156  //Because we're using ioctl to perform the transaction we dont know exactly the type of error that occurred
158  }
159 
160  return I2cStatus::I2C_OK;
161  }
162 
163 } // end namespace Drv
bool open(const char *device)
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
U8 * getData() const
Definition: Buffer.cpp:68
U32 getSize() const
Definition: Buffer.cpp:72
LinuxI2cDriver(const char *const compName)
C++-compatible configuration header for fprime configuration.
Auto-generated base for LinuxI2cDriver component.
Other errors that don&#39;t fit.
I2C driver failed to open device.
Transaction okay.
#define FW_ASSERT(...)
Definition: Assert.hpp:14