Peter Scholtens / Mbed 2 deprecated SimpleDecimationFilter

Dependencies:   mbed FastIO

Revision:
6:a5fc4e2ff34b
Parent:
5:1c0bfd69719f
Child:
7:5141bd76b08d
--- a/main.cpp	Fri Apr 17 02:19:41 2015 +0000
+++ b/main.cpp	Tue Apr 21 12:19:41 2015 +0000
@@ -1,5 +1,16 @@
 #include "mbed.h"
 
+/* version 0.0.9, P.C.S. Scholtens, Datang NXP, April 21th 2015, Nijmegen, Netherlands
+   - Run time counter overflow fill now continue looking for same bit, however
+     clipping the actual store value. This prevents underflow occurence of other symbol
+     and may create lock if no bitstream is present.
+   - Time out function added to prevent lock in case no bitstream is present.
+   - Timer object renamed for clarity from t to timer, see http://xkcd.org/1513/
+   - Includes updated build of library mbed.
+   - Out-of-range of run length moved outside core loop, to speed up bitstream sampling
+     and consequently improving accuracy.
+*/
+
 /* version 0.0.8, P.C.S. Scholtens, Datang NXP, April 17th 2015, Shanghai, PR China
    - Corrected assigned synchronized values, as the first appearance wasn't assigned.
 */
@@ -24,24 +35,25 @@
    Implement histogram to find run lengths of zroes and ones. */
 
 /* version 0.0.3, P.C.S. Scholtens, Datang NXP, April 14th, 2015, Shanghai, PR China
-   Initial version. No synchronixzation of the symbols is done. */
+   Initial version. No synchronization of the symbols is done. */
 
 /* See also:
 https://developer.mbed.org/forum/bugs-suggestions/topic/3464/
 */
 
 #define  DEPTH  128
+#define  WATCH_DOG_TIME  4
 
 /* Reserve memory space for the histogram */
 unsigned int zeros[DEPTH];
 unsigned int ones[DEPTH];
 unsigned int assign[DEPTH];
 
-DigitalIn bitstream(p11);
+DigitalIn  bitstream(p11);
 DigitalOut myled(LED1);
-Serial pc(USBTX, USBRX); // tx, rx
-
-Timer t;
+Serial     pc(USBTX, USBRX); // tx, rx
+Timer      timer;
+Timeout    timeout;
 
 /* A function to clear the contents of both histograms */
 void clear_histogram() {
@@ -68,32 +80,50 @@
 
 }
 
+/* Will only be called if measurement time exceeds preset watch dog timer. */
+void at_time_out() {
+    pc.printf("Input clipped to level %i, no bitstream present.\n", (int) bitstream);
+    timeout.attach(&at_time_out, WATCH_DOG_TIME);
+}
+
 /* Function which fill the histogram */
 void fill_histogram(unsigned int num_unsync_samples) {
     unsigned int count = 0;
     unsigned int run_length = 0;
     bool previous_bit = (bool) bitstream;
+    /* Switch on watch dog timer */
+    timeout.attach(&at_time_out, WATCH_DOG_TIME);
     /* Implements run-in: skip the first sequence as it is only a partial one. */
-    while((previous_bit == (bool) bitstream)  && (run_length < DEPTH-1)) {
-        run_length++;
+    while(previous_bit == (bool) bitstream) {
+        /* Do nothing, intentionally */;
     };
     previous_bit =  !previous_bit;
-    /* Start actual counting here */
     run_length   =  0;
+    /* Start actual counting here, store in variable run_length (will be clipped to DEPTH) */
     while(count < num_unsync_samples) {
-        while((previous_bit == (bool) bitstream)  && (run_length < DEPTH-1)) {
+        /* Core of the loop */
+        while(previous_bit == (bool) bitstream) {
             run_length++;
         };
+        /* Increment counter before clipping to preserve accuracy. */
+        count += run_length;
+        /* Test if run length exceeds depth of histogram, if so assign clip value. */
+        if (run_length < DEPTH-1) {
+            run_length = DEPTH-1;
+        }
+        /* Now write in histogram array of interest */
         if (previous_bit) {
             ones[run_length]++;
         }
         else {
             zeros[run_length]++;
         }
-        count        += run_length;
+        /* Reset for next run length counting loop */
         run_length   =  0;
         previous_bit =  !previous_bit;
     }
+    /* Switch off watch dog timer */
+    timeout.detach();
 }
 
 /* Here we count the number of unsynchronized symbols, mimicing previous implementation */
@@ -158,17 +188,18 @@
     unsigned int num_of_zeros, num_of_ones, value_of_unsync_zeros, value_of_unsync_ones, value_of_synced_zeros, value_of_synced_ones;
     float unsync_dutycycle, synced_dutycycle, unsync_voltage, synced_voltage;
     pc.baud(115200);
-    pc.printf("Bitstream counter, version 0.0.8, P.C.S. Scholtens, April 17th 2015, Shanghai, PR China.\n");
+    pc.printf("Bitstream counter, version 0.0.9, P.C.S. Scholtens, April 21th 2015, Nijmegen, Netherlands.\n");
+    pc.printf("Build " __DATE__ " " __TIME__ "\n");
     /*LPC_TIM2->PR = 0x0000002F;  / * decimal 47 */ 
     /*LPC_TIM3->PR = 24;*/
     clear_histogram();
     while(1) {
-        t.reset();
+        timer.reset();
         myled = 1;
         clear_histogram();
-        t.start();
+        timer.start();
         fill_histogram(1e7);
-        t.stop();
+        timer.stop();
         pc.printf("\n------   Captured Histogram   ------\n");
         print_histogram();
         num_of_zeros = get_num_unsync_symbols(0);
@@ -189,7 +220,7 @@
         pc.printf("Summed Values      %8i %8i\n",           value_of_synced_zeros, value_of_synced_ones);
         pc.printf("Duty Cyle %f, equals %f Volt\n",        synced_dutycycle     , synced_voltage);
         pc.printf("------------------------------------\n");
-        pc.printf("Measured in %f sec.\n",                  t.read());
+        pc.printf("Measured in %f sec.\n",                  timer.read());
         pc.printf("====================================\n");
         myled = 0;
         wait(0.1);