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
 
5
struct timeval startTime;   //Times needed for calculating Distance
6
struct timeval travelTime;
7
 
8
// IO Acces
9
struct bcm2835_peripheral {
10
    unsigned long addr_p;
11
    int mem_fd;
12
    void *map;
13
    volatile unsigned int *addr;
14
};
15
struct bcm2835_peripheral gpio = {GPIO_BASE};
16
 
17
 
18
//>> Create MMAP for GPIO 
19
//------------------------------------------------------------------------------------------------------
20
int map_peripheral(struct bcm2835_peripheral *p)
21
{
22
   // Open /dev/mem
23
   if ((p->mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
24
      printf("Failed to open /dev/mem, try checking permissions.\n");
25
      return -1;
26
   }
27
   p->map = mmap(
28
      NULL,
29
      BLOCK_SIZE,
30
      PROT_READ|PROT_WRITE,
31
      MAP_SHARED,
32
      p->mem_fd,      
33
      p->addr_p      
34
   );
35
   if (p->map == MAP_FAILED) {
36
        perror("mmap");
37
        return -1;
38
   }
39
   p->addr = (volatile unsigned int *)p->map;
40
   return 0;
41
}
42
 
43
void unmap_peripheral(struct bcm2835_peripheral *p) {
44
    munmap(p->map, BLOCK_SIZE);
45
    close(p->mem_fd);
46
}
47
 
48
//>> Funktion for short buzzing Sound
49
//------------------------------------------------------------------------------------------------------
50
void buzzer_short(int seq){
51
        INP_GPIO(4);
52
        OUT_GPIO(4);
53
        for (int i = 0; i < seq; ++i)
54
        {
55
                if(seq > 1){usleep(50000);}
56
                GPIO_SET = 1 << 4;
57
                usleep(50000);
58
                GPIO_CLR = 1 << 4;
59
        }
60
}
61
 
62
//>> Calculate Distance with external sonar
63
//------------------------------------------------------------------------------------------------------
64
int calculate_distance(){
65
  int distanceTemp;
66
  int loopbreaker = 10000;        //Breaks the first loop in case it gets stuck (which happens from time to time if the Objekt facing the sensor is too close)
67
  GPIO_SET = 1 << ULTRA_TRIGGER;
68
  usleep(10);
69
  GPIO_CLR = 1 << ULTRA_TRIGGER;    
70
 
71
  while(!(GPIO_READ(ULTRA_ECHO)))
72
  {
73
    loopbreaker--;
74
    if(!loopbreaker){break;}
75
  }
76
  gettimeofday(&startTime, NULL);
77
 
78
  while((GPIO_READ(ULTRA_ECHO)))
79
  gettimeofday(&travelTime, NULL);
80
 
81
  loopbreaker = 0;
82
  distanceTemp = (travelTime.tv_usec - startTime.tv_usec) / 58;
83
  return distanceTemp;
84
}
85
 
86
//>> Initialize GPIO
87
//------------------------------------------------------------------------------------------------------
88
void gpio_init(){
89
  if(map_peripheral(&gpio) == -1)
90
    {
91
        printf("Failed to map the physical GPIO registers into the virtual memory space.\n");
92
    }
93
 
94
  INP_GPIO(ULTRA_ECHO);
95
  OUT_GPIO(ULTRA_TRIGGER);
96
  GPIO_CLR = 1 << ULTRA_TRIGGER;
97
  sleep(1);
98
}