-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCardI2CX.cpp
391 lines (328 loc) · 8.44 KB
/
CCardI2CX.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <iostream>
#include <sstream>
#include <syslog.h>
#include "Mutex.h"
#include "MutexLock.h"
#include "CCardI2CX.h"
namespace IrvCS
{
/**
* Guard i2c calls from getting called concurrently
**/
static Mutex gI2cAccessMutex;
CCardI2CX::CCardI2CX():addr_(0x38), initialized_(false),
isPoweredOn_(false)
{
int gpioInitStatus=initGpios();
int powerStatus=0;
if (0 != gpioInitStatus)
{
syslog(LOG_ERR, "Encountered %d errors initializing GPIO's", gpioInitStatus);
}
//
// For irvine-02, payload power is always turned on
//
if (0 != setPayloadPower(pl5VGpio_, 1))
{
powerStatus++;
}
if (0 != setPayloadPower(pl3VGpio_,1))
{
powerStatus++;
}
powerStatus+=powerOn();
if (powerStatus == 0)
{
syslog(LOG_NOTICE, "%s Initialized", __FILENAME__);
initialized_=true;
} else
{
syslog(LOG_ERR, "%s: Unable to initialize", __FILENAME__);
}
}
int CCardI2CX::initGpios()
{
int errors=0;
if (!pl3VGpio_.initialize(102, "PL 3V Pwr"))
{
syslog(LOG_ERR, "Unable to initialize 3V PL GPIO)");
errors++;
}
if (!pl5VGpio_.initialize(103, "PL 5V Pwr"))
{
syslog(LOG_ERR, "Unable to initialize 5V PL GPIO)");
errors++;
}
return errors;
}
CCardI2CX::~CCardI2CX()
{
syslog(LOG_NOTICE, "%s Cleaning up", __FILENAME__);
// close any resources
if (i2cdev_ >= 0)
{
close(i2cdev_);
}
}
int CCardI2CX::powerOn()
{
int powerStatus=0;
pwrTimestamp_=time(NULL);
if (isPoweredOn_)
{
// Already on.
syslog(LOG_DEBUG, "5V PL Pwr Access");
return 0;
}
syslog(LOG_INFO, "Powering on C-Card");
//
// power on 5V payload for C-Card
//
if (0 != setPayloadPower(pl5VGpio_, 1))
{
return --powerStatus;
}
isPoweredOn_=true;
const char *i2cbus="/dev/i2c-1";
// initialize i2c bus
syslog(LOG_INFO, "Initializing %s",i2cbus);
i2cdev_=open(i2cbus, O_RDWR);
if (i2cdev_<0)
{
syslog(LOG_ERR, "Unable to open i2c device %s: %s (%d)",
i2cbus, strerror(errno), errno);
return --powerStatus;
}
if (ioctl(i2cdev_, I2C_SLAVE, addr_) < 0)
{
syslog(LOG_ERR, "Unable to initialize %02x on %s: %s (%d)",
addr_, i2cbus, strerror(errno), errno);
return --powerStatus;
}
// Read input register to make sure we can access valid data
int inputRegister=0;
if (0 > (inputRegister=getRegister(RegInput)))
{
return --powerStatus;
}
__s32 result=i2c_smbus_write_byte_data(i2cdev_, RegConfig, DSA_CONFIG_PORTS);
if (result < 0)
{
syslog(LOG_ERR, "Unable to write data to register %02x: %s (%d)",
RegConfig, strerror(result*-1), result);
return --powerStatus;
}
if (reset() < 0)
{
syslog(LOG_ERR, "Unable to initialize state");
return --powerStatus;
}
return powerStatus;
}
/**
* Perform the specified DSA command/operation.
* @param id the id of the DSA
* @param cmd the command to perform (Release or Deploy)
* @param timeoutSec the timeout in seconds
* @return StatOk if successful
* @return OpStatus (<0)
*/
OpStatus CCardI2CX::performDsaOperation(DsaId id, DsaCmd cmd, int timeoutSec)
{
if (cmd != Deploy && cmd != Release)
{
return StatInvalidInput;
}
int status=dsaPerform(id, cmd, timeoutSec);
if (status < 0)
{
return static_cast<OpStatus>(status);
}
return StatOk;
}
/**
* Get the sensor status for the given DSA/Cmd
**/
int CCardI2CX::getSensorStatus(DsaId id, DsaCmd cmd)
{
uint8_t statusBit=DSA_SENSE_OFFSET+id+((cmd==Release)?0:1);
return (dsaSenseBits_<<statusBit) & 0x1;
}
/**
* Get the status bits
**/
int CCardI2CX::getSensorStatusBits()
{
int retVal=getRegister(RegInput);
if (0 > retVal)
{
return retVal;
}
return retVal & 0xFF;
}
int CCardI2CX::setPayloadPower(Gpio &pwrGpio, uint8_t onOrOff)
{
int retVal=0;
const char *opString=(onOrOff==0?"Disabled":"Enabled");
std::stringstream stm;
stm<<pwrGpio;
retVal=pwrGpio.set(onOrOff);
if (retVal != 0)
{
syslog(LOG_ERR, "Unable to set %s to %d (%d)", stm.str().c_str(), onOrOff,
retVal);
} else
{
syslog(LOG_INFO, "%s --> %s", stm.str().c_str(), opString);
}
return retVal;
}
int CCardI2CX::setI2Cstate(uint8_t state)
{
// set the initial output states
int result=i2c_smbus_write_byte_data(i2cdev_, (int)RegOutput, state);
if (result < 0)
{
syslog(LOG_ERR, "Unable to set register %02x with %02x: %s (%d)",
RegOutput, state, strerror(result*-1), result);
} else
{
syslog(LOG_INFO, "CCardI2CX SET --> %02x", state);
}
return result;
}
int CCardI2CX::setState(uint8_t state)
{
MutexLock lock(gI2cAccessMutex);
return setI2Cstate(state);
}
int CCardI2CX::getRegister(const RegisterType regType)
{
MutexLock lock(gI2cAccessMutex);
int result=i2c_smbus_read_byte_data(i2cdev_, regType);
if (result < 0)
{
syslog(LOG_ERR, "Unable to get register %02x: %s (%d)",
RegOutput, strerror(result*-1), result);
}
return result;
}
int CCardI2CX::getState(uint8_t &state)
{
int result=0;
uint8_t outputRegister=0;
outputRegister=getRegister(RegOutput);
state=(uint8_t)(result&DSA_CMD_MASK);
return 0;
}
static void setDeployState(int bitOffset, int gpioResult, uint8_t &deployState)
{
uint8_t stateBit=1<<bitOffset;
if (gpioResult == 0)
{
deployState &= ~stateBit;
return;
} else if (gpioResult > 0)
{
deployState |= stateBit;
} else // set error bit which is in the first upper 2 bits.
{
deployState |= 1<<(bitOffset+4);
}
}
uint8_t CCardI2CX::getDsaDeployState()
{
uint8_t deployState=0;
int retrieveStatus=0;
uint8_t inputRegister=0;
inputRegister=getRegister(RegInput);
inputRegister << DSA_SENSE_OFFSET;
return deployState;
}
int8_t CCardI2CX::getStates(uint8_t &portState, uint8_t &deployState)
{
deployState=getDsaDeployState();
return getState(portState);
}
bool CCardI2CX::isOk()
{
return initialized_;
}
int CCardI2CX::reset()
{
return setI2Cstate(portState_.reset());
}
int CCardI2CX::dsaPerform(DsaId id, DsaCmd cmd, int timeoutSec)
{
int status=CC_OK;
int dsaIndex=0;
int timeCount=0; // incremented every second we wait
syslog(LOG_INFO, "Performing DSA-%d %s",
id==DSA_1?1:2, cmd == Release?"Release":"Deploy");
// first reset everything
portState_.reset();
status=portState_.setDsa(id, cmd);
int setStatus=setState(status);
if (0 < setStatus)
{
syslog(LOG_WARNING, "%s Unable to set expander value to %02x",
__FILENAME__, status);
return setStatus;
}
if (cmd != Deploy && cmd != Release)
{
return StatInvalidInput;
}
// make sure this matches the DsaId enumeration
const char *dsaIdStr[]={
"DSA_1",
"",
"DSA_2",
"DSA_Unknown"
};
//
// Wait for DSA sense to change
//
syslog(LOG_INFO, "Waiting %d sec for %s Sensor to change",
timeoutSec, dsaIdStr[id]);
while (true)
{
int retrieveStatus= getSensorStatusBits();
if (0 > retrieveStatus)
{
syslog(LOG_ERR, "Unable to retrive sensor status");
break;
}
dsaSenseBits_=retrieveStatus&0xFF;
int sensorStatus=getSensorStatus(id, cmd);
if (cmd == Release && sensorStatus == 1)
{
syslog(LOG_INFO, "Released %s at %d sec", dsaIdStr[id], timeCount+1);
break;
} else if (cmd == Deploy && sensorStatus == 1)
{
syslog(LOG_INFO, "Deployed %s at %d sec", dsaIdStr[id], timeCount+1);
break;
} else if (timeCount++ > timeoutSec)
{
syslog(LOG_WARNING, "%s operation timed out after %d sec",
dsaIdStr[id], timeCount);
status=StatTimeOut;
break;
}
sleep(1);
}
cleanup:
reset();
return status;
}
}