Merge branch 'px4dev_new_param' of github.com:PX4/Firmware into px4dev_new_param

This commit is contained in:
Lorenz Meier
2012-08-20 07:51:07 +02:00
5 changed files with 482 additions and 106 deletions
+1 -1
View File
@@ -73,7 +73,7 @@ test_param(int argc, char *argv[])
if ((uint32_t)val != 0xa5a5a5a5)
errx(1, "parameter value mismatch after write");
param_export(NULL, false);
param_export(-1, false);
warnx("parameter test PASS");
+1 -1
View File
@@ -37,6 +37,6 @@
APPNAME = eeprom
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
STACKSIZE = 4096
include $(APPDIR)/mk/app.mk
+81 -3
View File
@@ -41,18 +41,23 @@
#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 <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
@@ -65,13 +70,30 @@
__EXPORT int eeprom_main(int argc, char *argv[]);
static void eeprom_start(void);
static void eeprom_ioctl(unsigned operation);
static void eeprom_save(const char *name);
static void eeprom_load(const char *name);
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 (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'");
@@ -88,24 +110,80 @@ eeprom_start(void)
errx(1, "EEPROM service already started");
/* find the right I2C */
struct i2c_s *i2c = up_i2cinitialize(PX4_I2C_BUS_ONBOARD);
struct i2c_dev_s *i2c = up_i2cinitialize(PX4_I2C_BUS_ONBOARD);
if (i2c == NULL)
errx(1, "failed to locate I2C bus");
/* start the MTD driver */
struct mtd_dev_s *mtd = at24c_initialize(i2c);
if (mtd == NULL)
errx(1, "failed to initialize EEPROM driver");
/* start NXFFS */
ret = nxffs_initialize(mtd);
if (ret < 0)
err(1, "failed to initialize NXFFS");
/* mount the EEPROM */
ret = mount(NULL, "/eeprom", "nxffs", 0, NULL);
if (ret < 0)
err(1, "failed to mount EEPROM");
errx(0, "mounted EEPROM at /eeprom");
}
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)
{
int fd = open(name, O_WRONLY | O_CREAT | O_EXCL);
if (fd < 0)
err(1, "create '%s'", 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)
{
int fd = open(name, O_RDONLY);
if (fd < 0)
err(1, "open '%s'", name);
int result = param_import(fd);
close(fd);
if (result < 0)
unlink(name);
exit(0);
}
File diff suppressed because it is too large Load Diff
+80 -27
View File
@@ -1,7 +1,45 @@
/****************************************************************************
*
* Copyright (C) 2012 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.
*
****************************************************************************/
/**
* @file param.h
*
* Global parameter store.
*
* Note that a number of API members are marked const or pure; these
* assume that the set of parameters cannot change, or that a parameter
* cannot change type or size over its lifetime. If any of these assumptions
* are invalidated, the attributes should be re-evaluated.
*/
#ifndef _SYSTEMLIB_PARAM_PARAM_H
@@ -10,16 +48,18 @@
#include <stdint.h>
#include <stdbool.h>
/** Maximum size of the parameter backing file */
#define PARAM_FILE_MAXSIZE 4096
/**
* Parameter types.
*/
typedef enum param_type_e
{
typedef enum param_type_e {
/* globally-known parameter types */
PARAM_TYPE_INT32 = 0,
PARAM_TYPE_FLOAT,
/* structure parameters; these are expected to be identified by name */
/* structure parameters; size is encoded in the type value */
PARAM_TYPE_STRUCT = 100,
PARAM_TYPE_STRUCT_MAX = 16384 + PARAM_TYPE_STRUCT,
@@ -45,7 +85,22 @@ typedef uintptr_t param_t;
* @param name The canonical name of the parameter being looked up.
* @return A handle to the parameter, or PARAM_INVALID if the parameter does not exist.
*/
__EXPORT param_t param_find(const char *name);
__EXPORT param_t param_find(const char *name) __attribute__((const));
/**
* Return the total number of parameters.
*
* @return The number of parameters.
*/
__EXPORT unsigned param_count(void) __attribute__((const));
/**
* Look up a parameter by index.
*
* @param index An index from 0 to n, where n is param_count()-1.
* @return A handle to the parameter, or PARAM_INVALID if the index is out of range.
*/
__EXPORT param_t param_for_index(unsigned index) __attribute__((const));
/**
* Obtain the name of a parameter.
@@ -53,7 +108,7 @@ __EXPORT param_t param_find(const char *name);
* @param param A handle returned by param_find or passed by param_foreach.
* @return The name assigned to the parameter, or NULL if the handle is invalid.
*/
__EXPORT const char *param_name(param_t param);
__EXPORT const char *param_name(param_t param) __attribute__((const));
/**
* Obtain the type of a parameter.
@@ -61,7 +116,7 @@ __EXPORT const char *param_name(param_t param);
* @param param A handle returned by param_find or passed by param_foreach.
* @return The type assigned to the parameter.
*/
__EXPORT param_type_t param_type(param_t param);
__EXPORT param_type_t param_type(param_t param) __attribute__((const));
/**
* Determine the size of a parameter.
@@ -69,15 +124,15 @@ __EXPORT param_type_t param_type(param_t param);
* @param param A handle returned by param_find or passed by param_foreach.
* @return The size of the parameter's value.
*/
__EXPORT size_t param_size(param_t param);
__EXPORT size_t param_size(param_t param) __attribute__((const));
/**
* Obtain the scalar value of a parameter.
* Copy the value of a parameter.
*
* @param param A handle returned by param_find or passed by param_foreach.
* @param val Where to return the value, assumed to point to suitable storage for the parameter type.
* For structures, a pointer to the structure is returned.
* @return Zero if the parameter's value could be returned as a scalar, nonzero otherwise.
* For structures, a bitwise copy of the structure is performed to this address.
* @return Zero if the parameter's value could be returned, nonzero otherwise.
*/
__EXPORT int param_get(param_t param, void *val);
@@ -89,25 +144,25 @@ __EXPORT int param_get(param_t param, void *val);
* For structures, the pointer is assumed to point to a copy of the structure.
* @return Zero if the parameter's value could be set from a scalar, nonzero otherwise.
*/
__EXPORT int param_set(param_t param, void *val);
__EXPORT int param_set(param_t param, const void *val);
/**
* Export changed parameters to a file.
*
* @param filename The name of the file to export to. If it exists, it will be overwritten.
* @param fd File descriptor to export to.
* @param only_unsaved Only export changed parameters that have not yet been exported.
* @return Zero on success, nonzero on failure.
*/
__EXPORT int param_export(const char *filename, bool only_unsaved);
__EXPORT int param_export(int fd, bool only_unsaved);
/**
* Import parameters from a file, discarding any unrecognized parameters.
*
* @param filename The name of the file to import from.
* @param fd File descriptor to import from. (Currently expected to be a file.)
* @return Zero on success, nonzero if an error occurred during import.
* Note that in the failure case, parameters may be inconsistent.
*/
__EXPORT int param_import(const char *filename);
__EXPORT int param_import(int fd);
/**
* Apply a function to each parameter.
@@ -126,7 +181,7 @@ __EXPORT void param_foreach(void (*func)(void *arg, param_t param), void *arg,
/*
* Macros creating static parameter definitions.
*
* Note that these structures are not known by name; they are
* Note that these structures are not known by name; they are
* collected into a section that is iterated by the parameter
* code.
*/
@@ -137,8 +192,8 @@ __EXPORT void param_foreach(void (*func)(void *arg, param_t param), void *arg,
__attribute__((used, section("__param"))) \
struct param_info_s __param__##_name = { \
.name = #_name, \
.type = PARAM_TYPE_INT32, \
.val.i = _default \
.type = PARAM_TYPE_INT32, \
.val.i = _default \
}
/** define a float parameter */
@@ -147,8 +202,8 @@ __EXPORT void param_foreach(void (*func)(void *arg, param_t param), void *arg,
__attribute__((used, section("__param"))) \
struct param_info_s __param__##_name = { \
.name = #_name, \
.type = PARAM_TYPE_FLOAT, \
.val.f = _default \
.type = PARAM_TYPE_FLOAT, \
.val.f = _default \
}
/** define a parameter that points to a structure */
@@ -157,18 +212,17 @@ __EXPORT void param_foreach(void (*func)(void *arg, param_t param), void *arg,
__attribute__((used, section("__param"))) \
struct param_info_s __param__##_name = { \
.name = #_name, \
.type = PARAM_TYPE_STRUCT + sizeof(_default), \
.val.p = &_default; \
.type = PARAM_TYPE_STRUCT + sizeof(_default), \
.val.p = &_default; \
}
/**
* Parameter value union.
*/
union param_value_u
{
union param_value_u {
void *p;
int32_t i;
float f;
float f;
};
/**
@@ -177,8 +231,7 @@ union param_value_u
* This is normally not used by user code; see the PARAM_DEFINE macros
* instead.
*/
struct param_info_s
{
struct param_info_s {
const char *name;
param_type_t type;
union param_value_u val;