+
+/* 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, 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;
+};
+
+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)
+
+size_t get_freemem(void)
+{
+ return (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
+}
+
+void
+printfreelist(const char * title)
+{
+ struct __freelist *fp1;
+ int i;
+ unsigned int freesum = 0;
+
+ if (!__flp) {
+ printf_P(PSTR("%s no free list\n"), title ? title : "");
+ } else {
+ printf_P(PSTR("Free list: %s\n"), title ? title : "");
+ for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
+ printf_P(PSTR(" entry %d @ %04x: size %4u, next "),
+ i, (size_t)fp1, fp1->sz);
+ if (fp1->nx)
+ printf_P(PSTR("%04x\n"), (size_t)fp1->nx);
+ else
+ printf_P(PSTR("NULL\n"));
+ freesum += fp1->sz;
+ }
+ }
+
+ freesum += get_freemem();
+
+ printf_P(PSTR("SP: %04x, __brkval: %04x, Total free: %04u\n"),
+ (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
+}
+
+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);
+
+ 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 */