rusefi-1/firmware/controllers/system/timer/signal_executor_sleep.cpp

100 lines
3.1 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file signal_executor_sleep.cpp
* @brief Asynchronous output signal code
*
* Here we have the simplest, thread-based implementation of signal executor.
* TODO: https://sourceforge.net/p/rusefi/tickets/6/
*
* @date Feb 10, 2013
2020-01-13 18:57:43 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*
* This file is part of rusEfi - see http://rusefi.com
*
* rusEfi is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
2018-09-16 19:26:57 -07:00
#include "global.h"
#include "os_access.h"
2015-07-10 06:01:56 -07:00
#include "scheduler.h"
2016-09-26 19:02:53 -07:00
#include "main_trigger_callback.h"
2015-07-10 06:01:56 -07:00
2019-04-12 19:07:03 -07:00
#if EFI_SIMULATOR
2016-09-25 20:02:28 -07:00
// this is about debugging
2019-03-29 06:11:13 -07:00
#include "efi_gpio.h"
2016-09-25 20:02:28 -07:00
#endif /* EFI_SIMULATOR */
2020-07-19 11:58:46 -07:00
#if EFI_PRINTF_FUEL_DETAILS
bool printSchedulerDebug = true;
2020-07-19 12:25:49 -07:00
#endif // EFI_PRINTF_FUEL_DETAILS
2020-07-19 11:58:46 -07:00
2019-04-12 19:07:03 -07:00
#if EFI_SIGNAL_EXECUTOR_SLEEP
2015-07-10 06:01:56 -07:00
void SleepExecutor::scheduleByTimestamp(scheduling_s *scheduling, efitimeus_t timeUs, action_s action) {
scheduleForLater(scheduling, timeUs - getTimeNowUs(), action);
2015-07-10 06:01:56 -07:00
}
void SleepExecutor::scheduleByTimestampNt(scheduling_s* scheduling, efitick_t timeNt, action_s action) {
scheduleByTimestamp(scheduling, NT2US(timeNt), action);
}
2016-09-25 20:02:28 -07:00
static void timerCallback(scheduling_s *scheduling) {
2019-04-12 19:07:03 -07:00
#if EFI_PRINTF_FUEL_DETAILS
2020-07-19 11:58:46 -07:00
if (printSchedulerDebug) {
if (scheduling->action.getCallback() == (schfunc_t)&turnInjectionPinLow) {
printf("executing cb=turnInjectionPinLow p=%d sch=%d now=%d\r\n", (int)scheduling->action.getArgument(), (int)scheduling,
2016-09-25 20:02:28 -07:00
(int)getTimeNowUs());
2020-07-19 11:58:46 -07:00
} else {
2016-09-25 20:02:28 -07:00
// printf("exec cb=%d p=%d\r\n", (int)scheduling->callback, (int)scheduling->param);
2020-07-19 11:58:46 -07:00
}
2016-09-25 20:02:28 -07:00
}
2020-07-19 11:58:46 -07:00
#endif // EFI_PRINTF_FUEL_DETAILS
2019-11-23 15:38:16 -08:00
scheduling->action.execute();
2016-09-25 20:02:28 -07:00
}
static void doScheduleForLater(scheduling_s *scheduling, int delayUs, action_s action) {
2015-07-10 06:01:56 -07:00
int delaySt = MY_US2ST(delayUs);
if (delaySt <= 0) {
/**
* in case of zero delay, we should invoke the callback
*/
action.execute();
2015-07-10 06:01:56 -07:00
return;
}
chibios_rt::CriticalSectionLocker csl;
scheduling->action = action;
2015-07-10 06:01:56 -07:00
int isArmed = chVTIsArmedI(&scheduling->timer);
2016-10-02 09:02:42 -07:00
if (isArmed) {
2017-03-03 19:58:30 -08:00
/**
* timer reuse is normal for example in case of sudden RPM increase
*/
2015-07-10 06:01:56 -07:00
chVTResetI(&scheduling->timer);
2016-09-25 20:02:28 -07:00
}
2019-04-12 19:07:03 -07:00
#if EFI_SIMULATOR
2020-01-10 20:17:58 -08:00
if (action.getCallback() == (schfunc_t)&turnInjectionPinLow) {
printf("setTime cb=turnInjectionPinLow p=%d\r\n", (int)action.getArgument());
2016-09-25 20:02:28 -07:00
} else {
// printf("setTime cb=%d p=%d\r\n", (int)callback, (int)param);
}
#endif /* EFI_SIMULATOR */
2015-07-10 06:01:56 -07:00
2016-09-25 20:02:28 -07:00
chVTSetI(&scheduling->timer, delaySt, (vtfunc_t)timerCallback, scheduling);
2015-07-10 06:01:56 -07:00
}
void SleepExecutor::scheduleForLater(scheduling_s *scheduling, int delayUs, action_s action) {
doScheduleForLater(scheduling, delayUs, action);
}
2015-07-10 06:01:56 -07:00
#endif /* EFI_SIGNAL_EXECUTOR_SLEEP */