From 143086ba2c10cc7901c7d7456944b36eb3cb0e57 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Sat, 17 Dec 2016 20:41:17 +0100 Subject: [PATCH] HMC5883: Harden shutdown logic --- src/drivers/hmc5883/hmc5883.cpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/drivers/hmc5883/hmc5883.cpp b/src/drivers/hmc5883/hmc5883.cpp index 509dc45ea0..81c5c1d5fc 100644 --- a/src/drivers/hmc5883/hmc5883.cpp +++ b/src/drivers/hmc5883/hmc5883.cpp @@ -781,7 +781,11 @@ HMC5883::start() void HMC5883::stop() { - work_cancel(HPWORK, &_work); + if (_measure_ticks > 0) { + /* ensure no new items are queued while we cancel this one */ + _measure_ticks = 0; + work_cancel(HPWORK, &_work); + } } int @@ -802,6 +806,10 @@ HMC5883::cycle_trampoline(void *arg) void HMC5883::cycle() { + if (_measure_ticks == 0) { + return; + } + /* collection phase? */ if (_collect_phase) { @@ -840,12 +848,14 @@ HMC5883::cycle() /* next phase is collection */ _collect_phase = true; - /* schedule a fresh cycle call when the measurement is done */ - work_queue(HPWORK, - &_work, - (worker_t)&HMC5883::cycle_trampoline, - this, - USEC2TICK(HMC5883_CONVERSION_INTERVAL)); + if (_measure_ticks > 0) { + /* schedule a fresh cycle call when the measurement is done */ + work_queue(HPWORK, + &_work, + (worker_t)&HMC5883::cycle_trampoline, + this, + USEC2TICK(HMC5883_CONVERSION_INTERVAL)); + } } int