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

bench test uses a semaphore instead of flags+sleep #110

Merged
merged 8 commits into from
Jun 29, 2023
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
2 changes: 1 addition & 1 deletion firmware/controllers/algo/obd_error_codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2123,7 +2123,7 @@ enum class ObdCode : uint16_t {
CUSTOM_INSTANT_MAP_DECODING = 6899,
STACK_USAGE_COMMUNICATION = 6900,
STACK_USAGE_MIL = 6901,
STACK_USAGE_BENCH = 6902,
CUSTOM_6902 = 6902,
STACK_USAGE_STATUS = 6903,
STACK_USAGE_4 = 6904,

Expand Down
46 changes: 23 additions & 23 deletions firmware/controllers/bench_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ static void benchOn(OutputPin* output) {
output->setValue(true);
}

static char pin_error[64];

static void benchOff(OutputPin* output) {
#if EFI_PROD_CODE && (BOARD_EXT_GPIOCHIPS > 0)
static char pin_error[64];

brain_pin_diag_e diag = output->getDiag();
if (diag == PIN_INVALID) {
efiPrintf("No Diag on this pin");
Expand Down Expand Up @@ -123,6 +123,8 @@ static int count;
static brain_pin_e brainPin;
static OutputPin* pinX;

static chibios_rt::CounterSemaphore benchSemaphore(0);

static void pinbench(float startdelay, float ontime, float offtime, int iterations,
OutputPin* pinParam, brain_pin_e brainPinParam)
{
Expand All @@ -134,6 +136,7 @@ static void pinbench(float startdelay, float ontime, float offtime, int iteratio
brainPin = brainPinParam;
// let's signal bench thread to wake up
isBenchTestPending = true;
benchSemaphore.signal();
}

/*==========================================================================*/
Expand Down Expand Up @@ -271,27 +274,25 @@ void fuelPumpBench(void) {
fuelPumpBenchExt(3000.0);
}

class BenchController : public PeriodicController<UTILITY_THREAD_STACK_SIZE> {
class BenchController : public ThreadController<UTILITY_THREAD_STACK_SIZE> {
public:
BenchController() : PeriodicController("BenchThread") { }
BenchController() : ThreadController("BenchTest", PRIO_BENCH_TEST) { }
private:
void PeriodicTask(efitick_t nowNt) override {
UNUSED(nowNt);
setPeriod(50 /* ms */);

validateStack("Bench", ObdCode::STACK_USAGE_BENCH, 128);

// naive inter-thread communication - waiting for a flag
if (isBenchTestPending) {
isBenchTestPending = false;
runBench(brainPin, pinX, startDelayMs, onTime, offTime, count);
}

if (widebandUpdatePending) {
#if EFI_WIDEBAND_FIRMWARE_UPDATE && EFI_CAN_SUPPORT
updateWidebandFirmware();
#endif
widebandUpdatePending = false;
void ThreadTask() override {
while (true) {
benchSemaphore.wait();

if (isBenchTestPending) {
isBenchTestPending = false;
runBench(brainPin, pinX, startDelayMs, onTime, offTime, count);
}

if (widebandUpdatePending) {
#if EFI_WIDEBAND_FIRMWARE_UPDATE && EFI_CAN_SUPPORT
updateWidebandFirmware();
#endif
widebandUpdatePending = false;
}
}
}
};
Expand Down Expand Up @@ -394,6 +395,7 @@ static void handleCommandX14(uint16_t index) {
#endif
case 0x12:
widebandUpdatePending = true;
benchSemaphore.signal();
return;
case 0x14:
#ifdef STM32F7
Expand Down Expand Up @@ -446,7 +448,6 @@ void executeTSCommand(uint16_t subsystem, uint16_t index) {

case TS_IGNITION_CATEGORY:
if (!running) {
/* WARN: fixed charge time */
doRunSparkBench(index, 300.0, engineConfiguration->benchTestOnTime,
engineConfiguration->benchTestOffTime, engineConfiguration->benchTestCount);
}
Expand Down Expand Up @@ -568,7 +569,6 @@ void initBenchTest() {
addConsoleAction(CMD_HPFP_BENCH, hpfpValveBench);

addConsoleActionFFFFF("luabench2", luaOutBench2);
instance.setPeriod(200 /*ms*/);
instance.start();
onConfigurationChangeBenchTest();
}
Expand Down
1 change: 1 addition & 0 deletions firmware/controllers/thread_priority.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
// These can get starved without too much adverse effect
#define PRIO_AUX_SERIAL NORMALPRIO
#define PRIO_KNOCK_PROCESS (NORMALPRIO - 10)
#define PRIO_BENCH_TEST (NORMALPRIO - 10)

// These are intentionally low priority so they can't get in the way of anything else
#define PRIO_FLASH_WRITE LOWPRIO + 20
Expand Down
Loading