123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #include "bus.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "io.h"
- #ifdef __linux__
- #ifndef ARM
- int fifos[128];
- #endif
- #endif
- #ifdef ARM
- int mem_fd;
- void *gpio_map;
-
- // I/O access
- volatile unsigned *gpio;
-
- void setup_io()
- {
- /* open /dev/mem */
- if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
- printf("can't open /dev/mem \n");
- exit(-1);
- }
-
- /* mmap GPIO */
- gpio_map = mmap(
- NULL, //Any adddress in our space will do
- BLOCK_SIZE, //Map length
- PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
- MAP_SHARED, //Shared with other processes
- mem_fd, //File to map
- GPIO_BASE //Offset to GPIO peripheral
- );
-
- close(mem_fd); //No need to keep mem_fd open after mmap
-
- if (gpio_map == MAP_FAILED) {
- printf("mmap error %d\n", (int)gpio_map);//errno also set!
- exit(-1);
- }
-
- // Always use volatile pointer!
- gpio = (volatile unsigned *)gpio_map;
-
-
- } // setup_io
-
- #endif
- void write_pin(int pin, int value)
- {
- #ifdef __linux__
- #ifndef ARM
- char c = (value)?'1':'0';
- write(fifos[pin], &c, 1);
- #else
- GPIO_WRITE(pin, value);
- #endif
- #else
- digitalWrite(pin, value);
- #endif
- }
- int read_pin(int pin)
- {
- #ifdef __linux__
- #ifndef ARM
- int f = fifos[pin];
- int c=0;
- while(read(fifos[pin], &c,1)<=0)usleep(10);
- return (c=='0')?0:1;
- #else
- return GET_GPIO(pin);
- #endif
- #else
- return digitalRead(pin);
- #endif
- }
- void init_read_pin(int d, int clk, int ack)
- {
- #ifdef __linux__
- #ifndef ARM
- fifos[d]=open("/tmp/fifodata", O_RDONLY | O_NONBLOCK);
- fifos[clk]=open("/tmp/fifoclk", O_RDONLY | O_NONBLOCK);
- fifos[ack]=open("/tmp/fifoack", O_WRONLY );
- #else
- INP_GPIO(d);
- INP_GPIO(clk);
- OUT_GPIO(ack);
- #endif
- #else
- pinMode(d, INPUT);
- pinMode(clk, INPUT);
- pinMode(ack, OUTPUT);
- #endif
- }
- void init_write_pin(int d, int clk, int ack)
- {
- #ifdef __linux__
- #ifndef ARM
- fifos[d]=open("/tmp/fifoack", O_RDONLY | O_NONBLOCK);
- fifos[clk]=open("/tmp/fifodata", O_WRONLY);
- fifos[ack]=open("/tmp/fifoclk", O_WRONLY);
- #else
- OUT_GPIO(d);
- OUT_GPIO(clk);
- INP_GPIO(ack);
- #endif
- #else
- pinMode(d, OUTPUT);
- pinMode(clk, OUTPUT);
- pinMode(ack, INPUT);
- #endif
- }
- void free_fifo(int d, int clk, int ack)
- {
- #ifdef __linux__
- #ifndef ARM
- close(fifos[d]);
- close(fifos[clk]);
- close(fifos[ack]);
- #endif
- #endif
- }
|