You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gpio-raw.cpp 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "gpio-raw.h"
  2. int mem_fd;
  3. char *gpio_mem, *gpio_map;
  4. char *spi0_mem, *spi0_map;
  5. // I/O access
  6. volatile unsigned *gpio;
  7. //
  8. // Set up a memory regions to access GPIO
  9. //
  10. bool setup_io()
  11. {
  12. /* open /dev/mem */
  13. if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
  14. /*printf("can't open /dev/mem \n");
  15. exit (-1);*/
  16. return false;
  17. }
  18. /* mmap GPIO */
  19. // Allocate MAP block
  20. if ((gpio_mem = (char*)malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
  21. /*printf("allocation error \n");
  22. exit (-1);*/
  23. return false;
  24. }
  25. // Make sure pointer is on 4K boundary
  26. if ((unsigned long)gpio_mem % PAGE_SIZE)
  27. gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);
  28. // Now map it
  29. gpio_map = (char *)mmap(
  30. (caddr_t)gpio_mem,
  31. BLOCK_SIZE,
  32. PROT_READ|PROT_WRITE,
  33. MAP_SHARED|MAP_FIXED,
  34. mem_fd,
  35. GPIO_BASE
  36. );
  37. if ((long)gpio_map < 0) {
  38. /*printf("mmap error %d\n", (int)gpio_map);
  39. exit (-1);*/
  40. return false;
  41. }
  42. // Always use volatile pointer!
  43. gpio = (volatile unsigned *)gpio_map;
  44. return true;
  45. } // setup_io