stub.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #else
  41. void setup_io()
  42. {
  43. }
  44. #endif
  45. void write_pin(int pin, int value)
  46. {
  47. #ifdef __linux__
  48. #ifndef ARM
  49. char c = (value)?'1':'0';
  50. write(fifos[pin], &c, 1);
  51. #else
  52. GPIO_WRITE(pin, value);
  53. #endif
  54. #else
  55. digitalWrite(pin, value);
  56. #endif
  57. }
  58. int read_pin(int pin)
  59. {
  60. #ifdef __linux__
  61. #ifndef ARM
  62. int f = fifos[pin];
  63. int c=0;
  64. while(read(fifos[pin], &c,1)<=0)usleep(10);
  65. return (c=='0')?0:1;
  66. #else
  67. return GET_GPIO(pin);
  68. #endif
  69. #else
  70. return digitalRead(pin);
  71. #endif
  72. }
  73. void init_read_pin(int d, int clk, int ack)
  74. {
  75. #ifdef __linux__
  76. #ifndef ARM
  77. fifos[d]=open("/tmp/fifodata", O_RDONLY | O_NONBLOCK);
  78. fifos[clk]=open("/tmp/fifoclk", O_RDONLY | O_NONBLOCK);
  79. fifos[ack]=open("/tmp/fifoack", O_WRONLY );
  80. #else
  81. INP_GPIO(d);
  82. INP_GPIO(clk);
  83. OUT_GPIO(ack);
  84. #endif
  85. #else
  86. pinMode(d, INPUT);
  87. pinMode(clk, INPUT);
  88. pinMode(ack, OUTPUT);
  89. #endif
  90. }
  91. void init_write_pin(int d, int clk, int ack)
  92. {
  93. #ifdef __linux__
  94. #ifndef ARM
  95. fifos[d]=open("/tmp/fifoack", O_RDONLY | O_NONBLOCK);
  96. fifos[clk]=open("/tmp/fifodata", O_WRONLY);
  97. fifos[ack]=open("/tmp/fifoclk", O_WRONLY);
  98. #else
  99. OUT_GPIO(d);
  100. OUT_GPIO(clk);
  101. INP_GPIO(ack);
  102. #endif
  103. #else
  104. pinMode(d, OUTPUT);
  105. pinMode(clk, OUTPUT);
  106. pinMode(ack, INPUT);
  107. #endif
  108. }
  109. void free_fifo(int d, int clk, int ack)
  110. {
  111. #ifdef __linux__
  112. #ifndef ARM
  113. close(fifos[d]);
  114. close(fifos[clk]);
  115. close(fifos[ack]);
  116. #endif
  117. #endif
  118. }