]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/print-utils.c
Tup: F_CPU per config
[z180-stamp.git] / avr / print-utils.c
1 /*
2 * (C) Copyright 2014,2018 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7 #include "common.h"
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <ctype.h>
11 #include "con-utils.h"
12 #include "print-utils.h"
13
14 void print_blanks(uint_fast8_t count)
15 {
16 while(count--)
17 putchar(' ');
18 }
19
20
21 ERRNUM eeprom_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
22 {
23 eeprom_read_block((void *) buf, (const void *) (size_t) addr, count);
24 return ESUCCESS;
25 }
26
27 ERRNUM ram_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
28 {
29 while (count--)
30 *buf++ = *(uint8_t *) (size_t) addr++;
31 return ESUCCESS;
32 }
33
34 ERRNUM flash_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
35 {
36 while (count--)
37 *buf++ = *(const __memx uint8_t *) (__uint24) addr++;
38 return ESUCCESS;
39 }
40
41 ERRNUM dump_mem(uint32_t address, uint32_t offset, uint32_t len,
42 ERRNUM (*readfkt)(uint8_t *, uint32_t, uint8_t), char *title)
43 {
44 uint8_t buf[16];
45 uint8_t llen = 16;
46 uint8_t pre = offset % 16;
47 offset = offset & ~0x0f;
48 len += pre;
49 uint8_t i;
50
51 if (title && *title) {
52 printf_P(PSTR("%s\n"),title);
53 }
54
55 while (len) {
56 if (len < 16)
57 llen = len;
58 ERRNUM err = readfkt(buf, address, llen - pre);
59 if (err != ESUCCESS)
60 return err;
61
62 if (title)
63 print_blanks(4);
64 printf_P(PSTR("%.5lx:"),offset);
65 for (i = 0; i < llen; i++) {
66 if ((i % 8) == 0)
67 putchar(' ');
68 if (i < pre)
69 printf_P(PSTR(".. "));
70 else
71 printf_P(PSTR("%.2x "), buf[i-pre]);
72 }
73 /* fill line with whitespace for nice ASCII print */
74 print_blanks(3 * (16u - i) + (16u-i)/8 + 1 + pre);
75 /* Print data in ASCII characters */
76 for (i = pre; i < llen; i++)
77 printf_P(PSTR("%c"), isprint(buf[i-pre]) ? buf[i-pre] : '.');
78 putchar('\n');
79
80 address += llen - pre;
81 offset += 16;
82 pre = 0;
83 len -= llen;
84
85 if (ctrlc())
86 return EINTR;
87 }
88 return ESUCCESS;
89 }
90
91 void dump_eep(uint32_t addr, unsigned int len, char *title)
92 {
93 dump_mem(addr, addr, len, eeprom_read_buf, title);
94 }
95
96 void dump_ram(uint8_t *addr, size_t offset, unsigned int len, char *title)
97 {
98 dump_mem((uint32_t) (size_t) addr, offset, len, ram_read_buf, title);
99 }