sensor: add device config helpers

Define some configuration structures and macros that can be used in
device configuration.  These usage scenarios are as follows:

 * device drivers will use SENSOR_DECLARE_* macros in their device
   configuration structures and SENSOR_GET_* macros in the init
   functions;

 * application code will use SENSOR_TRIG_* to fill in the device
   configuration structures.

Change-Id: I3a897999175b14a4cd1111da4c26434741294e52
Signed-off-by: Vlad Dogaru <vlad.dogaru@intel.com>
This commit is contained in:
Vlad Dogaru 2016-04-21 17:13:46 +03:00 committed by Inaky Perez-Gonzalez
commit 2982f0e730

View file

@ -33,10 +33,6 @@ extern "C" {
#include <device.h>
#include <errno.h>
#ifdef CONFIG_SENSOR_WORKQUEUE
#include <misc/nano_work.h>
#endif
/** @brief Sensor value types. */
enum sensor_value_type {
/** val1 contains an integer value, val2 is unused. */
@ -467,6 +463,79 @@ static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
rad->val2 = ((int64_t)d * SENSOR_PI / 180LL) % 1000000LL;
}
/**
* @brief configuration parameters for sensor triggers.
*/
enum sensor_trigger_mode {
/** Do not use triggering. */
SENSOR_TRIG_MODE_NONE,
/**
* Driver should start a workqueue specifically for this
* device. See @ref sensor_trig_or_wq_config for instruction on
* how to specify the parameters of the workqueue.
*/
SENSOR_TRIG_MODE_OWN_WQ,
/** Use the system workqueue. */
SENSOR_TRIG_MODE_GLOBAL_WQ,
};
struct sensor_trigger_config {
/**
* This is always set to NULL when using a @ref
* sensor_trigger_config. See the comment in @ref
* sensor_trig_or_wq_config.
*/
void *always_null;
enum sensor_trigger_mode mode;
};
/**
* @brief Structure used for sensor trigger configuration.
*
* If @ref fiber_config.stack is non-NULL, the driver should start its
* on fiber based on @fiber_config. Otherwise, use @ref
* trig_config.mode to decide if and how to use triggering.
*/
union sensor_trig_or_wq_config {
struct fiber_config fiber_config;
struct sensor_trigger_config trig_config;
};
#define SENSOR_DECLARE_TRIG_CONFIG \
union sensor_trig_or_wq_config trig_or_wq_config
#define SENSOR_TRIG_WQ_OWN(_stack, _prio) \
.trig_or_wq_config = { \
.fiber_config = { \
.stack = (_stack), \
.stack_size = sizeof(_stack), \
.prio = (_prio), \
} \
}
#define SENSOR_TRIG_WQ_GLOBAL \
.trig_or_wq_config = { \
.trig_config = { \
.always_null = NULL, \
.mode = SENSOR_TRIG_MODE_GLOBAL_WQ, \
} \
}
#define SENSOR_TRIG_NONE \
.trig_or_wq_config = { \
.trig_config = { \
.always_null = NULL, \
.mode = SENSOR_TRIG_MODE_NONE, \
} \
}
#define SENSOR_GET_TRIG_MODE(_conf) \
(!(_conf)->trig_or_wq_config.fiber_config.stack \
? SENSOR_TRIG_MODE_OWN_WQ : \
(_conf)->trig_or_wq_config.trig_config.mode)
#define SENSOR_GET_WQ_CONFIG(_conf) \
((_conf)->trig_or_wq_config.fiber_config)
#ifdef __cplusplus
}
#endif