From 202c9e174f120eef347b4d71c233aa73c481b2bc Mon Sep 17 00:00:00 2001
From: kYc0o <fco.ja.ac@gmail.com>
Date: Thu, 9 Nov 2017 01:59:02 +0100
Subject: [PATCH] tests/periph_flashpage: add write_raw command

---
 tests/periph_flashpage/Makefile |  3 ++-
 tests/periph_flashpage/main.c   | 40 +++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/tests/periph_flashpage/Makefile b/tests/periph_flashpage/Makefile
index 746a26411b..6307181d98 100644
--- a/tests/periph_flashpage/Makefile
+++ b/tests/periph_flashpage/Makefile
@@ -2,7 +2,8 @@ APPLICATION = periph_flashpage
 BOARD ?= iotlab-m3
 include ../Makefile.tests_common
 
-FEATURES_REQUIRED = periph_flashpage
+FEATURES_REQUIRED += periph_flashpage
+FEATURES_OPTIONAL += periph_flashpage_raw
 
 USEMODULE += shell
 
diff --git a/tests/periph_flashpage/main.c b/tests/periph_flashpage/main.c
index df99d946ef..b7a901f3ee 100644
--- a/tests/periph_flashpage/main.c
+++ b/tests/periph_flashpage/main.c
@@ -32,6 +32,20 @@
  */
 static uint8_t page_mem[FLASHPAGE_SIZE];
 
+#ifdef MODULE_PERIPH_FLASHPAGE_RAW
+/*
+ * @brief   Allocate an aligned buffer for raw writings
+ */
+static char raw_buf[64] __attribute__ ((aligned (FLASHPAGE_RAW_ALIGNMENT)));
+
+static uint32_t getaddr(const char *str)
+{
+    uint32_t addr = strtol(str, NULL, 16);
+
+    return addr;
+}
+#endif
+
 static int getpage(const char *str)
 {
     int page = atoi(str);
@@ -164,6 +178,29 @@ static int cmd_write(int argc, char **argv)
     return 0;
 }
 
+#ifdef MODULE_PERIPH_FLASHPAGE_RAW
+static int cmd_write_raw(int argc, char **argv)
+{
+    uint32_t addr;
+
+    if (argc < 3) {
+        printf("usage: %s <addr> <data>\n", argv[0]);
+        return 1;
+    }
+
+    addr = getaddr(argv[1]);
+
+    /* try to align */
+    memcpy(raw_buf, argv[2], strlen(argv[2]));
+
+    flashpage_write_raw((void*)addr, raw_buf, strlen(raw_buf));
+
+    printf("wrote local data to flash address %#lx of len %u\n",
+           addr, strlen(raw_buf));
+    return 0;
+}
+#endif
+
 static int cmd_erase(int argc, char **argv)
 {
     int page;
@@ -248,6 +285,9 @@ static const shell_command_t shell_commands[] = {
     { "dump_local", "Dump the local page buffer to STDOUT", cmd_dump_local },
     { "read", "Read and output the given page", cmd_read },
     { "write", "Write (ASCII) data to the given page", cmd_write },
+#ifdef MODULE_PERIPH_FLASHPAGE_RAW
+    { "write_raw", "Write (ASCII, max 64B) data to the given address", cmd_write_raw },
+#endif
     { "erase", "Erase the given page", cmd_erase },
     { "edit", "Write bytes to the local page", cmd_edit },
     { "test", "Write and verify test pattern", cmd_test },
-- 
GitLab