Parcourir la source

opti; file save

master
Robin Thoni il y a 9 ans
Parent
révision
f1041af03c
2 fichiers modifiés avec 27 ajouts et 31 suppressions
  1. 2
    1
      gpio-raw.h
  2. 25
    30
      main.c

+ 2
- 1
gpio-raw.h Voir le fichier

@@ -32,7 +32,8 @@ extern volatile unsigned *gpio;
32 32
 #define GPIO_SET *(gpio+7)  // sets   bits which are 1 ignores bits which are 0
33 33
 #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
34 34
 #define GPIO_LEV *(gpio+13)
35
-#define GPIO_VALUE(PIN) ((GPIO_LEV & (1 << PIN)) != 0)
35
+#define GPIO_VALUE_R(ALL, PIN) ((ALL & (1 << PIN)) != 0)
36
+#define GPIO_VALUE(PIN) GPIO_VALUE_R(GPIO_LEV, PIN)
36 37
 
37 38
 bool setup_io();
38 39
 

+ 25
- 30
main.c Voir le fichier

@@ -6,53 +6,48 @@
6 6
 #include <stdio.h>
7 7
 #include <string.h>
8 8
 #include <stdlib.h>
9
-#include <pthread.h>
10 9
 #include <signal.h>
11 10
 
12 11
 #include "gpio-raw.h"
13 12
 
14
-bool thread_exit = false;
15 13
 
16
-static void signal_handler(int signo)
17
-{
18
-    thread_exit = true;
19
-}
14
+volatile unsigned n = 350000000 / sizeof(unsigned char);
20 15
 
21
-void* worker(void*)
16
+void signal_handler(int signo)
22 17
 {
23
-  FILE* fd = fopen("clock.out", "w");
24
-  while (!thread_exit)
25
-    sleep(1);
26
-  fclose(fd);
27
-  return 0;
18
+  std::cout << "signal catched" << std::endl;
19
+  n = 0;
28 20
 }
29 21
 
30 22
 int main(int argc, char* argv[])
31 23
 {
32
-  signal(SIGINT, signal_handler);
33 24
   setup_io();
34
-  INP_GPIO(4); // must use INP_GPIO before we can use OUT_GPIO
25
+  INP_GPIO(4);
35 26
 
36 27
   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
-
28
+  unsigned char* v = new unsigned char[n];
29
+  unsigned count = 0;
30
+  std::cout << "Ready to sniff. Press enter...";
31
+  getchar();
32
+  signal(SIGINT, signal_handler);
41 33
   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
-  }
34
+    for (; count < n; ++count)
35
+    {
36
+      const unsigned value = GPIO_LEV;
37
+      v[count] = GPIO_VALUE_R(value, 6) | (GPIO_VALUE_R(value, 26) << 1);
38
+    }
51 39
   gettimeofday(&t2, NULL);
52
-  pthread_join(thread, 0);
40
+  signal(SIGINT, SIG_DFL);
41
+  std::cout << "Saving file..." << std::endl;
42
+  int fd = open("gpio.out", O_CREAT | O_WRONLY);
43
+  write(fd, (void*)v, count * sizeof(*v));
44
+  close(fd);
53 45
   unsigned long long ut1 = (__suseconds_t)(1000000L * t1.tv_sec + t1.tv_usec);
54 46
   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;
47
+  unsigned long long diff = ut2 - ut1;
48
+  double unit = (double)diff / count;
49
+  double mhz = (1000000.0 / unit) / 1000000.0;
50
+  std::cout << "count: " << count << " diff: " << diff << " unit: "
51
+    << unit << " Mhz: " << mhz << std::endl;
57 52
   return 0;
58 53
 }

Chargement…
Annuler
Enregistrer