Commit faaee0f0 authored by Beat Küng's avatar Beat Küng Committed by Lorenz Meier
Browse files

fix pwm_out: avoid race condition when setting mode

Bootup failed in some cases with:
ERROR [mixer] can't reset mixers on /dev/pwm_output1

The reason was the mode change was not applied yet.
parent 18f17dcb
......@@ -580,9 +580,9 @@ void PWMOut::Run()
// push backup schedule
ScheduleDelayed(_backup_schedule_interval_us);
if (_new_mode_request.load() != _mode) {
if (_new_mode_request.load() != MODE_NO_REQUEST) {
set_mode(_new_mode_request.load());
_new_mode_request.store(_mode);
_new_mode_request.store(MODE_NO_REQUEST);
}
_mixing_output.update();
......@@ -1762,6 +1762,22 @@ int PWMOut::fmu_new_mode(PortMode new_mode)
return OK;
}
void PWMOut::request_mode(Mode new_mode)
{
if (_new_mode_request.load() != MODE_NO_REQUEST) {
PX4_ERR("already being set"); // not expected to happen
return;
}
_new_mode_request.store(new_mode);
ScheduleNow();
// wait until processed
int max_time = 1000;
while (_new_mode_request.load() != MODE_NO_REQUEST && max_time-- > 0) {
px4_usleep(1000);
}
}
namespace
{
......
......@@ -118,6 +118,8 @@ public:
MODE_4CAP,
MODE_5CAP,
MODE_6CAP,
MODE_NO_REQUEST
};
PWMOut() = delete;
......@@ -157,7 +159,7 @@ public:
int set_mode(Mode mode);
Mode get_mode() { return _mode; }
void request_mode(Mode new_mode) { _new_mode_request.store(new_mode); }
void request_mode(Mode new_mode);
static int set_i2c_bus_clock(unsigned bus, unsigned clock_hz);
......@@ -181,7 +183,7 @@ private:
Mode _mode{MODE_NONE};
px4::atomic<Mode> _new_mode_request{MODE_NONE};
px4::atomic<Mode> _new_mode_request{MODE_NO_REQUEST};
uint32_t _backup_schedule_interval_us{1_s};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment