Analysis of serial port buffer management

Source: Internet
Author: User


First, overview:

The serial port usually contains two buffers, that is, the send buffer and receive buffer. When sending data, the data is sent to a buffer and then sent via a serial port, and the received data is received before being read.

Reasonable and appropriate use of buffer, not only can make the normal communication between different devices, but also help save memory, improve efficiency.

Second, buffer allocation management:

Method One:

Implemented through memory pools

1. Data structure:

struct _CHN_POOL_MGR

{

U8 BUFFER[BUF_SZ];

U32 Free_bitmap;

};

Parameter meaning: struct _chn_pool_mgr: Data type of the memory pool

Buffer: buffers, size BUF_SZ 192

Free_bitmap: Sign bit.

Note: The buffer is divided into several blocks, each block size BLK_SZ

Free_bitmap the free block and the used block in the flag buffer block, 1 means idle, and 0 means to be used

Note: The shaded part indicates that the data is stored

Free_bitmap initialized to (1 << (sizeof (chn_pool_mgr.buffer)/BLK_SZ)-1 i.e. (1 << (2^10/2^6))-1, binary number 11111 ... 11111B, total 16 1, buffer block all idle

The Alloc_a_slot () function allocates a buffer block: detects the Free_bitmap value, assigns an idle buffer block to a smaller chunk, and returns the label of the allocated buffer block.

Free_bitmap value

Allocation of buffer block markings

Alloc_a_slot () return value

1111...111111

0

0

1111...111100

2

2

1111...110010

1

1

1111...000100

2

2

Not too clear ....

struct _chn_slot

{

S16 TX;

S16 Rx;

/*the current Count of this channel * *

S16 data_cnt;

S16 Data_max;

};

Parameter meaning: struct _chn_slot: A structure that records the read-write state of the buffer

Tx: Record buffer block markings and where the data is written (as shown in the figure)

Rx: Record buffer block marking and data reading location (as shown in the figure)

DATA_CNT: The amount of data not read in the record buffer

Data_max: The maximum amount of data allowed in the buffer when writing data to the buffer

Tx,rx Data meaning:

2, example analysis

1 writes data to the buffer:

Write data to the buffer, write 90 bytes at a time, write two times.

Initial state:

Assume that each parameter in the struct _CHN_SLOT structure is an initial state: tx = RX = Invalid_ptr, that is (Invalid_blk_no << blk_no_shift), data_cnt is 0,data_ MAX is Uart_max_len

The status of the memory pool is as follows:

Write Data:

Alloc_a_slot () Allocation buffer block:

Detection of free_bitmap, allocation of buffer block labeled 2 block;

TX Records buffer block marking and writing data location (0x80);

The last byte of the buffer block is placed to invalid_blk_no;

Into the following states:

Write 90 data:

Because the > blk_sz-1 (the maximum number of bytes in a block holds data), the Alloc_a_slot () allocation buffer block is called again

Detection of free_bitmap, allocation of buffer block labeled 4 block;

TX Records buffer block marking and writing data location (0x100);

The last byte of the buffer block is placed to invalid_blk_no;

In addition, the last byte of the block labeled 2 is recorded as the label (4), and the remaining data is written, TX records the data position (0X11B).

After the write is complete, the parameter states are as follows:

Second Write data:

Similar to the above, the buffer block label and data position of the TX record continue to be written backwards.

Eventually becomes the following state:

3 reading data from the buffer

Read data from the buffer, read 40 at a time, and finish reading.

Suppose the buffer state at this time, and the parameters are as follows:

DATA_CNT is 180

At this point, the data is read, the RX records the buffer label and the data location, reads 40 data successfully, and then changes to:

Continue to read the data, labeled 2 of the buffer block in the data read, from the last byte of the block to know the next Buffer block label, RX Record, at this point, the status of the parameters are as follows:

Continue reading, data_cnt to 0, read end.

Summary:

From the above analysis, the use of the memory pool method, through detection free_bitmap can make the buffer by the use of multiple tasks, save space.

Method Two:

Using cyclic queues to implement

1. Data type:

struct _chn_slot

{

int TX, RX;

U8 Buf[buffer_len];

};

Parameter meaning:

TX: Record Buffer Write location

Rx: Record Buffer read location

BUF: Buffer

2, the method realizes:

Each byte is deposited, TX is moved one bit, each byte is taken away, the RX moves a

When TX moves to the end of the buffer, if the buffer head is read, TX continues to hold the data in the header as follows:

When (Rx + 1)% Buffer_len = TX, the buffer is full

Comparison of the two methods:

Compare content

Method One

Method Two (circular queue type)

Number of tasks using buffers

Allow multiple tasks

Only single task

Space utilization

High

Low

The order in which buffers are used

Priority use of buffer blocks at low address

Loop from low address to high address

The number of bytes in the data that cannot be stored

Number of buffer blocks

1 b

Complexity of programming

Slightly complicated

Simple

Note: Method one cannot hold data in bytes to record the position of the next buffer block; Loop queue, the number of bytes that cannot hold data used to recycle buffers

One of the biggest benefits of a method is that it can be used together by multiple tasks, do not affect, help save memory, and each time allocate space, will give priority to use low address of free block, dataset, help reduce memory pool occupy, the space freed by a task can be used by another task, improve utilization, but programming is slightly complex.

Loop-Queue programming is simple, easy to understand, especially for the use of a single task, but the utilization of the buffer is not very high, and can not multitask use.

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.