]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/debug.c
switch fifos conin,conout
[z180-stamp.git] / avr / debug.c
index 620eb4dc372f523ff49b06d4ae1f9458ef2333dc..f3632c288018c14ca31476ae4263f5393f5e8d75 100644 (file)
+/*
+ * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
 #include "common.h"
 #include <stdlib.h>
+#include <string.h>
 #include <ctype.h>
 #include <avr/eeprom.h>
 
 #include "command.h"
+#include "cli_readline.h"
+#include "print-utils.h"
 #include "debug.h"
 
 /*
  * Debugging
  */
-#ifdef DEBUG
-
 
-static void print_blanks(uint_fast8_t count)
-{
-       while(count--)
-               putchar(' ');
-}
+#ifdef DEBUG
 
 
-void dump_ram(const uint8_t *startaddr, int len, char *title)
-{
-       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);
-
-       while (len) {
-               if (len < 16)
-                       llen = len;
-
-               printf_P(PSTR("    %.4x:"), (size_t) addr);
-               print_blanks(3 * pre);
-               for (i = pre; i < llen; i++)
-                       printf_P(PSTR(" %.2x"), addr[i]);
-               print_blanks(3 * (16 - i + 1) + pre);
-               for (i = pre; i < llen; i++)
-                       printf_P(PSTR("%c"), isprint(addr[i]) ? addr[i] : '.');
-               putchar('\n');
-
-               pre = 0;
-               addr += 16;
-               len -= llen;
-       }
-}
-
+#if 0
 void dump_heap(void)
 {
        extern unsigned int __brkval;
 
-       dump_ram((uint8_t *) __malloc_heap_start,
+       dump_ram(__malloc_heap_start,
                __brkval - (unsigned int) __malloc_heap_start,
                "=== Heap:");
 }
+#endif
 
-/* TODO: combine with dump_ram() */
-void dump_eep(const uint8_t *addr, unsigned int len)
-{
-       uint_fast8_t i;
-       uint8_t buf[16];
-
-       printf_P(PSTR("eeprom dump:"));
-       while (len) {
-               printf_P(PSTR("\n    0x%.4x:"), (unsigned int) addr);
-               for (i = 0; i<16; i++)
-                       buf[i] = eeprom_read_byte(addr + i);
-               for (i = 0; i<16; i++)
-                       printf_P(PSTR(" %.2x"), buf[i]);
-               printf_P(PSTR("   "));
-               for (i = 0; i<16; i++)
-                       printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.');
-
-               addr += 16;
-               len -= len > 16 ? 16 : len;
-       }
-       putchar('\n');
-}
 
 /*
- * EEPROM Display
+ * Memory Display
  *     md addr {len}
  */
-int do_dump_eep(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-       const uint8_t *addr;
-       uint16_t length;
-
-       (void) cmdtp;
+       void (*readwhat)(uint8_t *buf, uint32_t addr, uint8_t count);
 
-
-       /* We use the last specified parameters, unless new ones are
-        * entered.
-        */
-       addr = 0;
-       length = 128;
+       (void) cmdtp; (void) flag;
 
        if (argc < 2)
                return CMD_RET_USAGE;
 
-       if ((flag & CMD_FLAG_REPEAT) == 0) {
-               /* Address is specified since argc > 1 */
-               addr = (const uint8_t *) (size_t) strtoul(argv[1], NULL, 16);
-
-               /* If another parameter, it is the length to display. */
-               if (argc > 2)
-                       length = (uint16_t) strtoul(argv[2], NULL, 16);
+       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;
        }
 
+       /* Address is specified since argc > 1 */
+       addr =  strtoul(argv[1], NULL, 16);
+
+       /* If another parameter, it is the length to display. */
+       if (argc > 2)
+               length = (uint16_t) strtoul(argv[2], NULL, 16);
+
        /* Print the lines. */
-       dump_eep(addr, length);
+       dump_mem(addr, addr, length, readwhat, NULL);
 
-       return 0;
+       return CMD_RET_SUCCESS;
 }
 
-int do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        uint16_t src, dest, count;
        int_fast8_t step;
@@ -134,19 +94,19 @@ int do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
        if (src > E2END) {
                debug("src > EEPROM size: 0x%04x\n", src);
-               return 1;
+               return CMD_RET_FAILURE;
        }
        if (dest > E2END) {
                debug("dest > EEPROM size: 0x%04x\n", dest);
-               return 1;
+               return CMD_RET_FAILURE;
        }
        if (count > E2END+1) {
                debug("count > EEPROM size: 0x%04x\n", count);
-               return 1;
+               return CMD_RET_FAILURE;
        }
        if (count == 0) {
-               debug("Zero length ???\n");
-               return 1;
+               debug("Zero length?\n");
+               return CMD_RET_FAILURE;
        }
 
        if (dest > src) {
@@ -164,7 +124,131 @@ int do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                dest += step;
 
        }
-       return 0;
+       return CMD_RET_SUCCESS;
 }
-#endif /* DEBUG */
 
+
+/* Modify memory.
+ *
+ * Syntax:
+ *     !mm {addr}
+ *     !nm {addr}
+ */
+
+ static uint8_t        *mm_last_addr;
+
+static command_ret_t
+mod_mem_avr(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
+{
+       uint8_t *addr;
+       uint8_t data;
+       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) strtoul(argv[1], NULL, 16);
+       }
+
+       /* Print the address, followed by value.  Then accept input for
+        * the next value.  A non-converted value exits.
+        */
+       do {
+               data = *addr;
+               printf_P(PSTR("%04x: %02x"), addr, data);
+
+               nbytes = cli_readline(PSTR(" ? "));
+               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 = strtoul(console_buffer, &endp, 16);
+                       nbytes = endp - console_buffer;
+                       if (nbytes) {
+                               *addr = data;
+                               if (incrflag)
+                                       addr++;
+                       }
+               }
+       } while (nbytes);
+
+       mm_last_addr = addr;
+       return CMD_RET_SUCCESS;
+}
+
+
+command_ret_t do_mem_mm_avr(cmd_tbl_t *cmdtp, int 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, int flag, int argc, char * const argv[])
+{
+       return mod_mem_avr (cmdtp, 0, flag, argc, argv);
+}
+
+/*------------------------------------------------------------------------------*/
+
+#if 1
+
+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) */
+
+#define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
+
+void
+printfreelist(const char * title)
+{
+       struct __freelist *fp1;
+       int i;
+       unsigned int freesum = 0;
+
+/* TODO: printf_P */
+
+       if (!__flp) {
+               printf("%s no free list\n", title ? title : "");
+       } else {
+               printf("Free list: %s\n", title ? title : "");
+               for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
+                       printf("    entry %d @ %04x: size %4u, next ",
+                              i, (size_t)fp1, fp1->sz);
+                       if (fp1->nx)
+                               printf("%04x\n", (size_t)fp1->nx);
+                       else
+                               printf("NULL\n");
+                       freesum += fp1->sz;
+               }
+       }
+
+       freesum +=  (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
+
+       printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
+               (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
+}
+
+#endif
+
+#endif /* DEBUG */