aboutsummaryrefslogtreecommitdiff
path: root/src/systemcmds/eeprom
diff options
context:
space:
mode:
Diffstat (limited to 'src/systemcmds/eeprom')
-rw-r--r--src/systemcmds/eeprom/24xxxx_mtd.c571
-rw-r--r--src/systemcmds/eeprom/eeprom.c265
-rw-r--r--src/systemcmds/eeprom/module.mk39
3 files changed, 875 insertions, 0 deletions
diff --git a/src/systemcmds/eeprom/24xxxx_mtd.c b/src/systemcmds/eeprom/24xxxx_mtd.c
new file mode 100644
index 000000000..e34be44e3
--- /dev/null
+++ b/src/systemcmds/eeprom/24xxxx_mtd.c
@@ -0,0 +1,571 @@
+/************************************************************************************
+ * Driver for 24xxxx-style I2C EEPROMs.
+ *
+ * Adapted from:
+ *
+ * drivers/mtd/at24xx.c
+ * Driver for I2C-based at24cxx EEPROM(at24c32,at24c64,at24c128,at24c256)
+ *
+ * Copyright (C) 2011 Li Zhuoyi. All rights reserved.
+ * Author: Li Zhuoyi <lzyy.cn@gmail.com>
+ * History: 0.1 2011-08-20 initial version
+ *
+ * 2011-11-1 Added support for larger MTD block sizes: Hal Glenn <hglenn@2g-eng.com>
+ *
+ * Derived from drivers/mtd/m25px.c
+ *
+ * Copyright (C) 2009-2011 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************************************/
+
+/************************************************************************************
+ * Included Files
+ ************************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/i2c.h>
+#include <nuttx/mtd.h>
+
+#include "systemlib/perf_counter.h"
+
+/************************************************************************************
+ * Pre-processor Definitions
+ ************************************************************************************/
+
+/*
+ * Configuration is as for the AT24 driver, but the CONFIG_MTD_AT24XX define should be
+ * omitted in order to avoid building the AT24XX driver as well.
+ */
+
+/* As a minimum, the size of the AT24 part and its 7-bit I2C address are required. */
+
+#ifndef CONFIG_AT24XX_SIZE
+# warning "Assuming AT24 size 64"
+# define CONFIG_AT24XX_SIZE 64
+#endif
+#ifndef CONFIG_AT24XX_ADDR
+# warning "Assuming AT24 address of 0x50"
+# define CONFIG_AT24XX_ADDR 0x50
+#endif
+
+/* Get the part configuration based on the size configuration */
+
+#if CONFIG_AT24XX_SIZE == 32
+# define AT24XX_NPAGES 128
+# define AT24XX_PAGESIZE 32
+#elif CONFIG_AT24XX_SIZE == 48
+# define AT24XX_NPAGES 192
+# define AT24XX_PAGESIZE 32
+#elif CONFIG_AT24XX_SIZE == 64
+# define AT24XX_NPAGES 256
+# define AT24XX_PAGESIZE 32
+#elif CONFIG_AT24XX_SIZE == 128
+# define AT24XX_NPAGES 256
+# define AT24XX_PAGESIZE 64
+#elif CONFIG_AT24XX_SIZE == 256
+# define AT24XX_NPAGES 512
+# define AT24XX_PAGESIZE 64
+#endif
+
+/* For applications where a file system is used on the AT24, the tiny page sizes
+ * will result in very inefficient FLASH usage. In such cases, it is better if
+ * blocks are comprised of "clusters" of pages so that the file system block
+ * size is, say, 256 or 512 bytes. In any event, the block size *must* be an
+ * even multiple of the pages.
+ */
+
+#ifndef CONFIG_AT24XX_MTD_BLOCKSIZE
+# warning "Assuming driver block size is the same as the FLASH page size"
+# define CONFIG_AT24XX_MTD_BLOCKSIZE AT24XX_PAGESIZE
+#endif
+
+/* The AT24 does not respond on the bus during write cycles, so we depend on a long
+ * timeout to detect problems. The max program time is typically ~5ms.
+ */
+#ifndef CONFIG_AT24XX_WRITE_TIMEOUT_MS
+# define CONFIG_AT24XX_WRITE_TIMEOUT_MS 20
+#endif
+
+/************************************************************************************
+ * Private Types
+ ************************************************************************************/
+
+/* This type represents the state of the MTD device. The struct mtd_dev_s
+ * must appear at the beginning of the definition so that you can freely
+ * cast between pointers to struct mtd_dev_s and struct at24c_dev_s.
+ */
+
+struct at24c_dev_s {
+ struct mtd_dev_s mtd; /* MTD interface */
+ FAR struct i2c_dev_s *dev; /* Saved I2C interface instance */
+ uint8_t addr; /* I2C address */
+ uint16_t pagesize; /* 32, 63 */
+ uint16_t npages; /* 128, 256, 512, 1024 */
+
+ perf_counter_t perf_reads;
+ perf_counter_t perf_writes;
+ perf_counter_t perf_resets;
+ perf_counter_t perf_read_retries;
+ perf_counter_t perf_read_errors;
+ perf_counter_t perf_write_errors;
+};
+
+/************************************************************************************
+ * Private Function Prototypes
+ ************************************************************************************/
+
+/* MTD driver methods */
+
+static int at24c_erase(FAR struct mtd_dev_s *dev, off_t startblock, size_t nblocks);
+static ssize_t at24c_bread(FAR struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, FAR uint8_t *buf);
+static ssize_t at24c_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, FAR const uint8_t *buf);
+static int at24c_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg);
+
+void at24c_test(void);
+
+/************************************************************************************
+ * Private Data
+ ************************************************************************************/
+
+/* At present, only a single AT24 part is supported. In this case, a statically
+ * allocated state structure may be used.
+ */
+
+static struct at24c_dev_s g_at24c;
+
+/************************************************************************************
+ * Private Functions
+ ************************************************************************************/
+
+static int at24c_eraseall(FAR struct at24c_dev_s *priv)
+{
+ int startblock = 0;
+ uint8_t buf[AT24XX_PAGESIZE + 2];
+
+ struct i2c_msg_s msgv[1] = {
+ {
+ .addr = priv->addr,
+ .flags = 0,
+ .buffer = &buf[0],
+ .length = sizeof(buf),
+ }
+ };
+
+ memset(&buf[2], 0xff, priv->pagesize);
+
+ for (startblock = 0; startblock < priv->npages; startblock++) {
+ uint16_t offset = startblock * priv->pagesize;
+ buf[1] = offset & 0xff;
+ buf[0] = (offset >> 8) & 0xff;
+
+ while (I2C_TRANSFER(priv->dev, &msgv[0], 1) < 0) {
+ fvdbg("erase stall\n");
+ usleep(10000);
+ }
+ }
+
+ return OK;
+}
+
+/************************************************************************************
+ * Name: at24c_erase
+ ************************************************************************************/
+
+static int at24c_erase(FAR struct mtd_dev_s *dev, off_t startblock, size_t nblocks)
+{
+ /* EEprom need not erase */
+
+ return (int)nblocks;
+}
+
+/************************************************************************************
+ * Name: at24c_test
+ ************************************************************************************/
+
+void at24c_test(void)
+{
+ uint8_t buf[CONFIG_AT24XX_MTD_BLOCKSIZE];
+ unsigned count = 0;
+ unsigned errors = 0;
+
+ for (count = 0; count < 10000; count++) {
+ ssize_t result = at24c_bread(&g_at24c.mtd, 0, 1, buf);
+ if (result == ERROR) {
+ if (errors++ > 2) {
+ vdbg("too many errors\n");
+ return;
+ }
+ } else if (result != 1) {
+ vdbg("unexpected %u\n", result);
+ }
+ if ((count % 100) == 0)
+ vdbg("test %u errors %u\n", count, errors);
+ }
+}
+
+/************************************************************************************
+ * Name: at24c_bread
+ ************************************************************************************/
+
+static ssize_t at24c_bread(FAR struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, FAR uint8_t *buffer)
+{
+ FAR struct at24c_dev_s *priv = (FAR struct at24c_dev_s *)dev;
+ size_t blocksleft;
+ uint8_t addr[2];
+ int ret;
+
+ struct i2c_msg_s msgv[2] = {
+ {
+ .addr = priv->addr,
+ .flags = 0,
+ .buffer = &addr[0],
+ .length = sizeof(addr),
+ },
+ {
+ .addr = priv->addr,
+ .flags = I2C_M_READ,
+ .buffer = 0,
+ .length = priv->pagesize,
+ }
+ };
+
+#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
+ startblock *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
+ nblocks *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
+#endif
+ blocksleft = nblocks;
+
+ fvdbg("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
+
+ if (startblock >= priv->npages) {
+ return 0;
+ }
+
+ if (startblock + nblocks > priv->npages) {
+ nblocks = priv->npages - startblock;
+ }
+
+ while (blocksleft-- > 0) {
+ uint16_t offset = startblock * priv->pagesize;
+ unsigned tries = CONFIG_AT24XX_WRITE_TIMEOUT_MS;
+
+ addr[1] = offset & 0xff;
+ addr[0] = (offset >> 8) & 0xff;
+ msgv[1].buffer = buffer;
+
+ for (;;) {
+
+ perf_begin(priv->perf_reads);
+ ret = I2C_TRANSFER(priv->dev, &msgv[0], 2);
+ perf_end(priv->perf_reads);
+
+ if (ret >= 0)
+ break;
+
+ fvdbg("read stall");
+ usleep(1000);
+
+ /* We should normally only be here on the first read after
+ * a write.
+ *
+ * XXX maybe do special first-read handling with optional
+ * bus reset as well?
+ */
+ perf_count(priv->perf_read_retries);
+
+ if (--tries == 0) {
+ perf_count(priv->perf_read_errors);
+ return ERROR;
+ }
+ }
+
+ startblock++;
+ buffer += priv->pagesize;
+ }
+
+#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
+ return nblocks / (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
+#else
+ return nblocks;
+#endif
+}
+
+/************************************************************************************
+ * Name: at24c_bwrite
+ *
+ * Operates on MTD block's and translates to FLASH pages
+ *
+ ************************************************************************************/
+
+static ssize_t at24c_bwrite(FAR struct mtd_dev_s *dev, off_t startblock, size_t nblocks,
+ FAR const uint8_t *buffer)
+{
+ FAR struct at24c_dev_s *priv = (FAR struct at24c_dev_s *)dev;
+ size_t blocksleft;
+ uint8_t buf[AT24XX_PAGESIZE + 2];
+ int ret;
+
+ struct i2c_msg_s msgv[1] = {
+ {
+ .addr = priv->addr,
+ .flags = 0,
+ .buffer = &buf[0],
+ .length = sizeof(buf),
+ }
+ };
+
+#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
+ startblock *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
+ nblocks *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
+#endif
+ blocksleft = nblocks;
+
+ if (startblock >= priv->npages) {
+ return 0;
+ }
+
+ if (startblock + nblocks > priv->npages) {
+ nblocks = priv->npages - startblock;
+ }
+
+ fvdbg("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
+
+ while (blocksleft-- > 0) {
+ uint16_t offset = startblock * priv->pagesize;
+ unsigned tries = CONFIG_AT24XX_WRITE_TIMEOUT_MS;
+
+ buf[1] = offset & 0xff;
+ buf[0] = (offset >> 8) & 0xff;
+ memcpy(&buf[2], buffer, priv->pagesize);
+
+ for (;;) {
+
+ perf_begin(priv->perf_writes);
+ ret = I2C_TRANSFER(priv->dev, &msgv[0], 1);
+ perf_end(priv->perf_writes);
+
+ if (ret >= 0)
+ break;
+
+ fvdbg("write stall");
+ usleep(1000);
+
+ /* We expect to see a number of retries per write cycle as we
+ * poll for write completion.
+ */
+ if (--tries == 0) {
+ perf_count(priv->perf_write_errors);
+ return ERROR;
+ }
+ }
+
+ startblock++;
+ buffer += priv->pagesize;
+ }
+
+#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
+ return nblocks / (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
+#else
+ return nblocks;
+#endif
+}
+
+/************************************************************************************
+ * Name: at24c_ioctl
+ ************************************************************************************/
+
+static int at24c_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
+{
+ FAR struct at24c_dev_s *priv = (FAR struct at24c_dev_s *)dev;
+ int ret = -EINVAL; /* Assume good command with bad parameters */
+
+ fvdbg("cmd: %d \n", cmd);
+
+ switch (cmd) {
+ case MTDIOC_GEOMETRY: {
+ FAR struct mtd_geometry_s *geo = (FAR struct mtd_geometry_s *)((uintptr_t)arg);
+
+ if (geo) {
+ /* Populate the geometry structure with information need to know
+ * the capacity and how to access the device.
+ *
+ * NOTE: that the device is treated as though it where just an array
+ * of fixed size blocks. That is most likely not true, but the client
+ * will expect the device logic to do whatever is necessary to make it
+ * appear so.
+ *
+ * blocksize:
+ * May be user defined. The block size for the at24XX devices may be
+ * larger than the page size in order to better support file systems.
+ * The read and write functions translate BLOCKS to pages for the
+ * small flash devices
+ * erasesize:
+ * It has to be at least as big as the blocksize, bigger serves no
+ * purpose.
+ * neraseblocks
+ * Note that the device size is in kilobits and must be scaled by
+ * 1024 / 8
+ */
+
+#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
+ geo->blocksize = CONFIG_AT24XX_MTD_BLOCKSIZE;
+ geo->erasesize = CONFIG_AT24XX_MTD_BLOCKSIZE;
+ geo->neraseblocks = (CONFIG_AT24XX_SIZE * 1024 / 8) / CONFIG_AT24XX_MTD_BLOCKSIZE;
+#else
+ geo->blocksize = priv->pagesize;
+ geo->erasesize = priv->pagesize;
+ geo->neraseblocks = priv->npages;
+#endif
+ ret = OK;
+
+ fvdbg("blocksize: %d erasesize: %d neraseblocks: %d\n",
+ geo->blocksize, geo->erasesize, geo->neraseblocks);
+ }
+ }
+ break;
+
+ case MTDIOC_BULKERASE:
+ ret = at24c_eraseall(priv);
+ break;
+
+ case MTDIOC_XIPBASE:
+ default:
+ ret = -ENOTTY; /* Bad command */
+ break;
+ }
+
+ return ret;
+}
+
+/************************************************************************************
+ * Public Functions
+ ************************************************************************************/
+
+/************************************************************************************
+ * Name: at24c_initialize
+ *
+ * Description:
+ * Create an initialize MTD device instance. MTD devices are not registered
+ * in the file system, but are created as instances that can be bound to
+ * other functions (such as a block or character driver front end).
+ *
+ ************************************************************************************/
+
+FAR struct mtd_dev_s *at24c_initialize(FAR struct i2c_dev_s *dev) {
+ FAR struct at24c_dev_s *priv;
+
+ fvdbg("dev: %p\n", dev);
+
+ /* Allocate a state structure (we allocate the structure instead of using
+ * a fixed, static allocation so that we can handle multiple FLASH devices.
+ * The current implementation would handle only one FLASH part per I2C
+ * device (only because of the SPIDEV_FLASH definition) and so would have
+ * to be extended to handle multiple FLASH parts on the same I2C bus.
+ */
+
+ priv = &g_at24c;
+
+ if (priv) {
+ /* Initialize the allocated structure */
+
+ priv->addr = CONFIG_AT24XX_ADDR;
+ priv->pagesize = AT24XX_PAGESIZE;
+ priv->npages = AT24XX_NPAGES;
+
+ priv->mtd.erase = at24c_erase;
+ priv->mtd.bread = at24c_bread;
+ priv->mtd.bwrite = at24c_bwrite;
+ priv->mtd.ioctl = at24c_ioctl;
+ priv->dev = dev;
+
+ priv->perf_reads = perf_alloc(PC_ELAPSED, "EEPROM read");
+ priv->perf_writes = perf_alloc(PC_ELAPSED, "EEPROM write");
+ priv->perf_resets = perf_alloc(PC_COUNT, "EEPROM reset");
+ priv->perf_read_retries = perf_alloc(PC_COUNT, "EEPROM read retries");
+ priv->perf_read_errors = perf_alloc(PC_COUNT, "EEPROM read errors");
+ priv->perf_write_errors = perf_alloc(PC_COUNT, "EEPROM write errors");
+ }
+
+ /* attempt to read to validate device is present */
+ unsigned char buf[5];
+ uint8_t addrbuf[2] = {0, 0};
+
+ struct i2c_msg_s msgv[2] = {
+ {
+ .addr = priv->addr,
+ .flags = 0,
+ .buffer = &addrbuf[0],
+ .length = sizeof(addrbuf),
+ },
+ {
+ .addr = priv->addr,
+ .flags = I2C_M_READ,
+ .buffer = &buf[0],
+ .length = sizeof(buf),
+ }
+ };
+
+ perf_begin(priv->perf_reads);
+ int ret = I2C_TRANSFER(priv->dev, &msgv[0], 2);
+ perf_end(priv->perf_reads);
+
+ if (ret < 0) {
+ return NULL;
+ }
+
+ /* Return the implementation-specific state structure as the MTD device */
+
+ fvdbg("Return %p\n", priv);
+ return (FAR struct mtd_dev_s *)priv;
+}
+
+/*
+ * XXX: debug hackery
+ */
+int at24c_nuke(void)
+{
+ return at24c_eraseall(&g_at24c);
+}
diff --git a/src/systemcmds/eeprom/eeprom.c b/src/systemcmds/eeprom/eeprom.c
new file mode 100644
index 000000000..49da51358
--- /dev/null
+++ b/src/systemcmds/eeprom/eeprom.c
@@ -0,0 +1,265 @@
+/****************************************************************************
+ *
+ * Copyright (C) 2012 PX4 Development Team. All rights reserved.
+ * Author: Lorenz Meier <lm@inf.ethz.ch>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/**
+ * @file eeprom.c
+ *
+ * EEPROM service and utility app.
+ */
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mount.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+
+#include <nuttx/i2c.h>
+#include <nuttx/mtd.h>
+#include <nuttx/fs/nxffs.h>
+#include <nuttx/fs/ioctl.h>
+
+#include <arch/board/board.h>
+
+#include "systemlib/systemlib.h"
+#include "systemlib/param/param.h"
+#include "systemlib/err.h"
+
+#ifndef PX4_I2C_BUS_ONBOARD
+# error PX4_I2C_BUS_ONBOARD not defined, cannot locate onboard EEPROM
+#endif
+
+__EXPORT int eeprom_main(int argc, char *argv[]);
+
+static void eeprom_attach(void);
+static void eeprom_start(void);
+static void eeprom_erase(void);
+static void eeprom_ioctl(unsigned operation);
+static void eeprom_save(const char *name);
+static void eeprom_load(const char *name);
+static void eeprom_test(void);
+
+static bool attached = false;
+static bool started = false;
+static struct mtd_dev_s *eeprom_mtd;
+
+int eeprom_main(int argc, char *argv[])
+{
+ if (argc >= 2) {
+ if (!strcmp(argv[1], "start"))
+ eeprom_start();
+
+ if (!strcmp(argv[1], "save_param"))
+ eeprom_save(argv[2]);
+
+ if (!strcmp(argv[1], "load_param"))
+ eeprom_load(argv[2]);
+
+ if (!strcmp(argv[1], "erase"))
+ eeprom_erase();
+
+ if (!strcmp(argv[1], "test"))
+ eeprom_test();
+
+ if (0) { /* these actually require a file on the filesystem... */
+
+ if (!strcmp(argv[1], "reformat"))
+ eeprom_ioctl(FIOC_REFORMAT);
+
+ if (!strcmp(argv[1], "repack"))
+ eeprom_ioctl(FIOC_OPTIMIZE);
+ }
+ }
+
+ errx(1, "expected a command, try 'start'\n\t'save_param /eeprom/parameters'\n\t'load_param /eeprom/parameters'\n\t'erase'\n");
+}
+
+
+static void
+eeprom_attach(void)
+{
+ /* find the right I2C */
+ struct i2c_dev_s *i2c = up_i2cinitialize(PX4_I2C_BUS_ONBOARD);
+ /* this resets the I2C bus, set correct bus speed again */
+ I2C_SETFREQUENCY(i2c, 400000);
+
+ if (i2c == NULL)
+ errx(1, "failed to locate I2C bus");
+
+ /* start the MTD driver, attempt 5 times */
+ for (int i = 0; i < 5; i++) {
+ eeprom_mtd = at24c_initialize(i2c);
+ if (eeprom_mtd) {
+ /* abort on first valid result */
+ if (i > 0) {
+ warnx("warning: EEPROM needed %d attempts to attach", i+1);
+ }
+ break;
+ }
+ }
+
+ /* if last attempt is still unsuccessful, abort */
+ if (eeprom_mtd == NULL)
+ errx(1, "failed to initialize EEPROM driver");
+
+ attached = true;
+}
+
+static void
+eeprom_start(void)
+{
+ int ret;
+
+ if (started)
+ errx(1, "EEPROM already mounted");
+
+ if (!attached)
+ eeprom_attach();
+
+ /* start NXFFS */
+ ret = nxffs_initialize(eeprom_mtd);
+
+ if (ret < 0)
+ errx(1, "failed to initialize NXFFS - erase EEPROM to reformat");
+
+ /* mount the EEPROM */
+ ret = mount(NULL, "/eeprom", "nxffs", 0, NULL);
+
+ if (ret < 0)
+ errx(1, "failed to mount /eeprom - erase EEPROM to reformat");
+
+ started = true;
+ warnx("mounted EEPROM at /eeprom");
+ exit(0);
+}
+
+extern int at24c_nuke(void);
+
+static void
+eeprom_erase(void)
+{
+ if (!attached)
+ eeprom_attach();
+
+ if (at24c_nuke())
+ errx(1, "erase failed");
+
+ errx(0, "erase done, reboot now");
+}
+
+static void
+eeprom_ioctl(unsigned operation)
+{
+ int fd;
+
+ fd = open("/eeprom/.", 0);
+
+ if (fd < 0)
+ err(1, "open /eeprom");
+
+ if (ioctl(fd, operation, 0) < 0)
+ err(1, "ioctl");
+
+ exit(0);
+}
+
+static void
+eeprom_save(const char *name)
+{
+ if (!started)
+ errx(1, "must be started first");
+
+ if (!name)
+ err(1, "missing argument for device name, try '/eeprom/parameters'");
+
+ warnx("WARNING: 'eeprom save_param' deprecated - use 'param save' instead");
+
+ /* delete the file in case it exists */
+ unlink(name);
+
+ /* create the file */
+ int fd = open(name, O_WRONLY | O_CREAT | O_EXCL);
+
+ if (fd < 0)
+ err(1, "opening '%s' failed", name);
+
+ int result = param_export(fd, false);
+ close(fd);
+
+ if (result < 0) {
+ unlink(name);
+ errx(1, "error exporting to '%s'", name);
+ }
+
+ exit(0);
+}
+
+static void
+eeprom_load(const char *name)
+{
+ if (!started)
+ errx(1, "must be started first");
+
+ if (!name)
+ err(1, "missing argument for device name, try '/eeprom/parameters'");
+
+ warnx("WARNING: 'eeprom load_param' deprecated - use 'param load' instead");
+
+ int fd = open(name, O_RDONLY);
+
+ if (fd < 0)
+ err(1, "open '%s'", name);
+
+ int result = param_load(fd);
+ close(fd);
+
+ if (result < 0)
+ errx(1, "error importing from '%s'", name);
+
+ exit(0);
+}
+
+extern void at24c_test(void);
+
+static void
+eeprom_test(void)
+{
+ at24c_test();
+ exit(0);
+}
diff --git a/src/systemcmds/eeprom/module.mk b/src/systemcmds/eeprom/module.mk
new file mode 100644
index 000000000..07f3945be
--- /dev/null
+++ b/src/systemcmds/eeprom/module.mk
@@ -0,0 +1,39 @@
+############################################################################
+#
+# Copyright (c) 2012, 2013 PX4 Development Team. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name PX4 nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################################
+
+#
+# EEPROM file system driver
+#
+
+MODULE_COMMAND = eeprom
+SRCS = 24xxxx_mtd.c eeprom.c