Paste Search Dynamic
Recent pastes
GPIO registers into the virtua
  1. #include <stdio.h>
  2. #include <sys/mman.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7.  
  8. /* others definitions */
  9. #define BLOCK_SIZE               (4*1024)
  10.  
  11. /* PWM0 registers */
  12. #define PWM0_BASE_ADDRESS        0x7E20C000
  13.  
  14. #define PWM0_CTL                 (PWM0_BASE_ADDRESS + 0x00)
  15. #define PWM0_STA                 (PWM0_BASE_ADDRESS + 0x04)
  16. #define PWM0_DMAC                (PWM0_BASE_ADDRESS + 0x08)
  17. #define PWM0_RNG1                (PWM0_BASE_ADDRESS + 0x10)
  18. #define PWM0_DAT1                (PWM0_BASE_ADDRESS + 0x14)
  19. #define PWM0_FIF1                (PWM0_BASE_ADDRESS + 0x18)
  20. #define PWM0_RNG2                (PWM0_BASE_ADDRESS + 0x20)
  21. #define PWM0_DAT2                (PWM0_BASE_ADDRESS + 0x24)
  22.  
  23. /* gpio registers */
  24. #define GPIO_PULSE               12                               // broche physique 32
  25. #define ALT_FUNC0                0x04                             // b0100
  26.  
  27. #define GPIO_BASE_ADDRESS        0x7E200000
  28. #define GPFSEL1                  (GPIO_BASE_ADDRESS + 0x04)
  29. #define GPFSEL1_GPIO_PULSE_MASK  (0x000001C0)                     // b0000 0000 0000 0000 0000 0001 1100 0000
  30. #define GPSET0                   (GPIO_BASE_ADDRESS + 0x1C)
  31. #define GPCLR0                   (GPIO_BASE_ADDRESS + 0x28)
  32.  
  33. struct bcm2711_peripheral
  34. {
  35.     unsigned long addr_p;
  36.     int mem_fd;
  37.     void *map;
  38.     volatile unsigned int *addr;
  39. };
  40.  
  41. struct bcm2711_peripheral gpio = {GPIO_BASE_ADDRESS};
  42.  
  43. /* macros definitions */
  44. #define INP_GPIO(g)   *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3))
  45. #define OUT_GPIO(g)   *(gpio.addr + ((g)/10)) |=  (1<<(((g)%10)*3))
  46. #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3))
  47. #define GPIO_SET  *(gpio.addr + 7)
  48. #define GPIO_CLR  *(gpio.addr + 10)
  49. #define GPIO_READ(g)  *(gpio.addr + 13) &= (1<<(g))
  50.  
  51. // Exposes the physical address defined in the passed structure using mmap on /dev/mem
  52. int map_peripheral(struct bcm2711_peripheral *p)
  53. {
  54.    // Open /dev/mem
  55.    if ((p->mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0)
  56.    {
  57.       printf("Failed to open /dev/mem, try using sudo.n");
  58.       return -1;
  59.    }
  60.  
  61.    p->map = mmap(
  62.       null,
  63.       BLOCK_SIZE,
  64.       PROT_READ|PROT_WRITE,
  65.       MAP_SHARED,
  66.       p->mem_fd,      // File descriptor to physical memory virtual file '/dev/mem'
  67.       p->addr_p       // Address in physical map that we want this memory block to expose
  68.    );
  69.  
  70.    if (p->map == MAP_FAILED)
  71.    {
  72.         perror("mmap");
  73.         return -1;
  74.    }
  75.  
  76.    p->addr = (volatile unsigned int *) p->map;
  77.  
  78.    return 0;
  79. }
  80.  
  81. void unmap_peripheral(struct bcm2711_peripheral *p)
  82. {
  83.     munmap(p->map, BLOCK_SIZE);
  84.     close(p->mem_fd);
  85. }
  86.  
  87. int main()
  88. {
  89.    /* initialisations */
  90.    // check if gpio are usable
  91.    if(map_peripheral(&gpio) == -1)
  92.    {
  93.       printf("Failed to map the physical GPIO registers into the virtual memory space.n");
  94.       return -1;
  95.    }
  96.  
  97.    // define PULSE_PIN as output
  98.    INP_GPIO(GPIO_PULSE);
  99.    OUT_GPIO(GPIO_PULSE);
  100.  
  101.    while(1)
  102.    {
  103.     GPIO_SET = 1 << GPIO_PULSE;
  104.     sleep(1);
  105.  
  106.     GPIO_CLR = 1 << GPIO_PULSE;
  107.     sleep(1);
  108.    }
  109.  
  110.    return 0;
  111. }
Parsed in 0.014 seconds