nrf52840 dongle connection

Subject: Request for Assistance: Coding BLE and SPI for Gesture Control using nRF52840

Dear nRF Support Team,

I hope this email finds you well. My name is sharon and I am currently working on a project that involves sending gesture data from a mobile app to an LED matrix. I am using the nRF52840 dongle as a BLE interface between my Android application and the LED matrix.

Below is an overview of my setup:

  • Mobile App: Custom-built (using Jetpack Compose)
  • BLE Interface: nRF52840 dongle
  • MCU: S32K118 (communicates with LED driver via SPI)
  • Communication Flow:
    1. Phone sends gestures via Bluetooth to nRF52840
    2. nRF52840 communicates with the S32K118 MCU via SPI
    3. S32K118 controls the LED matrix driver (SPI)

I am reaching out to request your assistance with the following:

  1. BLE Communication:

    • I need guidance on coding the nRF52840 to receive BLE data from the mobile app.
    • How can I efficiently send gesture data (e.g., touchpad coordinates or patterns) over BLE?
  2. SPI Communication:

    • What would be the recommended approach for transferring BLE data from the nRF52840 to the S32K118 via SPI?
    • Are there any reference examples for SPI communication on the nRF52840 that I can follow?

Any code snippets, libraries, or documentation you can provide would be greatly appreciated. Additionally, if there are specific SDK examples or configurations you recommend, I’d be glad to explore those.

Thank you for your time and support. Please let me know if you need any further information from my end.

Best regards,
 iam using vs studio for coding with nrf extension

Parents
  • Hi Sharon,

    BLE Communication:

    • I need guidance on coding the nRF52840 to receive BLE data from the mobile app.

    The nRF Connect SDK suppors most BLE use cases. To get started I suggest you first go through nRF Connect SDK Fundamentals, and then Bluetooth Low Energy Fundamentals.

    How can I efficiently send gesture data (e.g., touchpad coordinates or patterns) over BLE?

    There are several ways to do this. You can use existing service(s) or a custom service. A simple way to get started is to use the Nordic UART Service (NUS) which gives you a simple data channel. But this is up to you.

    What would be the recommended approach for transferring BLE data from the nRF52840 to the S32K118 via SPI?

    It is difficult to be specifc here as I don't have experience with S32K118. I am wondering why you need the additional MCU though, why not use the nRF52840 and connect the LED matrix driver directly from that? Do you need an additional MCU?

    Are there any reference examples for SPI communication on the nRF52840 that I can follow?

    The SDK has some SPI examples, and this is covered in nRF Connect SDK Intermediate.

    Br,

    Einar

  • can u guide me how send data recieved in dongle via ble to other peripherals via spi i only want to send data from dongle please help how to do and with stepss please

  • This is a big question that does not have a short answer, and the design of your product is up to you. Please start by going through the material I linked to in my previous post. That will show you all the basics you need to do what you have described.

  • i only want to send data from ble so for this use  i can use nus right?? no back and froth communication

  • #include <zephyr/types.h>
    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/drivers/spi.h>
    #include <zephyr/bluetooth/bluetooth.h>
    #include <zephyr/bluetooth/uuid.h>
    #include <zephyr/bluetooth/gatt.h>
    #include <bluetooth/services/nus.h>
    #include <dk_buttons_and_leds.h>
    #include <zephyr/logging/log.h>

    LOG_MODULE_REGISTER(BLE_SPI_Bridge, LOG_LEVEL_INF);

    #define RUN_STATUS_LED DK_LED1
    #define RUN_LED_BLINK_INTERVAL 1000

    static struct bt_conn *current_conn;
    static K_SEM_DEFINE(ble_init_ok, 0, 1);

    /* SPI device configuration */
    #define SPI_NODE DT_NODELABEL(bme280)  // Adjust according to your SPI hardware node
    static const struct device *spi_dev = DEVICE_DT_GET(SPI_NODE);

    static struct spi_config spi_cfg = {
        .frequency = 1000000,  // 1 MHz, adjust as needed
        .operation = SPI_OP_MODE_MASTER | SPI_WORD_SET(8) | SPI_TRANSFER_MSB,
        .cs = NULL,
    };

    /* Initialize SPI communication */
    static int spi_init(void) {
        if (!device_is_ready(spi_dev)) {
            LOG_ERR("SPI device not ready");
            return -ENODEV;
        }
        LOG_INF("SPI initialized");
        return 0;
    }

    /* BLE connection callback */
    static void connected(struct bt_conn *conn, uint8_t err) {
        char addr[BT_ADDR_LE_STR_LEN];

        if (err) {
            LOG_ERR("Connection failed (err %u)", err);
            return;
        }

        bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
        LOG_INF("Connected %s", addr);

        current_conn = bt_conn_ref(conn);
        dk_set_led_on(RUN_STATUS_LED);
    }

    static void disconnected(struct bt_conn *conn, uint8_t reason) {
        char addr[BT_ADDR_LE_STR_LEN];

        bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
        LOG_INF("Disconnected: %s (reason %u)", addr, reason);

        if (current_conn) {
            bt_conn_unref(current_conn);
            current_conn = NULL;
        }
        dk_set_led_off(RUN_STATUS_LED);
    }

    /* BLE receive callback - Send received data over SPI */
    static void bt_receive_cb(struct bt_conn *conn, const uint8_t *data, uint16_t len) {
        struct spi_buf spi_buf = {
            .buf = (void *)data,
            .len = len,
        };
        struct spi_buf_set tx = {
            .buffers = &spi_buf,
            .count = 1,
        };

        int err = spi_write(spi_dev, &spi_cfg, &tx);
        if (err) {
            LOG_ERR("Failed to send data over SPI (err %d)", err);
        } else {
            LOG_INF("Data sent over SPI: %.*s", len, data);
        }
    }

    /* NUS callback structure */
    static struct bt_nus_cb nus_cb = {
        .received = bt_receive_cb,
    };

    BT_CONN_CB_DEFINE(conn_callbacks) = {
        .connected = connected,
        .disconnected = disconnected,
    };

    int main(void) {
        int err;

        dk_leds_init();
        err = spi_init();
        if (err) {
            LOG_ERR("SPI initialization failed");
            return;
        }

        err = bt_enable(NULL);
        if (err) {
            LOG_ERR("Bluetooth init failed (err %d)", err);
            return;
        }
        LOG_INF("Bluetooth initialized");

        k_sem_give(&ble_init_ok);

        err = bt_nus_init(&nus_cb);
        if (err) {
            LOG_ERR("Failed to initialize NUS (err %d)", err);
            return;
        }

        err = bt_le_adv_start(BT_LE_ADV_CONN, NULL, 0, NULL, 0);
        if (err) {
            LOG_ERR("Advertising failed to start (err %d)", err);
            return;
        }

        LOG_INF("Advertising started");

        for (;;) {
            dk_set_led(RUN_STATUS_LED, 1);
            k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
            dk_set_led(RUN_STATUS_LED, 0);
            k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
        }
        return 0;
    }   this code showing error 
    can uhelp
  • Hi,

    Some questions:

    • How are you building this (for which board and which SDK version, and what is the configuration)?
    • Which errors do you get? Please share the full log

    PS: To share code in a readable way you can use Insert -> Code.

Reply Children
  • /*
     * Copyright (c) 2018 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    /** @file
     *  @brief Nordic UART Bridge Service (NUS) sample
     */
    #include "uart_async_adapter.h"
    
    #include <zephyr/types.h>
    #include <zephyr/kernel.h>
    #include <zephyr/drivers/uart.h>
    #include <zephyr/usb/usb_device.h>
    
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    #include <soc.h>
    
    #include <zephyr/bluetooth/bluetooth.h>
    #include <zephyr/bluetooth/uuid.h>
    #include <zephyr/bluetooth/gatt.h>
    #include <zephyr/bluetooth/hci.h>
    
    #include <bluetooth/services/nus.h>
    
    #include <dk_buttons_and_leds.h>
    
    #include <zephyr/settings/settings.h>
    
    #include <stdio.h>
    
    #include <zephyr/logging/log.h>
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/drivers/spi.h>
    
    
    
    #define STACKSIZE CONFIG_BT_NUS_THREAD_STACK_SIZE
    #define PRIORITY 7
    
    #define DEVICE_NAME CONFIG_BT_DEVICE_NAME
    #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
    
    #define RUN_STATUS_LED DK_LED1
    #define RUN_LED_BLINK_INTERVAL 1000
    
    #define CON_STATUS_LED DK_LED2
    
    #define KEY_PASSKEY_ACCEPT DK_BTN1_MSK
    #define KEY_PASSKEY_REJECT DK_BTN2_MSK
    
    #define UART_WAIT_FOR_BUF_DELAY K_MSEC(50)
    #define UART_WAIT_FOR_RX CONFIG_BT_NUS_UART_RX_WAIT_TIME
    
    static K_SEM_DEFINE(ble_init_ok, 0, 1);
    
    static struct bt_conn *current_conn;
    static struct bt_conn *auth_conn;
    
    //static const struct device *uart = DEVICE_DT_GET(DT_CHOSEN(nordic_nus_uart));
    static struct k_work_delayable uart_work;
    
    
    
    LOG_MODULE_REGISTER(main);
    
    // SPI Configuration
    #define SPI_OP SPI_WORD_SET(8) | SPI_TRANSFER_MSB
    
    static const struct spi_dt_spec spi_dev = SPI_DT_SPEC_GET(DT_NODELABEL(bme280), SPI_OP, 0);
    
    // Example Data for SPI Write
    uint8_t led_control_data[3] = { 0x05, 0x0A, 0x01 }; // Row=5, Column=10, Status=ON
    
    // Function to send data over SPI
    void bme_write_reg(const uint8_t *data, size_t len) {
        struct spi_buf tx_buf = { .buf = (void *)data, .len = len };
        struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 };
    
        if (spi_write(spi_dev.bus, &spi_dev.config, &tx) != 0) {
            LOG_ERR("SPI write failed");
        } else {
            LOG_INF("SPI data sent: %d %d %d", data[0], data[1], data[2]);
        }
    }
    
    // BLE Receive Callback
    static void ble_receive_cb(struct bt_conn *conn, const uint8_t *data, uint16_t len) {
        if (len == 3) {
            memcpy(led_control_data, data, len);  // Store the received data
            LOG_INF("Received BLE Data: Row=%d, Column=%d, Status=%d", 
                    data[0], data[1], data[2]);
        } else {
            LOG_WRN("Invalid data length received over BLE");
        }
    }
    
    // BLE NUS Callbacks
    static struct bt_nus_cb nus_cb = {
        .received = ble_receive_cb,
    };
    
    // Bluetooth Initialization
    void bt_ready(int err) {
        if (err) {
            LOG_ERR("Bluetooth init failed (err %d)", err);
            return;
        }
    
        LOG_INF("Bluetooth initialized");
    
        // Start advertising
        err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, NULL, 0, NULL, 0);
        if (err) {
            LOG_ERR("Advertising failed to start (err %d)", err);
            return;
        }
    
        LOG_INF("Advertising successfully started");
    }
    
    // Main function
    void main(void) {
        int err;
    
        LOG_INF("Initializing Bluetooth and SPI...");
    
        // Initialize Bluetooth
        err = bt_enable(bt_ready);
        if (err) {
            LOG_ERR("Bluetooth enable failed (err %d)", err);
            return;
        }
    
        // Initialize SPI device
        if (!device_is_ready(spi_dev.bus)) {
            LOG_ERR("SPI device not ready");
            return;
        }
    
        LOG_INF("SPI device initialized");
    
        // Register NUS service
        bt_nus_init(&nus_cb);
    
        // Main loop: Continuously send SPI data
        while (1) {
            bme_write_reg(led_control_data, sizeof(led_control_data));
    
            // Sleep to avoid overwhelming the SPI bus
            k_msleep(1000);  // Send data every 1 second
        }
    }
    iam using nrf52840 dongle  and sdk version iam using is nrf connect extension in vs studio 2.7.0 data is not sending via spi please help

  • I see.

    sharon123321 said:
    data is not sending via spi

    Do you get any errors in the log, or something else? In what way is it not working? If that does not provide anythign specific, what do you see with a logic analyzer on the SPI lines, and how does it differ from what you would expect? Please elaborate.

  • i have replaced the 

    Sending data between a UART and a Bluetooth LE connection example code instead of uart i included spi connection but its not sending nothing when i used picoscope

Related