Skip to content

Commit

Permalink
tests/periph/flashpage: make mtd_raw write bytes instead of ASCII
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian18 committed May 6, 2024
1 parent a5996e2 commit 629c0c2
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions tests/periph/flashpage/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "architecture.h"
#include "od.h"
#include "shell.h"
#include "periph/flashpage.h"
#include "unaligned.h"
#include "fmt.h"

#define LINE_LEN (16)

Expand All @@ -53,7 +55,7 @@
/*
* @brief Allocate an aligned buffer for raw writings
*/
static char raw_buf[RAW_BUF_SIZE] ALIGNMENT_ATTR;
static uint8_t raw_buf[RAW_BUF_SIZE] ALIGNMENT_ATTR;

#ifdef MODULE_PERIPH_FLASHPAGE_PAGEWISE
/**
Expand Down Expand Up @@ -221,17 +223,29 @@ static int cmd_write_raw(int argc, char **argv)
uintptr_t addr;

if (argc < 3) {
printf("usage: %s <addr> <data>\n", argv[0]);
printf("usage: %s <addr> <hexadecimal data>\n", argv[0]);
return 1;
}

addr = getaddr(argv[1]);
/* try to align */
memcpy(raw_buf, argv[2], strlen(argv[2]));
int len;
if ((len = strlen(argv[2])) % 2 || (unsigned)len > sizeof(raw_buf) * 2) {
printf("error: data must have an even length and must be <= %"PRIuSIZE"\n",
sizeof(raw_buf) * 2);
return 1;
}
for (int i = 0; i < len; i++) {
if (!isxdigit((int)(argv[2][i]))) {
printf("error: data must be hexadecimal\n");
return 1;
}
}
len = fmt_hex_bytes(raw_buf, argv[2]);

flashpage_write((void*)(uintptr_t)addr, raw_buf, strlen(raw_buf));
printf("wrote local data to flash address %#" PRIxPTR " of len %" PRIuSIZE "\n",
addr, strlen(raw_buf));
flashpage_write((void*)(uintptr_t)addr, raw_buf, len);
printf("wrote local data to flash address %#" PRIxPTR " of len %d\n",
addr, len);
return 0;
}

Expand Down Expand Up @@ -414,7 +428,7 @@ static int cmd_test_last_raw(int argc, char **argv)
(void) argc;
(void) argv;

memset(raw_buf, 0, sizeof(raw_buf));
memset(raw_buf, 0xff, sizeof(raw_buf));

/* try to align */
memcpy(raw_buf, "test12344321tset", 16);
Expand All @@ -428,7 +442,7 @@ static int cmd_test_last_raw(int argc, char **argv)
flashpage_write(flashpage_addr(TEST_LAST_AVAILABLE_PAGE), raw_buf, sizeof(raw_buf));

/* verify that previous write_raw effectively wrote the desired data */
if (memcmp(flashpage_addr(TEST_LAST_AVAILABLE_PAGE), raw_buf, strlen(raw_buf)) != 0) {
if (memcmp(flashpage_addr(TEST_LAST_AVAILABLE_PAGE), raw_buf, 16) != 0) {
puts("error verifying the content of last page");
return 1;
}
Expand Down Expand Up @@ -575,10 +589,10 @@ static int cmd_test_last_rwwee_raw(int argc, char **argv)
/* erase the page first */
flashpage_rwwee_write_page(((int)FLASHPAGE_RWWEE_NUMOF - 1), NULL);

flashpage_rwwee_write(flashpage_rwwee_addr((int)FLASHPAGE_RWWEE_NUMOF - 1), raw_buf, strlen(raw_buf));
flashpage_rwwee_write(flashpage_rwwee_addr((int)FLASHPAGE_RWWEE_NUMOF - 1), raw_buf, 16);

/* verify that previous write_raw effectively wrote the desired data */
if (memcmp(flashpage_rwwee_addr((int)FLASHPAGE_RWWEE_NUMOF - 1), raw_buf, strlen(raw_buf)) != 0) {
if (memcmp(flashpage_rwwee_addr((int)FLASHPAGE_RWWEE_NUMOF - 1), raw_buf, 16) != 0) {
puts("error verifying the content of last RWWEE page");
return 1;
}
Expand Down Expand Up @@ -673,7 +687,7 @@ static const shell_command_t shell_commands[] = {
{ "read", "Copy the given page to the local page buffer and dump to STDOUT", cmd_read },
{ "write", "Write the local page buffer to the given page", cmd_write },
#endif
{ "write_raw", "Write (ASCII, max 64B) data to the given address", cmd_write_raw },
{ "write_raw", "Write raw bytes (max 64B) to the given address", cmd_write_raw },
{ "erase", "Erase the given page buffer", cmd_erase },
#ifdef MODULE_PERIPH_FLASHPAGE_PAGEWISE
{ "edit", "Write bytes to the local page buffer", cmd_edit },
Expand Down

0 comments on commit 629c0c2

Please sign in to comment.