Skip to content

Commit c558931

Browse files
committed
Move setIgnitionChannels() into scheduler.*
1 parent c1d8d66 commit c558931

File tree

4 files changed

+108
-66
lines changed

4 files changed

+108
-66
lines changed

speeduino/scheduler.cpp

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,13 @@ void disableIgnSchedule(uint8_t channel)
465465

466466
void disableAllFuelSchedules(void)
467467
{
468-
for (uint8_t index=0; index<INJ_CHANNELS; ++index) {
468+
for (uint8_t index=0; index<(uint8_t)INJ_CHANNELS; ++index) {
469469
disableFuelSchedule(index);
470470
}
471471
}
472472
void disableAllIgnSchedules(void)
473473
{
474-
for (uint8_t index=0; index<IGN_CHANNELS; ++index) {
474+
for (uint8_t index=0; index<(uint8_t)IGN_CHANNELS; ++index) {
475475
disableIgnSchedule(index);
476476
}
477477
}
@@ -836,7 +836,7 @@ static inline void calculateNonRotaryIgnitionAngles(uint16_t dwellAngle, const s
836836
* both start and end angles are calculated for each channel.
837837
* Also the mode of ignition firing - wasted spark vs. dedicated spark per cyl. - is considered here.
838838
*/
839-
void __attribute__((flatten)) calculateIgnitionAngles(const config2 &page2, const config4 &page4, const decoder_status_t &decoderStatus, statuses &current)
839+
void calculateIgnitionAngles(const config2 &page2, const config4 &page4, const decoder_status_t &decoderStatus, statuses &current)
840840
{
841841
matchIgnitionSchedulersToSyncState(page2, page4, decoderStatus, current);
842842

@@ -851,3 +851,69 @@ void __attribute__((flatten)) calculateIgnitionAngles(const config2 &page2, cons
851851
calculateNonRotaryIgnitionAngles(dwellAngle, current);
852852
}
853853
}
854+
855+
static inline __attribute__((always_inline)) void setIgnitionChannel(IgnitionSchedule &schedule, uint16_t crankAngle, uint16_t dwellTime, byte channelMask, uint8_t channelIdx)
856+
{
857+
if (BIT_CHECK(channelMask, (channelIdx)-1U)) {
858+
setIgnitionSchedule(schedule, crankAngle, dwellTime);
859+
} else {
860+
disableSchedule(schedule);
861+
}
862+
}
863+
864+
void setIgnitionChannels(const statuses &current, uint16_t crankAngle, uint16_t dwellTime, byte channelMask) {
865+
#define SET_IGNITION_CHANNEL(channelIdx) setIgnitionChannel(ignitionSchedule ##channelIdx, crankAngle, dwellTime, channelMask, channelIdx);
866+
867+
switch (current.maxIgnOutputs)
868+
{
869+
case 8:
870+
#if IGN_CHANNELS >= 8
871+
SET_IGNITION_CHANNEL(8)
872+
#endif
873+
[[gnu::fallthrough]];
874+
//cppcheck-suppress misra-c2012-16.3
875+
case 7:
876+
#if IGN_CHANNELS >= 7
877+
SET_IGNITION_CHANNEL(7)
878+
#endif
879+
[[gnu::fallthrough]];
880+
//cppcheck-suppress misra-c2012-16.3
881+
case 6:
882+
#if IGN_CHANNELS >= 6
883+
SET_IGNITION_CHANNEL(6)
884+
#endif
885+
[[gnu::fallthrough]];
886+
//cppcheck-suppress misra-c2012-16.3
887+
case 5:
888+
#if IGN_CHANNELS >= 5
889+
SET_IGNITION_CHANNEL(5)
890+
#endif
891+
[[gnu::fallthrough]];
892+
//cppcheck-suppress misra-c2012-16.3
893+
case 4:
894+
#if IGN_CHANNELS >= 4
895+
SET_IGNITION_CHANNEL(4)
896+
#endif
897+
[[gnu::fallthrough]];
898+
//cppcheck-suppress misra-c2012-16.3
899+
case 3:
900+
#if IGN_CHANNELS >= 3
901+
SET_IGNITION_CHANNEL(3)
902+
#endif
903+
[[gnu::fallthrough]];
904+
//cppcheck-suppress misra-c2012-16.3
905+
case 2:
906+
#if IGN_CHANNELS >= 2
907+
SET_IGNITION_CHANNEL(2)
908+
#endif
909+
[[gnu::fallthrough]];
910+
//cppcheck-suppress misra-c2012-16.3
911+
case 1:
912+
SET_IGNITION_CHANNEL(1)
913+
break;
914+
default:
915+
break;
916+
}
917+
918+
#undef SET_IGNITION_CHANNEL
919+
}

speeduino/scheduler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,6 @@ void changeHalfToFullSync(const config2 &page2, statuses &current);
321321
void changeFullToHalfSync(const config2 &page2, const config4 &page4, statuses &current);
322322

323323
void calculateIgnitionAngles(const config2 &page2, const config4 &page4, const decoder_status_t &decoderStatus, statuses &current);
324+
void setIgnitionChannels(const statuses &current, uint16_t crankAngle, uint16_t dwellTime, byte channelMask);
324325

325326
#endif // SCHEDULER_H

speeduino/speeduino.ino

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -156,60 +156,6 @@ static inline void setFuelSchedules(const statuses &current, const uint16_t (&in
156156
#undef SET_FUEL_CHANNEL
157157
}
158158

159-
static inline __attribute__((always_inline)) void setIgnitionChannel(IgnitionSchedule &schedule, uint8_t channel, uint16_t crankAngle, uint16_t dwell) {
160-
if ((currentStatus.maxIgnOutputs >= channel) && BIT_CHECK(ignitionChannelsOn, channel-1U)) {
161-
setIgnitionSchedule(schedule, crankAngle, dwell);
162-
}
163-
}
164-
165-
static inline __attribute__((always_inline)) void setIgnitionChannels(uint16_t crankAngle, uint16_t dwell) {
166-
#define SET_IGNITION_CHANNEL(channelIdx) \
167-
setIgnitionChannel(ignitionSchedule ##channelIdx, UINT8_C((channelIdx)), crankAngle, dwell);
168-
169-
#if IGN_CHANNELS >= 1
170-
SET_IGNITION_CHANNEL(1)
171-
#endif
172-
173-
#if defined(USE_IGN_REFRESH)
174-
if( (isRunning(ignitionSchedule1)) && (ignitionSchedule1.dischargeAngle > (int)crankAngle) && (configPage4.StgCycles == 0) && (configPage2.perToothIgn != true) )
175-
{
176-
crankAngle = ignitionLimits(getCrankAngle()); //Refresh the crank angle info
177-
178-
adjustCrankAngle(ignitionSchedule1, crankAngle);
179-
}
180-
#endif
181-
182-
#if IGN_CHANNELS >= 2
183-
SET_IGNITION_CHANNEL(2)
184-
#endif
185-
186-
#if IGN_CHANNELS >= 3
187-
SET_IGNITION_CHANNEL(3)
188-
#endif
189-
190-
#if IGN_CHANNELS >= 4
191-
SET_IGNITION_CHANNEL(4)
192-
#endif
193-
194-
#if IGN_CHANNELS >= 5
195-
SET_IGNITION_CHANNEL(5)
196-
#endif
197-
198-
#if IGN_CHANNELS >= 6
199-
SET_IGNITION_CHANNEL(6)
200-
#endif
201-
202-
#if IGN_CHANNELS >= 7
203-
SET_IGNITION_CHANNEL(7)
204-
#endif
205-
206-
#if IGN_CHANNELS >= 8
207-
SET_IGNITION_CHANNEL(8)
208-
#endif
209-
210-
#undef SET_IGNITION_CHANNEL
211-
}
212-
213159
/** Speeduino main loop.
214160
*
215161
* Main loop chores (roughly in the order that they are performed):
@@ -1017,10 +963,7 @@ BEGIN_LTO_ALWAYS_INLINE(void) loop(void)
1017963
}
1018964
else { fixedCrankingOverride = 0; }
1019965

1020-
if(ignitionChannelsOn > 0)
1021-
{
1022-
setIgnitionChannels(ignitionLimits(getCrankAngle()), currentStatus.dwell + fixedCrankingOverride);
1023-
} //Ignition schedules on
966+
setIgnitionChannels(currentStatus, ignitionLimits(getCrankAngle()), currentStatus.dwell + fixedCrankingOverride, ignitionChannelsOn);
1024967

1025968
if ( (!currentStatus.resetPreventActive) && (resetControl == RESET_CONTROL_PREVENT_WHEN_RUNNING) )
1026969
{

test/test_schedules/test_igniton_schedule_controller.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,22 @@ static void test_calculateIgnitionAngles_nonrotary(void)
6060
{
6161
ignition_test_context_t context;
6262
CRANK_ANGLE_MAX_IGN = 720U;
63-
context.page2.nCylinders = 8U;
64-
context.current.maxIgnOutputs = 8U;
6563
context.page4.sparkMode = IGN_MODE_SEQUENTIAL;
6664
context.decoderStatus.syncStatus = SyncStatus::Full;
67-
6865
setup_ignition_channel_angles();
69-
context.calculateIgnitionAngles();
70-
assert_ignition_angles(context);
66+
67+
for (uint8_t index=0; index<=IGN_CHANNELS; ++index)
68+
{
69+
context.current.maxIgnOutputs = index;
70+
context.calculateIgnitionAngles();
71+
assert_ignition_angles(context);
72+
}
7173
}
7274

7375
static void test_calculateIgnitionAngles_rotary(void)
7476
{
7577
ignition_test_context_t context;
78+
CRANK_ANGLE_MAX_IGN = 360;
7679
context.current.maxIgnOutputs = 4U;
7780
context.page4.sparkMode = IGN_MODE_ROTARY;
7881
context.page2.nCylinders = 4U;
@@ -85,6 +88,7 @@ static void test_calculateIgnitionAngles_rotary(void)
8588
static void test_calculateIgnitionAngles_rotary_non_4_output_uses_non_rotary(void)
8689
{
8790
ignition_test_context_t context;
91+
CRANK_ANGLE_MAX_IGN = 720;
8892
context.current.maxIgnOutputs = 5U; // Not 4
8993
context.page4.sparkMode = IGN_MODE_ROTARY;
9094

@@ -114,12 +118,40 @@ static void test_calculateIgnitionAngles_sync_state_transitions(void)
114118
TEST_ASSERT_EQUAL_UINT16(360U, CRANK_ANGLE_MAX_IGN);
115119
}
116120

121+
static void test_setIgnitionChannels_mask_enables_and_disables_channels(void)
122+
{
123+
ignition_test_context_t context;
124+
context.current.maxIgnOutputs = IGN_CHANNELS;
125+
context.page4.sparkMode = IGN_MODE_SEQUENTIAL;
126+
CRANK_ANGLE_MAX_IGN = 720U;
127+
setup_ignition_channel_angles();
128+
context.calculateIgnitionAngles();
129+
130+
for (uint8_t index=0; index<=IGN_CHANNELS; ++index)
131+
{
132+
context.current.maxIgnOutputs = index;
133+
// Enable channels 1, 3, 5 & 7
134+
setIgnitionChannels(context.current, 0U, context.current.dwell, 0b01010101);
135+
136+
// Enabled channels should be pending, disabled should remain OFF
137+
RUNIF_IGNCHANNEL1( { if (context.current.maxIgnOutputs>=1) { TEST_ASSERT_EQUAL_UINT8(PENDING, (uint8_t)ignitionSchedule1.Status); } }, {});
138+
RUNIF_IGNCHANNEL2( { if (context.current.maxIgnOutputs>=2) { TEST_ASSERT_EQUAL_UINT8(OFF, (uint8_t)ignitionSchedule2.Status); } }, {});
139+
RUNIF_IGNCHANNEL3( { if (context.current.maxIgnOutputs>=3) { TEST_ASSERT_EQUAL_UINT8(PENDING, (uint8_t)ignitionSchedule3.Status); } }, {});
140+
RUNIF_IGNCHANNEL4( { if (context.current.maxIgnOutputs>=4) { TEST_ASSERT_EQUAL_UINT8(OFF, (uint8_t)ignitionSchedule4.Status); } }, {});
141+
RUNIF_IGNCHANNEL5( { if (context.current.maxIgnOutputs>=5) { TEST_ASSERT_EQUAL_UINT8(PENDING, (uint8_t)ignitionSchedule5.Status); } }, {});
142+
RUNIF_IGNCHANNEL6( { if (context.current.maxIgnOutputs>=6) { TEST_ASSERT_EQUAL_UINT8(OFF, (uint8_t)ignitionSchedule6.Status); } }, {});
143+
RUNIF_IGNCHANNEL7( { if (context.current.maxIgnOutputs>=7) { TEST_ASSERT_EQUAL_UINT8(PENDING, (uint8_t)ignitionSchedule7.Status); } }, {});
144+
RUNIF_IGNCHANNEL8( { if (context.current.maxIgnOutputs>=8) { TEST_ASSERT_EQUAL_UINT8(OFF, (uint8_t)ignitionSchedule8.Status); } }, {});
145+
}
146+
}
147+
117148
void test_ignition_schedule_controller(void)
118149
{
119150
SET_UNITY_FILENAME() {
120151
RUN_TEST(test_calculateIgnitionAngles_nonrotary);
121152
RUN_TEST(test_calculateIgnitionAngles_rotary);
122153
RUN_TEST(test_calculateIgnitionAngles_rotary_non_4_output_uses_non_rotary);
123154
RUN_TEST(test_calculateIgnitionAngles_sync_state_transitions);
155+
RUN_TEST(test_setIgnitionChannels_mask_enables_and_disables_channels);
124156
}
125157
}

0 commit comments

Comments
 (0)