Skip to content

Commit

Permalink
MISRA fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
adbancroft committed Dec 23, 2024
1 parent 4372edc commit c0c81b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
4 changes: 4 additions & 0 deletions speeduino/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ struct config2 {
byte crkngAddCLTAdv : 1;
byte includeAFR : 1; //< Enable AFR compensation ? (See also @ref config2.incorporateAFR)
byte hardCutType : 1;
// cppcheck-suppress misra-c2012-6.1
LoadSource ignAlgorithm : 3;
byte indInjAng : 1;
byte injOpen; ///< Injector opening time (ms * 10)
Expand All @@ -533,6 +534,7 @@ struct config2 {
byte nCylinders : 4; ///< Number of cylinders

//config2 in ini
// cppcheck-suppress misra-c2012-6.1
LoadSource fuelAlgorithm : 3;///< Fuel algorithm - 0=Manifold pressure/MAP (LOAD_SOURCE_MAP, default, proven), 1=Throttle/TPS (LOAD_SOURCE_TPS), 2=IMAP/EMAP (LOAD_SOURCE_IMAPEMAP)
byte fixAngEnable : 1; ///< Whether fixed/locked timing is enabled (0=disable, 1=enable, See @ref configPage4.FixAng)
byte nInjectors : 4; ///< Number of injectors
Expand Down Expand Up @@ -1031,6 +1033,7 @@ struct config10 {
byte knock_recoveryStep; //Byte 121

//Byte 122
// cppcheck-suppress misra-c2012-6.1
LoadSource fuel2Algorithm : 3;
byte fuel2Mode : 3;
byte fuel2SwitchVariable : 2;
Expand Down Expand Up @@ -1104,6 +1107,7 @@ struct config10 {
byte fuelTempValues[6]; //180

//Byte 186
// cppcheck-suppress misra-c2012-6.1
LoadSource spark2Algorithm : 3;
byte spark2Mode : 3;
byte spark2SwitchVariable : 2;
Expand Down
20 changes: 10 additions & 10 deletions speeduino/secondaryTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
static inline uint8_t getVE2(void)
{
currentStatus.fuelLoad2 = getLoad(configPage10.fuel2Algorithm, currentStatus);
return get3DTableValue(&fuelTable2, currentStatus.fuelLoad2, currentStatus.RPM); //Perform lookup into fuel map for RPM vs MAP value
return get3DTableValue(&fuelTable2, currentStatus.fuelLoad2, (table3d_axis_t)currentStatus.RPM); //Perform lookup into fuel map for RPM vs MAP value
}

static inline bool fuelModeCondSwitchRpmActive(void) {
Expand Down Expand Up @@ -60,16 +60,16 @@ void calculateSecondaryFuel(void)
currentStatus.VE2 = getVE2();
BIT_SET(currentStatus.status3, BIT_STATUS3_FUEL2_ACTIVE); //Set the bit indicating that the 2nd fuel table is in use.
//Fuel 2 table is treated as a % value. Table 1 and 2 are multiplied together and divided by 100
uint16_t combinedVE = percentage(currentStatus.VE2, (uint16_t)currentStatus.VE1);
currentStatus.VE = min(MAX_VE, combinedVE);
uint16_t combinedVE = div100((uint16_t)currentStatus.VE2 * (uint16_t)currentStatus.VE1);
currentStatus.VE = (uint8_t)min(MAX_VE, combinedVE);
}
else if(configPage10.fuel2Mode == FUEL2_MODE_ADD)
{
currentStatus.VE2 = getVE2();
BIT_SET(currentStatus.status3, BIT_STATUS3_FUEL2_ACTIVE); //Set the bit indicating that the 2nd fuel table is in use.
//Fuel tables are added together, but a check is made to make sure this won't overflow the 8-bit VE value
uint16_t combinedVE = (uint16_t)currentStatus.VE1 + (uint16_t)currentStatus.VE2;
currentStatus.VE = min(MAX_VE, combinedVE);
currentStatus.VE = (uint8_t)min(MAX_VE, combinedVE);
}
else if(fuelModeCondSwitchActive() || fuelModeInputSwitchActive())
{
Expand All @@ -91,7 +91,7 @@ static constexpr int16_t MIN_ADVANCE = INT8_MIN; // out-of-line constant require

static inline uint8_t lookupSpark2(void) {
currentStatus.ignLoad2 = getLoad(configPage10.spark2Algorithm, currentStatus);
return get3DTableValue(&ignitionTable2, currentStatus.ignLoad2, currentStatus.RPM);
return get3DTableValue(&ignitionTable2, currentStatus.ignLoad2, (table3d_axis_t)currentStatus.RPM);
}

/**
Expand All @@ -101,9 +101,9 @@ static inline uint8_t lookupSpark2(void) {
*/
static inline int8_t getAdvance2(void)
{
int16_t advance2 = (int16_t)lookupSpark2() - INT16_C(OFFSET_IGNITION);
int16_t lookUpResult = (int16_t)lookupSpark2() - INT16_C(OFFSET_IGNITION);
// Clamp to return type range.
advance2 = constrain(advance2, MIN_ADVANCE, MAX_ADVANCE);
int8_t advance2 = (int8_t)constrain(lookUpResult, MIN_ADVANCE, MAX_ADVANCE);
#if !defined(UNIT_TEST)
//Perform the corrections calculation on the secondary advance value, only if it uses a switched mode
if( (configPage10.spark2SwitchVariable == SPARK2_MODE_CONDITIONAL_SWITCH) || (configPage10.spark2SwitchVariable == SPARK2_MODE_INPUT_SWITCH) ) {
Expand Down Expand Up @@ -164,9 +164,9 @@ void calculateSecondarySpark(void)
BIT_SET(currentStatus.status5, BIT_STATUS5_SPARK2_ACTIVE);
uint8_t spark2Percent = lookupSpark2();
//Spark 2 table is treated as a % value. Table 1 and 2 are multiplied together and divided by 100
int16_t combinedAdvance = div100((int16_t)((int16_t)spark2Percent * (int16_t)currentStatus.advance1));
int16_t combinedAdvance = div100((int16_t)spark2Percent * (int16_t)currentStatus.advance1);
//make sure we don't overflow and accidentally set negative timing: currentStatus.advance can only hold a signed 8 bit value
currentStatus.advance = (int8_t)min((int16_t)MAX_ADVANCE, combinedAdvance);
currentStatus.advance = (int8_t)min(MAX_ADVANCE, combinedAdvance);

// This is informational only, but the value needs corrected into the int8_t range
currentStatus.advance2 = (int8_t)max(MIN_ADVANCE, (int16_t)((int16_t)spark2Percent-INT16_C(OFFSET_IGNITION)));
Expand All @@ -177,7 +177,7 @@ void calculateSecondarySpark(void)
currentStatus.advance2 = getAdvance2();
//Spark tables are added together, but a check is made to make sure this won't overflow the 8-bit VE value
int16_t combinedAdvance = (int16_t)currentStatus.advance1 + (int16_t)currentStatus.advance2;
currentStatus.advance = min(MAX_ADVANCE, combinedAdvance);
currentStatus.advance = (int8_t)min(MAX_ADVANCE, combinedAdvance);
}
else if(sparkModeCondSwitchActive() || sparkModeInputSwitchActive())
{
Expand Down
8 changes: 8 additions & 0 deletions speeduino/statuses.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,22 @@ using byte = uint8_t;
* unit based values in similar variable(s) without ADC part in name (see sensors.ino for reading of sensors).
*/
struct statuses {
// cppcheck-suppress misra-c2012-6.1 ; False positive - MISRA C:2012 Rule (R 6.1) permits the use of boolean for bit fields.
volatile bool hasSync : 1; /**< Flag for crank/cam position being known by decoders (See decoders.ino).
This is used for sanity checking e.g. before logging tooth history or reading some sensors and computing readings. */
// cppcheck-suppress misra-c2012-6.1
bool initialisationComplete : 1; //Tracks whether the setup() function has run completely
// cppcheck-suppress misra-c2012-6.1
bool clutchTrigger : 1;
// cppcheck-suppress misra-c2012-6.1
bool previousClutchTrigger : 1;
// cppcheck-suppress misra-c2012-6.1
volatile bool fpPrimed : 1; //Tracks whether or not the fuel pump priming has been completed yet
// cppcheck-suppress misra-c2012-6.1
volatile bool injPrimed : 1; //Tracks whether or not the injector priming has been completed yet
// cppcheck-suppress misra-c2012-6.1
volatile bool tachoSweepEnabled : 1;
// cppcheck-suppress misra-c2012-6.1
volatile bool tachoAlt : 1;

uint16_t RPM; ///< RPM - Current Revs per minute
Expand Down

0 comments on commit c0c81b3

Please sign in to comment.