embassy-rs / nrf-softdevice

Apache License 2.0
269 stars 80 forks source link

Configure nrf UART with ble #267

Closed Gibbz closed 4 months ago

Gibbz commented 4 months ago

Im trying to setup nrf UART over BLE. Ive previously done it on the esp32 in cpp. But im struggling with the uuid's on the softdevice.

Link here: https://developer.nordicsemi.com/nRF51_SDK/nRF51_SDK_v8.x.x/doc/8.0.0/s110/html/a00072.html

The UART service id's, which also needs to be given to the advertising data:

service id: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
rx id: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
tx id: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E

Is there an easy way to convert the string uuid's to [u8, 16] arrays for the advertising builder?

    static ADV_DATA: LegacyAdvertisementPayload = LegacyAdvertisementBuilder::new()
        .flags(&[Flag::GeneralDiscovery, Flag::LE_Only])
        .services_16(
            ServiceList::Complete, // or complete
            &[
                ServiceUuid16::BATTERY, 
                ServiceUuid16::USER_DATA, 
                ServiceUuid16::DEVICE_INFORMATION, 
            ]) // TODO: UART service id
        .services_128(ServiceList::Complete, 
            &[
                UART_UUID // <- this need to be [u8, 16]
            ])
        .full_name(DEVICE_NAME)
        //.raw(AdvertisementDataType::APPEARANCE, &[0xC1, 0x03]) // TODO: research
        .build();

Edit, also tried:

const UART: [u8; 16] = [0x6E, 0x40, 0x00, 0x01, 0x0B, 0x5A, 0xF3, 0x93, 0x9E, 0x0A, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E];

    // TODO: convert back to static?
    static ADV_DATA: LegacyAdvertisementPayload = LegacyAdvertisementBuilder::new()
        .flags(&[Flag::GeneralDiscovery, Flag::LE_Only])
        .services_16(
            ServiceList::Complete, // or complete
            &[
                ServiceUuid16::BATTERY, 
                ServiceUuid16::USER_DATA, 
                ServiceUuid16::DEVICE_INFORMATION, 
            ]) // TODO: UART service id
        .services_128(ServiceList::Complete, 
            &[
                UART
            ])
        .full_name(DEVICE_NAME)
        //.raw(AdvertisementDataType::APPEARANCE, &[0xC1, 0x03]) // TODO: research
        .build();

but i get the error:

error[E0080]: could not evaluate static initializer
   --> /home/bronson/.cargo/git/checkouts/nrf-softdevice-03ef4aef10e777e4/d5f023b/nrf-softdevice/src/ble/advertisement_builder.rs:261:9
    |
261 |         core::assert!(self.ptr <= K, "advertisement exceeded buffer length");
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'advertisement exceeded buffer length', /home/bronson/.cargo/git/checkouts/nrf-softdevice-03ef4aef10e777e4/d5f023b/nrf-softdevice/src/ble/advertisement_builder.rs:261:9
Gibbz commented 4 months ago

Ive got the uuid's kind of working... Ok enough to get a connection anyway... However my next issue is getting the notifications enabled.

For the nrf toolbox terminal to work, it requires that the notifications are enabled. In the log its reports that write to characteristic - missing property error: [UUID for rx]

How do i set the value, and notify back of the change? In my c++ version I had the following, which i need to replicate.

  if (pCharacteristic == uart_rx_characteristic) {
    // notify data recieved
    uart_tx_characteristic->notify(data, size);
}

Link to my source: https://github.com/bit-shift-io/bike-aid/blob/main/bike-aid-nrf-rs/src/ble/service_uart.rs https://github.com/bit-shift-io/bike-aid/blob/main/bike-aid-nrf-rs/src/ble/server.rs https://github.com/bit-shift-io/bike-aid/blob/main/bike-aid-nrf-rs/src/tasks/bluetooth.rs

Gibbz commented 4 months ago

All good! I had my rx and tx mixed up!!