diff --git a/RotaxMonitor/src/isr.h b/RotaxMonitor/src/isr.h index 7c34bac..1c643bf 100644 --- a/RotaxMonitor/src/isr.h +++ b/RotaxMonitor/src/isr.h @@ -38,12 +38,12 @@ #endif static const std::map names = { - {TRIG_FLAG_A12P, "TRIG_A12P"}, - {TRIG_FLAG_A12N, "TRIG_A12N"}, - {TRIG_FLAG_A34P, "TRIG_A34P"}, - {TRIG_FLAG_A34N, "TRIG_A34N"}, - {SPARK_FLAG_A12, "SPARK_A12"}, - {SPARK_FLAG_A34, "SPARK_A34"}, + {TRIG_FLAG_A12P, "TRIG_FLAG_A12P"}, + {TRIG_FLAG_A12N, "TRIG_FLAG_A12N"}, + {TRIG_FLAG_A34P, "TRIG_FLAG_A34P"}, + {TRIG_FLAG_A34N, "TRIG_FLAG_A34N"}, + {SPARK_FLAG_A12, "SPARK_FLAG_A12"}, + {SPARK_FLAG_A34, "SPARK_FLAG_A34"}, }; // Task handle @@ -91,13 +91,16 @@ struct ignitionBoxStatus coilsStatus coils34; // voltage from generator float volts_gen = 0.0; + + bool spark12 = false; + bool spark34 = false; }; ignitionBoxStatus ignA_status; ignitionBoxStatus ignB_status; // Pin to flag Map -static uint32_t pin2trig[49]; +static uint32_t pin2trig[49] = {0}; void initTriggerPinMapping() { pin2trig[TRIG_A12P] = TRIG_FLAG_A12P; @@ -112,7 +115,7 @@ void initTriggerPinMapping() #endif }; -static uint32_t pin2spark[49]; +static uint32_t pin2spark[49] = {0}; void initSparkPinMapping() { pin2spark[SPARK_A12] = SPARK_FLAG_A12; @@ -127,55 +130,55 @@ void initSparkPinMapping() // ISR (Pass return bitmask to ISR management function) // one function for each wake up pin conncted to a trigger // ===================== -void IRAM_ATTR trig_isr_a() +void IRAM_ATTR trig_isr_a(void* arg) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; volatile const int64_t time_us = esp_timer_get_time(); if (!trigA_TaskHandle) return; // exit if task is not running - #ifndef TEST - uint32_t status = GPIO.status; - #else - uint32_t status = GPIO.status1.val; - #endif - uint32_t pickup_flags = 0; + uint32_t flag = (uint32_t)arg; + ignA_status.spark12 = false; + ignA_status.spark34 = false; - while (status) - { - uint32_t pin = __builtin_ctz(status); // trova primo bit attivo - status &= ~(1 << pin); // clear bit - pickup_flags |= pin2trig[pin]; + if (flag & TRIG_FLAG_A12P) { + ignA_status.coils12.trig_time = time_us; + xTaskNotifyFromISR(trigA_TaskHandle, flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); + } + if (flag & TRIG_FLAG_A34P) { + ignA_status.coils34.trig_time = time_us; + xTaskNotifyFromISR(trigA_TaskHandle, flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); + } + + if (flag & SPARK_FLAG_A12) { + ignA_status.spark12 = true; + ignA_status.coils12.spark_time = time_us; + xTaskNotifyGive(trigA_TaskHandle); + } + if (flag & SPARK_FLAG_A34) { + ignA_status.spark12 = false; + ignA_status.coils34.spark_time = time_us; + xTaskNotifyGive(trigA_TaskHandle); } - if (pickup_flags & TRIG_FLAG_A12P) - ignA_status.coils12.trig_time = time_us; - if (pickup_flags & TRIG_FLAG_A34P) - ignA_status.coils34.trig_time = time_us; - - xTaskNotifyFromISR(trigA_TaskHandle, GPIO.status1.val, eSetBits, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + //xTaskNotifyFromISR(trigA_TaskHandle, flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); + if (xHigherPriorityTaskWoken) { + portYIELD_FROM_ISR(); + } } -void IRAM_ATTR spark_a() -{ - BaseType_t xHigherPriorityTaskWoken = pdFALSE; - volatile const int64_t time_us = esp_timer_get_time(); - if (!trigA_TaskHandle) - return; - #ifndef TEST - uint32_t spark_flag = GPIO.status1.val & SPARK_A12 ? SPARK_FLAG_A12 : SPARK_FLAG_A34; - #else - uint32_t spark_flag = GPIO.status & SPARK_A12 ? SPARK_FLAG_A12 : SPARK_FLAG_A34; - #endif +// void IRAM_ATTR spark_a(void* arg) +// { +// BaseType_t xHigherPriorityTaskWoken = pdFALSE; +// volatile const int64_t time_us = esp_timer_get_time(); +// if (!trigA_TaskHandle) +// return; - if (spark_flag & SPARK_FLAG_A12) - ignA_status.coils12.spark_time = time_us; - if (spark_flag & SPARK_FLAG_A34) - ignA_status.coils12.spark_time = time_us; - xTaskNotifyFromISR(trigA_TaskHandle, GPIO.status, eSetBits, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); -} +// uint32_t flag = (uint32_t) arg; + +// xTaskNotifyFromISR(trigA_TaskHandle, flag, eSetBits, &xHigherPriorityTaskWoken); +// portYIELD_FROM_ISR(xHigherPriorityTaskWoken); +// } #ifndef TEST void IRAM_ATTR trig_isr_b() diff --git a/RotaxMonitor/src/main.cpp b/RotaxMonitor/src/main.cpp index f111742..bfad25e 100644 --- a/RotaxMonitor/src/main.cpp +++ b/RotaxMonitor/src/main.cpp @@ -11,6 +11,16 @@ #include #include +void printTaskList() { + char buffer[1024]; + + Serial.println("Task Name\tState\tPrio\tStack\tNum"); + + vTaskList(buffer); + + Serial.println(buffer); +} + void setup() { delay(250); @@ -108,11 +118,8 @@ void loop() uint32_t count(0); while (running) { - //digitalWrite(POT_B_CS, HIGH); - //delay(500); - //LOG_INFO("Main Loop [", count++, "]"); - //digitalWrite(POT_B_CS, LOW); - //delay(500); + //printTaskList(); + delay(10000); } if (trigA_TaskHandle) diff --git a/RotaxMonitor/src/tasks.h b/RotaxMonitor/src/tasks.h index a13eae3..dd965aa 100644 --- a/RotaxMonitor/src/tasks.h +++ b/RotaxMonitor/src/tasks.h @@ -13,7 +13,7 @@ //Global Variables and Flags static bool rt_task_running = true; -const auto spark_timeout_max = 1; // convert to microsecond timer +const auto spark_timeout_max = 1000; // convert to microsecond timer // RT task parameters struct rtTaskParams { @@ -33,43 +33,55 @@ void ignitionA_task(void *pvParameters) { Devices* dev = (Devices*) params->dev; ADS1256* adc = dev->adc_a; - // Global task variables - uint32_t pickup_flag = 0; - uint32_t spark_flag = 0; - // Ignition A Interrupts - attachInterrupt(TRIG_A12P, trig_isr_a, RISING); - attachInterrupt(TRIG_A34P, trig_isr_a, RISING); - attachInterrupt(TRIG_A12N, trig_isr_a, RISING); - attachInterrupt(TRIG_A34N, trig_isr_a, RISING); - attachInterrupt(SPARK_A12, spark_a, RISING); - attachInterrupt(SPARK_A34, spark_a, RISING); + attachInterruptArg(TRIG_A12P, trig_isr_a, (void*)TRIG_FLAG_A12P, RISING); + attachInterruptArg(TRIG_A12N, trig_isr_a, (void*)TRIG_FLAG_A12N, RISING); + attachInterruptArg(TRIG_A34P, trig_isr_a, (void*)TRIG_FLAG_A34P, RISING); + attachInterruptArg(TRIG_A34N, trig_isr_a, (void*)TRIG_FLAG_A34N, RISING); + attachInterruptArg(SPARK_A12, trig_isr_a, (void*)SPARK_FLAG_A12, RISING); + attachInterruptArg(SPARK_A34, trig_isr_a, (void*)SPARK_FLAG_A34, RISING); + uint32_t it=0; while (rt_task_running) { + // Global task variables + uint32_t pickup_flag = 0; + uint32_t spark_flag = 0; + // WAIT FOR PICKUP SIGNAL xTaskNotifyWait( - ULONG_MAX, // non pulire all'ingresso + 0x00, // non pulire all'ingresso ULONG_MAX, // pulisci i primi 8 bit &pickup_flag, // valore ricevuto portMAX_DELAY ); - LOG_ERROR("Pickup Interrupt Status", printBits(pickup_flag).c_str()); + + Serial.print("\033[2J"); // clear screen + Serial.print("\033[H"); // cursor home + LOG_INFO("Iteration [", it, "]"); + + //LOG_INFO("Pickup Flags: ", printBits(pickup_flag).c_str()); if (!names.contains(pickup_flag)) { + LOG_ERROR("Wrong Pickup Flag"); continue; } else { LOG_INFO("Pickup Trigger: ", names.at(pickup_flag)); } + // WAIT FOR SPARK TO HAPPEN - auto spark_timeout = xTaskNotifyWait( - ULONG_MAX, // non pulire all'ingresso - ULONG_MAX, // pulisci gli 8 bit successivi - &spark_flag, // valore ricevuto - spark_timeout_max - ); - if (spark_timeout == pdPASS){ //otherwise timeout - LOG_ERROR("Spark Interrupt Status", printBits(spark_flag).c_str()); + auto spark_timeout = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(5)); + //auto spark_timeout = xTaskNotifyWait( + // 0x00, // non pulire all'ingresso + // ULONG_MAX, // pulisci gli 8 bit successivi + // &spark_flag, // valore ricevuto + // spark_timeout_max + //); + + digitalWrite(POT_A_CS, HIGH); + if (ignA_status.spark12 || ignA_status.spark34) { //otherwise timeout + //LOG_INFO("Spark Flags: ", printBits(spark_flag).c_str()); + spark_flag = ignA_status.spark12 ? SPARK_FLAG_A12 : SPARK_FLAG_A34; if (!names.contains(spark_flag)) { - continue; + LOG_ERROR("Wrong Spark Flag"); } else { LOG_INFO("Spark Trigger:", names.at(spark_flag)); } @@ -77,27 +89,32 @@ void ignitionA_task(void *pvParameters) { LOG_INFO("Spark Timeout"); } + xTaskNotifyStateClear(NULL); + ulTaskNotifyValueClear(NULL, 0xFFFFFFFF); + // A trigger from pickup 12 is followed by a spark event on 34 or vice versa pickup 34 triggers spark on 12 - if ((pickup_flag == TRIG_FLAG_A12P || pickup_flag == TRIG_FLAG_A12N) && spark_flag != SPARK_A12) { + if ((pickup_flag == TRIG_FLAG_A12P || pickup_flag == TRIG_FLAG_A12N) && spark_flag != SPARK_FLAG_A12) { ignA_status.coils12.spark_status = ignA_status.coils34.spark_status = sparkStatus::SPARK_SYNC_FAIL; // Save error on circular buffer and skip to next cycle // + LOG_ERROR("Spark Mismatch"); continue; } - const uint32_t combined_flags = pickup_flag & spark_flag; coilsStatus* c; - switch (combined_flags) + switch (pickup_flag) { - case TRIG_A12P: - case TRIG_A12N: - case SPARK_A12: + case TRIG_FLAG_A12P: + case TRIG_FLAG_A12N: + //case SPARK_FLAG_A12: c = &ignA_status.coils12; - case TRIG_A34P: - case TRIG_A34N: - case SPARK_A34: + break; + case TRIG_FLAG_A34P: + case TRIG_FLAG_A34N: + //case SPARK_FLAG_A34: c = &ignA_status.coils34; + break; } - + bool new_data = false; switch (pickup_flag) { @@ -112,6 +129,7 @@ void ignitionA_task(void *pvParameters) { c->soft_start_status = softStartStatus::NORMAL; // because spark on positive edge c->spark_status = sparkStatus::SPARK_POS_OK; // do not wait for spark on negative edge LOG_INFO("Trigger Spark POSITIVE"); + LOG_INFO("Spark12 Delay Timer: ", (int)c->spark_delay); } // Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED else if (spark_timeout == pdFAIL) { @@ -133,6 +151,7 @@ void ignitionA_task(void *pvParameters) { c->soft_start_status = softStartStatus::SOFT_START; c->spark_status == sparkStatus::SPARK_NEG_OK; LOG_INFO("Trigger Spark NEGATIVE"); + LOG_INFO("Spark12 Delay Timer: ", (int)ignA_status.coils12.spark_delay); } // Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED else if (spark_timeout == pdFAIL && expected_negative12) { @@ -150,7 +169,11 @@ void ignitionA_task(void *pvParameters) { } default: LOG_ERROR("Invalid A Interrupt"); + break; } + it++; + digitalWrite(POT_A_CS, LOW); + continue; if (new_data) { vTaskDelay(pdMS_TO_TICKS(1)); // delay 1ms to allow peak detectors to charge for negative cycle @@ -180,11 +203,11 @@ void ignitionA_task(void *pvParameters) { } } - + LOG_WARN("Ending realTime Task"); // Ignition A Interrupts DETACH detachInterrupt(TRIG_A12P); - detachInterrupt(TRIG_A34P); detachInterrupt(TRIG_A12N); + detachInterrupt(TRIG_A34P); detachInterrupt(TRIG_A34N); detachInterrupt(SPARK_A12); detachInterrupt(SPARK_A34);