Frequency counter library using GPS 1PPS signal and temperature controlled 50MHz Base clock. Ported from F411 Frequency Counter.

Dependencies:   RingBuff

Dependents:   Frequency_Cntr_1PPS_F746ZG

Fork of Frq_cuntr_full by Kenji Arai

Please refer following.
/users/kenjiArai/notebook/frequency-counters/

Revision:
2:194f82ad3041
Parent:
1:102230f2879d
Child:
3:339307e1dc0d
--- a/frq_cuntr_full.h	Sat Nov 22 23:02:39 2014 +0000
+++ b/frq_cuntr_full.h	Sun Dec 21 12:14:46 2014 +0000
@@ -8,7 +8,7 @@
  *  http://mbed.org/users/kenjiArai/
  *      Additional functions and modification
  *      started: October   18th, 2014
- *      Revised: Nobember  23rd, 2014
+ *      Revised: December  21st, 2014
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -22,34 +22,20 @@
 
 #include "mbed.h"
 
-#define DEBUG           // use Communication with PC(UART)
+//#define DEBUG           // use Communication with PC(UART)
+
 /*
     CAUTION:
     If you select internal clock (100MHz), you need consider PLL clock behavior.
-    PLL clock drifts over 60Hz (increase frequency) within 30 to 40 second
+    PLL clock drifts over 70Hz (increase frequency) within 30 to 40 second
     then suddenly back to low frequency and increase again.
     Even you prepare precision external clock, this frequency drift cannot avoid.
+    Comment out "BASE_EXTERNAL_CLOCK" is only for checking!!
  */
 #define BASE_EXTERNAL_CLOCK
 
 namespace Frequency_counter
 {
-#define CNT_BASE        29999978        // 30MHz
-#define CNT_FIX_BASE    600000          // 0.03sec
-
-#if defined(BASE_EXTERNAL_CLOCK)
-#define CNT_UPPER       (CNT_BASE + 20)
-#define CNT_LOWER       (CNT_BASE - 20)
-#else
-#define CNT_UPPER       (0xffffffff - 100)
-#define CNT_LOWER       (0          + 100)
-#endif
-
-#if defined(BASE_EXTERNAL_CLOCK)
-#define ONE_SECOND_COUNT    (CNT_BASE - CNT_FIX_BASE)
-#else
-#define ONE_SECOND_COUNT    0x2faf080   // 50MHz
-#endif
 
 #define CNT_BF_SIZE     120             // 1PPS data save size
 
@@ -59,11 +45,16 @@
  * @code
  * #include "mbed.h"
  *
- * // PC_6,PC_7 & PB6 use for Timer3 & 4(16+16bit)
- * // PA_0,PA_1 & PB10 use for Timer2(32bit)
- * FRQ_CUNTR fc(PC_6, 1.0);    // Input port(Fixed) & gate time[sec]
+ * using namespace Frequency_counter;
+ *
+ * // PC_6,PC_7 & PB_6 use for Timer3 & 4(16+16bit)
+ * // PA_0,PA_1 & PB_10 use for Timer2(32bit)
+ * // PA_8 & PC_7 use for MCO (Test purpose)
+ * FRQ_CUNTR   fc(PC_6, 1.0, 50.000000);// Input port, gate time[sec] and External clock freq.
  *
  * int main() {
+ *   uint32_t counter_1pps = 0;
+ *   double new_frequency = 0;
  *   // This is for test purpose
  *   fc.port_mco1_mco2_set(2);      // Clk/2 ->1/1(100MHz) cannot measure!!
  *   fc.read_frequency_TIM2(1.0);   // read TIM2 source frequency (test)
@@ -73,7 +64,7 @@
  *      counter_1pps = fc.read_avarage_1pps();
  *      while (fc.status_freq_update() == 0) {;}
  *      new_frequency = fc.read_freq_data();
- *      print("1PPS/ave = %9d , FREQUENCY = %9d\r\n", counter_1pps, new_frequency);
+ *       PRINTF("1PPS/ave = %9d , FREQUENCY = %11.3f\r\n", counter_1pps, new_frequency);
  *   }
  * }
  * @endcode
@@ -82,17 +73,24 @@
 class FRQ_CUNTR
 {
 public:
+
     /** Configure data pin (Not changeable)
-      * @param Freq. input pin
+      * @param Freq. input pin + Gate time[sec] + External clock[MHz]
       */
-    FRQ_CUNTR(PinName f_in, double gt);
+    FRQ_CUNTR(PinName f_in, double gt, double ex_clock);
 
     /** Set gate time
       * @param gate time [sec]
-      * @return !=0: new data is avairable, 0: not yet
+      * @return gate time (range 50mS to 1 minute)
       */
     double set_gate_time(double gt);
 
+    /** Read gate time
+      * @param none
+      * @return gate time (range 50mS to 1 minute)
+      */
+    double read_gate_time(void);
+
     /** Read status (new frequency data is available or not)
       * @param none
       * @return !=0: new data is avairable, 0: not yet
@@ -103,7 +101,7 @@
       * @param none
       * @return frequency data
       */
-    uint32_t read_freq_data(void);
+    double read_freq_data(void);
 
     /** Read avarage measued data GPS 1PPS
       * @param none
@@ -117,35 +115,49 @@
       */
     uint32_t status_1pps(void);
 
-    /** This is a test purpose function
+    /** Read GPS status
+      * @param none
+      * @return 1: GPS is ready
+      */
+    uint8_t gps_status(void);
+
+    /** This is a "TEST PURPOSE" function
       * Check PA0 pin input or internal clock frequency
       * @param none
       * @return Frequency
       */
     uint32_t read_frequency_TIM2(float gate_time);
 
-    /** This is a test purpose function
+    /** This is a "TEST PURPOSE" function
       * Check PC6 pin input frequency
       * @param none
       * @return Frequency
       */
     uint32_t read_frequency_TIM3P4(float gate_time);
 
-    /** This is a test purpose function
+    /** This is a "TEST PURPOSE" function
       * Output clock from pin PA8 & PC9 uses for MCO_1 & MCO_2
       * @param none
       * @return none
       */
     void port_mco1_mco2_set(uint8_t select);
 
+    /** This is a "DEBUG PURPOSE" function
+      * print internal data (need to define "DEBUG"
+      * @param none
+      * @return none
+      */
+    void debug_printf_internal_data(void);
+
 protected:
     DigitalIn _pin;
 
     void initialize_Freq_counter(void);     // Initialize timers
-    // Internal clock (50MHz) and IC2 for GPS 1pps signal measurement
+    // Internal clock (100MHz) or External clock(?MHz) and IC2 for GPS 1pps signal measurement
     void initialize_TIM2(void);
     // Initialize TIM3 and TIM4 as 32bit counter (TIM3(16bit) + TIM4(16bit))
     void initialize_TIM3P4(void);
+    void set_external_clock(double ex_clock); // Set external clock data
     uint32_t set_1PPS_data(void);           // Set GPS 1PPS counter value
     uint32_t read_ic2_counter_TIM2(void);   // Read TIM2 captured counter value
     uint32_t check_ic2_status_TIM2(void);   // Check TIM2 IC2 status
@@ -154,8 +166,15 @@
     uint8_t  read_oc_port_status(void);     // Check TIM2 OC port
 
 private:
+    double   newest_frequency;
+    double   gate_time;
+    double   ex_clock_freq;
+    uint32_t ex_clk_base;
+    uint32_t clk_hi_const;
+    uint32_t clk_upper_limit;
+    uint32_t clk_lower_limit;
+    uint8_t  gps_ready;     
     // TIM2
-    double   gate_time;
     uint32_t counter_tim2;
     uint32_t old_cntr_tim2;
     // TIM3+4
@@ -168,6 +187,7 @@
     uint64_t onepps_cnt_avarage;
     uint8_t  onepps_buf_full;
     uint8_t  onepps_ready_flg;
+
 };
 
 /*