Skip to content

Commit

Permalink
Switch to Telink Zephyr main branch (get rid of maintaining special Z…
Browse files Browse the repository at this point in the history
…ephyr branch) (#25940)

* [Telink] Switch to Telink Zephyr main branch

* [Telink] Update telink image to 0.6.53
  • Loading branch information
andriy-bilynskyy authored and pull[bot] committed Sep 22, 2023
1 parent 2c60361 commit 857e435
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/examples-telink.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
if: github.actor != 'restyled-io[bot]'

container:
image: connectedhomeip/chip-build-telink:0.6.47
image: connectedhomeip/chip-build-telink:0.6.53
volumes:
- "/tmp/bloat_reports:/tmp/bloat_reports"

Expand Down
3 changes: 3 additions & 0 deletions config/telink/app/zephyr.conf
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,6 @@ CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE=255

# Legacy
CONFIG_LEGACY_INCLUDE_PATH=y

# BLE MAC address
CONFIG_B91_BLE_CTRL_MAC_FLASH_ADDR=0x1FE000
7 changes: 7 additions & 0 deletions config/telink/chip-module/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,10 @@ config CHIP_ENABLE_PM_DURING_BLE
default y
help
Enable PM during BLE operation.

config CHIP_OPENTHREAD_TX_POWER
int "OpenThread Transmission power"
range -30 9
default 0
help
OpenThread Transmission power in dBm.
2 changes: 1 addition & 1 deletion examples/lighting-app/telink/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ CONFIG_CHIP_ENABLE_PM_DURING_BLE=n

# Custom RF power values
CONFIG_B91_BLE_CTRL_RF_POWER_P9P11DBM=y
CONFIG_IEEE802154_B91_CUSTOM_RF_POWER=9
CONFIG_CHIP_OPENTHREAD_TX_POWER=9
2 changes: 1 addition & 1 deletion examples/temperature-measurement-app/telink/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ CONFIG_CHIP_ENABLE_PM_DURING_BLE=n

# Custom RF power values
CONFIG_B91_BLE_CTRL_RF_POWER_P9P11DBM=y
CONFIG_IEEE802154_B91_CUSTOM_RF_POWER=9
CONFIG_CHIP_OPENTHREAD_TX_POWER=9
2 changes: 1 addition & 1 deletion examples/window-app/telink/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ CONFIG_CHIP_ENABLE_PM_DURING_BLE=n

# Custom RF power values
CONFIG_B91_BLE_CTRL_RF_POWER_P9P11DBM=y
CONFIG_IEEE802154_B91_CUSTOM_RF_POWER=9
CONFIG_CHIP_OPENTHREAD_TX_POWER=9
45 changes: 12 additions & 33 deletions src/platform/telink/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@

#include <array>

// Includes for ieee802154 switchings
#include <drivers/ieee802154/b91.h>

using namespace ::chip;
using namespace ::chip::Ble;
using namespace ::chip::System;
Expand Down Expand Up @@ -148,14 +145,10 @@ CHIP_ERROR InitRandomStaticAddress()

BLEManagerImpl BLEManagerImpl::sInstance;

bool ThreadConnectivityReady;
bool BLERadioInitialized;

CHIP_ERROR BLEManagerImpl::_Init(void)
{
ThreadConnectivityReady = false;
BLERadioInitialized = false;
mconId = NULL;
mBLERadioInitialized = false;
mconId = NULL;

mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;
mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
Expand Down Expand Up @@ -305,21 +298,20 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
return CHIP_ERROR_INCORRECT_STATE;
}

if (!BLERadioInitialized)
if (!mBLERadioInitialized)
{
char bt_dev_name[CONFIG_BT_DEVICE_NAME_MAX];
strncpy(bt_dev_name, bt_get_name(), sizeof(bt_dev_name));
/* Block IEEE802154 */
/* @todo: move to RadioSwitch module*/
const struct device * radio_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_ieee802154));
__ASSERT(radio_dev != NULL, "Get radio_dev fail");
b91_deinit(radio_dev);

/* Switch off Thread */
ThreadStackMgrImpl().SetThreadEnabled(false);
ThreadStackMgrImpl().SetRadioBlocked(true);

/* Init BLE stack */
err = bt_enable(NULL);
VerifyOrReturnError(err == 0, MapErrorZephyr(err));
(void) bt_set_name(bt_dev_name);
BLERadioInitialized = true;
mBLERadioInitialized = true;
#if defined(CONFIG_PM) && !defined(CONFIG_CHIP_ENABLE_PM_DURING_BLE)
pm_policy_state_lock_get(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
#endif
Expand Down Expand Up @@ -931,9 +923,6 @@ CHIP_ERROR BLEManagerImpl::HandleThreadStateChange(const ChipDeviceEvent * event

error = PlatformMgr().PostEvent(&attachEvent);
VerifyOrExit(error == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "PostEvent err: %" CHIP_ERROR_FORMAT, error.Format()));

ChipLogDetail(DeviceLayer, "Thread Connectivity Ready");
ThreadConnectivityReady = true;
}

exit:
Expand All @@ -942,8 +931,7 @@ CHIP_ERROR BLEManagerImpl::HandleThreadStateChange(const ChipDeviceEvent * event

CHIP_ERROR BLEManagerImpl::HandleBleConnectionClosed(const ChipDeviceEvent * event)
{
/* It is time to swich to IEEE802154 radio if it is provisioned */
if (ThreadConnectivityReady)
if (ThreadStackMgrImpl().IsReadyToAttach())
{
SwitchToIeee802154();
}
Expand All @@ -954,28 +942,19 @@ CHIP_ERROR BLEManagerImpl::HandleBleConnectionClosed(const ChipDeviceEvent * eve
/* @todo: move to RadioSwitch module */
void BLEManagerImpl::SwitchToIeee802154(void)
{
int result = 0;

ChipLogProgress(DeviceLayer, "SwitchToIeee802154");

/* Stop BLE */
StopAdvertising();

/* Deinit BLE stack */
bt_disable();
// irq_disable(IRQ1_SYSTIMER);
BLERadioInitialized = false;
mBLERadioInitialized = false;

#if defined(CONFIG_PM) && !defined(CONFIG_CHIP_ENABLE_PM_DURING_BLE)
pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
#endif

const struct device * radio_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_ieee802154));
__ASSERT(radio_dev != NULL, "Get radio_dev fail");

/* Init IEEE802154 */
result = b91_init(radio_dev);
__ASSERT(result == 0, "Init IEEE802154 err: %d", result);
ThreadStackMgrImpl().SetRadioBlocked(false);
ThreadStackMgrImpl().SetThreadEnabled(true);
}

} // namespace Internal
Expand Down
1 change: 1 addition & 0 deletions src/platform/telink/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
PacketBufferHandle c3CharDataBufferHandle;
#endif
bool mBLERadioInitialized;

void DriveBLEState(void);
CHIP_ERROR PrepareAdvertisingRequest(void);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/telink/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static_library("telink") {
if (chip_enable_openthread) {
sources += [
"../OpenThread/OpenThreadUtils.cpp",
"../Zephyr/ThreadStackManagerImpl.cpp",
"ThreadStackManagerImpl.cpp",
"ThreadStackManagerImpl.h",
]

Expand Down
123 changes: 123 additions & 0 deletions src/platform/telink/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
*
* Copyright (c) 2023 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.
*/

/**
* @file
* Provides an implementation of the ThreadStackManager object
* for Telink platform.
*
*/
/* this file behaves like a config.h, comes first */
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp>
#include <platform/telink/ThreadStackManagerImpl.h>

#include <inet/UDPEndPointImpl.h>
#include <lib/support/CodeUtils.h>
#include <platform/OpenThread/OpenThreadUtils.h>
#include <platform/ThreadStackManager.h>

namespace chip {
namespace DeviceLayer {

using namespace ::chip::DeviceLayer::Internal;
using namespace ::chip::Inet;

ThreadStackManagerImpl ThreadStackManagerImpl::sInstance;

CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack()
{
mRadioBlocked = false;
mReadyToAttach = false;
otInstance * const instance = openthread_get_default_instance();

ReturnErrorOnFailure(GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>::DoInit(instance));
#ifdef CONFIG_CHIP_OPENTHREAD_TX_POWER
/* On Zephyr platform otPlatRadioSetTransmitPower does not touch radio HW */
if (otPlatRadioSetTransmitPower(OTInstance(), CONFIG_CHIP_OPENTHREAD_TX_POWER) != OT_ERROR_NONE)
{
ChipLogError(DeviceLayer, "Can't set OpenThread TX power");
}
#endif /* CONFIG_CHIP_OPENTHREAD_TX_POWER */

UDPEndPointImplSockets::SetJoinMulticastGroupHandler([](InterfaceId, const IPAddress & address) {
const otIp6Address otAddress = ToOpenThreadIP6Address(address);

ThreadStackMgr().LockThreadStack();
const auto otError = otIp6SubscribeMulticastAddress(openthread_get_default_instance(), &otAddress);
ThreadStackMgr().UnlockThreadStack();

return MapOpenThreadError(otError);
});

UDPEndPointImplSockets::SetLeaveMulticastGroupHandler([](InterfaceId, const IPAddress & address) {
const otIp6Address otAddress = ToOpenThreadIP6Address(address);

ThreadStackMgr().LockThreadStack();
const auto otError = otIp6UnsubscribeMulticastAddress(openthread_get_default_instance(), &otAddress);
ThreadStackMgr().UnlockThreadStack();

return MapOpenThreadError(otError);
});

return CHIP_NO_ERROR;
}

void ThreadStackManagerImpl::_LockThreadStack()
{
openthread_api_mutex_lock(openthread_get_default_context());
}

bool ThreadStackManagerImpl::_TryLockThreadStack()
{
// There's no openthread_api_mutex_try_lock() in Zephyr, so until it's contributed we must use the low-level API
return k_mutex_lock(&openthread_get_default_context()->api_lock, K_NO_WAIT) == 0;
}

void ThreadStackManagerImpl::_UnlockThreadStack()
{
openthread_api_mutex_unlock(openthread_get_default_context());
}

CHIP_ERROR
ThreadStackManagerImpl::_AttachToThreadNetwork(const Thread::OperationalDataset & dataset,
NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * callback)
{
CHIP_ERROR result = CHIP_NO_ERROR;

if (mRadioBlocked)
{
/* On Telink platform it's not possible to rise Thread network when its used by BLE,
so just mark that it's provisioned and rise Thread after BLE disconnect */
result = SetThreadProvision(dataset.AsByteSpan());
if (result == CHIP_NO_ERROR)
{
mReadyToAttach = true;
callback->OnResult(NetworkCommissioning::Status::kSuccess, CharSpan(), 0);
}
}
else
{
result =
Internal::GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>::_AttachToThreadNetwork(dataset, callback);
}
return result;
}

} // namespace DeviceLayer
} // namespace chip
Loading

0 comments on commit 857e435

Please sign in to comment.