Problem configuring devicetree nRF5340 new board

Hello Everyone,

I am new to nordic devices, but i have been for the past 2 weeks trying to build something. First I have created a new board "Holyiot_21069" with Visual Studio code with "Create a new board", and checked all configurations to nrf5340 chip. This created all the files necessary, including the boar dts and pinctrl.dtsi files.

Now, I cant build a project, and the problem persists with the same error:

-- Found BOARD.dts: C:/ncs/v2.9.0/zephyr/boards/nordic/holyiot_21069/holyiot_21069_nrf5340_cpuapp.dts
Traceback (most recent call last):
File "C:\ncs\v2.9.0\zephyr\scripts\dts\gen_defines.py", line 1120, in <module>
main()
File "C:\ncs\v2.9.0\zephyr\scripts\dts\gen_defines.py", line 59, in main
edt = edtlib.EDT(args.dts, args.bindings_dirs,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ncs\v2.9.0\zephyr\scripts\dts\python-devicetree\src\devicetree\edtlib.py", line 2069, in __init__
self._finish_init()
File "C:\ncs\v2.9.0\zephyr\scripts\dts\python-devicetree\src\devicetree\edtlib.py", line 2077, in _finish_init
self._init_nodes()
File "C:\ncs\v2.9.0\zephyr\scripts\dts\python-devicetree\src\devicetree\edtlib.py", line 2348, in _init_nodes
node._init_props(default_prop_types=self._default_prop_types,
File "C:\ncs\v2.9.0\zephyr\scripts\dts\python-devicetree\src\devicetree\edtlib.py", line 1491, in _init_props
self._init_prop(prop_spec, err_on_deprecated)
File "C:\ncs\v2.9.0\zephyr\scripts\dts\python-devicetree\src\devicetree\edtlib.py", line 1512, in _init_prop
val = self._prop_val(name, prop_type, prop_spec.deprecated,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ncs\v2.9.0\zephyr\scripts\dts\python-devicetree\src\devicetree\edtlib.py", line 1608, in _prop_val
return prop.to_nums()
^^^^^^^^^^^^^^
File "C:\ncs\v2.9.0\zephyr\scripts\dts\python-devicetree\src\devicetree\dtlib.py", line 404, in to_nums
_err("expected property '{0}' on {1} in {2} to be assigned with "
File "C:\ncs\v2.9.0\zephyr\scripts\dts\python-devicetree\src\devicetree\dtlib.py", line 2095, in _err
raise DTError(msg)
devicetree.dtlib.DTError: expected property 'gpios' on /soc/peripheral@50000000/gpio@842500/gpio_led in C:/ncs/v2.9.0/zephyr/misc/empty_file.c to be assigned with 'gpios = < (number) (number) ... >;', not 'gpios = < &gpio0 0x14 0x0 >;'
CMake Error at C:/ncs/v2.9.0/zephyr/cmake/modules/dts.cmake:295 (execute_process):
execute_process failed command indexes:

1: "Child return code: 1"

This is my pinctrl.dtsi and dts files:

/*
 * Copyright (c) 2023 Your Name
 * SPDX-License-Identifier: Apache-2.0
 */


 &pinctrl {
     spi1_default: spi1_default {
         group1 {
             psels = <NRF_PSEL(SPIM_SCK, 0, 7)>,
                     <NRF_PSEL(SPIM_MOSI, 0, 5)>,
                     <NRF_PSEL(SPIM_MISO, 0, 6)>;
         };
     };
 
     spi1_sleep: spi1_sleep {
         group1 {
             psels = <NRF_PSEL(SPIM_SCK, 0, 7)>,
                     <NRF_PSEL(SPIM_MOSI, 0, 5)>,
                     <NRF_PSEL(SPIM_MISO, 0, 6)>;
             low-power-enable;
         };
     };
 };

/dts-v1/;
#include <nordic/nrf5340_cpuapp_qkaa.dtsi>
#include "holyiot_21069-pinctrl.dtsi"

/ {
	model = "Holyiot 21069 nRF5340 Application Core";
	compatible = "nordic,holyiot-21069-cpuapp";

	chosen {
		zephyr,sram = &sram0_image;
		zephyr,flash = &flash0;
		zephyr,code-partition = &slot0_partition;
		zephyr,sram-secure-partition = &sram0_s;
		zephyr,sram-non-secure-partition = &sram0_ns;
		
	};
};


&spi1 {
    compatible = "nordic,nrf-spim";
    pinctrl-0 = <&spi1_default>;
	pinctrl-1 = <&spi1_sleep>;
    pinctrl-names = "default", "sleep";
    status = "okay";
    cs-gpios = <&gpio0 4 GPIO_ACTIVE_LOW>;  /* Optional CS pin (P0.15) */
};

&gpio0 {
    status = "okay";

    gpio_led: gpio_led {
        gpios = <&gpio0 20 GPIO_ACTIVE_HIGH>;
        label = "LED 0";
        gpio-hog;
    };

    start_button: start_button {
        gpios = <&gpio0 22 GPIO_ACTIVE_HIGH>;
        label = "Start Button";
        gpio-hog;
    };

    reset_button: reset_button {
        gpios = <&gpio0 29 GPIO_ACTIVE_HIGH>;
        label = "Reset Button";
        gpio-hog;
    };

    drdy_input: drdy_input {
        gpios = <&gpio0 3 GPIO_ACTIVE_LOW>;
        label = "DRDY Input";
        gpio-hog;
        input;
        bias-pull-up;
    };
};

#include "holyiot_21069-cpuapp_partitioning.dtsi"
#include "holyiot_21069-shared_sram.dtsi"

Parents Reply Children
  • Today I have already made some modifications. I based my board files into nrf5340dk cpuapp.

    //holyiot_21069_common.dtsi
    
    *
     * Copyright (c) 2020 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
     / {
    	leds {
    		compatible = "gpio-leds";
            gpio_led: gpio_led {
                gpios = <&gpio0 20 GPIO_ACTIVE_HIGH>;
                label = "LED 0";
    //            zephyr,code=<OUTPUT_LED_0>;
            };
        
            start_signal: start_signal {
                gpios = <&gpio0 22 GPIO_ACTIVE_HIGH>;
                label = "Start Button";
    //            zephyr,code=<OUTPUT_LED_1>;
            };
        
            reset_signal: reset_signal {
                gpios = <&gpio0 29 GPIO_ACTIVE_HIGH>;
                label = "Reset Button";
     //           zephyr,code=<OUTPUT_LED_2>;
               
            };
        
            };
        
    
        buttons {
            compatible = "gpio-keys";
            drdy_input: drdy_input {
                gpios = <&gpio0 3 GPIO_ACTIVE_HIGH>;
                label = "DRDY Input";
            };
        };
    
    	/* These aliases are provided for compatibility with samples */
    	aliases {
    		gpioled = &gpio_led;
    		gpiostart = &start_signal;
    		gpioreset = &reset_signal;
    		gpiodrdy = &drdy_input;
    	};
    
    };

    //holyiot_21069_cpuapp_common.dtsi
    
    /*
     * Copyright (c) 2019-2020 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: Apache-2.0
     */
     #include "holyiot_21069_common.dtsi"
     #include "holyiot_21069-pinctrl.dtsi"
     #include <zephyr/dt-bindings/input/input-event-codes.h>
     
     / {
     
         chosen {
             zephyr,console = &uart0;
             zephyr,shell-uart = &uart0;
             zephyr,uart-mcumgr = &uart0;
             zephyr,bt-mon-uart = &uart0;
             zephyr,bt-c2h-uart = &uart0;
             zephyr,bt-hci = &bt_hci_ipc0;
             nordic,802154-spinel-ipc = &ipc0;
             zephyr,ieee802154 = &ieee802154;
         };
     
         pwmleds {
             compatible = "pwm-leds";
             pwm_led0: pwm_led_0 {
                 pwms = <&pwm0 0 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
             };
         };
     
     
     //	gpio_fwd: nrf-gpio-forwarder {
     //		compatible = "nordic,nrf-gpio-forwarder";
     //		status = "okay";
     //		uart {
     //			gpios = <&gpio1 1 0>, <&gpio1 0 0>, <&gpio0 11 0>, <&gpio0 10 0>;
     //		};
     //	};
     
         /* These aliases are provided for compatibility with samples */
         aliases {
             pwm-led0 = &pwm_led0;
         };
     };
     
     &vregmain {
         regulator-initial-mode = <NRF5X_REG_MODE_DCDC>;
     };
     
     &vregradio {
         regulator-initial-mode = <NRF5X_REG_MODE_DCDC>;
     };
     
     &vregh {
         status = "okay";
     };
     
     &adc {
         status = "okay";
     };
     
     &gpiote {
         status = "okay";
     };
     
     &gpio0 {
         status = "okay";
     };
     
     &gpio1 {
         status = "okay";
     };
     
     &spi1 {
        compatible = "nordic,nrf-spim";
        pinctrl-0 = <&spi1_default>;
    	pinctrl-1 = <&spi1_sleep>;
        pinctrl-names = "default", "sleep";
        status = "okay";
        cs-gpios = <&gpio0 4 GPIO_ACTIVE_LOW>;  /* Optional CS pin (P0.15) */
    };
    
     &ieee802154 {
         status = "okay";
     };
     
     zephyr_udc0: &usbd {
         compatible = "nordic,nrf-usbd";
         status = "okay";
     };
     
     /* Include default memory partition configuration file */
     #include <common/nordic/nrf5340_cpuapp_partition.dtsi>

    //holyiot_21069_nrf5340_cpuapp.dts
    
    /dts-v1/;
    #include <nordic/nrf5340_cpuapp_qkaa.dtsi>
    #include "holyiot_21069_cpuapp_common.dtsi"
    
    / {
    	model = "Holyiot 21069 nRF5340 Application Core";
    	compatible = "nordic,holyiot-21069-cpuapp";
    
    	chosen {
    		zephyr,sram = &sram0_image;
    		zephyr,flash = &flash0;
    		zephyr,code-partition = &slot0_partition;
    		zephyr,sram-secure-partition = &sram0_s;
    		zephyr,sram-non-secure-partition = &sram0_ns;
    		
    	};
    };
    
    #include "holyiot_21069-cpuapp_partitioning.dtsi"
    #include "holyiot_21069-shared_sram.dtsi"
    
    
    &adc {
    	status = "disabled";
    };

    /*
     * Copyright (c) 2023 Your Name
     * SPDX-License-Identifier: Apache-2.0
     */
    //holyiot_21069-pinctrl.dtsi
    
     &pinctrl {
         spi1_default: spi1_default {
             group1 {
                 psels = <NRF_PSEL(SPIM_SCK, 0, 7)>,
                         <NRF_PSEL(SPIM_MOSI, 0, 5)>,
                         <NRF_PSEL(SPIM_MISO, 0, 6)>;
             };
         };
     
         spi1_sleep: spi1_sleep {
             group1 {
                 psels = <NRF_PSEL(SPIM_SCK, 0, 7)>,
                         <NRF_PSEL(SPIM_MOSI, 0, 5)>,
                         <NRF_PSEL(SPIM_MISO, 0, 6)>;
                 low-power-enable;
             };
         };
     };

    // src/main.c
    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/drivers/spi.h>
    #include <zephyr/logging/log.h>
    #include <zephyr/sys/printk.h>
    #include <zephyr/bluetooth/bluetooth.h>
    #include <zephyr/bluetooth/gatt.h>
    #include <zephyr/sys/byteorder.h>
    
    LOG_MODULE_REGISTER(test5);
    // Define the device labels for SPI and GPIOs
    #define SPI1_NODE DT_NODELABEL(spi1)
    /* The devicetree node identifier for the "led0" alias. */
    #define LED0_NODE DT_ALIAS(gpioled)
    #define START_NODE DT_ALIAS(gpiostart)
    #define RESET_NODE DT_ALIAS(gpioreset)
    #define DRDY_NODE DT_ALIAS(gpiodrdy)
    #define GPIO_DRDY_PIN 3
    
    static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
    static const struct gpio_dt_spec start = GPIO_DT_SPEC_GET(START_NODE, gpios);
    static const struct gpio_dt_spec reset = GPIO_DT_SPEC_GET(RESET_NODE, gpios);
    
    
    // SPI configuration
    #define SPI_OPERATION (SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8))
     const struct device *spi_dev;
     const struct device *gpio_dev;
     /* SPI configuration */
    static const struct spi_config spi_cfg = {
        .frequency = 3000000, /* 1 MHz */
        .operation = SPI_OPERATION,
        .slave = 0,
    };
    #define BNO086_ROTATION_VECTOR_REPORT_ID 0x05
    
    /* Callback structure */
    static struct gpio_callback drdy_cb;
    
    // Task thread stack sizes
    #define STACK_SIZE 1024
    #define BUFFER_SIZE 224      // Sending buffer size: 224 bytes
    #define SAMPLE_SIZE 16       // Fused data sample size: 16 bytes (4 floats)
    // Double buffers
    static uint8_t buffer1[BUFFER_SIZE];
    static uint8_t buffer2[BUFFER_SIZE];
    static uint8_t *active_buffer = buffer1;
    static uint8_t *processing_buffer = buffer2;
    int contador=0;
    // Shared variables
    static struct k_thread producer_thread;
    static struct k_thread consumer_thread;
    static K_THREAD_STACK_DEFINE(producer_stack, 2048);
    static K_THREAD_STACK_DEFINE(consumer_stack, 2048);
    // BLE connection and characteristic handles
    static struct bt_conn *current_conn;
    static uint16_t gatt_handle = 0x0012; // <-- Set correct handle for BLE characteristic
    
    
    // Forward declarations
    /* Callback function prototype */
    /* Function Prototypes */
    static void drdy_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins);
    static void producer_task(void *arg1, void *arg2, void *arg3);
    static void consumer_task(void *arg1, void *arg2, void *arg3);
    void configure_gpios(void);
    void configure_bno086(void);
    void start_ble(void);
    void send_data(uint8_t *data);
    
    
    
    static void drdy_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins) {
        k_wakeup(&producer_thread);
    }
    
    
    void configure_gpios(void) {
    
        
       int ret;
       spi_dev = DEVICE_DT_GET(SPI1_NODE);
       if (!device_is_ready(spi_dev)) {
        printk("Error: SPI1 device is not ready\n");
        return;
        }
    
    	ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
    	if (ret < 0) {
    		return;
    	}
    
          // Configure GPIOs
        ret = gpio_pin_configure_dt(&start, GPIO_OUTPUT_ACTIVE);
        if (ret < 0) {
            printk("Failed to configure LED GPIO\n");
            return;
        }
    
        ret = gpio_pin_configure_dt(&reset, GPIO_OUTPUT_ACTIVE);
        if (ret < 0) {
            printk("Failed to configure START GPIO\n");
            return;
        }
    
        gpio_dev=DEVICE_DT_GET(DRDY_NODE);
        if (gpio_dev == NULL) {
            printk("Failed to get SPI device binding\n");
            return;
        }
        ret = gpio_pin_configure(gpio_dev, GPIO_DRDY_PIN, GPIO_INPUT | GPIO_PULL_UP);
        if (ret != 0) {
            printk("Error: Failed to configure DRDY pin\n");
            return;
        }
        // Set up GPIO callback for DRDY pin
       gpio_init_callback(&drdy_cb, drdy_callback, BIT(GPIO_DRDY_PIN));
        gpio_add_callback(gpio_dev, &drdy_cb);
        gpio_pin_interrupt_configure(gpio_dev, GPIO_DRDY_PIN, GPIO_INT_EDGE_TO_INACTIVE);
        gpio_pin_set_dt(&start, 0);
        
    }
    
    void configure_bno086(void){
         uint8_t set_feature_cmd[] = {
            0xFD, // Set Feature Command
            BNO086_ROTATION_VECTOR_REPORT_ID, // Rotation Vector Report ID
            0x00, // Reserved
            0x90, 0x01, 0x00, 0x00, // Sample Rate = 400 Hz (in microseconds)
        };
    
        struct spi_buf tx_buf = { .buf = set_feature_cmd, .len = sizeof(set_feature_cmd) };
        struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 };
    
        spi_write(spi_dev, &spi_cfg, &tx);
        LOG_INF("BNO086 configured for 400 Hz Rotation Vector");
    }
    
    /* BLE Connection Callback */
    static void connected(struct bt_conn *conn, uint8_t err) {
        if (err) {
            LOG_ERR("Failed to connect (err: %d)", err);
            return;
        }
        LOG_INF("Connected");
        current_conn = conn;
    }
    
    /* BLE Disconnection Callback */
    static void disconnected(struct bt_conn *conn, uint8_t reason) {
        LOG_INF("Disconnected (reason: %d)", reason);
        current_conn = NULL;
    }
    
    /* Send Data via BLE */
    void send_data(uint8_t *data) {
        if (!current_conn) {
            LOG_WRN("Not connected");
            return;
        }
        int err = bt_gatt_write_without_response(current_conn, gatt_handle, data, BUFFER_SIZE, false);
        if (err) {
            LOG_ERR("Failed to send data (err: %d)", err);
        } else {
            LOG_INF("Data sent");
        }
    }
    
    
    
    /* BLE Initialization */
    void start_ble(void) {
        int err = bt_enable(NULL);
        if (err) {
            LOG_ERR("Bluetooth init failed (err %d)", err);
            return;
        }
        LOG_INF("Bluetooth initialized");
    
        static struct bt_conn_cb conn_callbacks = {
            .connected = connected,
            .disconnected = disconnected,
        };
        bt_conn_cb_register(&conn_callbacks);
    
        struct bt_le_adv_param adv_params = BT_LE_ADV_PARAM_INIT(
            BT_LE_ADV_OPT_CONNECTABLE,
            BT_GAP_ADV_FAST_INT_MIN_2,
            BT_GAP_ADV_FAST_INT_MAX_2,
            NULL
        );
    
        err = bt_le_adv_start(&adv_params, NULL, 0, NULL, 0);
        if (err) {
            LOG_ERR("Advertising failed (err: %d)", err);
        } else {
            LOG_INF("Advertising started");
        }
    }
    
    // Producer task: Reads data from bno086
    /* Producer Thread */
    static void producer_task(void *arg1, void *arg2, void *arg3) {
        uint8_t read_data[16];
    
        while (1) {
            k_sleep(K_FOREVER);
    
            /* Read Rotation Vector Data */
            struct spi_buf tx_buf = { .buf = NULL, .len = 0 };
            struct spi_buf rx_buf = { .buf = read_data, .len = sizeof(read_data) };
            struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 };
            struct spi_buf_set rx = { .buffers = &rx_buf, .count = 1 };
    
            spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
    
            /* Copy data into active buffer */
            memcpy(active_buffer, read_data, sizeof(read_data));
            contador+=16;
            if (contador >= BUFFER_SIZE) {
            /* Swap buffers if full */
            uint8_t *temp = active_buffer;
            active_buffer = processing_buffer;
            processing_buffer = temp;
            contador=0;
            k_wakeup(&consumer_thread);
            }
        }
    }
    
    // BLE Send task: Sends data to the server via notifications
    /* Consumer Thread */
    static void consumer_task(void *arg1, void *arg2, void *arg3) {
        while (1) {
            k_sleep(K_FOREVER);
    
            /* Send data via BLE */
            send_data(processing_buffer);
            LOG_INF("Data sent.");
        }
    }
    
    
    int main(void)
    {
        // Initialize Bluetooth
        configure_gpios();
        configure_bno086();
    
        // Initialize BLE
        start_ble();
       
        
        // Create threads
        k_thread_create(&producer_thread, producer_stack, STACK_SIZE,
                        producer_task, NULL, NULL, NULL, 5, 0, K_NO_WAIT);
        k_thread_create(&consumer_thread, consumer_stack, STACK_SIZE,
                        consumer_task, NULL, NULL, NULL, 5, 0, K_NO_WAIT);
     return 0;
    }
    

    Could you help me please, I really don't know how to fix this...

  • When building the project I got an error saying that:

    in function 'configure_gpios': C:\ncs\Apps\test5\src\main.c:124: undifined reference to '__device_dts_ord_12'

  • Could you zip the folder where these files are in ? and then upload the zip file?

  • Hi!

    franciscopinho said:
    in function 'configure_gpios': C:\ncs\Apps\test5\src\main.c:124: undifined reference to '__device_dts_ord_12'

    Looking at zephyr\app\build\app\zephyr\include\generated\zephyr\devicetree_generated.h , we see that we have this at 12: 

     *   12  /buttons/drdy_input
    we also see in the error code it's releated to gpio_pin_configure() usage.
    Try this main.c
    // src/main.c
    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/drivers/spi.h>
    #include <zephyr/logging/log.h>
    #include <zephyr/sys/printk.h>
    #include <zephyr/bluetooth/bluetooth.h>
    #include <zephyr/bluetooth/gatt.h>
    #include <zephyr/sys/byteorder.h>
    
    LOG_MODULE_REGISTER(test5);
    // Define the device labels for SPI and GPIOs
    #define SPI1_NODE DT_NODELABEL(spi1)
    /* The devicetree node identifier for the "led0" alias. */
    #define LED0_NODE DT_ALIAS(gpioled)
    #define START_NODE DT_ALIAS(gpiostart)
    #define RESET_NODE DT_ALIAS(gpioreset)
    #define DRDY_NODE DT_ALIAS(gpiodrdy)
    #define GPIO_DRDY_PIN DT_PHA(DRDY_NODE, gpios, pin)
    
    
    
    //#define GPIO_DRDY_PIN 3
    
    static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
    static const struct gpio_dt_spec start = GPIO_DT_SPEC_GET(START_NODE, gpios);
    static const struct gpio_dt_spec reset = GPIO_DT_SPEC_GET(RESET_NODE, gpios);
    
    
    // SPI configuration
    #define SPI_OPERATION (SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8))
     const struct device *spi_dev;
     const struct device *gpio_dev;
     /* SPI configuration */
    static const struct spi_config spi_cfg = {
        .frequency = 3000000, /* 1 MHz */
        .operation = SPI_OPERATION,
        .slave = 0,
    };
    #define BNO086_ROTATION_VECTOR_REPORT_ID 0x05
    
    /* Callback structure */
    static struct gpio_callback drdy_cb;
    
    // Task thread stack sizes
    #define STACK_SIZE 1024
    #define BUFFER_SIZE 224      // Sending buffer size: 224 bytes
    #define SAMPLE_SIZE 16       // Fused data sample size: 16 bytes (4 floats)
    // Double buffers
    static uint8_t buffer1[BUFFER_SIZE];
    static uint8_t buffer2[BUFFER_SIZE];
    static uint8_t *active_buffer = buffer1;
    static uint8_t *processing_buffer = buffer2;
    int contador=0;
    // Shared variables
    static struct k_thread producer_thread;
    static struct k_thread consumer_thread;
    static K_THREAD_STACK_DEFINE(producer_stack, 2048);
    static K_THREAD_STACK_DEFINE(consumer_stack, 2048);
    // BLE connection and characteristic handles
    static struct bt_conn *current_conn;
    static uint16_t gatt_handle = 0x0012; // <-- Set correct handle for BLE characteristic
    
    
    // Forward declarations
    /* Callback function prototype */
    /* Function Prototypes */
    static void drdy_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins);
    static void producer_task(void *arg1, void *arg2, void *arg3);
    static void consumer_task(void *arg1, void *arg2, void *arg3);
    void configure_gpios(void);
    void configure_bno086(void);
    void start_ble(void);
    void send_data(uint8_t *data);
    
    
    
    static void drdy_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins) {
        k_wakeup(&producer_thread);
    }
    
    
    void configure_gpios(void) {
    
        
       int ret;
       spi_dev = DEVICE_DT_GET(SPI1_NODE);
       if (!device_is_ready(spi_dev)) {
        printk("Error: SPI1 device is not ready\n");
        return;
        }
    
    	ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
    	if (ret < 0) {
    		return;
    	}
    
          // Configure GPIOs
        ret = gpio_pin_configure_dt(&start, GPIO_OUTPUT_ACTIVE);
        if (ret < 0) {
            printk("Failed to configure LED GPIO\n");
            return;
        }
    
        ret = gpio_pin_configure_dt(&reset, GPIO_OUTPUT_ACTIVE);
        if (ret < 0) {
            printk("Failed to configure START GPIO\n");
            return;
        }
    
        gpio_dev = DEVICE_DT_GET(DT_PHANDLE(DT_ALIAS(gpiodrdy), gpios));
        if (gpio_dev == NULL) {
            printk("Failed to get SPI device binding\n");
            return;
        }
        ret = gpio_pin_configure(gpio_dev, GPIO_DRDY_PIN, GPIO_INPUT | GPIO_PULL_UP);
        if (ret != 0) {
            printk("Error: Failed to configure DRDY pin\n");
            return;
        }
        // Set up GPIO callback for DRDY pin
       gpio_init_callback(&drdy_cb, drdy_callback, BIT(GPIO_DRDY_PIN));
        gpio_add_callback(gpio_dev, &drdy_cb);
        gpio_pin_interrupt_configure(gpio_dev, GPIO_DRDY_PIN, GPIO_INT_EDGE_TO_INACTIVE);
        gpio_pin_set_dt(&start, 0);
        
    }
    
    void configure_bno086(void){
         uint8_t set_feature_cmd[] = {
            0xFD, // Set Feature Command
            BNO086_ROTATION_VECTOR_REPORT_ID, // Rotation Vector Report ID
            0x00, // Reserved
            0x90, 0x01, 0x00, 0x00, // Sample Rate = 400 Hz (in microseconds)
        };
    
        struct spi_buf tx_buf = { .buf = set_feature_cmd, .len = sizeof(set_feature_cmd) };
        struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 };
    
        spi_write(spi_dev, &spi_cfg, &tx);
        LOG_INF("BNO086 configured for 400 Hz Rotation Vector");
    }
    
    /* BLE Connection Callback */
    static void connected(struct bt_conn *conn, uint8_t err) {
        if (err) {
            LOG_ERR("Failed to connect (err: %d)", err);
            return;
        }
        LOG_INF("Connected");
        current_conn = conn;
    }
    
    /* BLE Disconnection Callback */
    static void disconnected(struct bt_conn *conn, uint8_t reason) {
        LOG_INF("Disconnected (reason: %d)", reason);
        current_conn = NULL;
    }
    
    /* Send Data via BLE */
    void send_data(uint8_t *data) {
        if (!current_conn) {
            LOG_WRN("Not connected");
            return;
        }
        int err = bt_gatt_write_without_response(current_conn, gatt_handle, data, BUFFER_SIZE, false);
        if (err) {
            LOG_ERR("Failed to send data (err: %d)", err);
        } else {
            LOG_INF("Data sent");
        }
    }
    
    
    
    /* BLE Initialization */
    void start_ble(void) {
        int err = bt_enable(NULL);
        if (err) {
            LOG_ERR("Bluetooth init failed (err %d)", err);
            return;
        }
        LOG_INF("Bluetooth initialized");
    
        static struct bt_conn_cb conn_callbacks = {
            .connected = connected,
            .disconnected = disconnected,
        };
        bt_conn_cb_register(&conn_callbacks);
    
        struct bt_le_adv_param adv_params = BT_LE_ADV_PARAM_INIT(
            BT_LE_ADV_OPT_CONNECTABLE,
            BT_GAP_ADV_FAST_INT_MIN_2,
            BT_GAP_ADV_FAST_INT_MAX_2,
            NULL
        );
    
        err = bt_le_adv_start(&adv_params, NULL, 0, NULL, 0);
        if (err) {
            LOG_ERR("Advertising failed (err: %d)", err);
        } else {
            LOG_INF("Advertising started");
        }
    }
    
    // Producer task: Reads data from bno086
    /* Producer Thread */
    static void producer_task(void *arg1, void *arg2, void *arg3) {
        uint8_t read_data[16];
    
        while (1) {
            k_sleep(K_FOREVER);
    
            /* Read Rotation Vector Data */
            struct spi_buf tx_buf = { .buf = NULL, .len = 0 };
            struct spi_buf rx_buf = { .buf = read_data, .len = sizeof(read_data) };
            struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 };
            struct spi_buf_set rx = { .buffers = &rx_buf, .count = 1 };
    
            spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
    
            /* Copy data into active buffer */
            memcpy(active_buffer, read_data, sizeof(read_data));
            contador+=16;
            if (contador >= BUFFER_SIZE) {
            /* Swap buffers if full */
            uint8_t *temp = active_buffer;
            active_buffer = processing_buffer;
            processing_buffer = temp;
            contador=0;
            k_wakeup(&consumer_thread);
            }
        }
    }
    
    // BLE Send task: Sends data to the server via notifications
    /* Consumer Thread */
    static void consumer_task(void *arg1, void *arg2, void *arg3) {
        while (1) {
            k_sleep(K_FOREVER);
    
            /* Send data via BLE */
            send_data(processing_buffer);
            LOG_INF("Data sent.");
        }
    }
    
    
    int main(void)
    {
        // Initialize Bluetooth
        configure_gpios();
        configure_bno086();
    
        // Initialize BLE
        start_ble();
       
        
        // Create threads
        k_thread_create(&producer_thread, producer_stack, STACK_SIZE,
                        producer_task, NULL, NULL, NULL, 5, 0, K_NO_WAIT);
        k_thread_create(&consumer_thread, consumer_stack, STACK_SIZE,
                        consumer_task, NULL, NULL, NULL, 5, 0, K_NO_WAIT);
     return 0;
    }
    
Related