-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip_xio_old.c
255 lines (240 loc) · 7.49 KB
/
chip_xio_old.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
/*--------------------------------------------------------------------------------------------
// 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
---------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "chip_xio.h"
#define BUFFS 3
// 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];
// Use to check avalability of pin for export.
// Return '1' found directory. Return '0' no directory.
int check_export_pin( int pin )
{
struct stat info;
char* str = "/sys/class/gpio/gpio";
unsigned int size;
size = (unsigned int)strlen( str );
size += ADDRESS_SIZE;
char path[size];
sprintf( path, "%s%d", str, pin );
if(stat( path, &info ) != 0)
return 0; // No DIR access
else if(info.st_mode & S_IFDIR) //Is a DIR
return 1;
else
return 0; //No DIR
}
// Export a pin
int export_pin( int pin )
{
char* front = "sh -c 'echo";
char* back = "> /sys/class/gpio/export'";
unsigned int size;
size = (unsigned int)strlen( front );
size += (unsigned int)strlen( back );
size += (ADDRESS_SIZE+2);
char str[size];
sprintf( str, "%s %d %s", front, pin, back );
return system( str );
}
// Unexport a pin
int unexport_pin( int pin )
{
char* front = "sh -c 'echo";
char* back = "> /sys/class/gpio/unexport'";
unsigned int size;
size = (unsigned int)strlen( front );
size += (unsigned int)strlen( back );
size += (ADDRESS_SIZE+2);
char str[size];
sprintf( str, "%s %d %s", front, pin, back );
return system( str );
}
// Set the selected pin for output
void set_pin_output( int pin )
{
char* front = "sh -c 'echo out > /sys/class/gpio/gpio";
char* back = "/direction'";
unsigned int size;
size = (unsigned int)strlen( front );
size += (unsigned int)strlen( back );
size += ADDRESS_SIZE;
char str[size];
sprintf( str, "%s%d%s", front, pin, back );
system( str );
}
// Set the selected pin for input
void set_pin_input( int pin )
{
char* front = "sh -c 'echo in > /sys/class/gpio/gpio";
char* back = "/direction'";
unsigned int size;
size = (unsigned int)strlen( front );
size += (unsigned int)strlen( back );
size += ADDRESS_SIZE;
char str[size];
sprintf( str, "%s%d%s", front, pin, back );
system( str );
}
// Get the direction of the pin. Input or output.
char* get_pin_direction( int pin )
{
int fd;
char* front = "/sys/class/gpio/gpio";
char* back = "/direction";
unsigned int size;
size = (unsigned int)strlen( front );
size += (unsigned int)strlen( back );
size += ADDRESS_SIZE;
char str[size];
sprintf( str, "%s%d%s", front, pin, back );
if(( fd = open( str, O_RDONLY, 0 )) == -1 )
error( "get_pin_direction: can't open" );
read( fd, buff, 3 );
close(fd);
return &buff[0];
}
// Get the current value of the pin.
char *get_pin_value( int pin )
{
int fd;
char* front = "/sys/class/gpio/gpio";
char* back = "/value";
unsigned int size;
size = (unsigned int)strlen( front );
size += (unsigned int)strlen( back );
size += ADDRESS_SIZE;
char str[size];
sprintf( str, "%s%d%s", front, pin, back );
if(( fd = open( str, O_RDONLY, 0 )) == -1 )
error( "get_pin_value: can't open" );
buff[1] = 0;
read( fd, buff, 1 );
close(fd);
return &buff[0];
}
// Set the selected output pin high.
void set_pin_high( int pin )
{
char* front = "sh -c 'echo 1 > /sys/class/gpio/gpio";
char* back = "/value'";
unsigned int size;
size = (unsigned int)strlen( front );
size += (unsigned int)strlen( back );
size += ADDRESS_SIZE;
char str[size];
sprintf( str, "%s%d%s", front, pin, back );
system( str );
}
// Set the selected output pin low.
void set_pin_low( int pin )
{
char* front = "sh -c 'echo 0 > /sys/class/gpio/gpio";
char* back = "/value'";
unsigned int size;
size = (unsigned int)strlen( front );
size += (unsigned int)strlen( back );
size += ADDRESS_SIZE;
char str[size];
sprintf( str, "%s%d%s", front, pin, back );
system( str );
}
/*
// Test program
void main( void )
{
int count;
if( check_export_pin( XIO_P0 ) == 1 )
{
printf("Pin %d is in use!\n", XIO_P0);
return;
}
else if( check_export_pin(XIO_P1) == 1)
{
printf("Pin %d is in use!\n", XIO_P1);
return;
}
else
printf("Pins are not in use!\n");
printf( "export pin XIO_P0\n" );
export_pin( XIO_P0 );
printf( "export pin XIO_P1\n" );
export_pin( XIO_P1 );
printf( "XIO_P0 Pin Direction is: %s\n", get_pin_direction( XIO_P0 ));
printf( "XIO_P1 Pin Direction is: %s\n", get_pin_direction( XIO_P1 ));
// strncmp is used because
if( strncmp( get_pin_direction( XIO_P0 ), "in", 2 ) == 0 )
{
printf( "Change input to output\n" );
set_pin_output( XIO_P0 );
}
else
{
printf( "Pin set for output\n" );
}
if( strncmp( get_pin_direction( XIO_P1 ), "in", 2 ) == 0 )
{
printf( "Change input to output\n" );
set_pin_output( XIO_P1 );
}
else
{
printf( "Pin set for output\n" );
}
for( count = 0; count < 10; count++ )
{
printf( "Setting XIO_P0 high\n");
set_pin_high( XIO_P0 );
printf( "Setting XIO_P1 high\n");
set_pin_high( XIO_P1 );
printf( "XIO_P0 Pin Value is: %s\n", get_pin_value( XIO_P0 ) );
printf( "XIO_P1 Pin Value is: %s\n", get_pin_value( XIO_P1 ) );
sleep( 5 );
printf("Setting XIO_P0 low\n");
set_pin_low( XIO_P0 );
printf("Setting XIO_P1 low\n");
set_pin_low( XIO_P1 );
printf( "XIO_P0 Pin Value is: %s\n", get_pin_value( XIO_P0 ) );
printf( "XIO_P1 Pin Value is: %s\n", get_pin_value( XIO_P1 ) );
sleep( 5 );
}
printf( "Setting XIO_P0 high\n");
set_pin_high( XIO_P0 );
printf( "Setting XIO_P1 high\n");
set_pin_high( XIO_P1 );
printf( "XIO_P0 Pin Value is: %s\n", get_pin_value( XIO_P0 ) );
printf( "XIO_P1 Pin Value is: %s\n", get_pin_value( XIO_P1 ) );
printf( "Unexport pin XIO_P1\n" );
unexport_pin( XIO_P1 );
printf( "Unexport pin XIO_P0\n" );
unexport_pin( XIO_P0 );
}
*/