fome-fw/firmware/controllers/algo/signal_executor.cpp

141 lines
4.2 KiB
C++
Raw Normal View History

2014-08-29 07:52:33 -07:00
/**
2015-01-01 13:04:22 -08:00
* @file signal_executor.cpp
2014-08-29 07:52:33 -07:00
*
* todo: we should split this file into two:
* one for pure scheduling and another one for signal output which would
* use the scheduling
*
* @date Dec 4, 2013
2015-01-12 15:04:10 -08:00
* @author Andrey Belomutskiy, (c) 2012-2015
2014-08-29 07:52:33 -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/>.
*/
#include "main.h"
#include "signal_executor.h"
2014-09-27 15:03:08 -07:00
#include "efiGpio.h"
2014-11-23 20:03:16 -08:00
#include "engine.h"
2014-08-29 07:52:33 -07:00
2014-09-01 06:02:46 -07:00
/**
* Signal executors feed digital events right into WaveChart used by Sniffer tab of Dev Console
*/
2014-08-29 07:52:33 -07:00
#include "rpm_calculator.h"
2014-11-23 20:03:16 -08:00
EXTERN_ENGINE;
2015-01-01 13:04:22 -08:00
#if EFI_WAVE_CHART
2014-11-23 19:03:07 -08:00
#include "wave_chart.h"
extern WaveChart waveChart;
2015-01-01 13:04:22 -08:00
#endif
2014-08-29 07:52:33 -07:00
2015-01-13 12:03:49 -08:00
#include "efiGpio.h"
extern engine_pins_s enginePins;
2014-11-23 20:03:16 -08:00
2015-01-13 12:03:49 -08:00
static const char *sparkNames[ENGINE_CHANNEL_COUNT] = { "spa1", "spa2", "spa3", "spa4", "spa5", "spa6", "spa7", "spa8",
"spa9", "spa10", "spa11", "spa12"};
2015-01-13 11:03:46 -08:00
2015-01-13 12:03:49 -08:00
static const char *injectorNames[ENGINE_CHANNEL_COUNT] = { "inj1", "inj2", "inj3", "inj4", "inj5", "inj6", "inj7", "inj8", "inj9",
"inj10", "inj11", "inj12"};
2015-01-13 11:03:46 -08:00
2014-08-29 07:52:33 -07:00
void initSignalExecutor(void) {
initSignalExecutorImpl();
2015-01-13 11:03:46 -08:00
2015-01-13 12:03:49 -08:00
for (int i = 0; i < ENGINE_CHANNEL_COUNT;i++) {
enginePins.coils[i].name = sparkNames[i];
enginePins.injectors[i].name = injectorNames[i];
}
2014-08-29 07:52:33 -07:00
}
2015-01-13 09:05:05 -08:00
//uint32_t dbgStart;
//uint32_t dbgDurr;
2014-11-22 14:03:07 -08:00
2015-01-13 09:05:05 -08:00
void turnPinHigh(NamedOutputPin *output) {
2015-01-13 10:06:16 -08:00
efiAssertVoid(output!=NULL, "NULL @ turnPinHigh");
2014-08-29 07:52:33 -07:00
#if EFI_DEFAILED_LOGGING
// signal->hi_time = hTimeNow();
#endif /* EFI_DEFAILED_LOGGING */
2014-12-24 11:05:19 -08:00
#if EFI_GPIO
2014-08-29 07:52:33 -07:00
// turn the output level ACTIVE
// todo: this XOR should go inside the setOutputPinValue method
2015-01-13 09:05:05 -08:00
doSetOutputPinValue2(output, true);
2014-08-29 07:52:33 -07:00
// sleep for the needed duration
2014-12-24 11:05:19 -08:00
#endif
2014-08-29 07:52:33 -07:00
#if EFI_WAVE_CHART
2014-11-23 20:03:16 -08:00
// explicit check here is a performance optimization to speed up no-chart mode
2014-11-25 11:03:49 -08:00
if (CONFIG(isDigitalChartEnabled)) {
2014-11-23 20:03:16 -08:00
// this is a performance optimization - array index is cheaper then invoking a method with 'switch'
2015-01-13 09:05:05 -08:00
const char *pinName = output->name;
2014-11-22 15:04:49 -08:00
// dbgDurr = hal_lld_get_counter_value() - dbgStart;
2014-11-22 14:03:07 -08:00
2014-11-23 20:03:16 -08:00
addWaveChartEvent(pinName, WC_UP);
}
2014-08-29 07:52:33 -07:00
#endif /* EFI_WAVE_ANALYZER */
2014-11-22 15:04:49 -08:00
// dbgDurr = hal_lld_get_counter_value() - dbgStart;
2014-08-29 07:52:33 -07:00
}
2015-01-13 09:05:05 -08:00
void turnPinLow(NamedOutputPin *output) {
2015-01-13 11:03:46 -08:00
efiAssertVoid(output!=NULL, "NULL turnPinLow");
2014-12-24 11:05:19 -08:00
#if EFI_GPIO
2014-08-29 07:52:33 -07:00
// turn off the output
2015-01-13 09:05:05 -08:00
doSetOutputPinValue2(output, false);
2014-12-24 11:05:19 -08:00
#endif
2014-08-29 07:52:33 -07:00
#if EFI_DEFAILED_LOGGING
systime_t after = hTimeNow();
debugInt(&signal->logging, "a_time", after - signal->hi_time);
scheduleLogging(&signal->logging);
#endif /* EFI_DEFAILED_LOGGING */
#if EFI_WAVE_CHART
2014-11-25 11:03:49 -08:00
if (CONFIG(isDigitalChartEnabled)) {
2014-11-23 20:03:16 -08:00
// this is a performance optimization - array index is cheaper then invoking a method with 'switch'
2015-01-13 09:05:05 -08:00
const char *pinName = output->name;
2014-11-22 14:03:07 -08:00
2014-11-23 20:03:16 -08:00
addWaveChartEvent(pinName, WC_DOWN);
}
2014-08-29 07:52:33 -07:00
#endif /* EFI_WAVE_ANALYZER */
}
int getRevolutionCounter(void);
/**
*
* @param delay the number of ticks before the output signal
* immediate output if delay is zero
* @param dwell the number of ticks of output duration
*
*/
2015-02-12 13:04:27 -08:00
void scheduleOutput(OutputSignal *signal, efitimeus_t nowUs, float delayUs, float durationUs) {
2014-12-24 11:05:19 -08:00
#if EFI_GPIO
2015-02-12 13:04:27 -08:00
if (durationUs < 0) {
firmwareError("duration cannot be negative: %d", durationUs);
2014-08-29 07:52:33 -07:00
return;
}
2015-02-12 13:04:27 -08:00
if (cisnan(durationUs)) {
firmwareError("NaN in scheduleOutput", durationUs);
2014-08-29 07:52:33 -07:00
return;
}
2014-11-07 22:05:46 -08:00
efiAssertVoid(signal!=NULL, "signal is NULL");
2014-08-29 07:52:33 -07:00
int index = getRevolutionCounter() % 2;
scheduling_s * sUp = &signal->signalTimerUp[index];
scheduling_s * sDown = &signal->signalTimerDown[index];
2015-02-12 13:04:27 -08:00
scheduleByTime("out up", sUp, nowUs + (int) delayUs, (schfunc_t) &turnPinHigh, signal->output);
scheduleByTime("out down", sDown, nowUs + (int) (delayUs + durationUs), (schfunc_t) &turnPinLow, signal->output);
2014-12-24 11:05:19 -08:00
#endif
2014-08-29 07:52:33 -07:00
}