]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/debug.c
rewrite of cmd_cpu/do_cpu_freq
[z180-stamp.git] / avr / debug.c
index 47b11b097cc5b51130f460eb760e5d4fd6564231..89ef4b131a0757e5dedf45f6773abe733c581bcd 100644 (file)
+/*
+ * (C) Copyright 2014,2016 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * SPDX-License-Identifier:    GPL-2.0
+ */
+
+#include "debug.h"
 #include "common.h"
 #include "common.h"
-#include <stdlib.h>
+#include <stdlib.h>                    /* __malloc_margin */
 #include <string.h>
 #include <ctype.h>
 #include <avr/eeprom.h>
 
 #include "command.h"
 #include <string.h>
 #include <ctype.h>
 #include <avr/eeprom.h>
 
 #include "command.h"
-#include "debug.h"
+#include "cli_readline.h"
+#include "eval_arg.h"
+#include "print-utils.h"
 
 /*
  * Debugging
  */
 
 /*
  * Debugging
  */
-#ifdef DEBUG
-
-static void print_blanks(uint_fast8_t count)
-{
-       while(count--)
-               putchar(' ');
-}
-
-static uint8_t ram_read_byte(const uint8_t *p)
-{
-       return *p;
-}
-
-void dump_mem(const uint8_t *startaddr, int len,
-               uint8_t (*readfkt)(const uint8_t *), char *title)
-{
-       uint8_t buf[16];
-       char *indent = NULL;
-       uint8_t llen = 16;
-       uint8_t pre = (size_t) startaddr % 16;
-       const uint8_t *addr = (uint8_t *) ((size_t) startaddr & ~0x0f);
-       len += pre;
-       uint8_t i;
-
-       if (title && *title) {
-               printf_P(PSTR("%s\n"),title);
-               indent = "    ";
-       }
-
-       while (len) {
-               if (len < 16)
-                       llen = len;
-
-               for (i = pre; i < llen; i++)
-                       buf[i] = readfkt(addr + i);
-
-               printf_P(PSTR("%s%04x:"),indent, addr);
-               for (i = 0; i < llen; i++) {
-                       if ((i % 8) == 0)
-                               putchar(' ');
-                       if (i < pre)
-                               printf_P(PSTR(".. "));
-                       else
-                               printf_P(PSTR("%.2x "), buf[i]);
-               }
-               /* fill line with whitespace for nice ASCII print */
-               print_blanks(3 * (16u - i) + (16u-i)/8 + 1 + pre);
-               /* Print data in ASCII characters */
-               for (i = pre; i < llen; i++)
-                       printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.');
-               putchar('\n');
-
-               pre = 0;
-               addr += 16;
-               len -= llen;
-       }
-}
-
-void dump_eep(const uint8_t *addr, unsigned int len, char *title)
-{
-       dump_mem(addr, len, eeprom_read_byte, title);
-}
-
-void dump_ram(const uint8_t *addr, unsigned int len, char *title)
-{
-       dump_mem(addr, len, ram_read_byte, title);
-}
-
-
-#if 0
-void dump_heap(void)
-{
-       extern unsigned int __brkval;
-
-       dump_ram((uint8_t *) __malloc_heap_start,
-               __brkval - (unsigned int) __malloc_heap_start,
-               "=== Heap:");
-}
-#endif
 
 
+#ifdef DEBUG
 
 /*
  * Memory Display
  *     md addr {len}
  */
 
 /*
  * Memory Display
  *     md addr {len}
  */
-command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_dump_mem(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
 {
 {
-//     static const uint8_t *addr;
-//     static uint16_t length = 128;
-       uint8_t (*readhow)(const uint8_t *);
-
-       (void) cmdtp; (void) flag;
+       ERRNUM (*readwhat)(uint8_t *buf, uint32_t addr, uint8_t count);
 
        if (argc < 2)
                return CMD_RET_USAGE;
 
 
        if (argc < 2)
                return CMD_RET_USAGE;
 
-       const uint8_t *addr;
-       uint16_t length = 128;
-
-       if (strchr(argv[0],'r') != NULL)
-               readhow = ram_read_byte;
-       else if (strchr(argv[0],'e') != NULL)
-               readhow = eeprom_read_byte;
-       else
+       uint32_t addr;
+       uint32_t length = 128;
+
+       switch (argv[0][3]) {
+       case 'r':
+               readwhat = ram_read_buf;
+               break;
+       case 'e':
+               readwhat = eeprom_read_buf;
+               break;
+       case 'f':
+               readwhat = flash_read_buf;
+               break;
+       default:
                return CMD_RET_USAGE;
                return CMD_RET_USAGE;
+       }
 
        /* Address is specified since argc > 1 */
 
        /* Address is specified since argc > 1 */
-       addr = (const uint8_t *) (size_t) strtoul(argv[1], NULL, 16);
+       addr =  eval_arg(argv[1], NULL);
 
        /* If another parameter, it is the length to display. */
        if (argc > 2)
 
        /* If another parameter, it is the length to display. */
        if (argc > 2)
-               length = (uint16_t) strtoul(argv[2], NULL, 16);
+               length = (uint16_t) eval_arg(argv[2], NULL);
 
        /* Print the lines. */
 
        /* Print the lines. */
-       dump_mem(addr, length, readhow, NULL);
+       dump_mem(addr, addr, length, readwhat, NULL);
 
        return CMD_RET_SUCCESS;
 }
 
 
        return CMD_RET_SUCCESS;
 }
 
-command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
 {
        uint16_t src, dest, count;
        int_fast8_t step;
 {
        uint16_t src, dest, count;
        int_fast8_t step;
@@ -140,9 +74,9 @@ command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
        if (argc != 4)
                return CMD_RET_USAGE;
 
        if (argc != 4)
                return CMD_RET_USAGE;
 
-       src = (size_t) strtoul(argv[1], NULL, 16);
-       dest = (size_t) strtoul(argv[2], NULL, 16);
-       count = (size_t) strtoul(argv[3], NULL, 16);
+       src   = (size_t) eval_arg(argv[1], NULL);
+       dest  = (size_t) eval_arg(argv[2], NULL);
+       count = (size_t) eval_arg(argv[3], NULL);
 
        if (src > E2END) {
                debug("src > EEPROM size: 0x%04x\n", src);
 
        if (src > E2END) {
                debug("src > EEPROM size: 0x%04x\n", src);
@@ -179,21 +113,100 @@ command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
        return CMD_RET_SUCCESS;
 }
 
        return CMD_RET_SUCCESS;
 }
 
-/*------------------------------------------------------------------------------*/
 
 
+/* Modify memory.
+ *
+ * Syntax:
+ *     !mm {addr}
+ *     !nm {addr}
+ */
+
+ static uint8_t        *mm_last_addr;
 
 
-#if 1
+static command_ret_t
+mod_mem_avr(cmd_tbl_t *cmdtp, int incrflag, uint_fast8_t flag, int argc, char * const argv[])
+{
+       uint8_t *addr;
+       int nbytes;
+
+       (void) cmdtp;
+
+       if (argc != 2)
+               return CMD_RET_USAGE;
+
+       /* We use the last specified parameters, unless new ones are
+        * entered.
+        */
+       addr = mm_last_addr;
+
+       if ((flag & CMD_FLAG_REPEAT) == 0) {
+               /* New command specified.
+                */
+
+               /* Address is specified since argc > 1
+               */
+               addr = (uint8_t *) (size_t) eval_arg(argv[1], NULL);
+       }
+
+       /* Print the address, followed by value.  Then accept input for
+        * the next value.  A non-converted value exits.
+        */
+       do {
+               uint8_t data = *addr;
+               printf_P(PSTR("%04x: %02x"), addr, data);
+
+               nbytes = cli_readline(PSTR(" ? "), 0);
+               if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
+                       /* <CR> pressed as only input, don't modify current
+                        * location and move to next. "-" pressed will go back.
+                        */
+                       if (incrflag)
+                               addr += nbytes ? -1 : 1;
+                       nbytes = 1;
+
+               } else {
+                       char *endp;
+                       data = eval_arg(console_buffer, &endp);
+                       nbytes = endp - console_buffer;
+                       if (nbytes) {
+                               *addr = data;
+                               if (incrflag)
+                                       addr++;
+                       }
+               }
+       } while (nbytes > 0);
+
+       mm_last_addr = addr;
+       return CMD_RET_SUCCESS;
+}
+
+
+command_ret_t do_mem_mm_avr(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+{
+       return mod_mem_avr (cmdtp, 1, flag, argc, argv);
+}
+command_ret_t do_mem_nm_avr(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+{
+       return mod_mem_avr (cmdtp, 0, flag, argc, argv);
+}
+
+/*------------------------------------------------------------------------------*/
 
 struct __freelist {
        size_t sz;
        struct __freelist *nx;
 };
 
 
 struct __freelist {
        size_t sz;
        struct __freelist *nx;
 };
 
-extern char *__brkval;         /* first location not yet allocated */
-extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
+extern char *__brkval;                         /* first location not yet allocated */
+extern struct __freelist *__flp;       /* freelist pointer (head of freelist) */
 
 #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
 
 
 #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
 
+size_t get_freemem(void)
+{
+       return (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
+}
+
 void
 printfreelist(const char * title)
 {
 void
 printfreelist(const char * title)
 {
@@ -201,30 +214,47 @@ printfreelist(const char * title)
        int i;
        unsigned int freesum = 0;
 
        int i;
        unsigned int freesum = 0;
 
-/* TODO: printf_P */
-
        if (!__flp) {
        if (!__flp) {
-               printf("%s no free list\n", title ? title : "");
+               printf_P(PSTR("%s no free list\n"), title ? title : "");
        } else {
        } else {
-               printf("Free list: %s\n", title ? title : "");
+               printf_P(PSTR("Free list: %s\n"), title ? title : "");
                for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
                for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
-                       printf("    entry %d @ %04x: size %4u, next ",
+                       printf_P(PSTR("    entry %d @ %04x: size %4u, next "),
                               i, (size_t)fp1, fp1->sz);
                        if (fp1->nx)
                               i, (size_t)fp1, fp1->sz);
                        if (fp1->nx)
-                               printf("%04x\n", (size_t)fp1->nx);
+                               printf_P(PSTR("%04x\n"), (size_t)fp1->nx);
                        else
                        else
-                               printf("NULL\n");
+                               printf_P(PSTR("NULL\n"));
                        freesum += fp1->sz;
                }
        }
 
                        freesum += fp1->sz;
                }
        }
 
-       freesum +=  (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
+       freesum += get_freemem();
 
 
-       printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
+       printf_P(PSTR("SP: %04x, __brkval: %04x, Total free: %04u\n"),
                (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
 }
 
                (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
 }
 
-#endif
+command_ret_t do_pr_free_avr(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
+{
+       printfreelist(NULL);
 
 
-#endif /* DEBUG */
+       return CMD_RET_SUCCESS;
+}
+
+void dump_heap(void)
+{
+       //extern unsigned int __brkval;
+
+       dump_ram((uint8_t *)__malloc_heap_start, (size_t) __malloc_heap_start,  __brkval - __malloc_heap_start,
+                                       "=== Heap:");
+}
+
+command_ret_t do_pr_heap_avr(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
+{
+       dump_heap();
 
 
+       return CMD_RET_SUCCESS;
+}
+
+#endif /* DEBUG */