Skip to content

Commit

Permalink
Inject the coil & injector control functions into the scheduled I/O m…
Browse files Browse the repository at this point in the history
…odule. Much cleaner than the module trying to figure it out itself.

Costs 14 bytes RAM
  • Loading branch information
adbancroft committed Dec 17, 2024
1 parent ff222d5 commit 8d85c5e
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 99 deletions.
49 changes: 49 additions & 0 deletions speeduino/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "corrections.h"
#include "idle.h"
#include "table2d.h"
#include "scheduledIO_direct.h"
#include "acc_mc33810.h"
#include "board_definition.h"
#include "unit_testing.h"
Expand Down Expand Up @@ -316,6 +317,12 @@ void initialiseAll(void)
digitalWrite(LED_BUILTIN, HIGH);
}

// This saves a couple of bytes of RAM versus using static constexpr statements
static inline void useDirectChannelControl(void) {
setInjectorControlActions({ openInjector_DIRECT, closeInjector_DIRECT, toggleInjector_DIRECT });
setIgnitionControlActions({ coilStartCharging_DIRECT, coilStopCharging_DIRECT });
}

#ifndef SMALL_FLASH_MODE //No support for bluepill here anyway

static void setPinMappingsV2_0Shield(void) {
Expand Down Expand Up @@ -346,6 +353,8 @@ static void setPinMappingsV2_0Shield(void) {
pinFuelPump = 4; //Fuel pump output
pinFlex = 2; // Flex sensor (Must be external interrupt enabled)
pinResetControl = 43; //Reset control output

useDirectChannelControl();
}

static void setPinMappingsV3_0Shield(void) {
Expand Down Expand Up @@ -397,6 +406,8 @@ static void setPinMappingsV3_0Shield(void) {
pinFan = 27;
pinO2 = A22;
#endif

useDirectChannelControl();
}

static void setPinMappingsMx5Nb2Shield(void) {
Expand Down Expand Up @@ -450,6 +461,8 @@ static void setPinMappingsMx5Nb2Shield(void) {
pinFan = 50; //Won't work (No mapping for pin 35)
pinTachOut = 28; //Done
#endif

useDirectChannelControl();
}

static void setPinMappingsMx5Na18Shield(void) {
Expand Down Expand Up @@ -500,6 +513,8 @@ static void setPinMappingsMx5Na18Shield(void) {
pinFan = 50; //Won't work (No mapping for pin 35)
pinTachOut = 28; //Done
#endif

useDirectChannelControl();
}

static void setPinMappingsMx5Na16Shield(void) {
Expand Down Expand Up @@ -552,6 +567,8 @@ static void setPinMappingsMx5Na16Shield(void) {
pinFan = 50; //Won't work (No mapping for pin 35)
pinTachOut = 28; //Done
#endif

useDirectChannelControl();
}

static void setPinMappingsTurtanasPcb(void) {
Expand Down Expand Up @@ -586,6 +603,8 @@ static void setPinMappingsTurtanasPcb(void) {
pinTachOut = 49; //Tacho output pin
pinFlex = 2; // Flex sensor (Must be external interrupt enabled)
pinResetControl = 26; //Reset control

useDirectChannelControl();
}

static void setPinMappingsDazV6Shield(void) {
Expand Down Expand Up @@ -627,6 +646,8 @@ static void setPinMappingsDazV6Shield(void) {
pinSpareLOut5 = 53;
#endif
pinFan = 47; //Pin for the fan output

useDirectChannelControl();
}

static void setPinMappingsNO2CShield(void) {
Expand Down Expand Up @@ -672,6 +693,8 @@ static void setPinMappingsNO2CShield(void) {
pinSpareLOut3 = 36; //low current output spare3 - ONLY WITH DB
#endif
pinResetControl = 26; //Reset control output

useDirectChannelControl();
}

static void setPinMappingsUA4CShield(void) {
Expand Down Expand Up @@ -718,6 +741,8 @@ static void setPinMappingsUA4CShield(void) {
#endif
pinFan = 24; //Pin for the fan output
pinResetControl = 46; //Reset control output PLACEHOLDER value for now

useDirectChannelControl();
}

static void setPinMappingsBlitzboxBL49sp(void) {
Expand Down Expand Up @@ -756,6 +781,8 @@ static void setPinMappingsBlitzboxBL49sp(void) {
#endif
pinFan = 12; //Pin for the fan output
pinResetControl = 46; //Reset control output PLACEHOLDER value for now

useDirectChannelControl();
}

static void setPinMappingsDiyEfiCore4Shield(void) {
Expand Down Expand Up @@ -803,6 +830,8 @@ static void setPinMappingsDiyEfiCore4Shield(void) {
pinFan = 40; //Pin for the fan output
pinResetControl = 46; //Reset control output PLACEHOLDER value for now
#endif // CORE_AVR

useDirectChannelControl();
}

static void setPinMappingsPlazomatV0_1Shield(void) {
Expand Down Expand Up @@ -839,6 +868,8 @@ static void setPinMappingsPlazomatV0_1Shield(void) {
pinTachOut = 49; //Tacho output pin
pinResetControl = 26; //Reset control output
#endif // CORE_AVR

useDirectChannelControl();
}

#endif // SMALL_FLASH_MODE
Expand Down Expand Up @@ -1059,6 +1090,8 @@ static void setPinMappingsV4_0Shield(void) {
pinTrigger = PC13; //The CAS pin also led pin so bad idea
pinTrigger2 = PC15; //The Cam Sensor pin
#endif

useDirectChannelControl();
}

static void setPinMappingBmwPnP(void) {
Expand Down Expand Up @@ -1143,6 +1176,8 @@ static void setPinMappingBmwPnP(void) {
pinIdleUp = PE14; //(placeholder)
pinCTPS = PA6; //(placeholder)
#endif

useDirectChannelControl();
}

static void setPinMappingsTeensyRevA(void) {
Expand Down Expand Up @@ -1179,6 +1214,8 @@ static void setPinMappingsTeensyRevA(void) {
pinSpareLOut1 = 21; //low current output spare1
#endif
#endif

useDirectChannelControl();
}

static void setPinMappingsTeensyRevB(void) {
Expand Down Expand Up @@ -1215,6 +1252,8 @@ static void setPinMappingsTeensyRevB(void) {
pinSpareLOut1 = 21; //low current output spare1
#endif
#endif

useDirectChannelControl();
}


Expand Down Expand Up @@ -1252,6 +1291,8 @@ static void setPinMappingsJuiceBox(void) {
pinSpareLOut1 = 55; //low current output spare1 - NOT USED
#endif
#endif

useDirectChannelControl();
}

static void setPinMappingsDropBear(void) {
Expand Down Expand Up @@ -1356,6 +1397,10 @@ static void setPinMappingsDropBear(void) {
MC33810_BIT_INJ[5] = 5;
MC33810_BIT_INJ[6] = 6;
MC33810_BIT_INJ[7] = 7;

initMC33810();
setInjectorControlActions({ openInjector_MC33810, closeInjector_MC33810, toggleInjector_MC33810 });
setIgnitionControlActions({ coilStartCharging_MC33810, coilStopCharging_MC33810 });
#endif
}

Expand Down Expand Up @@ -1400,6 +1445,8 @@ static void setPinMappingBearCub(void) {
pinFan = 25; //Pin for the fan output
pinResetControl = 46; //Reset control output PLACEHOLDER value for now
#endif

useDirectChannelControl();
}

static void setPinMappingsSTM32(void) {
Expand Down Expand Up @@ -1580,6 +1627,8 @@ static void setPinMappingsSTM32(void) {
pinTrigger = PA10; //The CAS pin
pinTrigger2 = PA13; //The Cam Sensor pin
#endif

useDirectChannelControl();
}

/** Set board / microcontroller specific pin mappings / assignments.
Expand Down
59 changes: 23 additions & 36 deletions speeduino/scheduledIO.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,41 @@
#include "scheduledIO.h"

#include "scheduledIO_direct.h"
/** @file
* Injector and Coil (toggle/open/close) control (under various situations, eg with particular cylinder count, rotary engine type or wasted spark ign, etc.).
* Also accounts for presence of MC33810 injector/ignition (dwell, etc.) control circuit.
* Functions here are typically assigned (at initialisation) to callback function variables (e.g. inj1StartFunction or inj1EndFunction)
* form where they are called (by scheduler.ino).
*/

#if defined(OUTPUT_CONTROL_SUPPORTED)
ScheduleOutputControl ignitionOutputControl;
#endif

#if defined(OUTPUT_CONTROL_SUPPORTED)
ScheduleOutputControl injectorOutputControl;
#endif

static inline ioPort registerIOPin(uint8_t pin, ScheduleOutputControl ioType) {
if(ioType == OUTPUT_CONTROL_DIRECT) {
return pinToOutputPort(pin);
} else {
return nullIoPort();
}
}

static inline void registerPins(ioPort toRegister[], uint8_t toRegisterSize, const uint8_t pins[], ScheduleOutputControl ioType) {
static inline void registerPins(ioPort toRegister[], uint8_t toRegisterSize, const uint8_t pins[]) {
for (uint8_t index=0U; index<toRegisterSize; ++index) {
toRegister[index] = registerIOPin(pins[index], ioType);
}
#if defined(OUTPUT_CONTROL_SUPPORTED)
if (ioType==OUTPUT_CONTROL_MC33810) {
initMC33810();
toRegister[index] = pinToOutputPort(pins[index]);
}
#endif
}

void initialiseInjectorPins(const uint8_t pins[]) {
#if defined(OUTPUT_CONTROL_SUPPORTED)
injectorOutputControl = isValidPin(pinMC33810_1_CS) ? OUTPUT_CONTROL_MC33810 : OUTPUT_CONTROL_DIRECT;
registerPins(injectorPins, _countof(injectorPins), pins, injectorOutputControl);
#else
registerPins(injectorPins, _countof(injectorPins), pins, OUTPUT_CONTROL_DIRECT);
#endif
registerPins(injectorPins, _countof(injectorPins), pins);
}

void initialiseIgnitionPins(const uint8_t pins[]) {
#if defined(OUTPUT_CONTROL_SUPPORTED)
ignitionOutputControl = isValidPin(pinMC33810_2_CS) ? OUTPUT_CONTROL_MC33810 : OUTPUT_CONTROL_DIRECT;
registerPins(ignitionPins, _countof(ignitionPins), pins, ignitionOutputControl);
#else
registerPins(ignitionPins, _countof(ignitionPins), pins, OUTPUT_CONTROL_DIRECT);
#endif
registerPins(ignitionPins, _countof(ignitionPins), pins);
}

static inline void nullAction(uint8_t index) {
UNUSED(index);
// Do nothing
}

// cppcheck-suppress misra-c2012-8.4
injector_control_t injectorControl = { nullAction, nullAction, nullAction };

void setInjectorControlActions(const injector_control_t &control) {
injectorControl = control;
}

// cppcheck-suppress misra-c2012-8.4
coil_control_t coilControl = { nullAction, nullAction };

void setIgnitionControlActions(const coil_control_t &control) {
coilControl = control;
}
Loading

0 comments on commit 8d85c5e

Please sign in to comment.