Skip to content

Commit

Permalink
lps25h move to px4 work queue
Browse files Browse the repository at this point in the history
  • Loading branch information
dagar committed May 24, 2019
1 parent 9003aeb commit b8befe3
Showing 1 changed file with 19 additions and 47 deletions.
66 changes: 19 additions & 47 deletions src/drivers/barometer/lps25h/lps25h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#include <unistd.h>

#include <nuttx/arch.h>
#include <nuttx/wqueue.h>
#include <px4_work_queue/ScheduledWorkItem.hpp>
#include <nuttx/clock.h>

#include <board_config.h>
Expand Down Expand Up @@ -189,11 +189,7 @@ enum LPS25H_BUS {
LPS25H_BUS_SPI
};

#ifndef CONFIG_SCHED_WORKQUEUE
# error This requires CONFIG_SCHED_WORKQUEUE.
#endif

class LPS25H : public cdev::CDev
class LPS25H : public cdev::CDev, public px4::ScheduledWorkItem
{
public:
LPS25H(device::Device *interface, const char *path);
Expand All @@ -213,8 +209,7 @@ class LPS25H : public cdev::CDev
device::Device *_interface;

private:
work_s _work{};
unsigned _measure_ticks{0};
unsigned _measure_interval{0};

ringbuffer::RingBuffer *_reports{nullptr};
bool _collect_phase{false};
Expand Down Expand Up @@ -259,15 +254,7 @@ class LPS25H : public cdev::CDev
* and measurement to provide the most recent measurement possible
* at the next interval.
*/
void cycle();

/**
* 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);
void Run() override;

/**
* Write a register.
Expand Down Expand Up @@ -312,6 +299,7 @@ extern "C" __EXPORT int lps25h_main(int argc, char *argv[]);

LPS25H::LPS25H(device::Device *interface, const char *path) :
CDev(path),
ScheduledWorkItem(px4::device_bus_to_wq(interface->get_device_id())),
_interface(interface),
_sample_perf(perf_alloc(PC_ELAPSED, "lps25h_read")),
_comms_errors(perf_alloc(PC_COUNT, "lps25h_comms_errors"))
Expand Down Expand Up @@ -386,7 +374,7 @@ LPS25H::read(struct file *filp, char *buffer, size_t buflen)
}

/* 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.
Expand Down Expand Up @@ -448,10 +436,10 @@ LPS25H::ioctl(struct file *filp, int cmd, unsigned long arg)
/* set default polling rate */
case SENSOR_POLLRATE_DEFAULT: {
/* 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 */
_measure_ticks = USEC2TICK(LPS25H_CONVERSION_INTERVAL);
_measure_interval = (LPS25H_CONVERSION_INTERVAL);

/* if we need to start the poll state machine, do it */
if (want_start) {
Expand All @@ -464,18 +452,18 @@ LPS25H::ioctl(struct file *filp, int cmd, unsigned long arg)
/* adjust to a legal polling interval in Hz */
default: {
/* 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 */
unsigned ticks = USEC2TICK(1000000 / arg);
unsigned interval = (1000000 / arg);

/* check against maximum rate */
if (ticks < USEC2TICK(LPS25H_CONVERSION_INTERVAL)) {
if (interval < (LPS25H_CONVERSION_INTERVAL)) {
return -EINVAL;
}

/* update interval for next measurement */
_measure_ticks = ticks;
_measure_interval = interval;

/* if we need to start the poll state machine, do it */
if (want_start) {
Expand Down Expand Up @@ -507,13 +495,13 @@ LPS25H::start()
_reports->flush();

/* schedule a cycle to start things */
work_queue(HPWORK, &_work, (worker_t)&LPS25H::cycle_trampoline, this, 1);
ScheduleNow();
}

void
LPS25H::stop()
{
work_cancel(HPWORK, &_work);
ScheduleClear();
}

int
Expand All @@ -540,15 +528,7 @@ LPS25H::reset()
}

void
LPS25H::cycle_trampoline(void *arg)
{
LPS25H *dev = reinterpret_cast<LPS25H *>(arg);

dev->cycle();
}

void
LPS25H::cycle()
LPS25H::Run()
{
/* collection phase? */
if (_collect_phase) {
Expand All @@ -567,14 +547,10 @@ LPS25H::cycle()
/*
* 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 */
work_queue(HPWORK,
&_work,
(worker_t)&LPS25H::cycle_trampoline,
this,
_measure_ticks - USEC2TICK(LPS25H_CONVERSION_INTERVAL));
ScheduleDelayed(_measure_interval - LPS25H_CONVERSION_INTERVAL);

return;
}
Expand All @@ -589,11 +565,7 @@ LPS25H::cycle()
_collect_phase = true;

/* schedule a fresh cycle call when the measurement is done */
work_queue(HPWORK,
&_work,
(worker_t)&LPS25H::cycle_trampoline,
this,
USEC2TICK(LPS25H_CONVERSION_INTERVAL));
ScheduleDelayed(LPS25H_CONVERSION_INTERVAL);
}

int
Expand Down Expand Up @@ -713,7 +685,7 @@ LPS25H::print_info()
{
perf_print_counter(_sample_perf);
perf_print_counter(_comms_errors);
printf("poll interval: %u ticks\n", _measure_ticks);
printf("poll interval: %u \n", _measure_interval);
print_message(_last_report);

_reports->print_info("report queue");
Expand Down

0 comments on commit b8befe3

Please sign in to comment.