]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cmd_cpu.c
clean up includes
[z180-stamp.git] / avr / cmd_cpu.c
index 6e36b2b64f5c622cabe1cc352387777307c5364d..2abf5db150d602c0d6498f0d486f40dfdb77fc2a 100644 (file)
@@ -5,7 +5,6 @@
  */
 
 #include "cmd_cpu.h"
-//#include <ctype.h>
 #include <util/atomic.h>
 
 #include "z80-if.h"
@@ -26,7 +25,7 @@
 #define debug_cpu(fmt, args...)                                                              \
        debug_cond(DEBUG_CPU, fmt, ##args)
 
-
+static
 char * ulltoa (uint64_t val, char *s)
 {
        char *p = s;
@@ -61,21 +60,19 @@ static uint32_t z80_measure_phi(uint_fast8_t cycles)
 
        PRR1 &= ~_BV(PRTIM3);
        TCCR3A = 0;
-       TCCR3B = 0b000<<CS30;   /* stop counter */
+       TCCR3B = 0b000<<CS30;                   /* stop counter */
        TCNT3 = 0;
        x_ovfl = 0;
        TIFR3 = _BV(TOV3);
        ref_ovfl = 0;
 
        ATOMIC_BLOCK(ATOMIC_FORCEON) {
-               /* Reset pending int */
-               EIFR = _BV(INTF6);
-               /* Wait for falling edge */
-               while ((EIFR & _BV(INTF6)) == 0)
+               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 */
+               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) {
@@ -111,6 +108,10 @@ static uint32_t z80_measure_phi(uint_fast8_t cycles)
        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);
@@ -124,19 +125,9 @@ static uint32_t z80_measure_phi(uint_fast8_t cycles)
 
        /* round to 5 decimal digits */
        int_fast8_t sc = 5;
-       while (sc > 0 || x_tmp >= 100000) {
-               x_tmp = (x_tmp + 5)/10;
-           sc--;
-       }
+       for ( ; sc > 0 || x_tmp >= 100000; sc--) x_tmp = (x_tmp + 5)/10;
        x_freq = x_tmp;
-       while (sc < 0) {
-               x_freq *= 10;
-           sc++;
-       }
-
-       /* Stop Timer */
-       TCCR3B = 0;
-       PRR1 |= _BV(PRTIM3);
+       for ( ; sc < 0; sc++) x_freq *= 10;
 
        return x_freq;
 }
@@ -159,7 +150,12 @@ static const FLASH char * const FLASH cpu_strings[] = {
 
 static const FLASH char * const FLASH opt_strings[] = {
        FSTR("swnu"),           /* Options for chkcpu */
-       FSTR("swnuc:t:"),       /* Oprions for cpufreq */
+       FSTR("swnuc:"),         /* Oprions for cpufreq */
+};
+
+static const FLASH char * const FLASH env_names[] = {
+       FSTR(ENV_CPU),          /* Env var for chkcpu result */
+       FSTR(ENV_CPU_FREQ),     /* Env var for cpufreq result */
 };
 
 command_ret_t do_cpu_freq_chk(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
@@ -169,7 +165,7 @@ command_ret_t do_cpu_freq_chk(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED,
        uint32_t cpu_freq = 0;
        uint_fast8_t lcycles = 0;
        uint_fast8_t freq_cmd = 0;
-       uint16_t timeout = 1000;
+//     uint16_t timeout = 1000;
        uint8_t eimsk_save;
        ERRNUM err = ESUCCESS;
 
@@ -194,9 +190,9 @@ command_ret_t do_cpu_freq_chk(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED,
                case 'c':
                        lcycles = eval_arg(optarg, NULL);
                        break;
-               case 't':
-                       timeout = eval_arg(optarg, NULL);
-                       break;
+//             case 't':
+//                     timeout = eval_arg(optarg, NULL);
+//                     break;
                default: /* '?' */
                        return CMD_RET_USAGE;
                }
@@ -281,24 +277,27 @@ command_ret_t do_cpu_freq_chk(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED,
        if (err)
                cmd_error(CMD_RET_FAILURE, err, NULL);
 
+       char result_str[11];
+
        if (freq_cmd) {
+               ultoa(cpu_freq, result_str, 10);
+       } else {
+               if (cputype >= ARRAY_SIZE(cpu_strings))
+                       cputype = 0;
+               strcpy_P(result_str, cpu_strings[cputype]);
+       }
 
-               if (!(options & O_SILENT))
-                       printf_P(PSTR("%lu\n"), cpu_freq);
+       if (!(options & O_SILENT))
+               printf_P(PSTR("%s\n"), result_str);
 
-#if 0
-               if (options & O_WENV) {
-                       if (setenv_ulong(PSTR(ENV_CPU_FREQ), cpu_freq)) {
-                               if (!(options & O_SILENT))
-                                       printf_P(PSTR("'SETENV (%S, %lu)' failed!\n"), PSTR(ENV_CPU_FREQ), cpu_freq);
-                               return CMD_RET_FAILURE;
+       if (options & O_WENV) {
+               if (setenv(env_names[freq_cmd], result_str)) {
+                       if (!(options & O_SILENT)) {
+                               printf_P(PSTR("'setenv %S %s' failed!\n"), env_names[freq_cmd], result_str);
+                               //cmd_error(CMD_RET_FAILURE, ENOMEM, PSTR("'setenv (%S, %s)' failed"), env_names[freq_cmd], result_str);
                        }
+                       return CMD_RET_FAILURE;
                }
-#endif
-       } else {
-               if (cputype >= ARRAY_SIZE(cpu_strings))
-                       cputype = 0;
-               printf_P(PSTR("Detected CPU: %S\n"), cpu_strings[cputype]);
        }
 
        return CMD_RET_SUCCESS;
@@ -419,19 +418,29 @@ command_ret_t do_busack_test(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED,
 /*
  * command table for subcommands
  */
-
 cmd_tbl_t cmd_tbl_cpu[] = {
 CMD_TBL_ITEM(
        freq,   CONFIG_SYS_MAXARGS,     CTBL_RPT,       do_cpu_freq_chk,
        "Measure cpu frequency",
-       "[-qwn] [-c loopcycles] [-t timeout]\n"
-       "     -q Be quiet\n"
-//     "     -w  Write result to environment variable '"ENV_CPU_FREQ"'"
+//     "[-swnu] [-c loopcycles] [-t timeout]\n"
+       "[-swnu] [-c loopcycles]\n"
+       "     -s Be silent\n"
+       "     -w Write result to environment variable '"ENV_CPU_FREQ"'"
+       "     -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."
+//     "     -t Timeout (ms)\n"
 ),
 CMD_TBL_ITEM(
        chkcpu, CONFIG_SYS_MAXARGS,     CTBL_RPT|CTBL_SUBCMDAUTO, do_cpu_freq_chk,
        "Check/Identify CPU",
-       ""
+//     "[-swnu] [-c loopcycles] [-t timeout]\n"
+       "[-swnu] [-c loopcycles]\n"
+       "     -s Be silent\n"
+       "     -w Write result to environment variable '"ENV_CPU"'"
+       "     -n Don't load code snippet. \n"
+       "     -u Don't unload. Leave code snippet in ram."
+//     "     -t Timeout (ms)\n"
 ),
 CMD_TBL_ITEM(
        buscmd, CONFIG_SYS_MAXARGS,     CTBL_RPT,       do_bus_test,