Skip to content

Commit

Permalink
MC33810 initialisation is atomic.
Browse files Browse the repository at this point in the history
And MC33810 code is now fully encapsulated.
  • Loading branch information
adbancroft committed Dec 17, 2024
1 parent 8d85c5e commit 810bc46
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 48 deletions.
17 changes: 5 additions & 12 deletions speeduino/acc_mc33810.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

#if defined(OUTPUT_CONTROL_SUPPORTED)

//These are default values for which injector is attached to which output on the IC.
//They may (Probably will) be changed during init by the board specific config in init.ino
uint8_t MC33810_BIT_INJ[INJ_CHANNELS] = { 0, 1, 2, 3, 4, 5, 6, 7 };
uint8_t MC33810_BIT_IGN[IGN_CHANNELS] = { 0, 1, 2, 3, 4, 5, 6, 7 };

// TODO: these are transient & should be removed.
byte pinMC33810_1_CS;
byte pinMC33810_2_CS;

mc33810_IC_t mc33810_1;
mc33810_IC_t mc33810_2;

Expand All @@ -19,13 +10,15 @@ mc33810_IC_t mc33810_2;
#define MC33810_2_ACTIVE() setPin_Low(mc33810_2.pin);
#define MC33810_2_INACTIVE() setPin_High(mc33810_2.pin);

void initMC33810(void)
void initMC33810(const mc33810_config_t &ic1, const mc33810_config_t &ic2)
{
//Set the output states of both ICs to be off to fuel and ignition
mc33810_1.stateBits = 0U;
mc33810_1.pin = pinToOutputPort(pinMC33810_1_CS);
mc33810_1.pin = pinToOutputPort(ic1.pin);
mc33810_1.config = ic1;
mc33810_2.stateBits = 0U;
mc33810_2.pin = pinToOutputPort(pinMC33810_2_CS);
mc33810_2.pin = pinToOutputPort(ic2.pin);
mc33810_2.config = ic2;

SPI.begin();
//These are the SPI settings per the datasheet
Expand Down
28 changes: 14 additions & 14 deletions speeduino/acc_mc33810.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#include "port_pin.h"
#include "bit_manip.h"

extern byte pinMC33810_1_CS;
extern byte pinMC33810_2_CS;

void initMC33810(void);
struct mc33810_config_t {
uint8_t pin;
uint8_t injBits[4];
uint8_t coilBits[4];
};
void initMC33810(const mc33810_config_t &ic1, const mc33810_config_t &ic2);

/// @cond
// Implementation details - ignore
Expand All @@ -23,6 +25,7 @@ void initMC33810(void);
struct mc33810_IC_t {
ioPort pin; ///< The Speeduino pin that connects to the MC33810
volatile byte stateBits; ///< The MC33810 channel states, 1 per bit (on or off)
mc33810_config_t config; ///< The config - needed for the bit indices
};

static inline void mc33810TransferState(mc33810_IC_t &mc33810) {
Expand All @@ -35,52 +38,49 @@ static inline void mc33810TransferState(mc33810_IC_t &mc33810) {

static inline mc33810_IC_t& mc33810ICFromChannel(uint8_t channelIndex) {
if (channelIndex<5) {
// The lower 4 fuel & ignition channels are assigned to IC1/pin1
// The lower 4 fuel & ignition channels are assigned to MC33810-1/pin1
extern mc33810_IC_t mc33810_1;
return mc33810_1;
} else {
// The upper 4 fuel & ignition channels are assigned to IC2/pin2
// The upper 4 fuel & ignition channels are assigned to MC33810-2/pin2
extern mc33810_IC_t mc33810_2;
return mc33810_2;
}
}

///@endcond

extern uint8_t MC33810_BIT_INJ[INJ_CHANNELS];
extern uint8_t MC33810_BIT_IGN[IGN_CHANNELS];

static inline void openInjector_MC33810(uint8_t channelIndex) {
mc33810_IC_t& ic = mc33810ICFromChannel(channelIndex);
BIT_SET(ic.stateBits, MC33810_BIT_INJ[channelIndex-1]);
BIT_SET(ic.stateBits, ic.config.injBits[(channelIndex-1U) % _countof(ic.config.injBits)]);

mc33810TransferState(ic);
}

static inline void closeInjector_MC33810(uint8_t channelIndex) {
mc33810_IC_t& ic = mc33810ICFromChannel(channelIndex);
BIT_CLEAR(ic.stateBits, MC33810_BIT_INJ[channelIndex-1]);
BIT_CLEAR(ic.stateBits, ic.config.injBits[(channelIndex-1U) % _countof(ic.config.injBits)]);

mc33810TransferState(ic);
}

static inline void toggleInjector_MC33810(uint8_t channelIndex) {
mc33810_IC_t& ic = mc33810ICFromChannel(channelIndex);
BIT_TOGGLE(ic.stateBits, MC33810_BIT_INJ[channelIndex-1]);
BIT_TOGGLE(ic.stateBits, ic.config.injBits[(channelIndex-1U) % _countof(ic.config.injBits)]);

mc33810TransferState(ic);
}

static inline void coilStartCharging_MC33810(uint8_t channelIndex) {
mc33810_IC_t& ic = mc33810ICFromChannel(channelIndex);
BIT_SET(ic.stateBits, MC33810_BIT_IGN[channelIndex-1]);
BIT_SET(ic.stateBits, ic.config.coilBits[(channelIndex-1U) % _countof(ic.config.coilBits)]);

mc33810TransferState(ic);
}

static inline void coilStopCharging_MC33810(uint8_t channelIndex) {
mc33810_IC_t& ic = mc33810ICFromChannel(channelIndex);
BIT_CLEAR(ic.stateBits, MC33810_BIT_IGN[channelIndex-1]);
BIT_CLEAR(ic.stateBits, ic.config.coilBits[(channelIndex-1U) % _countof(ic.config.coilBits)]);

mc33810TransferState(ic);
}
Expand Down
25 changes: 3 additions & 22 deletions speeduino/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1376,29 +1376,10 @@ static void setPinMappingsDropBear(void) {
pinResetControl = 49; //PLaceholder only. Cannot use 42-47 as these are the SD card
#endif

pinMC33810_1_CS = 10;
pinMC33810_2_CS = 9;

//Pin alignment to the MC33810 outputs
MC33810_BIT_INJ[0] = 3;
MC33810_BIT_INJ[1] = 1;
MC33810_BIT_INJ[2] = 0;
MC33810_BIT_INJ[3] = 2;
MC33810_BIT_INJ[0] = 4;
MC33810_BIT_INJ[1] = 5;
MC33810_BIT_INJ[2] = 6;
MC33810_BIT_INJ[3] = 7;

MC33810_BIT_INJ[4] = 3;
MC33810_BIT_INJ[5] = 1;
MC33810_BIT_INJ[6] = 0;
MC33810_BIT_INJ[7] = 2;
MC33810_BIT_INJ[4] = 4;
MC33810_BIT_INJ[5] = 5;
MC33810_BIT_INJ[6] = 6;
MC33810_BIT_INJ[7] = 7;

initMC33810();
initMC33810(
{ 10U, { 3, 1, 0, 2 }, { 3, 1, 0, 2 } },
{ 9U, { 4, 5, 6, 7 }, { 4, 5, 6, 7 } });
setInjectorControlActions({ openInjector_MC33810, closeInjector_MC33810, toggleInjector_MC33810 });
setIgnitionControlActions({ coilStartCharging_MC33810, coilStopCharging_MC33810 });
#endif
Expand Down

0 comments on commit 810bc46

Please sign in to comment.