mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-14 01:27:41 +00:00
zephyr: west supporting out-of-tree build
This commit is contained in:
parent
e406aa8dcd
commit
277e512c6e
1
port/zephyr/.gitignore
vendored
Normal file
1
port/zephyr/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
build
|
213
port/zephyr/CMakeLists.txt
Normal file
213
port/zephyr/CMakeLists.txt
Normal file
@ -0,0 +1,213 @@
|
||||
cmake_minimum_required (VERSION 3.5)
|
||||
|
||||
SET(CMAKE_OSX_SYSROOT /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk)
|
||||
SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
#set(CMAKE_BUILD_TYPE RelWithDebInfo)
|
||||
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
|
||||
project(BTstack-Zephyr)
|
||||
|
||||
set(BTSTACK_ROOT ${CMAKE_SOURCE_DIR}/../../)
|
||||
if( DEFINED ENV{BTSTACK_ROOT} )
|
||||
set(BTSTACK_ROOT $ENV{BTSTACK_ROOT})
|
||||
endif()
|
||||
|
||||
# pkgconfig
|
||||
find_package(PkgConfig REQUIRED)
|
||||
|
||||
# to generate .h from .gatt files
|
||||
find_package (Python REQUIRED COMPONENTS Interpreter)
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
# local dir for btstack_config.h after build dir to avoid using .h from Makefile
|
||||
set(INCLUDES_PATH "" )
|
||||
list(APPEND INCLUDES_PATH 3rd-party/micro-ecc )
|
||||
list(APPEND INCLUDES_PATH 3rd-party/bluedroid/decoder/include)
|
||||
list(APPEND INCLUDES_PATH 3rd-party/bluedroid/encoder/include)
|
||||
list(APPEND INCLUDES_PATH 3rd-party/lc3-google/include)
|
||||
list(APPEND INCLUDES_PATH 3rd-party/md5)
|
||||
list(APPEND INCLUDES_PATH 3rd-party/hxcmod-player)
|
||||
list(APPEND INCLUDES_PATH 3rd-party/hxcmod-player/mod)
|
||||
list(APPEND INCLUDES_PATH 3rd-party/lwip/core/src/include)
|
||||
list(APPEND INCLUDES_PATH 3rd-party/lwip/dhcp-server)
|
||||
list(APPEND INCLUDES_PATH 3rd-party/rijndael)
|
||||
list(APPEND INCLUDES_PATH 3rd-party/yxml)
|
||||
list(APPEND INCLUDES_PATH 3rd-party/tinydir)
|
||||
list(APPEND INCLUDES_PATH src)
|
||||
#list(APPEND INCLUDES_PATH chipset/realtek)
|
||||
#list(APPEND INCLUDES_PATH chipset/zephyr)
|
||||
#list(APPEND INCLUDES_PATH platform/posix)
|
||||
list(APPEND INCLUDES_PATH platform/embedded)
|
||||
list(APPEND INCLUDES_PATH platform/lwip)
|
||||
list(APPEND INCLUDES_PATH platform/lwip/port)
|
||||
list(TRANSFORM INCLUDES_PATH PREPEND ${BTSTACK_ROOT}/)
|
||||
list(APPEND INCLUDES_PATH . )
|
||||
include_directories( ${INCLUDES_PATH} )
|
||||
|
||||
file(GLOB SOURCES_SRC "${BTSTACK_ROOT}/src/*.c" ) #"${BTSTACK_ROOT}/example/sco_demo_util.c")
|
||||
|
||||
#message("${SOURCES_SRC}")
|
||||
|
||||
file(GLOB SOURCES_BLE "${BTSTACK_ROOT}/src/ble/*.c")
|
||||
file(GLOB SOURCES_GATT "${BTSTACK_ROOT}/src/ble/gatt-service/*.c")
|
||||
file(GLOB SOURCES_CLASSIC "${BTSTACK_ROOT}/src/classic/*.c")
|
||||
file(GLOB SOURCES_MESH "${BTSTACK_ROOT}/src/mesh/*.c" "${BTSTACK_ROOT}/src/mesh/gatt-service/*.c")
|
||||
file(GLOB SOURCES_BLUEDROID "${BTSTACK_ROOT}/3rd-party/bluedroid/encoder/srce/*.c" "${BTSTACK_ROOT}/3rd-party/bluedroid/decoder/srce/*.c")
|
||||
file(GLOB SOURCES_MD5 "${BTSTACK_ROOT}/3rd-party/md5/md5.c")
|
||||
file(GLOB SOURCES_UECC "${BTSTACK_ROOT}/3rd-party/micro-ecc/uECC.c")
|
||||
file(GLOB SOURCES_YXML "${BTSTACK_ROOT}/3rd-party/yxml/yxml.c")
|
||||
file(GLOB SOURCES_HXCMOD "${BTSTACK_ROOT}/3rd-party/hxcmod-player/*.c" "../../3rd-party/hxcmod-player/mods/*.c")
|
||||
file(GLOB SOURCES_RIJNDAEL "${BTSTACK_ROOT}/3rd-party/rijndael/rijndael.c")
|
||||
file(GLOB SOURCES_EMBEDDED "${BTSTACK_ROOT}/platform/embedded/*.c")
|
||||
#file(GLOB SOURCES_ZEPHYR "${CMAKE_SOURCE_DIR}/src/main.c")
|
||||
#file(GLOB SOURCES_ZEPHYR "${BTSTACK_ROOT}/chipset/zephyr/*.c")
|
||||
#file(GLOB SOURCES_REALTEK "${BTSTACK_ROOT}/chipset/realtek/*.c")
|
||||
#file(GLOB SOURCES_LC3_GOOGLE "${BTSTACK_ROOT}/3rd-party/lc3-google/src/*.c")
|
||||
|
||||
set(LWIP_CORE_SRC
|
||||
3rd-party/lwip/core/src/core/def.c
|
||||
3rd-party/lwip/core/src/core/inet_chksum.c
|
||||
3rd-party/lwip/core/src/core/init.c
|
||||
3rd-party/lwip/core/src/core/ip.c
|
||||
3rd-party/lwip/core/src/core/mem.c
|
||||
3rd-party/lwip/core/src/core/memp.c
|
||||
3rd-party/lwip/core/src/core/netif.c
|
||||
3rd-party/lwip/core/src/core/pbuf.c
|
||||
3rd-party/lwip/core/src/core/tcp.c
|
||||
3rd-party/lwip/core/src/core/tcp_in.c
|
||||
3rd-party/lwip/core/src/core/tcp_out.c
|
||||
3rd-party/lwip/core/src/core/timeouts.c
|
||||
3rd-party/lwip/core/src/core/udp.c
|
||||
)
|
||||
set (LWIP_IPV4_SRC
|
||||
3rd-party/lwip/core/src/core/ipv4/acd.c
|
||||
3rd-party/lwip/core/src/core/ipv4/dhcp.c
|
||||
3rd-party/lwip/core/src/core/ipv4/etharp.c
|
||||
3rd-party/lwip/core/src/core/ipv4/icmp.c
|
||||
3rd-party/lwip/core/src/core/ipv4/ip4.c
|
||||
3rd-party/lwip/core/src/core/ipv4/ip4_addr.c
|
||||
3rd-party/lwip/core/src/core/ipv4/ip4_frag.c
|
||||
)
|
||||
set (LWIP_NETIF_SRC
|
||||
3rd-party/lwip/core/src/netif/ethernet.c
|
||||
)
|
||||
set (LWIP_HTTPD
|
||||
3rd-party/lwip/core/src/apps/http/altcp_proxyconnect.c
|
||||
3rd-party/lwip/core/src/apps/http/fs.c
|
||||
3rd-party/lwip/core/src/apps/http/httpd.c
|
||||
)
|
||||
set (LWIP_DHCPD
|
||||
3rd-party/lwip/dhcp-server/dhserver.c
|
||||
)
|
||||
set (LWIP_PORT
|
||||
platform/lwip/port/sys_arch.c
|
||||
platform/lwip/bnep_lwip.c
|
||||
)
|
||||
|
||||
set (SOURCES_LWIP ${LWIP_CORE_SRC} ${LWIP_IPV4_SRC} ${LWIP_NETIF_SRC} ${LWIP_HTTPD} ${LWIP_DHCPD} ${LWIP_PORT})
|
||||
list(TRANSFORM SOURCES_LWIP PREPEND ${BTSTACK_ROOT}/)
|
||||
|
||||
file(GLOB SOURCES_SRC_OFF "${BTSTACK_ROOT}/src/hci_transport_*.c")
|
||||
list(REMOVE_ITEM SOURCES_SRC ${SOURCES_SRC_OFF})
|
||||
|
||||
file(GLOB SOURCES_BLE_OFF "${BTSTACK_ROOT}/src/ble/att_db_util.c")
|
||||
list(REMOVE_ITEM SOURCES_BLE ${SOURCES_BLE_OFF})
|
||||
|
||||
set(SOURCES
|
||||
${SOURCES_MD5}
|
||||
${SOURCES_YXML}
|
||||
${SOURCES_BLUEDROID}
|
||||
${SOURCES_EMBEDDED}
|
||||
${SOURCES_RIJNDAEL}
|
||||
${SOURCES_LC3_GOOGLE}
|
||||
${SOURCES_SRC}
|
||||
${SOURCES_BLE}
|
||||
${SOURCES_GATT}
|
||||
# ${SOURCES_MESH}
|
||||
# ${SOURCES_CLASSIC}
|
||||
${SOURCES_UECC}
|
||||
# ${SOURCES_HXCMOD}
|
||||
# ${SOURCES_REALTEK}
|
||||
# ${SOURCES_ZEPHYR}
|
||||
)
|
||||
list(SORT SOURCES)
|
||||
|
||||
# create static lib
|
||||
add_library(btstack STATIC ${SOURCES})
|
||||
|
||||
# extra compiler warnings
|
||||
target_compile_options(btstack PRIVATE
|
||||
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
|
||||
-Wunused-variable -Wswitch-default -Werror -Wall>
|
||||
$<$<CXX_COMPILER_ID:GNU>:
|
||||
-Wunused-but-set-variable -Wunused-variable -Wswitch-default -Werror -Wall>
|
||||
$<$<CXX_COMPILER_ID:MSVC>:
|
||||
/W4>)
|
||||
|
||||
# get zephyr's idea of build flags
|
||||
zephyr_get_include_directories_for_lang( C includes)
|
||||
zephyr_get_system_include_directories_for_lang(C system_includes)
|
||||
zephyr_get_compile_definitions_for_lang( C definitions)
|
||||
zephyr_get_compile_options_for_lang( C options)
|
||||
|
||||
set(external_project_cflags
|
||||
${includes}$<SEMICOLON>${definitions}$<SEMICOLON>${options}$<SEMICOLON>${system_includes}
|
||||
)
|
||||
|
||||
# prevent imacros argument from deduplication
|
||||
string( REGEX REPLACE "\\$<TARGET_PROPERTY:compiler,imacros>\\$<SEMICOLON>([^\\$]+)"
|
||||
"\$<1:SHELL:-imacros \\1>"
|
||||
_all_options ${external_project_cflags} )
|
||||
|
||||
target_compile_options( btstack PUBLIC ${_all_options})
|
||||
|
||||
set(EXAMPLES "gatt_counter" )
|
||||
if(DEFINED ENV{EXAMPLE})
|
||||
set( EXAMPLES $ENV{EXAMPLE} )
|
||||
endif()
|
||||
|
||||
list(SORT EXAMPLES)
|
||||
file(GLOB EXAMPLES_GATT "${BTSTACK_ROOT}/example/*.gatt")
|
||||
|
||||
# remove some
|
||||
file(GLOB EXAMPLES_OFF "../../example/sco_demo_util.c" "../../example/ant_test.c" "../../example/avrcp_browsing_client.c")
|
||||
list(REMOVE_ITEM EXAMPLES_C ${EXAMPLES_OFF})
|
||||
|
||||
# on Mac 10.14, adding lwip to libstack results in a yet not understood link error
|
||||
# workaround: add lwip sources only to lwip_examples
|
||||
set (LWIP_EXAMPLES pan_lwip_http_server)
|
||||
|
||||
# create targets
|
||||
foreach(EXAMPLE ${EXAMPLES})
|
||||
# get_filename_component(EXAMPLE ${EXAMPLE_FILE} NAME_WE)
|
||||
|
||||
# get c file
|
||||
set (SOURCES_EXAMPLE ${BTSTACK_ROOT}/example/${EXAMPLE}.c)
|
||||
list(APPEND SOURCES_EXAMPLE "${CMAKE_SOURCE_DIR}/src/main.c")
|
||||
message("${BTSTACK_ROOT}")
|
||||
message("${SOURCES_EXAMPLE}")
|
||||
# add lwip sources for lwip examples
|
||||
if ( "${LWIP_EXAMPLES}" MATCHES ${EXAMPLE} )
|
||||
list(APPEND SOURCES_EXAMPLE ${SOURCES_LWIP})
|
||||
endif()
|
||||
|
||||
# add GATT DB creation
|
||||
if ( "${EXAMPLES_GATT}" MATCHES ${EXAMPLE} )
|
||||
message("example ${EXAMPLE} -- with GATT DB")
|
||||
add_custom_command(
|
||||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE}.h
|
||||
DEPENDS ${BTSTACK_ROOT}/example/${EXAMPLE}.gatt
|
||||
COMMAND ${Python_EXECUTABLE}
|
||||
ARGS ${BTSTACK_ROOT}/tool/compile_gatt.py ${BTSTACK_ROOT}/example/${EXAMPLE}.gatt ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE}.h
|
||||
)
|
||||
list(APPEND SOURCES_EXAMPLE ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE}.h)
|
||||
else()
|
||||
message("example ${EXAMPLE}")
|
||||
endif()
|
||||
# add_executable(${EXAMPLE} ${SOURCES_EXAMPLE} )
|
||||
target_sources( app PRIVATE ${SOURCES_EXAMPLE} )
|
||||
# target_link_libraries(${EXAMPLE} btstack)
|
||||
target_link_libraries( app PRIVATE btstack )
|
||||
endforeach(EXAMPLE)
|
39
port/zephyr/README.md
Normal file
39
port/zephyr/README.md
Normal file
@ -0,0 +1,39 @@
|
||||
# Bluetooth: btstack
|
||||
|
||||
## Overview
|
||||
|
||||
This sample demonstrates Bluetooth functionality utilizing btstack.
|
||||
|
||||
## Requirements
|
||||
|
||||
* A board with Bluetooth LE support
|
||||
|
||||
## Building and Running
|
||||
|
||||
first step needs only be done once.
|
||||
Step two every time.
|
||||
|
||||
### 1. Build environment preconditions
|
||||
Follow the getting started [guide](https://docs.zephyrproject.org/latest/develop/getting_started/index.html)
|
||||
till you can build samples.
|
||||
Then update 'env.sh' accordingly.
|
||||
|
||||
### 2. Prepare the build environmet
|
||||
before beeing able to build this sample the environment needs to be prepared with:
|
||||
```sh
|
||||
source env.sh
|
||||
```
|
||||
|
||||
### 3. Building samples
|
||||
You can build using:
|
||||
```sh
|
||||
west build -b nrf52840dk_nrf52840
|
||||
```
|
||||
to change zephyr platform settings use:
|
||||
```sh
|
||||
west build -b nrf52840dk_nrf52840 -t menuconfig
|
||||
```
|
||||
to build a different example:
|
||||
```sh
|
||||
EXAMPLE=gatt_streamer_server west build -b nrf52840dk_nrf52840
|
||||
```
|
47
port/zephyr/btstack_config.h
Normal file
47
port/zephyr/btstack_config.h
Normal file
@ -0,0 +1,47 @@
|
||||
//
|
||||
// btstack_config.h for mRF5-Zephyr port
|
||||
//
|
||||
// Documentation: https://bluekitchen-gmbh.com/btstack/#how_to/
|
||||
//
|
||||
|
||||
#ifndef BTSTACK_CONFIG_H
|
||||
#define BTSTACK_CONFIG_H
|
||||
|
||||
// Port related features
|
||||
|
||||
// BTstack features that can be enabled
|
||||
#define ENABLE_BLE
|
||||
#define ENABLE_LE_CENTRAL
|
||||
#define ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
|
||||
#define ENABLE_LE_DATA_LENGTH_EXTENSION
|
||||
#define ENABLE_LE_PERIPHERAL
|
||||
#define ENABLE_LOG_ERROR
|
||||
#define ENABLE_LOG_INFO
|
||||
#define ENABLE_PRINTF_HEXDUMP
|
||||
|
||||
// BTstack configuration. buffers, sizes, ...
|
||||
#define HCI_ACL_PAYLOAD_SIZE 260
|
||||
#define MAX_NR_GATT_CLIENTS 1
|
||||
#define MAX_NR_HCI_CONNECTIONS 1
|
||||
#define MAX_NR_L2CAP_CHANNELS 1
|
||||
#define MAX_NR_L2CAP_SERVICES 1
|
||||
#define MAX_NR_SM_LOOKUP_ENTRIES 3
|
||||
#define MAX_NR_WHITELIST_ENTRIES 1
|
||||
|
||||
#define MAX_NR_BNEP_CHANNELS 0
|
||||
#define MAX_NR_BNEP_SERVICES 0
|
||||
#define MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES 0
|
||||
#define MAX_NR_HFP_CONNECTIONS 0
|
||||
#define MAX_NR_LE_DEVICE_DB_ENTRIES 1
|
||||
#define MAX_NR_RFCOMM_CHANNELS 0
|
||||
#define MAX_NR_RFCOMM_MULTIPLEXERS 0
|
||||
#define MAX_NR_RFCOMM_SERVICES 0
|
||||
#define MAX_NR_SERVICE_RECORD_ITEMS 0
|
||||
|
||||
// hack to fix usage of hci_init in zephry
|
||||
#define hci_init btstack_hci_init
|
||||
|
||||
// LE Device DB using TLV
|
||||
#define NVM_NUM_DEVICE_DB_ENTRIES 16
|
||||
|
||||
#endif
|
4
port/zephyr/env.sh
Normal file
4
port/zephyr/env.sh
Normal file
@ -0,0 +1,4 @@
|
||||
ZEPHYR_ROOT=<needs to be set>
|
||||
|
||||
source ${ZEPHYR_ROOT}/.venv/bin/activate
|
||||
source ${ZEPHYR_ROOT}/zephyr/zephyr-env.sh
|
57
port/zephyr/prj.conf
Normal file
57
port/zephyr/prj.conf
Normal file
@ -0,0 +1,57 @@
|
||||
# Debug output
|
||||
#CONFIG_CONSOLE=y
|
||||
#CONFIG_STDOUT_CONSOLE=y
|
||||
CONFIG_UART_CONSOLE=n
|
||||
CONFIG_RTT_CONSOLE=y
|
||||
CONFIG_USE_SEGGER_RTT=y
|
||||
#CONFIG_GPIO=y
|
||||
#CONFIG_SERIAL=y
|
||||
#CONFIG_UART_INTERRUPT_DRIVEN=y
|
||||
|
||||
CONFIG_I2C=n
|
||||
CONFIG_WATCHDOG=n
|
||||
CONFIG_SPI=n
|
||||
CONFIG_SERIAL=n
|
||||
|
||||
CONFIG_PM=n
|
||||
|
||||
CONFIG_DYNAMIC_INTERRUPTS=n
|
||||
CONFIG_IRQ_OFFLOAD=n
|
||||
|
||||
CONFIG_BOOT_BANNER=n
|
||||
CONFIG_BOOT_DELAY=0
|
||||
|
||||
CONFIG_SIZE_OPTIMIZATIONS=y
|
||||
CONFIG_ARM_MPU=n
|
||||
|
||||
CONFIG_BT=y
|
||||
CONFIG_BT_HCI_RAW=y
|
||||
CONFIG_BT_BUF_ACL_RX_SIZE=255
|
||||
CONFIG_BT_BUF_CMD_TX_SIZE=255
|
||||
CONFIG_BT_BUF_EVT_DISCARDABLE_SIZE=255
|
||||
CONFIG_BT_CTLR_ASSERT_HANDLER=y
|
||||
CONFIG_BT_MAX_CONN=16
|
||||
CONFIG_BT_TINYCRYPT_ECC=n
|
||||
#CONFIG_BT_CTLR_DTM_HCI=y
|
||||
|
||||
CONFIG_BT_CTLR_PRIVACY=n
|
||||
CONFIG_BT_CTLR_PHY_2M=n
|
||||
|
||||
CONFIG_BT_RX_STACK_SIZE=1024
|
||||
CONFIG_BT_HCI_TX_STACK_SIZE_WITH_PROMPT=y
|
||||
CONFIG_BT_HCI_TX_STACK_SIZE=640
|
||||
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=512
|
||||
#CONFIG_MPSL_WORK_STACK_SIZE=640
|
||||
CONFIG_MAIN_STACK_SIZE=4096
|
||||
CONFIG_IDLE_STACK_SIZE=128
|
||||
CONFIG_ISR_STACK_SIZE=1024
|
||||
|
||||
#CONFIG_THREAD_ANALYZER=y
|
||||
#CONFIG_THREAD_ANALYZER_USE_PRINTK=y
|
||||
#CONFIG_THREAD_ANALYZER_AUTO=y
|
||||
#CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=5
|
||||
#CONFIG_THREAD_NAME=y
|
||||
|
||||
# Workaround: Unable to allocate command buffer when using K_NO_WAIT since
|
||||
# Host number of completed commands does not follow normal flow control.
|
||||
CONFIG_BT_BUF_CMD_TX_COUNT=10
|
12
port/zephyr/sample.yaml
Normal file
12
port/zephyr/sample.yaml
Normal file
@ -0,0 +1,12 @@
|
||||
sample:
|
||||
name: Bluetooth btstack
|
||||
tests:
|
||||
sample.bluetooth.btstack:
|
||||
harness: bluetooth
|
||||
platform_allow:
|
||||
- bbc_microbit
|
||||
- qemu_x86
|
||||
- nrf52840dk_nrf52840
|
||||
integration_platforms:
|
||||
- qemu_x86
|
||||
tags: bluetooth
|
272
port/zephyr/src/main.c
Normal file
272
port/zephyr/src/main.c
Normal file
@ -0,0 +1,272 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Nordic Semiconductor ASA
|
||||
* Copyright (c) 2015-2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/bluetooth/bluetooth.h>
|
||||
#include <zephyr/bluetooth/l2cap.h>
|
||||
#include <zephyr/bluetooth/hci.h>
|
||||
#include <zephyr/bluetooth/buf.h>
|
||||
#include <zephyr/bluetooth/hci_raw.h>
|
||||
|
||||
// Nordic NDK
|
||||
#include "nrf.h"
|
||||
|
||||
// BTstack
|
||||
#include "btstack_debug.h"
|
||||
#include "btstack_event.h"
|
||||
#include "btstack_memory.h"
|
||||
#include "btstack_run_loop_zephyr.h"
|
||||
#include "hci.h"
|
||||
#include "hci_dump.h"
|
||||
#include "hci_dump_embedded_stdout.h"
|
||||
#include "hci_transport.h"
|
||||
|
||||
#include "btstack_tlv.h"
|
||||
#include "btstack_tlv_none.h"
|
||||
#include "ble/le_device_db_tlv.h"
|
||||
|
||||
static K_FIFO_DEFINE(tx_queue);
|
||||
static K_FIFO_DEFINE(rx_queue);
|
||||
|
||||
//
|
||||
// hci_transport_zephyr.c
|
||||
//
|
||||
|
||||
static void (*transport_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
|
||||
|
||||
/**
|
||||
* init transport
|
||||
* @param transport_config
|
||||
*/
|
||||
static void transport_init(const void *transport_config){
|
||||
/* startup Controller */
|
||||
bt_enable_raw(&rx_queue);
|
||||
}
|
||||
|
||||
/**
|
||||
* open transport connection
|
||||
*/
|
||||
static int transport_open(void){
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* close transport connection
|
||||
*/
|
||||
static int transport_close(void){
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* register packet handler for HCI packets: ACL, SCO, and Events
|
||||
*/
|
||||
static void transport_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
|
||||
transport_packet_handler = handler;
|
||||
}
|
||||
|
||||
static void send_hardware_error(uint8_t error_code){
|
||||
// hci_outgoing_event[0] = HCI_EVENT_HARDWARE_ERROR;
|
||||
// hci_outgoing_event[1] = 1;
|
||||
// hci_outgoing_event[2] = error_code;
|
||||
// hci_outgoing_event_ready = 1;
|
||||
}
|
||||
|
||||
static int transport_send_packet(uint8_t packet_type, uint8_t *packet, int size){
|
||||
struct net_buf *buf;
|
||||
switch (packet_type){
|
||||
case HCI_COMMAND_DATA_PACKET:
|
||||
buf = bt_buf_get_tx(BT_BUF_CMD, K_NO_WAIT, packet, size);
|
||||
if (!buf) {
|
||||
log_error("No available command buffers!\n");
|
||||
break;
|
||||
}
|
||||
|
||||
memcpy(net_buf_add(buf, size), packet, size);
|
||||
bt_send(buf);
|
||||
break;
|
||||
case HCI_ACL_DATA_PACKET:
|
||||
buf = bt_buf_get_tx(BT_BUF_ACL_OUT, K_NO_WAIT, packet, size);
|
||||
if (!buf) {
|
||||
log_error("No available ACL buffers!\n");
|
||||
break;
|
||||
}
|
||||
|
||||
memcpy(net_buf_add(buf, size), packet, size);
|
||||
bt_send(buf);
|
||||
break;
|
||||
default:
|
||||
send_hardware_error(0x01); // invalid HCI packet
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const hci_transport_t transport = {
|
||||
/* const char * name; */ "nRF5-Zephyr",
|
||||
/* void (*init) (const void *transport_config); */ &transport_init,
|
||||
/* int (*open)(void); */ &transport_open,
|
||||
/* int (*close)(void); */ &transport_close,
|
||||
/* void (*register_packet_handler)(void (*handler)(...); */ &transport_register_packet_handler,
|
||||
/* int (*can_send_packet_now)(uint8_t packet_type); */ NULL,
|
||||
/* int (*send_packet)(...); */ &transport_send_packet,
|
||||
/* int (*set_baudrate)(uint32_t baudrate); */ NULL,
|
||||
/* void (*reset_link)(void); */ NULL,
|
||||
};
|
||||
|
||||
static const hci_transport_t * transport_get_instance(void){
|
||||
return &transport;
|
||||
}
|
||||
|
||||
static void transport_deliver_controller_packet(struct net_buf * buf){
|
||||
uint16_t size = buf->len;
|
||||
uint8_t * packet = buf->data;
|
||||
switch (bt_buf_get_type(buf)) {
|
||||
case BT_BUF_ACL_IN:
|
||||
transport_packet_handler(HCI_ACL_DATA_PACKET, packet, size);
|
||||
break;
|
||||
case BT_BUF_EVT:
|
||||
transport_packet_handler(HCI_EVENT_PACKET, packet, size);
|
||||
break;
|
||||
default:
|
||||
log_error("Unknown type %u\n", bt_buf_get_type(buf));
|
||||
break;
|
||||
}
|
||||
net_buf_unref(buf);
|
||||
}
|
||||
|
||||
// btstack_run_loop_zephry.c
|
||||
|
||||
// the run loop
|
||||
//static btstack_linked_list_t timers;
|
||||
|
||||
// TODO: handle 32 bit ms time overrun
|
||||
static uint32_t btstack_run_loop_zephyr_get_time_ms(void){
|
||||
return k_uptime_get_32();
|
||||
}
|
||||
|
||||
static void btstack_run_loop_zephyr_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
|
||||
ts->timeout = k_uptime_get_32() + 1 + timeout_in_ms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute run_loop
|
||||
*/
|
||||
static void btstack_run_loop_zephyr_execute(void) {
|
||||
while (1) {
|
||||
// process timers
|
||||
uint32_t now = k_uptime_get_32();
|
||||
btstack_run_loop_base_process_timers(now);
|
||||
|
||||
// get time until next timer expires
|
||||
k_timeout_t timeout;
|
||||
timeout.ticks = btstack_run_loop_base_get_time_until_timeout(now);
|
||||
if (timeout.ticks < 0){
|
||||
timeout.ticks = K_TICKS_FOREVER;
|
||||
}
|
||||
|
||||
// process RX fifo only
|
||||
struct net_buf *buf = net_buf_get(&rx_queue, timeout);
|
||||
if (buf){
|
||||
transport_deliver_controller_packet(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void btstack_run_loop_zephyr_btstack_run_loop_init(void){
|
||||
btstack_run_loop_base_init();
|
||||
}
|
||||
|
||||
static const btstack_run_loop_t btstack_run_loop_zephyr = {
|
||||
&btstack_run_loop_zephyr_btstack_run_loop_init,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
&btstack_run_loop_zephyr_set_timer,
|
||||
&btstack_run_loop_base_add_timer,
|
||||
&btstack_run_loop_base_remove_timer,
|
||||
&btstack_run_loop_zephyr_execute,
|
||||
&btstack_run_loop_base_dump_timer,
|
||||
&btstack_run_loop_zephyr_get_time_ms,
|
||||
};
|
||||
/**
|
||||
* @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init
|
||||
*/
|
||||
const btstack_run_loop_t * btstack_run_loop_zephyr_get_instance(void){
|
||||
return &btstack_run_loop_zephyr;
|
||||
}
|
||||
|
||||
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
||||
|
||||
static bd_addr_t static_address;
|
||||
|
||||
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||
if (packet_type != HCI_EVENT_PACKET) return;
|
||||
if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
|
||||
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
|
||||
printf("BTstack up and running as %s.\n", bd_addr_to_str(static_address));
|
||||
}
|
||||
|
||||
int btstack_main(void);
|
||||
|
||||
#if defined(CONFIG_BT_CTLR_ASSERT_HANDLER)
|
||||
void bt_ctlr_assert_handle(char *file, uint32_t line)
|
||||
{
|
||||
printf("CONFIG_BT_CTLR_ASSERT_HANDLER: file %s, line %u\n", file, line);
|
||||
while (1) {
|
||||
}
|
||||
}
|
||||
#endif /* CONFIG_BT_CTLR_ASSERT_HANDLER */
|
||||
|
||||
void main(void)
|
||||
{
|
||||
// configure console UART by replacing CONFIG_UART_NRF5_BAUD_RATE with 115200 in uart_console.c
|
||||
|
||||
printf("BTstack booting up..\n");
|
||||
|
||||
// start with BTstack init - especially configure HCI Transport
|
||||
btstack_memory_init();
|
||||
btstack_run_loop_init(btstack_run_loop_zephyr_get_instance());
|
||||
|
||||
// enable full log output while porting
|
||||
hci_dump_init(hci_dump_embedded_stdout_get_instance());
|
||||
|
||||
const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_none_init_instance();
|
||||
// setup global tlv
|
||||
btstack_tlv_set_instance(btstack_tlv_impl, NULL);
|
||||
|
||||
// setup LE Device DB using TLV
|
||||
le_device_db_tlv_configure(btstack_tlv_impl, NULL);
|
||||
|
||||
// init HCI
|
||||
hci_init(transport_get_instance(), NULL);
|
||||
|
||||
// nRF5 chipsets don't have an official public address
|
||||
// Instead, a Static Random Address is assigned during manufacturing
|
||||
// let's use it as well
|
||||
big_endian_store_16(static_address, 0, NRF_FICR->DEVICEADDR[1] | 0xc000);
|
||||
big_endian_store_32(static_address, 2, NRF_FICR->DEVICEADDR[0]);
|
||||
gap_random_address_set(static_address);
|
||||
|
||||
// inform about BTstack state
|
||||
hci_event_callback_registration.callback = &packet_handler;
|
||||
hci_add_event_handler(&hci_event_callback_registration);
|
||||
|
||||
// hand over to btstack embedded code
|
||||
btstack_main();
|
||||
|
||||
// go
|
||||
btstack_run_loop_execute();
|
||||
|
||||
while (1){};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user