Zigbee / Bluetooth wireless module design, how hard do you know?

Professor Zhou Ligong's new book, "Programming for Ametal Frameworks and Interfaces (I)," provides a comprehensive introduction to the AMetal framework. By reading this book, readers can learn advanced software design principles and development ideas for interface programming. The focus is on mastering your own "core domain," transforming your programming mindset, and achieving mutual growth between the company and the individual. Authorized by Professor Zhou Ligong, the Zhiyuan Electronic Public Number will now serialize the content of the book, sharing it with you.

The ninth chapter focuses on the BLE & Zigbee wireless module. Specifically, this article discusses section 9.2: the Zigbee core board.

9.2 Zigbee Core Board

The AW824P2EF is a core board developed by Guangzhou Zhiyuan Electronics Co., Ltd. based on LPC824 + JN5161, supporting the Fastzigbee networking protocol and allowing user-based secondary development. JN5161 is a Zigbee chip provided by NXP Semiconductors, operating within the IEEE802.15.4 standard ISM band (2.4-2.5 GHz). One of the key features of this module is its complete software and hardware ecosystem, making it ideal for applications in industrial control, data acquisition, agricultural control, mining personnel positioning, smart homes, and remote controls.

> > > 9.2.1 Product Introduction

The AW824P2EF core board has the following features:

  • Working voltage: 2.1V ~ 3.6V
  • Maximum transmission power: 20dBm
  • Maximum receiving sensitivity: -95dBm
  • Integrated Zigbee serial port transparent transmission
  • ARM Cortex-M0+ processor with 8KB SRAM and 32KB Flash, supporting 12-bit ADC, SPI, I2C, and UART.

The AW824P2EF core board integrates the complex communication protocols of wireless products into the built-in MCU, significantly simplifying the development process of wireless products. Users can configure and transparently send and receive data through the serial port. The board has 35 pins, with pin distribution shown in Figure 9.6 and pin function descriptions in Table 9.6.

Table 9.6 AW824P2EF Core Board Pin Function Description

Figure 9.6 AW824P2EF Pinout Diagram

To facilitate rapid development, serial port 1 (PIO0_26 and PIO0_27) of the LPC824 is connected to the serial port of the built-in Zigbee chip in the AW824P2EF, and PIO0_28 is connected to the reset pin of the ZM5161. See Figure 9.7 for details. When using secondary development of the AW824P2EF, PIO0_26 of the LPC824 must be configured as the serial port RX function, and PIO0_27 as the serial port TX function.

Figure 9.7 Schematic Diagram of Hardware Connection

ZB_RST is the reset pin of the ZM5161. When the PIO0_28 pin outputs a low-level signal greater than 1μs, the core board can be reliably reset.

In the AW824P2EF, the Zigbee core board defaults to the long-term industrial application experience of Guangzhou Zhiyuan Electronics Co., Ltd., which is a self-developed Zigbee protocol stack suitable for various industrial applications: Fastzigbee. To distinguish it from the original chip JN5161, the Zigbee module is named ZM5161.

> > > 9.2.2 Network Application

Alongside the Fastzigbee protocol, the ZM516x series modules support other multi-industry wireless protocol stacks such as Zigbee Pro, Zigbee Pro Home Automation, Zigbee Pro Smart Energy, Zigbee Pro Light Link, Zigbee RF4CE, and JenNet-IP. The AW824P2EF Zigbee module runs the Fastzigbee protocol stack by default. Fastzigbee has the following characteristics:

  • Excellent device startup speed, response speed, and data transmission efficiency
  • Network capacity of up to 65,535 terminal nodes
  • Terminal node power consumption as low as 100nA, lower than 100% of current Zigbee modules
  • Support for multi-level relay functions, with self-adjusting and self-repairing network features
  • Support for multiple remote I/O and remote ADC, with short address functionality and customizable user applications
  • Larger link budget

Using the robust Fastzigbee network transparent transmission protocol equipped with the ZM516X module, various network topologies can be constructed. Its biggest features are strong practicality, high transmission efficiency, reliable performance, simple secondary development, flexible engineering distribution, and a network topology diagram of Fastzigbee is shown in Figure 9.8.

Figure 9.8 Fastzigbee Network Topology Diagram

The terminal node of Fastzigbee is responsible for data acquisition from the sensing device, typically using battery-powered intermittent operation, requiring low power consumption. The routing node is responsible for signal relay, and when the terminal node signal cannot directly reach the gateway node, the routing node relays the signal. The routing node also provides multiple signal paths to the terminal node to ensure signal transmission robustness, and the routing node cannot sleep. The Fastzigbee gateway node uploads the data collected by the terminal node to the cloud server. Gateway nodes can use wired Ethernet or wireless 3/4G networks to transmit data to the cloud server. There are several important parameters to configure for the Fastzigbee network.

Channel number

The channel number determines the radio frequency used by the Zigbee network. Zigbee can operate at 2.4 GHz (globally popular), 868 MHz (popular in Europe), and 915 MHz (popular in the US). The 2.4 GHz band uses a frequency range from 2405 MHz to 2480 MHz and is divided into 16 channels. Channel numbers range from 11 to 26, with a center frequency interval of 5 MHz. All nodes of the same Zigbee network must work on the same channel. By distributing two different Zigbee networks on different channels, two separate networks can be physically isolated, eliminating wireless interference between them.

2. Node type

The Fastzigbee network divides Zigbee nodes into two types: terminal nodes and routing nodes. Terminal nodes are responsible for specific functions and need to sleep. Routing nodes are responsible for relaying signals. When the signal between terminal nodes is unreachable, routing nodes can be added to realize signal relaying, increasing wireless transmission distance. The Fastzigbee network is a peer-to-peer network. All terminal nodes and routing nodes are peer-to-peer. They can send and receive data to each other. There is no need for a coordinator to establish a network like traditional Zigbee networks. The peer-to-peer network makes the network more complete, simple, stable, and reliable.

3. PanID

PanID is the network ID number of Zigbee. It distinguishes two different Zigbee networks. Unlike channel numbers, PanID only logically distinguishes two networks. If two different PanID networks work under the same channel, they can still cause mutual wireless interference.

4. Network address

All nodes under the same Zigbee network have a unique 16-bit address that identifies each node and addresses the data to be sent and received.

5. Data transmission mode

Zigbee data transmission mode is divided into unicast and broadcast modes. Unicast specifies a target network address when sending data. Only the node with the target network address can receive this data. Broadcast sends data, and all other nodes in the same network can receive this data.

> > > 9.2.3 Zigbee Initialization

The AMetal platform already supports the ZM516X module, which allows users to directly configure and send/receive data related to network parameters using the corresponding API. Users do not need to care about the underlying communication protocol. Initialization must be completed before using each function. The function prototype (am_zm516x.h) is:

This function is intended to get the instance handle of the ZM516X module, where p_dev is a pointer to an instance of type am_zm516x_dev_t, p_devinfo is a pointer to the instance information of the am_zm516x_devinfo_t type, and uart_handle is the serial port handle used to communicate with the Zigbee module.

Instance

An example of defining the am_zm516x_dev_t type (am_zm516x.h) is as follows:

Where g_zm516x_dev is a user-defined instance whose address is passed as an argument to p_dev.

Instance information

The instance information mainly describes the information related to the Zigbee module. The definition of the type am_zm516x_devinfo_t (am_zm516x.h) is as follows:

Where rst_pin represents the reset pin of the module, so that the program resets the Zigbee module through this pin when needed. In the AW824P2EF, the Zigbee reset pin ZB_RST is connected to PIO0_28 of the LPC824, so the value of rst_pin should be assigned to PIO0_28.

To improve the efficiency of data processing and ensure that the received data is not lost due to ongoing transactions, the data transmission and reception of the Zigbee module requires a buffer for buffering the data. The actual size of the buffer is specified by the user according to the actual situation, usually set to 256 bytes. p_txbuf and txbuf_size describe the first address and size of the transmit buffer, while p_rxbuf and rxbuf_size describe the first address and size of the receive buffer. For example, define a buffer of 256 bytes for sending and receiving:

Where g_zm516x_txbuf[128] is a user-defined array space used as a send buffer, and its address (array name g_zm516x_txbuf or first element address &g_zm516x_txbuf[0]) is used as the value of the p_txbuf member in the instance information, and the array size is used as the value of the txbuf_size member in the instance information. Similarly, g_zm516x_rxbuf[256] acts as a receive buffer with the address as the value of the p_rxbuf member in the instance information and the array size as the value of the rxbuf_size member in the instance information. Based on the above information, the instance information can be defined as follows:

Where g_zm516x_devinfo is a user-defined instance information whose address is passed as an argument to p_devinfo.

UART handle uart_handle

If USART2 of LPC824 is used to communicate with ZM516x, the UART handle is obtained as the argument of uart_handle through the USART2 instance initialization function am_lpc82x_usart2_inst_init() of LPC82x, which is:

Example handle

The return value of the ZM516X initialization function am_zm516x_init() is the handle of the ZM516X instance, which will be used as the argument to the zm516x_handle parameter of other function interface functions.

Its type am_zm516x_handle_t type (am_zm516x.h) is defined as follows:

If the return value is NULL, the initialization fails; if the return value is not NULL, a valid handle is returned. Based on the modular programming idea, the definitions of initialization-related instances, instance information, etc., are stored in the ZM516X configuration file (am_hwconf_zm516x.c), and the instance initialization function interface, source file, and header file are extracted through the header file (am_hwconf_zm516x.h). The program examples are detailed in Listing 9.34 and Listing 9.35.

Listing 9.34 ZM516X Instance Initialization Function Implementation (am_hwconf_zm516x.c)

Listing 9.35 ZM516X Instance Initialization Function Declaration (am_hwconf_zm516x.h)

Subsequent only need to use the parameterless instance initialization function to get the instance handle of ZM516X. Which is:

> > > 9.2.4 Zigbee Configuration Interface

AMetal provides 10 ZM516X module configuration-related interface functions, users can directly use these interface functions to complete the configuration of the Zigbee module, as shown in Table 9.7.

Table 9.7 ZM516X Module Configuration Interface Functions

1. Read the local configuration

This function is used to read the information of the current permanent configuration parameters. The function prototype is:

Among them, p_info is a pointer for obtaining configuration information, and am_zm516x_cfg_info_t is a type of configuration information structure, which contains information about all permanent configuration parameters of the ZM516X module. For details, see Listing 9.36.

Listing 9.36 ZM516X Permanent Configuration Information Structure

A detailed description of each parameter is given in Table 9.8.

Table 9.8 Description of permanent configuration parameters of the ZM516X module

See Appendix 9.37 for a sample program for reading the ZM516X local configuration.

Listing 9.37 reads the ZM516X local configuration sample program

2. Modify the local configuration

This function is used to modify the information of the current permanent configuration parameters. The modified configuration information will not be lost after power failure. The function prototype is:

Where p_info is a pointer to the configuration information. After the function is executed, if you want the configuration parameters to take effect, you need to execute the module reset function to reset the module, let the module reload the new configuration parameters, and modify the sample configuration of the local configuration. See Listing 9.38.

Listing 9.38 Modifying the ZM516X Local Configuration Sample Program

3. Module reset

This function is used to control the ZM516X module to generate a hardware reset. The function prototype is:

The reset function allows the user to perform a reset operation on the module. If the user uses the am_zm516x_cfg_info_set() function to modify the configuration, the module reset function needs to be executed to reload the new parameter after the module is reset. The sample program for module reset is detailed in Listing 9.39.

Listing 9.39 Module Reset Sample Program

4. Restore factory settings

This function is used to restore the permanent parameters of the ZM516X module to the factory default parameters. The function prototype is:

The sample program for module factory reset is detailed in Listing 9.40.

Listing 9.40 Factory Reset Sample Program

5. Set the channel number

This function is used to temporarily change the channel number of the ZM516X module during system operation. The function prototype is:

The channel number set by this function is only valid temporarily. After the module is restarted (power-down restart or software reset), the setting will be lost and the module will reuse the channel number in the permanent parameter configuration information. The sample program for setting the module channel number is shown in Listing 9.41.

Listing 9.41 Setting the module channel number sample program

6. Set the destination address

This function is used to temporarily change the destination address of the ZM516X module during system operation. The function prototype is:

Where p_zb_addr is a pointer to the address of the Zigbee module of the target node, and the type am_zm516x_addr_t is defined as follows (am_zm516x.h):

Where p_addr points to the buffer of the network address stored in bytes, and addr_size specifies the length of the address. If the target address is 0x2002, its Zigbee module address can be defined as follows:

The destination address set by this function is only valid temporarily. After the module is restarted, the setting will be lost and the module will reuse the destination address in the permanent parameter configuration information. A sample program for setting the module's destination address is shown in Listing 9.42.

Listing 9.42 Setting the module destination address sample program

7. Set the header display

The ZM516X module provides a transparent data transmission channel. If only two modules communicate, there is no need to care which module the received data is sent from, but if it is receiving data from multiple modules, the user wants to know that the current data is received. The data is sent from which module, you can use this function to set the source of the received data. The function prototype is:

Where flag is the header display flag. When the value is TRUE, it indicates that when the module receives one frame of data, the first two bytes of the data packet are the network address of the source node of the data packet, and the user can distinguish the currently received one. From which module the data is sent; conversely, if the value is FLASE, the first 2 bytes are not added to indicate the network address of the source node of the packet. This setting is only temporarily valid, and the set information will be lost after the module is restarted. The sample program for setting the module header display is shown in Listing 9.43.

Listing 9.43 Setting the module header display sample program

8. Go to sleep

This function is used to put the ZM516X module into hibernation to reduce power consumption. The function prototype is:

After the module enters sleep, the temporary parameter configuration is not saved, and the module can be woken up by resetting the module function. The sample program that puts the module into hibernation is detailed in Listing 9.44.

Listing 9.44 Bringing the module into the sleep sample program

9. Set the communication mode

The ZM516X module supports both unicast (default) and broadcast communication modes. This function can be used to change the communication mode used. The function prototype is:

Where mode represents the communication mode, its type am_zm516x_comm_mode_t is an enumerated type, enumerating all possible values, am_zm516x_comm_mode_t is defined as follows:

The communication mode set by this function is only valid temporarily, and will be lost after the module is restarted. The sample program for setting the module communication method is shown in Listing 9.45.

Listing 9.45 Setting the module communication method sample program

10. Read signal strength

This function is used to read the signal strength between the node of the specified address and the local node, and is used to evaluate the quality of the link between the two nodes. The function prototype is:

Where *p_zb_addr is a pointer to the address of the Zigbee module of the target node, and p_signal is used to get the signal strength. An example program for reading the signal strength of a module is shown in Listing 9.46.

Listing 9.46 Read module signal strength sample program

> > > 9.2.5 Zigbee Data Transmission Interface

The data transmission interface realizes transparent transmission of data, and the data transmission includes transmission and reception of data, and the interface thereof is shown in Table 9.9.

Table 9.9 ZM516X Data Transfer Interface Function (am_zm516x.h)

3. Send data

The ZM516X module provides a transparent channel to the user after the parameters are configured. The user only needs to send data to the serial port of the ZM516X module, and the module will send the data to the configured destination address. AMetal provides a special send data interface function. The user only needs to call the interface to complete the sending of user data. The function prototype is:

The ZM516X module send function calls the serial port send function with a circular queue. The length of the ring queue is defined in the driver initialization function. The transmitted data pointer is defined as void *, and the user can send any type of data of the specified length.

The sample program for sending data is detailed in Listing 9.47.

Listing 9.47 Send Data Sample Program

4. Receive data

AMetal also provides a special receiving data interface function, the user only needs to call the interface function to complete the reception of user data. The function prototype is:

The receiving function calls the serial port receiving function with a circular queue. The user needs to define the length of the circular queue in the driver initialization function according to the needs of the system. The pointer to the receive function's data is defined as void *, which can be used to place the received data in any type of data cache. The sample program for receiving data is detailed in Listing 9.48.

Listing 9.48 Receive Data Sample Program

One of the modules has a local network address of 0x2001, a target network address of 0x2002, another module with a local network address of 0x2002, and a target network address of 0x2001. The two modules send data once every 1s, and then receive the data of the other party. The data is printed out, as detailed in Listing 9.49.

Listing 9.49 Two modules send and receive data sample programs to each other

In the program, the data receiving function receiving timeout time of the ZM516X module is 10ms. After snd_tick is added to 100, the data sending function is called once after the time is accumulated to 1s, and the data is sent to the target node.

The above application configures the local network address to 0x2001 and the target network address to 0x2002. The address of the other module is exactly the opposite, so the program of another module needs to modify the program list 9.49 (20 ~ 23) as follows:

> > > 9.2.6 Application Case

AM824ZB is the Zigbee secondary development evaluation board developed by Guangzhou Zhiyuan Electronics Co., Ltd. based on AW824P2EF. The evaluation board integrates a variety of experimental circuits, such as watchdogs, buzzers, digital temperature sensors, thermistors, buttons, etc., to facilitate user interactive experiments using Zigbee for wireless communication.

The AM824ZB development kit includes two AM824ZB development boards, a MiniCK100 emulator, and two antennas for long-distance networking applications. The schematic diagram of the AM824ZB development board is shown in Figure 9.9. The main control core is AW824P2EF. For details, please refer to the website of Guangzhou Zhiyuan Electronics Co., Ltd. ().

Figure 9.9 AM824ZB Development Board Interface Distribution

For complete information, please refer to (Guangzhou Zhiyuan Electronics Co., Ltd.) and (Guangzhou Zhou Ligong Microcontroller Technology Co., Ltd.) website. For samples, please contact the local offices.

Based on the two AM824ZB development boards in the development kit, you can do a simple application: control the flipping of the LED0 status of the other party through independent buttons. Each time the button is pressed, the status of the other LED0 changes (from lit to off). Or from off to light).

1. Application writing

To implement this application case, as a simple example, we define that when the button is pressed, a string "key_pressed" is sent to the target node, and when the target node receives the "key_pressed" string, it flips LED0.

For the two modules, although the logic of the application is exactly the same, in the networking application, each node must be assigned a different network address, for example, their addresses are set to 0x2001 and 0x2002 respectively. To do this, you need to write a generic function that implements the core application logic. The differences (for example, local address and destination address) are specified by parameters. See Listing 9.50 for details.

Listing 9.50 Example of an application that implements LED control using Zigbee (app_led_control.c)

Here, firstly, according to the parameters, the configuration of the local address and the communication destination address is completed. After the configuration is completed, the setting is made effective by the module reset, and then the key is pressed in the main loop of the while(1), and when the button is pressed, the string "key_pressed" is sent, and then the data is received. If "key_pressed" is received, the state of the local LED0 is toggled.

To make it easier for the main program to use, declare its interface to the app_led_control.h file, as shown in Listing 9.51.

Listing 9.51 Application Interface Declaration (app_led_control.h)

2. Main program writing

For the sake of distinction, the two boards are called A board and B board respectively. The network address of the A board is 0x2001, and the target address is 0x2002. See Listing 9.52 for details.

Listing 9.52 A board (network address 0x2001) main program

The network address of the B board is 0x2002, and the destination address is 0x2001. See Listing 9.53 for details.

Program Listing 9.53 B board (network address 0x2002) main program

Ethernet Network Connectors

Ethernet Network Connectors,Ethernet Cable Adapter,Ethernet Cable Connector,Internet Cable Connector

Dongguan Zhuoyuexin Automotive Electronics Co.,Ltd , https://www.zyx-fakra.com