Robin Thoni 8 anos atrás
commit
0205be5d97
3 arquivos alterados com 153 adições e 0 exclusões
  1. 56
    0
      gpio-raw.c
  2. 39
    0
      gpio-raw.h
  3. 58
    0
      main.c

+ 56
- 0
gpio-raw.c Ver arquivo

@@ -0,0 +1,56 @@
1
+#include "gpio-raw.h"
2
+
3
+int  mem_fd;
4
+char *gpio_mem, *gpio_map;
5
+char *spi0_mem, *spi0_map;
6
+
7
+
8
+// I/O access
9
+volatile unsigned *gpio;
10
+
11
+//
12
+// Set up a memory regions to access GPIO
13
+//
14
+bool setup_io()
15
+{
16
+   /* open /dev/mem */
17
+   if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
18
+      /*printf("can't open /dev/mem \n");
19
+      exit (-1);*/
20
+     return false;
21
+   }
22
+
23
+   /* mmap GPIO */
24
+
25
+   // Allocate MAP block
26
+   if ((gpio_mem = (char*)malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
27
+      /*printf("allocation error \n");
28
+      exit (-1);*/
29
+     return false;
30
+   }
31
+
32
+   // Make sure pointer is on 4K boundary
33
+   if ((unsigned long)gpio_mem % PAGE_SIZE)
34
+     gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);
35
+
36
+   // Now map it
37
+   gpio_map = (char *)mmap(
38
+      (caddr_t)gpio_mem,
39
+      BLOCK_SIZE,
40
+      PROT_READ|PROT_WRITE,
41
+      MAP_SHARED|MAP_FIXED,
42
+      mem_fd,
43
+      GPIO_BASE
44
+   );
45
+
46
+   if ((long)gpio_map < 0) {
47
+      /*printf("mmap error %d\n", (int)gpio_map);
48
+      exit (-1);*/
49
+     return false;
50
+   }
51
+
52
+   // Always use volatile pointer!
53
+   gpio = (volatile unsigned *)gpio_map;
54
+  return true;
55
+
56
+} // setup_io

+ 39
- 0
gpio-raw.h Ver arquivo

@@ -0,0 +1,39 @@
1
+#ifndef GPIO_RAW_H
2
+#define GPIO_RAW_H
3
+#define BCM2708_PERI_BASE        0x20000000
4
+#define GPIO_BASE                (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
5
+
6
+#include <dirent.h>
7
+#include <fcntl.h>
8
+#include <assert.h>
9
+#include <sys/mman.h>
10
+#include <sys/types.h>
11
+#include <sys/stat.h>
12
+#include <stdlib.h>
13
+
14
+#include <unistd.h>
15
+
16
+#define PAGE_SIZE (4*1024)
17
+#define BLOCK_SIZE (4*1024)
18
+
19
+extern int  mem_fd;
20
+extern char *gpio_mem, *gpio_map;
21
+extern char *spi0_mem, *spi0_map;
22
+
23
+
24
+// I/O access
25
+extern volatile unsigned *gpio;
26
+
27
+// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
28
+#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
29
+#define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3))
30
+#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
31
+
32
+#define GPIO_SET *(gpio+7)  // sets   bits which are 1 ignores bits which are 0
33
+#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
34
+#define GPIO_LEV *(gpio+13)
35
+#define GPIO_VALUE(PIN) ((GPIO_LEV & (1 << PIN)) != 0)
36
+
37
+bool setup_io();
38
+
39
+#endif // GPIO_RAW_H

+ 58
- 0
main.c Ver arquivo

@@ -0,0 +1,58 @@
1
+#define _POSIX_C_SOURCE 199309L
2
+#include <sys/time.h>
3
+#include <time.h>
4
+#include <iostream>
5
+#include <stdlib.h>
6
+#include <stdio.h>
7
+#include <string.h>
8
+#include <stdlib.h>
9
+#include <pthread.h>
10
+#include <signal.h>
11
+
12
+#include "gpio-raw.h"
13
+
14
+bool thread_exit = false;
15
+
16
+static void signal_handler(int signo)
17
+{
18
+    thread_exit = true;
19
+}
20
+
21
+void* worker(void*)
22
+{
23
+  FILE* fd = fopen("clock.out", "w");
24
+  while (!thread_exit)
25
+    sleep(1);
26
+  fclose(fd);
27
+  return 0;
28
+}
29
+
30
+int main(int argc, char* argv[])
31
+{
32
+  signal(SIGINT, signal_handler);
33
+  setup_io();
34
+  INP_GPIO(4); // must use INP_GPIO before we can use OUT_GPIO
35
+
36
+  struct timeval t1, t2;
37
+  const int n = argc == 2 ? atoi(argv[1]) : 1000000;
38
+  pthread_t thread;
39
+  pthread_create(&thread, 0, worker, 0);
40
+
41
+  gettimeofday(&t1, NULL);
42
+  for (int i = 0; !thread_exit; ++i)
43
+  {
44
+    //GPIO_VALUE(6);
45
+    //tab[i] = GPIO_VALUE(6);
46
+    printf("%i\n", GPIO_VALUE(6));
47
+    //fprintf(fd, "%i\n", v);
48
+    //GPIO_SET = 1<<4;
49
+    //GPIO_CLR = 1<<4;
50
+  }
51
+  gettimeofday(&t2, NULL);
52
+  pthread_join(thread, 0);
53
+  unsigned long long ut1 = (__suseconds_t)(1000000L * t1.tv_sec + t1.tv_usec);
54
+  unsigned long long ut2 = (__suseconds_t)(1000000L * t2.tv_sec + t2.tv_usec);
55
+  std::cout << ut1 << " " << ut2 << " ";
56
+  std::cout << (ut2 - ut1) << " " << (float)(ut2 - ut1) / n << std::endl;
57
+  return 0;
58
+}

Carregando…
Cancelar
Salvar