stub.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 c=0;
  63. while(read(fifos[pin], &c,1)<=0)usleep(10);
  64. return (c=='0')?0:1;
  65. #else
  66. return GET_GPIO(pin);
  67. #endif
  68. #else
  69. return digitalRead(pin);
  70. #endif
  71. }
  72. void init_read_pin(int d, int clk, int ack)
  73. {
  74. #ifdef __linux__
  75. #ifndef ARM
  76. fifos[d]=open("/tmp/fifodata", O_RDONLY | O_NONBLOCK);
  77. fifos[clk]=open("/tmp/fifoclk", O_RDONLY | O_NONBLOCK);
  78. fifos[ack]=open("/tmp/fifoack", O_WRONLY );
  79. #else
  80. INP_GPIO(d);
  81. INP_GPIO(clk);
  82. OUT_GPIO(ack);
  83. #endif
  84. #else
  85. pinMode(d, INPUT);
  86. pinMode(clk, INPUT);
  87. pinMode(ack, OUTPUT);
  88. #endif
  89. }
  90. void init_write_pin(int d, int clk, int ack)
  91. {
  92. #ifdef __linux__
  93. #ifndef ARM
  94. fifos[d]=open("/tmp/fifoack", O_RDONLY | O_NONBLOCK);
  95. fifos[clk]=open("/tmp/fifodata", O_WRONLY);
  96. fifos[ack]=open("/tmp/fifoclk", O_WRONLY);
  97. #else
  98. OUT_GPIO(d);
  99. OUT_GPIO(clk);
  100. INP_GPIO(ack);
  101. #endif
  102. #else
  103. pinMode(d, OUTPUT);
  104. pinMode(clk, OUTPUT);
  105. pinMode(ack, INPUT);
  106. #endif
  107. }
  108. void free_fifo(int d, int clk, int ack)
  109. {
  110. #ifdef __linux__
  111. #ifndef ARM
  112. close(fifos[d]);
  113. close(fifos[clk]);
  114. close(fifos[ack]);
  115. #endif
  116. #endif
  117. }