| 436 | } |
| 437 | |
| 438 | void core_loop(void*) { |
| 439 | esp_task_wdt_add(NULL); // Register this task with WDT |
| 440 | TickType_t xLastWakeTime = xTaskGetTickCount(); |
| 441 | const TickType_t xFrequency = pdMS_TO_TICKS(1); // Convert 1ms to ticks |
| 442 | int loopPhase = 0; |
| 443 | |
| 444 | while (true) { |
| 445 | START_TIME_MEASUREMENT(all); |
| 446 | START_TIME_MEASUREMENT(comm); |
| 447 | |
| 448 | // Input, Runs as fast as possible |
| 449 | receive_can(); // Receive CAN messages |
| 450 | receive_rs485(); // Process serial2 RS485 interface |
| 451 | |
| 452 | END_TIME_MEASUREMENT_MAX(comm, datalayer.system.status.time_comm_us); |
| 453 | |
| 454 | // Process |
| 455 | currentMillis = millis(); |
| 456 | loopPhase = 1 - loopPhase; // Spread out slower tasks across multiple iterations |
| 457 | if (currentMillis - previousMillis10ms >= INTERVAL_10_MS && loopPhase == 0) { |
| 458 | if ((currentMillis - previousMillis10ms >= INTERVAL_10_MS_DELAYED) && |
| 459 | (milliseconds(currentMillis) > esp32hal->BOOTUP_TIME())) { |
| 460 | set_event(EVENT_TASK_OVERRUN, (currentMillis - previousMillis10ms)); |
| 461 | } |
| 462 | previousMillis10ms = currentMillis; |
| 463 | if (datalayer.system.info.performance_measurement_active) { |
| 464 | START_TIME_MEASUREMENT(10ms); |
| 465 | monitor_equipment_stop_button(); |
| 466 | led_exe(); |
| 467 | handle_contactors(); // Take care of startup precharge/contactor closing |
| 468 | if (precharge_control_enabled) { |
| 469 | handle_precharge_control(currentMillis); //Drive the hia4v1 via PWM |
| 470 | } |
| 471 | END_TIME_MEASUREMENT_MAX(10ms, datalayer.system.status.time_10ms_us); |
| 472 | } else { //Run 10ms tasks without timing it |
| 473 | monitor_equipment_stop_button(); |
| 474 | led_exe(); |
| 475 | handle_contactors(); // Take care of startup precharge/contactor closing |
| 476 | if (precharge_control_enabled) { |
| 477 | handle_precharge_control(currentMillis); //Drive the hia4v1 via PWM |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (currentMillis - previousMillisUpdateVal >= INTERVAL_1_S && loopPhase == 1) { |
| 483 | previousMillisUpdateVal = currentMillis; // Order matters on the update_loop! |
| 484 | if (datalayer.system.info.performance_measurement_active) { |
| 485 | START_TIME_MEASUREMENT(values); |
| 486 | } |
| 487 | update_pause_state(); // Check if we are OK to send CAN or need to pause |
| 488 | |
| 489 | // Fetch battery values |
| 490 | if (battery) { |
| 491 | battery->update_values(); |
| 492 | } |
| 493 | |
| 494 | if (battery2) { |
| 495 | battery2->update_values(); |
nothing calls this directly
no test coverage detected