Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

si57x_ctrl: add timeout to busy polling. #63

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/modules/si57x_ctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class Controller: public RegisterDecoderController {
/** The device's internal crystal frequency. */
double fxtal;

/** Update all registers and return state of busy flag. */
bool get_busy();
/** Loop until timeout while busy, return immediately otherwise. Updates all
* registers every iteration. Returns the final state of the busy flag. */
bool still_busy();

public:
/** The startup frequency has to be provided if read_startup_regs() is going
* to be called. */
Expand Down
40 changes: 28 additions & 12 deletions modules/si57x_ctrl.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <chrono>
#include <thread>

#include "printer.h"
#include "util.h"
#include "si57x_util.h"
Expand All @@ -14,6 +17,10 @@ namespace {
.device_id = SI57X_CTRL_DEVID,
.abi_ver_major = 1
};

using namespace std::chrono_literals;
constexpr auto busy_loop_time = 10ms;
constexpr unsigned busy_wait_attempts = 2s / busy_loop_time;
}

Core::Core(struct pcie_bars &bars):
Expand Down Expand Up @@ -71,10 +78,27 @@ Controller::Controller(struct pcie_bars &bars, double fstartup):
}
Controller::~Controller() = default;

void Controller::write_params()
bool Controller::get_busy()
{
dec.get_data();
if (dec.get_general_data<int32_t>("BUSY")) {
return dec.get_general_data<int32_t>("BUSY");
}

bool Controller::still_busy()
{
bool busy = get_busy();
for (unsigned i = 0; busy && i < busy_wait_attempts; i++) {
std::this_thread::sleep_for(busy_loop_time);

busy = get_busy();
}

return busy;
}

void Controller::write_params()
{
if (get_busy()) {
/* clear any action bits */
regs.ctl = 0;
throw std::runtime_error("writes not allowed while busy");
Expand All @@ -94,11 +118,7 @@ bool Controller::read_startup_regs()
write_general("READ_STRP_REGS", 1);
write_params();

do {
dec.get_data();
} while (dec.get_general_data<int32_t>("BUSY"));

if (!dec.get_general_data<int32_t>("STRP_COMPLETE"))
if (still_busy() || !dec.get_general_data<int32_t>("STRP_COMPLETE"))
return false;
}

Expand Down Expand Up @@ -127,11 +147,7 @@ bool Controller::apply_config()
write_general("APPLY_CFG", 1);
write_params();

do {
dec.get_data();
} while (dec.get_general_data<int32_t>("BUSY"));

if (!dec.get_general_data<int32_t>("CFG_IN_SYNC"))
if (still_busy() || !dec.get_general_data<int32_t>("CFG_IN_SYNC"))
return false;

return true;
Expand Down