Advertisement
Guest User

Raspberryp pi 3, hw gpio flip

a guest
Sep 23rd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.54 KB | None | 0 0
  1. //
  2. //  How to access GPIO registers from C-code on the Raspberry-Pi
  3. //  Example program
  4. //  15-January-2012
  5. //  Dom and Gert
  6. //  Revised: 15-Feb-2013
  7. //  https://elinux.org/RPi_GPIO_Code_Samples
  8.  
  9. //  Revised: Sep-2019
  10. //  Adding timer fun
  11.  
  12. // Access from ARM Running Linux
  13.  
  14. #define BCM2708_PERI_BASE        0x3F000000
  15. #define GPIO_BASE                (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <fcntl.h>
  21. #include <sys/mman.h>
  22. #include <unistd.h>
  23. #include <time.h>
  24.  
  25. #define PAGE_SIZE (4*1024)
  26. #define BLOCK_SIZE (4*1024)
  27.  
  28. int  mem_fd;
  29. void *gpio_map;
  30.  
  31. // I/O access
  32. volatile unsigned *gpio;
  33.  
  34.  
  35. // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
  36. #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  37. #define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3))
  38. #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  39.  
  40. #define GPIO_SET *(gpio+7)  // sets   bits which are 1 ignores bits which are 0
  41. #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
  42.  
  43. #define GET_GPIO(g) (*(gpio+13)&(1<<g)) // 0 if LOW, (1<<g) if HIGH
  44.  
  45. #define GPIO_PULL *(gpio+37) // Pull up/pull down
  46. #define GPIO_PULLCLK0 *(gpio+38) // Pull up/pull down clock
  47.  
  48. void setup_io();
  49.  
  50. void printButton(int g)
  51. {
  52.   if (GET_GPIO(g)) // !=0 <-> bit is 1 <- port is HIGH=3.3V
  53.     printf("Button pressed!\n");
  54.   else // port is LOW=0V
  55.     printf("Button released!\n");
  56. }
  57.  
  58. int main(int argc, char **argv)
  59. {
  60.   int g,rep;
  61.  
  62.   // Set up gpi pointer for direct register access
  63.   setup_io();
  64.  
  65.   // Switch GPIO 7..11 to output mode
  66.  
  67.  /************************************************************************\
  68.   * You are about to change the GPIO settings of your computer.          *
  69.   * Mess this up and it will stop working!                               *
  70.   * It might be a good idea to 'sync' before running this program        *
  71.   * so at least you still have your code changes written to the SD-card! *
  72.  \************************************************************************/
  73.  
  74.   // Set GPIO pin 4 to output
  75.     INP_GPIO(4); // must use INP_GPIO before we can use OUT_GPIO
  76.     OUT_GPIO(4);
  77.    
  78.     clock_t start_t, counter_t;
  79.    
  80.     const double frequency = 100000;
  81.     const double delay = CLOCKS_PER_SEC/frequency;
  82.    
  83.     printf("CLOCKS_PER_SEC %ld\n", CLOCKS_PER_SEC);
  84.     printf("delay %f\n", delay);
  85.    
  86.     counter_t = clock() + delay;
  87.    
  88.     while(1)
  89.     {
  90.       while ( clock() < counter_t )
  91.       {}
  92.       counter_t += delay;
  93.       GPIO_SET = 1<<4;
  94.      
  95.       while ( clock() < counter_t )
  96.       {}
  97.       counter_t += delay;
  98.       GPIO_CLR = 1<<4;
  99.     }
  100.  
  101.   return 0;
  102.  
  103. } // main
  104.  
  105.  
  106. //
  107. // Set up a memory regions to access GPIO
  108. //
  109. void setup_io()
  110. {
  111.    /* open /dev/mem */
  112.    if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
  113.       printf("can't open /dev/mem \n");
  114.       exit(-1);
  115.    }
  116.  
  117.    /* mmap GPIO */
  118.    gpio_map = mmap(
  119.       NULL,             //Any adddress in our space will do
  120.       BLOCK_SIZE,       //Map length
  121.       PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
  122.       MAP_SHARED,       //Shared with other processes
  123.       mem_fd,           //File to map
  124.       GPIO_BASE         //Offset to GPIO peripheral
  125.    );
  126.  
  127.    close(mem_fd); //No need to keep mem_fd open after mmap
  128.  
  129.    if (gpio_map == MAP_FAILED) {
  130.       printf("mmap error %d\n", (int)gpio_map);//errno also set!
  131.       exit(-1);
  132.    }
  133.  
  134.    // Always use volatile pointer!
  135.    gpio = (volatile unsigned *)gpio_map;
  136.  
  137.  
  138. } // setup_io
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement