-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip_xio.c
369 lines (355 loc) · 9.23 KB
/
chip_xio.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
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
/*--------------------------------------------------------------------------------------------
// Name: chip_xio.c
//
// Synopsis: chip_xio is a basic application to drive the xio port on the C.H.I.P. Layered on
// the existing sysfs implementation for NON-RTA pin driving.
// At this time it only controls individual pin input or output. This code is not
// portable with other OS's without rewrite.
//
// GPIO Sysfs Interface for Userspace
// https://www.kernel.org/doc/Documentation/gpio/sysfs.txt
//
// Next Thing Co. documentation.
// https://docs.getchip.com/chip.html#gpio
// Pins XIO-P0 to P7 linearly map to gpio408 to gpio415 on kernel 4.3 and gpio1016 to
// gpio1023 on kernel 4.4.11. For kernel 4.4.13-ntc-mlc the range is gpio1013 to
// gpio1019. See documentation to determine version.
//
// Please note this software is not warrantied and any resulting damage from its use is not
// the writers responsibility. Use at your own risk!
//
// Copyright (c) 2017 Ovide N. Mercure
---------------------------------------------------------------------------------------------*/
// TODO: Error Handling.
// TODO: PORT Handling.
//--------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "chip_xio.h"
#define BUFFS 3
#define COMMAND_BUFF_SIZE 50
// This will change based on kernal
// version. See note above.
// #define ADDRESS_SIZE 4
// Future port access
//int xio_port_arr[] = { XIO_P0, XIO_P1, XIO_P2, XIO_P3, XIO_P4, XIO_P5, XIO_P6, XIO_P7 };
char buff[BUFFS],command[COMMAND_BUFF_SIZE];
char* tok[] = {
"/sys",
"/class",
"/gpio",
"/export'",
"/unexport'",
"/value",
"/direction",
"sh -c 'echo",
"out",
"in",
"1",
"0",
">",
"'" };
#define EXPORT_DIR tok[0], tok[1], tok[2]
// In house helper functions
//-------------------------------------------------------------------------------------------------
// Create function which takes in all tokens and assembls them in that order
// using format string as guide.
//--------------------------------------------------------------------------
// Return: String length
//
unsigned int create_command( char* retstr, char* format, ... )
{
*retstr = 0;
va_list a_list;
va_start( a_list, format );
vsprintf( retstr, format, a_list );
va_end( a_list );
return strlen( retstr );
}
// Functions included in header file.
//----------------------------------------------------------------------------------------------------
// Check for superuser assess.
// Check for export directory
//---------------------------
// Return: 0 on error; 1 on success.
//
int chip_xio_start( void )
{
if( getuid() != 0 )
{
perror("Sudo user required!\n");
return 0;
}
if( check_export_dir() < 0 )
{
printf("Cannot access export dir: %s\n", command );
return 0;
}
return 1;
}
// Check for valid address.
//-------------------------
// Return: 0 on error; 1 on success
//
int is_xio_pin( int pin )
{
int base_add;
for( base_add = BASE; base_add < (BASE+8); base_add++ )
{
if( pin == base_add )
{
//printf("Valid Address!\n");
return 1;
}
}
printf("%d is not a valid XIO pin address\n", pin);
return 0;
}
// Get xio base address.
//---------------------
// Return: BASE from header file.
//
int get_xio_pin_base( )
{
return BASE;
}
// Use to check for export directory.
// ----------------------------------
// Return: -1 no export dir; 0 export dir found.
//
int check_export_dir( )
{
int status;
struct stat info;
create_command( command, "%s%s%s", EXPORT_DIR );
status = lstat( command, &info );
//perror( "stat\n" );
//printf( "return status: %d\n", status );
if( status == -1 )
{
printf("Export directory not found!\n");
return -1;
}
else
{
printf("Export directory found!\n");
return 0;
}
}
// Use to check avalability of pin for export.
// -------------------------------------------
// Return: -1 pin dir not found; 0 pin dir found.
//
int check_export_pin( int pin )
{
if( !is_xio_pin( pin ) )
{
return 0;
}
struct stat info;
int status;
create_command( command, "%s%s%s%s%d", EXPORT_DIR, tok[2], pin );
status = lstat( command, &info );
//perror( "stat\n" );
//printf( "return status: %d\n", status );
if( status == -1 )
{
printf( "%d pin directory not found!\n", pin );
return -1;
}
else
{
printf( "%d pin directory Found!\n", pin );
return 0;
}
}
// Export a pin.
//--------------
// Return: 0 on error; 1 on success
//
int export_pin( int pin )
{
int status;
status = check_export_pin( pin );
if( status == -1 )
{
create_command( command, "%s %d %s %s%s%s%s", tok[7], pin, tok[12], EXPORT_DIR, tok[3] );
status = system( command ); // system function returns 0 on success change to 1.
if( status == -1 )
{
perror("System command failed: export_pin!\n");
status = 0;
} else {
//printf("Exporting pin\n");
return 1;
}
}
return status;
}
// Unexport a pin.
//-----------------
// Return: 0 on error; 1 on success
//
int unexport_pin( int pin )
{
if( !is_xio_pin( pin ) )
{
return 0;
}
int status;
create_command( command, "%s %d %s %s%s%s%s", tok[7], pin, tok[12], EXPORT_DIR, tok[4] );
status = system( command ); // system function returns 0 on success change to 1.
if( status == -1 )
{
perror("System command failed: unexport_pin!\n");
status = 0;
} else {
//printf("Unexport pin\n");
return 1;
}
}
// Set the selected pin for output.
//---------------------------------
// Return: 0 on error; 1 on success
//
int set_pin_output( int pin )
{
if( !is_xio_pin( pin ) )
{
return 0;
}
int status;
create_command( command, "%s %s %s %s%s%s%s%d%s%s", tok[7], tok[8],
tok[12], EXPORT_DIR, tok[2], pin, tok[6], tok[13] );
status = system( command );
if( status == -1 )
{
perror("System command failed: set_pin_output!\n");
status = 0;
} else {
//printf("Pin output mode success!\n");
return 1;
}
}
// Set the selected pin for input
//-------------------------------
// Return: 0 on error; 1 on success
//
int set_pin_input( int pin )
{
if( !is_xio_pin( pin ) )
{
return 0;
}
int status;
create_command( command, "%s %s %s %s%s%s%s%d%s%s", tok[7], tok[9],
tok[12], EXPORT_DIR, tok[2], pin, tok[6], tok[13] );
status = system( command );
if( status == -1 )
{
perror("System command failed: set_pin_input!\n");
status = 0;
} else {
//printf("Pin input mode success\n!");
return 1;
}
}
// Get the direction of the pin. Input or output.
//------------------------------------------------
// Returns: NULL string on error; "in" or "out" string on success
//
char* get_pin_direction( int pin )
{
if( !is_xio_pin( pin ) )
{
return NULL;
}
int fd;
create_command( command, "%s%s%s%s%d%s", EXPORT_DIR, tok[2], pin, tok[6] );
if(( fd = open( command, O_RDONLY, 0 )) == -1 )
{
perror( "get_pin_direction: can't open" );
return NULL;
}
read( fd, buff, 3 );
if( strncmp( buff, "out", 3 ) != 0 )
{
buff[2] = 0; //Make sure to add NULL character after "in".
}
close(fd);
return &buff[0];
}
// Get the current value of the pin.
//----------------------------------
// Returns: NULL string on error; "1" or "0" string on success
//
char *get_pin_value( int pin )
{
if( !is_xio_pin( pin ) )
{
return NULL;
}
int fd;
create_command( command, "%s%s%s%s%d%s", EXPORT_DIR, tok[2], pin, tok[5] );
if(( fd = open( command, O_RDONLY, 0 )) == -1 )
{
perror( "get_pin_value: can't open" );
return NULL;
}
buff[1] = 0; //Make sure to add NULL after "1" or "0".
read( fd, buff, 1 );
close(fd);
return &buff[0];
}
// Set the selected output pin high.
//----------------------------------
// Return: 0 on error; 1 on success
//
int set_pin_high( int pin )
{
if( !is_xio_pin( pin ) )
{
return 0;
}
int status;
create_command( command, "%s %s %s %s%s%s%s%d%s%s", tok[7], tok[10],
tok[12], EXPORT_DIR, tok[2], pin, tok[5], tok[13] );
status = system( command );
if( status == -1 )
{
perror("System command failed: set_pin_high!\n");
return 0;
} else {
//printf("Set output pin high success\n!");
return 1;
}
}
// Set the selected output pin low.
//----------------------------------
// Return: 0 on error; 1 on success
//
int set_pin_low( int pin )
{
if( !is_xio_pin( pin ) )
{
return 0;
}
int status;
create_command( command, "%s %s %s %s%s%s%s%d%s%s", tok[7], tok[11],
tok[12], EXPORT_DIR, tok[2] , pin, tok[5], tok[13] );
status = system( command );
if( status == -1 )
{
perror("System command failed: set_pin_low!\n");
return 0;
} else {
//printf("Set output pin low success\n!");
return 1;
}
}