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

Reply Children
  • 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.

Related