Zstack Serial Port

Source: Internet
Author: User
Tags sleep valid




The serial port receives sends the data to have two kinds of ways, one is the interrupt mode, the other is the DMA way, here mainly in the interruption way, looks at uses the serial port to send, receives the data the entire process. Here is an example of the Serialapp routine.

Call the Haldriverinit () function in the Mian function, initialize the serial port in the function, mainly configuring the PIN and DMA channel

void Haldriverinit (void)

{

...................................

#if (defined Hal_uart) && (Hal_uart = = TRUE)

Haluartinit ();

#endif

....................................

}

From the program can be seen to use the protocol stack in the serial port, the initialization of the serial port must be defined Hal_uart and Hal_uart TRUE in the Hal_board_cfg.h file.

#ifndef Hal_uart

#if (defined zapp_p1) | | (defined zapp_p2) | | (defined ZTOOL_P1) | | (defined ZTOOL_P2)

#define Hal_uart TRUE

#else

#define Hal_uart FALSE

#endif

#endif

Then, after the Osal_start_system () system is started, Hal_processpoll () is called to read the time and the serial port.

There is a passage in the CC2430 data sheet.

Data Reception on the UART is Initiatedwhen a 1 are written to the uxcsr.re bit

The UART would then search for a valid start bit on the RXDX input pin and set the

uxcsr.active bit High. When a validstart bit had been detected the received byte is shifted into the receive register. The Uxcsr.rx_byte bit is set and a receive interrupt are generated when the operation have completed. The received data byte is available through the UXBUF register. When Uxbuf was read, Uxcsr.rx_byte was cleared by hardware.

When there is data received, the UXCSR.RE bit will be set to 1, then the UART will find a valid start bit on the input pin of the RXDX, and when this start bit is found, the uxcsr.active bit will be the high level. When a valid start bit is found, the received bytes are moved to the receive register. The Uxcsr.rx_byte bit is then set to 1. Also, the receive interrupt is generated when the receive operation is complete. The received data can be manipulated by manipulating the UXBUF register, and when the data of the UXBUF register is read out, the Uxcsr.rx_byte bit is removed by the hardware. The interruption of the serial port interrupts first call the processing function, this is the interrupt function received.

#if hal_uart_0_enable

Hal_isr_function (HALUART0RXISR, Urx0_vector)

{

Cfg0->rxbuf[cfg0->rxhead] = u0dbuf;

if (Cfg0->rxhead = = Cfg0->rxmax)

{

Cfg0->rxhead = 0;

}

Else

{

cfg0->rxhead++;

}

}

#endif

The interrupt function mainly refers to the U0DBUF register, which is the register that receives the data, reads the data into the structure of the UART, cfg0->rxbuf[], in which the memory allocation of the array is in the Haluartopen () function.

The following definitions are in the SERIALAPP.C

#if!defined (Serial_app_rx_max)

#if (defined (HAL_UART_DMA)) && HAL_UART_DMA

#define SERIAL_APP_RX_MAX 128

#else

#define SERIAL_APP_RX_MAX 64

#endif

#endif

The Serialapp_init () function has the following assignment,

UartConfig.rx.maxBufSize = Serial_app_rx_max;

The Haluartopen () function has the following assignment: so its cfg->rxmax=128,

Cfg->rxmax = config->rx.maxbufsize;

Where Rxhead This parameter always points to a position like a parameter being stored in RXBUF. Because the hardware serial buffer u0dbuf can only hold one byte, if not in time to transfer this received, then will be overwritten by the next byte, so the Rxhead variable points to the address of the store, of course, is based on the definition of rxbuf storage space.

and if (Cfg0->rxhead = = Cfg0->rxmax) This sentence also illustrates very clearly, once this count reaches the definition of the maximum number of received, that is, the RXBUF storage space has been filled, then can not continue to store.

After the interrupt function is executed, it should jump to the place where the interrupt occurred, then the program continues execution, and after Osal_start_system () starts the system, it loops through the hal_processpoll () to read the time and the serial port.

void Hal_processpoll ()

{

Haltimertick ();

#if (defined Hal_uart) && (Hal_uart = = TRUE)

Haluartpoll ();

#endif

}

The following is the Haluartpoll (); The source code of the function, where there is a procedure for processing the data received by the docking.

void Haluartpoll (void)

{

#if (hal_uart_0_enable | hal_uart_1_enable)

Static Uint8 TICKSHDW;

uartcfg_t *cfg;

Uint8 tick;

#if hal_uart_0_enable

When a serial receive interrupt occurs, the CFG0 will change, if the serial port does not have data input cfg0 is empty, when the data received cfg0 will be changed in the Serial Interrupt service program

if (CFG0)

{

CFG = cfg0;

}

#endif

#if hal_uart_1_enable

if (CFG1)

{

CFG = CFG1;

}

#endif

Use the LSB of the sleep timer (ST0 must is read first anyway).

After the system is power up, the sleep timer will automatically start to do the self-increment count ST0 that the sleep timer starts to the lowest 8 bits of the current calculated value

tick = ST0-TICKSHDW;

TICKSHDW = ST0;

Here is an infinite loop

Do

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.