/**
 * @file cal_tables_rom.c (families/t06211/compact_src)
 * @brief ROM-decoded t06211 calibration.
 *
 * AUTO-GENERATED by tools/extract_calibration.py
 * Source ROM: rom_eeprom_dump_0000-9FFF_504012.bin
 * Calibration base (RWA4): 0x9BD8
 * Flash anchor: 0x7E18
 * Generated: 2026-04-30 13:02:51
 *
 * DO NOT EDIT — regenerate with:
 *   python tools/extract_calibration.py --family t06211
 */
#include "pwm.h"

/* ── Submap x/y arrays ──────────────────────────────────────────────── */

static const int16_t setpoint_x[6] = { 8389, 5872, 4614, 2726, 1426, 0 };
static const int16_t setpoint_y[6] = { 1707, 1707, 1707, 939, 427, 427 };

static const int16_t pwm_A_x[9] = { 25166, 18455, 13841, 8389, 5872, 4614, 2726, 1426, 0 };
static const int16_t pwm_A_y[9] = { 1707, 1365, 1195, 768, 427, 85, 0, -171, -469 };

static const int16_t pwm_B_x[10] = { 1707, 1365, 1195, 768, 427, 85, 0, -171, -469, -512 };
static const int16_t pwm_B_y[10] = { 819, 737, 492, 0, 41, 49, 82, 102, 205, 205 };

static const int16_t shape_x[4] = { 819, 737, 492, 0 };
static const int16_t shape_y[4] = { 41, 49, 82, 102 };

/* ── Y-tables (dereferenced from pwm_y_table_ptr / shape_y_table_ptr) ── */

static const int16_t shape_y_table_rom[4] = { 41, 49, 82, 102 };

static const int16_t pwm_y_table_rom[90] = {
    /* row 0 */   205,   205,   205,   205,   205,   205,     0,     0,     0,
    /* row 1 */  1638,  1392,  1229,   962,   778,   512,     0,     0,     0,
    /* row 2 */  1843,  1577,  1433,  1208,  1003,   758,     0,     0,     0,
    /* row 3 */  2457,  2109,  1986,  1761,  1577,  1229,   287,     0,     0,
    /* row 4 */  2867,  2744,  2641,  2457,  2314,  2129,  1269,   205,   205,
    /* row 5 */  3686,  3481,  3460,  3378,  3276,  3174,  2907,  1024,   737,
    /* row 6 */  3890,  3870,  3849,  3829,  3808,  3788,  3235,  1310,  1024,
    /* row 7 */  3890,  3890,  3890,  3890,  3890,  3890,  3890,  2076,  1638,
    /* row 8 */  3890,  3890,  3890,  3890,  3890,  3890,  3890,  3890,  3481,
    /* row 9 */  3890,  3890,  3890,  3890,  3890,  3890,  3890,  3890,  3890,
};

/* ── Descriptors ────────────────────────────────────────────────────── */

pwm_submap_descr_t pwm_submap_descrs[PWM_SUBMAP_COUNT] = {
    [PWM_SUBMAP_SETPOINT_INTERP] = {
        .flags = 0, .input_ptr = NULL, .count = 6,
        .x = setpoint_x, .input_addr = 0x0040,
    },
    [PWM_SUBMAP_PWM_A] = {
        .flags = 0, .input_ptr = NULL, .count = 9,
        .x = pwm_A_x, .input_addr = 0x0040,
    },
    [PWM_SUBMAP_PWM_B] = {
        .flags = 0, .input_ptr = NULL, .count = 10,
        .x = pwm_B_x, .input_addr = 0x0046,
    },
    [PWM_SUBMAP_SHAPE_EVAL] = {
        .flags = 0, .input_ptr = NULL, .count = 4,
        .x = shape_x, .input_addr = 0x0142,
    },
};

const int16_t *pwm_submap_y_of(uint16_t idx)
{
    switch (idx) {
    case PWM_SUBMAP_SETPOINT_INTERP: return setpoint_y;
    case PWM_SUBMAP_PWM_A: return pwm_A_y;
    case PWM_SUBMAP_PWM_B: return pwm_B_y;
    case PWM_SUBMAP_SHAPE_EVAL: return shape_y;
    default: return NULL;
    }
}

/* ── Scalars ────────────────────────────────────────────────────────── */

const pwm_calibration_t pwm_cal_rom = {
    .large_pos_error_thresh = 128,  /* cal+0x10E */
    .large_neg_error_thresh = (int16_t)0xFF00,  /* cal+0x110 */
    .pi_low_clamp           = (int16_t)0xFE00,  /* cal+0x120 */
    .pi_high_clamp          = 1707,  /* cal+0x124 */

    /* CAN-decoded setpoint (FUN_64c3) cal constants */
    .b_fb_kw_upper_bound    = 7680,  /* cal+0x004 */
    .b_fb_kw_lower_bound    = (int16_t)0xFD00,  /* cal+0x006 */
    /* setpoint_offset = cal+0x4c - cal+0x4e = 3499 - 4156 = -657 */
    .setpoint_offset        = (int16_t)0xFD6F,
    .target_5e_min_clamp    = (int16_t)0xFE00,  /* cal+0x122 */
    .can_aux_12e_max        = 1451,  /* cal+0x002 */
    .error_thresh_114       = 100,  /* cal+0x114 */
    .pi_thresh_116          = 96,  /* cal+0x116 */
    .pi_sat_count_threshold = 800,  /* cal+0x112 = 0x0320 */
    .rpm_threshold_11E      = 2936,  /* cal+0x11E */
    .pi_cl_rpm_floor        = 420,  /* RAM[0x605c] = 0x01A4 (flash mirror) */

    .pwm_detail_x0          = (int16_t)0x9C40,  /* cal+0x0EE */
    .pwm_detail_x1          = (int16_t)0x8235,  /* cal+0x0F0 */
    .pwm_cached_ptr_0F2     = 5662,  /* cal+0x0F2 (legacy scalar) */
    .pwm_cached_ptr_102     = 168,  /* cal+0x102 (legacy scalar) */

    /* RPM-window matching: inline 8-int16 band array at cal+0x0F2..0x100
     * (4 (lo,hi) pairs) plus halfwidth at cal+0x102. Drives the three-
     * phase pwm_period slew in FUN_5314 (open-questions §5 closeout). */
    .pwm_rpm_windows        = { 5662, 6795, 8808, 10486, 11954, 13422, 18036, 19713 },
    .pwm_window_halfwidth   = 168,

    /* pwm_const_104 / pwm_slew_step are the same value (cal+0x104 = 354);
     * pwm_const_104 retained for backward compat, pwm_slew_step is the
     * semantic name used by the RPM-window matcher (per-cycle pwm_period
     * slew magnitude, applied at Phase 1 / Phase 3). Cached to
     * RAM[0x02ec] at FUN_5314:0x53b9. */
    .pwm_const_104          = 354,  /* cal+0x104 (legacy alias) */
    .pwm_slew_step          = 354,  /* cal+0x104 (semantic) */

    .pwm_y_table            = pwm_y_table_rom,    /* cal+0x154 @ 0x9E28 */
    .shape_y_table          = shape_y_table_rom,  /* cal+0x15E @ 0x9E20 */

    /* CL-correction gain — cached at absolute ROM[0x6056], not cal-relative. */
    .closed_loop_gain_const = 10,  /* ROM[0x6056] */

    /* PI runtime-reset values — boot-derived in ROM (FUN_76aa @ 0x76aa).
     * Trim bytes at RAM[0x0410-0x0416] have no writers so each cal base
     * passes through verbatim. Field shape mirrors T06215's
     * pwm_calibration_t for cross-family consistency. */
    .init_p_shape_bound_pos    = 853,  /* cal+0x10A */
    .init_p_shape_bound_neg    = (int16_t)0xFCAB,  /* cal+0x10C */
    .init_p_gain_normal        = 480,  /* cal+0x118 */
    .init_integ_step_normal    = 256,  /* cal+0x11A */
    .init_open_loop_p_gain     = 6,  /* cal+0x11C low byte (clamped <=15) */
    .init_pos_error_normalizer = 350,  /* cal+0x108 */
    .init_neg_error_normalizer = 350,  /* cal+0x106 */

    /* PWM period endpoints (alias of pwm_detail_x0/x1) as unsigned. */
    .pwm_period_min         = 33333,  /* cal+0x0F0 */
    .pwm_period_max         = 40000,  /* cal+0x0EE */

    /* Duty clamp bounds — RAM[0x6058]/RAM[0x605a] flash mirrors with the
     * same defaults as the default family's pwm_min/pwm_max. */
    .pwm_min                = 0x00CD,  /* RAM[0x6058] = 205  */
    .pwm_max                = 0x0F32,  /* RAM[0x605a] = 3890 */
};

/* Family-1 API parity placeholder — t06211 keeps Y-tables inside
 * pwm_cal_rom, so pwm_flash_rom has no data. Callers pass &pwm_flash_rom
 * to pwm_init() purely for signature compatibility. */
const pwm_flash_t pwm_flash_rom = { 0 };
