Browse Source

lps25h move to px4 work queue

sbg
Daniel Agar 6 years ago
parent
commit
b8befe36b9
  1. 66
      src/drivers/barometer/lps25h/lps25h.cpp

66
src/drivers/barometer/lps25h/lps25h.cpp

@ -57,7 +57,7 @@
#include <unistd.h> #include <unistd.h>
#include <nuttx/arch.h> #include <nuttx/arch.h>
#include <nuttx/wqueue.h> #include <px4_work_queue/ScheduledWorkItem.hpp>
#include <nuttx/clock.h> #include <nuttx/clock.h>
#include <board_config.h> #include <board_config.h>
@ -189,11 +189,7 @@ enum LPS25H_BUS {
LPS25H_BUS_SPI LPS25H_BUS_SPI
}; };
#ifndef CONFIG_SCHED_WORKQUEUE class LPS25H : public cdev::CDev, public px4::ScheduledWorkItem
# error This requires CONFIG_SCHED_WORKQUEUE.
#endif
class LPS25H : public cdev::CDev
{ {
public: public:
LPS25H(device::Device *interface, const char *path); LPS25H(device::Device *interface, const char *path);
@ -213,8 +209,7 @@ protected:
device::Device *_interface; device::Device *_interface;
private: private:
work_s _work{}; unsigned _measure_interval{0};
unsigned _measure_ticks{0};
ringbuffer::RingBuffer *_reports{nullptr}; ringbuffer::RingBuffer *_reports{nullptr};
bool _collect_phase{false}; bool _collect_phase{false};
@ -259,15 +254,7 @@ private:
* and measurement to provide the most recent measurement possible * and measurement to provide the most recent measurement possible
* at the next interval. * at the next interval.
*/ */
void cycle(); void Run() override;
/**
* Static trampoline from the workq context; because we don't have a
* generic workq wrapper yet.
*
* @param arg Instance pointer for the driver that is polling.
*/
static void cycle_trampoline(void *arg);
/** /**
* Write a register. * Write a register.
@ -312,6 +299,7 @@ extern "C" __EXPORT int lps25h_main(int argc, char *argv[]);
LPS25H::LPS25H(device::Device *interface, const char *path) : LPS25H::LPS25H(device::Device *interface, const char *path) :
CDev(path), CDev(path),
ScheduledWorkItem(px4::device_bus_to_wq(interface->get_device_id())),
_interface(interface), _interface(interface),
_sample_perf(perf_alloc(PC_ELAPSED, "lps25h_read")), _sample_perf(perf_alloc(PC_ELAPSED, "lps25h_read")),
_comms_errors(perf_alloc(PC_COUNT, "lps25h_comms_errors")) _comms_errors(perf_alloc(PC_COUNT, "lps25h_comms_errors"))
@ -386,7 +374,7 @@ LPS25H::read(struct file *filp, char *buffer, size_t buflen)
} }
/* if automatic measurement is enabled */ /* if automatic measurement is enabled */
if (_measure_ticks > 0) { if (_measure_interval > 0) {
/* /*
* While there is space in the caller's buffer, and reports, copy them. * While there is space in the caller's buffer, and reports, copy them.
@ -448,10 +436,10 @@ LPS25H::ioctl(struct file *filp, int cmd, unsigned long arg)
/* set default polling rate */ /* set default polling rate */
case SENSOR_POLLRATE_DEFAULT: { case SENSOR_POLLRATE_DEFAULT: {
/* do we need to start internal polling? */ /* do we need to start internal polling? */
bool want_start = (_measure_ticks == 0); bool want_start = (_measure_interval == 0);
/* set interval for next measurement to minimum legal value */ /* set interval for next measurement to minimum legal value */
_measure_ticks = USEC2TICK(LPS25H_CONVERSION_INTERVAL); _measure_interval = (LPS25H_CONVERSION_INTERVAL);
/* if we need to start the poll state machine, do it */ /* if we need to start the poll state machine, do it */
if (want_start) { if (want_start) {
@ -464,18 +452,18 @@ LPS25H::ioctl(struct file *filp, int cmd, unsigned long arg)
/* adjust to a legal polling interval in Hz */ /* adjust to a legal polling interval in Hz */
default: { default: {
/* do we need to start internal polling? */ /* do we need to start internal polling? */
bool want_start = (_measure_ticks == 0); bool want_start = (_measure_interval == 0);
/* convert hz to tick interval via microseconds */ /* convert hz to tick interval via microseconds */
unsigned ticks = USEC2TICK(1000000 / arg); unsigned interval = (1000000 / arg);
/* check against maximum rate */ /* check against maximum rate */
if (ticks < USEC2TICK(LPS25H_CONVERSION_INTERVAL)) { if (interval < (LPS25H_CONVERSION_INTERVAL)) {
return -EINVAL; return -EINVAL;
} }
/* update interval for next measurement */ /* update interval for next measurement */
_measure_ticks = ticks; _measure_interval = interval;
/* if we need to start the poll state machine, do it */ /* if we need to start the poll state machine, do it */
if (want_start) { if (want_start) {
@ -507,13 +495,13 @@ LPS25H::start()
_reports->flush(); _reports->flush();
/* schedule a cycle to start things */ /* schedule a cycle to start things */
work_queue(HPWORK, &_work, (worker_t)&LPS25H::cycle_trampoline, this, 1); ScheduleNow();
} }
void void
LPS25H::stop() LPS25H::stop()
{ {
work_cancel(HPWORK, &_work); ScheduleClear();
} }
int int
@ -540,15 +528,7 @@ LPS25H::reset()
} }
void void
LPS25H::cycle_trampoline(void *arg) LPS25H::Run()
{
LPS25H *dev = reinterpret_cast<LPS25H *>(arg);
dev->cycle();
}
void
LPS25H::cycle()
{ {
/* collection phase? */ /* collection phase? */
if (_collect_phase) { if (_collect_phase) {
@ -567,14 +547,10 @@ LPS25H::cycle()
/* /*
* Is there a collect->measure gap? * Is there a collect->measure gap?
*/ */
if (_measure_ticks > USEC2TICK(LPS25H_CONVERSION_INTERVAL)) { if (_measure_interval > (LPS25H_CONVERSION_INTERVAL)) {
/* schedule a fresh cycle call when we are ready to measure again */ /* schedule a fresh cycle call when we are ready to measure again */
work_queue(HPWORK, ScheduleDelayed(_measure_interval - LPS25H_CONVERSION_INTERVAL);
&_work,
(worker_t)&LPS25H::cycle_trampoline,
this,
_measure_ticks - USEC2TICK(LPS25H_CONVERSION_INTERVAL));
return; return;
} }
@ -589,11 +565,7 @@ LPS25H::cycle()
_collect_phase = true; _collect_phase = true;
/* schedule a fresh cycle call when the measurement is done */ /* schedule a fresh cycle call when the measurement is done */
work_queue(HPWORK, ScheduleDelayed(LPS25H_CONVERSION_INTERVAL);
&_work,
(worker_t)&LPS25H::cycle_trampoline,
this,
USEC2TICK(LPS25H_CONVERSION_INTERVAL));
} }
int int
@ -713,7 +685,7 @@ LPS25H::print_info()
{ {
perf_print_counter(_sample_perf); perf_print_counter(_sample_perf);
perf_print_counter(_comms_errors); perf_print_counter(_comms_errors);
printf("poll interval: %u ticks\n", _measure_ticks); printf("poll interval: %u \n", _measure_interval);
print_message(_last_report); print_message(_last_report);
_reports->print_info("report queue"); _reports->print_info("report queue");

Loading…
Cancel
Save