Test working at 100Khz
This commit is contained in:
@@ -7,8 +7,13 @@
|
||||
static hw_timer_t *timerA = NULL;
|
||||
static hw_timer_t *timerB = NULL;
|
||||
TaskHandle_t main_t = NULL;
|
||||
static uint32_t count = 0;
|
||||
|
||||
static const std::map<const uint32_t, const char*> pin2Name = {
|
||||
#define FREQUENCY 100000 // 100 KHz
|
||||
//#define PERIOD_US 1000000 / FREQUENCY
|
||||
#define PERIOD_US 10
|
||||
|
||||
static const std::map<const uint32_t, const char *> pin2Name = {
|
||||
{PIN_TRIG_A12P, "HIGH_PIN_TRIG_A12P"},
|
||||
{~PIN_TRIG_A12P, "LOW_PIN_TRIG_A12P"},
|
||||
{PIN_TRIG_A12N, "HIGH_PIN_TRIG_A12N"},
|
||||
@@ -20,8 +25,18 @@ static const std::map<const uint32_t, const char*> pin2Name = {
|
||||
{SPARK_A12, "HIGH_SPARK_A12"},
|
||||
{~SPARK_A12, "LOW_SPARK_A12"},
|
||||
{SPARK_A34, "HIGH_SPARK_A34"},
|
||||
{~SPARK_A34, "LOW_SPARK_A34"}
|
||||
};
|
||||
{~SPARK_A34, "LOW_SPARK_A34"},
|
||||
{State::S_WAIT_10MS_END, "S_WAIT_10MS_END"},
|
||||
{State::S_WAIT_10MS, "S_WAIT_10MS"}};
|
||||
|
||||
static timerStatus stsA = {
|
||||
.clock_period_us = (uint32_t)PERIOD_US,
|
||||
.pause_long_us = 10000,
|
||||
.pause_short_us = 1000,
|
||||
.coil_pulse_us = 500,
|
||||
.spark_pulse_us = 100,
|
||||
.spark_delay_us = 100,
|
||||
.main_task = NULL};
|
||||
|
||||
void setup()
|
||||
{
|
||||
@@ -43,25 +58,22 @@ void setup()
|
||||
pinMode(SPARK_B12, OUTPUT);
|
||||
pinMode(SPARK_B34, OUTPUT);
|
||||
|
||||
main_t = xTaskGetCurrentTaskHandleForCore(1);
|
||||
pinMode(12, ANALOG);
|
||||
stsA.main_task = xTaskGetCurrentTaskHandleForCore(1);
|
||||
|
||||
// Timer: 80MHz / 80 = 1MHz → 1 tick = 1 µs
|
||||
timerA = timerBegin(10000);
|
||||
timerAttachInterruptArg(timerA, &onTimer, (void *)main_t);
|
||||
timerAlarm(timerA, 10000, true, 0);
|
||||
|
||||
// Timer: 80MHz / 80 = 1MHz → 1 tick = 1 µs
|
||||
// timerB = timerBegin(1);
|
||||
// timerAttachInterrupt(timerB, &onTimer);
|
||||
// timerStart(timerB);
|
||||
timerA = timerBegin(FREQUENCY);
|
||||
timerAttachInterruptArg(timerA, &onTimer, (void *)&stsA);
|
||||
timerAlarm(timerA, 1, true, 0);
|
||||
|
||||
LOG_INFO("Setup Complete");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint32_t value = 0;
|
||||
if (xTaskNotifyWait(0x00, ULONG_MAX, &value, 0))
|
||||
Serial.println(pin2Name.at(value));
|
||||
delay(10);
|
||||
LOG_INFO("Loop: ", count++);
|
||||
uint32_t spark_delay = (uint32_t)(map(analogRead(12), 0, 4096, 100, 1000) / PERIOD_US);
|
||||
stsA.spark_delay_us = spark_delay * PERIOD_US;
|
||||
LOG_INFO("Spark Delay uS: ", stsA.spark_delay_us);
|
||||
delay(500);
|
||||
}
|
||||
|
||||
@@ -1,119 +1,156 @@
|
||||
#include "timer.h"
|
||||
|
||||
#define TIME_CONST 1
|
||||
|
||||
enum State {
|
||||
S_12P,
|
||||
S_12N_DELAY,
|
||||
S_12N,
|
||||
S_WAIT_10MS,
|
||||
S_34P,
|
||||
S_34N_DELAY,
|
||||
S_34N,
|
||||
S_WAIT_1MS,
|
||||
S_WAIT_10MS_END
|
||||
};
|
||||
|
||||
volatile State state = S_12P;
|
||||
volatile uint32_t state_time = 0;
|
||||
|
||||
void onTimer(void* arg) {
|
||||
volatile static bool wait_sent = false;
|
||||
|
||||
void onTimer(void *arg)
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
TaskHandle_t task = (TaskHandle_t)(arg);
|
||||
state_time += 1;
|
||||
timerStatus *params = (timerStatus *)(arg);
|
||||
TaskHandle_t task = params->main_task;
|
||||
|
||||
switch (state) {
|
||||
// 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);
|
||||
xTaskNotifyFromISR(task, PIN_TRIG_A12P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
params->coil12p_high = true;
|
||||
wait_sent = false;
|
||||
}
|
||||
|
||||
if (state_time == 2*TIME_CONST){
|
||||
xTaskNotifyFromISR(task, SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
if (params->state_time == params->spark_delay_us)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A12, HIGH);
|
||||
}
|
||||
|
||||
if (state_time == 7*TIME_CONST) {
|
||||
xTaskNotifyFromISR(task, ~SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil12p_high)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, ~PIN_TRIG_A12P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12P, LOW);
|
||||
params->coil12p_high = false;
|
||||
}
|
||||
|
||||
if (params->state_time == (params->spark_delay_us + params->spark_pulse_us))
|
||||
{
|
||||
//xTaskNotifyFromISR(task, ~SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A12, LOW);
|
||||
}
|
||||
|
||||
if (state_time >= 5*TIME_CONST) {
|
||||
xTaskNotifyFromISR(task, ~PIN_TRIG_A12P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12P, LOW);
|
||||
}
|
||||
|
||||
if (state_time >= 10*TIME_CONST) {
|
||||
state = S_12N;
|
||||
state_time = 0;
|
||||
if (params->state_time >= params->pause_short_us)
|
||||
{
|
||||
params->state = S_12N;
|
||||
params->state_time = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case S_12N:
|
||||
xTaskNotifyFromISR(task, PIN_TRIG_A12N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
if (params->state_time == params->clock_period_us && !params->coil12n_high)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, PIN_TRIG_A12N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12N, HIGH);
|
||||
params->coil12n_high = true;
|
||||
}
|
||||
|
||||
if (state_time >= 5*TIME_CONST) {
|
||||
xTaskNotifyFromISR(task, ~PIN_TRIG_A12N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil12n_high)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, ~PIN_TRIG_A12N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12N, LOW);
|
||||
state = S_WAIT_10MS;
|
||||
state_time = 0;
|
||||
params->coil12n_high = false;
|
||||
params->state = S_WAIT_10MS;
|
||||
params->state_time = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case S_WAIT_10MS:
|
||||
if (state_time >= 10*TIME_CONST) {
|
||||
state = S_34P;
|
||||
state_time = 0;
|
||||
if (!wait_sent)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, S_WAIT_10MS, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
wait_sent = true;
|
||||
}
|
||||
if (params->state_time >= params->pause_long_us)
|
||||
{
|
||||
params->state = S_34P;
|
||||
params->state_time = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case S_34P:
|
||||
xTaskNotifyFromISR(task, PIN_TRIG_A34P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
if (params->state_time == params->clock_period_us && !params->coil34p_high)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, PIN_TRIG_A34P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34P, HIGH);
|
||||
params->coil34p_high;
|
||||
wait_sent = false;
|
||||
}
|
||||
|
||||
if (state_time == 2*TIME_CONST){
|
||||
xTaskNotifyFromISR(task, SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
if (params->state_time == params->spark_delay_us)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A34, HIGH);
|
||||
}
|
||||
|
||||
if (state_time == 7*TIME_CONST) {
|
||||
xTaskNotifyFromISR(task, ~SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil34p_high)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, ~PIN_TRIG_A34P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34P, LOW);
|
||||
params->coil34p_high = false;
|
||||
}
|
||||
|
||||
if (params->state_time == params->spark_delay_us + params->spark_pulse_us)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, ~SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A34, LOW);
|
||||
}
|
||||
|
||||
if (state_time >= 5*TIME_CONST) {
|
||||
xTaskNotifyFromISR(task, ~PIN_TRIG_A34P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34P, LOW);
|
||||
}
|
||||
|
||||
if (state_time >= 10*TIME_CONST) {
|
||||
state = S_34N;
|
||||
state_time = 0;
|
||||
if (params->state_time >= params->pause_short_us)
|
||||
{
|
||||
params->state = S_34N;
|
||||
params->state_time = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case S_34N:
|
||||
xTaskNotifyFromISR(task, PIN_TRIG_A34N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
if (params->state_time == params->clock_period_us && !params->coil34n_high)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, PIN_TRIG_A34N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34N, HIGH);
|
||||
params->coil34n_high = true;
|
||||
}
|
||||
|
||||
if (state_time >= 5*TIME_CONST) {
|
||||
xTaskNotifyFromISR(task, ~PIN_TRIG_A34N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil34n_high)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, ~PIN_TRIG_A34N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34N, LOW);
|
||||
state = S_WAIT_10MS_END;
|
||||
state_time = 0;
|
||||
params->coil34n_high = false;
|
||||
params->state = S_WAIT_10MS_END;
|
||||
params->state_time = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case S_WAIT_10MS_END:
|
||||
if (state_time >= 10*TIME_CONST) {
|
||||
state = S_12P;
|
||||
state_time = 0;
|
||||
if (!wait_sent)
|
||||
{
|
||||
//xTaskNotifyFromISR(task, S_WAIT_10MS_END, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
wait_sent = true;
|
||||
}
|
||||
if (params->state_time >= params->pause_long_us)
|
||||
{
|
||||
params->state = S_12P;
|
||||
params->state_time = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
digitalWrite(PIN_TRIG_B12P, LOW);
|
||||
|
||||
if (xHigherPriorityTaskWoken)
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
|
||||
@@ -6,4 +6,35 @@
|
||||
|
||||
#include "driver/gpio.h"
|
||||
|
||||
void IRAM_ATTR onTimer(void* arg);
|
||||
enum State
|
||||
{
|
||||
S_12P,
|
||||
S_12N_DELAY,
|
||||
S_12N,
|
||||
S_WAIT_10MS,
|
||||
S_34P,
|
||||
S_34N_DELAY,
|
||||
S_34N,
|
||||
S_WAIT_1MS,
|
||||
S_WAIT_10MS_END
|
||||
};
|
||||
|
||||
struct timerStatus
|
||||
{
|
||||
State state = State::S_12P;
|
||||
uint32_t state_time = 0;
|
||||
uint32_t clock_period_us;
|
||||
uint32_t pause_long_us;
|
||||
uint32_t pause_short_us;
|
||||
uint32_t coil_pulse_us;
|
||||
uint32_t spark_pulse_us;
|
||||
uint32_t spark_delay_us;
|
||||
bool soft_start = false;
|
||||
bool coil12p_high = false;
|
||||
bool coil34p_high = false;
|
||||
bool coil12n_high = false;
|
||||
bool coil34n_high = false;
|
||||
TaskHandle_t main_task;
|
||||
};
|
||||
|
||||
void IRAM_ATTR onTimer(void *arg);
|
||||
|
||||
Reference in New Issue
Block a user