Skip to content

Commit

Permalink
[Telink]: BLE commissioning (source part) (#15723)
Browse files Browse the repository at this point in the history
* [Telink]: Add basic skeleton to integrate Telink SDK

* [Telink]: Rework Telink SDK integration skeleton

* [Telink]: Prepare BLEManagerImpl sceleton for implementation

* [Telink]: BLE advertisement is scanable by chip-device-ctrl tool

* [Telink]: Fix mDNS error occurs on commissioning window opening

* [Telink]: Implement Matter BLE connection

* [Telink]: Remove my_ prefix for GATT variables

* [Telink]: Implement Matter GATT Services

* [Telink]: Fix issue with improper structure alignment

* [Telink]: Implement Rx characteristic write handling

* [Telink]: Implement Matter BLE secure connection

* [Telink]: Move Shutdown implementation to .cpp part

* [Telink]: Add data->is_ready flag to manipulate radio driver state

* [Telink]: Adapt solution for Thread 1.2

* [Telink]: Increase size of CHIP thread stack due to stack overflow issue

* [Telink]: Add board name to CMakeLists.txt

* [Telink]: Add HandleThreadStateChange method to automatic BLE disconnection on Thread provisioning

* [Telink]: Refactoring of SwitchToIeee802154 and other cleanups and improvements

The commit itself is not really stable. It should be definitely cleaned in a future
WARNING: for debug purposes, timeouts in src/platform/DeviceControlServer.cpp and
src/platform/DeviceControlServer.cpp has been hard-coded with bigger value.
The timeouts shall be changed back in the future.

* [Telink]: Fix issue with interrupts disabling after commissioning completion

* [Telink]: Fix issue with memory corruption in _InitStack() method

* [Telink]: Remove debug LED initialization form BLEManagerImpl

* [Telink]: Restore default timeouts

* [Telink]: Add tlsr9518adk80d overlay to setup clock frequency to 96MHz

* [Telink]: Fix issue when Matter GATT characteristics are not discoverable after connection

* [Telink]: Modify default CHIP library timeouts to meet Telink IC timings

WARNING: This commit shall be reverted before pull request to upstream

* [Telink]: Adapt solution for latest Zephyr commit

* [Telink]: Change log level

* [Telink]: Remove unnecessary SwitchToIeee802154

* [Telink]: Fix issue with non-logging of Detail log level

* [Telink]: Fix fail at the end of commissioning

* [Telink]: Cleanups

* [Telink]: Restyled fix

* [Telink]: Update lighting example Readme.md

* [Telink]: Remove unnecessary reboot.h include

* [Telink]: Fix spelling mistakes

* [Telink]: Remove restriction for progress logging

* [Telink]: Add HW cryptography support.

* [Telink]: Update docker image version to 0.5.59

Co-authored-by: Alex Tsitsiura <[email protected]>
Co-authored-by: Andrii Bilynskyi <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Nov 27, 2023
1 parent 9064e2c commit 2515753
Show file tree
Hide file tree
Showing 46 changed files with 15,334 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/examples-telink.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
if: github.actor != 'restyled-io[bot]'

container:
image: connectedhomeip/chip-build-telink:0.5.58
image: connectedhomeip/chip-build-telink:0.5.59
volumes:
- "/tmp/bloat_reports:/tmp/bloat_reports"

Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,6 @@
[submodule "p6/serial-flash"]
path = third_party/p6/p6_sdk/libs/serial-flash
url = https://github.com/Infineon/serial-flash
[submodule "third_party/telink_sdk/repo"]
path = third_party/telink_sdk/repo
url = https://github.com/rikorsev/telink_b91_ble_sdk
18 changes: 18 additions & 0 deletions build_overrides/telink.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2020-2021 Project CHIP Authors
#
# 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.

declare_args() {
# Root directory for Telink SDK.
telink_sdk_build_root = "//third_party/telink_sdk"
}
26 changes: 21 additions & 5 deletions config/telink/app/zephyr.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ CONFIG_CHIP=y

CONFIG_STD_CPP14=y

# Logging
CONFIG_LOG=y
CONFIG_LOG_MODE_MINIMAL=y
CONFIG_PRINTK=y
CONFIG_ASSERT=y
CONFIG_CBPRINTF_LIBC_SUBSTS=y
CONFIG_MATTER_LOG_LEVEL_DBG=y

# Generic networking options
CONFIG_NETWORKING=y
Expand Down Expand Up @@ -64,10 +66,7 @@ CONFIG_OPENTHREAD_DEBUG=y
CONFIG_OPENTHREAD_L2_DEBUG=y
CONFIG_OPENTHREAD_L2_LOG_LEVEL_INF=y

CONFIG_OPENTHREAD_CHANNEL=26

CONFIG_NET_CONFIG_IEEE802154_DEV_NAME="IEEE802154_b91"

CONFIG_NET_CONFIG_MY_IPV6_ADDR="fdde:ad00:beef::1"
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="fdde:ad00:beef::2"

Expand All @@ -82,14 +81,31 @@ CONFIG_OPENTHREAD_MTD_NETDIAG=y
CONFIG_OPENTHREAD_ENABLE_SERVICE=y
CONFIG_OPENTHREAD_MANUAL_START=y
CONFIG_OPENTHREAD_THREAD_STACK_SIZE=6144
CONFIG_OPENTHREAD_LOG_LEVEL_DEBG=y

# Enable Thread 1.2 features
CONFIG_OPENTHREAD_THREAD_VERSION_1_2=y
CONFIG_OPENTHREAD_DUA=y
CONFIG_OPENTHREAD_MLR=y

# mbedTLS tweaks
CONFIG_MBEDTLS_DEBUG=y

CONFIG_MBEDTLS_USER_CONFIG_ENABLE=y
CONFIG_MBEDTLS_USER_CONFIG_FILE="telink-mbedtls-config.h"

# TBD: Something wrong with this heap. Need to be investigated. Cirrently just set minimal size
CONFIG_MBEDTLS_HEAP_SIZE=0

CONFIG_OPENTHREAD_EXTERNAL_HEAP=y

# Switch on Telink SDK
CONFIG_TELINK_BLE_LIB=y

# Config dynamic interrupts to have posibility to switch between BLE/Thread radio drivers
CONFIG_DYNAMIC_INTERRUPTS=y

# Set multiplicator of Name Value Storage (NVS) as 8 to reach NVS sector size 2048
# nvs_sector_size = flash_page_size * mult = 256 * 8 = 2048
CONFIG_SETTINGS_NVS_SECTOR_SIZE_MULT=8

# Shall settings
CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE=255
6 changes: 5 additions & 1 deletion config/telink/chip-gn/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/telink.gni")

import("${build_root}/chip/tests.gni")

Expand All @@ -24,7 +25,10 @@ declare_args() {
}

group("telink") {
deps = [ "${chip_root}/src/lib" ]
deps = [
"${chip_root}/src/lib",
"${telink_sdk_build_root}",
]

if (chip_build_tests) {
deps += [ "${chip_root}/src:tests" ]
Expand Down
1 change: 0 additions & 1 deletion config/telink/chip-gn/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ chip_build_tests = false
chip_project_config_include = ""
chip_system_project_config_include = ""
chip_ble_project_config_include = ""
chip_config_network_layer_ble = false

custom_toolchain = "${chip_root}/config/telink/chip-gn/toolchain:zephyr"

Expand Down
27 changes: 22 additions & 5 deletions config/telink/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,16 @@ if (CONFIG_POSIX_API)
)
endif()

zephyr_include_directories(${ZEPHYR_BASE}/../modules/crypto/mbedtls/mbedtls/include)
# Add MbedTLS config file
list(APPEND CHIP_CFLAGS -DMBEDTLS_CONFIG_FILE=CONFIG_MBEDTLS_CFG_FILE)

# Add hardware cryptography dependencies
zephyr_include_directories(${ZEPHYR_BASE}/../modules/crypto/mbedtls/include)
zephyr_include_directories(${ZEPHYR_BASE}/modules/mbedtls/configs)
zephyr_include_directories(${CHIP_ROOT}/src/platform/telink/)
zephyr_include_directories(${CHIP_ROOT}/src/platform/telink/crypto/)
zephyr_sources(${ZEPHYR_BASE}/../modules/hal/telink/tlsr9/drivers/B91/aes.c)
zephyr_sources(${ZEPHYR_BASE}/../modules/hal/telink/tlsr9/drivers/B91/pke.c)

zephyr_get_compile_flags(CHIP_CFLAGS_C C)
zephyr_get_compile_flags(CHIP_CFLAGS_CC CXX)
Expand All @@ -130,6 +138,10 @@ if (CONFIG_CHIP_PW_RPC)
list(APPEND CHIP_LIBRARIES -lPwRpc)
endif()

if (CONFIG_TELINK_BLE_LIB)
list(APPEND CHIP_LIBRARIES -lB91_ble_lib)
endif()

# Set up CHIP project configuration file

if (CONFIG_CHIP_PROJECT_CONFIG)
Expand Down Expand Up @@ -200,10 +212,8 @@ chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TE
chip_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS)
chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL)

if (BOARD STREQUAL "native_posix")
chip_gn_arg_string("target_cpu" "x86")
elseif (BOARD STREQUAL "native_posix_64")
chip_gn_arg_string("target_cpu" "x64")
if (CONFIG_CHIP_ENABLE_DNSSD_SRP)
chip_gn_arg_string("chip_mdns" "platform")
endif()

file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/args.gn CONTENT ${CHIP_GN_ARGS})
Expand Down Expand Up @@ -243,12 +253,19 @@ target_include_directories(chip INTERFACE
${CHIP_ROOT}/src/include
${CHIP_ROOT}/src/lib
${CHIP_ROOT}/third_party/nlassert/repo/include
${CHIP_ROOT}/third_party/telink_sdk
${CMAKE_CURRENT_BINARY_DIR}/gen/include
)
target_link_directories(chip INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/lib)

if (CONFIG_CHIP_LIB_SHELL)
target_link_options(chip INTERFACE -Wl,--whole-archive -lCHIPShell -Wl,--no-whole-archive)
endif()

if (CONFIG_TELINK_BLE_LIB)
target_link_directories(chip INTERFACE ${CHIP_ROOT}/third_party/telink_sdk/repo/eagle_ble_sdk/proj_lib)
endif()

target_link_libraries(chip INTERFACE -Wl,--start-group ${CHIP_LIBRARIES} -Wl,--end-group)
add_dependencies(chip chip-gn)

Expand Down
23 changes: 23 additions & 0 deletions config/telink/chip-module/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Copyright (c) 2021 Project CHIP Authors
#
# 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.
#

rsource "../../zephyr/Kconfig"

config TELINK_BLE_LIB
bool "Include third party Telink BLE SDK"
default n
help
Include third party Telink BLE SDK
2 changes: 1 addition & 1 deletion config/telink/chip-module/zephyr/module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

build:
cmake: .
kconfig: ../../zephyr/Kconfig
kconfig: Kconfig
2 changes: 2 additions & 0 deletions examples/lighting-app/telink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#
cmake_minimum_required(VERSION 3.13.1)

set(BOARD tlsr9518adk80d)

get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/third_party/connectedhomeip REALPATH)
get_filename_component(NLIO_ROOT ${CHIP_ROOT}/third_party/nlio/repo/include REALPATH)
get_filename_component(TELINK_COMMON ${CHIP_ROOT}/examples/platform/telink REALPATH)
Expand Down
121 changes: 18 additions & 103 deletions examples/lighting-app/telink/Readme.md
Original file line number Diff line number Diff line change
@@ -1,111 +1,39 @@
![Telink B91 EVK](http://wiki.telink-semi.cn/wiki/assets/Hardware/B91_Generic_Starter_Kit_Hardware_Guide/connection_chart.png)

## Build
## Build and flash

1. Pull docker image from repository:

```
```bash
$ docker pull connectedhomeip/chip-build-telink:latest
```

1. Run docker container:

```
$ docker run -it -v ${CHIP_BASE}:/root/chip connectedhomeip/chip-build-telink:latest
```bash
$ docker run -it --rm -v ${CHIP_BASE}:/root/chip -v /dev/bus/usb:/dev/bus/usb --device-cgroup-rule "c 189:* rmw" connectedhomeip/chip-build-telink:latest
```

here `${CHIP_BASE}` is directory which contains CHIP repo files **!!!Pay
attention that OUTPUT_DIR should contains ABSOLUTE path to output dir**

1. Bootstrap the build environment:
```
source ./scripts/bootstrap.sh
```
1. Run build script:
```
./scripts/build/build_examples.py --target telink-tlsr9518adk80d-light build
```
1. Exit docker container and collect build artifacts. Firmware binary would be
located in
**\${CHIP_BASE}/out/telink-tlsr9518adk80d-light/zephyr/zephyr.bin**
## Flash
### Presetup
To make `west flash` command works following steps should be performed just
once:
1. Download toolchain:
```
$ wget http://wiki.telink-semi.cn/tools_and_sdk/Tools/IDE/telink_riscv_linux_toolchain.zip
$ unzip telink_riscv_linux_toolchain.zip
```
1. Add TELINK_TOOLCHAIN_BASE variable to environment:
```
$ export TELINK_TOOLCHAIN_BASE=${PATH_TO_TOOLCHAIN}
```
1. Setup dependencies:
1. Activate the build environment:

```
$ sudo dpkg --add-architecture i386
$ sudo apt-get update
$ sudo apt-get install -y libc6:i386 libncurses5:i386 libstdc++6:i386
```bash
$ source ./scripts/activate.sh
```

### Flashing
1. In the example dir run:

1. Go to example directory and flash board with west command
```
$ cd ${TELINK_LIGHTING_EXAMPLE_DIR} && west flash
```bash
$ west build
```

## Border Router
### Build
Use following manual to build your own border router:
https://openthread.io/guides/border-router/build Pay attention that border
router should be configured as Access Point i.e next command should be executed
in step 3:
1. Flash binary:

```
$ BORDER_ROUTING=0 NETWORK_MANAGER=1 ./script/setup
```
### Setup IPv6
Pay attention that border router should be configured as IPv6 access point.
1. To do so perform the following command:
```
ip -6 addr add 2001:db8:1::1/64 dev wlan0
```
2. Add following lines in **/etc/dnsmasq.conf** file:
$ west flash --erase
```
enable-ra
dhcp-range=2001:db8:1::, ra-only, 64, 12h
```
### Config network
Use [Web GUI](https://openthread.io/guides/border-router/web-gui) to config
Thread network **tlsr9518adk80d** board supports only static commissioning with
predefined Thread credentials shown in table below:
| Item | Value |
| :--------------------- | :------------------------------: |
| Network name | OpenThreadDemo |
| Network ExtendedPAN ID | 1111111122222222 |
| PAN ID | 0x1234 |
| Passphrase | 123456 |
| Master Key | 00112233445566778899aabbccddeeff |
| Channel | 15 |
| On-Mesh Prefix | fd11:22:: |

## Usage

Expand All @@ -123,11 +51,12 @@ To get output from device, connect UART to following pins:

The following buttons are available on **tlsr9518adk80d** board:

| Name | Function | Description |
| :------- | :--------------- | :----------------------------------------------------------------------------------------------------- |
| Button 1 | Factory reset | Perform factory reset to forget currently commissioned Thread network and back to uncommissioned state |
| Button 2 | Lighting control | Manually triggers the lighting state |
| Button 3 | Thread start | Commission thread with static credentials and enables the Thread on device |
| Name | Function | Description |
| :------- | :--------------------- | :----------------------------------------------------------------------------------------------------- |
| Button 1 | Factory reset | Perform factory reset to forget currently commissioned Thread network and back to uncommissioned state |
| Button 2 | Lighting control | Manually triggers the lighting state |
| Button 3 | Thread start | Commission thread with static credentials and enables the Thread on device |
| Button 4 | Open commission window | The button is opening commissioning window to perform commissioning over BLE |

### LEDs

Expand All @@ -142,20 +71,6 @@ following states:

**Blue** LED shows current state of lightbulb

### Check connection
1. With your client device (PC, Laptop etc.) connect to BorderRouterAP WiFi
2. Press Button 3 on **tlsr9518adk80d** board and wait till it joins to Thread
network
3. Find adjusted IPv6 address in UART output of **tlsr9518adk80d**
4. Perform following command on your client device:
```
ping -6 ${IP_ADDRESS_OF_CHIP_DEVICE}
```
here `${IP_ADDRESS_OF_CHIP_DEVICE}` is address which you got from UART
output of **tlsr9518adk80d** board If everything is ok then `ping` command
should perform transactions without losses
### CHIP tool commands

1. Build
Expand Down
Loading

0 comments on commit 2515753

Please sign in to comment.