Skip to content

Commit

Permalink
Direct coil/injector control initialised by each board.
Browse files Browse the repository at this point in the history
Direct I/O now all encapsulated
  • Loading branch information
adbancroft committed Dec 17, 2024
1 parent 810bc46 commit cd15bda
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 92 deletions.
4 changes: 2 additions & 2 deletions speeduino/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ bool pinIsOutput(byte pin)
bool isIdlePWM = (configPage6.iacAlgorithm > 0) && ((configPage6.iacAlgorithm <= 3) || (configPage6.iacAlgorithm == 6));
bool isIdleSteper = (configPage6.iacAlgorithm > 3) && (configPage6.iacAlgorithm != 6);
//Injector?
for (uint8_t index=0U; index<_countof(injectorPins); ++index) {
for (uint8_t index=0U; index<_countof(pinInjectors); ++index) {
used = used || (pin == pinInjectors[index]);
}
//Ignition?
for (uint8_t index=0U; index<_countof(ignitionPins); ++index) {
for (uint8_t index=0U; index<_countof(pinCoils); ++index) {
used = used || (pin == pinCoils[index]);
}

Expand Down
46 changes: 24 additions & 22 deletions speeduino/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ void initialiseAll(void)
currentLoopTime = micros_safe();
mainLoopCount = 0;

initialiseFuelSchedulers(pinInjectors);
initialiseIgnitionSchedulers(pinCoils);
initialiseFuelSchedulers();
initialiseIgnitionSchedulers();

//Begin priming the fuel pump. This is turned off in the low resolution, 1s interrupt in timers.ino
//First check that the priming time is not 0
Expand All @@ -318,9 +318,11 @@ void initialiseAll(void)
}

// This saves a couple of bytes of RAM versus using static constexpr statements
static inline void useDirectChannelControl(void) {
static inline void useDirectChannelControl(const uint8_t injPins[], const uint8_t ignPins[]) {
setInjectorControlActions({ openInjector_DIRECT, closeInjector_DIRECT, toggleInjector_DIRECT });
setIgnitionControlActions({ coilStartCharging_DIRECT, coilStopCharging_DIRECT });
initialiseInjectorPins_DIRECT(injPins);
initialiseIgnitionPins_DIRECT(ignPins);
}

#ifndef SMALL_FLASH_MODE //No support for bluepill here anyway
Expand Down Expand Up @@ -354,7 +356,7 @@ static void setPinMappingsV2_0Shield(void) {
pinFlex = 2; // Flex sensor (Must be external interrupt enabled)
pinResetControl = 43; //Reset control output

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsV3_0Shield(void) {
Expand Down Expand Up @@ -407,7 +409,7 @@ static void setPinMappingsV3_0Shield(void) {
pinO2 = A22;
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsMx5Nb2Shield(void) {
Expand Down Expand Up @@ -462,7 +464,7 @@ static void setPinMappingsMx5Nb2Shield(void) {
pinTachOut = 28; //Done
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsMx5Na18Shield(void) {
Expand Down Expand Up @@ -514,7 +516,7 @@ static void setPinMappingsMx5Na18Shield(void) {
pinTachOut = 28; //Done
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsMx5Na16Shield(void) {
Expand Down Expand Up @@ -568,7 +570,7 @@ static void setPinMappingsMx5Na16Shield(void) {
pinTachOut = 28; //Done
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsTurtanasPcb(void) {
Expand Down Expand Up @@ -604,7 +606,7 @@ static void setPinMappingsTurtanasPcb(void) {
pinFlex = 2; // Flex sensor (Must be external interrupt enabled)
pinResetControl = 26; //Reset control

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsDazV6Shield(void) {
Expand Down Expand Up @@ -647,7 +649,7 @@ static void setPinMappingsDazV6Shield(void) {
#endif
pinFan = 47; //Pin for the fan output

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsNO2CShield(void) {
Expand Down Expand Up @@ -694,7 +696,7 @@ static void setPinMappingsNO2CShield(void) {
#endif
pinResetControl = 26; //Reset control output

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsUA4CShield(void) {
Expand Down Expand Up @@ -742,7 +744,7 @@ static void setPinMappingsUA4CShield(void) {
pinFan = 24; //Pin for the fan output
pinResetControl = 46; //Reset control output PLACEHOLDER value for now

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsBlitzboxBL49sp(void) {
Expand Down Expand Up @@ -782,7 +784,7 @@ static void setPinMappingsBlitzboxBL49sp(void) {
pinFan = 12; //Pin for the fan output
pinResetControl = 46; //Reset control output PLACEHOLDER value for now

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsDiyEfiCore4Shield(void) {
Expand Down Expand Up @@ -831,7 +833,7 @@ static void setPinMappingsDiyEfiCore4Shield(void) {
pinResetControl = 46; //Reset control output PLACEHOLDER value for now
#endif // CORE_AVR

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsPlazomatV0_1Shield(void) {
Expand Down Expand Up @@ -869,7 +871,7 @@ static void setPinMappingsPlazomatV0_1Shield(void) {
pinResetControl = 26; //Reset control output
#endif // CORE_AVR

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

#endif // SMALL_FLASH_MODE
Expand Down Expand Up @@ -1091,7 +1093,7 @@ static void setPinMappingsV4_0Shield(void) {
pinTrigger2 = PC15; //The Cam Sensor pin
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingBmwPnP(void) {
Expand Down Expand Up @@ -1177,7 +1179,7 @@ static void setPinMappingBmwPnP(void) {
pinCTPS = PA6; //(placeholder)
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsTeensyRevA(void) {
Expand Down Expand Up @@ -1215,7 +1217,7 @@ static void setPinMappingsTeensyRevA(void) {
#endif
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsTeensyRevB(void) {
Expand Down Expand Up @@ -1253,7 +1255,7 @@ static void setPinMappingsTeensyRevB(void) {
#endif
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}


Expand Down Expand Up @@ -1292,7 +1294,7 @@ static void setPinMappingsJuiceBox(void) {
#endif
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsDropBear(void) {
Expand Down Expand Up @@ -1427,7 +1429,7 @@ static void setPinMappingBearCub(void) {
pinResetControl = 46; //Reset control output PLACEHOLDER value for now
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

static void setPinMappingsSTM32(void) {
Expand Down Expand Up @@ -1609,7 +1611,7 @@ static void setPinMappingsSTM32(void) {
pinTrigger2 = PA13; //The Cam Sensor pin
#endif

useDirectChannelControl();
useDirectChannelControl(pinInjectors, pinCoils);
}

/** Set board / microcontroller specific pin mappings / assignments.
Expand Down
16 changes: 1 addition & 15 deletions speeduino/scheduledIO.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
#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).
*/

static inline void registerPins(ioPort toRegister[], uint8_t toRegisterSize, const uint8_t pins[]) {
for (uint8_t index=0U; index<toRegisterSize; ++index) {
toRegister[index] = pinToOutputPort(pins[index]);
}
}

void initialiseInjectorPins(const uint8_t pins[]) {
registerPins(injectorPins, _countof(injectorPins), pins);
}

void initialiseIgnitionPins(const uint8_t pins[]) {
registerPins(ignitionPins, _countof(ignitionPins), pins);
}

static inline void nullAction(uint8_t index) {
UNUSED(index);
// Do nothing
Expand Down
22 changes: 4 additions & 18 deletions speeduino/scheduledIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,12 @@ struct injector_control_t {
*/
void setInjectorControlActions(const injector_control_t &control);

/**
* @brief Setup the injector (fuel) pins and output control method
*
* @param pins Pin numbers in cylinder order. *Assumption is that the array has at least IGN_CHANNELS elements*
*/
void initialiseInjectorPins(const uint8_t pins[]);

/**
* @brief Open an injector
*
* @param channelIndex **One** based index of the injector. I.e. the injector channel number
*/
static inline void openInjector(uint8_t channelIndex) {
static CRITICAL_INLINE void openInjector(uint8_t channelIndex) {
extern injector_control_t injectorControl;
injectorControl.openInjector(channelIndex);
if (channelIndex-1U<=BIT_STATUS1_INJ4) {
Expand All @@ -57,7 +50,7 @@ static inline void openInjector(uint8_t channelIndex) {
*
* @param channelIndex **One** based index of the injector. I.e. the injector channel number
*/
static inline void closeInjector(uint8_t channelIndex) {
static CRITICAL_INLINE void closeInjector(uint8_t channelIndex) {
extern injector_control_t injectorControl;
injectorControl.closeInjector(channelIndex);
if (channelIndex-1U<=BIT_STATUS1_INJ4) {
Expand Down Expand Up @@ -98,19 +91,12 @@ struct coil_control_t {
*/
void setIgnitionControlActions(const coil_control_t &control);

/**
* @brief Setup the ignition (coil) pins and output control method
*
* @param pins Pin numbers in cylinder order. *Assumption is that the array has at least IGN_CHANNELS elements*
*/
void initialiseIgnitionPins(const uint8_t pins[]);

/**
* @brief Start charging a coil
*
* @param channelIndex **One** based index of the coil. I.e. the coil channel number
*/
static inline void beginCoilCharge(uint8_t channelIndex) {
static CRITICAL_INLINE void beginCoilCharge(uint8_t channelIndex) {
extern coil_control_t coilControl;
coilControl.beginCoilCharge(channelIndex);
if(configPage6.tachoMode) {
Expand All @@ -125,7 +111,7 @@ static inline void beginCoilCharge(uint8_t channelIndex) {
*
* @param channelIndex **One** based index of the coil. I.e. the coil channel number
*/
static inline void endCoilCharge(uint8_t channelIndex) {
static CRITICAL_INLINE void endCoilCharge(uint8_t channelIndex) {
extern coil_control_t coilControl;
coilControl.endCoilCharge(channelIndex);
if(configPage6.tachoMode) {
Expand Down
17 changes: 16 additions & 1 deletion speeduino/scheduledIO_direct.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
#include "scheduledIO_direct.h"

static inline void registerPins(ioPort toRegister[], uint8_t toRegisterSize, const uint8_t pins[]) {
for (uint8_t index=0U; index<toRegisterSize; ++index) {
toRegister[index] = pinToOutputPort(pins[index]);
}
}

// cppcheck-suppress misra-c2012-8.4
ioPort injectorPins[INJ_CHANNELS];

void initialiseInjectorPins_DIRECT(const uint8_t pins[]) {
registerPins(injectorPins, _countof(injectorPins), pins);
}

// cppcheck-suppress misra-c2012-8.4
ioPort ignitionPins[IGN_CHANNELS];
ioPort ignitionPins[IGN_CHANNELS];

void initialiseIgnitionPins_DIRECT(const uint8_t pins[]) {
registerPins(ignitionPins, _countof(ignitionPins), pins);
}
25 changes: 18 additions & 7 deletions speeduino/scheduledIO_direct.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@
#include "globals.h"
#include "port_pin.h"

//These are for the direct port manipulation of the injectors, coils
extern ioPort injectorPins[INJ_CHANNELS];
extern ioPort ignitionPins[IGN_CHANNELS];

//Functions are used to define how each injector control system functions. These are then called by the master openInjectx() function.
//The DIRECT functions (ie individual pins) are defined below. Others should be defined in their relevant acc_x.h file
/**
* @brief Setup the injector (fuel) pins
*
* @param pins Pin numbers in cylinder order. *Assumption is that the array has at least IGN_CHANNELS elements*
*/
void initialiseInjectorPins_DIRECT(const uint8_t pins[]);

static inline void openInjector_DIRECT(uint8_t channelIndex) { setPin_High(injectorPins[channelIndex-1U]); }
static inline void closeInjector_DIRECT(uint8_t channelIndex) { setPin_Low(injectorPins[channelIndex-1U]); }
static inline void toggleInjector_DIRECT(uint8_t channelIndex) { togglePin(injectorPins[channelIndex-1U]); }
static inline void openInjector_DIRECT(uint8_t channelIndex) { extern ioPort injectorPins[INJ_CHANNELS]; setPin_High(injectorPins[channelIndex-1U]); }
static inline void closeInjector_DIRECT(uint8_t channelIndex) { extern ioPort injectorPins[INJ_CHANNELS]; setPin_Low(injectorPins[channelIndex-1U]); }
static inline void toggleInjector_DIRECT(uint8_t channelIndex) { extern ioPort injectorPins[INJ_CHANNELS]; togglePin(injectorPins[channelIndex-1U]); }

// ======================== Ignition ========================

/**
* @brief Setup the ignition (coil) pins
*
* @param pins Pin numbers in cylinder order. *Assumption is that the array has at least IGN_CHANNELS elements*
*/
void initialiseIgnitionPins_DIRECT(const uint8_t pins[]);

static inline void coilStartCharging_DIRECT(uint8_t channelIndex) {
extern ioPort ignitionPins[IGN_CHANNELS];
setPinState(ignitionPins[channelIndex-1U], (configPage4.IgInv == GOING_HIGH ? LOW : HIGH));
}
static inline void coilStopCharging_DIRECT(uint8_t channelIndex) {
extern ioPort ignitionPins[IGN_CHANNELS];
setPinState(ignitionPins[channelIndex-1U], (configPage4.IgInv == GOING_HIGH ? HIGH : LOW));
}
6 changes: 2 additions & 4 deletions speeduino/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,11 @@ static void initialiseFuelContext(void)
currentStatus.status3 |= currentStatus.nSquirts << BIT_STATUS3_NSQUIRTS1; //Top 3 bits of the status3 variable are the number of squirts. This must be done after the above section due to nSquirts being forced to 1 for sequential
}

void initialiseFuelSchedulers(const uint8_t pins[])
void initialiseFuelSchedulers(void)
{
turnOffInjectors();

resetFuelSchedulers();
initialiseInjectorPins(pins);
initialiseFuelContext();
setFuelChannelAngles();
initialiseStagedInjection();
Expand Down Expand Up @@ -683,12 +682,11 @@ static void initialiseIgnitionContext(void)
CRANK_ANGLE_MAX_IGN = configPage4.sparkMode == IGN_MODE_SEQUENTIAL ? 720 : 360;
}

void initialiseIgnitionSchedulers(const uint8_t pins[])
void initialiseIgnitionSchedulers(void)
{
turnOffCoils();

resetIgnitionSchedulers();
initialiseIgnitionPins(pins);
initialiseIgnitionContext();
setIgnitionChannelAngles();
setIgnitionScheduleCallbacks(configPage4.sparkMode);
Expand Down
Loading

0 comments on commit cd15bda

Please sign in to comment.