From 11093162780e3029fce47b7d8b87ea7199942bfb Mon Sep 17 00:00:00 2001 From: Sid Hsu Date: Wed, 29 Mar 2023 22:56:03 +0800 Subject: [PATCH] [Infineon] Implement event-based Thread stack task for CYW30739. (#25796) * [Infineon] Implement event-based Thread stack task for CYW30739. * Add EventFlags for platform-specific wrappers of event flags. * Implement otTaskletsSignalPending and otSysEventSignalPending callback functions for the OpenThread stack to notify the Thread stack task if there are pending events to be processed. * Update CYW30739 SDK and ot-ifx submodules for the event-based implementations. * Use the default value of CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE. * Disable OTA of lighting and lock apps because CYW30739 is running out of flash. * Enable OTA and disable FTD for the flash space. * Fix the documents. --- .../lighting-app/infineon/cyw30739/args.gni | 2 +- examples/lock-app/infineon/cyw30739/args.gni | 2 +- src/platform/Infineon/CYW30739/BUILD.gn | 2 + .../CYW30739/CHIPDevicePlatformConfig.h | 1 - src/platform/Infineon/CYW30739/EventFlags.cpp | 72 +++++++++++++++++++ src/platform/Infineon/CYW30739/EventFlags.h | 49 +++++++++++++ .../Infineon/CYW30739/PlatformManagerImpl.cpp | 28 ++------ .../Infineon/CYW30739/PlatformManagerImpl.h | 6 +- .../CYW30739/ThreadStackManagerImpl.cpp | 30 ++++++++ .../CYW30739/ThreadStackManagerImpl.h | 13 ++-- .../infineon/cyw30739_sdk/repos/30739A0 | 2 +- .../cyw30739_sdk/repos/CYW930739M2EVB-01 | 2 +- .../infineon/cyw30739_sdk/repos/btsdk-include | 2 +- .../infineon/cyw30739_sdk/repos/btsdk-tools | 2 +- third_party/openthread/ot-ifx | 2 +- 15 files changed, 176 insertions(+), 39 deletions(-) create mode 100644 src/platform/Infineon/CYW30739/EventFlags.cpp create mode 100644 src/platform/Infineon/CYW30739/EventFlags.h diff --git a/examples/lighting-app/infineon/cyw30739/args.gni b/examples/lighting-app/infineon/cyw30739/args.gni index b8aeb760b79fb6..c19de1f1304321 100644 --- a/examples/lighting-app/infineon/cyw30739/args.gni +++ b/examples/lighting-app/infineon/cyw30739/args.gni @@ -18,7 +18,7 @@ import("${chip_root}/src/platform/Infineon/CYW30739/args.gni") cyw30739_sdk_target = get_label_info(":sdk", "label_no_toolchain") -chip_openthread_ftd = true +chip_openthread_ftd = false chip_enable_ota_requestor = true diff --git a/examples/lock-app/infineon/cyw30739/args.gni b/examples/lock-app/infineon/cyw30739/args.gni index b8aeb760b79fb6..c19de1f1304321 100644 --- a/examples/lock-app/infineon/cyw30739/args.gni +++ b/examples/lock-app/infineon/cyw30739/args.gni @@ -18,7 +18,7 @@ import("${chip_root}/src/platform/Infineon/CYW30739/args.gni") cyw30739_sdk_target = get_label_info(":sdk", "label_no_toolchain") -chip_openthread_ftd = true +chip_openthread_ftd = false chip_enable_ota_requestor = true diff --git a/src/platform/Infineon/CYW30739/BUILD.gn b/src/platform/Infineon/CYW30739/BUILD.gn index 9759c32ede89b9..61f03102cc75b3 100644 --- a/src/platform/Infineon/CYW30739/BUILD.gn +++ b/src/platform/Infineon/CYW30739/BUILD.gn @@ -39,6 +39,8 @@ static_library("CYW30739") { "ConnectivityManagerImpl.h", "DiagnosticDataProviderImpl.cpp", "DiagnosticDataProviderImpl.h", + "EventFlags.cpp", + "EventFlags.h", "FactoryDataProvider.cpp", "FactoryDataProvider.h", "InetPlatformConfig.h", diff --git a/src/platform/Infineon/CYW30739/CHIPDevicePlatformConfig.h b/src/platform/Infineon/CYW30739/CHIPDevicePlatformConfig.h index f66eab30f51e68..1e43a7dcbbf87f 100644 --- a/src/platform/Infineon/CYW30739/CHIPDevicePlatformConfig.h +++ b/src/platform/Infineon/CYW30739/CHIPDevicePlatformConfig.h @@ -63,6 +63,5 @@ // -------------------- Network Telemetry Configuration -------------------- // -------------------- Event Logging Configuration -------------------- -#define CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE 256 // -------------------- Software Update Manager Configuration -------------------- diff --git a/src/platform/Infineon/CYW30739/EventFlags.cpp b/src/platform/Infineon/CYW30739/EventFlags.cpp new file mode 100644 index 00000000000000..4a299843cf6a39 --- /dev/null +++ b/src/platform/Infineon/CYW30739/EventFlags.cpp @@ -0,0 +1,72 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2019 Nest Labs, Inc. + * + * 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 a class that serves as a wrapper for the event system of CYW30739 + * platform's underlying RTOS. An event instance is comprised of 32 flags. + * Each flag can be utilized for thread synchronization purposes. + */ +#include "EventFlags.h" + +#include + +namespace chip { +namespace DeviceLayer { + +CHIP_ERROR EventFlags::Init(void) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + wiced_result_t result; + + mFlags = wiced_rtos_create_event_flags(); + VerifyOrExit(mFlags != nullptr, err = CHIP_ERROR_NO_MEMORY); + + result = wiced_rtos_init_event_flags(mFlags); + VerifyOrExit(result == WICED_SUCCESS, err = CHIP_ERROR_NO_MEMORY); + +exit: + return err; +} + +CHIP_ERROR EventFlags::Set(uint32_t flags) +{ + assert(!wiced_rtos_check_for_stack_overflow()); + + if (wiced_rtos_set_event_flags(mFlags, flags) != WICED_SUCCESS) + { + ChipLogError(DeviceLayer, "wiced_rtos_set_event_flags %08lx", flags); + return CHIP_ERROR_INTERNAL; + } + return CHIP_NO_ERROR; +} + +CHIP_ERROR EventFlags::WaitAnyForever(uint32_t & flags) +{ + const wiced_result_t result = + wiced_rtos_wait_for_event_flags(mFlags, 0xffffffff, &flags, WICED_TRUE, WAIT_FOR_ANY_EVENT, WICED_WAIT_FOREVER); + if (result != WICED_SUCCESS) + { + ChipLogError(DeviceLayer, "wiced_rtos_wait_for_event_flags 0x%08x", result); + return CHIP_ERROR_INTERNAL; + } + return CHIP_NO_ERROR; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Infineon/CYW30739/EventFlags.h b/src/platform/Infineon/CYW30739/EventFlags.h new file mode 100644 index 00000000000000..3ef5824683fc64 --- /dev/null +++ b/src/platform/Infineon/CYW30739/EventFlags.h @@ -0,0 +1,49 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2019 Nest Labs, Inc. + * + * 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 a class that serves as a wrapper for the event system of CYW30739 + * platform's underlying RTOS. An event instance is comprised of 32 flags. + * Each flag can be utilized for thread synchronization purposes. + */ + +#pragma once + +#include +#include + +namespace chip { +namespace DeviceLayer { + +/** + * A class represents an event group with 32 flags. + */ +class EventFlags +{ +public: + CHIP_ERROR Init(void); + CHIP_ERROR Set(uint32_t flags); + CHIP_ERROR WaitAnyForever(uint32_t & flags); + +private: + wiced_event_flags_t * mFlags; +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Infineon/CYW30739/PlatformManagerImpl.cpp b/src/platform/Infineon/CYW30739/PlatformManagerImpl.cpp index c2da0a1efc8414..643cecc44d9fdb 100644 --- a/src/platform/Infineon/CYW30739/PlatformManagerImpl.cpp +++ b/src/platform/Infineon/CYW30739/PlatformManagerImpl.cpp @@ -50,11 +50,8 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) VerifyOrExit(mThread != nullptr, err = CHIP_ERROR_NO_MEMORY); /* Initialize the event flags. */ - mEventFlags = wiced_rtos_create_event_flags(); - VerifyOrExit(mEventFlags != nullptr, err = CHIP_ERROR_NO_MEMORY); + ReturnErrorOnFailure(mEventFlags.Init()); - result = wiced_rtos_init_event_flags(mEventFlags); - VerifyOrExit(result == WICED_SUCCESS, err = CHIP_ERROR_NO_MEMORY); /* Initialize the event queue. */ mEventQueue = wiced_rtos_create_queue(); VerifyOrExit(mEventQueue != nullptr, err = CHIP_ERROR_NO_MEMORY); @@ -87,12 +84,9 @@ void PlatformManagerImpl::_RunEventLoop(void) while (true) { - uint32_t flags_set = 0; - const wiced_result_t result = wiced_rtos_wait_for_event_flags(mEventFlags, 0xffffffff, &flags_set, WICED_TRUE, - WAIT_FOR_ANY_EVENT, WICED_WAIT_FOREVER); - if (result != WICED_SUCCESS) + uint32_t flags_set = 0; + if (mEventFlags.WaitAnyForever(flags_set) != CHIP_NO_ERROR) { - ChipLogError(DeviceLayer, "wiced_rtos_wait_for_event_flags 0x%08x", result); continue; } @@ -147,7 +141,7 @@ CHIP_ERROR PlatformManagerImpl::_PostEvent(const ChipDeviceEvent * event) return CHIP_ERROR_INTERNAL; } - SetEventFlags(kPostEventFlag); + mEventFlags.Set(kPostEventFlag); return CHIP_NO_ERROR; } @@ -172,16 +166,6 @@ CHIP_ERROR PlatformManagerImpl::_StartChipTimer(System::Clock::Timeout durationM void PlatformManagerImpl::_Shutdown() {} -void PlatformManagerImpl::SetEventFlags(uint32_t flags) -{ - assert(!wiced_rtos_check_for_stack_overflow()); - - if (wiced_rtos_set_event_flags(mEventFlags, flags) != WICED_SUCCESS) - { - ChipLogError(DeviceLayer, "%s wiced_rtos_set_event_flags %08lx", __func__, flags); - } -} - void PlatformManagerImpl::HandleTimerEvent(void) { const CHIP_ERROR err = static_cast(DeviceLayer::SystemLayer()).HandlePlatformTimer(); @@ -217,7 +201,7 @@ void PlatformManagerImpl::HandlePostEvent(void) /* Set another application thread event if the event queue is not empty. */ if (!wiced_rtos_is_queue_empty(mEventQueue)) { - SetEventFlags(kPostEventFlag); + mEventFlags.Set(kPostEventFlag); } } @@ -229,7 +213,7 @@ void PlatformManagerImpl::EventLoopTaskMain(uint32_t arg) void PlatformManagerImpl::TimerCallback(WICED_TIMER_PARAM_TYPE params) { - PlatformMgrImpl().SetEventFlags(kTimerEventFlag); + PlatformMgrImpl().mEventFlags.Set(kTimerEventFlag); } int PlatformManagerImpl::GetEntropy(void * data, unsigned char * output, size_t len, size_t * olen) diff --git a/src/platform/Infineon/CYW30739/PlatformManagerImpl.h b/src/platform/Infineon/CYW30739/PlatformManagerImpl.h index 7d121f1dcb7b10..20912ddab9d032 100644 --- a/src/platform/Infineon/CYW30739/PlatformManagerImpl.h +++ b/src/platform/Infineon/CYW30739/PlatformManagerImpl.h @@ -26,7 +26,8 @@ #include #include -#include + +#include "EventFlags.h" namespace chip { namespace DeviceLayer { @@ -57,7 +58,6 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener CHIP_ERROR _StartChipTimer(System::Clock::Timeout durationMS); void _Shutdown(void); - void SetEventFlags(uint32_t flags); void HandleTimerEvent(void); void HandlePostEvent(void); @@ -67,7 +67,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener friend PlatformManagerImpl & PlatformMgrImpl(void); wiced_thread_t * mThread; - wiced_event_flags_t * mEventFlags; + EventFlags mEventFlags; wiced_queue_t * mEventQueue; wiced_timer_t mTimer; wiced_mutex_t * mMutex; diff --git a/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.cpp b/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.cpp index 67a1bea4c7ba42..747c04990242ca 100644 --- a/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.cpp +++ b/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.cpp @@ -45,6 +45,8 @@ CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack() mThread = wiced_rtos_create_thread(); VerifyOrExit(mThread != nullptr, err = CHIP_ERROR_NO_MEMORY); + ReturnErrorOnFailure(mEventFlags.Init()); + mMutex = wiced_rtos_create_mutex(); VerifyOrExit(mMutex != nullptr, err = CHIP_ERROR_NO_MEMORY); @@ -58,6 +60,16 @@ CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack() return err; } +void ThreadStackManagerImpl::SignalThreadActivityPending() +{ + mEventFlags.Set(kActivityPendingEventFlag); +} + +void ThreadStackManagerImpl::SignalThreadActivityPendingFromISR() +{ + mEventFlags.Set(kActivityPendingFromISREventFlag); +} + CHIP_ERROR ThreadStackManagerImpl::_StartThreadTask() { CHIP_ERROR err = CHIP_NO_ERROR; @@ -87,6 +99,12 @@ void ThreadStackManagerImpl::ThreadTaskMain(void) { while (true) { + uint32_t flags = 0; + if (mEventFlags.WaitAnyForever(flags) != CHIP_NO_ERROR) + { + continue; + } + LockThreadStack(); ProcessThreadActivity(); UnlockThreadStack(); @@ -102,6 +120,18 @@ void ThreadStackManagerImpl::ThreadTaskMain(uint32_t arg) } // namespace DeviceLayer } // namespace chip +using namespace ::chip::DeviceLayer; + +extern "C" void otTaskletsSignalPending(otInstance * p_instance) +{ + ThreadStackMgrImpl().SignalThreadActivityPending(); +} + +extern "C" void otSysEventSignalPending(void) +{ + ThreadStackMgrImpl().SignalThreadActivityPendingFromISR(); +} + extern "C" void * otPlatCAlloc(size_t aNum, size_t aSize) { return CHIPPlatformMemoryCalloc(aNum, aSize); diff --git a/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.h b/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.h index 2b3e295501dc58..cdb2cb8ee65fdc 100644 --- a/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.h +++ b/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.h @@ -26,7 +26,8 @@ #include #include -#include + +#include "EventFlags.h" namespace chip { namespace DeviceLayer { @@ -45,6 +46,8 @@ class ThreadStackManagerImpl final : public ThreadStackManager, // ===== Methods that implement the ThreadStackManager abstract interface. CHIP_ERROR _InitThreadStack(); + void SignalThreadActivityPending(); + void SignalThreadActivityPendingFromISR(); inline bool IsCurrentTask(void) { return wiced_rtos_is_current_thread(mThread) == WICED_SUCCESS; } protected: @@ -55,11 +58,6 @@ class ThreadStackManagerImpl final : public ThreadStackManager, bool _TryLockThreadStack(); void _UnlockThreadStack(); - // ===== Methods that override the GenericThreadStackManagerImpl_OpenThread abstract interface. - - void _OnCHIPoBLEAdvertisingStart(); - void _OnCHIPoBLEAdvertisingStop(); - private: // ===== Members for internal use by the following friends. @@ -67,6 +65,7 @@ class ThreadStackManagerImpl final : public ThreadStackManager, friend ThreadStackManagerImpl & ::chip::DeviceLayer::ThreadStackMgrImpl(void); wiced_thread_t * mThread; + EventFlags mEventFlags; wiced_mutex_t * mMutex; static ThreadStackManagerImpl sInstance; @@ -75,6 +74,8 @@ class ThreadStackManagerImpl final : public ThreadStackManager, void ThreadTaskMain(void); static void ThreadTaskMain(uint32_t arg); + static constexpr uint32_t kActivityPendingEventFlag = 1 << 0; + static constexpr uint32_t kActivityPendingFromISREventFlag = 1 << 1; }; /** diff --git a/third_party/infineon/cyw30739_sdk/repos/30739A0 b/third_party/infineon/cyw30739_sdk/repos/30739A0 index 85a07264df8533..f951c7d3d3c37e 160000 --- a/third_party/infineon/cyw30739_sdk/repos/30739A0 +++ b/third_party/infineon/cyw30739_sdk/repos/30739A0 @@ -1 +1 @@ -Subproject commit 85a07264df8533e8eaea85e340a98b4e648adfa1 +Subproject commit f951c7d3d3c37e995b38ea1fdfe1cdae9a5e9cc8 diff --git a/third_party/infineon/cyw30739_sdk/repos/CYW930739M2EVB-01 b/third_party/infineon/cyw30739_sdk/repos/CYW930739M2EVB-01 index d5f5b678236866..d3e46e83a248fd 160000 --- a/third_party/infineon/cyw30739_sdk/repos/CYW930739M2EVB-01 +++ b/third_party/infineon/cyw30739_sdk/repos/CYW930739M2EVB-01 @@ -1 +1 @@ -Subproject commit d5f5b678236866620b8f4a88360fb035d3407e06 +Subproject commit d3e46e83a248fdd4617839761ce2333f8eac1f2c diff --git a/third_party/infineon/cyw30739_sdk/repos/btsdk-include b/third_party/infineon/cyw30739_sdk/repos/btsdk-include index b1eb9b39f3ef86..b9a1aac342f239 160000 --- a/third_party/infineon/cyw30739_sdk/repos/btsdk-include +++ b/third_party/infineon/cyw30739_sdk/repos/btsdk-include @@ -1 +1 @@ -Subproject commit b1eb9b39f3ef86211164c4d9825bee6316ac76c6 +Subproject commit b9a1aac342f2399ccd4894b0fe6f9e323a613b68 diff --git a/third_party/infineon/cyw30739_sdk/repos/btsdk-tools b/third_party/infineon/cyw30739_sdk/repos/btsdk-tools index 9743681eed4d3f..0943e94ccca9ed 160000 --- a/third_party/infineon/cyw30739_sdk/repos/btsdk-tools +++ b/third_party/infineon/cyw30739_sdk/repos/btsdk-tools @@ -1 +1 @@ -Subproject commit 9743681eed4d3f16a291bff6f8aee65349d1241b +Subproject commit 0943e94ccca9ed871b0e9a6ebfbc126ad50a6d11 diff --git a/third_party/openthread/ot-ifx b/third_party/openthread/ot-ifx index 2ee7240000f9bc..6bf0fdbd389794 160000 --- a/third_party/openthread/ot-ifx +++ b/third_party/openthread/ot-ifx @@ -1 +1 @@ -Subproject commit 2ee7240000f9bc6a876f3f4b31f02ca82dca5f43 +Subproject commit 6bf0fdbd38979435f66667ac0113d4fcee972a32