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
|
2018-01-20 17:55:31 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
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"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "scheduler.h"
|
|
|
|
#include "signal_executor.h"
|
2016-09-26 19:02:53 -07:00
|
|
|
#include "main_trigger_callback.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-09-25 20:02:28 -07:00
|
|
|
#if EFI_SIMULATOR || defined(__DOXYGEN__)
|
|
|
|
// this is about debugging
|
|
|
|
#include "efiGpio.h"
|
|
|
|
#endif /* EFI_SIMULATOR */
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#if EFI_SIGNAL_EXECUTOR_SLEEP || defined(__DOXYGEN__)
|
|
|
|
|
2019-01-08 21:53:54 -08:00
|
|
|
void SleepExecutor::scheduleByTimestamp(scheduling_s *scheduling, efitimeus_t timeUs, schfunc_t callback, void *param) {
|
|
|
|
scheduleForLater(scheduling, timeUs - getTimeNowUs(), callback, param);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:02:28 -07:00
|
|
|
static void timerCallback(scheduling_s *scheduling) {
|
2016-09-25 21:03:15 -07:00
|
|
|
#if EFI_PRINTF_FUEL_DETAILS || defined(__DOXYGEN__)
|
2016-09-25 20:02:28 -07:00
|
|
|
if (scheduling->callback == (schfunc_t)&seTurnPinLow) {
|
|
|
|
printf("executing cb=seTurnPinLow p=%d sch=%d now=%d\r\n", (int)scheduling->param, (int)scheduling,
|
|
|
|
(int)getTimeNowUs());
|
|
|
|
} else {
|
|
|
|
// printf("exec cb=%d p=%d\r\n", (int)scheduling->callback, (int)scheduling->param);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_SIMULATOR */
|
|
|
|
scheduling->callback(scheduling->param);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-09 06:37:16 -08:00
|
|
|
static void doScheduleForLater(scheduling_s *scheduling, int delayUs, schfunc_t callback, void *param) {
|
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
|
|
|
|
*/
|
|
|
|
callback(param);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-25 11:51:21 -07:00
|
|
|
bool alreadyLocked = lockAnyContext();
|
2016-09-25 20:02:28 -07:00
|
|
|
scheduling->callback = callback;
|
|
|
|
scheduling->param = param;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#if EFI_SIMULATOR || defined(__DOXYGEN__)
|
|
|
|
if (callback == (schfunc_t)&seTurnPinLow) {
|
|
|
|
printf("setTime cb=seTurnPinLow p=%d\r\n", (int)param);
|
|
|
|
} 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);
|
2018-02-03 14:07:04 -08:00
|
|
|
if (!alreadyLocked) {
|
2017-05-25 11:51:21 -07:00
|
|
|
unlockAnyContext();
|
2018-02-03 14:07:04 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2019-01-09 04:57:43 -08:00
|
|
|
void SleepExecutor::scheduleForLater(scheduling_s *scheduling, int delayUs, schfunc_t callback, void *param) {
|
2019-01-09 06:37:16 -08:00
|
|
|
doScheduleForLater(scheduling, delayUs, callback, param);
|
2019-01-09 04:57:43 -08:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_SIGNAL_EXECUTOR_SLEEP */
|