/*
 * Copyright (c) 2021 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include <zephyr/dt-bindings/ipc_service/static_vrings.h>

/ {
    chosen {
        nordic,pm-ext-flash = &eeprom_m95p32;
    };
    aliases
    {
        motor-en = &out_motor_en;
        motor-m1 = &out_motor_m1;
        motor-m2 = &out_motor_m2;
        eeprom-wp = &out_eeprom_wp;
        comm-data = &out_comm_data;
        comm-clk = &out_comm_clk;
        spi-eeprom = &eeprom_m95p32;
        spi-flash0 = &eeprom_m95p32;
    };
    sunion_signal
    {
        compatible = "gpio-leds";
        out_motor_en: user_output0
        {
            gpios = <&gpio1 14 GPIO_ACTIVE_HIGH>;
            label = "Motor gpio 0";
        };
        out_motor_m1: user_output1
        {
            gpios = <&gpio1 12 GPIO_ACTIVE_HIGH>;
            label = "Motor gpio 1";
        };
        out_motor_m2: user_output2
        {
            gpios = <&gpio1 11 GPIO_ACTIVE_HIGH>;
            label = "Motor gpio 2";
        };
        out_eeprom_wp: user_output3
        {
            gpios = <&gpio0 27 GPIO_ACTIVE_HIGH>;
            label = "eeprom gpio 0";
        };
        out_comm_clk: user_output4
        {
            gpios = <&gpio1 4 GPIO_ACTIVE_LOW>;
            label = "comm gpio 0";
        };
        out_comm_data: user_output5
        {
            gpios = <&gpio1 5 GPIO_ACTIVE_LOW>;
            label = "comm gpio 1";
        };
    };
    zephyr,user
    {
        io-channels = <&adc 0>;
        tiny-gpios = <&gpio0 25 (GPIO_ACTIVE_LOW|GPIO_PULL_UP)>;
        aw9523-gpios = <&gpio0 12 (GPIO_ACTIVE_LOW|GPIO_PULL_UP)>;
        set-gpios = <&gpio0 9 (GPIO_ACTIVE_LOW|GPIO_PULL_UP)>;
        reset-gpios = <&gpio1 7 (GPIO_ACTIVE_LOW|GPIO_PULL_UP)>;
        test2-gpios = <&gpio0 8 (GPIO_ACTIVE_LOW|GPIO_PULL_UP)>;
    };
    pwmleds {
        compatible = "pwm-leds";
        pwm_led0: pwm_led_0 {
            pwms = <&pwm0 0 PWM_USEC(363U) PWM_POLARITY_NORMAL>;
        };
    };
};

/* Set IPC thread priority to the highest value to not collide with other threads. */
&ipc0 {
    zephyr,priority = <0 PRIO_COOP>;
};

/* Disable unused peripherals to reduce power consumption */
&spi2 {
	status = "disabled";
};
&usbd {
	status = "disabled";
};

/* Disable unused peripherals to reduce power consumption */
&button2 {
	status = "disabled";
};
&button3 {
	status = "disabled";
};
&qspi
{
    status = "disabled";
    pinctrl-0 = <&qspi_default>;
    pinctrl-1 = <&qspi_sleep>;
    pinctrl-names = "default", "sleep";
    mx25r64: mx25r6435f@0
    {
        compatible = "nordic,qspi-nor";
        reg = <0>;
        /* MX25R64 supports only pp and pp4io */
        writeoc = "pp4io";
        /* MX25R64 supports all readoc options */
        readoc = "read4io";
        sck-frequency = <8000000>;
        jedec-id = [c2 28 17];
        sfdp-bfp =
        [
            e5 20 f1 ff  ff ff ff 03  44 eb 08 6b  08 3b 04 bb
            ee ff ff ff  ff ff 00 ff  ff ff 00 ff  0c 20 0f 52
            10 d8 00 ff  23 72 f5 00  82 ed 04 cc  44 83 68 44
            30 b0 30 b0  f7 c4 d5 5c  00 be 29 ff  f0 d0 ff ff
		];
        size = <67108864>;
        has-dpd;
        t-enter-dpd = <10000>;
        t-exit-dpd = <35000>;
    };
};

/delete-node/ &mx25r64;
&spi4 {
	status = "disabled";
};
&usbd {
	status = "disabled";
};


&led0 {
    status = "disabled";
};
&led1 {
    status = "disabled";
};
&led2 {
    status = "disabled";
};
&led3 {
    status = "disabled";
};

&spi3 {
	compatible = "nordic,nrf-spim";
	status = "okay";
	pinctrl-0 = <&spi3_default>;
	pinctrl-1 = <&spi3_sleep>;
	pinctrl-names = "default", "sleep";
	cs-gpios = <&gpio0 18 GPIO_ACTIVE_LOW>;
	eeprom_m95p32:eeprom_m95p32@0 {
		compatible = "atmel,at25";
		reg = <0>;
		size = <DT_SIZE_K(512)>;
		pagesize = <512>;
		address-width = <24>;
		spi-max-frequency = <8000000>;
		timeout = <5>;
        wp-gpios = <&gpio0 15 (GPIO_ACTIVE_HIGH)>;
	};
};
// &spi3
// {
//     status = "okay";
//     cs-gpios = <&gpio0 18 GPIO_ACTIVE_LOW>;
//     pinctrl-0 = <&spi3_default>;
//     pinctrl-1 = <&spi3_sleep>;
//     pinctrl-names = "default", "sleep";

//     mx25r64: mx25r6435f@0
//     {
//         compatible = "jedec,spi-nor";
//         status = "okay";
//         reg = <0>;
//         spi-max-frequency = <8000000>;
//         label = "MX25R64";
//         jedec-id = [c2 28 17];
//         sfdp-bfp =
//         [
//             e5 20 f1 ff  ff ff ff 03  44 eb 08 6b  08 3b 04 bb
//             ee ff ff ff  ff ff 00 ff  ff ff 00 ff  0c 20 0f 52
//             10 d8 00 ff  23 72 f5 00  82 ed 04 cc  44 83 68 44
//             30 b0 30 b0  f7 c4 d5 5c  00 be 29 ff  f0 d0 ff ff
//         ];
//         size = <67108864>;
//         has-dpd;
//         t-enter-dpd = <10000>;
//         t-exit-dpd = <35000>;
//     };
// };

&i2c1
{
    compatible = "nordic,nrf-twim";
    status = "okay";

    pinctrl-0 = <&i2c1_default_alt_alt>;
    pinctrl-1 = <&i2c1_sleep_alt_alt>;
    pinctrl-names = "default", "sleep";
    zephyr,concat-buf-size = <66>;
    outside_dev: outside@5B
    {
       compatible = "sunion,outside";
       reg = <0x5B>;
       //int-gpios = <&gpio0 0 GPIO_ACTIVE_LOW>;
    };
    eeprom_dev: store@50
    {
        compatible = "sunion,eeprom";
        reg = <0x50>;
    };

    rtc_dev: timer@32
    {
        compatible = "sunion,rtc";
        reg = <0x32>;
    };
};

/* Marcus added on 20230623 : change pwm0 pin from pin 0.28 to pin 0.26 */
&pinctrl
{
    pwm0_default: pwm0_default {
        phandle = < 0xb >;
        group1 {
            psels = <NRF_PSEL(PWM_OUT0, 0, 26)>;
        };
    };
    pwm0_sleep: pwm0_sleep {
        phandle = < 0xc >;
        group1 {
            psels = <NRF_PSEL(PWM_OUT0, 0, 26)>;
            low-power-enable;
        };
    };
   i2c1_default_alt_alt: i2c1_default_alt_alt
   {
        group1
        {
            psels = <NRF_PSEL(TWIM_SDA, 1, 2)>,
                    <NRF_PSEL(TWIM_SCL, 1, 3)>;
            bias-pull-up;
        };
    };

    i2c1_sleep_alt_alt: i2c1_sleep_alt_alt
    {
        group1
        {
            psels = <NRF_PSEL(TWIM_SDA, 1, 2)>,
                    <NRF_PSEL(TWIM_SCL, 1, 3)>;
            low-power-enable;
        };
    };
    spi3_default: spi3_default {
		group1 {
			psels = <NRF_PSEL(SPIM_SCK, 0, 17)>,
				<NRF_PSEL(SPIM_MOSI, 0, 13)>,
				<NRF_PSEL(SPIM_MISO, 0, 14)>;
		};
	};

	spi3_sleep: spi3_sleep {
		group1 {
			psels = <NRF_PSEL(SPIM_SCK, 0, 17)>,
				<NRF_PSEL(SPIM_MOSI, 0, 13)>,
				<NRF_PSEL(SPIM_MISO, 0, 14)>;
			low-power-enable;
		};
	};
};
&adc {
	#address-cells = <1>;
	#size-cells = <0>;
    status = "okay";

	channel@0 {
		reg = <0>;
		zephyr,gain = "ADC_GAIN_1_6";
		zephyr,reference = "ADC_REF_INTERNAL";
		zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
		zephyr,input-positive = <NRF_SAADC_AIN0>; /* P0.04 */
		zephyr,resolution = <12>;
	};
};

&rtc0 {
    status = "okay";
};

// &uart0 {
//     status = "disabled";
// };

// &gpio_fwd {
//     status = "disabled";
// };

