diff --git a/config/telink/chip-module/CMakeLists.txt b/config/telink/chip-module/CMakeLists.txt index 545074b0b928b3..8e798c058525c6 100644 --- a/config/telink/chip-module/CMakeLists.txt +++ b/config/telink/chip-module/CMakeLists.txt @@ -155,6 +155,16 @@ else() unset(USB_BOOT_DTC_OVERLAY_FILE) endif() +if(${TLNK_MARS_BOARD} MATCHES y) + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BASE_BOARD}_mars_boot.overlay") + set(MARS_BOOT_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BASE_BOARD}_mars_boot.overlay") + else() + unset(MARS_BOOT_DTC_OVERLAY_FILE) + endif() +else() + unset(MARS_CONF_OVERLAY_FILE) +endif() + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BASE_BOARD}.overlay") set(GLOBAL_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BASE_BOARD}.overlay") else() @@ -186,7 +196,7 @@ if (CONFIG_BOOTLOADER_MCUBOOT) add_custom_target(build_mcuboot ALL COMMAND west build -b ${BASE_BOARD} -d build_mcuboot ${ZEPHYR_BASE}/../bootloader/mcuboot/boot/zephyr - -- -DOVERLAY_CONFIG=${GLOBAL_BOOTLOADER_CONF_OVERLAY_FILE} -DDTC_OVERLAY_FILE="${GLOBAL_DTC_OVERLAY_FILE};${FLASH_DTC_OVERLAY_FILE};${USB_BOOT_DTC_OVERLAY_FILE}" + -- -DOVERLAY_CONFIG=${GLOBAL_BOOTLOADER_CONF_OVERLAY_FILE} -DDTC_OVERLAY_FILE="${GLOBAL_DTC_OVERLAY_FILE};${FLASH_DTC_OVERLAY_FILE};${USB_BOOT_DTC_OVERLAY_FILE};${MARS_BOOT_DTC_OVERLAY_FILE}" COMMAND cp ${PROJECT_BINARY_DIR}/../modules/chip-module/build_mcuboot/zephyr/zephyr.bin ${PROJECT_BINARY_DIR}/zephyr.mcuboot.bin ) diff --git a/config/telink/chip-module/Kconfig b/config/telink/chip-module/Kconfig index b1bd07660937bd..93ebc31a0f426e 100644 --- a/config/telink/chip-module/Kconfig +++ b/config/telink/chip-module/Kconfig @@ -202,3 +202,8 @@ config CHIP_ENABLE_POWER_ON_FACTORY_RESET Enable power on factory reset sequence. If device power triggered off during first 5 seconds after power on and this sequence repeated 5 times - factory reset will be involved. + +config CHIP_USE_MARS_SENSOR + bool "Use Mars board sensor" + depends on SOC_SERIES_RISCV_TELINK_B9X && (BOARD_TLSR9518ADK80D || BOARD_TLSR9518ADK80D_RETENTION) + default n diff --git a/examples/air-quality-sensor-app/telink/CMakeLists.txt b/examples/air-quality-sensor-app/telink/CMakeLists.txt index fd5a5b14256426..908bea9eb73a98 100644 --- a/examples/air-quality-sensor-app/telink/CMakeLists.txt +++ b/examples/air-quality-sensor-app/telink/CMakeLists.txt @@ -19,6 +19,23 @@ get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/third_party/connect get_filename_component(TELINK_COMMON ${CHIP_ROOT}/examples/platform/telink REALPATH) get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH) +set(ignoreMe "${TLNK_MARS_BOARD}") + +if(${TLNK_MARS_BOARD} MATCHES y) + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.overlay") + set(MARS_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.overlay") + else() + unset(MARS_DTC_OVERLAY_FILE) + endif() + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.conf") + set(MARS_CONF_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.conf") + else() + unset(MARS_CONF_OVERLAY_FILE) + endif() +else() + unset(MARS_CONF_OVERLAY_FILE) +endif() + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") set(LOCAL_DTC_OVERLAY_FILE "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") else() @@ -41,14 +58,14 @@ endif() if(DTC_OVERLAY_FILE) set(DTC_OVERLAY_FILE - "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" + "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" CACHE STRING "" FORCE ) else() - set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) + set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE prj.conf) +set(CONF_FILE ${MARS_CONF_OVERLAY_FILE} prj.conf) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -78,10 +95,12 @@ target_sources(app PRIVATE src/ZclCallbacks.cpp ${TELINK_COMMON}/common/src/mainCommon.cpp ${TELINK_COMMON}/common/src/AppTaskCommon.cpp + ${TELINK_COMMON}/common/src/SensorManagerCommon.cpp ${TELINK_COMMON}/util/src/LEDWidget.cpp ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp ${TELINK_COMMON}/util/src/PWMDevice.cpp + ${TELINK_COMMON}/util/src/WS2812Device.cpp ${CHIP_ROOT}/examples/air-quality-sensor-app/air-quality-sensor-common/src/air-quality-sensor-manager.cpp) chip_configure_data_model(app diff --git a/examples/air-quality-sensor-app/telink/include/AppTask.h b/examples/air-quality-sensor-app/telink/include/AppTask.h index a1b55a9b83108f..659de081dbf01f 100644 --- a/examples/air-quality-sensor-app/telink/include/AppTask.h +++ b/examples/air-quality-sensor-app/telink/include/AppTask.h @@ -31,7 +31,8 @@ class AppTask : public AppTaskCommon CHIP_ERROR Init(void); - static void AirQualityActionEventHandler(AppEvent * aEvent); + static void AirQualitySensorUpdateTimerTimeoutCallback(k_timer * timer); + static void AirQualitySensorUpdateTimerEventHandler(AppEvent * aEvent); static AppTask sAppTask; }; diff --git a/examples/air-quality-sensor-app/telink/src/AppTask.cpp b/examples/air-quality-sensor-app/telink/src/AppTask.cpp index 94988ea8bbe5a0..ac2f61b3c06f52 100644 --- a/examples/air-quality-sensor-app/telink/src/AppTask.cpp +++ b/examples/air-quality-sensor-app/telink/src/AppTask.cpp @@ -17,50 +17,100 @@ */ #include "AppTask.h" +#include "SensorManagerCommon.h" #include LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); +using namespace chip; using namespace ::chip::app::Clusters; using namespace ::chip::app::Clusters::AirQuality; -AppTask AppTask::sAppTask; +namespace { +k_timer sAirQualitySensorUpdateTimer; +constexpr uint16_t kAirQualitySensorUpdateTimerPeriod = 10000; // 10s timer period +} // namespace -constexpr EndpointId kAirQualityEndpoint = 1; +AppTask AppTask::sAppTask; CHIP_ERROR AppTask::Init(void) { + CHIP_ERROR err; + #if APP_USE_EXAMPLE_START_BUTTON - SetExampleButtonCallbacks(AirQualityActionEventHandler); + SetExampleButtonCallbacks(AirQualitySensorUpdateTimerEventHandler); #endif InitCommonParts(); - AirQualitySensorManager::InitInstance(kAirQualityEndpoint); + err = SensorMgr().Init(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("Init of the Sensor Manager failed"); + return err; + } + + // Initialize air quality sensor update timer + k_timer_init(&sAirQualitySensorUpdateTimer, &AppTask::AirQualitySensorUpdateTimerTimeoutCallback, nullptr); + k_timer_user_data_set(&sAirQualitySensorUpdateTimer, this); + k_timer_start(&sAirQualitySensorUpdateTimer, K_MSEC(kAirQualitySensorUpdateTimerPeriod), K_NO_WAIT); + + AirQualitySensorManager::InitInstance(kExampleEndpointId); return CHIP_NO_ERROR; } void AppTask::UpdateClusterState(void) { + CHIP_ERROR ret; AirQualitySensorManager * mInstance = AirQualitySensorManager::GetInstance(); + int16_t temperature; + uint16_t humidity; + + ret = SensorMgr().GetTempAndHumMeasurValue(&temperature, &humidity); + if (ret != CHIP_NO_ERROR) + { + LOG_ERR("Update of the Air Quality clusters failed"); + return; + } + + LOG_INF("Update Air Quality: temperature is (%d*0.01)°C, humidity is %d", temperature, humidity); // Update AirQuality value - mInstance->OnAirQualityChangeHandler(AirQualityEnum::kModerate); + mInstance->OnAirQualityChangeHandler(AirQualityEnum::kUnknown); // Update Carbon Dioxide - mInstance->OnCarbonDioxideMeasurementChangeHandler(400); + mInstance->OnCarbonDioxideMeasurementChangeHandler(0); // Update Temperature value - mInstance->OnTemperatureMeasurementChangeHandler(18); + mInstance->OnTemperatureMeasurementChangeHandler(temperature); // Update Humidity value - mInstance->OnHumidityMeasurementChangeHandler(60); + mInstance->OnHumidityMeasurementChangeHandler(humidity); +} + +void AppTask::AirQualitySensorUpdateTimerTimeoutCallback(k_timer * timer) +{ + if (!timer) + { + return; + } + + AppEvent event; + event.Type = AppEvent::kEventType_Timer; + event.Handler = AirQualitySensorUpdateTimerEventHandler; + sAppTask.PostEvent(&event); } -void AppTask::AirQualityActionEventHandler(AppEvent * aEvent) +void AppTask::AirQualitySensorUpdateTimerEventHandler(AppEvent * aEvent) { - if (aEvent->Type == AppEvent::kEventType_Button) + if ((aEvent->Type == AppEvent::kEventType_Button) || (aEvent->Type == AppEvent::kEventType_Timer)) { sAppTask.UpdateClusterState(); } + + if (aEvent->Type == AppEvent::kEventType_Timer) + { + // Start next timer to measurement the air quality sensor + k_timer_start(&sAirQualitySensorUpdateTimer, K_MSEC(kAirQualitySensorUpdateTimerPeriod), K_NO_WAIT); + } } diff --git a/examples/lighting-app/telink/CMakeLists.txt b/examples/lighting-app/telink/CMakeLists.txt index e4dd1a4adc4094..2a7e46fc3d8076 100644 --- a/examples/lighting-app/telink/CMakeLists.txt +++ b/examples/lighting-app/telink/CMakeLists.txt @@ -21,6 +21,21 @@ get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH) set(ignoreMe "${TLNK_USB_DONGLE}") +if(${TLNK_MARS_BOARD} MATCHES y) + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.overlay") + set(MARS_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.overlay") + else() + unset(MARS_DTC_OVERLAY_FILE) + endif() + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.conf") + set(MARS_CONF_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.conf") + else() + unset(MARS_CONF_OVERLAY_FILE) + endif() +else() + unset(MARS_CONF_OVERLAY_FILE) +endif() + if(${TLNK_USB_DONGLE} MATCHES y) if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_usb.overlay") set(USB_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_usb.overlay") @@ -58,14 +73,14 @@ endif() if(DTC_OVERLAY_FILE) set(DTC_OVERLAY_FILE - "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${USB_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" + "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${USB_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" CACHE STRING "" FORCE ) else() - set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${USB_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) + set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${USB_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE ${USB_CONF_OVERLAY_FILE} prj.conf) +set(CONF_FILE ${USB_CONF_OVERLAY_FILE} ${MARS_CONF_OVERLAY_FILE} prj.conf) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -99,6 +114,7 @@ target_sources(app PRIVATE ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp ${TELINK_COMMON}/util/src/PWMDevice.cpp + ${TELINK_COMMON}/util/src/WS2812Device.cpp ${TELINK_COMMON}/util/src/ColorFormat.cpp) chip_configure_data_model(app diff --git a/examples/lighting-app/telink/include/AppTask.h b/examples/lighting-app/telink/include/AppTask.h index a9d9ab3712ef94..764cb209505a82 100644 --- a/examples/lighting-app/telink/include/AppTask.h +++ b/examples/lighting-app/telink/include/AppTask.h @@ -28,7 +28,12 @@ class AppTask : public AppTaskCommon #endif /* CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET */ void SetInitiateAction(PWMDevice::Action_t aAction, int32_t aActor, uint8_t * value); void UpdateClusterState(void); - PWMDevice & GetPWMDevice(void) { return mPwmRgbBlueLed; } + +#ifdef CONFIG_WS2812_STRIP + WS2812Device & GetLightingDevice(void) { return mWS2812Device; } +#else + PWMDevice & GetLightingDevice(void) { return mPwmRgbBlueLed; } +#endif /* CONFIG_WS2812_STRIP */ private: friend AppTask & GetAppTask(void); @@ -47,11 +52,16 @@ class AppTask : public AppTaskCommon static unsigned int sPowerOnFactoryResetTimerCnt; static k_timer sPowerOnFactoryResetTimer; #endif /* CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET */ + +#ifdef CONFIG_WS2812_STRIP + WS2812Device mWS2812Device; +#else PWMDevice mPwmRgbBlueLed; #if USE_RGB_PWM PWMDevice mPwmRgbGreenLed; PWMDevice mPwmRgbRedLed; #endif +#endif /* CONFIG_WS2812_STRIP */ static AppTask sAppTask; }; diff --git a/examples/lighting-app/telink/src/AppTask.cpp b/examples/lighting-app/telink/src/AppTask.cpp index 94f0f9dd571ed1..7dbb9e1aac2102 100644 --- a/examples/lighting-app/telink/src/AppTask.cpp +++ b/examples/lighting-app/telink/src/AppTask.cpp @@ -27,17 +27,23 @@ LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); namespace { -const struct pwm_dt_spec sPwmRgbSpecBlueLed = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0)); +#ifdef CONFIG_WS2812_STRIP +const struct device * const ws2812_dev = DEVICE_DT_GET(DT_ALIAS(led_strip)); +#else +const struct pwm_dt_spec sPwmRgbSpecBlueLed = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0)); #if USE_RGB_PWM const struct pwm_dt_spec sPwmRgbSpecGreenLed = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led1)); const struct pwm_dt_spec sPwmRgbSpecRedLed = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led2)); +#endif +#endif // CONFIG_WS2812_STRIP +#if defined(CONFIG_WS2812_STRIP) || USE_RGB_PWM uint8_t sBrightness; PWMDevice::Action_t sColorAction = PWMDevice::INVALID_ACTION; XyColor_t sXY; HsvColor_t sHSV; CtColor_t sCT; -#endif +#endif // CONFIG_WS2812_STRIP || USE_RGB_PWM } // namespace AppTask AppTask::sAppTask; @@ -55,6 +61,8 @@ void AppTask::PowerOnFactoryReset(void) CHIP_ERROR AppTask::Init(void) { + CHIP_ERROR err; + // Init lighting manager uint8_t minLightLevel = kDefaultMinLevel; Clusters::LevelControl::Attributes::MinLevel::Get(kExampleEndpointId, &minLightLevel); @@ -62,7 +70,15 @@ CHIP_ERROR AppTask::Init(void) uint8_t maxLightLevel = kDefaultMaxLevel; Clusters::LevelControl::Attributes::MaxLevel::Get(kExampleEndpointId, &maxLightLevel); - CHIP_ERROR err = sAppTask.mPwmRgbBlueLed.Init(&sPwmRgbSpecBlueLed, minLightLevel, maxLightLevel, maxLightLevel); +#ifdef CONFIG_WS2812_STRIP + err = sAppTask.mWS2812Device.Init(ws2812_dev, STRIP_NUM_PIXELS(led_strip)); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("WS2812 Device Init fail"); + return err; + } +#else + err = sAppTask.mPwmRgbBlueLed.Init(&sPwmRgbSpecBlueLed, minLightLevel, maxLightLevel, maxLightLevel); if (err != CHIP_NO_ERROR) { LOG_ERR("Blue RGB PWM Device Init fail"); @@ -84,6 +100,7 @@ CHIP_ERROR AppTask::Init(void) } #endif sAppTask.mPwmRgbBlueLed.SetCallbacks(ActionInitiated, ActionCompleted, nullptr); +#endif // CONFIG_WS2812_STRIP #if APP_USE_EXAMPLE_START_BUTTON SetExampleButtonCallbacks(LightingActionEventHandler); @@ -95,6 +112,21 @@ CHIP_ERROR AppTask::Init(void) void AppTask::LightingActionEventHandler(AppEvent * aEvent) { +#ifdef CONFIG_WS2812_STRIP + if (aEvent->Type == AppEvent::kEventType_Button) + { + if (sAppTask.mWS2812Device.IsTurnedOn()) + { + sAppTask.mWS2812Device.Set(SET_RGB_TURN_OFF); + } + else + { + sAppTask.mWS2812Device.Set(SET_RGB_TURN_ON); + } + + sAppTask.UpdateClusterState(); + } +#else PWMDevice::Action_t action = PWMDevice::INVALID_ACTION; int32_t actor = 0; @@ -130,6 +162,7 @@ void AppTask::LightingActionEventHandler(AppEvent * aEvent) { LOG_INF("Action is in progress or active"); } +#endif // CONFIG_WS2812_STRIP } void AppTask::ActionInitiated(PWMDevice::Action_t aAction, int32_t aActor) @@ -171,22 +204,18 @@ void AppTask::ActionCompleted(PWMDevice::Action_t aAction, int32_t aActor) void AppTask::UpdateClusterState(void) { -#if USE_RGB_PWM - bool isTurnedOn = - sAppTask.mPwmRgbRedLed.IsTurnedOn() || sAppTask.mPwmRgbGreenLed.IsTurnedOn() || sAppTask.mPwmRgbBlueLed.IsTurnedOn(); -#else - bool isTurnedOn = sAppTask.mPwmRgbBlueLed.IsTurnedOn(); -#endif - // write the new on/off value - EmberAfStatus status = Clusters::OnOff::Attributes::OnOff::Set(kExampleEndpointId, isTurnedOn); + EmberAfStatus status; + bool isTurnedOn; + uint8_t setLevel; - if (status != EMBER_ZCL_STATUS_SUCCESS) - { - LOG_ERR("Update OnOff fail: %x", status); - } +#if defined(CONFIG_WS2812_STRIP) || USE_RGB_PWM +#ifdef CONFIG_WS2812_STRIP + isTurnedOn = sAppTask.mWS2812Device.IsTurnedOn(); +#else + isTurnedOn = + sAppTask.mPwmRgbRedLed.IsTurnedOn() || sAppTask.mPwmRgbGreenLed.IsTurnedOn() || sAppTask.mPwmRgbBlueLed.IsTurnedOn(); +#endif // CONFIG_WS2812_STRIP -#if USE_RGB_PWM - uint8_t setLevel; if (sColorAction == PWMDevice::COLOR_ACTION_XY || sColorAction == PWMDevice::COLOR_ACTION_HSV || sColorAction == PWMDevice::COLOR_ACTION_CT) { @@ -194,11 +223,28 @@ void AppTask::UpdateClusterState(void) } else { +#ifdef CONFIG_WS2812_STRIP + setLevel = sAppTask.mWS2812Device.GetBlueLevel(); + if (setLevel > kDefaultMaxLevel) + { + setLevel = kDefaultMaxLevel; + } +#else setLevel = sAppTask.mPwmRgbBlueLed.GetLevel(); +#endif // CONFIG_WS2812_STRIP } #else - uint8_t setLevel = sAppTask.mPwmRgbBlueLed.GetLevel(); -#endif + isTurnedOn = sAppTask.mPwmRgbBlueLed.IsTurnedOn(); + setLevel = sAppTask.mPwmRgbBlueLed.GetLevel(); +#endif // CONFIG_WS2812_STRIP || USE_RGB_PWM + + // write the new on/off value + status = Clusters::OnOff::Attributes::OnOff::Set(kExampleEndpointId, isTurnedOn); + if (status != EMBER_ZCL_STATUS_SUCCESS) + { + LOG_ERR("Update OnOff fail: %x", status); + } + status = Clusters::LevelControl::Attributes::CurrentLevel::Set(kExampleEndpointId, setLevel); if (status != EMBER_ZCL_STATUS_SUCCESS) { @@ -208,22 +254,33 @@ void AppTask::UpdateClusterState(void) void AppTask::SetInitiateAction(PWMDevice::Action_t aAction, int32_t aActor, uint8_t * value) { -#if USE_RGB_PWM +#if defined(CONFIG_WS2812_STRIP) || USE_RGB_PWM bool setRgbAction = false; RgbColor_t rgb; -#endif +#endif // CONFIG_WS2812_STRIP || USE_RGB_PWM if (aAction == PWMDevice::ON_ACTION || aAction == PWMDevice::OFF_ACTION) { +#ifdef CONFIG_WS2812_STRIP + if (aAction == PWMDevice::ON_ACTION) + { + sAppTask.mWS2812Device.Set(SET_RGB_TURN_ON); + } + else if (aAction == PWMDevice::OFF_ACTION) + { + sAppTask.mWS2812Device.Set(SET_RGB_TURN_OFF); + } +#else sAppTask.mPwmRgbBlueLed.InitiateAction(aAction, aActor, value); #if USE_RGB_PWM sAppTask.mPwmRgbRedLed.InitiateAction(aAction, aActor, value); sAppTask.mPwmRgbGreenLed.InitiateAction(aAction, aActor, value); #endif +#endif // CONFIG_WS2812_STRIP } else if (aAction == PWMDevice::LEVEL_ACTION) { -#if USE_RGB_PWM +#if defined(CONFIG_WS2812_STRIP) || USE_RGB_PWM // Save a new brightness for ColorControl sBrightness = *value; @@ -238,19 +295,16 @@ void AppTask::SetInitiateAction(PWMDevice::Action_t aAction, int32_t aActor, uin } else { - rgb.r = sBrightness; - rgb.g = sBrightness; - rgb.b = sBrightness; + memset(&rgb, sBrightness, sizeof(RgbColor_t)); } ChipLogProgress(Zcl, "New brightness: %u | R: %u, G: %u, B: %u", sBrightness, rgb.r, rgb.g, rgb.b); setRgbAction = true; #else sAppTask.mPwmRgbBlueLed.InitiateAction(aAction, aActor, value); -#endif +#endif // CONFIG_WS2812_STRIP || USE_RGB_PWM } - -#if USE_RGB_PWM +#if defined(CONFIG_WS2812_STRIP) || USE_RGB_PWM else if (aAction == PWMDevice::COLOR_ACTION_XY) { sXY = *reinterpret_cast(value); @@ -283,11 +337,15 @@ void AppTask::SetInitiateAction(PWMDevice::Action_t aAction, int32_t aActor, uin if (setRgbAction) { +#ifdef CONFIG_WS2812_STRIP + sAppTask.mWS2812Device.SetLevel(&rgb); +#else sAppTask.mPwmRgbRedLed.InitiateAction(aAction, aActor, &rgb.r); sAppTask.mPwmRgbGreenLed.InitiateAction(aAction, aActor, &rgb.g); sAppTask.mPwmRgbBlueLed.InitiateAction(aAction, aActor, &rgb.b); +#endif // CONFIG_WS2812_STRIP } -#endif +#endif // CONFIG_WS2812_STRIP || USE_RGB_PWM } #ifdef CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET @@ -301,11 +359,15 @@ void AppTask::PowerOnFactoryResetEventHandler(AppEvent * aEvent) { LOG_INF("Lighting App Power On Factory Reset Handler"); sPowerOnFactoryResetTimerCnt = 1; +#ifdef CONFIG_WS2812_STRIP + sAppTask.mWS2812Device.Set(sPowerOnFactoryResetTimerCnt % 2); +#else sAppTask.mPwmRgbBlueLed.Set(sPowerOnFactoryResetTimerCnt % 2); #if USE_RGB_PWM sAppTask.mPwmRgbRedLed.Set(sPowerOnFactoryResetTimerCnt % 2); sAppTask.mPwmRgbGreenLed.Set(sPowerOnFactoryResetTimerCnt % 2); #endif +#endif // CONFIG_WS2812_STRIP k_timer_init(&sPowerOnFactoryResetTimer, PowerOnFactoryResetTimerEvent, nullptr); k_timer_start(&sPowerOnFactoryResetTimer, K_MSEC(kPowerOnFactoryResetIndicationTimeMs), K_MSEC(kPowerOnFactoryResetIndicationTimeMs)); @@ -315,11 +377,15 @@ void AppTask::PowerOnFactoryResetTimerEvent(struct k_timer * timer) { sPowerOnFactoryResetTimerCnt++; LOG_INF("Lighting App Power On Factory Reset Handler %u", sPowerOnFactoryResetTimerCnt); +#ifdef CONFIG_WS2812_STRIP + sAppTask.mWS2812Device.Set(sPowerOnFactoryResetTimerCnt % 2); +#else sAppTask.mPwmRgbBlueLed.Set(sPowerOnFactoryResetTimerCnt % 2); #if USE_RGB_PWM sAppTask.mPwmRgbRedLed.Set(sPowerOnFactoryResetTimerCnt % 2); sAppTask.mPwmRgbGreenLed.Set(sPowerOnFactoryResetTimerCnt % 2); #endif +#endif // CONFIG_WS2812_STRIP if (sPowerOnFactoryResetTimerCnt > kPowerOnFactoryResetIndicationMax) { k_timer_stop(timer); diff --git a/examples/lighting-app/telink/src/ZclCallbacks.cpp b/examples/lighting-app/telink/src/ZclCallbacks.cpp index 86e57809b67e11..53649639507142 100644 --- a/examples/lighting-app/telink/src/ZclCallbacks.cpp +++ b/examples/lighting-app/telink/src/ZclCallbacks.cpp @@ -48,7 +48,7 @@ void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & } else if (clusterId == LevelControl::Id && attributeId == LevelControl::Attributes::CurrentLevel::Id) { - if (GetAppTask().GetPWMDevice().IsTurnedOn()) + if (GetAppTask().GetLightingDevice().IsTurnedOn()) { ChipLogDetail(Zcl, "Cluster LevelControl: attribute CurrentLevel set to %u", *value); GetAppTask().SetInitiateAction(PWMDevice::LEVEL_ACTION, static_cast(AppEvent::kEventType_Lighting), value); @@ -138,7 +138,7 @@ void emberAfOnOffClusterInitCallback(EndpointId endpoint) if (status == EMBER_ZCL_STATUS_SUCCESS) { // Set actual state to stored before reboot - GetAppTask().GetPWMDevice().Set(storedValue); + GetAppTask().GetLightingDevice().Set(storedValue); } GetAppTask().UpdateClusterState(); diff --git a/examples/platform/telink/common/include/AppTaskCommon.h b/examples/platform/telink/common/include/AppTaskCommon.h index e0b344df5b6ec1..8cb25d4e8cbede 100644 --- a/examples/platform/telink/common/include/AppTaskCommon.h +++ b/examples/platform/telink/common/include/AppTaskCommon.h @@ -29,6 +29,10 @@ #include "PWMDevice.h" #endif +#ifdef CONFIG_WS2812_STRIP +#include "WS2812Device.h" +#endif + #include #include #include diff --git a/examples/temperature-measurement-app/telink/include/SensorManager.h b/examples/platform/telink/common/include/SensorManagerCommon.h similarity index 59% rename from examples/temperature-measurement-app/telink/include/SensorManager.h rename to examples/platform/telink/common/include/SensorManagerCommon.h index df1ffcf9378657..7181f8212a944c 100644 --- a/examples/temperature-measurement-app/telink/include/SensorManager.h +++ b/examples/platform/telink/common/include/SensorManagerCommon.h @@ -21,29 +21,42 @@ #include #include -#include "AppEventCommon.h" +#include "AppTaskCommon.h" #include #include +#if defined(CONFIG_CHIP_USE_MARS_SENSOR) && defined(CONFIG_WS2812_STRIP) && !defined(CONFIG_PM) +#define USE_COLOR_TEMPERATURE_LIGHT +#endif + class SensorManager { public: CHIP_ERROR Init(); + CHIP_ERROR GetTempAndHumMeasurValue(int16_t * pTempMeasured, uint16_t * pHumMeasured); - int16_t GetMeasuredValue(); - int16_t GetMinMeasuredValue(); - int16_t GetMaxMeasuredValue(); + int16_t GetMinMeasuredTempValue(); + int16_t GetMaxMeasuredTempValue(); private: friend SensorManager & SensorMgr(); - // Reads new generated sensor value, stores it, and updates local temperature attribute - static int16_t SensorEventHandler(); +#ifdef CONFIG_CHIP_USE_MARS_SENSOR + static void SensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer); + +#ifdef USE_COLOR_TEMPERATURE_LIGHT + WS2812Device mWS2812Device; + void SetColorTemperatureLight(int8_t temp); +#endif // USE_COLOR_TEMPERATURE_LIGHT - int16_t mMeasuredTempCelsius; + // SHT30 operating range −40…125°C + int16_t mMinMeasuredTempCelsius = -40; + int16_t mMaxMeasuredTempCelsius = 125; +#else int16_t mMinMeasuredTempCelsius = -40; int16_t mMaxMeasuredTempCelsius = 120; +#endif // CONFIG_CHIP_USE_MARS_SENSOR static SensorManager sSensorManager; }; diff --git a/examples/platform/telink/common/src/SensorManagerCommon.cpp b/examples/platform/telink/common/src/SensorManagerCommon.cpp new file mode 100644 index 00000000000000..1c4d5f9990a0cb --- /dev/null +++ b/examples/platform/telink/common/src/SensorManagerCommon.cpp @@ -0,0 +1,251 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * 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. + */ + +#include "SensorManagerCommon.h" +#ifdef CONFIG_CHIP_USE_MARS_SENSOR +#include +#endif // CONFIG_CHIP_USE_MARS_SENSOR + +LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); + +using namespace chip; +using namespace ::chip::DeviceLayer; + +constexpr float kMinTempDelta = 0.5; // 0.5 degree Celsius + +#ifdef CONFIG_CHIP_USE_MARS_SENSOR +k_timer sSensorBanForNextMeasurTimer; +volatile bool mSensorBanForNextMeasurFlag = false; +constexpr uint16_t kSensorBanForNextMeasurTimeout = 1000; // 1s timeout + +const struct device * const sht3xd_dev = DEVICE_DT_GET_ONE(sensirion_sht3xd); + +#ifdef USE_COLOR_TEMPERATURE_LIGHT +const struct device * const ws2812_dev = DEVICE_DT_GET(DT_ALIAS(led_strip)); + +#define TEMP_LOW_LIM 0 // °C +#define TEMP_HIGH_LIM 40 // °C +#endif // USE_COLOR_TEMPERATURE_LIGHT +#else +constexpr float kSimulatedHum = 55.5; // percents +constexpr uint16_t kSimulatedReadingFrequency = 4; // change simulated number +static float mSimulatedTemp[] = { 23.01, 24.02, 28.03, 25.50, 22.05, 21.25, 21.07, 26.08, 18.09, 27.11 }; +#endif // CONFIG_CHIP_USE_MARS_SENSOR + +SensorManager SensorManager::sSensorManager; + +CHIP_ERROR SensorManager::Init() +{ +#ifdef CONFIG_CHIP_USE_MARS_SENSOR + if (!device_is_ready(sht3xd_dev)) + { + LOG_ERR("Device %s is not ready", sht3xd_dev->name); + return CHIP_ERROR_INCORRECT_STATE; + } + +#ifdef USE_COLOR_TEMPERATURE_LIGHT + RgbColor_t rgb = { 0 }; + + CHIP_ERROR err = sSensorManager.mWS2812Device.Init(ws2812_dev, STRIP_NUM_PIXELS(led_strip)); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("WS2812 Device Init fail"); + return err; + } + + sSensorManager.mWS2812Device.SetLevel(&rgb); + sSensorManager.mWS2812Device.Set(SET_RGB_TURN_ON); +#endif // USE_COLOR_TEMPERATURE_LIGHT + + // Initialise the timer to ban sensor measurement + k_timer_init(&sSensorBanForNextMeasurTimer, &SensorManager::SensorBanForNextMeasurTimerTimeoutCallback, nullptr); + k_timer_user_data_set(&sSensorBanForNextMeasurTimer, this); +#endif // CONFIG_CHIP_USE_MARS_SENSOR + + return CHIP_NO_ERROR; +} + +CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t * pTempMeasured, uint16_t * pHumMeasured) +{ + static float lastTemp = 0.0; + float temp = 0.0; + float hum = 0.0; + +#ifdef CONFIG_CHIP_USE_MARS_SENSOR + static struct sensor_value sensorTemp = { 0 }; + static struct sensor_value sensorHum = { 0 }; + + if (!mSensorBanForNextMeasurFlag) + { + int status = sensor_sample_fetch(sht3xd_dev); + if (status) + { + LOG_ERR("Device %s is not ready to fetch the sensor samples (status: %d)", sht3xd_dev->name, status); + return System::MapErrorZephyr(status); + } + + status = sensor_channel_get(sht3xd_dev, SENSOR_CHAN_AMBIENT_TEMP, &sensorTemp); + if (status) + { + LOG_ERR("Device %s is not ready to temperature measurement (status: %d)", sht3xd_dev->name, status); + return System::MapErrorZephyr(status); + } + + status = sensor_channel_get(sht3xd_dev, SENSOR_CHAN_HUMIDITY, &sensorHum); + if (status) + { + LOG_ERR("Device %s is not ready to humidity measurement (status: %d)", sht3xd_dev->name, status); + return System::MapErrorZephyr(status); + } + + mSensorBanForNextMeasurFlag = true; + + // Start next timer to measurement the air quality sensor + k_timer_start(&sSensorBanForNextMeasurTimer, K_MSEC(kSensorBanForNextMeasurTimeout), K_NO_WAIT); + } + + temp = (float) sensor_value_to_double(&sensorTemp); + hum = (float) sensor_value_to_double(&sensorHum); + +#ifdef USE_COLOR_TEMPERATURE_LIGHT + SetColorTemperatureLight(temp); +#endif // USE_COLOR_TEMPERATURE_LIGHT +#else + /* Temperature simulation is used */ + static uint8_t nbOfRepetition = 0; + static uint8_t simulatedIndex = 0; + if (simulatedIndex >= ArraySize(mSimulatedTemp)) + { + simulatedIndex = 0; + } + temp = mSimulatedTemp[simulatedIndex]; + + nbOfRepetition++; + + if (nbOfRepetition >= kSimulatedReadingFrequency) + { + simulatedIndex++; + nbOfRepetition = 0; + } + + /* Humidity simulation is used */ + hum = kSimulatedHum; + +#endif // CONFIG_CHIP_USE_MARS_SENSOR + + if ((temp >= (lastTemp + kMinTempDelta)) || temp <= (lastTemp - kMinTempDelta)) + { + lastTemp = temp; + } + else + { + temp = lastTemp; + } + + if (pTempMeasured != NULL) + { + // Per spec Application Clusters 2.3.4.1. : MeasuredValue = 100 x temperature [°C] + *pTempMeasured = (int16_t) 100 * temp; + } + + if (pHumMeasured != NULL) + { + *pHumMeasured = (uint16_t) hum; + } + + return CHIP_NO_ERROR; +} + +int16_t SensorManager::GetMinMeasuredTempValue() +{ + return mMinMeasuredTempCelsius; +} + +int16_t SensorManager::GetMaxMeasuredTempValue() +{ + return mMaxMeasuredTempCelsius; +} + +#ifdef CONFIG_CHIP_USE_MARS_SENSOR +void SensorManager::SensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer) +{ + if (!timer) + { + return; + } + + mSensorBanForNextMeasurFlag = false; +} + +#ifdef USE_COLOR_TEMPERATURE_LIGHT +void SensorManager::SetColorTemperatureLight(int8_t temp) +{ + RgbColor_t rgb = { 0 }; + + if (temp >= mMinMeasuredTempCelsius && temp <= TEMP_LOW_LIM) + { + /* Set Color Temperature Light in range -40...0°C */ + rgb.b = RGB_MAX_VALUE * (1 - ((float) temp - TEMP_LOW_LIM) / (mMinMeasuredTempCelsius - TEMP_LOW_LIM)); + } + else if (temp >= TEMP_HIGH_LIM && temp <= mMaxMeasuredTempCelsius) + { + /* Set Color Temperature Light in range 40...125°C */ + rgb.r = RGB_MAX_VALUE * (1 - ((float) temp - TEMP_HIGH_LIM) / (mMaxMeasuredTempCelsius - TEMP_HIGH_LIM)); + } + else if (temp > TEMP_LOW_LIM && temp < TEMP_HIGH_LIM) + { + uint8_t steps_in_part = (TEMP_HIGH_LIM - TEMP_LOW_LIM) / 4; + uint8_t step_num = temp % steps_in_part; + float step_val = (float) RGB_MAX_VALUE / steps_in_part; + + if (temp < steps_in_part) + { + /* Set Color Temperature Light in range 1...9°C */ + rgb.b = RGB_MAX_VALUE; + rgb.g = step_num * step_val; + } + else if (temp < 2 * steps_in_part) + { + /* Set Color Temperature Light in range 10...19°C */ + rgb.b = RGB_MAX_VALUE; + rgb.g = RGB_MAX_VALUE; + rgb.r = step_num * step_val; + } + else if (temp < 3 * steps_in_part) + { + /* Set Color Temperature Light in range 20...29°C */ + rgb.r = RGB_MAX_VALUE; + rgb.g = RGB_MAX_VALUE; + rgb.b = RGB_MAX_VALUE - (step_num * step_val); + } + else + { + /* Set Color Temperature Light in range 30...39°C */ + rgb.r = RGB_MAX_VALUE; + rgb.g = RGB_MAX_VALUE - (step_num * step_val); + } + } + else + { + LOG_ERR("Couldn't set the Color Temperature Light"); + } + + sSensorManager.mWS2812Device.SetLevel(&rgb); +} +#endif // USE_COLOR_TEMPERATURE_LIGHT +#endif // CONFIG_CHIP_USE_MARS_SENSOR diff --git a/examples/platform/telink/util/include/WS2812Device.h b/examples/platform/telink/util/include/WS2812Device.h new file mode 100644 index 00000000000000..261802bde5f5be --- /dev/null +++ b/examples/platform/telink/util/include/WS2812Device.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * 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. + */ + +#pragma once + +#include + +#include + +#include + +#define STRIP_NUM_PIXELS(LED_STRIP) DT_PROP(DT_ALIAS(LED_STRIP), chain_length) +#define RGB_MIN_VALUE 0 +#define RGB_MAX_VALUE 255 + +#define SET_RGB_TURN_OFF 0 +#define SET_RGB_TURN_ON 1 + +class WS2812Device +{ +public: + CHIP_ERROR Init(const struct device * ws2812Device, uint32_t aChainLength); + void SetLevel(RgbColor_t * pRgb); + void Set(bool aTurnOn); + bool IsTurnedOn(void); + uint8_t GetBlueLevel(void) const { return mLedRgb.b; } + uint8_t GetGreenLevel(void) const { return mLedRgb.g; } + uint8_t GetRedLevel(void) const { return mLedRgb.r; } + +private: + enum WS2812State_t : uint8_t + { + kRgbState_On = 0, + kRgbState_Off, + }; + + void UpdateRgbLight(); + + const struct device * mWs2812Device; + uint32_t mChainLength; + RgbColor_t mLedRgb; + WS2812State_t mState; +}; diff --git a/examples/platform/telink/util/src/PWMDevice.cpp b/examples/platform/telink/util/src/PWMDevice.cpp index b77126b2b7b802..8456d5482a3e6e 100644 --- a/examples/platform/telink/util/src/PWMDevice.cpp +++ b/examples/platform/telink/util/src/PWMDevice.cpp @@ -25,7 +25,7 @@ #include #include -LOG_MODULE_DECLARE(app); +LOG_MODULE_REGISTER(PWMDevice); constexpr uint32_t kBreatheStepTimeMS = 10; diff --git a/examples/platform/telink/util/src/WS2812Device.cpp b/examples/platform/telink/util/src/WS2812Device.cpp new file mode 100644 index 00000000000000..06b70373f8ff29 --- /dev/null +++ b/examples/platform/telink/util/src/WS2812Device.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * 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. + */ + +#include "WS2812Device.h" + +#include + +#include +#include + +LOG_MODULE_REGISTER(WS2812Device); + +using namespace chip; + +CHIP_ERROR WS2812Device::Init(const struct device * ws2812Device, uint32_t aChainLength) +{ + mWs2812Device = ws2812Device; + mChainLength = aChainLength; + mState = kRgbState_Off; + memset(&mLedRgb, RGB_MAX_VALUE, sizeof(RgbColor_t)); + + if (!device_is_ready(mWs2812Device)) + { + LOG_ERR("Device %s is not ready", mWs2812Device->name); + return CHIP_ERROR_INCORRECT_STATE; + } + + UpdateRgbLight(); + + return CHIP_NO_ERROR; +} + +void WS2812Device::UpdateRgbLight() +{ + int status; + led_rgb setRgb = { 0 }; + + if (mState == kRgbState_On) + { + setRgb.r = mLedRgb.r; + setRgb.g = mLedRgb.g; + setRgb.b = mLedRgb.b; + } + + status = led_strip_update_rgb(mWs2812Device, &setRgb, mChainLength); + if (status) + { + LOG_ERR("Couldn't update strip: %d", status); + } +} + +void WS2812Device::SetLevel(RgbColor_t * pRgb) +{ + if (pRgb != NULL) + { + memcpy(&mLedRgb, pRgb, sizeof(RgbColor_t)); + } + + UpdateRgbLight(); +} + +void WS2812Device::Set(bool aTurnOn) +{ + mState = aTurnOn ? kRgbState_On : kRgbState_Off; + UpdateRgbLight(); +} + +bool WS2812Device::IsTurnedOn() +{ + return mState == kRgbState_On; +} diff --git a/examples/temperature-measurement-app/telink/CMakeLists.txt b/examples/temperature-measurement-app/telink/CMakeLists.txt index fd8a6e33132970..e409b2e2bc59de 100644 --- a/examples/temperature-measurement-app/telink/CMakeLists.txt +++ b/examples/temperature-measurement-app/telink/CMakeLists.txt @@ -19,6 +19,21 @@ get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/third_party/connect get_filename_component(TELINK_COMMON ${CHIP_ROOT}/examples/platform/telink REALPATH) get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH) +if(${TLNK_MARS_BOARD} MATCHES y) + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.overlay") + set(MARS_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.overlay") + else() + unset(MARS_DTC_OVERLAY_FILE) + endif() + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.conf") + set(MARS_CONF_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.conf") + else() + unset(MARS_CONF_OVERLAY_FILE) + endif() +else() + unset(MARS_CONF_OVERLAY_FILE) +endif() + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") set(LOCAL_DTC_OVERLAY_FILE "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") else() @@ -41,14 +56,14 @@ endif() if(DTC_OVERLAY_FILE) set(DTC_OVERLAY_FILE - "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" + "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" CACHE STRING "" FORCE ) else() - set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) + set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE prj.conf) +set(CONF_FILE ${MARS_CONF_OVERLAY_FILE} prj.conf) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -76,14 +91,14 @@ add_definitions( target_sources(app PRIVATE src/AppTask.cpp - src/SensorManager.cpp ${TELINK_COMMON}/common/src/mainCommon.cpp ${TELINK_COMMON}/common/src/AppTaskCommon.cpp + ${TELINK_COMMON}/common/src/SensorManagerCommon.cpp ${TELINK_COMMON}/util/src/LEDWidget.cpp ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp ${TELINK_COMMON}/util/src/PWMDevice.cpp - ) + ${TELINK_COMMON}/util/src/WS2812Device.cpp) chip_configure_data_model(app INCLUDE_SERVER diff --git a/examples/temperature-measurement-app/telink/include/AppTask.h b/examples/temperature-measurement-app/telink/include/AppTask.h index f681fa37fa2378..1aac92dc45aa7d 100644 --- a/examples/temperature-measurement-app/telink/include/AppTask.h +++ b/examples/temperature-measurement-app/telink/include/AppTask.h @@ -28,8 +28,8 @@ class AppTask : public AppTaskCommon CHIP_ERROR Init(void); - static void TemperatureMeasurementTimerTimeoutCallback(k_timer * timer); - static void TemperatureMeasurementTimerEventHandler(AppEvent * aEvent); + static void TemperatureMeasurementUpdateTimerTimeoutCallback(k_timer * timer); + static void TemperatureMeasurementUpdateTimerEventHandler(AppEvent * aEvent); static AppTask sAppTask; }; diff --git a/examples/temperature-measurement-app/telink/src/AppTask.cpp b/examples/temperature-measurement-app/telink/src/AppTask.cpp index 023381e5225db2..df1c5f6dc20e2d 100644 --- a/examples/temperature-measurement-app/telink/src/AppTask.cpp +++ b/examples/temperature-measurement-app/telink/src/AppTask.cpp @@ -17,43 +17,46 @@ */ #include "AppTask.h" -#include "SensorManager.h" +#include "SensorManagerCommon.h" LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); namespace { -k_timer sTemperatureMeasurementTimer; -constexpr uint16_t kSensorTimerPeriodMs = 5000; // 5s timer period +k_timer sTemperatureMeasurementUpdateTimer; +constexpr uint16_t kTemperatureMeasurementUpdateTimerPeriodMs = 5000; // 5s timer period } // namespace AppTask AppTask::sAppTask; CHIP_ERROR AppTask::Init(void) { - InitCommonParts(); + CHIP_ERROR err; - // Initialize temperature measurement timer - k_timer_init(&sTemperatureMeasurementTimer, &AppTask::TemperatureMeasurementTimerTimeoutCallback, nullptr); - k_timer_user_data_set(&sTemperatureMeasurementTimer, this); - k_timer_start(&sTemperatureMeasurementTimer, K_MSEC(kSensorTimerPeriodMs), K_NO_WAIT); + InitCommonParts(); - // Init Temperature Sensor - CHIP_ERROR err = SensorMgr().Init(); + err = SensorMgr().Init(); if (err != CHIP_NO_ERROR) { - LOG_ERR("SensorMgr Init fail"); + LOG_ERR("Init of the Sensor Manager failed"); return err; } + // Initialize temperature measurement update timer + k_timer_init(&sTemperatureMeasurementUpdateTimer, &AppTask::TemperatureMeasurementUpdateTimerTimeoutCallback, nullptr); + k_timer_user_data_set(&sTemperatureMeasurementUpdateTimer, this); + k_timer_start(&sTemperatureMeasurementUpdateTimer, K_MSEC(kTemperatureMeasurementUpdateTimerPeriodMs), K_NO_WAIT); + PlatformMgr().LockChipStack(); - app::Clusters::TemperatureMeasurement::Attributes::MinMeasuredValue::Set(kExampleEndpointId, SensorMgr().GetMinMeasuredValue()); - app::Clusters::TemperatureMeasurement::Attributes::MaxMeasuredValue::Set(kExampleEndpointId, SensorMgr().GetMaxMeasuredValue()); + app::Clusters::TemperatureMeasurement::Attributes::MinMeasuredValue::Set(kExampleEndpointId, + SensorMgr().GetMinMeasuredTempValue()); + app::Clusters::TemperatureMeasurement::Attributes::MaxMeasuredValue::Set(kExampleEndpointId, + SensorMgr().GetMaxMeasuredTempValue()); PlatformMgr().UnlockChipStack(); return CHIP_NO_ERROR; } -void AppTask::TemperatureMeasurementTimerTimeoutCallback(k_timer * timer) +void AppTask::TemperatureMeasurementUpdateTimerTimeoutCallback(k_timer * timer) { if (!timer) { @@ -62,23 +65,33 @@ void AppTask::TemperatureMeasurementTimerTimeoutCallback(k_timer * timer) AppEvent event; event.Type = AppEvent::kEventType_Timer; - event.Handler = TemperatureMeasurementTimerEventHandler; + event.Handler = TemperatureMeasurementUpdateTimerEventHandler; sAppTask.PostEvent(&event); } -void AppTask::TemperatureMeasurementTimerEventHandler(AppEvent * aEvent) +void AppTask::TemperatureMeasurementUpdateTimerEventHandler(AppEvent * aEvent) { + CHIP_ERROR ret; + int16_t temperature; + if (aEvent->Type != AppEvent::kEventType_Timer) { return; } + ret = SensorMgr().GetTempAndHumMeasurValue(&temperature, NULL); + if (ret != CHIP_NO_ERROR) + { + LOG_ERR("Update of the Temperature clusters failed"); + return; + } + + LOG_INF("Current temperature is (%d*0.01)°C", temperature); + PlatformMgr().LockChipStack(); - app::Clusters::TemperatureMeasurement::Attributes::MeasuredValue::Set(kExampleEndpointId, SensorMgr().GetMeasuredValue()); + app::Clusters::TemperatureMeasurement::Attributes::MeasuredValue::Set(kExampleEndpointId, temperature); PlatformMgr().UnlockChipStack(); - LOG_INF("Current temperature is (%d*0.01)°C", SensorMgr().GetMeasuredValue()); - // Start next timer to handle temp sensor. - k_timer_start(&sTemperatureMeasurementTimer, K_MSEC(kSensorTimerPeriodMs), K_NO_WAIT); + k_timer_start(&sTemperatureMeasurementUpdateTimer, K_MSEC(kTemperatureMeasurementUpdateTimerPeriodMs), K_NO_WAIT); } diff --git a/examples/temperature-measurement-app/telink/src/SensorManager.cpp b/examples/temperature-measurement-app/telink/src/SensorManager.cpp deleted file mode 100644 index 22d9bb1c57ef19..00000000000000 --- a/examples/temperature-measurement-app/telink/src/SensorManager.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* - * - * Copyright (c) 2023 Project CHIP Authors - * All rights reserved. - * - * 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. - */ - -#include "SensorManager.h" -#include "AppConfig.h" -#include "AppTask.h" - -#define TEMPERATURE_SIMULATION_IS_USED - -LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); - -using namespace chip; -using namespace ::chip::DeviceLayer; - -constexpr float kMinTemperatureDelta = 0.5; // 0.5 degree Celsius -constexpr uint16_t kSimulatedReadingFrequency = 5; // Change Simulated number -static float mSimulatedTemp[] = { 23.01, 24.02, 28.03, 25.50, 22.05, 21.25, 21.07, 26.08, 18.09, 27.11 }; - -k_timer sSensorTimer; - -SensorManager SensorManager::sSensorManager; - -CHIP_ERROR SensorManager::Init() -{ - // TODO: Initialize temp sensor - return CHIP_NO_ERROR; -} - -int16_t SensorManager::SensorEventHandler() -{ - float temperature = 0.0; - static float lastTemperature = 0.0; - -#ifdef TEMPERATURE_SIMULATION_IS_USED - static uint8_t nbOfRepetition = 0; - static uint8_t simulatedIndex = 0; - if (simulatedIndex >= ArraySize(mSimulatedTemp)) - { - simulatedIndex = 0; - } - temperature = mSimulatedTemp[simulatedIndex]; - - nbOfRepetition++; - - if (nbOfRepetition >= kSimulatedReadingFrequency) - { - simulatedIndex++; - nbOfRepetition = 0; - } - - if ((temperature >= (lastTemperature + kMinTemperatureDelta)) || temperature <= (lastTemperature - kMinTemperatureDelta)) - { - lastTemperature = temperature; - // Per spec Application Clusters 2.3.4.1. : MeasuredValue = 100 x temperature [°C] - sSensorManager.mMeasuredTempCelsius = (int16_t) 100 * temperature; - } -#else - // TODO: provide REAL sensor implementation - sSensorManager.mMeasuredTempCelsius = (int16_t) 100 * GetRealSensorTemperature(); -#endif // TEMPERATURE_SIMULATION_IS_USED - return sSensorManager.mMeasuredTempCelsius; -} - -int16_t SensorManager::GetMeasuredValue() -{ - return SensorEventHandler(); -} - -int16_t SensorManager::GetMinMeasuredValue() -{ - return mMinMeasuredTempCelsius; -} - -int16_t SensorManager::GetMaxMeasuredValue() -{ - return mMaxMeasuredTempCelsius; -} diff --git a/examples/thermostat/telink/CMakeLists.txt b/examples/thermostat/telink/CMakeLists.txt index f1588efa2c48e7..71f14895b28a55 100755 --- a/examples/thermostat/telink/CMakeLists.txt +++ b/examples/thermostat/telink/CMakeLists.txt @@ -19,6 +19,21 @@ get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/third_party/connect get_filename_component(TELINK_COMMON ${CHIP_ROOT}/examples/platform/telink REALPATH) get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH) +if(${TLNK_MARS_BOARD} MATCHES y) + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.overlay") + set(MARS_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.overlay") + else() + unset(MARS_DTC_OVERLAY_FILE) + endif() + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.conf") + set(MARS_CONF_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.conf") + else() + unset(MARS_CONF_OVERLAY_FILE) + endif() +else() + unset(MARS_CONF_OVERLAY_FILE) +endif() + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") set(LOCAL_DTC_OVERLAY_FILE "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") else() @@ -41,14 +56,14 @@ endif() if(DTC_OVERLAY_FILE) set(DTC_OVERLAY_FILE - "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" + "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" CACHE STRING "" FORCE ) else() - set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) + set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE prj.conf) +set(CONF_FILE ${MARS_CONF_OVERLAY_FILE} prj.conf) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -75,15 +90,16 @@ add_definitions( target_sources(app PRIVATE src/AppTask.cpp - src/SensorManager.cpp src/TemperatureManager.cpp src/ZclCallbacks.cpp ${TELINK_COMMON}/common/src/mainCommon.cpp ${TELINK_COMMON}/common/src/AppTaskCommon.cpp + ${TELINK_COMMON}/common/src/SensorManagerCommon.cpp ${TELINK_COMMON}/util/src/LEDWidget.cpp ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp - ${TELINK_COMMON}/util/src/PWMDevice.cpp) + ${TELINK_COMMON}/util/src/PWMDevice.cpp + ${TELINK_COMMON}/util/src/WS2812Device.cpp) chip_configure_data_model(app INCLUDE_SERVER diff --git a/examples/thermostat/telink/include/AppTask.h b/examples/thermostat/telink/include/AppTask.h index 8babf015ba4817..40cd86f2f551ea 100755 --- a/examples/thermostat/telink/include/AppTask.h +++ b/examples/thermostat/telink/include/AppTask.h @@ -19,13 +19,12 @@ #pragma once #include "AppTaskCommon.h" -#include "SensorManager.h" +#include "SensorManagerCommon.h" #include "TemperatureManager.h" class AppTask : public AppTaskCommon { public: - void UpdateClusterState(void); void UpdateThermoStatUI(void); private: @@ -34,6 +33,9 @@ class AppTask : public AppTaskCommon CHIP_ERROR Init(void); + static void ThermostatUpdateTimerTimeoutCallback(k_timer * timer); + static void ThermostatUpdateTimerEventHandler(AppEvent * aEvent); + static AppTask sAppTask; }; diff --git a/examples/thermostat/telink/include/SensorManager.h b/examples/thermostat/telink/include/SensorManager.h deleted file mode 100644 index 2d2cdb9dd44fc6..00000000000000 --- a/examples/thermostat/telink/include/SensorManager.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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. - */ - -#pragma once - -#include -#include - -#include "AppEventCommon.h" - -#include -#include - -class SensorManager -{ -public: - CHIP_ERROR Init(); - -private: - friend SensorManager & SensorMgr(); - - // Reads new generated sensor value, stores it, and updates local temperature attribute - static void TimerEventHandler(k_timer * timer); - static void SensorTimerEventHandler(AppEvent * aEvent); - - static SensorManager sSensorManager; -}; - -inline SensorManager & SensorMgr() -{ - return SensorManager::sSensorManager; -} diff --git a/examples/thermostat/telink/src/AppTask.cpp b/examples/thermostat/telink/src/AppTask.cpp index d9b7a2c2883844..101d1a9f0fda4d 100644 --- a/examples/thermostat/telink/src/AppTask.cpp +++ b/examples/thermostat/telink/src/AppTask.cpp @@ -20,18 +20,26 @@ LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); +namespace { +k_timer sThermostatUpdateTimer; +constexpr uint16_t kThermostatUpdateTimerPeriodMs = 30000; // 30s timer period +} // namespace + AppTask AppTask::sAppTask; CHIP_ERROR AppTask::Init(void) { + CHIP_ERROR err; + InitCommonParts(); - CHIP_ERROR err = SensorMgr().Init(); + err = SensorMgr().Init(); if (err != CHIP_NO_ERROR) { - LOG_ERR("SensorMgr Init fail"); + LOG_ERR("Init of the Sensor Manager failed"); return err; } + err = TempMgr().Init(); if (err != CHIP_NO_ERROR) { @@ -39,13 +47,56 @@ CHIP_ERROR AppTask::Init(void) return err; } + // Initialize temperature measurement timer + k_timer_init(&sThermostatUpdateTimer, &AppTask::ThermostatUpdateTimerTimeoutCallback, nullptr); + k_timer_user_data_set(&sThermostatUpdateTimer, this); + k_timer_start(&sThermostatUpdateTimer, K_MSEC(kThermostatUpdateTimerPeriodMs), K_NO_WAIT); + return CHIP_NO_ERROR; } +void AppTask::ThermostatUpdateTimerTimeoutCallback(k_timer * timer) +{ + if (!timer) + { + return; + } + + AppEvent event; + event.Type = AppEvent::kEventType_Timer; + event.Handler = ThermostatUpdateTimerEventHandler; + sAppTask.PostEvent(&event); +} + +void AppTask::ThermostatUpdateTimerEventHandler(AppEvent * aEvent) +{ + CHIP_ERROR ret; + int16_t temperature; + + if (aEvent->Type != AppEvent::kEventType_Timer) + { + return; + } + + ret = SensorMgr().GetTempAndHumMeasurValue(&temperature, NULL); + if (ret != CHIP_NO_ERROR) + { + LOG_ERR("Update of the Temperature clusters failed"); + return; + } + + LOG_INF("Current temperature is (%d*0.01)°C", temperature); + + PlatformMgr().LockChipStack(); + app::Clusters::Thermostat::Attributes::LocalTemperature::Set(kExampleEndpointId, temperature); + PlatformMgr().UnlockChipStack(); + + // Start next timer to handle temp sensor. + k_timer_start(&sThermostatUpdateTimer, K_MSEC(kThermostatUpdateTimerPeriodMs), K_NO_WAIT); +} + void AppTask::UpdateThermoStatUI(void) { LOG_INF("Thermostat Status - M:%d T:%d'C H:%d'C C:%d'C", TempMgr().GetMode(), TempMgr().GetCurrentTemp(), TempMgr().GetHeatingSetPoint(), TempMgr().GetCoolingSetPoint()); } - -void AppTask::UpdateClusterState() {} diff --git a/examples/thermostat/telink/src/SensorManager.cpp b/examples/thermostat/telink/src/SensorManager.cpp deleted file mode 100644 index 981447c8a12bd6..00000000000000 --- a/examples/thermostat/telink/src/SensorManager.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - * - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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. - */ - -#include "SensorManager.h" -#include "AppConfig.h" -#include "AppTask.h" - -LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); - -using namespace chip; -using namespace ::chip::DeviceLayer; - -constexpr EndpointId kThermostatEndpoint = 1; -constexpr uint16_t kSensorTimerPeriodMs = 30000; // 30s timer period -constexpr uint16_t kMinTemperatureDelta = 50; // 0.5 degree Celsius - -k_timer sSensorTimer; - -SensorManager SensorManager::sSensorManager; - -constexpr uint16_t kSimulatedReadingFrequency = (60000 / kSensorTimerPeriodMs); // Change Simulated number at each minutes -static int16_t mSimulatedTemp[] = { 2300, 2400, 2800, 2550, 2200, 2125, 2100, 2600, 1800, 2700 }; - -CHIP_ERROR SensorManager::Init() -{ - // Initialize temp sensor timer - k_timer_init(&sSensorTimer, &TimerEventHandler, nullptr); - k_timer_user_data_set(&sSensorTimer, this); - k_timer_start(&sSensorTimer, K_MSEC(kSensorTimerPeriodMs), K_NO_WAIT); - - return CHIP_NO_ERROR; -} - -void SensorManager::TimerEventHandler(k_timer * timer) -{ - AppEvent event; - event.Type = AppEvent::kEventType_Thermostat; - event.TimerEvent.Context = k_timer_user_data_get(timer); - event.Handler = SensorTimerEventHandler; - GetAppTask().PostEvent(&event); -} - -void SensorManager::SensorTimerEventHandler(AppEvent * aEvent) -{ - int16_t temperature = 0; - static int16_t lastTemperature = 0; - - static uint8_t nbOfRepetition = 0; - static uint8_t simulatedIndex = 0; - if (simulatedIndex >= ArraySize(mSimulatedTemp)) - { - simulatedIndex = 0; - } - temperature = mSimulatedTemp[simulatedIndex]; - - nbOfRepetition++; - if (nbOfRepetition >= kSimulatedReadingFrequency) - { - simulatedIndex++; - nbOfRepetition = 0; - } - - LOG_INF("Sensor Temp is : %d", temperature); - - if ((temperature >= (lastTemperature + kMinTemperatureDelta)) || temperature <= (lastTemperature - kMinTemperatureDelta)) - { - lastTemperature = temperature; - PlatformMgr().LockChipStack(); - app::Clusters::Thermostat::Attributes::LocalTemperature::Set(kThermostatEndpoint, temperature); - PlatformMgr().UnlockChipStack(); - } - - // Start next timer to handle temp sensor. - k_timer_start(&sSensorTimer, K_MSEC(kSensorTimerPeriodMs), K_NO_WAIT); -} diff --git a/src/platform/telink/tlsr9518adk80d_mars.conf b/src/platform/telink/tlsr9518adk80d_mars.conf new file mode 100644 index 00000000000000..1b7b4a47378f6c --- /dev/null +++ b/src/platform/telink/tlsr9518adk80d_mars.conf @@ -0,0 +1,17 @@ +CONFIG_CHIP_USE_MARS_SENSOR=y +CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=y + +# SHT3XD +CONFIG_I2C=y +CONFIG_SENSOR=y + +# WS2812 +CONFIG_LED_STRIP=y +CONFIG_LED_STRIP_LOG_LEVEL_DBG=y +CONFIG_WS2812_STRIP=y +CONFIG_WS2812_STRIP_GPIO_TELINK=y + +# DFU via USB +CONFIG_USB_DEVICE_STACK=y +CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n +CONFIG_USB_CDC_ACM_LOG_LEVEL_OFF=y diff --git a/src/platform/telink/tlsr9518adk80d_mars.overlay b/src/platform/telink/tlsr9518adk80d_mars.overlay new file mode 100644 index 00000000000000..303474ca04774d --- /dev/null +++ b/src/platform/telink/tlsr9518adk80d_mars.overlay @@ -0,0 +1,109 @@ +#include +#include + +/ { + aliases { + led-strip = &led_strip; + }; + + leds { + /delete-node/ led_3; + + led_red: led_3 { + label = "LED Red"; + gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; + }; + }; + + pwm_leds { + /delete-node/ pwm_led_0; + /delete-node/ pwm_led_1; + /delete-node/ pwm_led_2; + /delete-node/ pwm_led_3; + + pwm_led0: pwm_led_0 { + pwms = <&pwm0 0 PWM_MSEC(20) PWM_POLARITY_NORMAL>; + label = "PWM LED Blue"; + }; + pwm_led1: pwm_led_1 { + pwms = <&pwm0 3 PWM_MSEC(20) PWM_POLARITY_NORMAL>; + label = "PWM LED Green"; + }; + pwm_led2: pwm_led_2 { + pwms = <&pwm0 2 PWM_MSEC(20) PWM_POLARITY_NORMAL>; + label = "PWM LED Red"; + }; + pwm_led3: pwm_led_3 { + pwms = <&pwm0 1 PWM_MSEC(20) PWM_POLARITY_NORMAL>; + label = "PWM IDENTIFY LED Green"; + }; + }; + + keys { + /delete-node/ button_1; + /delete-node/ button_4; + compatible = "gpio-keys"; + key_1: button_1 { + gpios = <&gpiod 1 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; + }; + key_4: button_4 { + gpios = <&gpiod 3 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; + }; + }; + + led_strip: ws2812 { + compatible = "worldsemi,ws2812-gpio"; + + chain-length = <1>; /* arbitrary; change at will */ + color-mapping = ; + in-gpios = <&gpiob 4 0>; + }; +}; + +&gpioc { + interrupts = <38 1>; +}; + +&gpiod { + interrupts = <37 1>; + status = "okay"; +}; + +&pinctrl { + /* Define I2C pins: SCL(PE0), SDA(PC2) */ + i2c_scl_pe0_default: i2c_scl_pe0_default { + pinmux = ; + }; + i2c_sda_pc2_default: i2c_sda_pc2_default { + pinmux = ; + }; + + /* PWMs */ + pwm_ch0_pe3_default: pwm_ch0_pe3_default { + pinmux = ; + }; + pwm_ch1_pb5_default: pwm_ch1_pb5_default { + pinmux = ; + }; + pwm_ch2_pe2_default: pwm_ch2_pe2_default { + pinmux = ; + }; + pwm_ch4_pe4_default: pwm_ch4_pe4_default { + pinmux = ; + }; +}; + +&i2c { + pinctrl-0 = <&i2c_scl_pe0_default &i2c_sda_pc2_default>; + + sht3xd@44 { + compatible = "sensirion,sht3xd"; + reg = <0x44>; + }; +}; + +&pwm0 { + pinctrl-0 = <&pwm_ch0_pe3_default &pwm_ch1_pb5_default &pwm_ch2_pe2_default &pwm_ch4_pe4_default>; +}; diff --git a/src/platform/telink/tlsr9518adk80d_mars_boot.overlay b/src/platform/telink/tlsr9518adk80d_mars_boot.overlay new file mode 100644 index 00000000000000..64a998bb306178 --- /dev/null +++ b/src/platform/telink/tlsr9518adk80d_mars_boot.overlay @@ -0,0 +1,28 @@ +/ { + leds { + /delete-node/ led_0; + + led_blue: led_0 { + label = "LED Blue"; + gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; + }; + }; + + keys { + /delete-node/ button_dfu; + + key_dfu: button_dfu { + label = "USB DFU"; + gpios = <&gpiod 2 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; + }; + }; +}; + +&gpioc { + interrupts = <38 1>; +}; + +&gpiod { + interrupts = <37 1>; + status = "okay"; +};