Skip to content

Commit e26d018

Browse files
Jeppe Odgaardfabiobaltieri
authored andcommitted
samples: flash_shell: rework dump_buffer()
Add an optional compare buffer to check each byte. If the byte differ the read value will be printed as an error. Signed-off-by: Jeppe Odgaard <[email protected]>
1 parent 5ad65df commit e26d018

File tree

1 file changed

+33
-37
lines changed
  • samples/drivers/flash_shell/src

1 file changed

+33
-37
lines changed

samples/drivers/flash_shell/src/main.c

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,40 +98,33 @@ static int check_flash_device(const struct shell *sh)
9898
return 0;
9999
}
100100

101-
static void dump_buffer(const struct shell *sh, uint8_t *buf, size_t size)
101+
static int dump_buffer(const struct shell *sh, uint8_t *buf, size_t size,
102+
uint8_t *cmp_buf)
102103
{
103-
bool newline = false;
104-
uint8_t *p = buf;
105-
106-
while (size >= 16) {
107-
PR_SHELL(sh, "%02x %02x %02x %02x | %02x %02x %02x %02x | "
108-
"%02x %02x %02x %02x | %02x %02x %02x %02x\n",
109-
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
110-
p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
111-
p += 16;
112-
size -= 16;
113-
}
114-
if (size >= 8) {
115-
PR_SHELL(sh, "%02x %02x %02x %02x | %02x %02x %02x %02x | ",
116-
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
117-
p += 8;
118-
size -= 8;
119-
newline = true;
120-
}
121-
if (size >= 4) {
122-
PR_SHELL(sh, "%02x %02x %02x %02x | ",
123-
p[0], p[1], p[2], p[3]);
124-
p += 4;
125-
size -= 4;
126-
newline = true;
127-
}
128-
while (size--) {
129-
PR_SHELL(sh, "%02x ", *p++);
130-
newline = true;
131-
}
132-
if (newline) {
104+
int ret = 0;
105+
size_t i;
106+
107+
for (i = 0; i < size; i++) {
108+
/* Print each byte mismatch as error */
109+
if (cmp_buf != NULL && buf[i] != cmp_buf[i]) {
110+
PR_ERROR(sh, "%02x ", buf[i]);
111+
ret = -EIO;
112+
} else {
113+
PR_SHELL(sh, "%02x ", buf[i]);
114+
}
115+
116+
if ((i + 1) % 16 == 0) {
117+
PR_SHELL(sh, "\n");
118+
} else if ((i + 1) % 4 == 0) {
119+
PR_SHELL(sh, "| ");
120+
}
121+
}
122+
123+
if (i % 16 != 0) {
133124
PR_SHELL(sh, "\n");
134125
}
126+
127+
return ret;
135128
}
136129

137130
static int parse_ul(const char *str, unsigned long *result)
@@ -167,7 +160,7 @@ static int do_read(const struct shell *sh, off_t offset, size_t len,
167160
uint8_t buf[64];
168161
int ret;
169162
size_t read_len;
170-
bool cmp_ok = true;
163+
bool cmp_error = false;
171164

172165
do {
173166
read_len = len > sizeof(buf) ? sizeof(buf) : len;
@@ -176,19 +169,22 @@ static int do_read(const struct shell *sh, off_t offset, size_t len,
176169
PR_ERROR(sh, "flash_read error: %d\n", ret);
177170
return ret;
178171
}
179-
dump_buffer(sh, buf, read_len);
172+
ret = dump_buffer(sh, buf, read_len, cmp_buf);
173+
if (ret == -EIO) {
174+
cmp_error = true;
175+
}
180176
if (cmp_buf != NULL) {
181-
cmp_ok &= memcmp(cmp_buf, buf, read_len) == 0;
182177
cmp_buf += read_len;
183178
}
184179
len -= read_len;
185180
offset += read_len;
186181
} while (len > 0);
187182

188-
if (cmp_buf != NULL && !cmp_ok) {
189-
PR_ERROR(sh, "Verification ERROR!\n");
190-
return -EIO;
183+
if (cmp_error) {
184+
PR_ERROR(sh, "Write verification error, unexpected values "
185+
"marked red\n");
191186
}
187+
192188
return 0;
193189
}
194190

0 commit comments

Comments
 (0)