-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomm.c
334 lines (284 loc) · 6.17 KB
/
comm.c
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
/*
* comm.c:
* Communication routines "platform specific" for Raspberry Pi
*
* Copyright (c) 2016-2018 Sequent Microsystem
* <http://www.sequentmicrosystem.com>
***********************************************************************
* Author: Alexandru Burcea
***********************************************************************
*/
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <math.h>
#include "relay.h"
static volatile int globalResponse = 0;
int gLed1HwAdd = 0x20;
int gLed2HwAdd = 0x21;
PI_THREAD (waitForKey)
{
char resp;
int respI = NO;
struct termios info;
tcgetattr(0, &info); /* get current terminal attirbutes; 0 is the file descriptor for stdin */
info.c_lflag &= ~ICANON; /* disable canonical mode */
info.c_cc[VMIN] = 1; /* wait until at least one keystroke available */
info.c_cc[VTIME] = 0; /* no timeout */
tcsetattr(0, TCSANOW, &info); /* set i */
(void)piHiPri (10) ; // Set this thread to be high priority
resp = getchar();
if((resp == 'y') || (resp == 'Y'))
respI = YES;
piLock (COUNT_KEY) ;
globalResponse = respI ;
piUnlock (COUNT_KEY) ;
info.c_lflag |= ICANON; /* disable canonical mode */
info.c_cc[VMIN] = 0; /* wait until at least one keystroke available */
info.c_cc[VTIME] = 0; /* no timeout */
tcsetattr(0, TCSANOW, &info); /* set i */
printf("\n");
return &waitForKey;
}
void startThread(void)
{
wiringPiSetupSys ();
piThreadCreate (waitForKey);
}
int checkThreadResult(void)
{
int res;
piLock (COUNT_KEY) ;
res = globalResponse;
piUnlock(COUNT_KEY);
return res;
}
int readReg16(int dev, int add)
{
int val, ret;
val = wiringPiI2CReadReg16(dev, add);
ret = 0xff & (val >> 8);
ret+= 0xff00 & (val << 8);
return ret;
}
int writeReg16(int dev, int add, int val)
{
int wVal;
wVal = 0xff & (val >> 8);
wVal += 0xff00 & (val << 8);
wiringPiI2CWriteReg16(dev,add, wVal);
delay(1);
return 0;
}
int writeReg8(int dev, int add, int val)
{
wiringPiI2CWriteReg8(dev, add, val);
return 0;
}
int readReg8(int dev, int add)
{
return wiringPiI2CReadReg8(dev, add);
}
int readReg24(int dev, int add)
{
int val, aux8;
aux8 = readReg8(dev, add + 2);
val = aux8;
/*val = 0xffff00 & (val << 8);*/
aux8 = readReg8(dev, add + 1);
val += 0xff00 & (aux8 << 8);
aux8 = readReg8(dev, add );
val += 0xff0000 & (aux8 << 16);
#ifdef DEBUG_I
printbits(val);
printf("\n");
printf("%#08x\n", val);
#endif
return val;
}
int writeReg24(int dev, int add, int val)
{
int wVal;//, aux8;
wVal = 0xff & (val >> 8);
writeReg8(dev,add+1, wVal);
wVal = 0xff & ( val >> 16);
writeReg8(dev,add, wVal);
wVal = 0xff & val;
writeReg8(dev,add+2, wVal);
return 0;
}
int doBoardInit(int hwAdd)
{
int dev, bV = -1;
int add;
add = hwAdd ^ 0x07;
dev = wiringPiI2CSetup (add);
if(dev == -1)
{
return ERROR;
}
bV = wiringPiI2CReadReg8 (dev, RELAY8_CFG_REG_ADD);
if(bV == -1)
{
printf( "Relay8 id %d not detected\n", hwAdd - RELAY8_HW_I2C_BASE_ADD);
return ERROR;
}
if(bV != 0) //non initialized I/O Expander
{
// make all I/O output
bV = wiringPiI2CWriteReg8(dev, RELAY8_CFG_REG_ADD, 0x00);
if(bV < 0)
return ERROR;
wiringPiI2CWriteReg8(dev, RELAY8_OUTPORT_REG_ADD, 0x00);
}
return dev;
}
int boardCheck(int hwAdd)
{
int dev, bV = -1;
hwAdd ^= 0x07;
dev = wiringPiI2CSetup (hwAdd);
if(dev == -1)
{
return FAIL;
}
bV = wiringPiI2CReadReg8 (dev, RELAY8_CFG_REG_ADD);
if(bV == -1)
{
return FAIL;
}
return OK;
}
/*
* getLedVal
* Get the value of leds
* arg: chip 0 - 1
* ret: 0x0000 - 0xffff - success; -1 - fail
*/
int getLedVal(int chip)
{
int dev = -1;
int ret = 0;
u16 rVal = 0;
if((chip < 0) || (chip > 1))
{
return -1;
}
dev = wiringPiI2CSetup(gLed1HwAdd + chip);
if(dev <= 0)
{
return -1;
}
ret = wiringPiI2CReadReg16(dev, 0x02);
if(ret < 0)
{
return -1;
}
rVal = 0xff00 & (ret << 4);
rVal += 0x01 & (ret >> 3);
rVal += 0x02 & (ret >> 1);
rVal += 0x04 & (ret << 1);
rVal += 0x08 & (ret << 3);
rVal += 0x10 & (ret >> 11);
rVal += 0x20 & (ret >> 9);
rVal += 0x40 & (ret >> 7);
rVal += 0x80 & (ret >> 5);
return rVal;
}
/*
* setLedVal
* Get the value of leds
* arg: chip 0 - 1
* arg: val 0x0000 - 0xffff
* ret: 0 - success; -1 - fail
*/
int setLedVal(int chip, int val)
{
int dev = -1;
int ret = 0;
u16 wVal = 0;
if((chip < 0) || (chip > 1))
{
return -1;
}
if((val < 0) || (val > 0xffff))
{
return -1;
}
dev = wiringPiI2CSetup(gLed1HwAdd + chip);
if(dev <= 0)
{
return -1;
}
wVal = 0x0ff0 & ( val >> 4);
wVal += 0x1000 & (val << 5);
wVal += 0x2000 & (val << 7);
wVal += 0x4000 & (val << 9);
wVal += 0x8000 & (val << 11);
wVal += 0x0001 & (val >> 3);
wVal += 0x0002 & (val >> 1);
wVal += 0x0004 & (val << 1);
wVal += 0x0008 & (val << 3);
//wVal = 0xffff & val;
wiringPiI2CWriteReg16(dev, 0x06, 0x0000); // set all to output
wiringPiI2CWriteReg16(dev, 0x02, wVal);
return ret;
}
/*
* wrLeds
* Write leds as a corresponence of relay
*/
#ifdef LED_RELAY_LINK
static int wrLedsR(int stack, int val)
{
int dev, addOut = 0x02;
if(stack < 2)
{
dev = wiringPiI2CSetup(gLed1HwAdd);
}
else if(stack < 4)
{
dev = wiringPiI2CSetup(gLed2HwAdd);
}
else
{
#ifdef DEBUG_LED
printf("Invalid stack level\n");
#endif
return -1;
}
if(val < 0 || val > 255)
{
#ifdef DEBUG_LED
printf("Led value out of range\n");
#endif
return -1;
}
if(dev == -1)
{
#ifdef DEBUG_LED
printf("No led board detected\n");
#endif
return -1;
}
addOut += (stack & 0x01);
wiringPiI2CWriteReg16(dev, 0x06, 0x0000);
wiringPiI2CWriteReg8(dev, addOut, 0xff & (~val));
// todo: check write succesful
return 1;
}
#endif
void busyWait(int ms)
{
delay(ms);
}