modified to also read channel B
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
|
||||
@@ -52,6 +54,31 @@ static timerStatus stsA = {
|
||||
.coil_pulse_us = 1000,
|
||||
.spark_pulse_us = 100,
|
||||
.spark_delay_us = 50,
|
||||
.pins = {
|
||||
.pin_trig_12p = PIN_TRIG_A12P,
|
||||
.pin_trig_12n = PIN_TRIG_A12N,
|
||||
.pin_trig_34p = PIN_TRIG_A34P,
|
||||
.pin_trig_34n = PIN_TRIG_A34N,
|
||||
.pin_spark_12 = SPARK_A12,
|
||||
.pin_spark_34 = SPARK_A34
|
||||
},
|
||||
.main_task = NULL};
|
||||
|
||||
static timerStatus stsB = {
|
||||
.clock_period_us = (uint32_t)PERIOD_US,
|
||||
.pause_long_us = 10000,
|
||||
.pause_short_us = 1000,
|
||||
.coil_pulse_us = 1000,
|
||||
.spark_pulse_us = 100,
|
||||
.spark_delay_us = 50,
|
||||
.pins = {
|
||||
.pin_trig_12p = PIN_TRIG_B12P,
|
||||
.pin_trig_12n = PIN_TRIG_B12N,
|
||||
.pin_trig_34p = PIN_TRIG_B34P,
|
||||
.pin_trig_34n = PIN_TRIG_B34N,
|
||||
.pin_spark_12 = SPARK_B12,
|
||||
.pin_spark_34 = SPARK_B34
|
||||
},
|
||||
.main_task = NULL};
|
||||
|
||||
static bool isEnabled = false;
|
||||
@@ -82,12 +109,23 @@ void setup()
|
||||
|
||||
pinMode(ENABLE_PIN, INPUT_PULLUP);
|
||||
|
||||
// get the task handle for the main loop
|
||||
stsA.main_task = xTaskGetCurrentTaskHandleForCore(1);
|
||||
stsB.main_task = xTaskGetCurrentTaskHandleForCore(1);
|
||||
|
||||
// Begin timer with preset fixed frequency
|
||||
timerA = timerBegin(FREQUENCY);
|
||||
timerB = timerBegin(FREQUENCY);
|
||||
|
||||
// Stop timers because of autostart
|
||||
timerStop(timerA);
|
||||
timerStop(timerB);
|
||||
|
||||
// Attach interrupts and call callback every timer expiry
|
||||
timerAttachInterruptArg(timerA, &onTimer, (void *)&stsA);
|
||||
timerAlarm(timerA, 1, true, 0);
|
||||
timerAttachInterruptArg(timerB, &onTimer, (void *)&stsB);
|
||||
timerAlarm(timerA, 1, true, 0); // infinite number of reloads
|
||||
timerAlarm(timerB, 1, true, 0);
|
||||
|
||||
LOG_INFO("Setup Complete");
|
||||
}
|
||||
@@ -103,10 +141,13 @@ void loop()
|
||||
} else {
|
||||
stsA.soft_start = false;
|
||||
}
|
||||
stsB.soft_start = stsA.soft_start;
|
||||
stsB.spark_delay_us = stsA.spark_delay_us;
|
||||
|
||||
double new_rpm = (double)(map(analogRead(FREQ_POT), 0, 4096, RPM_MIN, RPM_MAX));
|
||||
filtered_rpm = filtered_rpm + 0.1 * (new_rpm - filtered_rpm);
|
||||
stsA.pause_long_us = (uint32_t)(60000000.0f / filtered_rpm / 2.0f);
|
||||
stsB.pause_long_us = stsA.pause_long_us;
|
||||
|
||||
if (isEnabled) {
|
||||
LOG_INFO("==== System is ENABLED ====");
|
||||
@@ -121,8 +162,11 @@ void loop()
|
||||
|
||||
if (digitalRead(ENABLE_PIN) == LOW && !isEnabled) {
|
||||
timerStart(timerA);
|
||||
delayMicroseconds(50);
|
||||
timerStart(timerB);
|
||||
isEnabled = true;
|
||||
} else if (digitalRead(ENABLE_PIN) == HIGH && isEnabled) {
|
||||
timerStop(timerA);
|
||||
timerStop(timerA);
|
||||
isEnabled = false;
|
||||
}
|
||||
|
||||
@@ -7,20 +7,18 @@ void onTimer(void *arg)
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
timerStatus *params = (timerStatus *)(arg);
|
||||
TaskHandle_t task = params->main_task;
|
||||
const timerPins pins = params->pins;
|
||||
|
||||
// increment state time
|
||||
params->state_time += params->clock_period_us;
|
||||
|
||||
digitalWrite(PIN_TRIG_B12P, HIGH);
|
||||
|
||||
switch (params->state)
|
||||
{
|
||||
|
||||
case S_12P:
|
||||
if (params->state_time == params->clock_period_us && !params->coil12p_high)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, PIN_TRIG_A12P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12P, HIGH);
|
||||
digitalWrite(pins.pin_trig_12p, HIGH);
|
||||
params->coil12p_high = true;
|
||||
wait_sent = false;
|
||||
}
|
||||
@@ -29,21 +27,18 @@ void onTimer(void *arg)
|
||||
{
|
||||
if (params->state_time == params->spark_delay_us)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A12, HIGH);
|
||||
digitalWrite(pins.pin_spark_12, HIGH);
|
||||
}
|
||||
|
||||
if (params->state_time == (params->spark_delay_us + params->spark_pulse_us))
|
||||
{
|
||||
// xTaskNotifyFromISR(task, ~SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A12, LOW);
|
||||
digitalWrite(pins.pin_spark_12, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil12p_high)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, ~PIN_TRIG_A12P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12P, LOW);
|
||||
digitalWrite(pins.pin_trig_12p, LOW);
|
||||
params->coil12p_high = false;
|
||||
}
|
||||
|
||||
@@ -57,8 +52,7 @@ void onTimer(void *arg)
|
||||
case S_12N:
|
||||
if (params->state_time == params->clock_period_us && !params->coil12n_high)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, PIN_TRIG_A12N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12N, HIGH);
|
||||
digitalWrite(pins.pin_trig_12n, HIGH);
|
||||
params->coil12n_high = true;
|
||||
}
|
||||
|
||||
@@ -66,21 +60,18 @@ void onTimer(void *arg)
|
||||
{
|
||||
if (params->state_time == params->spark_delay_us)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A12, HIGH);
|
||||
digitalWrite(pins.pin_spark_12, HIGH);
|
||||
}
|
||||
|
||||
if (params->state_time == (params->spark_delay_us + params->spark_pulse_us))
|
||||
{
|
||||
// xTaskNotifyFromISR(task, ~SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A12, LOW);
|
||||
digitalWrite(pins.pin_spark_12, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil12n_high)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, ~PIN_TRIG_A12N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12N, LOW);
|
||||
digitalWrite(pins.pin_trig_12n, LOW);
|
||||
params->coil12n_high = false;
|
||||
params->state = S_WAIT_10MS;
|
||||
params->state_time = 0;
|
||||
@@ -90,7 +81,6 @@ void onTimer(void *arg)
|
||||
case S_WAIT_10MS:
|
||||
if (!wait_sent)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, S_WAIT_10MS, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
wait_sent = true;
|
||||
}
|
||||
if (params->state_time >= params->pause_long_us)
|
||||
@@ -103,8 +93,7 @@ void onTimer(void *arg)
|
||||
case S_34P:
|
||||
if (params->state_time == params->clock_period_us && !params->coil34p_high)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, PIN_TRIG_A34P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34P, HIGH);
|
||||
digitalWrite(pins.pin_trig_34p, HIGH);
|
||||
params->coil34p_high = true;;
|
||||
wait_sent = false;
|
||||
}
|
||||
@@ -113,21 +102,18 @@ void onTimer(void *arg)
|
||||
{
|
||||
if (params->state_time == params->spark_delay_us)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A34, HIGH);
|
||||
digitalWrite(pins.pin_spark_34, HIGH);
|
||||
}
|
||||
|
||||
if (params->state_time == params->spark_delay_us + params->spark_pulse_us)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, ~SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A34, LOW);
|
||||
digitalWrite(pins.pin_spark_34, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil34p_high)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, ~PIN_TRIG_A34P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34P, LOW);
|
||||
digitalWrite(pins.pin_trig_34p, LOW);
|
||||
params->coil34p_high = false;
|
||||
}
|
||||
|
||||
@@ -141,8 +127,7 @@ void onTimer(void *arg)
|
||||
case S_34N:
|
||||
if (params->state_time == params->clock_period_us && !params->coil34n_high)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, PIN_TRIG_A34N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34N, HIGH);
|
||||
digitalWrite(pins.pin_trig_34n, HIGH);
|
||||
params->coil34n_high = true;
|
||||
}
|
||||
|
||||
@@ -150,21 +135,18 @@ void onTimer(void *arg)
|
||||
{
|
||||
if (params->state_time == params->spark_delay_us)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A34, HIGH);
|
||||
digitalWrite(pins.pin_spark_34, HIGH);
|
||||
}
|
||||
|
||||
if (params->state_time == params->spark_delay_us + params->spark_pulse_us)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, ~SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A34, LOW);
|
||||
digitalWrite(pins.pin_spark_34, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil34n_high)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, ~PIN_TRIG_A34N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34N, LOW);
|
||||
digitalWrite(pins.pin_trig_34n, LOW);
|
||||
params->coil34n_high = false;
|
||||
params->state = S_WAIT_10MS_END;
|
||||
params->state_time = 0;
|
||||
@@ -174,7 +156,6 @@ void onTimer(void *arg)
|
||||
case S_WAIT_10MS_END:
|
||||
if (!wait_sent)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, S_WAIT_10MS_END, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
wait_sent = true;
|
||||
}
|
||||
if (params->state_time >= params->pause_long_us)
|
||||
@@ -184,9 +165,7 @@ void onTimer(void *arg)
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
digitalWrite(PIN_TRIG_B12P, LOW);
|
||||
|
||||
|
||||
if (xHigherPriorityTaskWoken)
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
#include "pins.h"
|
||||
@@ -19,6 +21,15 @@ enum State
|
||||
S_WAIT_10MS_END
|
||||
};
|
||||
|
||||
struct timerPins {
|
||||
const uint8_t pin_trig_12p;
|
||||
const uint8_t pin_trig_12n;
|
||||
const uint8_t pin_trig_34p;
|
||||
const uint8_t pin_trig_34n;
|
||||
const uint8_t pin_spark_12;
|
||||
const uint8_t pin_spark_34;
|
||||
};
|
||||
|
||||
struct timerStatus
|
||||
{
|
||||
State state = State::S_12P;
|
||||
@@ -34,6 +45,7 @@ struct timerStatus
|
||||
bool coil34p_high = false;
|
||||
bool coil12n_high = false;
|
||||
bool coil34n_high = false;
|
||||
timerPins pins;
|
||||
TaskHandle_t main_task;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user