Guest User

rtgpio.c

a guest
Mar 19th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <sys/ioctl.h>
  9. #include <signal.h>
  10. #include <sys/mman.h>
  11.  
  12. #include <native/task.h>
  13. #include <native/timer.h>
  14.  
  15. #define TIMESLEEP 10000
  16.  
  17. static volatile uint32_t *gpio;
  18.  
  19. RT_TASK blink_task;
  20. int fd;
  21.  
  22. void blink(void *arg __attribute__((__unused__)))
  23. {
  24.  
  25.     int iomask = 0;
  26.    
  27.     rt_task_set_periodic(NULL, TM_NOW, TIMESLEEP);
  28.  
  29.     while(1) {
  30.         rt_task_wait_period(NULL);
  31.        
  32.         if( iomask == 0 ) {
  33.             *gpio &= ~(1 << 8);
  34.             iomask = 1;
  35.         }
  36.         else {
  37.             *gpio |= (1 << 8);
  38.             iomask = 0;
  39.         }
  40.     }
  41. }
  42.  
  43. void catch_signal() {}
  44.  
  45. int main(void)
  46. {
  47.     signal(SIGTERM, catch_signal);
  48.     signal(SIGINT, catch_signal);
  49.  
  50.     /* Avoids memory swapping for this program */
  51.     mlockall(MCL_CURRENT|MCL_FUTURE);
  52.  
  53.     //Obtain handle to physical memory
  54.     if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) {
  55.         printf("Unable to open /dev/mem: %s\n", strerror(errno));
  56.         return -1;
  57.     }
  58.  
  59.     gpio = (uint32_t *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x20ac000);
  60.  
  61.     if ((int32_t)gpio < 0){
  62.         printf("Mmap failed: %s\n", strerror(errno));
  63.         return -1;
  64.     }
  65.  
  66.     //set gpio8 as an output
  67.     //increment the pointer to 0x20ac004
  68.     *(gpio + 4) |= (1 << 8);
  69.  
  70.     /* Task Creation */
  71.     rt_task_create(&blink_task, "blinkLed", 0, 99, 0);
  72.    
  73.     rt_task_start(&blink_task, &blink, NULL);
  74.    
  75.     getchar();
  76.     rt_task_delete(&blink_task);
  77.    
  78.     //close(fd);
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment