Skip to content

Commit 062b770

Browse files
s07641069serhiiSalamakhamishadeshrestyled-commitsinterfer
authored
[Telink] Introduce power management (#24787)
* [Telink] Introduce power management (#72) * [Telink] Moved LightingManager into PWMDevice * [Telink] Enabled PM for Light Switch example * [Telink] Added OpenThread power management support * [Telink] Configured project for the lowest power consumption * [Telink] Restyle * [Telink] Adapt system status LED and buttons with Power Mode usage * [Telink] Fixed factory reset in pool mode of button manager * [Telink] Updated Factory Reset trigger * Restyled by clang-format * [Telink] Adding lighting color feature (#59) * [Telink] Added lighting color feature * [Telink] Added tlsr9518adk80d.overlay to lighting-app * [Telink] Updated RGB functionality * [Telink] Updated RGB PWM pins Co-authored-by: Alex Tsitsiura <s07641069@gmail.com> * Restyled by whitespace * Restyled by clang-format * [Telink] Minor changes * [Telink] Add 'telink reboot' shell CLI command (#63) * [Telink] Restyled * [Telink] Enable CHIP SED support * [Telink] Update new configs name/location * [Telink] restyle * [Telink] Add PM to all apps * [Telink] set default configs * [Telink] Revert EOL * [Telink] Revert EOL --------- Co-authored-by: Serhii Salamakha <serhii.salamakha@gmail.com> Co-authored-by: Misha.Tkachenko <mishadesh@gmail.com> Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Dmytro Huz <75682372+interfer@users.noreply.github.com> * [Telink] Copyright update * [Telink] Fix builds after merge of master * [Telink] Review fixes * [Telink] Custom RF power values example * [Telink] Remove Level Control cluster from binding * [Telink] Add CONFIG_MATTER_LOG_LEVEL --------- Co-authored-by: Serhii Salamakha <serhii.salamakha@gmail.com> Co-authored-by: Misha.Tkachenko <mishadesh@gmail.com> Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Dmytro Huz <75682372+interfer@users.noreply.github.com>
1 parent 4982399 commit 062b770

File tree

36 files changed

+666
-290
lines changed

36 files changed

+666
-290
lines changed

config/telink/chip-module/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2022 Project CHIP Authors
2+
# Copyright (c) 2022-2023 Project CHIP Authors
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -191,6 +191,7 @@ chip_gn_arg_string("zephyr_ar" ${CMAKE_AR})
191191
chip_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER})
192192
chip_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER})
193193
chip_gn_arg_bool ("is_debug" CONFIG_DEBUG)
194+
chip_gn_arg_bool ("chip_logging" CONFIG_LOG)
194195
chip_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD)
195196
chip_gn_arg_bool ("chip_openthread_ftd" CONFIG_OPENTHREAD_FTD)
196197
chip_gn_arg_bool ("chip_config_network_layer_ble" CONFIG_BT)
@@ -199,6 +200,10 @@ chip_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQU
199200
chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TESTS)
200201
chip_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS)
201202
chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL)
203+
chip_gn_arg_bool ("chip_error_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 1)
204+
chip_gn_arg_bool ("chip_progress_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 3)
205+
chip_gn_arg_bool ("chip_detail_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 4)
206+
chip_gn_arg_bool ("chip_automation_logging" FALSE)
202207

203208
if (CONFIG_CHIP_FACTORY_DATA)
204209
chip_gn_arg_bool ("chip_use_transitional_commissionable_data_provider" "false")

config/telink/chip-module/Kconfig

+13
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,16 @@ config CHIP_LOG_SIZE_OPTIMIZATION
149149
information that is too detailed to be used in most cases. You can find
150150
full configuration enabled by this option in the
151151
platform/telink/CHIPPlatformConfig.h file.
152+
153+
config CHIP_BUTTON_MANAGER_IRQ_MODE
154+
bool "Use GPIO in an IRQ mode instead of polling the GPIO"
155+
default n
156+
help
157+
Use GPIO in an IRQ mode to avoid button polling loop and extend the battery lifetime by waking up by GPIO event.
158+
GPIO events are working only with GPIO IRQ. This option changes button matrix configuration.
159+
160+
config CHIP_ENABLE_APPLICATION_STATUS_LED
161+
bool "Enable application status LED"
162+
default y
163+
help
164+
Enable application status LED.

examples/all-clusters-app/telink/include/AppTask.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (c) 2022 Project CHIP Authors
3+
* Copyright (c) 2022-2023 Project CHIP Authors
44
* All rights reserved.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,13 +19,19 @@
1919
#pragma once
2020

2121
#include "AppEvent.h"
22+
#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
2223
#include "LEDWidget.h"
24+
#endif
2325
#include "PWMDevice.h"
2426

2527
#include <zephyr/drivers/gpio.h>
2628

2729
#include <platform/CHIPDeviceLayer.h>
2830

31+
#if CONFIG_CHIP_FACTORY_DATA
32+
#include <platform/telink/FactoryDataProvider.h>
33+
#endif
34+
2935
#include <cstdint>
3036

3137
struct k_timer;
@@ -41,14 +47,18 @@ class AppTask
4147

4248
private:
4349
friend AppTask & GetAppTask(void);
50+
4451
CHIP_ERROR Init(void);
4552

4653
static void ActionIdentifyStateUpdateHandler(k_timer * timer);
4754

4855
void DispatchEvent(AppEvent * event);
4956

50-
static void UpdateStatusLED(void);
57+
#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
58+
static void UpdateLedStateEventHandler(AppEvent * aEvent);
5159
static void LEDStateUpdateHandler(LEDWidget * ledWidget);
60+
static void UpdateStatusLED();
61+
#endif
5262
static void FactoryResetButtonEventHandler(void);
5363
static void StartThreadButtonEventHandler(void);
5464
static void StartBleAdvButtonEventHandler(void);
@@ -61,7 +71,6 @@ class AppTask
6171
static void FactoryResetHandler(AppEvent * aEvent);
6272
static void StartThreadHandler(AppEvent * aEvent);
6373
static void StartBleAdvHandler(AppEvent * aEvent);
64-
static void UpdateLedStateEventHandler(AppEvent * aEvent);
6574
static void UpdateIdentifyStateEventHandler(AppEvent * aEvent);
6675

6776
static void InitButtons(void);
@@ -70,6 +79,10 @@ class AppTask
7079

7180
static AppTask sAppTask;
7281
PWMDevice mPwmIdentifyLed;
82+
83+
#if CONFIG_CHIP_FACTORY_DATA
84+
chip::DeviceLayer::FactoryDataProvider<chip::DeviceLayer::ExternalFlashFactoryData> mFactoryDataProvider;
85+
#endif
7386
};
7487

7588
inline AppTask & GetAppTask(void)

examples/all-clusters-app/telink/prj.conf

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2022 Project CHIP Authors
2+
# Copyright (c) 2022-2023 Project CHIP Authors
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -27,6 +27,9 @@ CONFIG_PWM=y
2727
# OpenThread configs
2828
CONFIG_OPENTHREAD_MTD=y
2929
CONFIG_OPENTHREAD_FTD=n
30+
CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT=n
31+
CONFIG_CHIP_SED_IDLE_INTERVAL=200
32+
CONFIG_CHIP_THREAD_SSED=n
3033

3134
# Default OpenThread network settings
3235
CONFIG_OPENTHREAD_PANID=4660
@@ -53,4 +56,19 @@ CONFIG_CHIP_DEVICE_SOFTWARE_VERSION_STRING="2022"
5356
CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART=y
5457

5558
# CHIP shell
56-
CONFIG_CHIP_LIB_SHELL=n
59+
CONFIG_CHIP_LIB_SHELL=n
60+
61+
# Disable factory data support.
62+
CONFIG_CHIP_FACTORY_DATA=n
63+
CONFIG_CHIP_FACTORY_DATA_BUILD=n
64+
CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n
65+
66+
# Enable Button IRQ mode. The poling mode is used by default.
67+
CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=n
68+
69+
# Disable Status LED.
70+
CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED=y
71+
72+
# Enable Power Management
73+
CONFIG_PM=n
74+
CONFIG_PM_DEVICE=n

examples/all-clusters-app/telink/src/AppTask.cpp

+71-38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (c) 2022 Project CHIP Authors
3+
* Copyright (c) 2022-2023 Project CHIP Authors
44
* All rights reserved.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,16 +22,14 @@
2222
#include "AppEvent.h"
2323
#include "ButtonManager.h"
2424
#include "binding-handler.h"
25-
#include <app/server/OnboardingCodesUtil.h>
26-
#include <app/server/Server.h>
27-
28-
#include <DeviceInfoProviderImpl.h>
2925

3026
#include "ThreadUtil.h"
3127

28+
#include <DeviceInfoProviderImpl.h>
3229
#include <app/clusters/identify-server/identify-server.h>
30+
#include <app/server/OnboardingCodesUtil.h>
31+
#include <app/server/Server.h>
3332
#include <app/util/attribute-storage.h>
34-
3533
#include <credentials/DeviceAttestationCredsProvider.h>
3634
#include <credentials/examples/DeviceAttestationCredsExample.h>
3735

@@ -67,7 +65,8 @@ using namespace ::chip::Credentials;
6765
using namespace ::chip::DeviceLayer;
6866

6967
namespace {
70-
constexpr int kFactoryResetTriggerTimeout = 2000;
68+
constexpr int kFactoryResetCalcTimeout = 3000;
69+
constexpr int kFactoryResetTriggerCntr = 3;
7170
constexpr int kAppEventQueueSize = 10;
7271
constexpr uint8_t kButtonPushEvent = 1;
7372
constexpr uint8_t kButtonReleaseEvent = 0;
@@ -84,20 +83,28 @@ constexpr uint32_t kIdentifyBreatheRateMs = 1000;
8483

8584
const struct pwm_dt_spec sPwmIdentifySpecGreenLed = LIGHTING_PWM_SPEC_IDENTIFY_GREEN;
8685

86+
#if CONFIG_CHIP_FACTORY_DATA
87+
// NOTE! This key is for test/certification only and should not be available in production devices!
88+
uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
89+
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
90+
#endif
91+
8792
K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent));
8893
k_timer sFactoryResetTimer;
94+
uint8_t sFactoryResetCntr = 0;
8995

96+
#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
9097
LEDWidget sStatusLED;
98+
#endif
9199

92100
Button sFactoryResetButton;
93101
Button sThreadStartButton;
94102
Button sBleAdvStartButton;
95103

96-
bool sIsThreadProvisioned = false;
97-
bool sIsThreadEnabled = false;
98-
bool sIsThreadAttached = false;
99-
bool sHaveBLEConnections = false;
100-
bool sIsFactoryResetTimerActive = false;
104+
bool sIsThreadProvisioned = false;
105+
bool sIsThreadEnabled = false;
106+
bool sIsThreadAttached = false;
107+
bool sHaveBLEConnections = false;
101108

102109
chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
103110

@@ -135,24 +142,21 @@ CHIP_ERROR AppTask::Init(void)
135142
{
136143
LOG_INF("SW Version: %u, %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION, CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);
137144

138-
// Initialize status LED
145+
// Initialize LEDs
146+
#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
139147
LEDWidget::InitGpio(LEDS_PORT);
140148
LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler);
141149
sStatusLED.Init(SYSTEM_STATE_LED);
142150

143151
UpdateStatusLED();
152+
#endif
144153

145154
InitButtons();
146155

147156
// Initialize function button timer
148157
k_timer_init(&sFactoryResetTimer, &AppTask::FactoryResetTimerTimeoutCallback, nullptr);
149158
k_timer_user_data_set(&sFactoryResetTimer, this);
150159

151-
// Init ZCL Data Model and start server
152-
static chip::CommonCaseDeviceServerInitParams initParams;
153-
(void) initParams.InitializeStaticResourcesBeforeServerInit();
154-
chip::Server::GetInstance().Init(initParams);
155-
156160
// Initialize PWM Identify led
157161
CHIP_ERROR err = sAppTask.mPwmIdentifyLed.Init(&sPwmIdentifySpecGreenLed, kDefaultMinLevel, kDefaultMaxLevel, kDefaultMaxLevel);
158162
if (err != CHIP_NO_ERROR)
@@ -163,10 +167,28 @@ CHIP_ERROR AppTask::Init(void)
163167

164168
sAppTask.mPwmIdentifyLed.SetCallbacks(nullptr, nullptr, ActionIdentifyStateUpdateHandler);
165169

166-
// Initialize device attestation config
170+
// Initialize CHIP server
171+
#if CONFIG_CHIP_FACTORY_DATA
172+
ReturnErrorOnFailure(mFactoryDataProvider.Init());
173+
SetDeviceInstanceInfoProvider(&mFactoryDataProvider);
174+
SetDeviceAttestationCredentialsProvider(&mFactoryDataProvider);
175+
SetCommissionableDataProvider(&mFactoryDataProvider);
176+
// Read EnableKey from the factory data.
177+
MutableByteSpan enableKey(sTestEventTriggerEnableKey);
178+
err = mFactoryDataProvider.GetEnableKey(enableKey);
179+
if (err != CHIP_NO_ERROR)
180+
{
181+
LOG_ERR("mFactoryDataProvider.GetEnableKey() failed. Could not delegate a test event trigger");
182+
memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey));
183+
}
184+
#else
167185
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());
186+
#endif
168187

169-
gExampleDeviceInfoProvider.SetStorageDelegate(&chip::Server::GetInstance().GetPersistentStorage());
188+
static CommonCaseDeviceServerInitParams initParams;
189+
(void) initParams.InitializeStaticResourcesBeforeServerInit();
190+
ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams));
191+
gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
170192
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
171193

172194
#if CONFIG_CHIP_OTA_REQUESTOR
@@ -226,13 +248,8 @@ CHIP_ERROR AppTask::StartApp(void)
226248

227249
while (true)
228250
{
229-
int ret = k_msgq_get(&sAppEventQueue, &event, K_MSEC(10));
230-
231-
while (!ret)
232-
{
233-
DispatchEvent(&event);
234-
ret = k_msgq_get(&sAppEventQueue, &event, K_NO_WAIT);
235-
}
251+
k_msgq_get(&sAppEventQueue, &event, K_FOREVER);
252+
DispatchEvent(&event);
236253
}
237254
}
238255

@@ -298,15 +315,20 @@ void AppTask::FactoryResetButtonEventHandler(void)
298315

299316
void AppTask::FactoryResetHandler(AppEvent * aEvent)
300317
{
301-
if (!sIsFactoryResetTimerActive)
318+
if (sFactoryResetCntr == 0)
302319
{
303-
k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetTriggerTimeout), K_NO_WAIT);
304-
sIsFactoryResetTimerActive = true;
320+
k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetCalcTimeout), K_NO_WAIT);
305321
}
306-
else
322+
323+
sFactoryResetCntr++;
324+
LOG_INF("Factory Reset Trigger Counter: %d/%d", sFactoryResetCntr, kFactoryResetTriggerCntr);
325+
326+
if (sFactoryResetCntr == kFactoryResetTriggerCntr)
307327
{
308328
k_timer_stop(&sFactoryResetTimer);
309-
sIsFactoryResetTimerActive = false;
329+
sFactoryResetCntr = 0;
330+
331+
chip::Server::GetInstance().ScheduleFactoryReset();
310332
}
311333
}
312334

@@ -369,6 +391,7 @@ void AppTask::StartBleAdvHandler(AppEvent * aEvent)
369391
}
370392
}
371393

394+
#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
372395
void AppTask::UpdateLedStateEventHandler(AppEvent * aEvent)
373396
{
374397
if (aEvent->Type == AppEvent::kEventType_UpdateLedState)
@@ -404,20 +427,25 @@ void AppTask::UpdateStatusLED(void)
404427
sStatusLED.Blink(50, 950);
405428
}
406429
}
430+
#endif
407431

408432
void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */)
409433
{
410434
switch (event->Type)
411435
{
412436
case DeviceEventType::kCHIPoBLEAdvertisingChange:
413437
sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0;
438+
#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
414439
UpdateStatusLED();
440+
#endif
415441
break;
416442
case DeviceEventType::kThreadStateChange:
417443
sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned();
418444
sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled();
419445
sIsThreadAttached = ConnectivityMgr().IsThreadAttached();
446+
#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
420447
UpdateStatusLED();
448+
#endif
421449
break;
422450
case DeviceEventType::kThreadConnectivityChange:
423451
#if CONFIG_CHIP_OTA_REQUESTOR
@@ -485,16 +513,21 @@ void AppTask::FactoryResetTimerEventHandler(AppEvent * aEvent)
485513
return;
486514
}
487515

488-
sIsFactoryResetTimerActive = false;
489-
LOG_INF("FactoryResetHandler");
490-
chip::Server::GetInstance().ScheduleFactoryReset();
516+
sFactoryResetCntr = 0;
517+
LOG_INF("Factory Reset Trigger Counter is cleared");
491518
}
492519

493520
void AppTask::InitButtons(void)
494521
{
495-
sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, true, FactoryResetButtonEventHandler);
496-
sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, false, StartThreadButtonEventHandler);
497-
sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, false, StartBleAdvButtonEventHandler);
522+
#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE
523+
sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_1, FactoryResetButtonEventHandler);
524+
sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, StartThreadButtonEventHandler);
525+
sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, StartBleAdvButtonEventHandler);
526+
#else
527+
sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler);
528+
sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, StartThreadButtonEventHandler);
529+
sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler);
530+
#endif
498531

499532
ButtonManagerInst().AddButton(sFactoryResetButton);
500533
ButtonManagerInst().AddButton(sThreadStartButton);

0 commit comments

Comments
 (0)