-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPIO_Custom_Utils.c
1128 lines (945 loc) · 33.1 KB
/
GPIO_Custom_Utils.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/epoll.h>
#define GPIO_PATH "/sys/class/gpio/gpio"
#define GPIO_PATH_EXPORT "/sys/class/gpio/export"
#define GPIO_PATH_UNEXPORT "/sys/class/gpio/unexport"
#define GPIO_VALUE "/value"
#define GPIO_EDGE "/edge"
#define GPIO_DIRECTION "/direction"
#define LINUX_OFFSET 54
#define GPIO_OUT 0
#define GPIO_IN 1
#define EDGE_RISING 0
#define EDGE_FALLING 1
#define EDGE_BOTH 2
/**
* Structure used for transceiver
* Has nb_Data_Pins number of data pins
* One clock
*
* nb_Data_Pins : Total number of data pins (clk not included)
* pins_Ports : Array of pins ports
* pins_Fds : Files descriptors opened on the pseudo files of the pins
* clk_Port : Port of the clock
* clk_fd : File descriptor for the clock
*/
typedef struct {
int nb_Data_Pins;
int *pins_Ports;
int *pins_Fds;
int clk_Port;
int clk_fd;
} CustomTransceiver;
int gpio_SetDataDirection(int portAddr, int mode);
int gpio_SetDataDirection_Range(int firstPortAddr, int lastPortAddr, int mode);
int gpio_Unexport(int portAddr);
int gpio_Read(int portAddr, void *buf, size_t count);
int gpio_Open(int portAddr, int flags);
int gpio_Close_Transceiver(CustomTransceiver transceiver);
int gpio_Set_Edge(int clkAddr, int edgeOption);
int gpio_Clocked_Read(int portAddr, int clkAddr, char *buf, size_t count, char *edgeOption, size_t edgeOptionSize);
int gpio_Clocked_Poll(int portAddr);
int gpio_Clocked_Poll_Monitor(int clkAddr, int portAddr);
int gpio_Read_Clocked_Poll(int portAddr, int clkAddr, char buf[], size_t count, useconds_t clkReadRate, int edgeOption);
int reader_Fast();
int gpio_Write_Fast(int fd, char value);
void gpio_Write_Fast_Tester(int nbTest, int internalRepetition);
int gpio_Export_Transceiver(CustomTransceiver transceiver, int mode);
int gpio_Unexport_Transceiver(CustomTransceiver transceiver);
CustomTransceiver gpio_Open_Transceiver(CustomTransceiver transceiver, int flags);
int gpio_Write_Rising_half_Transceiver(CustomTransceiver transceiver, char data[], int dataSize);
int gpio_Write_Falling_half_Transceiver(CustomTransceiver transceiver, char data[], int dataSize);
int gpio_Write_Rising_Transceiver(CustomTransceiver transceiver, char data[], int dataSize);
int gpio_Write_Falling_Transceiver(CustomTransceiver transceiver, char data[], int dataSize);
void transceiver_Print_Info(CustomTransceiver transceiver);
int gpio_Write_Both(CustomTransceiver transceiver, char dataFirst[], char dataSecond[], int dataSize);
void transceiver_Write_Tester(int nbTest, int internalRepetition, CustomTransceiver transceiver);
int main(int argc, char *argv[]) {
// //gpio_Write_Fast_Tester(10, 10000);
// int used_pins_Port[] = {7, 8, 9, 10};
// CustomTransceiver transceiver;
// transceiver.clk_Port = 24;
// transceiver.pins_Ports = used_pins_Port;
// transceiver.nb_Data_Pins = 4;
// if (gpio_Export_Transceiver(transceiver, GPIO_OUT) < 0) {
// //exit(EXIT_FAILURE);
// }
// usleep(100);
//
// transceiver = gpio_Open_Transceiver(transceiver, O_WRONLY);
//
//
// char data[] = {'1', '0', '1', '0'};
// //gpio_Write_Rising_half_Transceiver(transceiver, data, 4);
// transceiver_Print_Info(transceiver);
// if (gpio_Close_Transceiver(transceiver) < 0) {
// // exit(EXIT_FAILURE);
// }
// if (gpio_Unexport_Transceiver(transceiver) < 0) {
// // exit(EXIT_FAILURE);
// }
int used_pins_Port[] = {8, 9, 10};
CustomTransceiver transceiver;
transceiver.clk_Port = 7;
transceiver.pins_Ports = used_pins_Port;
transceiver.nb_Data_Pins = 3;
if (gpio_Export_Transceiver(transceiver, GPIO_OUT) < 0) {
//exit(EXIT_FAILURE);
}
usleep(100);
transceiver = gpio_Open_Transceiver(transceiver, O_WRONLY);
usleep(10);
transceiver_Write_Tester(10, 10000, transceiver);
if (gpio_Close_Transceiver(transceiver) < 0) {
// exit(EXIT_FAILURE);
}
if (gpio_Unexport_Transceiver(transceiver) < 0) {
// exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
// TODO split gpio export and mode set
// TODO Handle ports already exported "Device or resource bus"
/**
* Mandatory initialization for using port
* Set the port as an Input or Output
*
* Write the port in gpio/export.
* Write the mode in gpio*Port* /direction.
* Write the value in gpio*Port /value
*
* @param portAddr Address of the port used (refer to the .xdc file)0
* @param mode Output : 0, Input : 1.
* @return 0 In case of success
*/
int gpio_SetDataDirection(int portAddr, int mode) {
if (mode != GPIO_OUT && mode != GPIO_IN) {
perror("Enter a binary value for mode");
return -1;
}
char stringAddr[10];
char directionPath[80];
char valuePath[80];
int portAddrOffsetted = portAddr + LINUX_OFFSET;
snprintf(stringAddr, sizeof(stringAddr), "%d", portAddrOffsetted);
snprintf(directionPath, sizeof(directionPath), "%s%d%s", GPIO_PATH, portAddrOffsetted, GPIO_DIRECTION);
snprintf(valuePath, sizeof(valuePath), "%s%d%s", GPIO_PATH, portAddrOffsetted, GPIO_VALUE);
// Exporting the port
int fileDescriptor = open(GPIO_PATH_EXPORT, O_WRONLY);
if (fileDescriptor == -1)
perror("Opening gpio/export failed\r\n");
int returnFlag = (int) write(fileDescriptor, stringAddr, sizeof(stringAddr));
if (returnFlag == -1)
perror("Writing to gpio/export failed\r\n");
returnFlag = close(fileDescriptor);
if (returnFlag == -1)
perror("Closing gpio/export failed\r\n");
// Writing in/out in the direction file
fileDescriptor = open(directionPath, O_WRONLY);
if (fileDescriptor == -1)
perror("Opening gpio/gpio*Port*/direction failed\r\n");
if (mode == GPIO_OUT)
returnFlag = (int) write(fileDescriptor, "out", strlen("out"));
else
returnFlag = (int) write(fileDescriptor, "in", strlen("in"));
if (returnFlag == -1)
perror("Writing to gpio/gpio*Port*/direction failed\r\n");
returnFlag = close(fileDescriptor);
if (returnFlag == -1)
perror("Closing gpio/gpio*Port*/direction failed\r\n");
return 0;
}
/**
* Unexport the selected port, good practice to unexport all used port at the end of a program
*
* @param portAddr Port to unexport
* @return 0 in case of success, -1 in case of failure
*/
int gpio_Unexport(int portAddr) {
char stringAddr[10];
int portAddrOffsetted = portAddr + LINUX_OFFSET;
snprintf(stringAddr, sizeof(stringAddr), "%d", portAddrOffsetted);
// Writing the port address
int fileDescriptor = open(GPIO_PATH_UNEXPORT, O_WRONLY);
if (fileDescriptor == -1)
perror("Opening gpio/unexport failed\r\n");
int returnFlag = (int) write(fileDescriptor, stringAddr, sizeof(stringAddr));
if (returnFlag == -1)
perror("Writing to gpio/unexport failed\r\n");
returnFlag = close(fileDescriptor);
if (returnFlag == -1)
perror("Closing gpio/unexport failed\r\n");
return 0;
}
/**
* Read content found for the corresponding GPIO pin and put it in a buffer
*
* @param portAddr Target port
* @param buf Buffer to store the read result
* @param count Number of bytes to read
* @return the number of bytes read
*/
int gpio_Read(int portAddr, void *buf, size_t count) {
char valuePath[80];
int portAddrOffsetted = portAddr + LINUX_OFFSET;
snprintf(valuePath, sizeof(valuePath), "%s%d%s", GPIO_PATH, portAddrOffsetted, GPIO_VALUE);
// Reading the value from the value pseudo file
int fileDescriptor = open(valuePath, O_RDONLY);
if (fileDescriptor == -1)
perror("Opening gpio/gpio*Port*/value failed\r\n");
int returnFlag = (int) read(fileDescriptor, buf, count);
if (returnFlag == -1)
perror("Reading gpio/gpio*Port*/value failed\r\n");
returnFlag = close(fileDescriptor);
if (returnFlag == -1)
perror("Closing gpio/gpio*Port*/value failed\r\n");
return returnFlag;
}
/**
* Set a whole range of port as Inputs our Outputs
*
* @param firstPortAddr The address of the first port of the range
* @param lastPortAddr The address of the last port of the range
* @param mode Output : 0, Input : 1
* @return 0 In case of success
*/
int gpio_SetDataDirection_Range(int firstPortAddr, int lastPortAddr, int mode) {
if (firstPortAddr > lastPortAddr) {
perror("First address greater than last address");
return -1;
}
int index;
for (index = 0; index <= (lastPortAddr - firstPortAddr); index++) {
gpio_SetDataDirection(firstPortAddr + index, mode);
}
return 0;
}
/**
* Read the data on the portAddr synchronous to the clk
*
* @param portAddr
* @param clkAddr
* @param buf
* @param count
* @param edgeOption
* @return
*/
// TODO remove unused comment
// TODO make it work
int gpio_Clocked_Read(int portAddr, int clkAddr, char *buf, size_t count, char *edgeOption, size_t edgeOptionSize) {
//todo add edge check
char stringCLKAddr[10];
char portValuePath[80];
char clkValuePath[80];
char edgePath[80];
int portAddrOffSetted = portAddr + LINUX_OFFSET;
int clkAddrOffSetted = clkAddr + LINUX_OFFSET;
snprintf(stringCLKAddr, sizeof(stringCLKAddr), "%d", clkAddrOffSetted);
snprintf(portValuePath, sizeof(portValuePath), "%s%d%s", GPIO_PATH, portAddrOffSetted, GPIO_VALUE);
snprintf(edgePath, sizeof(edgePath), "%s%d%s", GPIO_PATH, clkAddrOffSetted, GPIO_EDGE);
snprintf(clkValuePath, sizeof(clkValuePath), "%s%d%s", GPIO_PATH, clkAddrOffSetted, GPIO_VALUE);
// Writing rising/edge/both in the edge file
int edge_fileDescriptor = open(edgePath, O_WRONLY);
if (edge_fileDescriptor == -1)
perror("Opening gpio/gpio*CLK*/edge failed\r\n");
if (write(edge_fileDescriptor, edgeOption, edgeOptionSize) == -1)
perror("Writing to gpio/gpio*CLK*/edge failed\r\n");
if (close(edge_fileDescriptor) == -1)
perror("Closing gpio/gpio*CLK*/edge failed\r\n");
int portFileDescriptor = open(portValuePath,/* O_APPEND |*/ O_RDONLY);
if (portFileDescriptor == -1)
perror("Opening gpio/gpio*Port*/value failed\r\n");
int clkFileDescriptor;
struct pollfd fds;
char buffer[2];
if ((clkFileDescriptor = open(clkValuePath, O_RDONLY)) < 0) {
perror("Opening clk value port failed");
return (-1);
}
int index;
fds.fd = clkFileDescriptor;
fds.events = POLLPRI;
// Fill the buffer with the result of the reading
for (index = 0; index < count - 1; index++) {
if (poll(&fds, 1, -1) < 0) {
perror("poll");
break;
}
lseek(clkFileDescriptor, 0, SEEK_SET);
if (read(portFileDescriptor, &buffer, 2) < 0) {
perror("read");
break;
}
buffer[1] = '\0';
buf[index] = buffer[0];
printf("Character read : %c\r\n", buffer[0]);
if (read(clkFileDescriptor, &buffer, 2) != 2) {
perror("Read");
break;
}
buffer[1] = '\0';
}
buf[count - 1] = '\0';
if (close(clkFileDescriptor) == -1)
perror("Closing gpio/gpio*CLK Port*/value failed\r\n");
if (close(portFileDescriptor) == -1)
perror("Closing gpio/gpio*Port*/value failed\r\n");
return 0;
}
int gpio_Clocked_Poll(int portAddr) {
char valuePath[60], edgePath[60];
snprintf(valuePath, sizeof(valuePath), "%s%d%s", GPIO_PATH, portAddr + LINUX_OFFSET, GPIO_VALUE);
snprintf(edgePath, sizeof(edgePath), "%s%d%s", GPIO_PATH, portAddr + LINUX_OFFSET, GPIO_EDGE);
int fd;
struct pollfd fds;
struct timeval tv;
char buffer[2];
int edgeFd = open(edgePath, O_WRONLY);
if (edgeFd < 0) {
perror("Opening edge failed");
exit(EXIT_FAILURE);
}
if (write(edgeFd, "rising", strlen("rising")) < 0) {
perror("Writing edge failed");
exit(EXIT_FAILURE);
}
close(edgeFd);
if ((fd = open(valuePath, O_RDONLY)) < 0) {
perror("Opening value path failed");
exit(EXIT_FAILURE);
}
while (1) {
fds.fd = fd;
fds.events = POLLPRI;
if (poll(&fds, 1, -1) < 0) {
perror("poll");
break;
}
gettimeofday(&tv, NULL);
lseek(fd, 0, SEEK_SET);
if (read(fd, &buffer, 2) != 2) {
perror("read");
break;
}
buffer[1] = '\0';
fprintf(stdout, "[%ld.%06ld]: %s\n", tv.tv_sec, tv.tv_usec, buffer);
}
close(fd);
return 0;
}
/**
* Monitor a pin and read it's content each rising edge on the clk pin
*
* @param clkAddr Address of the clock
* @param portAddr Address of the port to monitor
* @return will not return, infinite loop
*/
int gpio_Clocked_Poll_Monitor(int clkAddr, int portAddr) {
char clkValuePath[60], edgePath[60], portValuePath[60];
snprintf(clkValuePath, sizeof(clkValuePath), "%s%d%s", GPIO_PATH, clkAddr + LINUX_OFFSET, GPIO_VALUE);
snprintf(edgePath, sizeof(edgePath), "%s%d%s", GPIO_PATH, clkAddr + LINUX_OFFSET, GPIO_EDGE);
snprintf(portValuePath, sizeof(portValuePath), "%s%d%s", GPIO_PATH, portAddr + LINUX_OFFSET, GPIO_VALUE);
int fd;
struct pollfd fds;
struct timeval tv;
char buffer[2], portBufferReader[2];
int edgeFd = open(edgePath, O_WRONLY);
if (edgeFd < 0) {
perror("Opening edge failed");
exit(EXIT_FAILURE);
}
if (write(edgeFd, "rising", strlen("rising")) < 0) {
perror("Writing edge failed");
exit(EXIT_FAILURE);
}
close(edgeFd);
int portFd = open(portValuePath, O_RDONLY);
if (portFd < 0) {
perror("Opening monitored port failed");
exit(EXIT_FAILURE);
}
if ((fd = open(clkValuePath, O_RDONLY)) < 0) {
perror("Opening value path failed");
exit(EXIT_FAILURE);
}
while (1) {
fds.fd = fd;
fds.events = POLLPRI;
if (poll(&fds, 1, -1) < 0) {
perror("poll");
break;
}
gettimeofday(&tv, NULL);
lseek(fd, 0, SEEK_SET);
lseek(portFd, 0, SEEK_SET);
read(portFd, portBufferReader, 2);
if (read(fd, &buffer, 2) != 2) {
perror("read");
break;
}
buffer[1] = '\0';
portBufferReader[1] = '\0';
fprintf(stdout, "[%ld.%06ld]: %s\n", tv.tv_sec, tv.tv_usec, buffer);
printf("Data read : %s", portBufferReader);
}
close(fd);
close(portFd);
return 0;
}
/**
* Set the desired edge in the corresponding pseudo file of a clock
* @param clkAddr Address of the clock
* @param edgeOption EDGE_RISING / EDGE_FALLING / EDGE_BOTH
* @return 0 in case of success
*/
int gpio_Set_Edge(int clkAddr, int edgeOption) {
if (edgeOption != EDGE_BOTH && edgeOption != EDGE_FALLING && edgeOption != EDGE_RISING) {
perror("edgeOption invalid parameter\r\n");
exit(EXIT_FAILURE);
}
char edgePath[60];
snprintf(edgePath, sizeof(edgePath), "%s%d%s", GPIO_PATH, clkAddr + LINUX_OFFSET, GPIO_EDGE);
int edgeFd = open(edgePath, O_WRONLY);
if (edgeFd < 0) {
perror("Opening edge failed");
exit(EXIT_FAILURE);
}
if (edgeOption == EDGE_RISING) {
if (write(edgeFd, "rising", strlen("rising")) < 0) {
perror("Writing edge failed");
exit(EXIT_FAILURE);
}
} else if (edgeOption == EDGE_FALLING) {
if (write(edgeFd, "falling", strlen("falling")) < 0) {
perror("Writing edge failed");
exit(EXIT_FAILURE);
}
} else {
if (write(edgeFd, "both", strlen("both")) < 0) {
perror("Writing edge failed");
exit(EXIT_FAILURE);
}
}
close(edgeFd);
return 0;
}
/**
* Return a file descriptor for the opened pseudo file corresponding to the value of the port
*
* @param portAddr Port to open
* @param flags Flag passed to the open() function
* @return The file descriptor
*/
int gpio_Open(int portAddr, int flags) {
char portPath[60];
snprintf(portPath, sizeof(portPath), "%s%d%s", GPIO_PATH, portAddr + LINUX_OFFSET, GPIO_VALUE);
int portFd = open(portPath, flags);
return portFd;
}
// TODO add timeOUT
/**
* Wait for an specified edge on the clock port to fill the buffer with data read
* Will return only after the buffer is filled
* Discard the first character
* Read count - 1 character
*
* @param portAddr
* @param clkAddr
* @param buf Buffer that will be filled
* @param count Size of the buffer in character
* @param clkReadRate
* @param edgeOption EDGE_RISING / EDGE_FALLING / EDGE_BOTH
* @return The number of character read (should be count - 1)
*/
int
gpio_Read_Clocked_Poll(int portAddr, int clkAddr, char buf[], size_t count, useconds_t clkReadRate, int edgeOption) {
if (edgeOption != EDGE_BOTH && edgeOption != EDGE_FALLING && edgeOption != EDGE_RISING) {
perror("edgeOption invalid parameter\r\n");
exit(EXIT_FAILURE);
}
char clkValuePath[60], edgePath[60], portValuePath[60];
snprintf(clkValuePath, sizeof(clkValuePath), "%s%d%s", GPIO_PATH, clkAddr + LINUX_OFFSET, GPIO_VALUE);
snprintf(edgePath, sizeof(edgePath), "%s%d%s", GPIO_PATH, clkAddr + LINUX_OFFSET, GPIO_EDGE);
snprintf(portValuePath, sizeof(portValuePath), "%s%d%s", GPIO_PATH, portAddr + LINUX_OFFSET, GPIO_VALUE);
int fd;
struct pollfd fds;
char buffer[2], portBufferReader[2];
int edgeFd = open(edgePath, O_WRONLY | O_SYNC);
if (edgeFd < 0) {
perror("Opening edge failed");
exit(EXIT_FAILURE);
}
if (edgeOption == EDGE_RISING) {
if (write(edgeFd, "rising", strlen("rising")) < 0) {
perror("Writing edge failed");
exit(EXIT_FAILURE);
}
} else if (edgeOption == EDGE_FALLING) {
if (write(edgeFd, "falling", strlen("falling")) < 0) {
perror("Writing edge failed");
exit(EXIT_FAILURE);
}
} else {
if (write(edgeFd, "both", strlen("both")) < 0) {
perror("Writing edge failed");
exit(EXIT_FAILURE);
}
}
close(edgeFd);
int portFd = open(portValuePath, O_RDONLY | O_SYNC);
if (portFd < 0) {
perror("Opening monitored port failed");
exit(EXIT_FAILURE);
}
if ((fd = open(clkValuePath, O_RDONLY | O_SYNC)) < 0) {
perror("Opening value path failed");
exit(EXIT_FAILURE);
}
size_t index = 0;
// read it a first time
fds.fd = fd;
fds.events = POLLPRI;
if (poll(&fds, 1, -1) < 0) {
perror("poll");
exit(EXIT_FAILURE);
}
lseek(fd, 0, SEEK_SET);
if (read(fd, &buffer, 2) != 2) {
perror("read");
exit(EXIT_FAILURE);
}
buffer[1] = '\0';
// Actual reading loop
for (index = 0; index < count - 1; index++) {
fds.fd = fd;
fds.events = POLLPRI;
if (poll(&fds, 1, -1) < 0) {
perror("poll");
break;
}
lseek(fd, 0, SEEK_SET);
lseek(portFd, 0, SEEK_SET);
read(portFd, portBufferReader, 2);
if (read(fd, &buffer, 2) != 2) {
perror("read");
break;
}
buffer[1] = '\0';
portBufferReader[1] = '\0';
buf[index] = portBufferReader[0];
}
close(fd);
close(portFd);
buf[index] = '\0';
return index;
}
/**
* Work the same way as gpio_Read_Clocked_Poll but this time without openging or closing pseudos files
* Thus making it faser
* @param portFd File descriptor of the port to monitor
* @param clkFd File descriptor of the clock
* @param buf Buffer that will be filled
* @param count Size of the buffer in character
* @return The number of character read (should be count - 1)
*/
size_t
gpio_Read_Clocked_Poll_Fast(int portFd, int clkFd, char buf[], size_t count) {
struct pollfd fds;
char buffer[2], portBufferReader[2];
size_t index = 0;
// reads it a first time
fds.fd = clkFd;
fds.events = POLLPRI;
if (poll(&fds, 1, -1) < 0) {
perror("poll");
exit(EXIT_FAILURE);
}
lseek(clkFd, 0, SEEK_SET);
if (read(clkFd, &buffer, 2) != 2) {
perror("read");
exit(EXIT_FAILURE);
}
buffer[1] = '\0';
// Actual reading loop
for (index = 0; index < count - 1; index++) {
fds.fd = clkFd;
fds.events = POLLPRI;
if (poll(&fds, 1, -1) < 0) {
perror("poll");
break;
}
lseek(clkFd, 0, SEEK_SET);
lseek(portFd, 0, SEEK_SET);
read(portFd, portBufferReader, 2);
if (read(clkFd, &buffer, 2) != 2) {
perror("read");
break;
}
buffer[1] = '\0';
portBufferReader[1] = '\0';
buf[index] = portBufferReader[0];
}
buf[index] = '\0';
return index;
}
int reader_Fast() {
int clkIn = 32, dataIn = 33;
char bufferReader[20][9];
gpio_SetDataDirection(clkIn, GPIO_IN);
gpio_SetDataDirection(dataIn, GPIO_IN);
gpio_Set_Edge(clkIn, EDGE_RISING);
int clkFd = gpio_Open(clkIn, O_RDONLY);
if (clkFd < 0) {
perror("Port opening impossible");
exit(EXIT_FAILURE);
}
int dataFd = gpio_Open(dataIn, O_RDONLY);
if (dataFd < 0) {
perror("Port opening impossible");
exit(EXIT_FAILURE);
}
for (int index = 0; index < 20; index++) {
gpio_Read_Clocked_Poll_Fast(dataFd, clkFd, bufferReader[index], 9);
}
for (int i = 0; i < 20; i++) {
printf("index : %d, Data Read : %s\r\n", i, bufferReader[i]);
}
close(clkFd);
close(dataFd);
gpio_Unexport(clkIn);
gpio_Unexport(dataIn);
return 0;
}
/**
* Write a value directly on the pseudo file without opening it again
* @param fd File descriptor, pseudo file opened
* @param value Value to write '0' or '1'
* @return 0 in case of success, -1 in case of failure
*/
int gpio_Write_Fast(int fd, char value) {
if (value != '0' && value != '1') {
perror("Writing failed, value must be 1 or 0");
return -1;
}
char bufferWriter[2];
bufferWriter[0] = value;
bufferWriter[1] = '\0';
write(fd, bufferWriter, 2);
return 0;
}
/**
* Will test the function gpio_Write_Fast by measuring it's average write frequency.
* Port Ja1 is used
* @param nbTest
* @param internalRepetition
*/
void gpio_Write_Fast_Tester(int nbTest, int internalRepetition) {
int dataOut = 24;
gpio_SetDataDirection(24, GPIO_OUT);
int fd = gpio_Open(24, O_WRONLY);
usleep(10);
int index = 0;
struct timeval tv, tv2;
long testResult[nbTest];
long testResultUS[nbTest];
for (int j = 0; j < nbTest; j++) {
gettimeofday(&tv, NULL);
// Insert function to test here
for (index = 0; index < internalRepetition; index++) {
gpio_Write_Fast(fd, '0');
gpio_Write_Fast(fd, '1');
}
gettimeofday(&tv2, NULL);
testResult[j] = (int) (tv2.tv_sec * 1000000 + tv2.tv_usec) - (int) (tv.tv_sec * 1000000 + tv.tv_usec);
}
long result = 0;
for (int k = 1; k < nbTest - 1; k++) {
result += testResult[k];
}
double averageTimeElapsed = ((double) result) / (nbTest - 2);
printf("Average time for internal function measured is : %lf\r\n us", averageTimeElapsed);
double period = averageTimeElapsed / internalRepetition;
printf("Average period is : %lf us\r\n", period);
double frequency = ((double) 1.00 / period) * 1000000;
printf("Average frequency is : %lf Hz\r\n", frequency);
close(fd);
gpio_Unexport(24);
}
/**
* Export all the port of a transceiver and its clock in the correct direction
* @param transceiver Transceiver to export
* @param mode GPIO_IN or GPIO_OUT
* @return 0 in case of success, -1 in case of failure
*/
int gpio_Export_Transceiver(CustomTransceiver transceiver, int mode) {
int i, flag;
for (i = 0; i < transceiver.nb_Data_Pins; i++) {
flag = gpio_SetDataDirection(*(transceiver.pins_Ports + i), mode);
if (flag < 0)
return -1;
usleep(1);
}
flag = gpio_SetDataDirection(transceiver.clk_Port, mode);
if (flag < 0)
return -1;
}
/**
* Unexport all the port of a transceiver and its clock
* @param transceiver Transceiver to unexport
* @return 0 in case of success, -1 in case of failure
*/
int gpio_Unexport_Transceiver(CustomTransceiver transceiver) {
int i, flag;
for (i = 0; i < transceiver.nb_Data_Pins; i++) {
flag = gpio_Unexport(*(transceiver.pins_Ports + i));
if (flag < 0)
return -1;
usleep(1);
}
flag = gpio_Unexport(transceiver.clk_Port);
if (flag < 0)
return -1;
return 0;
}
/**
* Open all the port of the transceiver and its clock
* Assigning them files descriptors
* @param transceiver Transceiver to set up
* @param flags Opening flags (O_WRONLY)
* @return 0 in case of success, -1 in case of failure
*/
CustomTransceiver gpio_Open_Transceiver(CustomTransceiver transceiver, int flags) {
int i;
transceiver.pins_Fds = malloc(sizeof(int) * transceiver.nb_Data_Pins);
if (transceiver.pins_Fds == NULL) {
perror("Memory allocation for port files descriptor failed");
exit(EXIT_FAILURE);
}
for (i = 0; i < transceiver.nb_Data_Pins; i++) {
*(transceiver.pins_Fds + i) = gpio_Open(*(transceiver.pins_Ports + i), flags);
if (*(transceiver.pins_Fds + i) < 0) {
perror("Transceiver opening port files descriptors failed");
exit(EXIT_FAILURE);
}
usleep(2);
}
transceiver.clk_fd = gpio_Open(transceiver.clk_Port, flags);
if (transceiver.clk_fd < 0) {
perror("Transceiver opening clock file descriptor failed");
exit(EXIT_FAILURE);
}
return transceiver;
}
/**
* Close all the files descriptor opened for the transceiver
* Including the clock file descriptor
* @param transceiver Transceiver to close
* @return 0 in case of success, -1 in case of failure
*/
int gpio_Close_Transceiver(CustomTransceiver transceiver) {
int i, flag;
for (i = 0; i < transceiver.nb_Data_Pins; i++) {
flag = close(*(transceiver.pins_Fds + i));
if (flag < 0) {
perror("Transceiver closing port files descriptors failed");
return -1;
}
usleep(1);
}
transceiver.clk_fd = close(transceiver.clk_fd);
if (transceiver.clk_fd < 0) {
perror("Transceiver closing clock file descriptor failed");
return -1;
}
return 0;
}
/**
* Write port by port a data, then rise the clock
* Data array must be same number as the data pin numbers
* @param transceiver Transceiver containing all the information about clock and port
* @param data Array of bit to transmit
* @param dataSize Size of the array of data
* @return 0 in case of success, -1 in case of failure
*/
int gpio_Write_Rising_half_Transceiver(CustomTransceiver transceiver, char data[], int dataSize) {
int i, flag;
if (dataSize != transceiver.nb_Data_Pins) {
perror("Data size mismatch transceiver port number");
return -1;
}
// Write data
for (i = 0; i < transceiver.nb_Data_Pins; i++) {
flag = gpio_Write_Fast(*(transceiver.pins_Fds + i), data[i]);
if (flag < 0) {
perror("Transceiver Writing data failed");
return -1;
}
}
// Write Clk high
flag = gpio_Write_Fast(transceiver.clk_fd, '1');
if (flag < 0) {
perror("Transceiver writing clock failed");
return -1;
}
return 0;
}
/**
* Write port by port a data, then lower the clock
* Data array must be same number as the data pin numbers
* @param transceiver Transceiver containing all the information about clock and port
* @param data Array of bit to transmit
* @param dataSize Size of the array of data
* @return 0 in case of success, -1 in case of failure
*/
int gpio_Write_Falling_half_Transceiver(CustomTransceiver transceiver, char data[], int dataSize) {
int i, flag;
if (dataSize != transceiver.nb_Data_Pins) {
perror("Data size mismatch transceiver port number");
return -1;
}
// Write data
for (i = 0; i < transceiver.nb_Data_Pins; i++) {
flag = gpio_Write_Fast(*(transceiver.pins_Fds + i), data[i]);
if (flag < 0) {
perror("Transceiver Writing data failed");
return -1;
}
}
// Write Clk low
flag = gpio_Write_Fast(transceiver.clk_fd, '0');
if (flag < 0) {
perror("Transceiver writing clock failed");
return -1;
}
return 0;
}
/**
* Writing data on both edge of the clock
*
* @param transceiver Transceiver containing all the information about clock and port
* @param dataFirst First set of data to transmit on rising edge
* @param dataSecond Second set of data to transmit on falling edge
* @param dataSize Size of the array of data