stub.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "bus.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "io.h"
  6. #ifdef __linux__
  7. #ifndef ARM
  8. int fifos[128];
  9. #endif
  10. #endif
  11. #ifdef ARM
  12. int mem_fd;
  13. void *gpio_map;
  14. // I/O access
  15. volatile unsigned *gpio;
  16. void setup_io()
  17. {
  18. /* open /dev/mem */
  19. if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
  20. printf("can't open /dev/mem \n");
  21. exit(-1);
  22. }
  23. /* mmap GPIO */
  24. gpio_map = mmap(
  25. NULL, //Any adddress in our space will do
  26. BLOCK_SIZE, //Map length
  27. PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
  28. MAP_SHARED, //Shared with other processes
  29. mem_fd, //File to map
  30. GPIO_BASE //Offset to GPIO peripheral
  31. );
  32. close(mem_fd); //No need to keep mem_fd open after mmap
  33. if (gpio_map == MAP_FAILED) {
  34. printf("mmap error %d\n", (int)gpio_map);//errno also set!
  35. exit(-1);
  36. }
  37. // Always use volatile pointer!
  38. gpio = (volatile unsigned *)gpio_map;
  39. } // setup_io
  40. #endif
  41. void write_pin(int pin, int value)
  42. {
  43. #ifdef __linux__
  44. #ifndef ARM
  45. char c = (value)?'1':'0';
  46. write(fifos[pin], &c, 1);
  47. #else
  48. GPIO_WRITE(pin, value);
  49. #endif
  50. #else
  51. digitalWrite(pin, value);
  52. #endif
  53. }
  54. int read_pin(int pin)
  55. {
  56. #ifdef __linux__
  57. #ifndef ARM
  58. int f = fifos[pin];
  59. int c=0;
  60. while(read(fifos[pin], &c,1)<=0)usleep(10);
  61. return (c=='0')?0:1;
  62. #else
  63. return GET_GPIO(pin);
  64. #endif
  65. #else
  66. return digitalRead(pin);
  67. #endif
  68. }
  69. void init_read_pin(int d, int clk, int ack)
  70. {
  71. #ifdef __linux__
  72. #ifndef ARM
  73. fifos[d]=open("/tmp/fifodata", O_RDONLY | O_NONBLOCK);
  74. fifos[clk]=open("/tmp/fifoclk", O_RDONLY | O_NONBLOCK);
  75. fifos[ack]=open("/tmp/fifoack", O_WRONLY );
  76. #else
  77. INP_GPIO(d);
  78. INP_GPIO(clk);
  79. OUT_GPIO(ack);
  80. #endif
  81. #else
  82. pinMode(d, INPUT);
  83. pinMode(clk, INPUT);
  84. pinMode(ack, OUTPUT);
  85. #endif
  86. }
  87. void init_write_pin(int d, int clk, int ack)
  88. {
  89. #ifdef __linux__
  90. #ifndef ARM
  91. fifos[d]=open("/tmp/fifoack", O_RDONLY | O_NONBLOCK);
  92. fifos[clk]=open("/tmp/fifodata", O_WRONLY);
  93. fifos[ack]=open("/tmp/fifoclk", O_WRONLY);
  94. #else
  95. OUT_GPIO(d);
  96. OUT_GPIO(clk);
  97. INP_GPIO(ack);
  98. #endif
  99. #else
  100. pinMode(d, OUTPUT);
  101. pinMode(clk, OUTPUT);
  102. pinMode(ack, INPUT);
  103. #endif
  104. }
  105. void free_fifo(int d, int clk, int ack)
  106. {
  107. #ifdef __linux__
  108. #ifndef ARM
  109. close(fifos[d]);
  110. close(fifos[clk]);
  111. close(fifos[ack]);
  112. #endif
  113. #endif
  114. }