Firmware guide for Mesh light applications based on the TG7100B chip

更新时间:
复制 MD 格式

This topic describes how to develop firmware for a Bluetooth Mesh smart light device based on the TG7100B chip using the light_ctl application example from the Bluetooth Mesh SDK.

Background information

The light_ctl application example provides the following features.

  • Complies with the Bluetooth Mesh Module Software Specifications and the Bluetooth Mesh Device Extension Protocol. It supports network provisioning and control through the Tmall Genie speaker and Tmall Genie app for Tmall Genie ecosystem products. It also supports network provisioning and control through the Cloud Intelligence app for private label products.
  • Supports control of the light's on/off state, brightness, color temperature, and scene modes.
  • Supports device over-the-air (OTA) updates through IoT Platform and the Tmall Genie app. This feature is currently available only for Tmall Genie ecosystem projects.

TG7100B overview

The TG7100B is a cost-effective Bluetooth chip customized by Tmall Genie for Bluetooth Mesh access. It features a simple circuit design, excellent radio frequency (RF) performance, low power consumption, and an automotive-grade temperature range of -40 °C to 125 °C. For documents and software tools related to the TG7100B chip, see TG7100B.

Note

For issues related to the TG7100B chip, such as drivers, production testing, hardware design, and RF, contact technical support through your business representative. You can also obtain support for application development using the IoT Platform Bluetooth Mesh SDK, including product configuration, network provisioning, cloud connectivity, and OTA updates.

Compile and flash the firmware

  1. Download the SDK. For the download link, see Get the SDK. Make sure that you download TG7100B SDK V1.3.4.
  2. Configure the development environment. For more information, see Prepare the development environment.
  3. Compile the firmware. For more information, see Develop device firmware.
    Note
    • Generated flashing file: out/bluetooth.light_ctl@tg7100b/binary/total_image.hexf
    • Generated OTA file: out/bluetooth.light_ctl@tg7100b/binary/fota.bin
  4. Flash the firmware. For more information, see Flash the firmware.
  5. Flash the device certificate.
    • Disconnect the TG71XX Programmer.exe flashing tool, set the DIP switch on the development board to GND, and press the reset button on the board.
    • Open the SecureCRT serial debugging tool, select File > Quick Connect, and configure the serial port parameters for the development board as shown in the following figure. The default baud rate is 512000.
      Note The port number must be the same as the one that is automatically detected by the TG71XX Programmer.exe tool. You can also find the port number by right-clicking This PC and selecting Manage > System Tools > Device Manager > Ports (COM & LPT). The path to Device Manager may vary based on your operating system.
      串口连接
    • Click Connect.
    • Enter the following command to flash the device certificate.
      set_tt <ProductID> <Device Secret> <Device Name>
      Note The Product id, Device Secret, and Device Name (MAC address) in this command are the device certificate credentials that are generated when you add a device.

Application model configuration

For more information about Mesh Models, see Mesh Model guide for Bluetooth Mesh light applications. The light_ctl application example in the Bluetooth Mesh SDK implements control only for the on/off state, brightness, color temperature, and scene modes. The following table lists the Thing Specification Language (TSL) models and Attribute Types that are supported by default in the firmware.

ElementNameModelAttribute TypeAttribute ParameterNotes
Light (Primary element)On/OffGeneric OnOff Server 0x1000N/AN/ARequired
LightnessLightness Server 0x1300Optional
Color TemperatureLight CTL Server 0x1303Optional
ModeScene Server 0x1203Optional
Error CodeVendor Model Server 0x01A800000x00001 byteOptional
On/Off0x01001 byte: 0 for Off, 1 for OnRequired. The status is consistent with the on/off status of the Generic OnOff Server Model. It is used for the device to proactively report its on/off status.
Lightness0x01212 bytes: 0 to 65535Optional. The status is consistent with the lightness status of the Lightness Server Model. It is used for the device to proactively report the lightness property.
Color Temperature0x01222 bytes: 800 to 20000Optional. The status is consistent with the color temperature status of the Light CTL Server. It is used for the device to proactively report the color temperature property.
Mode0xF0042-byte enumerationOptional. The status is consistent with the Scene Server. It is used for the device to proactively report the mode property.
Event0xF0091 byte
  • 0x0003 Power-on event
  • 0x0023 Hardware reset event
Timed On/Off0xF010VariableOptional
Time Zone0xF01E1 byte: -12 to 12Optional
Time0xF01F4 bytes: standard UNIX timeOptional

The code that corresponds to the model configuration is in the app/example/bluetooth/light_ctl/light_ctl.c file, as shown below.

/* SIG general-purpose model definitions in the light product */
static struct bt_mesh_model primary_element[] = {
    BT_MESH_MODEL_CFG_SRV(),     /* Configuration Server */
    BT_MESH_MODEL_HEALTH_SRV(),  /* Health Server */

  MESH_MODEL_GEN_ONOFF_SRV(&light_elem_stat[0]),    /* On/Off Generic OnOff Server */
  MESH_MODEL_LIGHTNESS_SRV(&light_elem_stat[0]),    /* Lightness Server */
  MESH_MODEL_CTL_SRV(&light_elem_stat[0]),          /* Color Temperature Light CTL Server */
  MESH_MODEL_SCENE_SRV(&light_elem_stat[0]),        /* Scene Mode Scene Server */
};
/* Vendor-defined model definitions */
static struct bt_mesh_model primary_vendor_element[] = {
  MESH_MODEL_VENDOR_SRV(&light_elem_stat[0]),
};

/* Register the SIG general-purpose models and vendor-defined models for the main element of the light. GENIE_ADDR_LIGHT is defined as the multicast address 0xC000 for the light category. */
struct bt_mesh_elem light_elements[] = {
  BT_MESH_ELEM(0, primary_element, primary_vendor_element, GENIE_ADDR_LIGHT),
};
Note All models for the light must be bound to the multicast address 0xC000 (the multicast address for the light category, as specified in the preceding example) and 0xCFFF (the multicast address for all products, which is implemented by default in the Mesh protocol component of the Mesh SDK and does not need to be specified). For more information about multicast address definitions, see Device multicast addresses

Application layer event handling

The code for application layer event handling is located in the app/example/bluetooth/light_ctl/light_ctl.c file, as shown below.

static void light_ctl_event_handler(genie_event_e event, void *p_arg)
{
  switch (event)
  {
  case GENIE_EVT_SW_RESET:                            /* Software reset */
  {
    light_param_reset();
    light_led_blink(3, 1);
    aos_msleep(3000);
  }
  break;
  case GENIE_EVT_MESH_READY:                          /* The Mesh protocol stack is ready. Data can be sent and received. */
  {
    //User can report data to cloud at here
    GENIE_LOG_INFO("User report data");
    light_report_poweron_state(&light_elem_stat[0]);
  }
  break;
  case GENIE_EVT_SDK_MESH_PROV_SUCCESS:               /* Network provisioning successful */
  {
    light_led_blink(3, 0);                          /* The light blinks to indicate successful network provisioning. */
  }
  break;
#ifdef CONFIG_MESH_MODEL_TRANS
  case GENIE_EVT_USER_TRANS_CYCLE:
#endif
  case GENIE_EVT_USER_ACTION_DONE:                    /* Light effect transition finished */
  {
    sig_model_element_state_t *p_elem = (sig_model_element_state_t *)p_arg;
    light_update(p_elem);    
    if (event == GENIE_EVT_USER_ACTION_DONE)
    {
      light_save_state(p_elem);
    }
  }
  break;
  case GENIE_EVT_SIG_MODEL_MSG:                      /* Received a downstream SIG Model message */
  {
    sig_model_msg *p_msg = (sig_model_msg *)p_arg;

    if (p_msg)
    {
      GENIE_LOG_INFO("SIG mesg ElemID(%d)", p_msg->element_id);
    }
  }
  break;
  case GENIE_EVT_VENDOR_MODEL_MSG:                   /* Received a downstream Vendor Model message */
  {
    genie_transport_model_param_t *p_msg = (genie_transport_model_param_t *)p_arg;

    if (p_msg && p_msg->p_model && p_msg->p_model->user_data)
    {
      sig_model_element_state_t *p_elem_state = (sig_model_element_state_t *)p_msg->p_model->user_data;
      GENIE_LOG_INFO("ElemID(%d) TID(%d)", p_elem_state->element_id, p_msg->tid);
    }
  }
  break;
#ifdef CONFIG_GENIE_MESH_USER_CMD                     /* Used to implement a custom serial protocol */
  case GENIE_EVT_DOWN_MSG:
  {
    genie_down_msg_t *p_msg = (genie_down_msg_t *)p_arg;
    //User handle this msg,such as send to MCU    /* Here, you can forward data to the MCU based on a custom serial protocol. */
    if (p_msg)
    {
    }
  }
  break;
#endif
#ifdef MESH_MODEL_VENDOR_TIMER
  case GENIE_EVT_TIMEOUT:                           /* Entry point for handling local TimerOnOff timeouts */
  {
    vendor_attr_data_t *pdata = (vendor_attr_data_t *)p_arg;
    //User handle vendor timeout event at here
    if (pdata)
    {
      light_ctl_handle_order_msg(pdata);        /* The light On/Off operation is performed here. */
    }
  }
  break;
#endif
  default:
    break;
            

Serial command description

The following serial commands can be used for development and debugging.

Command NameDescriptionExample
set_ttBluetooth Mesh device certificateset_tt 5297793 0c51b11c6ec78b52b803b3bbaae64fba 486e704a5bf6
get_ttView the Bluetooth Mesh device certificate.No parameters
get_infoView information such as the version and MAC address.No parameters
rebootRestart the system.No parameters
resetReset the device.No parameters
mesgSend Mesh data.mesg D4 1 F000 000101
Note Description of the mesg command parameters:
  • The first parameter, D4, is the abbreviation for the first byte of the Opcode for the Vendor Message Attribute Indication message. Other values include D3, CE, and CF.
  • The second parameter specifies the sending mode and number of retries:
    • 0: No retries.
    • 1-252: The number of retries.
    • 253: Uses the first byte of the payload as the time interval parameter, in units of 100 ms. For example, `mesg D4 253 F000 030201` sends 0201 every 300 ms, and `mesg D4 253 F000 1e0201` sends 0201 every 3 seconds.
    • 254: Resends the message when a reply is received or a timeout occurs.
    • 255: Sends the message automatically once per second.
  • The third parameter is the destination address. It must be four characters long. If this parameter is set to 0000, the Mesh gateway multicast address F000 is used by default.
  • The fourth parameter is the content to send. For example, 000101 sends 0x00, 0x01, and 0x01. The content must be an even number of hexadecimal characters (0-9, a-f).

Firmware macro definitions

User-configurable macros are defined in the app/example/bluetooth/light_ctl/light_ctl.mk and genie_service/genie_service.mk files. The following table describes the important macros.
Macro nameDescription
CONFIG_BT_MESH_GATT_PROXYEnables the Proxy feature.
CONFIG_BT_MESH_PB_GATTEnables network provisioning using a mobile phone.
CONFIG_BT_MESH_RELAYEnables the relay feature.
CONFIG_GENIE_OTAEnables the OTA feature for mobile phones.
CONFIG_GENIE_RESET_BY_REPEATEnables the feature to enter network provisioning mode by powering the device on and off five consecutive times.
PROJECT_SW_VERSIONConfigures the version number, which is used for the OTA feature. The data type is int32.
CONFIG_PM_SLEEPEnables the low-power feature.
CONFIG_GENIE_MESH_GLPEnables the low-power feature in GLP mode.
CONFIG_DEBUGEnables BT_xxx log output.
CONFIG_BT_DEBUG_LOGEnables BT_DBG log output.
MESH_DEBUG_PROVEnables log output for network provisioning.
MESH_DEBUG_TXEnables log output for sending Mesh data at the Access layer.
MESH_DEBUG_RXEnables log output for receiving Mesh data at the Access layer.