freqmeter.c

00001 /* Simple Frequency meter.
00002    Copyright (C) 2001, 2002 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@nerim.fr)       
00004 
00005 This file is free software; you can redistribute it and/or modify it
00006 under the terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option) any
00008 later version.
00009 
00010 In addition to the permissions in the GNU General Public License, the
00011 Free Software Foundation gives you unlimited permission to link the
00012 compiled version of this file with other programs, and to distribute
00013 those programs without any restriction coming from the use of this
00014 file.  (The General Public License restrictions do apply in other
00015 respects; for example, they cover modification of the file, and
00016 distribution when not linked into another program.)
00017 
00018 This file is distributed in the hope that it will be useful, but
00019 WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; see the file COPYING.  If not, write to
00025 the Free Software Foundation, 59 Temple Place - Suite 330,
00026 Boston, MA 02111-1307, USA.  */
00027 
00050 #include <stdio.h>
00051 #include <sys/param.h>
00052 #include <sys/ports.h>
00053 #include <sys/interrupts.h>
00054 #include <sys/sio.h>
00055 #include <sys/locks.h>
00056 
00057 #define TIMER_DIV  (8192L)
00058 #define TIMER_TICK (M6811_CPU_E_CLOCK / TIMER_DIV)
00059 
00060 /* Setup for a 8Mhz quartz, and prescaler set to 1
00061    (500ns period).  */
00062 #define USEC_PER_TICK (1)
00063 #define USEC_DIVISOR  (2)
00064 
00065 unsigned short prev_time;
00066 unsigned long dt_sum;
00067 unsigned short dt_count;
00068 
00069 unsigned long tick_sum;
00070 unsigned short m_count;
00071 
00072 volatile unsigned char new_measure = 0;
00073 
00074 void input_capture_interrupt (void) __attribute__((interrupt));
00075 void timer_overflow (void) __attribute__((interrupt));
00076 
00077 void __attribute__((interrupt))
00078 input_capture_interrupt (void)
00079 {
00080   unsigned short t;
00081   unsigned short dt;
00082   
00083   t = get_input_capture_1 ();
00084   if (t > prev_time)
00085     {
00086       dt = t - prev_time;
00087       dt_sum += dt;
00088       dt_count++;
00089     }
00090   prev_time = t;
00091   _io_ports[M6811_TFLG1] |= M6811_IC1F;
00092 }
00093 
00094 void __attribute__((interrupt))
00095 timer_overflow (void)
00096 {
00097   static unsigned char nr_overflow = 100;
00098 
00099   nr_overflow--;
00100   if (nr_overflow == 0)
00101     {
00102       tick_sum = dt_sum;
00103       m_count = dt_count;
00104       dt_sum = 0;
00105       dt_count = 0;
00106       prev_time = 0xffff;
00107       new_measure++;
00108       nr_overflow = 100;
00109     }
00110   _io_ports[M6811_TFLG2] |= M6811_TOF;
00111 }
00112 
00113 static void
00114 report_frequency (unsigned long dt, unsigned short cnt)
00115 {
00116   if (cnt)
00117     dt = dt / cnt;
00118   dt = (dt * USEC_PER_TICK) / USEC_DIVISOR;
00119   dt = 1000000000L / dt;
00120   printf ("\rP: %ld HZ   ", dt / 1000);
00121 }
00122 
00123 int
00124 main ()
00125 {
00126   lock ();
00127   serial_init ();
00128 
00129   set_interrupt_handler (TIMER_INPUT1_VECTOR, input_capture_interrupt);
00130   set_interrupt_handler (TIMER_OVERFLOW_VECTOR, timer_overflow);
00131 
00132   _io_ports[M6811_TMSK2] = M6811_TOI;
00133   _io_ports[M6811_TCTL2] = M6811_EDG1B | M6811_EDG2B | M6811_EDG3B;
00134   _io_ports[M6811_TMSK1] = M6811_IC1I;
00135   new_measure = 0;
00136   prev_time = 0xffff;
00137   unlock ();
00138   
00139   while (1)
00140     {
00141       while (new_measure == 0)
00142         continue;
00143 
00144       new_measure = 0;
00145       report_frequency (tick_sum, m_count);
00146     }
00147 }