[PATCH 3/4] sensor: Rework core to use workqueue API


Vlad Dogaru <vlad.dogaru@...>
 

In the process, also rename CONFIG_SENSOR_DELAYED_WORK to something more
meaningful and make it visible in menuconfig. Previously it was
selected implicitly by drivers when needed, but this is error prone now
that this symbol also depends on others.

Change-Id: I4a87019b17316ed69c61c7e63f5457dbb66c6dc8
Signed-off-by: Vlad Dogaru <vlad.dogaru(a)intel.com>
---
drivers/sensor/Kconfig | 21 ++++++++++++---------
drivers/sensor/Makefile | 2 +-
drivers/sensor/sensor.c | 33 ++++++++-------------------------
include/sensor.h | 31 ++++++++++++-------------------
4 files changed, 33 insertions(+), 54 deletions(-)

diff --git a/drivers/sensor/Kconfig b/drivers/sensor/Kconfig
index dc91f23..d61d023 100644
--- a/drivers/sensor/Kconfig
+++ b/drivers/sensor/Kconfig
@@ -23,18 +23,21 @@ menuconfig SENSOR
help
Include sensor drivers in system config

-config SENSOR_DELAYED_WORK
- depends on SENSOR
- bool
+config SENSOR_WORKQUEUE
+ bool "Global sensor workqueue support"
+ default n
+ depends on SENSOR && NANO_WORK
+ help
+ Say Y here to enable a global workqueue for sensor drivers.

-config SENSOR_DELAYED_WORK_STACK_SIZE
- int "Sensor delayed work fiber stack size"
- depends on SENSOR && SENSOR_DELAYED_WORK
+config SENSOR_WORKQUEUE_STACK_SIZE
+ int "Sensor workqueue stack size"
+ depends on SENSOR && SENSOR_WORKQUEUE
default 1024

-config SENSOR_DELAYED_WORK_PRIORITY
- int "Sensor delayed work fiber priority"
- depends on SENSOR && SENSOR_DELAYED_WORK
+config SENSOR_WORKQUEUE_PRIORITY
+ int "Sensor workqueue priority"
+ depends on SENSOR && SENSOR_WORKQUEUE
default 10

config SENSOR_DEBUG
diff --git a/drivers/sensor/Makefile b/drivers/sensor/Makefile
index 964ea6e..3a1f81f 100644
--- a/drivers/sensor/Makefile
+++ b/drivers/sensor/Makefile
@@ -1,6 +1,6 @@
ccflags-y +=-I$(srctree)/drivers

-obj-$(CONFIG_SENSOR_DELAYED_WORK) += sensor.o
+obj-$(CONFIG_SENSOR_WORKQUEUE) += sensor.o

obj-$(CONFIG_BMA280) += sensor_bma280.o
obj-$(CONFIG_BMA280_TRIGGER) += sensor_bma280_trigger.o
diff --git a/drivers/sensor/sensor.c b/drivers/sensor/sensor.c
index 2421f16..7b14a6e 100644
--- a/drivers/sensor/sensor.c
+++ b/drivers/sensor/sensor.c
@@ -18,38 +18,21 @@
#include <sensor.h>
#include <init.h>

-static char __stack sensor_fiber_stack[CONFIG_SENSOR_DELAYED_WORK_STACK_SIZE];
-static struct nano_fifo sensor_fifo;
+struct nano_workqueue sensor_workqueue;

-struct nano_fifo *sensor_get_work_fifo(void)
-{
- return &sensor_fifo;
-}
-
-static void sensor_fiber_main(int arg1, int arg2)
-{
- ARG_UNUSED(arg1);
- ARG_UNUSED(arg2);
+static char __stack sensor_wq_stack[CONFIG_SENSOR_WORKQUEUE_STACK_SIZE];

- while (1) {
- struct sensor_work *work;
-
- work = nano_fiber_fifo_get(&sensor_fifo, TICKS_UNLIMITED);
-
- work->handler(work->arg);
- }
-}
+static struct fiber_config sensor_wq_config = {
+ .fc_stack = sensor_wq_stack,
+ .fc_stack_size = CONFIG_SENSOR_WORKQUEUE_STACK_SIZE,
+ .fc_prio = CONFIG_SENSOR_WORKQUEUE_PRIORITY,
+};

static int sensor_init(struct device *dev)
{
ARG_UNUSED(dev);

- nano_fifo_init(&sensor_fifo);
-
- fiber_fiber_start(sensor_fiber_stack,
- CONFIG_SENSOR_DELAYED_WORK_STACK_SIZE,
- sensor_fiber_main, 0, 0,
- CONFIG_SENSOR_DELAYED_WORK_PRIORITY, 0);
+ nano_workqueue_start(&sensor_workqueue, &sensor_wq_config);

return 0;
}
diff --git a/include/sensor.h b/include/sensor.h
index b52c403..22569ed 100644
--- a/include/sensor.h
+++ b/include/sensor.h
@@ -33,6 +33,10 @@ 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. */
@@ -353,29 +357,18 @@ static inline int sensor_channel_get(struct device *dev,
return api->channel_get(dev, chan, val);
}

-#ifdef CONFIG_SENSOR_DELAYED_WORK
-typedef void (*sensor_work_handler_t)(void *arg);
+#ifdef CONFIG_SENSOR_WORKQUEUE

-/**
- * @brief Sensor delayed work descriptor.
- *
- * Used by sensor drivers internally to delay function calls to a fiber
- * context.
- */
-struct sensor_work {
- sensor_work_handler_t handler;
- void *arg;
-};
+extern struct nano_workqueue sensor_workqueue;

/**
- * @brief Get a fifo to which sensor delayed work can be submitted
- *
- * If @ref CONFIG_SENSOR_DELAYED_WORK is enabled, the system creates a
- * global fiber that can execute delayed work on behalf of drivers.
- * This is useful for drivers which need a mechanism of delayed work but
- * do not create their own fibers due to system resource constraints.
+ * @brief Submit a work item to the system-wide sensor workqueue.
*/
-struct nano_fifo *sensor_get_work_fifo(void);
+static inline void sensor_work_submit(struct nano_work *work)
+{
+ nano_work_submit(&sensor_workqueue, work);
+}
+
#endif

/**
--
1.9.1

Join devel@lists.zephyrproject.org to automatically receive all group messages.