Bluetooth Low Energy Mode

Last Updated on : 2024-02-23 09:33:05download

The Bluetooth mesh specification defines the low power node (LPN) that can automatically make a friendship with a friend node. The friend node can store messages addressed to an associated LPN and will forward these messages to the LPN when it wakes up. TuyaOS Bluetooth Mesh SDK does not support LPN currently. If support becomes available in the future, a separate SDK will be released.

TuyaOS Bluetooth Mesh SDK supports low power through the sleep and wake-up modes. Battery-powered Bluetooth mesh devices will keep scanning if they do not sleep, leading to high power consumption of radio frequency and SoC. Therefore, enabling the sleep mode for these devices can help save energy. The power consumption of Bluetooth mesh chips on the market typically ranges from about 6 mA to 10 mA in active mode without sleep.

In low power mode, the device will not be able to receive mesh data. Bluetooth mesh low power is suitable for products such as remotes, scene panels, wireless switches, and sensors.

Sleep mode

The SDK supports two sleep modes: TUYA_CPU_SLEEP and TUYA_CPU_DEEP_SLEEP, as shown below.

Parameter TUYA_CPU_SLEEP TUYA_CPU_DEEP_SLEEP
Mode Suspend (sleep) Deep sleep
RAM retention Retention No retention. If the chip supports the retention mode, RAM retention is available.
PC pointer after wake-up Resume from where it left Start over from the beginning
Relative power consumption Low Ultra low

Data structure

TUYA_CPU_SLEEP_MODE_E

typedef enum {
    TUYA_CPU_SLEEP,
    TUYA_CPU_DEEP_SLEEP,
} TUYA_CPU_SLEEP_MODE_E;
  • TUYA_CPU_SLEEP: Defaults to suspend mode.

  • TUYA_CPU_DEEP_SLEEP: Deep sleep mode.

TUYA_WAKEUP_SOURCE_E

typedef enum {
    TUYA_WAKEUP_SOURCE_GPIO,
    TUYA_WAKEUP_SOURCE_TIMER,
} TUYA_WAKEUP_SOURCE_E;
  • TUYA_WAKEUP_SOURCE_GPIO: GPIO-based wake-up.

  • TUYA_WAKEUP_SOURCE_TIMER: Timer-based wake-up.

TUYA_WAKEUP_SOURCE_GPIO_T

typedef struct {
    TUYA_GPIO_NUM_E gpio_num;
    TUYA_GPIO_LEVEL_E level;
} TUYA_WAKEUP_SOURCE_GPIO_T;
  • gpio_num: GPIO number.

  • level: The level of the GPIO for wake-up.

TUYA_WAKEUP_SOURCE_TIME_T

typedef struct {
    TUYA_TIMER_NUM_E timer_num;
    TUYA_TIMER_MODE_E mode;
    UINT32_T ms;
} TUYA_WAKEUP_SOURCE_TIMER_T;

TUYA_WAKEUP_SOURCE_BASE_CFG_T

typedef struct {
    TUYA_WAKEUP_SOURCE_E source;
    union {
        TUYA_WAKEUP_SOURCE_GPIO_T gpio_param;
        TUYA_WAKEUP_SOURCE_TIMER_T timer_param;
    } wakeup_para;
}TUYA_WAKEUP_SOURCE_BASE_CFG_T;
  • source: The wake-up source.

  • gpio_param: GPIO-based wake-up.

  • timer_param: Timer-based wake-up.

API usage

Configure sleep mode

OPERATE_RET tkl_cpu_sleep_mode_set(BOOL_T enable, TUYA_CPU_SLEEP_MODE_E mode);
  • enable: Enable sleep mode.

  • mode: The sleep mode.

Enter sleep mode

VOID_T tkl_cpu_allow_sleep(VOID_T);

After the SDK calls this API, the device immediately enters sleep mode.

Configure wake-up source

OPERATE_RET tkl_wakeup_source_set(CONST TUYA_WAKEUP_SOURCE_BASE_CFG_T *param);

param: The parameter for the wake-up source. See Data Structure for data format.

How-to

In this example, the sleep mode is set to suspend, with GPIO 8 low level and a 30-second timer as the wake-up sources.

TUYA_WAKEUP_SOURCE_BASE_CFG_T param_gpio;
param_gpio.source = TUYA_WAKEUP_SOURCE_GPIO;
param_gpio.wakeup_para.gpio_param.gpio_num = TUYA_GPIO_NUM_8;
param_gpio.wakeup_para.gpio_param.level = TUYA_GPIO_LEVEL_LOW;

TUYA_WAKEUP_SOURCE_BASE_CFG_T param_timer;
param_timer.source = TUYA_WAKEUP_SOURCE_TIMER;
param_timer.wakeup_para.timer_param.timer_num = TUYA_TIMER_NUM_0;
param_timer.wakeup_para.timer_param.mode = TUYA_TIMER_MODE_ONCE;
param_timer.wakeup_para.timer_param.ms = 30000;

tkl_wakeup_source_set(&param_gpio);
tkl_wakeup_source_set(&param_timer);

tkl_cpu_sleep_mode_set(1, TUYA_CPU_SLEEP);
tkl_cpu_allow_sleep();

Things to note

Adapt IV updates

After a period of time, the mesh network will update its IV index. If the device does not wake up periodically for these updates, it will be unable to communicate with other devices in the mesh network.

Wake-up mechanism: Low power devices need to wake up every 96 hours for a minimum of 15 seconds each time.

Platform features

  • PHY6222 platform: It does not support timer-based wake-up in Deep_sleep mode.

  • PHY6222 platform: It does not support RAM retention in Deep_sleep mode.

  • TLSR825x platform: Theoretically, the maximum sleep time is 268 seconds.

Deep sleep retention

The TLSR825x platform supports 32 KB RAM retention in deep sleep mode, known as the deep sleep retention mode. The retention mode is disabled by default. You can enable the retention mode for the SDK v3.8.0 or later as instructed below.

Add #define TKL_DEEPSLEEP_RETENTION_ENABLE 1 in tuya_iot_config.h. When the retention mode is enabled, all variables will be stored in the retention area by default. Compilation may fail if the retention area exceeds 32 KB in size. To save space in the retention area, add the prefix _attribute_no_retention_bss_ to variables that do not need to be retained.

Note: When the heap is used to allocate memory, the heap content is not retained after wake-up.

After enabling retention mode, modify the sleep API as shown below. Otherwise, the IV update will not function correctly.

INT32_T tuya_cpu_sleep_wakeup(SleepMode_TypeDef deepsleep, SleepWakeupSrc_TypeDef wakeup_src, UINT32_T ms)
{
#if 0
    return cpu_long_sleep_wakeup(deepsleep, wakeup_src, ms * HAL_CLOCK_1MS_TICKS);
#else
    UINT32_T wakeup_tick = clock_time() + ms * CLOCK_SYS_CLOCK_1MS;
    return cpu_sleep_wakeup(deepsleep, wakeup_src, wakeup_tick);
#endif
}