Zigbee protocol stack learning serial port transparent transmission experiment (SerialApp) process analysis
[Copy link]
Function 1: Networking of the coordinator, terminal devices and routing devices discovering the network and joining the network
//Step 1: Z-Stack starts with the main() function. The main() function does two things: one is system initialization, and the other is to start
executing the round-robin query operating system
int main( void )
{
.......
// Initialize the operating system
osal_init_system(); //Step 2: Initialize the operating system
......
osal_start_system(); //After initializing the system task events, officially start executing the operating system
......
}
//Step 2: Enter the osal_init_system() function to execute the operating system initialization
uint8 osal_init_system( void ) //Initialize the operating system, the most important of which is to initialize the operating system tasks
{
// Initialize the Memory Allocation System
osal_mem_init();
// Initialize the message queue
osal_qHead = NULL;
// Initialize the timers
osalTimerInit();
// Initialize the Power Management System
osal_pwrmgr_init();
// Initialize the system tasks.
osalInitTasks(); //The third step is to execute the operating system task initialization function
// Setup efficient search for the first free block of heap.
osal_mem_kick();
return ( SUCCESS );
}
//The third step is to enter the osalInitTasks() function and execute the operating system task initialization
void osalInitTasks( void ) //The third step is to initialize the operating system task
{
uint8 taskID = 0;
tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
//The task priorities are arranged from high to low. The value of taskID corresponding to high priority is smaller
macTaskInit( taskID++ ); //The user does not need to consider
nwk_init( taskID++ ); //The user does not need to consider
Hal_Init( taskID++ ); //Hardware abstraction layer initialization, we need to consider
#if defined( MT_TASK )
MT_TaskInit( taskID++ );
#endif
APS_Init( taskID++ ); //No need for users to consider
#if defined ( ZIGBEE_FRAGMENTATION )
APSF_Init( taskID++ );
#endif
ZDApp_Init( taskID++ ); //Step 4, ZDApp layer, initialization. After executing the ZDApp_init function, if it is a coordinator, a network will be established
. If it is a terminal device, a network will be joined.
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
ZDNwkMgr_Init( taskID++ );
#endif
SerialApp_Init( taskID ); //Application layer SerialApp layer initialization, users need to consider setting a key trigger event here,
//When a key is pressed, a system message is generated
}
//The fourth step, enter the ZDApp_init() function and execute the ZDApp layer initialization
//The first step
void ZDApp_Init( uint8 task_id ) //The first step, ZDApp layer initialization.
{
// Save the task ID
ZDAppTaskID = task_id;
// Initialize the ZDO global device short address storage
ZDAppNwkAddr.addrMode = Addr16Bit;
ZDAppNwkAddr.addr.shortAddr = INVALID_NODE_ADDR;
(void)NLME_GetExtAddr(); // Load the saveExtAddr pointer.
// Check for manual "Hold Auto Start"
ZDAppCheckForHoldKey();
// Initialize ZDO items and setup the device - type of device to create.
ZDO_Init();
// Register the endpoint description with the AF
// This task doesn't have a Simple description, but we still need
// to register the endpoint.
afRegister( (endPointDesc_t *)&ZDApp_epDesc );
#if defined ( ZDO_USERDESC_RESPONSE )
ZDApp_InitUserDesc();
#endif // ZDO_USERDESC_RESPONSE
// Start the device?
if ( devState != DEV_HOLD ) // The initial value of devState is DEV_INIT, so when the ZDA layer is initialized, this conditional
statement
{
ZDOInitDevice( 0 ); //The second step, then go to ZDOInitDevice() function and execute the third step;
}
else
{
// Blink LED to indicate HOLD_START
HalLedBlink ( HAL_LED_4, 0, 50, 500 );
}
ZDApp_RegisterCBs();
} /* ZDApp_Init() */
//The third step, execute ZDOInitDevice() function to perform device initialization
uint8 ZDOInitDevice( uint16 startDelay ) //The third step, ZDO layer initializes the device,
{
.......
// Trigger the network start
ZDApp_NetworkInit( extendedDelay ); //Network initialization, jump to the corresponding function and execute The fourth step
.......
}
//The fourth step, execute the ZDApp_NetWorkInit() function
void ZDApp_NetworkInit( uint16 delay ) //The fourth step, network initialization
{
if ( delay )
{
// Wait awhile before starting the device
osal_start_timerEx( ZDAppTaskID, ZDO_NETWORK_INIT, delay ); //Send ZDO_NETWORK_INIT (network initialization) message to the ZDApp layer, go to //ZDApp
layer, execute the fifth step, ZDApp_event_loop() function
}
else
{
osal_set_event( ZDAppTaskID, ZDO_NETWORK_INIT );
}
}
//The fifth step, go to ZDApp_event_loop() function
UINT16 ZDApp_event_loop( uint8 task_id, UINT16 events )
{
if ( events & ZDO_NETWORK_INIT ) //The fivth step, network initialization event processing
{
/ / Initialize apps and start the network
devState = DEV_INIT;
//Device logic type, startup mode, beacon time, superframe length, then go to The sixth step to start the device, and then execute The sixth step
step, go to ZDO_StartDevice()
ZDO_StartDevice( (uint8)ZDO_Config_Node_Descriptor.LogicalType, devStartMode,
DEFAULT_BEACON_ORDER, DEFAULT_SUPERFRAME_ORDER );
|