1 | #include <stdio.h>
|
2 | #include <string.h>
|
3 | #include <unistd.h>
|
4 | #include <fcntl.h>
|
5 | #include <errno.h>
|
6 | #include <termios.h>
|
7 | #include <sys/types.h>
|
8 | #include <sys/select.h>
|
9 | #include <sys/mman.h>
|
10 |
|
11 | #define MAP_SIZE 4096UL
|
12 | #define MAP_MASK (MAP_SIZE -1)
|
13 | #define PIOB_BASE 0xfffff600UL
|
14 |
|
15 | //Output-Pins
|
16 | #define PIO_B22 ((unsigned long) 1 << 22)
|
17 | #define PIO_B25 ((unsigned long) 1 << 25)
|
18 | #define PIO_B05 ((unsigned long) 1 << 5)
|
19 | #define PIO_B04 ((unsigned long) 1 << 4)
|
20 | #define PIO_B24 ((unsigned long) 1 << 24)
|
21 | #define PIO_B26 ((unsigned long) 1 << 26)
|
22 | #define PIO_B27 ((unsigned long) 1 << 27)
|
23 | #define PIO_B23 ((unsigned long) 1 << 23)
|
24 |
|
25 | //configure Register
|
26 | #define PIOB_PER PIOB_BASE + 0x00
|
27 | #define PIOB_PDR PIOB_BASE + 0x04
|
28 | #define PIOB_OER PIOB_BASE + 0x10
|
29 | #define PIOB_ODR PIOB_BASE + 0x14
|
30 | #define PIOB_OSR PIOB_BASE + 0x18
|
31 | #define PIOB_SODR PIOB_BASE + 0x30
|
32 | #define PIOB_CODR PIOB_BASE + 0x34
|
33 | #define PIOB_ODSR PIOB_BASE + 0x38
|
34 | #define PIOB_PDSR PIOB_BASE + 0x3C
|
35 | #define PIOB_IFER PIOB_BASE + 0x20
|
36 | #define PIOB_IFDR PIOB_BASE + 0x24
|
37 | //Configure macros
|
38 | #define PIOB_Enable_Register(addr, pio) *((unsigned long *) (addr + (PIOB_PER & MAP_MASK))) = pio
|
39 | #define PIOB_Disable_Register(addr, pio) *((unsigned long *) (addr + (PIOB_PDR & MAP_MASK))) = pio
|
40 | #define PIOB_Enable_Output(addr, pio) *((unsigned long *) (addr + (PIOB_OER & MAP_MASK))) = pio
|
41 | #define PIOB_Disable_Output(addr, pio) *((unsigned long *) (addr + (PIOB_ODR & MAP_MASK))) = pio
|
42 | #define PIOB_Enable_Inputfilter(addr, pio) *((unsigned long *) (addr + (PIOB_IFER & MAP_MASK))) = pio
|
43 | #define PIOB_Enable_Input(addr, pio) \
|
44 | (*((unsigned long *) (addr + (PIOB_ODR & MAP_MASK))) = pio) && \
|
45 | (*((unsigned long *) (addr + (PIOB_IFER & MAP_MASK))) = pio) && \
|
46 | (*((unsigned long *) (addr + (PIOB_CODR & MAP_MASK))) = pio)
|
47 |
|
48 | #define PIOB_Set_Low(addr, pio) *((unsigned long *) (addr + (PIOB_CODR & MAP_MASK))) = pio
|
49 | #define PIOB_Set_High(addr, pio) *((unsigned long *) (addr + (PIOB_SODR & MAP_MASK))) = pio
|
50 |
|
51 | short lvl;
|
52 | unsigned long pin;
|
53 |
|
54 | void printError(){
|
55 | printf("\n\n!!!Error!!!\nSyntax is: setPin <Pin> 1|0 \nExample:\n################\n# setPin RTS 1 #\n################\nPossible Pins are:\nDSR\nRI\nRXD\nTXD\nDTR\nRTS\nCTS\nDCD\n\n\n");
|
56 | exit(1);
|
57 | }
|
58 |
|
59 | int main (int count, char *argv[]){
|
60 | if(count!=3){
|
61 | printError();
|
62 | }else{
|
63 | if(*argv[2]=='1'){
|
64 | lvl=1;
|
65 | }else if(*argv[2]=='0'){
|
66 | lvl=0;
|
67 | }else{
|
68 | printError();
|
69 | }
|
70 | }
|
71 | int fd;
|
72 | void *map_base;
|
73 | if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
|
74 | {
|
75 | printf("Fehler beim Zugriff auf /dev/mem");
|
76 | exit(1); //Fehler beim Zugriff auf dev-mem
|
77 | }
|
78 | map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, PIOB_BASE & ~MAP_MASK);
|
79 | if (map_base == (void *) -1)
|
80 | {
|
81 | printf("Fehler beim Zugriff auf mmap");
|
82 | exit(1); //Zugriffsfehler
|
83 | }
|
84 | PIOB_Enable_Register(map_base, PIO_B22 | PIO_B25 | PIO_B05 | PIO_B04 | PIO_B24 | PIO_B26 | PIO_B27 | PIO_B23);
|
85 | PIOB_Enable_Output(map_base, PIO_B22 | PIO_B25 | PIO_B05 | PIO_B04 | PIO_B24 | PIO_B26 | PIO_B27 | PIO_B23);
|
86 |
|
87 | char *name=argv[1];
|
88 | if(strcmp(name,"DSR")==0){
|
89 | pin=PIO_B22;
|
90 | }else if(strcmp(name,"RI")==0){
|
91 | pin=PIO_B25;
|
92 | }else if(strcmp(name,"RXD")==0){
|
93 | pin=PIO_B05;
|
94 | }else if(strcmp(name,"TXD")==0){
|
95 | pin=PIO_B04;
|
96 | }else if(strcmp(name,"DTR")==0){
|
97 | pin=PIO_B24;
|
98 | }else if(strcmp(name,"RTS")==0){
|
99 | pin=PIO_B26;
|
100 | }else if(strcmp(name,"CTS")==0){
|
101 | pin=PIO_B27;
|
102 | }else if(strcmp(name,"DCD")==0){
|
103 | pin=PIO_B23;
|
104 | }
|
105 |
|
106 | switch(lvl){
|
107 | case 0: PIOB_Set_Low(map_base, pin); printf("Pin %s wurde in IF0 auf 0 gesetzt.\n",name); break;
|
108 | case 1: PIOB_Set_High(map_base, pin);printf("Pin %s wurde in IF0 auf 1 gesetzt.\n",name); break;
|
109 | }
|
110 |
|
111 | if (munmap(map_base, MAP_SIZE) == -1) exit(1);
|
112 | close(fd);
|
113 | }
|