]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cmd_cpu.c
z180-serv: measure cpu frequency
[z180-stamp.git] / avr / cmd_cpu.c
index 2abf5db150d602c0d6498f0d486f40dfdb77fc2a..af9ded44a593d1f9efa0e48ba7b12b4ac05ac22c 100644 (file)
 #define debug_cpu(fmt, args...)                                                              \
        debug_cond(DEBUG_CPU, fmt, ##args)
 
-static
-char * ulltoa (uint64_t val, char *s)
-{
-       char *p = s;
-
-       while (val >= 10) {
-               *p++ = (val % 10) + '0';
-               val = val / 10;
-       }
-       *p++ = val + '0';
-       *p = '\0';
-
-       return strrev(s);
-}
-
 /*
  * delay for <count> ms...
  */
@@ -50,88 +35,6 @@ static void  test_delay(uint32_t count)
        while (get_timer(ts) <= count);
 }
 
-static uint32_t z80_measure_phi(uint_fast8_t cycles)
-{
-       uint16_t ref_stop;
-       uint16_t ref_ovfl;
-       uint8_t x_ovfl;
-       uint32_t x_freq;
-
-
-       PRR1 &= ~_BV(PRTIM3);
-       TCCR3A = 0;
-       TCCR3B = 0b000<<CS30;                   /* stop counter */
-       TCNT3 = 0;
-       x_ovfl = 0;
-       TIFR3 = _BV(TOV3);
-       ref_ovfl = 0;
-
-       ATOMIC_BLOCK(ATOMIC_FORCEON) {
-               EIFR = _BV(INTF6);                                      /* Reset pending int */
-               while ((EIFR & _BV(INTF6)) == 0)        /* Wait for falling edge */
-                       ;
-               OCR4B = TCNT4;
-               TCCR3B = 0b110<<CS30;           /* Count falling edges on T3 (==INT6) */
-               TIFR4 = _BV(OCF4B);                     /* clear compare match flag */
-
-               while (ref_ovfl < 60) {
-                       if ((TIFR4 & _BV(OCF4B)) != 0) {
-                               TIFR4 = _BV(OCF4B);
-                               ++ref_ovfl;
-                       }
-                       if ((TIFR3 & _BV(TOV3)) != 0) {
-                               TIFR3 = _BV(TOV3);
-                               ++x_ovfl;
-                       }
-               }
-
-               EIFR = _BV(INTF6);
-               for (;;) {
-                       if (EIFR & _BV(INTF6)) {
-                               TCCR3B = 0b000<<CS30;   /* stop counter */
-                               ref_stop = TCNT4;
-                               break;
-                       }
-                       if ((TIFR4 & _BV(OCF4B)) != 0) {
-                               TIFR4 = _BV(OCF4B);
-                               ++ref_ovfl;
-                       }
-               }
-       }
-
-       if ((TIFR3 & _BV(TOV3)) != 0) {
-               TIFR3 = _BV(TOV3);
-               x_ovfl++;
-       }
-
-       uint32_t ref_cnt = (ref_stop - OCR4B) + ((uint32_t)ref_ovfl << 16);
-       uint32_t x_cnt = TCNT3 + ((uint32_t) x_ovfl << 16);
-       uint64_t x_tmp = (uint64_t) 100000 * (x_cnt * cycles);
-
-       /* Stop Timer */
-       TCCR3B = 0;
-       PRR1 |= _BV(PRTIM3);
-
-//     char x_tmp_str[21];
-//
-//     debug_cpu("TCNT3: %6u, ref_cnt: %9lu\n", TCNT3, ref_cnt);
-//     ulltoa(x_tmp, x_tmp_str);
-//     debug_cpu("x_tmp: %s\n", x_tmp_str);
-
-       x_tmp = (x_tmp * getenv_ulong(PSTR(ENV_FMON), 10, F_CPU) + (ref_cnt / 2)) / ref_cnt;
-
-//     ulltoa(x_tmp, x_tmp_str);
-//     debug_cpu("x_tmp: %s\n", x_tmp_str);
-
-       /* round to 5 decimal digits */
-       int_fast8_t sc = 5;
-       for ( ; sc > 0 || x_tmp >= 100000; sc--) x_tmp = (x_tmp + 5)/10;
-       x_freq = x_tmp;
-       for ( ; sc < 0; sc++) x_freq *= 10;
-
-       return x_freq;
-}
-
 static const FLASH char * const FLASH cpu_strings[] = {
        FSTR("Unknown"),
        FSTR("8080"),
@@ -425,7 +328,7 @@ CMD_TBL_ITEM(
 //     "[-swnu] [-c loopcycles] [-t timeout]\n"
        "[-swnu] [-c loopcycles]\n"
        "     -s Be silent\n"
-       "     -w Write result to environment variable '"ENV_CPU_FREQ"'"
+       "     -w Write result to environment variable '"ENV_CPU_FREQ"'\n"
        "     -n Don't load code snippet. \n"
        "     -u Don't unload. Leave code snippet in ram.\n"
        "     -c Overwrite cycles per lopp for in \"l: a,(50h)/jp l\" loop."
@@ -437,7 +340,7 @@ CMD_TBL_ITEM(
 //     "[-swnu] [-c loopcycles] [-t timeout]\n"
        "[-swnu] [-c loopcycles]\n"
        "     -s Be silent\n"
-       "     -w Write result to environment variable '"ENV_CPU"'"
+       "     -w Write result to environment variable '"ENV_CPU"'\n"
        "     -n Don't load code snippet. \n"
        "     -u Don't unload. Leave code snippet in ram."
 //     "     -t Timeout (ms)\n"