/*
 * Copyright (c) 2018-2023 Nordic Semiconductor ASA
 * Copyright (c) 2017 Linaro Limited
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/dts-v1/;
#include <nordic/nrf52840_qiaa.dtsi>
#include "nrf52840_qalmari-pinctrl.dtsi"
#include <zephyr/dt-bindings/input/input-event-codes.h>
#include <zephyr/dt-bindings/led/led.h>

#include "fstab.dtsi"

/ {
	model = "Nordic NRF52840 QALMARI";
	compatible = "nordic,nrf52840-qalmari";

	chosen {
		zephyr,console = &cdc_acm_uart;
		zephyr,shell-uart = &cdc_acm_uart;
		zephyr,uart-mcumgr = &cdc_acm_uart;
		zephyr,bt-mon-uart = &cdc_acm_uart;
		zephyr,bt-c2h-uart = &cdc_acm_uart;
		zephyr,sram = &sram0;
		zephyr,flash = &flash0;
		zephyr,code-partition = &slot0_partition;
		zephyr,ieee802154 = &ieee802154;
	};

	leds {
		compatible = "gpio-leds";
		led1: led_1 {
			gpios = <&gpio0 30 GPIO_ACTIVE_LOW>;
			label = "LED 1";
			status = "okay";
		};
		led2: led_2 {
			gpios = <&gpio0 8 GPIO_ACTIVE_LOW>;
			label = "LED 2";
			status = "okay";
		};
		led3: led_3 {
			gpios = <&gpio0 20 GPIO_ACTIVE_LOW>;
			label = "LED 3";
			status = "okay";
		};
		led4: led_4 {
			gpios = <&gpio1 15 GPIO_ACTIVE_LOW>;
			label = "LED 4";
			status = "okay";
		};
		led5: led_5 {
			gpios = <&gpio1 13 GPIO_ACTIVE_LOW>;
			label = "LED 5";
			status = "okay";
		};
		led6: led_6 {
			gpios = <&gpio1 10 GPIO_ACTIVE_LOW>;
			label = "LED 6";
			status = "okay";
		};
		led7: led_7 {
			gpios = <&gpio0 29 GPIO_ACTIVE_LOW>;
			label = "LED 7";
			status = "okay";
		};
		led8: led_8 {
			gpios = <&gpio0 31 GPIO_ACTIVE_LOW>;
			label = "LED 8";
			status = "okay";
		};
	};

	pwmleds {
		compatible = "pwm-leds";
		pwm_led1: pwm_led_1 {
			pwms = <&pwm1 0 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            label = "PWM LED 1";
		};
		pwm_led2: pwm_led_2 {
			pwms = <&pwm1 1 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            label = "PWM LED 2";
		};
		pwm_led3: pwm_led_3 {
			pwms = <&pwm1 2 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            label = "PWM LED 3";
		};
		pwm_led4: pwm_led_4 {
			pwms = <&pwm1 3 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            label = "PWM LED 4";
		};
		pwm_led5: pwm_led_5 {
			pwms = <&pwm2 0 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            label = "PWM LED 5";
		};
		pwm_led6: pwm_led_6 {
			pwms = <&pwm2 1 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            label = "PWM LED 6";
		};
		pwm_led7: pwm_led_7 {
			pwms = <&pwm2 2 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            label = "PWM LED 7";
		};
		pwm_led8: pwm_led_8 {
			pwms = <&pwm2 3 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            label = "PWM LED 8";
		};
	};

	pwm {
		compatible = "pwm-leds";
		pwm_vibration: pwm_vibration {
			pwms = <&pwm0 0 PWM_MSEC(20) PWM_POLARITY_NORMAL>;
            label = "PWM vibration motor";
		};
		pwm_buzzer: pwm_buzzer {
			pwms = <&pwm0 1 PWM_MSEC(20) PWM_POLARITY_NORMAL>;
            label = "PWM buzzer";
		};

	};

	buttons {
		compatible = "gpio-keys";
		debounce-interval-ms = <100>;
		button1: button_1 {
			gpios = <&gpio0 17 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
			label = "Push button switch 1";
			zephyr,code = <INPUT_KEY_0>;
		};
		button2: button_2 {
			gpios = <&gpio0 15 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
			label = "Push button switch 2";
			zephyr,code = <INPUT_KEY_1>;
		};
	};

	interrupts {
		compatible = "gpio-keys";
		interrupt_head: interrupt_head {
			gpios = <&gpio0 23 (GPIO_ACTIVE_HIGH)>;
			label = "head interrupt pin";
		};
	};

	aliases {
		btn1 = &button1;
		btn2 = &button2;
		int-head = &interrupt_head;
		int-btn-1 = &button1;
		int-btn-2 = &button2;
		led1 = &led1;
		led2 = &led2;
		led3 = &led3;
		led4 = &led4;
		led5 = &led5;
		led6 = &led6;
		led7 = &led7;
		led8 = &led8;
		pwm-led1 = &pwm_led1;
		pwm-led2 = &pwm_led2;
		pwm-led3 = &pwm_led3;
		pwm-led4 = &pwm_led4;
		pwm-led5 = &pwm_led5;
		pwm-led6 = &pwm_led6;
		pwm-led7 = &pwm_led7;
		pwm-led8 = &pwm_led8;
		buzzer = &pwm_buzzer;
		vibration = &pwm_vibration;
		mcuboot-button0 = &button1;
		mcuboot-button1 = &button2;
		mcuboot-led0 = &led1;
		watchdog0 = &wdt0;
		pwm0 = &pwm0;
		pwm-leds = &pwm1;
		led-strip = &led_strip;
		i2c-1 = &i2c1;
		uart-0 = &uart0;
	};

	vbatt {
		status = "okay";
		compatible = "voltage-divider";
		io-channels = <&adc 2>;
		output-ohms = <100000>;
		full-ohms = <(100000 + 100000)>;
		//power-gpios = <&gpio0 11 GPIO_PULL_UP>;
	 };
};

&pwm0 {
	status = "okay";
	pinctrl-0 = <&pwm0_default>;
	pinctrl-1 = <&pwm0_sleep>;
	pinctrl-names = "default", "sleep";
};

&pwm1 {
	status = "okay";
	pinctrl-0 = <&pwm1_leds_default>;
	pinctrl-1 = <&pwm1_leds_sleep>;
	pinctrl-names = "default", "sleep";
};

&pwm2 {
	status = "okay";
	pinctrl-0 = <&pwm2_leds_default>;
	pinctrl-1 = <&pwm2_leds_sleep>;
	pinctrl-names = "default", "sleep";
};

&uart0 {
	compatible = "nordic,nrf-uarte";
	status = "okay";
	current-speed = <115200>;
	pinctrl-0 = <&uart0_default>;
	pinctrl-names = "default";
};

&spi0 {
	compatible = "nordic,nrf-spi";
	/* Cannot be used together with i2c0. */
	status = "okay";
	pinctrl-0 = <&spi0_default>;
	pinctrl-1 = <&spi0_sleep>;
	pinctrl-names = "default", "sleep";
	led_strip: ws2812-spi@0 {
		compatible = "worldsemi,ws2812-spi";
		reg = <0x0>;
		
		chain-length = <2>;
		color-mapping = <LED_COLOR_ID_GREEN
						LED_COLOR_ID_RED
						LED_COLOR_ID_BLUE>;
		spi-one-frame = <0x70>;
		spi-zero-frame = <0x40>;
		spi-max-frequency = <4000000>;
	};
};

&spi1 {
	compatible = "nordic,nrf-spi";
	//status = "okay";
	status = "disabled";
	pinctrl-0 = <&spi1_default>;
	pinctrl-1 = <&spi1_sleep>;
	pinctrl-names = "default", "sleep";
};

&i2c0 {
	/* Cannot be used together with spi0. */
	status = "disabled";
	pinctrl-0 = <&i2c0_default>;
	pinctrl-names = "default";
	clock-frequency = <I2C_BITRATE_STANDARD>;
};

&i2c1 {
	status = "okay";
	pinctrl-0 = <&i2c1_default>;
	pinctrl-names = "default";
	clock-frequency = <I2C_BITRATE_STANDARD>;
};

zephyr_udc0: &usbd {
	compatible = "nordic,nrf-usbd";
	status = "okay";
	
	cdc_acm_uart: cdc_acm_uart {
		compatible = "zephyr,cdc-acm-uart";
	};
};

&ieee802154 {
	status = "okay";
};

&adc {
	status = "okay";
};

&gpiote {
	status = "okay";
};

&uicr {
	nfct-pins-as-gpios;
	gpio-as-nreset;
};

&gpio0 {
	status = "okay";
};

&gpio1 {
	status = "okay";
};

&systick {
	status = "disabled";
};

&nfct {
	status = "disabled";
};

&egu0 {
	status = "okay";
};

&ccm {
	status = "okay";
};

&cryptocell {
	status = "okay";
};

&ecb {
	status = "okay";
};

&rng {
	status = "okay";
};

&ppi {
	status = "okay";
};

&clock {
	status = "okay";
};

// RTC0 is not okay anymore! v.4.3.99
// &rtc0 {
// 	status = "okay";
// };

&mwu {
	status = "okay";
};

&power {
	status = "okay";
};

&acl {
	status = "okay";
};

// ITM is used for printf debugging and tracing on the SWO pin
&itm {
	status = "okay";
};
