MCPcopy Create free account
hub / github.com/dalathegreat/Battery-Emulator / core_loop

Function core_loop

Software/Software.cpp:438–555  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

436}
437
438void 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();

Callers

nothing calls this directly

Calls 15

receive_canFunction · 0.85
receive_rs485Function · 0.85
millisFunction · 0.85
set_eventFunction · 0.85
led_exeFunction · 0.85
handle_contactorsFunction · 0.85
handle_precharge_controlFunction · 0.85
update_pause_stateFunction · 0.85
update_calculated_valuesFunction · 0.85

Tested by

no test coverage detected