-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitb2.c
423 lines (274 loc) · 7.48 KB
/
bitb2.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
// ===================================================================
#define PIN_LED 17 // LED Port /* P1-xx */
#define PIN_SENS 22 // Fototransistor Port
#define PIN_MOSI 10 // SPI MOSI to 74hc595
#define PIN_LOAD 23 // LOAD Parallel Input register 74hc589 (Toggle twice)
#define PIN_SCK 11 // SPI clock
#define PIN_MISO 9 // SPI MISO from 74hc589
//echo "17" > /sys/class/gpio/export
//echo "out" > /sys/class/gpio/gpio17/direction
//cat /sys/class/gpio/gpio9/value
#define ON 1
#define OFF 0
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
int LED_pin(unsigned char value);
unsigned char SENSOR_pin(void);
int SCK_pin(unsigned char value);
int LOAD_pin(unsigned char value);
unsigned char MISO_pin(void);
int MOSI_pin(unsigned char value);
unsigned char spiWrite(const unsigned char regData);
unsigned int enable_gpio(void);
unsigned int set_gpio_direcction (void);
unsigned int un_export_used_gpios(void);
void strobe_load_pin(void);
unsigned char contador,sens;
unsigned char read_data;
int i,j,k;
int main(int argc, char *argv[])
{
///usleep(500 * 1000);
enable_gpio();
set_gpio_direcction();
////////////////////////// Test Led and Fotototransistor //////////////////////////////
LED_pin(ON);
sleep(1); // wait to turn on/off led before reading (processor is to fast :))
for(i=0;i<5;i++)
{
sens=SENSOR_pin();
printf ("Sensor Read Back %d Done !\n\n",sens);
}
sleep(2); // time to read on screen
///////////////////////SPI Test Bed ///////////////////////////////
while (1) {
// read_data=spiWrite(contador++);
read_data=spiWrite(read_data);
printf(" Data read (Hex) %X\n ",read_data);
// Read / Write secuence
// If we have 3 output SR at MOSI output and 2 input SR in cascade
/// 1-send data 3 times read_data will be discarted each time
/// 2-Toggle Load pin twice
/// 3-Send Dummy data twice and save each recieved byte
strobe_load_pin();
strobe_load_pin();
}
un_export_used_gpios();
return(0);
}
unsigned char spiWrite(const unsigned char regData)
{
unsigned char SPICount; // Counter used to clock out the data
unsigned char SPIData; // Define a data structure for the SPI data.
unsigned char MISO_value;
unsigned char MISO_DATA=0;
SCK_pin(ON); // and CK High
SPIData = regData; // Preload the data to be sent
printf("Read Value (bin): ");
for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock out Data
{
if (SPIData & 0x80)
MOSI_pin(ON);
else
MOSI_pin(OFF);
SCK_pin(OFF);
MISO_value=MISO_pin();
SCK_pin(ON);
MISO_DATA <<=1; // 1st time dummy Shift
if (MISO_value==ON)
MISO_DATA|=1;
else
MISO_DATA&=~1;
SPIData <<= 1;
printf("%d",MISO_value);
} // and loop back to send the next bit
MOSI_pin(OFF); // Reset the MOSI data line
return(MISO_DATA);
}
//////////////////////////////// Low low Level functions //////////////////////
int LED_pin(unsigned char value)
{
if ( GPIOWrite(PIN_LED, value) == -1)
return(3);
}
unsigned char SENSOR_pin(void)
{
return(GPIORead(PIN_SENS));
}
int MOSI_pin(unsigned char value)
{
if ( GPIOWrite(PIN_MOSI, value) == -1)
return(3);
}
int LOAD_pin(unsigned char value)
{
if ( GPIOWrite(PIN_LOAD, value) == -1)
return(3);
}
int SCK_pin(unsigned char value)
{
if ( GPIOWrite(PIN_SCK, value) == -1)
return(3);
}
unsigned char MISO_pin(void)
{
return(GPIORead(PIN_MISO));
}
// Making changes taken from http://elinux.org/RPi_Low-level_peripherals#C_2
int GPIOExport(int pin)
{
#define BUFFER_MAX 3
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/export", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open export for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}
int GPIOUnexport(int pin)
{
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open unexport for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}
int GPIODirection(int pin, int dir)
{
static const char s_directions_str[] = "in\0out";
#define DIRECTION_MAX 35
char path[DIRECTION_MAX];
int fd;
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio direction for writing!\n");
return(-1);
}
if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3)) {
fprintf(stderr, "Failed to set direction!\n");
return(-1);
}
close(fd);
return(0);
}
int GPIORead(int pin)
{
#define VALUE_MAX 30
char path[VALUE_MAX];
char value_str[3];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_RDONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio value for reading!\n");
return(-1);
}
if (-1 == read(fd, value_str, 3)) {
fprintf(stderr, "Failed to read value!\n");
return(-1);
}
close(fd);
return(atoi(value_str));
}
int GPIOWrite(int pin, int value)
{
static const char s_values_str[] = "01";
char path[VALUE_MAX];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio value for writing!\n");
return(-1);
}
// int write( int handle, void *buffer, int nbyte );
if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) {
fprintf(stderr, "Failed to write value!\n");
return(-1);
}
close(fd);
return(0);
}
// Strobe Load Pin ... more later (too lazy now!!)
void strobe_load_pin(void)
{
LOAD_pin(OFF);
LOAD_pin(ON);
}
/*=======================================
Enable GPIO pins
========================================*/
unsigned int enable_gpio(void)
{
if (GPIOExport(PIN_LED) ==-1)
return(1);
if (GPIOExport(PIN_SENS) ==-1)
return(1);
if (GPIOExport(PIN_MOSI) ==-1)
return(1);
if (GPIOExport(PIN_LOAD) ==-1)
return(1);
if (GPIOExport(PIN_SCK) ==-1)
return(1);
if (GPIOExport(PIN_MISO) ==-1)
return(1);
return(0);
}
/*==============================
Set GPIO directions
==============================*/
unsigned int set_gpio_direcction (void)
{
// Pleavoid set_gpio_direcction (void) se put all I/O definitions here !!!
if (GPIODirection(PIN_LED,OUT) ==-1)
return(1);
if (GPIODirection(PIN_SENS,IN) ==-1)
return(1);
if (GPIODirection(PIN_MOSI,OUT) ==-1)
return(1);
if (GPIODirection(PIN_LOAD,OUT) ==-1)
return(1);
if (GPIODirection(PIN_SCK,OUT) ==-1)
return(1);
if (GPIODirection(PIN_MISO,IN) ==-1)
return(1);
return(0);
}
/*==============================
Unexport GPIO
==============================*/
unsigned int un_export_used_gpios(void)
{
if (GPIOUnexport(PIN_LED) ==-1)
return(4);
if (GPIOUnexport(PIN_SENS) ==-1)
return(4);
if (GPIOUnexport(PIN_MOSI) ==-1)
return(4);
if (GPIOUnexport(PIN_LOAD) ==-1)
return(4);
if (GPIOUnexport(PIN_SCK) ==-1)
return(4);
if (GPIOUnexport(PIN_MISO) ==-1)
return(4);
return(0);
}