rusEFI
The most advanced open source ECU
signal_executor_sleep.cpp
Go to the documentation of this file.
1 /**
2  * @file signal_executor_sleep.cpp
3  * @brief Asynchronous output signal code
4  *
5  * Here we have the simplest, thread-based implementation of signal executor.
6  *
7  * @date Feb 10, 2013
8  * @author Andrey Belomutskiy, (c) 2012-2020
9  *
10  * This file is part of rusEfi - see http://rusefi.com
11  *
12  * rusEfi is free software; you can redistribute it and/or modify it under the terms of
13  * the GNU General Public License as published by the Free Software Foundation; either
14  * version 3 of the License, or (at your option) any later version.
15  *
16  * rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
17  * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along with this program.
21  * If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "pch.h"
25 
26 #include "scheduler.h"
27 #include "main_trigger_callback.h"
28 
29 #if EFI_PRINTF_FUEL_DETAILS
30 bool printSchedulerDebug = true;
31 #endif // EFI_PRINTF_FUEL_DETAILS
32 
33 #if EFI_SIGNAL_EXECUTOR_SLEEP
34 
35 void SleepExecutor::scheduleByTimestamp(const char *msg, scheduling_s *scheduling, efitimeus_t timeUs, action_s action) {
36  scheduleForLater(msg, scheduling, timeUs - getTimeNowUs(), action);
37 }
38 
39 void SleepExecutor::scheduleByTimestampNt(const char *msg, scheduling_s* scheduling, efitick_t timeNt, action_s action) {
40  scheduleByTimestamp(msg, scheduling, NT2US(timeNt), action);
41 }
42 
43 static void timerCallback(scheduling_s *scheduling) {
44 #if EFI_PRINTF_FUEL_DETAILS
45  if (printSchedulerDebug) {
46  if (scheduling->action.getCallback() == (schfunc_t)&turnInjectionPinLow) {
47  printf("executing cb=turnInjectionPinLow p=%d sch=%d now=%d\r\n", (int)scheduling->action.getArgument(), (int)scheduling,
48  (int)getTimeNowUs());
49  } else {
50 // printf("exec cb=%d p=%d\r\n", (int)scheduling->callback, (int)scheduling->param);
51  }
52  }
53 #endif // EFI_PRINTF_FUEL_DETAILS
54  scheduling->action.execute();
55 }
56 
57 static void doScheduleForLater(scheduling_s *scheduling, int delayUs, action_s action) {
58  int delaySt = MY_US2ST(delayUs);
59  if (delaySt <= 0) {
60  /**
61  * in case of zero delay, we should invoke the callback
62  */
63  action.execute();
64  return;
65  }
66 
67  chibios_rt::CriticalSectionLocker csl;
68 
69  scheduling->action = action;
70  int isArmed = chVTIsArmedI(&scheduling->timer);
71  if (isArmed) {
72  /**
73  * timer reuse is normal for example in case of sudden RPM increase
74  */
75  chVTResetI(&scheduling->timer);
76  }
77 
78 #if EFI_SIMULATOR
79 // if (action.getCallback() == (schfunc_t)&turnInjectionPinLow) {
80  //printf("setTime cb=turnInjectionPinLow p=%d\r\n", (int)action.getArgument());
81 // } else {
82 // printf("setTime cb=%d p=%d\r\n", (int)callback, (int)param);
83 // }
84 #endif /* EFI_SIMULATOR */
85 
86  chVTSetI(&scheduling->timer, delaySt, (vtfunc_t)timerCallback, scheduling);
87 }
88 
89 void SleepExecutor::scheduleForLater(const char *msg, scheduling_s *scheduling, int delayUs, action_s action) {
90  doScheduleForLater(scheduling, delayUs, action);
91 }
92 
94  chibios_rt::CriticalSectionLocker csl;
95 
96  if (chVTIsArmedI(&s->timer)) {
97  chVTResetI(&s->timer);
98  }
99 
100  s->action = {};
101 }
102 
103 #endif /* EFI_SIGNAL_EXECUTOR_SLEEP */
void scheduleByTimestampNt(const char *msg, scheduling_s *scheduling, efitick_t timeNt, action_s action) override
void scheduleForLater(const char *msg, scheduling_s *scheduling, int delayUs, action_s action) override
void scheduleByTimestamp(const char *msg, scheduling_s *scheduling, efitimeus_t timeUs, action_s action) override
void cancel(scheduling_s *s) override
void execute()
Definition: scheduler.cpp:10
void * getArgument() const
Definition: scheduler.cpp:19
schfunc_t getCallback() const
Definition: scheduler.cpp:15
efitimeus_t getTimeNowUs()
Definition: efitime.cpp:19
static WrapAround62 timeNt
Definition: efitime.cpp:7
void turnInjectionPinLow(InjectionEvent *event)
Main logic header.
static const char * msg
void(* schfunc_t)(void *)
Definition: scheduler.h:9
bool printSchedulerDebug
static void timerCallback(scheduling_s *scheduling)
static void doScheduleForLater(scheduling_s *scheduling, int delayUs, action_s action)
virtual_timer_t timer
Definition: scheduler.h:45
action_s action
Definition: scheduler.h:58
printf("\n")