/*
 * EEG Testbed Firmware - Board Overlay
 * 
 * Target: u-blox NINA-B3 (nRF52840)
 * 
 * This overlay configures:
 * - USB CDC ACM for console
 * - SPI1 bus with 4 devices: FRAM, ADC, Accelerometer, Function Generator
 * - Custom GPIO outputs: Status LEDs, Lead-off switches, Reference control
 */

#include <zephyr/dt-bindings/adc/ad4130-adc.h>

/ {
    chosen {
        /* USB CDC ACM for console/shell */
        zephyr,console = &cdc_acm_uart0;
        zephyr,shell-uart = &cdc_acm_uart0;
        zephyr,uart-mcumgr = &cdc_acm_uart0;
    };

    aliases {
        /* Status LEDs */
        lc-red = &lc_red;
        lc-green = &lc_green;
        lc-blue = &lc_blue;
        /* Lead-off switches */
        sw-lod-vin = &sw_lod_vin;
        lo-1 = &lo_1;
        lo-2 = &lo_2;
        lo-3 = &lo_3;
        lo-4 = &lo_4;
        lo-5 = &lo_5;
        lo-6 = &lo_6;
        lo-7 = &lo_7;
        lo-8 = &lo_8;
        /* External reference control */
        ref-switch = &ref_switch;
        ref-power = &ref_power;
    };

    /* ========================================
     * Status LEDs (Active Low)
     * ======================================== */
    output_leds {
        compatible = "gpio-leds";
        status = "okay";

        lc_red: lc_red {
            gpios = <&gpio1 14 GPIO_ACTIVE_LOW>;
            label = "LC_RED";
        };
        lc_green: lc_green {
            gpios = <&gpio0 26 GPIO_ACTIVE_LOW>;
            label = "LC_GREEN";
        };
        lc_blue: lc_blue {
            gpios = <&gpio0 6 GPIO_ACTIVE_LOW>;
            label = "LC_BLUE";
        };
    };

    /* ========================================
     * Lead-Off Detection Switches (Active Low)
     * ======================================== */
    output_leadoff_sw {
        compatible = "gpio-leds";
        status = "okay";

        sw_lod_vin: sw_lod_vin {
            gpios = <&gpio0 30 GPIO_ACTIVE_LOW>;
        };
        lo_1: lo_1 { gpios = <&gpio0 14 GPIO_ACTIVE_LOW>; };
        lo_2: lo_2 { gpios = <&gpio0 15 GPIO_ACTIVE_LOW>; };
        lo_3: lo_3 { gpios = <&gpio0 16 GPIO_ACTIVE_LOW>; };
        lo_4: lo_4 { gpios = <&gpio0 24 GPIO_ACTIVE_LOW>; };
        lo_5: lo_5 { gpios = <&gpio0 25 GPIO_ACTIVE_LOW>; };
        lo_6: lo_6 { gpios = <&gpio0 3 GPIO_ACTIVE_LOW>; };
        lo_7: lo_7 { gpios = <&gpio0 28 GPIO_ACTIVE_LOW>; };
        lo_8: lo_8 { gpios = <&gpio0 2 GPIO_ACTIVE_LOW>; };
    };

    /* ========================================
     * External Reference Control (Active Low)
     * ======================================== */
    output_external_reference {
        compatible = "gpio-leds";
        status = "okay";

        ref_switch: ref_switch {
            gpios = <&gpio0 4 GPIO_ACTIVE_LOW>;
        };
        ref_power: ref_power {
            gpios = <&gpio0 5 GPIO_ACTIVE_LOW>;
        };
    };

    /* ADC channels for zephyr,user node */
    zephyr,user {
        io-channels = <&ad4130 1>, <&ad4130 2>, <&ad4130 3>, <&ad4130 4>,
                      <&ad4130 5>, <&ad4130 6>, <&ad4130 7>, <&ad4130 8>;
    };
};

/* ============================================================================
 * Peripheral Configuration
 * ============================================================================ */

&uart0 {
    status = "okay";
};

&gpio0 {
    status = "okay";
};

&gpio1 {
    status = "okay";
};

/* ============================================================================
 * SPI1 Bus Configuration
 * 
 * Shared bus with 4 devices:
 *   CS1 (P1.02): ADC (AD4130) - 5MHz
 *   CS2 (P1.03): Accelerometer (BMA400) - 5MHz
 *   CS3 (P0.17): Function Generator (AD9833) - 5MHz (uses FSYNC)
 *   CS4 (P1.11): FRAM (CY15B108QN) - 5MHz
 * ============================================================================ */
&spi3 {
    compatible = "nordic,nrf-spim";
    status = "okay";
    pinctrl-0 = <&spi3_default>;
    pinctrl-1 = <&spi3_sleep>;
    pinctrl-names = "default", "sleep";

    /* CS index: 0=AD4130, 1=BMA400, 2=AD9833, 3=FRAM */
    cs-gpios = <&gpio1 2 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>,   /* idx 0: AD4130 */
               <&gpio1 3 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>,   /* idx 1: BMA400 */
               <&gpio0 17 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>,  /* idx 2: AD9833 */
               <&gpio1 11 (GPIO_ACTIVE_LOW)>;  /* idx 3: FRAM */

    /* ADC - Analog Devices AD4130-8 (24-bit Sigma-Delta) */
    ad4130: ad4130@0 {
        compatible = "adi,ad4130-custom";
        reg = <0>;  /* CS index 0 */
        spi-max-frequency = <5000000>;
        status = "okay";

        #address-cells = <1>;
        #size-cells = <0>;
        #io-channel-cells = <1>;

        adc-mode = <0>;
        clock-type = <0>;

        /* 8 differential channels: AIN0-7 vs AIN8 (common reference) */
        channel@1 {
            reg = <1>;
            zephyr,gain = "ADC_GAIN_1";
            zephyr,reference = "ADC_REF_INTERNAL";
            zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
            zephyr,resolution = <24>;
            zephyr,input-positive = <AD4130_ADC_AIN0>;
            zephyr,input-negative = <AD4130_ADC_AIN8>;
        };
        channel@2 {
            reg = <2>;
            zephyr,gain = "ADC_GAIN_1";
            zephyr,reference = "ADC_REF_INTERNAL";
            zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
            zephyr,resolution = <24>;
            zephyr,input-positive = <AD4130_ADC_AIN1>;
            zephyr,input-negative = <AD4130_ADC_AIN8>;
        };
        channel@3 {
            reg = <3>;
            zephyr,gain = "ADC_GAIN_1";
            zephyr,reference = "ADC_REF_INTERNAL";
            zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
            zephyr,resolution = <24>;
            zephyr,input-positive = <AD4130_ADC_AIN2>;
            zephyr,input-negative = <AD4130_ADC_AIN8>;
        };
        channel@4 {
            reg = <4>;
            zephyr,gain = "ADC_GAIN_1";
            zephyr,reference = "ADC_REF_INTERNAL";
            zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
            zephyr,resolution = <24>;
            zephyr,input-positive = <AD4130_ADC_AIN3>;
            zephyr,input-negative = <AD4130_ADC_AIN8>;
        };
        channel@5 {
            reg = <5>;
            zephyr,gain = "ADC_GAIN_1";
            zephyr,reference = "ADC_REF_INTERNAL";
            zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
            zephyr,resolution = <24>;
            zephyr,input-positive = <AD4130_ADC_AIN4>;
            zephyr,input-negative = <AD4130_ADC_AIN8>;
        };
        channel@6 {
            reg = <6>;
            zephyr,gain = "ADC_GAIN_1";
            zephyr,reference = "ADC_REF_INTERNAL";
            zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
            zephyr,resolution = <24>;
            zephyr,input-positive = <AD4130_ADC_AIN5>;
            zephyr,input-negative = <AD4130_ADC_AIN8>;
        };
        channel@7 {
            reg = <7>;
            zephyr,gain = "ADC_GAIN_1";
            zephyr,reference = "ADC_REF_INTERNAL";
            zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
            zephyr,resolution = <24>;
            zephyr,input-positive = <AD4130_ADC_AIN6>;
            zephyr,input-negative = <AD4130_ADC_AIN8>;
        };
        channel@8 {
            reg = <8>;
            zephyr,gain = "ADC_GAIN_1";
            zephyr,reference = "ADC_REF_INTERNAL";
            zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
            zephyr,resolution = <24>;
            zephyr,input-positive = <AD4130_ADC_AIN7>;
            zephyr,input-negative = <AD4130_ADC_AIN8>;
        };
    };

    /* Accelerometer - Bosch BMA400 (3-axis) */
    accelerometer: accelerometer@1 {
        compatible = "custom,bma400";
        reg = <1>;  /* CS index 1 */
        spi-max-frequency = <5000000>;
        status = "okay";
    };

    /* Function Generator - Analog Devices AD9833 */
    funcgen: funcgen@2 {
        compatible = "adi,ad9833";
        reg = <2>;  /* CS index 2 */
        spi-max-frequency = <5000000>;
        fsync-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>;
        status = "okay";
    };

    /* FRAM - Infineon CY15B108QN (1MB SPI F-RAM) */
    fram: fram@3 {
        compatible = "infineon,cy15b108qn";
        reg = <3>;  /* CS index 3 */
        spi-max-frequency = <2000000>;
        status = "okay";
    };
};

/* ============================================================================
 * USB CDC ACM (Virtual COM Port)
 * ============================================================================ */
&zephyr_udc0 {
    cdc_acm_uart0: cdc_acm_uart0 {
        compatible = "zephyr,cdc-acm-uart";
    };

    cdc_acm_uart1: cdc_acm_uart1 {
        compatible = "zephyr,cdc-acm-uart";
    };
};

/* ============================================================================
 * Pin Control (SPI1 Pins)
 * ============================================================================ */
&pinctrl {
    spi3_default: spi1_default {
        sck {
            psels = <NRF_PSEL(SPIM_SCK, 0, 13)>;
        };

        data {
            psels = <NRF_PSEL(SPIM_MOSI, 1, 1)>,
                    <NRF_PSEL(SPIM_MISO, 1, 8)>;
        };
    };

    spi3_sleep: spi1_sleep {
        sck {
            psels = <NRF_PSEL(SPIM_SCK, 0, 13)>;
            low-power-enable;
        };

        data {
            psels = <NRF_PSEL(SPIM_MOSI, 1, 1)>,
                    <NRF_PSEL(SPIM_MISO, 1, 8)>;
            low-power-enable;
        };
    };
};

/* ============================================================================
 * Disable Unused Board Defaults
 * ============================================================================ */
&led0 { status = "disabled"; };
&led1 { status = "disabled"; };
&led2 { status = "disabled"; };
&button0 { status = "disabled"; };
&button1 { status = "disabled"; };
&red_pwm_led { status = "disabled"; };
&blue_pwm_led { status = "disabled"; };
&green_pwm_led { status = "disabled"; };
&pwm0 { status = "disabled"; };
&i2c0 { status = "disabled"; };
&spi0 { status = "disabled"; };
&spi1 { status = "disabled"; };
&spi2 { status = "disabled"; };
