I. Introduction
Starting today, we officially begin our ZigBee-related communication experiments. The protocol stack we are using is ZStack, specifically the TI ZStack-CC2530-2.3.0-1.4.0 version. You can also download this protocol stack directly from Texas Instruments' official website. While it's true that the company provides the code for the CC2530, as a beginner, there's no need to dive deep into the internal workings of the protocol stack. ZigBee is a mature technology, and our focus should be on learning how to use the ZigBee protocol stack effectively for real-world applications such as smart furniture. I believe many of you share the same experience, so let me walk you through the principles and architecture of ZigBee communication.
2. ZStack Architecture
The ZStack architecture is organized into layers, each providing specific services to the layer above. For instance, the data service entity handles data transmission, while the management entity manages other control functions. Each service entity communicates with its upper layer through a Service Access Point (SAP), which uses service primitives to perform specific tasks.
According to IEEE 802.15.4 and ZigBee standards, ZStack is divided into the physical layer, medium access control (MAC) layer, network layer, and application layer. The physical layer handles basic data transmission and reception, while the network layer ensures nodes are connected, making it a crucial part of ZigBee communication. The application layer is where we focus most, as it includes the application framework and the ZigBee Device Object (ZDO). For more details, you can refer to the official documentation. Let me now explain how ZigBee works.
ZStack is designed with an operating system-like approach, utilizing an event-driven round-robin mechanism and a dedicated Timer2 for timing. On the CC2530, Timer2 repeatedly triggers tasks like data acquisition, transmission, reception, and display. After each layer is initialized, the system enters low-power mode. When an event occurs, it wakes up, processes the interrupt, and then returns to low-power mode. If multiple events happen simultaneously, they are prioritized and processed one by one. This design significantly reduces power consumption.
As shown in the diagram, the main workflow of ZStack consists of six steps:
- Disable all interrupts
- Initialize the chip (onboard peripherals)
- Internal chip initialization
- Initialize the operating system
- Enable all interrupts
- Execute the operating system
This is the general process of ZigBee task handling.
Code Analysis
The protocol stack code is extensive and complex, but the figure gives a good overview.
This is just a small part of the entire stack, so we don’t need to focus too much on the setup. Instead, we should concentrate on learning how to use it. The main file we’ll configure is SampleApp.c, which is used for application development. For different applications, we simply modify this file and the corresponding configuration files. It contains six key functions that are essential for our work. Let’s take a closer look at them:
Void SampleApp_Init( uint8 task_id )
This function is used to initialize a task. It is called during the system initialization phase, and it should include any necessary user-level initialization, such as hardware setup, data table initialization, and power management.
Uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
This is the event handler, responsible for managing all events—such as timers, messages, and custom events. It is triggered by the main function when an event occurs. Its responsibilities include processing button presses, receiving data, and sending data. You can also add your own custom events here.
Void SampleApp_HandleKeys( uint8 shift, uint8 keys )
This function is called when a key is pressed, typically triggered by the KEY_CHANGE case in SampleApp_ProcessEvent. It allows us to define what happens when a specific button is pressed, enabling custom actions based on the key pressed.
Void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
This function handles all incoming data. It determines the source device based on the cluster ID. Inside the function, there is a switch statement where each case corresponds to a specific cluster ID. Users can define their own cluster IDs in SampleApp.h and add corresponding cases here to handle custom data.
Void SampleApp_SendPeriodicMessage( void )
This function is used for sending periodic messages, usually when the device is broadcasting. You can set the content of the message here.
Void SampleApp_SendFlashMessage( uint16 flashTime )
This is a helper function used by SampleApp_HandleKeys. It’s rarely used, so we won’t go into detail here.
These six functions are commonly used in ZigBee development. For beginners, understanding and modifying these functions is sufficient to get started. First, transplant the driver and initialize the hardware. Then, adjust these six functions to implement your ZigBee application. Today, I’ve shared my experience, and I encourage you to reach out and learn together. This is just my personal journey, and I hope it helps you along the way.
Outdoor Battery,Outdoor Powder Coating,Battery Rack,Integrated Rack
Guangdong Yuqiu Intelligent Technology Co.,Ltd , https://www.cntcetltd.com