Device Initialization

Last Updated on : 2024-02-23 07:22:06download

After a device is powered on and started, it calls a range of APIs to initialize the hardware environment, TuyaOS software, and applications.

API description

Initialization

TuyaOS Bluetooth Mesh SDK provides multiple initialization APIs, which are called by the SDK at different times.

OPERATE_RET tuya_init_first(VOID_T);
OPERATE_RET tuya_init_second(VOID_T);
OPERATE_RET tuya_init_third(VOID_T);
OPERATE_RET tuya_init_last(VOID_T);

The table below lists the differences between initialization APIs.

API Timing Use cases
tuya_init_first Invoked after the MCU is initialized. The device is required to respond immediately after startup.
tuya_init_second Invoked before the Bluetooth and mesh protocol stacks are initialized. Initialize the Bluetooth Low Energy (LE) and Bluetooth mesh protocol stacks.
tuya_init_third Invoked after the Bluetooth and mesh protocol stacks are initialized.
  • Features that can only be initialized after obtaining the status of the Bluetooth mesh stack.
  • Register the Bluetooth mesh model.
  • Initialize the main service.
tuya_init_last The final stage for SDK initialization. Other features.

Loop

The loop function or the run function is repeated by the while loop in the SDK. The loop function can implement multiple features, for example, the processing of queues or events and the software timer handler.

OPERATE_RET tuya_main_loop(VOID_T);

Firmware information initialization

VOID tal_firmware_infor_set(UINT8_T is_key, UINT8_T *product_id, UINT8_T *product_key, UINT16_T version, UINT16_T mesh_category, UINT8_T need_publish_addr);
VOID tal_mesh_factory_firmware_info_set(UINT8_T *firmname, UINT8_T *version);
VOID tal_mesh_gatt_ota_version_set(UINT16_T version);
  • product_id and product_key are product information. is_key determines which information to use, either the product ID (PID) or firmware key, for registering the device in the cloud.

    • The PID is the unique identifier of the product you create on the Tuya IoT Development Platform. It is recommended to configure the firmware using the PID.
    • The firmware key is an 8-character string generated when you create the firmware. Using the firmware key for registration can enable OEM, but it is a complex process. OEM allows you to bind a firmware key with multiple PIDs. The device uses the PID that you assign to the license when you place an order. The generic license does not contain PID information, which does not allow for device registration with the firmware key.
  • version: The firmware version in ASCII, in the format of x.x, with two sets of numbers.

  • mesh_category: The capability value. See Mesh Category.

  • need_publish_addr: Specifies whether the device requires the publish address for local control using products like remotes or wireless switches. See Local Control and Control via Remotes.

  • firmname: The firmware identifier, which must match the one created on the Tuya IoT Development Platform. It will be verified during authorization using Tuya’s host software.

Log initialization

typedef VOID (*TAL_LOG_OUTPUT_CB)(IN CONST CHAR_T *str);
OPERATE_RET tal_log_create_manage_and_init(CONST TAL_LOG_LEVEL_E level, CONST INT32_T buf_len, CONST TAL_LOG_OUTPUT_CB output);

Print standard TuyaOS logs with time and location, supporting logging level, string, and data stream.

  • level: The logging level, defaulting to TAL_LOG_LEVEL_DEBUG.

  • buf_len: The maximum length of the log buffer.

  • output: The exit function for log output, which is specified in the application layer. The demo uses UART0 for logging output by default.

Mesh node initialization

Register the element and model, and associate the model with a specific element.

OPERATE_RET tal_element_register(USHORT_T element_index);
OPERATE_RET tal_model_register(USHORT_T element_index, UINT_T model_id);

For more information about the model ID and data format, see the official Mesh Model Documents. tal_bluetooth_mesh_def.h also includes the definitions and IDs.

Lighting products

Element index Model Description
0 Config Model Device configuration
0 Generic OnOff Model Switch
0 Light Lightness Model Brightness
0 Light CTL Model Brightness and color temperature
0 Light CTL Temperature Model Color temperature
0 Light HSL Model HSL color model
0 Tuya Vendor Model Tuya-specific vendor model

Electrical products

Element index Model Description
0 Config Model Device configuration
0 Generic OnOff Model Switch 1
1 Generic OnOff Model Switch 2
2 Generic OnOff Model Switch 3
3 Generic OnOff Model Switch 4
4 Generic OnOff Model Switch 5
5 Generic OnOff Model Switch 6

Pass-through and other products

Element index Model Description
0 Config Model Device configuration
0 Generic OnOff Model Used to support the heartbeat strategy in legacy protocols.
0 Tuya Vendor Model Tuya-specific vendor model

Mesh data receiving initialization

typedef OPERATE_RET (*tal_mesh_msg_recv_cb)(TAL_MESH_ACCESS_MSG_T *msg_raw, TAL_MESH_NET_PARAM_T *net_param);
OPERATE_RET tal_mesh_msg_recv_cb_init(tal_mesh_msg_recv_cb access_data_cb);

TuyaOS Bluetooth Mesh SDK unifies the handling of multiple mesh data callbacks into a single callback. In the registered callback, you can handle the Opcode data for all registered models. For more information, see the callback implementation in the demo and the mesh DP control in the capability map.

Bluetooth LE data callback registration

typedef VOID(*TAL_BLE_EVT_FUNC_CB)(TAL_BLE_EVT_PARAMS_T *p_event);
OPERATE_RET tal_mesh_ble_recv_cb_init(TAL_BLE_EVT_FUNC_CB ble_event);

In the Bluetooth LE data callback, you can receive Bluetooth LE connection, disconnection, advertising, and GATT (Tuya-specific Bluetooth LE service) data. For more information, see the callback implementation in the demo.

Serial initialization

OPERATE_RET tal_uart_init(TUYA_UART_NUM_E port_id, TAL_UART_CFG_T *cfg);
VOID_T tal_uart_rx_reg_irq_cb(TUYA_UART_NUM_E port_id, TAL_UART_IRQ_CB rx_cb);

How-to

Schedule tasks with loop

Besides the tal_sw_timer software timer, you can schedule tasks using the system tick or other clock APIs in the loop function. If you require precise timekeeping, opt for a hardware timer instead. Here is the example code:

UINT32_T time_exceed(UINT32_T ref, UINT32_T span_ms){
    return ((tkl_system_get_millisecond() - ref) > span_ms);
}

VOID tuya_main_loop(VOID){
    STATIC UINT32_T pre_ms = 0;
    if(time_exceed(pre_ms , 1000)){
        app_process(); // Run the scheduled task.
        pre_ms = tkl_system_get_millisecond();
    }
}

Initialize firmware information

Refer to the implementation of the OPERATE_RET tuya_firmware_config(VOID_T); function in the demo and use the following API to write the firmware configuration into the SDK. The macro definition is generated by the IDE and script. You can either use the IDE for configuration or specify fixed values.

OPERATE_RET tuya_firmware_config(VOID_T)
{
    UINT8_T pid[] = PRODUCTKEY;
    UINT8_T firmware_key[] = PRODUCTKEY;
    UINT8_T firmware_name[] = BUILD_FIRMNAME;
    UINT8_T firmware_version[] = FW_VERSION;
    UINT16_T mesh_category = MESH_CATEGORY;

    tal_mesh_factory_firmware_info_set(firmware_name, firmware_version);
    tal_mesh_gatt_ota_version_set(FW_VERSION_HEX);
    tal_firmware_infor_set(IS_FIRMWARE_KEY, pid, firmware_key, FW_VERSION_HEX, mesh_category, NEED_PUBLISH_ADDR);
    return OPRT_OK;
}

Configure firmware information with IDE

Configure the firmware information such as pairing, firmware verification, and OTA version. The macro definition in the function is generated by the IDE. Open Tuya Wind IDE, right-click on the target application, and choose Config Project to configure the firmware information.

Device Initialization Device Initialization

Instead of using the IDE for configuration, you can write the firmware information to the SDK in the format specified by the IDE.

Turn on/off logging

The demo has logging turned off by default. You can modify #define ENABLE_LOG 1 in tuya_iot_config.h to enable log output using the API in tal_log. Note that components that have been compiled into a library do not have logs. To debug them, contact Tuya’s technical support.

The chip vendor SDK is open source, allowing you to enable logging directly.

  • For TLSR825x, enable LOG_FW_FUNC_EN in app_mesh.h to turn on logging.
  • For PHY6222, enable PHY_LOG_EN in EM_platform.h to turn on logging.

Initialize serial port

TAL_UART_CFG_T tal_uart_cfg = {
    .rx_buffer_size = 256,
    .open_mode = 0,
    .base_cfg = {
#if ENABLE_LOG
        .baudrate = 115200, // Use the baud 115200 for logging output. Prevent logs from being output from baud 9600 and blocking the application.
#else
        .baudrate = 9600, // Note: The baud rate is 9600 by default. The baud on the host software for authorization defaults to 9600.
#endif
        .parity = TUYA_UART_PARITY_TYPE_NONE,
        .databits = TUYA_UART_DATA_LEN_8BIT,
        .stopbits = TUYA_UART_STOP_LEN_1BIT,
        .flowctrl = TUYA_UART_FLOWCTRL_NONE,
    }
};

tal_uart_init(TUYA_UART_NUM_0, &tal_uart_cfg);
tal_uart_rx_reg_irq_cb(TUYA_UART_NUM_0, tuya_uart_irq_rx_cb);

Tuya’s testing tool uses the serial protocol for device authorization and testing.

If you use Tuya’s flashing tool, the baud rate must be 9600.

STATIC VOID_T tuya_uart_irq_rx_cb(TUYA_UART_NUM_E port_num, VOID_T *buff, UINT16_T len)
{
    if(port_num == 0) {
        tal_uart_receive_common_data(buff, len);
    }
    else {

    }
}

OPERATE_RET tal_uart_receive_common_data(UINT8_T *p_data, UINT16_T len) is used to forward the data received from the serial port to the SDK. This allows both testing data and host data to be processed by the corresponding component in the SDK.

SDK testing

TuyaOS Bluetooth Mesh SDK supports testing with the Logic software. You can test Bluetooth, Bluetooth mesh, and peripherals using serial communication.

This feature can be disabled to free up space for a real product.

Change #define TUYA_SDK_TEST 1 to #define TUYA_SDK_TEST 0 to disable the SDK testing feature.