Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2105 - 1
#include "main.h"
2
#include "gpio.h"
3
 
4
// IO Acces
5
struct bcm2835_peripheral {
6
    unsigned long addr_p;
7
    int mem_fd;
8
    void *map;
9
    volatile unsigned int *addr;
10
};
11
struct bcm2835_peripheral gpio = {GPIO_BASE};
12
 
13
 
14
int map_peripheral(struct bcm2835_peripheral *p)
15
{
16
   // Open /dev/mem
17
   if ((p->mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
18
      printf("Failed to open /dev/mem, try checking permissions.\n");
19
      return -1;
20
   }
21
   p->map = mmap(
22
      NULL,
23
      BLOCK_SIZE,
24
      PROT_READ|PROT_WRITE,
25
      MAP_SHARED,
26
      p->mem_fd,      // File descriptor to physical memory virtual file '/dev/mem'
27
      p->addr_p       // address in physical map that we want this memory block to expose
28
   );
29
   if (p->map == MAP_FAILED) {
30
        perror("mmap");
31
        return -1;
32
   }
33
   p->addr = (volatile unsigned int *)p->map;
34
   return 0;
35
}
36
 
37
void unmap_peripheral(struct bcm2835_peripheral *p) {
38
    munmap(p->map, BLOCK_SIZE);
39
    close(p->mem_fd);
40
}
41
 
42
void buzzer_short(int seq){
43
        INP_GPIO(4);
44
        OUT_GPIO(4);
45
        for (int i = 0; i < seq; ++i)
46
        {
47
                if(seq > 1){usleep(50000);}
48
                GPIO_SET = 1 << 4;
49
                usleep(50000);
50
                GPIO_CLR = 1 << 4;
51
        }
52
}
53
 
54
void gpio_init(){
55
  if(map_peripheral(&gpio) == -1)
56
    {
57
        printf("Failed to map the physical GPIO registers into the virtual memory space.\n");
58
    }
59
}