Hekateros  3.4.3
RoadNarrows Robotics Robot Arm Project
hekSysBoard.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Hekateros
4 //
5 // Library: libhekateros
6 //
7 // File: hekSysBoard.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2014-09-18 16:53:49 -0600 (Thu, 18 Sep 2014) $
12  * $Rev: 3748 $
13  *
14  * \brief \h_hek original system board.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  * \author Daniel Packard (daniel@roadnarrows.com)
18  *
19  * \copyright
20  * \h_copy 2013-2017. RoadNarrows LLC.\n
21  * http://www.roadnarrows.com\n
22  * All Rights Reserved
23  */
24 /*
25  * @EulaBegin@
26  *
27  * Unless otherwise stated explicitly, all materials contained are copyrighted
28  * and may not be used without RoadNarrows LLC's written consent,
29  * except as provided in these terms and conditions or in the copyright
30  * notice (documents and software) or other proprietary notice provided with
31  * the relevant materials.
32  *
33  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
34  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
35  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
36  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
37  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
38  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
41  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
42  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
43  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
44  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
45  *
46  * @EulaEnd@
47  */
48 ////////////////////////////////////////////////////////////////////////////////
49 
50 #include <string>
51 #include <sstream>
52 #include <stdlib.h>
53 
54 #include "rnr/rnrconfig.h"
55 #include "rnr/log.h"
56 #include "rnr/i2c.h"
57 
58 #include "Hekateros/hekateros.h"
59 #include "Hekateros/hekOptical.h"
60 #include "Hekateros/hekSysBoard.h"
61 
62 using namespace std;
63 using namespace hekateros;
64 
65 #if defined(ARCH_overo) || defined(ARCH_linaro)
66 #define TGT_EMBEDDED ///< on hekateros target
67 #else
68 #undef TGT_EMBEDDED ///< off hekateros target
69 #endif
70 
71 
72 int HekSysBoard::open(const string& dev)
73 {
74  byte_t val;
75  int rc;
76 
77  m_i2c.fd = -1;
78  m_i2c.addr = HekIOExpI2CAddr;
79 
80 #ifdef TGT_EMBEDDED
81 
82  // open i2c device
83  if( i2c_open(&m_i2c, dev.c_str()) < 0 )
84  {
85  LOGSYSERROR("%s.", dev.c_str());
86  return -HEK_ECODE_NO_DEV;
87  }
88 
89  LOGDIAG3("Hekateros i2c bus hardware initialized successfully.");
90 
91  //
92  // Set required i/o expansion configuration.
93  //
94 
95  // port 0 polarity
96  val = readIOExp(HekIOExpCmdPolarity0);
97 
98  if( val != HekIOExpConstPolarity0 )
99  {
100  if( (rc = writeIOExp(HekIOExpCmdPolarity0, HekIOExpConstPolarity0)) < 0 )
101  {
102  return rc;
103  }
104  }
105 
106  // port 0 input/output direction
107  val = readIOExp(HekIOExpCmdConfig0);
108 
109  if( val != HekIOExpConstConfig0 )
110  {
111  if( (rc = writeIOExp(HekIOExpCmdConfig0, HekIOExpConstConfig0)) < 0 )
112  {
113  return rc;
114  }
115  }
116 
117 #endif // TGT_EMBEDDED
118 
119  return HEK_OK;
120 }
121 
122 int HekSysBoard::close()
123 {
124  if( m_i2c.fd >= 0 )
125  {
126  i2c_close(&m_i2c);
127  m_i2c.fd = -1;
128  m_i2c.addr = 0;
129  }
130 
131  return HEK_OK;
132 }
133 
134 int HekSysBoard::scan()
135 {
136  return HEK_OK;
137 }
138 
139 int HekSysBoard::cmdReadFwVersion(int &ver)
140 {
141  ver = 0;
142 
143  return HEK_OK;
144 }
145 
146 byte_t HekSysBoard::cmdReadLimits()
147 {
148  byte_t cmd = HekIOExpCmdInput0;
149  byte_t val = 0;
150 
151 #ifdef TGT_EMBEDDED
152  if(i2c_transfer(&m_i2c, HekIOExpI2CAddr, &cmd, 1, &val, 1) < 0)
153  {
154  LOGSYSERROR("i2c_transfer: %s: addr=0x%.02x, command=%u.",
155  HekI2CDevice, HekIOExpI2CAddr, cmd);
156  }
157 #endif // TGT_EMBEDDED
158 
159  return val;
160 }
161 
162 byte_t HekSysBoard::cmdReadAux()
163 {
164  byte_t cmd = HekIOExpCmdInput1;
165  byte_t val = 0;
166 
167 #ifdef TGT_EMBEDDED
168  if(i2c_transfer(&m_i2c, HekIOExpI2CAddr, &cmd, 1, &val, 1) < 0)
169  {
170  LOGSYSERROR("i2c_transfer: %s: addr=0x%.02x, command=%u.",
171  HekI2CDevice, HekIOExpI2CAddr, cmd);
172  }
173 #endif // TGT_EMBEDDED
174 
175  return val;
176 }
177 
178 int HekSysBoard::cmdReadPin(int id)
179 {
180  byte_t cmd;
181  int shift;
182  byte_t mask;
183  byte_t bits = 0;
184  int val = 0;
185 
186  if( id < 8 )
187  {
188  cmd = HekIOExpCmdInput0;
189  shift = id;
190  }
191  else
192  {
193  cmd = HekIOExpCmdInput1;
194  shift = id - 8;
195  }
196 
197  mask = 0x01 << shift;
198 
199 #ifdef TGT_EMBEDDED
200  if(i2c_transfer(&m_i2c, HekIOExpI2CAddr, &cmd, 1, &bits, 1) < 0)
201  {
202  LOGSYSERROR("i2c_transfer: %s: addr=0x%.02x, command=%u.",
203  HekI2CDevice, HekIOExpI2CAddr, cmd);
204  }
205  else if( bits & mask )
206  {
207  val = 1;
208  }
209 #endif // TGT_EMBEDDED
210 
211  return val;
212 }
213 
214 int HekSysBoard::cmdWritePin(int id, int val)
215 {
216  byte_t cmd;
217  int shift;
218  byte_t mask;
219  byte_t bits;
220  byte_t buf[2];
221 
222  if( id < 8 )
223  {
224  cmd = HekIOExpCmdInput0;
225  shift = id;
226  bits = cmdReadLimits();
227  }
228  else
229  {
230  cmd = HekIOExpCmdInput0;
231  shift = id;
232  bits = cmdReadAux();
233  }
234 
235  mask = 0x01 << shift;
236  bits = (bits & ~mask) | (val & mask);
237 
238  buf[0] = cmd;
239  buf[1] = bits;
240 
241 #ifdef TGT_EMBEDDED
242  if(i2c_write(&m_i2c, HekIOExpI2CAddr, buf, 2) < 0)
243  {
244  LOGSYSERROR("i2c_write: %s: addr=0x%.02x, command=%u.",
245  HekI2CDevice, HekIOExpI2CAddr, cmd);
246  return -HEK_ECODE_SYS;
247  }
248 #endif // TGT_EMBEDDED
249 
250  return HEK_OK;
251 
252 }
253 
254 int HekSysBoard::cmdConfigPin(int id, char dir)
255 {
256  // TODO
257  return HEK_OK;
258 }
259 
260 int HekSysBoard::cmdSetAlarmLED(int val)
261 {
262  // TODO overio gpio
263  return HEK_OK;
264 }
265 
266 int HekSysBoard::cmdSetStatusLED(int val)
267 {
268  // not supported
269  return HEK_OK;
270 }
271 
272 int HekSysBoard::cmdSetHaltingState()
273 {
274  // not supported
275  return HEK_OK;
276 }
277 
278 byte_t HekSysBoard::readIOExp(byte_t byCmd)
279 {
280  byte_t byVal = 0;
281 
282 #ifdef TGT_EMBEDDED
283  if(i2c_transfer(&m_i2c, HekIOExpI2CAddr, &byCmd, 1, &byVal, 1) < 0)
284  {
285  LOGSYSERROR("i2c_transfer: %s: addr=0x%.02x, command=%u.",
286  HekI2CDevice, HekIOExpI2CAddr, byCmd);
287  }
288 #endif // TGT_EMBEDDED
289 
290  return byVal;
291 }
292 
293 int HekSysBoard::writeIOExp(byte_t byCmd, byte_t byVal)
294 {
295  byte_t buf[2];
296 
297  buf[0] = byCmd;
298  buf[1] = byVal;
299 
300 #ifdef TGT_EMBEDDED
301  if(i2c_write(&m_i2c, HekIOExpI2CAddr, buf, 2) < 0)
302  {
303  LOGSYSERROR("i2c_write: %s: addr=0x%.02x, command=%u.",
304  HekI2CDevice, HekIOExpI2CAddr, byCmd);
305  return -HEK_ECODE_SYS;
306  }
307 #endif // TGT_EMBEDDED
308 
309  return HEK_OK;
310 }
311 
312 
313 
<b><i>Hekateros</i></b> optical limit switches.
Top-level package include file.
<b><i>Hekateros</i></b> original system board.
The <b><i>Hekateros</i></b> namespace encapsulates all <b><i>Hekateros</i></b> related constructs...
Definition: hekateros.h:56