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

Prof. Zhou Ligong's new book "Programming for Ametal Frameworks and Interfaces (I)" introduces the AMetal framework in detail. By reading this book, you can learn highly multiplexed software design principles and development ideas for interface programming. Focus on your own "core domain", change your own programming thinking, and achieve common progress between the company and the individual. Authorized by Professor Zhou Ligong, from now on, the Zhiyuan Electronic Public Number will serialize the contents of the book, and is willing to share it.

The ninth chapter is the BLE&zigbee wireless module . The content of this article is 9.2 zigbee core board.

9.2 zigbee core board

AW824P2EF is a core board developed by Guangzhou Zhiyuan Electronics Co., Ltd. based on LPC824+JN5161 to support Fastzigbee networking protocol and secondary development of users. JN5161 is a zigbee chip provided by NXP Semiconductors, and its supported frequency band is IEEE802. 15.4 Standard ISM (2.4-2.5 GHz). The biggest feature of this module is its complete software and hardware ecosystem, so it can be quickly applied to industrial control, data acquisition, agricultural control, mining personnel positioning, smart home and smart remote control.

> > > 9.2.1 Product Introduction

The features of the AW824P2EF core board are as follows:

Working voltage 2.1V~3.6V;

The maximum transmission power is 20dbm;

Maximum receiving sensitivity -95dbm;

Built-in zigbee serial port transparent transmission;

ARM Cotex-M0+ processor with 8 KB SRAM and 32 KB Flash, supporting 12-bit ADC, SPI, I2C and UART.

The AW824P2EF core board integrates the extremely complex communication protocol of wireless products into the built-in MCU, greatly simplifying the complex development process of wireless products. Users can configure and transparently send and receive data to the core board through the serial port. The AW824P2EF core board has a total of 35 pins, the pin distribution is shown in Figure 9.6, and the pin function description is shown in Table 9.6.

Table 9.6 AW824P2EF Core Board Pin Function Description

Figure 9.6 AW824P2EF pinout diagram

In order to facilitate rapid development, the serial port 1 (PIO0_26 and PIO0_27) of the LPC824 has been 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 the secondary development of the AW824P2EF, the PIO0_26 of the LPC824 needs to be configured as the serial port RX function, and the PIO0_27 is configured 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 1us, 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

In addition to supporting the Fastzigbee protocol, the ZM516x series modules can 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, JenNet-IP, etc. The AW824P2EF zigbee module runs the Fastzigbee protocol stack by default. Fastzigbee has the following characteristics:

Equipment startup speed, response speed, and data transmission efficiency are outstanding;

The number of network capacity terminal nodes really reaches 65,535;

The power consumption of the terminal node is as low as 100nA, which is lower than the current 100% zigbee module;

Support multi-level relay function, the network has self-adjusting, self-repairing and other features;

Support multiple remote I/O and remote ADC, support short address function, free to modify with user application;

Have a larger link budget.

Using the robust Fastzigbee network transparent transmission protocol network equipped with the ZM516X module, it can construct a variety of network topology structures. Its biggest features are strong practicability, high transmission efficiency, reliable and stable performance, simple secondary development, and engineering. The distribution network is flexible, and the network topology diagram of Fastzigbee is shown in Figure 9.8.

Figure 9.8 Fastzigbee network topology diagram

Fastzigbee's terminal node is responsible for the data acquisition of the sensing device, generally using battery-powered intermittent operation, requiring low power consumption of the device; Fastzigbee's routing node is responsible for signal relay, when the terminal node signal can not directly reach the gateway node, routing The node is responsible for relaying the signal of the terminal node. The routing node also has a function of providing multiple signal paths to the terminal node to ensure the robustness of the signal transmission, and the routing node cannot sleep; the Fastzigbee gateway node is responsible for uploading the data collected by the terminal node to the cloud. Servers, gateway nodes can use a wired Ethernet network or a wireless 3/4G network to transmit data to the cloud server. There are several important parameters to configure for the Fastzigbee network.

Channel number

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

2. Node type

The Fastzigbee network divides zigbee nodes into two types: terminal nodes and routing nodes. The terminal node is the node responsible for performing the specific function, and the node needs to sleep; the routing node is responsible for relaying the signal. When the signal between the terminal nodes is unreachable, the routing node can be added to realize the signal relay, and the wireless transmission distance is increased. . 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 to have a coordinator to establish a network like a traditional zigbee network. The peer-to-peer network makes the network more complete. Simple, stable and reliable.

3. PanID

PanID is the network ID number of zigbee. PanID can distinguish two different zigbee networks. Unlike channel numbers, PanID only logically distinguishes two networks. If two different PanID networks work in Under the same channel, it will also 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

The zigbee data transmission mode is divided into unicast and broadcast modes. Unicast is to specify a target network address when sending data. Only the node of the target network address can receive this data. The broadcast is a node sending 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 can directly configure and send and 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 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 the module needs to be reset. In the AW824P2EF, zigbee's reset pin ZB_RST is connected to PPC0_28 of LPC824, so the value of rst_pin should be assigned to PIO0_28.

In order to improve the efficiency of data processing and ensure that the received data is not lost due to the transaction being processed, 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. Above 256 bytes, it is generally set to 256 bytes. P_txbuf and txbuf_size describe the first address and size of the transmit buffer, and p_rxbuf and rxbuf_size describe the first address and size of the receive buffer. For example, define a buffer of 256 bytes in size for sending and receiving:

Where g_zm516x_txbuf[128] is a user-defined array space for use 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 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 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

MTP and MPO Cable Assembly

MTP And MPO Cable Assembly,Optical Patch Cable,Fibre Optic Cable Assembly,Optical Jumper

Huizhou Fibercan Industrial Co.Ltd , https://www.fibercannetworks.com

Posted on