Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/bluetooth-fw/nimble/advert.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ void bt_driver_advert_set_advertising_data(const BLEAdData *ad_data) {

bool bt_driver_advert_advertising_enable(uint32_t min_interval_ms, uint32_t max_interval_ms,
bool enable_scan_resp) {
int rc;
uint8_t own_addr_type;
struct ble_gap_adv_params advp = {
.conn_mode = enable_scan_resp ? BLE_GAP_CONN_MODE_UND : BLE_GAP_DISC_MODE_NON,
.disc_mode = BLE_GAP_DISC_MODE_GEN,
.itvl_min = BLE_GAP_CONN_ITVL_MS(min_interval_ms),
.itvl_max = BLE_GAP_CONN_ITVL_MS(max_interval_ms),
};

int rc = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &advp, NULL, NULL);
rc = ble_hs_id_infer_auto(0, &own_addr_type);
PBL_ASSERTN(rc == 0);

rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &advp, NULL, NULL);
return rc == 0;
}

Expand Down
8 changes: 7 additions & 1 deletion src/bluetooth-fw/nimble/id.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ void bt_driver_id_set_local_device_name(const char device_name[BT_DEVICE_NAME_BU
}

void bt_driver_id_copy_local_identity_address(BTDeviceAddress *addr_out) {
int rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, (uint8_t *)&addr_out->octets, NULL);
int rc;
uint8_t own_addr_type;

rc = ble_hs_id_infer_auto(0, &own_addr_type);
PBL_ASSERTN(rc == 0);

rc = ble_hs_id_copy_addr(own_addr_type, (uint8_t *)&addr_out->octets, NULL);
PBL_ASSERTN(rc == 0);
}

Expand Down
29 changes: 25 additions & 4 deletions src/bluetooth-fw/nimble/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
#include "system/logging.h"
#include "system/passert.h"

static const uint32_t s_bt_stack_start_stop_timeout_ms = 500;
static const uint32_t s_bt_stack_start_stop_timeout_ms = 2000;

extern void pebble_pairing_service_init(void);

void ble_store_ram_init(void);

#if NIMBLE_CFG_CONTROLLER
static TaskHandle_t s_ll_task_handle;
#endif
static TaskHandle_t s_host_task_handle;
static SemaphoreHandle_t s_host_started;
static SemaphoreHandle_t s_host_stopped;
Expand Down Expand Up @@ -79,19 +82,34 @@ void bt_driver_init(void) {
nimble_port_init();
ble_store_ram_init();

TaskParameters_t task_params = {
TaskParameters_t host_task_params = {
.pvTaskCode = prv_host_task_main,
.pcName = "NimbleHost",
.usStackDepth = 4000 / sizeof(StackType_t), // TODO: probably reduce this
.uxPriority = (tskIDLE_PRIORITY + 3) | portPRIVILEGE_BIT,
.uxPriority = (configMAX_PRIORITIES - 2) | portPRIVILEGE_BIT,
.puxStackBuffer = NULL,
};

pebble_task_create(PebbleTask_BTCallback, &task_params, &s_host_task_handle);
pebble_task_create(PebbleTask_BTHost, &host_task_params, &s_host_task_handle);
PBL_ASSERTN(s_host_task_handle);

#if NIMBLE_CFG_CONTROLLER
TaskParameters_t ll_task_params = {
.pvTaskCode = nimble_port_ll_task_func,
.pcName = "NimbleLL",
.usStackDepth = (configMINIMAL_STACK_SIZE + 400) / sizeof(StackType_t),
.uxPriority = (configMAX_PRIORITIES - 1) | portPRIVILEGE_BIT,
.puxStackBuffer = NULL,
};

pebble_task_create(PebbleTask_BTController, &ll_task_params, &s_ll_task_handle);
PBL_ASSERTN(s_ll_task_handle);
#endif
}

bool bt_driver_start(BTDriverConfig *config) {
int rc;

PBL_LOG_D(LOG_DOMAIN_BT, LOG_LEVEL_INFO, "bt_driver_start");

s_dis_info = config->dis_info;
Expand All @@ -114,6 +132,9 @@ bool bt_driver_start(BTDriverConfig *config) {
return false;
}

rc = ble_hs_util_ensure_addr(0);
PBL_ASSERTN(rc == 0);

return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/bluetooth-fw/nimble/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def build(bld):
defines=['FILE_LOG_COLOR=LOG_COLOR_BLUE'],
use=[
'nimble',
'freertos',
'fw_includes',
'root_includes',
],
Expand Down
2 changes: 1 addition & 1 deletion src/bluetooth-fw/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _recurse(ctx):
elif ctx.env.bt_controller == 'qemu':
ctx.recurse('qemu')
elif ctx.env.bt_controller == 'nrf52':
ctx.recurse('stub')
ctx.recurse('nimble')
else:
ctx.fatal('Invalid bt controller {}'.format(ctx.env.bt_controller))

Expand Down
2 changes: 1 addition & 1 deletion src/fw/comm/bt_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// *and* by Bluetopia. It gets handed to Bluetopia using bt_lock_get() in
// BTPSKRNL.c when Bluetopia is initialized. The firmware uses this lock to
// protect Bluetooth-related state that is read and written from the Bluetooth
// callback task (PebbleTask_BTCallback) and other tasks. If we created our
// callback task (PebbleTask_BTHost) and other tasks. If we created our
// own mutex for this purpose, we would encounter dead-lock situations.
// For example:
// Task1: Bluetopia code -> grabs BT stack lock -> Pebble callback -> grabs pebble mutex
Expand Down
24 changes: 12 additions & 12 deletions src/fw/kernel/pebble_tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ char pebble_task_get_char(PebbleTask task) {
return 'w';
case PebbleTask_App:
return 'a';
case PebbleTask_BTCallback:
case PebbleTask_BTHost:
return 'b';
case PebbleTask_BTRX:
case PebbleTask_BTController:
return 'c';
case PebbleTask_BTTimer:
case PebbleTask_BTHCI:
return 'd';
case PebbleTask_NewTimers:
return 't';
Expand Down Expand Up @@ -122,11 +122,11 @@ void analytics_external_collect_stack_free(void) {
prv_task_get_stack_free(PebbleTask_KernelBackground), AnalyticsClient_System);

analytics_set(ANALYTICS_DEVICE_METRIC_STACK_FREE_BLUETOPIA_BIG,
prv_task_get_stack_free(PebbleTask_BTCallback), AnalyticsClient_System);
prv_task_get_stack_free(PebbleTask_BTHost), AnalyticsClient_System);
analytics_set(ANALYTICS_DEVICE_METRIC_STACK_FREE_BLUETOPIA_MEDIUM,
prv_task_get_stack_free(PebbleTask_BTRX), AnalyticsClient_System);
prv_task_get_stack_free(PebbleTask_BTController), AnalyticsClient_System);
analytics_set(ANALYTICS_DEVICE_METRIC_STACK_FREE_BLUETOPIA_SMALL,
prv_task_get_stack_free(PebbleTask_BTTimer), AnalyticsClient_System);
prv_task_get_stack_free(PebbleTask_BTHCI), AnalyticsClient_System);

analytics_set(ANALYTICS_DEVICE_METRIC_STACK_FREE_NEWTIMERS,
prv_task_get_stack_free(PebbleTask_NewTimers), AnalyticsClient_System);
Expand Down Expand Up @@ -172,9 +172,9 @@ void pebble_task_create(PebbleTask pebble_task, TaskParameters_t *task_params,
break;
case PebbleTask_KernelMain:
case PebbleTask_KernelBackground:
case PebbleTask_BTCallback:
case PebbleTask_BTRX:
case PebbleTask_BTTimer:
case PebbleTask_BTHost:
case PebbleTask_BTController:
case PebbleTask_BTHCI:
case PebbleTask_NewTimers:
case PebbleTask_PULSE:
mpu_init_region_from_region(&app_region, memory_layout_get_app_region(),
Expand All @@ -200,9 +200,9 @@ void pebble_task_create(PebbleTask pebble_task, TaskParameters_t *task_params,
case PebbleTask_KernelBackground:
stack_guard_region = memory_layout_get_kernel_bg_stack_guard_region();
break;
case PebbleTask_BTCallback:
case PebbleTask_BTRX:
case PebbleTask_BTTimer:
case PebbleTask_BTHost:
case PebbleTask_BTController:
case PebbleTask_BTHCI:
case PebbleTask_NewTimers:
case PebbleTask_PULSE:
break;
Expand Down
6 changes: 3 additions & 3 deletions src/fw/kernel/pebble_tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ typedef enum PebbleTask {
PebbleTask_Worker,
PebbleTask_App,

PebbleTask_BTCallback, // Task Bluetooth callbacks are handled on
PebbleTask_BTRX, // Task handling inbound data from BT controller
PebbleTask_BTTimer, // Timer task - only used by cc2564x BT controller today
PebbleTask_BTHost, // Bluetooth Host
PebbleTask_BTController, // Bluetooth Controller
PebbleTask_BTHCI, // Bluetooth HCI

PebbleTask_NewTimers,

Expand Down
2 changes: 2 additions & 0 deletions src/libc/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ void exit(int status) __attribute__((noreturn));
// Not implemented, but included in the header to build the default platform.c of libs.
void free(void *ptr);
void *malloc(size_t bytes);

long jrand48(unsigned short int s[3]);
52 changes: 52 additions & 0 deletions src/libc/rand48.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2025 Core Devices LLC
* Copyright 2025 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdlib.h>
#include <stdint.h>
#include <string.h>

static unsigned short int g_seed48[7] =
{
0,
0,
0,
0xe66d,
0xdeec,
0x5,
0xb
};

static uint64_t rand48_step(unsigned short int *xi,
unsigned short int *lc)
{
uint64_t a;
uint64_t x;

x = xi[0] | ((xi[1] + 0ul) << 16) | ((xi[2] + 0ull) << 32);
a = lc[0] | ((lc[1] + 0ul) << 16) | ((lc[2] + 0ull) << 32);
x = a * x + lc[3];

xi[0] = x;
xi[1] = x >> 16;
xi[2] = x >> 32;
return x & 0xffffffffffffull;
}

long jrand48(unsigned short int s[3])
{
return (long)(rand48_step(s, g_seed48 + 3) >> 16);
}
1 change: 1 addition & 0 deletions third_party/hal_nordic/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def build(bld):
micro_includes = [
'.',
'nrfx',
'nrfx/hal',
'nrfx/mdk',
'nrfx/drivers/include',
'nrfx/templates',
Expand Down
Loading