ZigBee Development (13) – Network Experiment Titting Stack

Experimental phenomenon: The module sends “HELLO WEBEE through the serial port! < span class="fontstyle2">“print it to the computer serial debugging assistant. The entire experiment was carried out in the protocol stack (TI z-stack 2.5.1a). Use the link in the previous section to download

The whole routine is very simple. It is divided into three steps. In fact, there are three sentences, but we can understand the specific principle: the code is not good. , If you want to live longer, let’s watch the tutorial. The steps are as follows:
  1, serial port initialization
  2, registration task number
  3, serial port sending

We open the Z-stack directoryProjects\zstack\Samples\ SamplesAPP\CC2530DB< span class="fontstyle0">Sa inside mpleApp.eww project. This experiment is directly based on the SampleApp of the protocol stack.
< /p>

After opening the project, you can see that the previous section talked about workspace < span class="fontstyle0">The two more important folders under the directory, Zmain and App. Here we mainly use App, which is also where users add their own code. span>

< span class="fontstyle0">mainly in SampleApp.c and SampleApp.h It’s fine, < /span>

< span class="fontstyle2">first Step: Serial port initialization
  Serial port initialization is very familiar, that is, configure the serial port number, baud rate, flow control, parity and so on. Previously, the registers were configured and then used.
span>

< span class="fontstyle0">   now we are in workspace Find the hal_uart of HAL\Target\CC2530EB\drivers .c file, we can It is very convenient to see that it has included serial port initialization, sending, receiving and other functions.
Looking at the operation functions of the serial port is quite complete, but the good things are yet to come! ! ! On the MT layer on the workspace , I found that there are many basic functions with MT. Including MT_UART.C, let’s open this file.
span>

< span class="fontstyle2"> See the MT_UartInit() function, there is also a serial port initialization function, Z-stack There is a MT layer, and users can choose the MT layer to configure and call other drivers. Further simplifies the operation process. Then add the initialization function under the file of SampleApp . span> span>

< span class="fontstyle2">< span class="fontstyle0">Open the APPdirectory “>OSAL_SampleApp.C file, find the osalInitTasks() task initialization function mentioned in the previous section SampleApp_Init () function, span> span>

< span class="fontstyle0">Enter This function was found in the file SampleApp.c. Add serial port initialization code here span> span>

/* *********Serial port initialization *************/
MT_UartInit ();

Enter MT_UartInit();, modify the initialization configuration you want,The code is as follows.

/************************************** ************************************************** ***********
* @fn MT_UartInit
*
* @brief Initialize MT with UART support
*
* @param None< br /> *
* @return None
********************************** ************************************************** **************
*/
void MT_UartInit ()
{
halUARTCfg_t uartConfig;

/* Initialize APP ID */
App_TaskID
= 0;

/* UART Configuration */
uartConfig.configured < /span>= TRUE;
uartConfig.baudRate
= MT_UART_DEFAULT_BAUDRATE;
uartConfig.flowControl < /span>= MT_UART_DEFAULT_OVERFLOW;
uartConfig.flowControlThreshold
= MT_UART_DEFAULT_THRESHOLD;
uartConfig.rx. maxBufSize
= MT_UART_DEFAULT_MAX_RX_BUFF;
uartConfig.tx.maxBufSize
= MT_UART_DEFAULT_MAX_TX_BUFF;
uartConfig.idleTimeout
= MT_UART_DEFAULT_IDLE_TIMEOUT;
uartConfig.intEnable
= TRUE;
#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
uartConfig.callBackFunc
= MT_UartProcessZToolData;
#elif defined (ZAPP_P1) || defined (ZAPP_P2)
uartConfig.callBackFunc
= MT_UartProcessZAppData;
#else
uartConfig.callBackFunc
= NULL;
#endif

/* Start UART */ span>
#if defined (MT_UART_DEFAULT_PORT)
HalUARTOpen (MT_UART_DEFAULT_PORT,
&uartConfig);
#else
/* Silence IAR compiler warning */
(
void)uartConfig;
#endif

/* Initialize for ZApp */
#if defined (ZAPP_P1) || defined (ZAPP_P2)
/* Default max bytes that ZAPP can take */
MT_UartMaxZAppBufLen
= 1;
MT_UartZAppRxStatus
= MT_UART_ZAPP_RX_READY;
< span style="color: #0000ff">#endif

< br />}

uartConfig.baudRate = MT_UART_DEFAULT_BAUDRATE; is the configuration Baud rate, we go to definition of MT_UART_DEFAULT_BAUDRATE,You can see:
    #define MT_UART_DEFAULT_BAUDRATE HAL_UART_BR_38400
  The default baud rate is 38400bps,Now we modify it to 115200bps, as follows:
    #define MT_UART_DEFAULT_BAUDRATE HAL_UART_BR_115200
uartConfig. flowControl = MT_UART_DEFAULT_OVERFLOW; The statement configures flow control, we can see when we enter the definition:
  #define MT_ UART_DEFAULT_OVERFLOW TRUE
   defaults to enable serial flow control, if you only connect to TX/RX 2 The way of root line must be closed flow control, just like the function bottom board.
  #define MT_UART_DEFAULT_OVERFLOW FALSE strong>
Note: 2 root The line communication connection must be closed flow control, otherwise it will never be able to send and receive information.
span>

< span class="fontstyle2">according to the pre-defined ZTOOL or ZAPP choose not The same data processing function. The following P1 and P2 are serial ports0 < span class="fontstyle0">and serial port1. I use ZTOOL, serial port 0. span> span>

< span class="fontstyle0">< span class="fontstyle0">can be used in option——C/C++ CompilerPreprocessor Inside you see ,has been added by defaultZTOOL_P1 Pre-compiled.
Share a picture
span>
span>

Step 2: Register the task number
   in SampleApp_Init();Add a statement below the serial port initialization statement just added:
  

MT_UartRegisterTaskID(task_id);//Register task number

means to pass serial port events through task_id Register at SampleApp_Init();

Step 3: Serial port transmission span>

After the previous two steps, the serial port can now send information. We added a power-on prompt Hello World after the initialization code just added.

HalUARTWrite(0,"Hello World \n",12); (Serial port 0,'character', the number of characters.) 

< span class="fontstyle0">Tip: Need to be in< span class="fontstyle2">SampleApp.c Add the header file sentence to this file: #include “MT_UART.h < /span>

Connect the emulator and USB to serial cable,Select CoordinatorEB,click to download and debug. Running at full speed, you can see that the serial port assistant receives information. span>

< span class="fontstyle0">but we found Hello World There is a small piece of garbled code behind. This is the serial port sending format defined by the Z-stack MT layer, as well as the LCD prompt information. The solution is:
Place the MT < span class="fontstyle0"> and LCD related content annotations. For example:
  ZTOOL_P1
  xMT_TASK
  xMT_SYS_FUNC
  xMT_ZDO_FUNC
  xLCD_SUPPORTED=DEBUG
xMT_TASK:means there is no definitionMT_TASK , which is not defined anymore. The other items also use this method. We recompile and download the modified ones, press the reset key, and observe that the serial port has been There is no garbled code.
span>

Expand:
Let’s do another test in the protocol stack, in osal_start_system() function for(;;)Add: HalUARTWrite(0 ,”Hello,WeBee\n”,12);as shown in 3.37, after downloading and running, it is found that the serial port keeps receiving Hello WeBee.
span>
span>

< span class="fontstyle2">这就证明了前一节的协议栈运行后会在这个函数里不停地循环查询任务、执行任务。 注意这只是一个演示用的方法,实际应用中可千万不能有把串口发送函数弄到这个位置然后给 PC 发信息的想法,

因为这破坏了协议栈任务轮询的工作原则,相当于我们普通单片机不停用 Delay 延时函数一样,是极其低效的。

分享图片

/**********串口初始化***************/
MT_UartInit ();

/************ ***************************************************************************************
* @fn MT_UartInit
*
* @brief Initialize MT with UART support
*
* @param None
*
* @return None
**************************************************************************************************
*/
void MT_UartInit ()
{
halUARTCfg_t uartConfig;

/* Initialize APP ID */
App_TaskID
= 0;

/* UART Configuration */
uartConfig.configured
= TRUE;
uartConfig.baudRate
= MT_UART_DEFAULT_BAUDRATE;
uartConfig.flowControl
= MT_UART_DEFAULT_OVERFLOW;
uartConfig.flowControlThreshold
= MT_UART_DEFAULT_THRESHOLD;
uartConfig.rx.maxBufSize
= MT_UART_DEFAULT_MAX_RX_BUFF;
uartConfig.tx.maxBufSize
= MT_UART_DEFAULT_MAX_TX_BUFF;
uartConfig.idleTimeout
= MT_UART_DEFAULT_IDLE_TIMEOUT;
uartConfig.intEnable
= TRUE;
#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
uartConfig.callBackFunc
= MT_UartProcessZToolData;
#elif defined (ZAPP_P1) || defined (ZAPP_P2)
uartConfig.callBackFunc
= MT_UartProcessZAppData;
#else
uartConfig.callBackFunc
= NULL;
#endif

/* Start UART */
#if defined (MT_UART_DEFAULT_PORT)
HalUARTOpen (MT_UART_DEFAULT_PORT,
&uartConfig);
#else
/* Silence IAR compiler warning */
(
void)uartConfig;
#endif

/* Initialize for ZApp */
#if defined (ZAPP_P1) || defined (ZAPP_P2)
/* Default max bytes that ZAPP can take */
MT_UartMaxZAppBufLen
= 1;
MT_UartZAppRxStatus
= MT_UART_ZAPP_RX_READY;
#endif

}

MT_UartRegisterTaskID(task_id);//登记任务号

HalUARTWrite(0,”Hello World\n”,12); (串口 0, ‘字符’,字符个数。 )

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1250 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.