Blob Blame Raw
From e04d3c4daab9688ee2ccfb222be4cbb57e02184f Mon Sep 17 00:00:00 2001
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Date: Thu, 17 Jan 2013 20:06:52 +0100
Subject: [PATCH 112/364] 	Rewrite spkmodem to use PIT for timing. Double
 the speed.

---
 ChangeLog                   |  4 ++++
 grub-core/commands/setpci.c |  2 +-
 grub-core/term/spkmodem.c   | 55 +++++++++++++++++++++++++++++++++++++--------
 util/spkmodem-recv.c        | 26 +++++++++------------
 4 files changed, 61 insertions(+), 26 deletions(-)

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