format src/systemcmds/mtd

This commit is contained in:
Daniel Agar 2015-09-05 12:21:11 -04:00
parent 7527026b89
commit edf010c6a9
2 changed files with 102 additions and 50 deletions

View File

@ -239,11 +239,13 @@ void at24c_test(void)
for (count = 0; count < 10000; count++) { for (count = 0; count < 10000; count++) {
ssize_t result = at24c_bread(&g_at24c.mtd, 0, 1, buf); ssize_t result = at24c_bread(&g_at24c.mtd, 0, 1, buf);
if (result == ERROR) { if (result == ERROR) {
if (errors++ > 2) { if (errors++ > 2) {
vdbg("too many errors\n"); vdbg("too many errors\n");
return; return;
} }
} else if (result != 1) { } else if (result != 1) {
vdbg("unexpected %u\n", result); vdbg("unexpected %u\n", result);
} }
@ -311,8 +313,9 @@ static ssize_t at24c_bread(FAR struct mtd_dev_s *dev, off_t startblock,
ret = I2C_TRANSFER(priv->dev, &msgv[0], 2); ret = I2C_TRANSFER(priv->dev, &msgv[0], 2);
perf_end(priv->perf_transfers); perf_end(priv->perf_transfers);
if (ret >= 0) if (ret >= 0) {
break; break;
}
fvdbg("read stall"); fvdbg("read stall");
usleep(1000); usleep(1000);
@ -396,8 +399,9 @@ static ssize_t at24c_bwrite(FAR struct mtd_dev_s *dev, off_t startblock, size_t
ret = I2C_TRANSFER(priv->dev, &msgv[0], 1); ret = I2C_TRANSFER(priv->dev, &msgv[0], 1);
perf_end(priv->perf_transfers); perf_end(priv->perf_transfers);
if (ret >= 0) if (ret >= 0) {
break; break;
}
fvdbg("write stall"); fvdbg("write stall");
usleep(1000); usleep(1000);
@ -503,7 +507,8 @@ static int at24c_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
* *
************************************************************************************/ ************************************************************************************/
FAR struct mtd_dev_s *at24c_initialize(FAR struct i2c_dev_s *dev) { FAR struct mtd_dev_s *at24c_initialize(FAR struct i2c_dev_s *dev)
{
FAR struct at24c_dev_s *priv; FAR struct at24c_dev_s *priv;
fvdbg("dev: %p\n", dev); fvdbg("dev: %p\n", dev);

View File

@ -92,8 +92,8 @@ static void mtd_erase(char *partition_names[], unsigned n_partitions);
static void mtd_readtest(char *partition_names[], unsigned n_partitions); static void mtd_readtest(char *partition_names[], unsigned n_partitions);
static void mtd_rwtest(char *partition_names[], unsigned n_partitions); static void mtd_rwtest(char *partition_names[], unsigned n_partitions);
static void mtd_print_info(void); static void mtd_print_info(void);
static int mtd_get_geometry(unsigned long *blocksize, unsigned long *erasesize, unsigned long *neraseblocks, static int mtd_get_geometry(unsigned long *blocksize, unsigned long *erasesize, unsigned long *neraseblocks,
unsigned *blkpererase, unsigned *nblocks, unsigned *partsize, unsigned n_partitions); unsigned *blkpererase, unsigned *nblocks, unsigned *partsize, unsigned n_partitions);
static bool attached = false; static bool attached = false;
static bool started = false; static bool started = false;
@ -107,9 +107,10 @@ static const int n_partitions_default = sizeof(partition_names_default) / sizeof
static void static void
mtd_status(void) mtd_status(void)
{ {
if (!attached) if (!attached) {
errx(1, "MTD driver not started"); errx(1, "MTD driver not started");
}
mtd_print_info(); mtd_print_info();
exit(0); exit(0);
} }
@ -122,40 +123,46 @@ int mtd_main(int argc, char *argv[])
/* start mapping according to user request */ /* start mapping according to user request */
if (argc >= 3) { if (argc >= 3) {
mtd_start(argv + 2, argc - 2); mtd_start(argv + 2, argc - 2);
} else { } else {
mtd_start(partition_names_default, n_partitions_default); mtd_start(partition_names_default, n_partitions_default);
} }
} }
if (!strcmp(argv[1], "test")) if (!strcmp(argv[1], "test")) {
mtd_test(); mtd_test();
}
if (!strcmp(argv[1], "readtest")) { if (!strcmp(argv[1], "readtest")) {
if (argc >= 3) { if (argc >= 3) {
mtd_readtest(argv + 2, argc - 2); mtd_readtest(argv + 2, argc - 2);
} else { } else {
mtd_readtest(partition_names_default, n_partitions_default); mtd_readtest(partition_names_default, n_partitions_default);
} }
} }
if (!strcmp(argv[1], "rwtest")) { if (!strcmp(argv[1], "rwtest")) {
if (argc >= 3) { if (argc >= 3) {
mtd_rwtest(argv + 2, argc - 2); mtd_rwtest(argv + 2, argc - 2);
} else { } else {
mtd_rwtest(partition_names_default, n_partitions_default); mtd_rwtest(partition_names_default, n_partitions_default);
} }
} }
if (!strcmp(argv[1], "status")) if (!strcmp(argv[1], "status")) {
mtd_status(); mtd_status();
}
if (!strcmp(argv[1], "erase")) { if (!strcmp(argv[1], "erase")) {
if (argc >= 3) { if (argc >= 3) {
mtd_erase(argv + 2, argc - 2); mtd_erase(argv + 2, argc - 2);
} else { } else {
mtd_erase(partition_names_default, n_partitions_default); mtd_erase(partition_names_default, n_partitions_default);
} }
} }
} }
errx(1, "expected a command, try 'start', 'erase', 'status', 'readtest', 'rwtest' or 'test'"); errx(1, "expected a command, try 'start', 'erase', 'status', 'readtest', 'rwtest' or 'test'");
@ -163,7 +170,7 @@ int mtd_main(int argc, char *argv[])
struct mtd_dev_s *ramtron_initialize(FAR struct spi_dev_s *dev); struct mtd_dev_s *ramtron_initialize(FAR struct spi_dev_s *dev);
struct mtd_dev_s *mtd_partition(FAR struct mtd_dev_s *mtd, struct mtd_dev_s *mtd_partition(FAR struct mtd_dev_s *mtd,
off_t firstblock, off_t nblocks); off_t firstblock, off_t nblocks);
#ifdef CONFIG_MTD_RAMTRON #ifdef CONFIG_MTD_RAMTRON
static void static void
@ -181,8 +188,9 @@ ramtron_attach(void)
SPI_SETMODE(spi, SPIDEV_MODE3); SPI_SETMODE(spi, SPIDEV_MODE3);
SPI_SELECT(spi, SPIDEV_FLASH, false); SPI_SELECT(spi, SPIDEV_FLASH, false);
if (spi == NULL) if (spi == NULL) {
errx(1, "failed to locate spi bus"); errx(1, "failed to locate spi bus");
}
/* start the RAMTRON driver, attempt 5 times */ /* start the RAMTRON driver, attempt 5 times */
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
@ -199,10 +207,12 @@ ramtron_attach(void)
} }
/* if last attempt is still unsuccessful, abort */ /* if last attempt is still unsuccessful, abort */
if (mtd_dev == NULL) if (mtd_dev == NULL) {
errx(1, "failed to initialize mtd driver"); errx(1, "failed to initialize mtd driver");
}
int ret = mtd_dev->ioctl(mtd_dev, MTDIOC_SETSPEED, (unsigned long)10 * 1000 * 1000);
int ret = mtd_dev->ioctl(mtd_dev, MTDIOC_SETSPEED, (unsigned long)10*1000*1000);
if (ret != OK) { if (ret != OK) {
// FIXME: From the previous warnx call, it looked like this should have been an errx instead. Tried // FIXME: From the previous warnx call, it looked like this should have been an errx instead. Tried
// that but setting the bug speed does fail all the time. Which was then exiting and the board would // that but setting the bug speed does fail all the time. Which was then exiting and the board would
@ -222,24 +232,28 @@ at24xxx_attach(void)
/* this resets the I2C bus, set correct bus speed again */ /* this resets the I2C bus, set correct bus speed again */
I2C_SETFREQUENCY(i2c, 400000); I2C_SETFREQUENCY(i2c, 400000);
if (i2c == NULL) if (i2c == NULL) {
errx(1, "failed to locate I2C bus"); errx(1, "failed to locate I2C bus");
}
/* start the MTD driver, attempt 5 times */ /* start the MTD driver, attempt 5 times */
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
mtd_dev = at24c_initialize(i2c); mtd_dev = at24c_initialize(i2c);
if (mtd_dev) { if (mtd_dev) {
/* abort on first valid result */ /* abort on first valid result */
if (i > 0) { if (i > 0) {
warnx("warning: EEPROM needed %d attempts to attach", i+1); warnx("warning: EEPROM needed %d attempts to attach", i + 1);
} }
break; break;
} }
} }
/* if last attempt is still unsuccessful, abort */ /* if last attempt is still unsuccessful, abort */
if (mtd_dev == NULL) if (mtd_dev == NULL) {
errx(1, "failed to initialize EEPROM driver"); errx(1, "failed to initialize EEPROM driver");
}
attached = true; attached = true;
} }
@ -250,15 +264,16 @@ mtd_start(char *partition_names[], unsigned n_partitions)
{ {
int ret; int ret;
if (started) if (started) {
errx(1, "mtd already mounted"); errx(1, "mtd already mounted");
}
if (!attached) { if (!attached) {
#ifdef CONFIG_ARCH_BOARD_PX4FMU_V1 #ifdef CONFIG_ARCH_BOARD_PX4FMU_V1
at24xxx_attach(); at24xxx_attach();
#else #else
ramtron_attach(); ramtron_attach();
#endif #endif
} }
if (!mtd_dev) { if (!mtd_dev) {
@ -270,8 +285,10 @@ mtd_start(char *partition_names[], unsigned n_partitions)
unsigned blkpererase, nblocks, partsize; unsigned blkpererase, nblocks, partsize;
ret = mtd_get_geometry(&blocksize, &erasesize, &neraseblocks, &blkpererase, &nblocks, &partsize, n_partitions); ret = mtd_get_geometry(&blocksize, &erasesize, &neraseblocks, &blkpererase, &nblocks, &partsize, n_partitions);
if (ret)
if (ret) {
exit(3); exit(3);
}
/* Now create MTD FLASH partitions */ /* Now create MTD FLASH partitions */
@ -320,10 +337,10 @@ mtd_start(char *partition_names[], unsigned n_partitions)
exit(0); exit(0);
} }
int mtd_get_geometry(unsigned long *blocksize, unsigned long *erasesize, unsigned long *neraseblocks, int mtd_get_geometry(unsigned long *blocksize, unsigned long *erasesize, unsigned long *neraseblocks,
unsigned *blkpererase, unsigned *nblocks, unsigned *partsize, unsigned n_partitions) unsigned *blkpererase, unsigned *nblocks, unsigned *partsize, unsigned n_partitions)
{ {
/* Get the geometry of the FLASH device */ /* Get the geometry of the FLASH device */
FAR struct mtd_geometry_s geo; FAR struct mtd_geometry_s geo;
@ -358,24 +375,31 @@ static ssize_t mtd_get_partition_size(void)
unsigned long blocksize, erasesize, neraseblocks; unsigned long blocksize, erasesize, neraseblocks;
unsigned blkpererase, nblocks, partsize = 0; unsigned blkpererase, nblocks, partsize = 0;
int ret = mtd_get_geometry(&blocksize, &erasesize, &neraseblocks, &blkpererase, &nblocks, &partsize, n_partitions_current); int ret = mtd_get_geometry(&blocksize, &erasesize, &neraseblocks, &blkpererase, &nblocks, &partsize,
n_partitions_current);
if (ret != OK) { if (ret != OK) {
errx(1, "Failed to get geometry"); errx(1, "Failed to get geometry");
} }
return partsize; return partsize;
} }
void mtd_print_info(void) void mtd_print_info(void)
{ {
if (!attached) if (!attached) {
exit(1); exit(1);
}
unsigned long blocksize, erasesize, neraseblocks; unsigned long blocksize, erasesize, neraseblocks;
unsigned blkpererase, nblocks, partsize; unsigned blkpererase, nblocks, partsize;
int ret = mtd_get_geometry(&blocksize, &erasesize, &neraseblocks, &blkpererase, &nblocks, &partsize, n_partitions_current); int ret = mtd_get_geometry(&blocksize, &erasesize, &neraseblocks, &blkpererase, &nblocks, &partsize,
if (ret) n_partitions_current);
if (ret) {
exit(3); exit(3);
}
warnx("Flash Geometry:"); warnx("Flash Geometry:");
@ -400,19 +424,24 @@ mtd_erase(char *partition_names[], unsigned n_partitions)
{ {
uint8_t v[64]; uint8_t v[64];
memset(v, 0xFF, sizeof(v)); memset(v, 0xFF, sizeof(v));
for (uint8_t i = 0; i < n_partitions; i++) { for (uint8_t i = 0; i < n_partitions; i++) {
uint32_t count = 0; uint32_t count = 0;
printf("Erasing %s\n", partition_names[i]); printf("Erasing %s\n", partition_names[i]);
int fd = open(partition_names[i], O_WRONLY); int fd = open(partition_names[i], O_WRONLY);
if (fd == -1) { if (fd == -1) {
errx(1, "Failed to open partition"); errx(1, "Failed to open partition");
} }
while (write(fd, v, sizeof(v)) == sizeof(v)) { while (write(fd, v, sizeof(v)) == sizeof(v)) {
count += sizeof(v); count += sizeof(v);
} }
printf("Erased %lu bytes\n", (unsigned long)count); printf("Erased %lu bytes\n", (unsigned long)count);
close(fd); close(fd);
} }
exit(0); exit(0);
} }
@ -427,21 +456,27 @@ mtd_readtest(char *partition_names[], unsigned n_partitions)
ssize_t expected_size = mtd_get_partition_size(); ssize_t expected_size = mtd_get_partition_size();
uint8_t v[128]; uint8_t v[128];
for (uint8_t i = 0; i < n_partitions; i++) { for (uint8_t i = 0; i < n_partitions; i++) {
ssize_t count = 0; ssize_t count = 0;
printf("reading %s expecting %u bytes\n", partition_names[i], expected_size); printf("reading %s expecting %u bytes\n", partition_names[i], expected_size);
int fd = open(partition_names[i], O_RDONLY); int fd = open(partition_names[i], O_RDONLY);
if (fd == -1) { if (fd == -1) {
errx(1, "Failed to open partition"); errx(1, "Failed to open partition");
} }
while (read(fd, v, sizeof(v)) == sizeof(v)) { while (read(fd, v, sizeof(v)) == sizeof(v)) {
count += sizeof(v); count += sizeof(v);
} }
if (count != expected_size) { if (count != expected_size) {
errx(1,"Failed to read partition - got %u/%u bytes", count, expected_size); errx(1, "Failed to read partition - got %u/%u bytes", count, expected_size);
} }
close(fd); close(fd);
} }
printf("readtest OK\n"); printf("readtest OK\n");
exit(0); exit(0);
} }
@ -458,38 +493,50 @@ mtd_rwtest(char *partition_names[], unsigned n_partitions)
ssize_t expected_size = mtd_get_partition_size(); ssize_t expected_size = mtd_get_partition_size();
uint8_t v[128], v2[128]; uint8_t v[128], v2[128];
for (uint8_t i = 0; i < n_partitions; i++) { for (uint8_t i = 0; i < n_partitions; i++) {
ssize_t count = 0; ssize_t count = 0;
off_t offset = 0; off_t offset = 0;
printf("rwtest %s testing %u bytes\n", partition_names[i], expected_size); printf("rwtest %s testing %u bytes\n", partition_names[i], expected_size);
int fd = open(partition_names[i], O_RDWR); int fd = open(partition_names[i], O_RDWR);
if (fd == -1) { if (fd == -1) {
errx(1, "Failed to open partition"); errx(1, "Failed to open partition");
} }
while (read(fd, v, sizeof(v)) == sizeof(v)) { while (read(fd, v, sizeof(v)) == sizeof(v)) {
count += sizeof(v); count += sizeof(v);
if (lseek(fd, offset, SEEK_SET) != offset) {
errx(1, "seek failed"); if (lseek(fd, offset, SEEK_SET) != offset) {
} errx(1, "seek failed");
if (write(fd, v, sizeof(v)) != sizeof(v)) { }
errx(1, "write failed");
} if (write(fd, v, sizeof(v)) != sizeof(v)) {
if (lseek(fd, offset, SEEK_SET) != offset) { errx(1, "write failed");
errx(1, "seek failed"); }
}
if (read(fd, v2, sizeof(v2)) != sizeof(v2)) { if (lseek(fd, offset, SEEK_SET) != offset) {
errx(1, "read failed"); errx(1, "seek failed");
} }
if (memcmp(v, v2, sizeof(v2)) != 0) {
errx(1, "memcmp failed"); if (read(fd, v2, sizeof(v2)) != sizeof(v2)) {
} errx(1, "read failed");
offset += sizeof(v); }
if (memcmp(v, v2, sizeof(v2)) != 0) {
errx(1, "memcmp failed");
}
offset += sizeof(v);
} }
if (count != expected_size) { if (count != expected_size) {
errx(1,"Failed to read partition - got %u/%u bytes", count, expected_size); errx(1, "Failed to read partition - got %u/%u bytes", count, expected_size);
} }
close(fd); close(fd);
} }
printf("rwtest OK\n"); printf("rwtest OK\n");
exit(0); exit(0);
} }