Skip to content

Commit

Permalink
riscv: telink: add dual mode for b92 and buteo and CI.
Browse files Browse the repository at this point in the history
- clean the boot flag in init part .
- add the kDnsstimer to protect from init to dnss .
- add special proc for ikea.
- add the user-para proc in matter.
- add 2m flash for buteo ,and 4m for b92.
- update CI for b92 and tl3218x .
- fix the switch logic part .

Signed-off-by: Haiwen Xia <[email protected]>
  • Loading branch information
haiwentelink committed Dec 30, 2024
1 parent e7b9e81 commit 4aa078c
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 22 deletions.
5 changes: 5 additions & 0 deletions config/telink/chip-module/Kconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ config COMPRESS_LZMA
default y if SOC_RISCV_TELINK_TL321X
default n

config CUSTOMER_MODE
bool "Disable application code and add demo code"
default y if SOC_RISCV_TELINK_TL321X || SOC_RISCV_TELINK_B92
default n

config DUAL_MODE_SWTICH
bool "Control Dual-Mode Switching"
default y if SOC_RISCV_TELINK_TL321X || SOC_RISCV_TELINK_B92
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/telink/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class AppTask : public AppTaskCommon
bool IsTurnedOn() const;
void SetInitiateAction(Fixture_Action aAction, int32_t aActor, uint8_t * value);
void UpdateClusterState(void);
void Init_cluster_info(void);
void Set_cluster_info(void);

private:
friend AppTask & GetAppTask(void);
Expand Down
89 changes: 81 additions & 8 deletions examples/lighting-app/telink/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include "ColorFormat.h"
#include "LEDManager.h"
#include "PWMManager.h"
#include <zephyr/device.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/kernel.h>

#include <app-common/zap-generated/attributes/Accessors.h>

Expand Down Expand Up @@ -55,22 +58,92 @@ void AppTask::PowerOnFactoryReset(void)
}
#endif /* CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET */

#if CONFIG_CUSTOMER_MODE
void i2c_demo_proc()
{
const uint8_t tx_buf[23] = { 0xc0, 0x63, 0x3f, 0x63, 0x63, 0x63, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00,
0x3f, 0x3f, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x2b, 0x06, 0xbe };
/* add the i2c module here */
printk("i2c demo start \n.");
uint32_t i2c_cfg = I2C_SPEED_SET(I2C_SPEED_FAST) | I2C_MODE_CONTROLLER;
/* get i2c device */
int rc;
const struct i2c_dt_spec i2c = I2C_DT_SPEC_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(ledcontrol_i2c));
if (!device_is_ready(i2c.bus))
{
printf("Device %s is not ready\n", i2c.bus->name);
return;
}
rc = i2c_configure(i2c.bus, i2c_cfg);
if (rc != 0)
{
printf("Failed to configure i2c device\n");
return;
}
i2c_write(i2c.bus, tx_buf + 1, sizeof(tx_buf) - 1, tx_buf[0]);
printk("i2c demo stop ,finish transfer\n");
}

void AppTask::Init_cluster_info(void)
{
printk("%%%%%%Set_cluster_info!!!!%%%%%%\n");
light_para_t * p_para = &light_para;
Protocols::InteractionModel::Status status;
printk("%%%%%%Set_cluster_info:p_para->onoff:%d!!!!%%%%%%\n", p_para->onoff);
status = Clusters::OnOff::Attributes::OnOff::Set(1, p_para->onoff);
// Set brightness value
printk("%%%%%%Set_cluster_info:p_para->level:%d!!!!%%%%%%\n", p_para->level);
status = Clusters::LevelControl::Attributes::CurrentLevel::Set(kExampleEndpointId, p_para->level);
// Set ColorMode value
/*
printk("%%%%%%Set_cluster_info:p_para->color_mode:%d!!!!%%%%%%\n",p_para->color_mode);
status = Clusters::ColorControl::Attributes::ColorMode::Set(1, p_para->color_mode);
*/
// Set ColorTemperatureMireds value
status = Clusters::ColorControl::Attributes::ColorTemperatureMireds::Set(1, p_para->color_temp_mireds);

// Set CurrentX value
status = Clusters::ColorControl::Attributes::CurrentX::Set(1, p_para->currentx);

// Set CurrentY value
status = Clusters::ColorControl::Attributes::CurrentY::Set(1, p_para->currenty);

// Set EnhancedCurrentHue value
status = Clusters::ColorControl::Attributes::EnhancedCurrentHue::Set(1, p_para->enhanced_current_hue);

// Set CurrentHue value
status = Clusters::ColorControl::Attributes::CurrentHue::Set(1, p_para->cur_hue);

// Set CurrentSaturation value
status = Clusters::ColorControl::Attributes::CurrentSaturation::Set(1, p_para->cur_saturation);

// Set OnOffTransitionTime value
status = Clusters::LevelControl::Attributes::OnOffTransitionTime::Set(1, p_para->onoff_transition);
}
#endif

CHIP_ERROR AppTask::Init(void)
{
SetExampleButtonCallbacks(LightingActionEventHandler);
InitCommonParts();

#if CONFIG_DUAL_MODE_SWTICH
if (sBoot_zb)
#if CONFIG_CUSTOMER_MODE
if (user_para.val == USER_ZB_SW_VAL)
{
/* Switching from TouchLink (Zigbee) to Matter. Restore previous states. */
sfixture_on = user_para.onoff;
sBrightness = user_para.lightness;
sAppTask.UpdateClusterState();
printk("Matter: Restored Zigbee On/Off and brightness states.\n");
}
#endif
else if (user_para.val == USER_MATTER_PAIR_VAL)
{
/* start from matter , add cluster info demo here*/
Init_cluster_info();
}
else
{
/* start from matter , but without zigbee fw*/
}
/* if need , can call i2c_demo_proc here to see light works or not*/

#else
Protocols::InteractionModel::Status status;

app::DataModel::Nullable<uint8_t> brightness;
Expand All @@ -91,7 +164,7 @@ CHIP_ERROR AppTask::Init(void)
// Set actual state to stored before reboot
SetInitiateAction(storedValue ? ON_ACTION : OFF_ACTION, static_cast<int32_t>(AppEvent::kEventType_DeviceAction), nullptr);
}

#endif
return CHIP_NO_ERROR;
}

Expand Down
9 changes: 9 additions & 0 deletions examples/lighting-app/telink/src/ZclCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ using namespace chip::app::Clusters;
void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
uint8_t * value)
{
#if CONFIG_CUTOMER_MODE
/* user mode , add the customer code here for cb*/
ClusterId clusterId = attributePath.mClusterId;
AttributeId attributeId = attributePath.mAttributeId;
ChipLogProgress(Zcl, "========MatterPostAttributeChangeCallback:clusterId=0x%x,AttributeId=0x%x,value=0x%x", clusterId,
attributeId, *value);

#else
static HsvColor_t hsv;
static XyColor_t xy;
ClusterId clusterId = attributePath.mClusterId;
Expand Down Expand Up @@ -116,4 +124,5 @@ void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath &
ChipLogDetail(Zcl, "Ignore ColorControl attribute (%u) that is not currently processed!", attributeId);
}
}
#endif
}
35 changes: 32 additions & 3 deletions examples/platform/telink/common/include/AppTaskCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,25 @@
#include <zephyr/storage/flash_map.h>
#include <zephyr/sys/reboot.h>

#if CONFIG_SOC_RISCV_TELINK_B92

#define ZB_NVS_PARTITION zigbee_partition
#define ZB_NVS_PARTITION_DEVICE FIXED_PARTITION_DEVICE(ZB_NVS_PARTITION)
#define ZB_NVS_START_ADR FIXED_PARTITION_OFFSET(ZB_NVS_PARTITION)
#define ZB_NVS_SEC_SIZE FIXED_PARTITION_SIZE(ZB_NVS_PARTITION)

#elif CONFIG_SOC_RISCV_TELINK_TL321X
#define ZB_NVS_PARTITION slot1_partition
#define ZB_NVS_PARTITION_DEVICE FIXED_PARTITION_DEVICE(ZB_NVS_PARTITION)
#define ZB_NVS_START_ADR FIXED_PARTITION_OFFSET(ZB_NVS_PARTITION)
/* zb para locate in the slot1 , and it will cost 104k size in slot1 */
#define ZB_NVS_SEC_SIZE (104 * 1024)
#endif

#define USER_INIT_VAL 0xff
#define USER_ZB_SW_VAL 0xaa
#define USER_MATTER_PAIR_VAL 0x55
#define USER_MATTER_BACK_ZB 0xa0 // only commisiion fail will back to zb
#define USER_PARA_MAC_OFFSET 0x100
#define USER_PARTITION user_para_partition
#define USER_PARTITION_DEVICE FIXED_PARTITION_DEVICE(USER_PARTITION)
Expand All @@ -55,11 +71,24 @@
typedef struct
{
uint8_t val;
uint8_t rfu;
uint8_t onoff;
uint8_t lightness;
uint8_t on_net;
} user_para_t;

typedef struct
{
uint8_t onoff;
uint8_t level;
uint16_t color_temp_mireds;
uint16_t currentx;
uint16_t currenty;
uint16_t enhanced_current_hue;
uint16_t onoff_transition;
uint8_t cur_hue;
uint8_t cur_saturation;
uint8_t color_mode;
} light_para_t;

extern light_para_t light_para;
extern user_para_t user_para;
extern uint8_t para_lightness;
extern uint8_t sBoot_zb;
Expand Down
58 changes: 49 additions & 9 deletions examples/platform/telink/common/src/AppTaskCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
#include <app/clusters/ota-requestor/OTARequestorInterface.h>
#endif

#include <app-common/zap-generated/attributes/Accessors.h>
#include <zephyr/fs/nvs.h>
#include <zephyr/settings/settings.h>

#if CONFIG_DUAL_MODE_SWTICH
uint8_t sBoot_zb = 0;
user_para_t user_para;
uint8_t sBoot_zb = 0;
uint8_t para_lightness = 0;
user_para_t user_para;
light_para_t light_para;
#endif

using namespace chip::app;
Expand All @@ -75,6 +77,7 @@ constexpr uint32_t kIdentifyBreatheRateMs = 1000;

#if CONFIG_DUAL_MODE_SWTICH
const struct device * flash_para_dev = USER_PARTITION_DEVICE;
const struct device * zb_para_dev = ZB_NVS_PARTITION_DEVICE;
constexpr int kDnssTimeout = 60000;
static k_timer sDnssTimer;
#endif
Expand Down Expand Up @@ -143,6 +146,8 @@ void FactoryResetExtHandler(void)
{
// Erase the user parameters partition to reset mode settings
flash_erase(flash_para_dev, USER_PARTITION_OFFSET, USER_PARTITION_SIZE);
// Need to erase zb nvs part in factory mode
flash_erase(zb_para_dev, ZB_NVS_START_ADR, ZB_NVS_SEC_SIZE);
}
#endif

Expand Down Expand Up @@ -232,6 +237,14 @@ void AppTaskCommon::PowerOnFactoryReset(void)
#endif /* CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET */

#if CONFIG_DUAL_MODE_SWTICH

void SwitchBackToZigbee()
{
uint8_t switch_flag = USER_MATTER_BACK_ZB;
flash_write(flash_para_dev, USER_PARTITION_OFFSET, &switch_flag, 1);
sys_reboot(0);
}

void AppTaskCommon::DnssTimerTimeoutCallback(k_timer * timer)
{
if (!timer)
Expand All @@ -242,8 +255,11 @@ void AppTaskCommon::DnssTimerTimeoutCallback(k_timer * timer)
* If Dnss initialization takes longer than 60 seconds,
* the device will reboot and revert to Zigbee mode.
*/
printk("Matter: DnssTimer expired. Rebooting...\n");
sys_reboot(0);
if (sBoot_zb)
{
printk("Matter: DnssTimer expired. Rebooting...\n");
SwitchBackToZigbee();
}
}
#endif

Expand All @@ -257,21 +273,24 @@ CHIP_ERROR AppTaskCommon::StartApp(void)
* a given timeframe.
*/
flash_read(flash_para_dev, USER_PARTITION_OFFSET, &user_para, sizeof(user_para));
/* Boot from Zigbee , need to clean the user parameters sector first and set a flag */
if (user_para.val == USER_ZB_SW_VAL)
{
sBoot_zb = 1;
/* if switch from zb , need to get all the cluster info from zb */
flash_read(flash_para_dev, USER_PARTITION_OFFSET + sizeof(user_para), &light_para, sizeof(light_para));
/* Ensure brightness is at least 2 to avoid display issues on HomePod Mini */
if (user_para.lightness < 2)
if (light_para.level < 2)
{
user_para.lightness = 2;
light_para.level = 2;
}
/*
* Pass the brightness value to the initialization code
* to avoid gaps in PWM pool initialization.
*/
if (user_para.onoff)
if (light_para.onoff)
{
para_lightness = user_para.lightness;
para_lightness = light_para.level;
}
k_timer_init(&sDnssTimer, &AppTask::DnssTimerTimeoutCallback, nullptr);
k_timer_start(&sDnssTimer, K_MSEC(kDnssTimeout), K_NO_WAIT);
Expand Down Expand Up @@ -333,13 +352,16 @@ CHIP_ERROR AppTaskCommon::InitCommonParts(void)
CHIP_ERROR err;

PrintFirmwareInfo();
#if CONFIG_CUSTOMER_MODE

#else
InitLeds();
UpdateStatusLED();

InitPwms();

InitButtons();
#endif

// Initialize function button timer
k_timer_init(&sFactoryResetTimer, &AppTask::FactoryResetTimerTimeoutCallback, nullptr);
Expand Down Expand Up @@ -823,6 +845,24 @@ void AppTaskCommon::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /*
#if CONFIG_DUAL_MODE_SWTICH
case DeviceEventType::kCommissioningComplete: {
uint8_t val = USER_MATTER_PAIR_VAL;
#if CONFIG_CUSTOMER_MODE
/* need to add here to update the cluster information , only in the zb switch and touchlink is paired*/
if (user_para.val == USER_ZB_SW_VAL && user_para.on_net)
{
Protocols::InteractionModel::Status status;
/* Switch from the touch link, need to restore previous values */
status = Clusters::OnOff::Attributes::OnOff::Set(kExampleEndpointId, light_para.onoff);
if (status != Protocols::InteractionModel::Status::Success)
{
LOG_ERR("Update OnOff fail: %x", to_underlying(status));
}
status = Clusters::LevelControl::Attributes::CurrentLevel::Set(kExampleEndpointId, light_para.level);
{
LOG_ERR("Update brightness fail: %x", to_underlying(status));
}
}
#endif
sBoot_zb = 0;
flash_erase(flash_para_dev, USER_PARTITION_OFFSET, USER_PARTITION_SIZE);
flash_write(flash_para_dev, USER_PARTITION_OFFSET, &val, 1);
printk("Commissioning complete; Matter commissioned flag set.\n");
Expand All @@ -833,7 +873,7 @@ void AppTaskCommon::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /*
if (sBoot_zb)
{
printk("FailSafeTimer expired; Matter commissioning failed. Rebooting to Zigbee mode...\n");
sys_reboot(0);
SwitchBackToZigbee();
}
else
{
Expand Down
Loading

0 comments on commit 4aa078c

Please sign in to comment.