a5bd9f6
From e04d3c4daab9688ee2ccfb222be4cbb57e02184f Mon Sep 17 00:00:00 2001
a5bd9f6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f6
Date: Thu, 17 Jan 2013 20:06:52 +0100
a5bd9f6
Subject: [PATCH 112/364] 	Rewrite spkmodem to use PIT for timing. Double
a5bd9f6
 the speed.
a5bd9f6
a5bd9f6
---
a5bd9f6
 ChangeLog                   |  4 ++++
a5bd9f6
 grub-core/commands/setpci.c |  2 +-
a5bd9f6
 grub-core/term/spkmodem.c   | 55 +++++++++++++++++++++++++++++++++++++--------
a5bd9f6
 util/spkmodem-recv.c        | 26 +++++++++------------
a5bd9f6
 4 files changed, 61 insertions(+), 26 deletions(-)
a5bd9f6
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index 9922c06..f8129ae 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,3 +1,7 @@
a5bd9f6
+2013-01-17  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
+	Rewrite spkmodem to use PIT for timing. Double the speed.
a5bd9f6
+
a5bd9f6
 2013-01-16  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
 
a5bd9f6
 	Add new command pcidump.
a5bd9f6
diff --git a/grub-core/commands/setpci.c b/grub-core/commands/setpci.c
a5bd9f6
index 4eaba7c..d5bc97d 100644
a5bd9f6
--- a/grub-core/commands/setpci.c
a5bd9f6
+++ b/grub-core/commands/setpci.c
a5bd9f6
@@ -129,7 +129,7 @@ grub_setpci_iter (grub_pci_device_t dev, grub_pci_id_t pciid,
a5bd9f6
 
a5bd9f6
   if (!write_mask)
a5bd9f6
     {
a5bd9f6
-      grub_printf (_("Register %x of %d:%d.%d is %x\n"), regaddr,
a5bd9f6
+      grub_printf (_("Register %x of %x:%02x.%x is %x\n"), regaddr,
a5bd9f6
 		   grub_pci_get_bus (dev),
a5bd9f6
 		   grub_pci_get_device (dev),
a5bd9f6
 		   grub_pci_get_function (dev),
a5bd9f6
diff --git a/grub-core/term/spkmodem.c b/grub-core/term/spkmodem.c
a5bd9f6
index 31dab65..b6e7a04 100644
a5bd9f6
--- a/grub-core/term/spkmodem.c
a5bd9f6
+++ b/grub-core/term/spkmodem.c
a5bd9f6
@@ -31,29 +31,65 @@ GRUB_MOD_LICENSE ("GPLv3+");
a5bd9f6
 extern struct grub_terminfo_output_state grub_spkmodem_terminfo_output;
a5bd9f6
 
a5bd9f6
 static void
a5bd9f6
+make_tone (grub_uint16_t freq_count, unsigned int duration)
a5bd9f6
+{
a5bd9f6
+  /* Program timer 2.  */
a5bd9f6
+  grub_outb (GRUB_PIT_CTRL_SELECT_2
a5bd9f6
+	     | GRUB_PIT_CTRL_READLOAD_WORD
a5bd9f6
+	     | GRUB_PIT_CTRL_SQUAREWAVE_GEN
a5bd9f6
+	     | GRUB_PIT_CTRL_COUNT_BINARY, GRUB_PIT_CTRL);
a5bd9f6
+  grub_outb (freq_count & 0xff, GRUB_PIT_COUNTER_2);		/* LSB */
a5bd9f6
+  grub_outb ((freq_count >> 8) & 0xff, GRUB_PIT_COUNTER_2);	/* MSB */
a5bd9f6
+
a5bd9f6
+  /* Start speaker.  */
a5bd9f6
+  grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
a5bd9f6
+	     | GRUB_PIT_SPK_TMR2 | GRUB_PIT_SPK_DATA,
a5bd9f6
+	     GRUB_PIT_SPEAKER_PORT);
a5bd9f6
+
a5bd9f6
+  for (; duration; duration--)
a5bd9f6
+    {
a5bd9f6
+      unsigned short counter, previous_counter = 0xffff;
a5bd9f6
+      while (1)
a5bd9f6
+	{
a5bd9f6
+	  counter = grub_inb (GRUB_PIT_COUNTER_2);
a5bd9f6
+	  counter |= ((grub_uint16_t) grub_inb (GRUB_PIT_COUNTER_2)) << 8;
a5bd9f6
+	  if (counter > previous_counter)
a5bd9f6
+	    {
a5bd9f6
+	      previous_counter = counter;
a5bd9f6
+	      break;
a5bd9f6
+	    }
a5bd9f6
+	  previous_counter = counter;
a5bd9f6
+	}
a5bd9f6
+    }
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static int inited;
a5bd9f6
+
a5bd9f6
+static void
a5bd9f6
 put (struct grub_term_output *term __attribute__ ((unused)), const int c)
a5bd9f6
 {
a5bd9f6
   int i;
a5bd9f6
 
a5bd9f6
+  if (!inited)
a5bd9f6
+    {
a5bd9f6
+      make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 50, 20);
a5bd9f6
+      inited = 1;
a5bd9f6
+    }
a5bd9f6
+
a5bd9f6
   for (i = 7; i >= 0; i--)
a5bd9f6
     {
a5bd9f6
       if ((c >> i) & 1)
a5bd9f6
-	grub_speaker_beep_on (2000);
a5bd9f6
+	make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 2000, 20);
a5bd9f6
       else
a5bd9f6
-	grub_speaker_beep_on (4000);
a5bd9f6
-      grub_millisleep (10);
a5bd9f6
-      grub_speaker_beep_on (1000);
a5bd9f6
-      grub_millisleep (10);
a5bd9f6
+	make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 4000, 40);
a5bd9f6
+      make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 1000, 10);
a5bd9f6
     }
a5bd9f6
-  grub_speaker_beep_on (50);
a5bd9f6
+  make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 50, 0);
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
 static grub_err_t
a5bd9f6
 grub_spkmodem_init_output (struct grub_term_output *term)
a5bd9f6
 {
a5bd9f6
-  grub_speaker_beep_on (50);
a5bd9f6
-  grub_millisleep (50);
a5bd9f6
-
a5bd9f6
   grub_terminfo_output_init (term);
a5bd9f6
 
a5bd9f6
   return 0;
a5bd9f6
@@ -63,6 +99,7 @@ static grub_err_t
a5bd9f6
 grub_spkmodem_fini_output (struct grub_term_output *term __attribute__ ((unused)))
a5bd9f6
 {
a5bd9f6
   grub_speaker_beep_off ();
a5bd9f6
+  inited = 0;
a5bd9f6
   return 0;
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
diff --git a/util/spkmodem-recv.c b/util/spkmodem-recv.c
a5bd9f6
index cbec3af..4cc88b8 100644
a5bd9f6
--- a/util/spkmodem-recv.c
a5bd9f6
+++ b/util/spkmodem-recv.c
a5bd9f6
@@ -5,19 +5,13 @@
a5bd9f6
 /* Compilation:  gcc -o spkmodem-recv spkmodem-recv  */
a5bd9f6
 /* Usage: parecord --channels=1 --rate=48000 --format=s16le | ./spkmodem-recv */
a5bd9f6
 
a5bd9f6
-#define RATE 48000
a5bd9f6
-#define SAMPLES_PER_TRAME 480
a5bd9f6
-#define AMPLITUDE_THRESHOLD 100000
a5bd9f6
-#define FREQ_SEP_MIN 15
a5bd9f6
-#define FREQ_SEP_NOM 20
a5bd9f6
-#define FREQ_SEP_MAX 25
a5bd9f6
-
a5bd9f6
-#define FREQ_DATA_MIN 10
a5bd9f6
-#define FREQ_DATA_THRESHOLD 60
a5bd9f6
-#define FREQ_DATA_MAX 120
a5bd9f6
-#define AMPLITUDE_SAMPLES 2 * SAMPLES_PER_TRAME
a5bd9f6
-
a5bd9f6
-#define THRESHOLD 1000
a5bd9f6
+#define SAMPLES_PER_TRAME 240
a5bd9f6
+#define FREQ_SEP_MIN 6
a5bd9f6
+#define FREQ_SEP_MAX 15
a5bd9f6
+#define FREQ_DATA_MIN 15
a5bd9f6
+#define FREQ_DATA_THRESHOLD 25
a5bd9f6
+#define FREQ_DATA_MAX 60
a5bd9f6
+#define THRESHOLD 500
a5bd9f6
 
a5bd9f6
 #define DEBUG 0
a5bd9f6
 
a5bd9f6
@@ -67,14 +61,14 @@ main ()
a5bd9f6
 	  c = 0;
a5bd9f6
 	  lp = 0;
a5bd9f6
 	}
a5bd9f6
-      if (f2 > 12 && f2 < 25
a5bd9f6
-	  && f1 > 5 && f1 < 120)
a5bd9f6
+      if (f2 > FREQ_SEP_MIN && f2 < FREQ_SEP_MAX
a5bd9f6
+	  && f1 > FREQ_DATA_MIN && f1 < FREQ_DATA_MAX)
a5bd9f6
 	{
a5bd9f6
 #if DEBUG
a5bd9f6
 	  printf ("%d %d %d @%d\n", f1, f2, FREQ_DATA_THRESHOLD,
a5bd9f6
 		  ftell (stdin) - sizeof (trame));
a5bd9f6
 #endif
a5bd9f6
-	  if (f1 < 60)
a5bd9f6
+	  if (f1 < FREQ_DATA_THRESHOLD)
a5bd9f6
 	    c |= (1 << bitn);
a5bd9f6
 	  bitn--;
a5bd9f6
 	  if (bitn < 0)
a5bd9f6
-- 
a5bd9f6
1.8.1.4
a5bd9f6