Skip to content
Snippets Groups Projects
Commit ae6028c7 authored by Alexandre Abadie's avatar Alexandre Abadie
Browse files

tests/periph_eeprom: refactor test application + fix bounds

parent dfe03562
Branches
No related tags found
No related merge requests found
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include "shell.h" #include "shell.h"
...@@ -65,7 +66,7 @@ static int cmd_read(int argc, char **argv) ...@@ -65,7 +66,7 @@ static int cmd_read(int argc, char **argv)
return 1; return 1;
} }
if (pos + count >= EEPROM_SIZE) { if (pos + count > EEPROM_SIZE) {
puts("Failed: cannot read out of eeprom bounds"); puts("Failed: cannot read out of eeprom bounds");
return 1; return 1;
} }
...@@ -78,6 +79,26 @@ static int cmd_read(int argc, char **argv) ...@@ -78,6 +79,26 @@ static int cmd_read(int argc, char **argv)
return 0; return 0;
} }
static int cmd_read_byte(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s <pos>\n", argv[0]);
return 1;
}
uint32_t pos = atoi(argv[1]);
if (pos >= EEPROM_SIZE) {
puts("Failed: cannot read out of eeprom bounds");
return 1;
}
uint8_t byte = eeprom_read_byte(pos);
printf("Byte read from EEPROM: 0x%02X (%c)\n", byte, byte);
return 0;
}
static int cmd_write(int argc, char **argv) static int cmd_write(int argc, char **argv)
{ {
if (argc < 3) { if (argc < 3) {
...@@ -87,7 +108,7 @@ static int cmd_write(int argc, char **argv) ...@@ -87,7 +108,7 @@ static int cmd_write(int argc, char **argv)
uint32_t pos = atoi(argv[1]); uint32_t pos = atoi(argv[1]);
if (pos + strlen(argv[2]) >= EEPROM_SIZE) { if (pos + strlen(argv[2]) > EEPROM_SIZE) {
puts("Failed: cannot write out of eeprom bounds"); puts("Failed: cannot write out of eeprom bounds");
return 1; return 1;
} }
...@@ -98,10 +119,75 @@ static int cmd_write(int argc, char **argv) ...@@ -98,10 +119,75 @@ static int cmd_write(int argc, char **argv)
return 0; return 0;
} }
static int cmd_write_byte(int argc, char **argv)
{
if (argc < 3) {
printf("usage: %s <pos> <byte>\n", argv[0]);
return 1;
}
uint32_t pos = atoi(argv[1]);
if (pos >= EEPROM_SIZE) {
puts("Failed: cannot write out of eeprom bounds");
return 1;
}
eeprom_write_byte(pos, *(uint8_t *)argv[2]);
printf("Byte written to EEPROM\n");
return 0;
}
static int cmd_test(int argc, char **argv)
{
(void)argv;
if (argc != 1) {
puts("FAILED");
return 1;
}
const char *test = "test";
/* test read/write function */
/* read/write from beginning of EEPROM */
size_t ret = eeprom_write(0, (uint8_t *)test, 4);
assert(ret == 4);
char *expected[4];
ret = eeprom_read(0, (uint8_t *)expected, 4);
assert(strncmp((const char *)expected, (const char *)test, 4) == 0);
assert(ret == 4);
/* read/write at end of EEPROM */
ret = eeprom_write(EEPROM_SIZE - 4, (uint8_t *)test, 4);
assert(ret == 4);
ret = eeprom_read(EEPROM_SIZE - 4, (uint8_t *)expected, 4);
assert(strncmp((const char *)expected, test, 4) == 0);
assert(ret == 4);
/* read/write single byte */
eeprom_write_byte(0, 'A');
assert(eeprom_read_byte(0) == 'A');
eeprom_write_byte(EEPROM_SIZE - 1, 'A');
assert(eeprom_read_byte(EEPROM_SIZE - 1) == 'A');
eeprom_write_byte(EEPROM_SIZE / 2, 'A');
assert(eeprom_read_byte(EEPROM_SIZE / 2) == 'A');
puts("SUCCESS");
return 0;
}
static const shell_command_t shell_commands[] = { static const shell_command_t shell_commands[] = {
{ "info", "Print information about eeprom", cmd_info }, { "info", "Print information about eeprom", cmd_info },
{ "read", "Read bytes from eeprom", cmd_read }, { "read", "Read bytes from eeprom", cmd_read },
{ "write", "Write bytes to eeprom", cmd_write}, { "write", "Write bytes to eeprom", cmd_write },
{ "read_byte", "Read a single byte from eeprom", cmd_read_byte },
{ "write_byte", "Write a single byte to eeprom", cmd_write_byte },
{ "test", "Test the EEPROM implementation", cmd_test },
{ NULL, NULL, NULL } { NULL, NULL, NULL }
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment