From 953ea2a25a824079ec5fcecdc80768f766a18a3a Mon Sep 17 00:00:00 2001 From: mnerv Date: Sat, 4 Nov 2023 02:28:32 +0100 Subject: [PATCH] Bluetooth advertise --- CMakeLists.txt | 2 +- boards/arm/nrf52840_test/nrf52840_test.dts | 1 + prj.conf | 5 ++ src/main.c | 60 ++++++++++++++++++++++ src/main.cpp | 18 ------- 5 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 src/main.c delete mode 100644 src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 84e50d5..1371f8c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(hellobt) -target_sources(app PRIVATE src/main.cpp) +target_sources(app PRIVATE src/main.c) diff --git a/boards/arm/nrf52840_test/nrf52840_test.dts b/boards/arm/nrf52840_test/nrf52840_test.dts index e75d8b1..7f62bcc 100644 --- a/boards/arm/nrf52840_test/nrf52840_test.dts +++ b/boards/arm/nrf52840_test/nrf52840_test.dts @@ -4,6 +4,7 @@ /dts-v1/; #include +// https://wiki.makerdiary.com/nrf52840-mdk-usb-dongle/getting-started/ / { model = "nrf52840_test"; compatible = "nrf52840-test"; diff --git a/prj.conf b/prj.conf index 672d4dd..30fde55 100644 --- a/prj.conf +++ b/prj.conf @@ -11,3 +11,8 @@ CONFIG_BT=y CONFIG_BT_HCI=y CONFIG_BT_CTLR=y CONFIG_BT_LL_SW_SPLIT=y +CONFIG_BT_DEVICE_NAME="HelloBT_Peripheral" +CONFIG_BT_PERIPHERAL=y + +# Enable print for float +CONFIG_CBPRINTF_FP_SUPPORT=y \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..bf94689 --- /dev/null +++ b/src/main.c @@ -0,0 +1,60 @@ +#include "zephyr/kernel.h" +#include "zephyr/drivers/gpio.h" +#include "zephyr/random/rand32.h" + +#include "zephyr/bluetooth/bluetooth.h" +#include "zephyr/bluetooth/uuid.h" +#include "zephyr/bluetooth/addr.h" +#include "zephyr/bluetooth/hci.h" +#include + +#define DEVICE_NAME CONFIG_BT_DEVICE_NAME +#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) +#define COMPANY_ID_CODE 0x0059 + +// static struct bt_le_adv_param* adv_param = BT_LE_ADV_PARAM( +// BT_LE_ADV_OPT_NONE, +// 800, +// 801, +// NULL +// ); +static uint8_t url_data[] = { + 0x17, '/', '/', 'n', 'r', 'z', '.', 's', 'e' +}; + +// Advertisment data +static const struct bt_data ad[] = { + BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), + BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), +}; + +// Scan response packet +static const struct bt_data sd[] = { + BT_DATA(BT_DATA_URI, url_data, sizeof(url_data)), + // BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_128_ENCODE(0x00001523, 0x1212, 0xefde, 0x1523, 0x785feabcd123)), +}; + +int main() { + int err; + + err = bt_enable(NULL); + if (err) { + printk("Bluetooth init failed (err %d)\n", err); + return 1; + } + printk("Bluetooth initialised!\n"); + + err = bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd)); + if (err) { + printk("Advertising failed to start (err %d)\n", err); + return 1; + } + printk("Advertising successfully started!\n"); + + while (true) { + float value = (float)(sys_rand32_get() % 1000)/1000.0f; + printk("%u: Hello, World! %.3f\n", k_uptime_get_32(), value); + k_msleep(250); + } + return 0; +} diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index 04946c7..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "zephyr/kernel.h" -#include "zephyr/drivers/gpio.h" -#include "zephyr/random/rand32.h" - -#define LED_NODE DT_ALIAS(led0) -static gpio_dt_spec led = GPIO_DT_SPEC_GET(LED_NODE, gpios); - -auto main() -> int { - if (!gpio_is_ready_dt(&led)) return 1; - if (gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE) < 0) return 1; - - gpio_pin_set_dt(&led, 0); - while (true) { - printk("Hello, World! %d - %u\n", 100'000, sys_rand32_get()); - k_msleep(250); - } - return 0; -}