// =============================================================================
//  task_config.h -- FreeRTOS task layout for AR-Autopilot
// =============================================================================
//
//  Single source of truth for stack sizes, priorities, and core pinning of
//  every long-running task in the firmware. Centralising this here makes the
//  real-time profile of the system reviewable at a glance.
//
//  Pinning policy (brief section 6, rule #11 -- "deterministic latency"):
//
//    Core 1 (APP_CPU) -- safety-critical real-time:
//        AR_TASK_CORE_REALTIME = 1
//        - PID inner loop      (Sprint 2; stub in Sprint 1)
//        - PID outer loop      (Sprint 3; stub in Sprint 1)
//        - Rudder sensor       (Sprint 1: 100 Hz median filter)
//        - Safety monitor      (Sprint 1: limit switches, watchdog feed)
//
//    Core 0 (PRO_CPU) -- communications + housekeeping:
//        AR_TASK_CORE_COMMS = 0
//        - NMEA 2000 RX        (Sprint 1: PGN 127250 / 127251)
//        - Modbus slave        (Sprint 1: eModbus internals already async)
//        - Health reporter     (Sprint 1: 1 Hz log)
//
//  This split guarantees the real-time control loops cannot be starved or
//  jittered by bus traffic, even if NMEA 2000 saturates or the Modbus client
//  bursts.
//
//  Priorities use the standard ESP32 FreeRTOS scale (0 idle .. 24 max).
//  configMAX_PRIORITIES is 25 on default ESP32 Arduino builds.
// =============================================================================

#pragma once

// Core assignment
#define AR_TASK_CORE_REALTIME    1
#define AR_TASK_CORE_COMMS       0

// Priorities (higher = more priority)
#define AR_TASK_PRIO_PID_INNER       24    // hard real-time, 50 Hz
#define AR_TASK_PRIO_RUDDER_SENSOR   23    // 100 Hz ADC, must feed PID fresh data
#define AR_TASK_PRIO_PID_OUTER       22    // 10 Hz heading control
#define AR_TASK_PRIO_SAFETY          21    // limit switches, watchdog feed
#define AR_TASK_PRIO_N2K_RX          15    // CAN polling, event-driven
#define AR_TASK_PRIO_MODBUS          14    // eModbus internal tasks (delegated)
#define AR_TASK_PRIO_HEARTBEAT       10    // LED blink + uptime log
#define AR_TASK_PRIO_HEALTH           5    // periodic stats, low priority

// Stack sizes (bytes). Tune these once we see real high-water marks via
// uxTaskGetStackHighWaterMark() in the health reporter.
#define AR_TASK_STACK_PID_INNER      4096
#define AR_TASK_STACK_PID_OUTER      4096
#define AR_TASK_STACK_RUDDER_SENSOR  3072
#define AR_TASK_STACK_SAFETY         3072
#define AR_TASK_STACK_N2K_RX         6144   // NMEA2000 parsing is heap-heavy
#define AR_TASK_STACK_MODBUS         4096
#define AR_TASK_STACK_HEARTBEAT      2048
#define AR_TASK_STACK_HEALTH         3072

// Loop periods (ms). Convert with pdMS_TO_TICKS() at the use site.
#define AR_PERIOD_MS_PID_INNER       20     // 50 Hz
#define AR_PERIOD_MS_PID_OUTER      100     // 10 Hz
#define AR_PERIOD_MS_RUDDER_SENSOR   10     // 100 Hz
#define AR_PERIOD_MS_SAFETY          20     // 50 Hz
#define AR_PERIOD_MS_HEARTBEAT     1000     // 1 Hz LED blink
#define AR_PERIOD_MS_HEALTH        5000     // every 5 s

// Watchdog (brief section 7)
#define AR_WATCHDOG_TIMEOUT_S        2
