Good project, don't keep it private! Open source wheels for microcontroller development

Click on "Uncle Wheat" above and select "Top/Star Public Account"

Welfare dry goods, delivered as soon as possible

Hello everyone, my name is Wheat. Today I recommend an open source wheel suitable for single-chip bare metal development.

QueueForMcu

The queue function module based on single-chip microcomputer is mainly used for 8-bit, 16-bit, and 32-bit single-chip microcomputer applications that do not run RTOS, and is compatible with most single-chip microcomputer platforms.

Open source code: https://github.com/xiaoxinpro/QueueForMcu

1. Characteristics

  • Dynamically create queue objects

  • Dynamically set queue data buffer

  • Statically specify the length of the queue element data

  • Save queue data by passing by value

2. Quick use

#include "queue.h"

#define Q_UART_BUFFER_SIZE  1024

QUEUE_HandleTypeDef qUartTx;
QUEUE_DATA_T BufferUartTx[Q_UART_BUFFER_SIZE];

int main(void)
{
  QUEUE_DATA_T temp;
  
  //初始化队列
  Queue_Init(&qUartTx, BufferUartTx, Q_UART_BUFFER_SIZE);
  
  while(1)
  {
    //入队
    Queue_Push(&qUartTx, 'Q');
    Queue_Push(&qUartTx, 'u');
    Queue_Push(&qUartTx, 'e');
    Queue_Push(&qUartTx, 'u');
    Queue_Push(&qUartTx, 'e');
    
    //出队
    Queue_Pop(&qUartTx, &temp);
    Queue_Pop(&qUartTx, &temp);
    Queue_Pop(&qUartTx, &temp);
    Queue_Pop(&qUartTx, &temp);
    Queue_Pop(&qUartTx, &temp);
  }
}

3. Configuration instructions

Currently QueueForMcu has only one static configuration item, as follows:

There is a macro definition in the queue.hfile QUEUE_DATA_Tto specify the data length of the queue elements, the default is unsigned char, it can be changed to other data types as needed.

Fourth, the data structure

The data structure of the queue is QUEUE_HandleTypeDefused to save the state of the queue. The source code is as follows:

typedef struct QUEUE_HandleTypeDef{
    unsigned int head;                      //队列头指针
    unsigned int tail;                      //队列尾指针
    unsigned int buffer_length;             //队列缓存长度(初始化时赋值)
    QUEUE_DATA_T * buffer;                  //队列缓存数组(初始化时赋值)
}QUEUE_HandleTypeDef;

where QUEUE_DATA_Tis the custom data type in the configuration item.

5. Create a queue

1. Create a queue cache

Since we save the queue data by value passing, we need to manually create a queue buffer to store the queue data before creating the queue.

QUEUE_DATA_T BufferUartTx[1024];

The above code creates 1024a .

2. Create a queue structure

Next, QUEUE_HandleTypeDefcreate a queue structure to hold the state of the queue using:

QUEUE_HandleTypeDef qUartTx;

3. Initialize the queue

After preparing the queue buffer and queue structure, call the Queue_Initfunction to create the queue. The function prototype is as follows:

void Queue_Init(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * buffer, unsigned int len)

Parameter Description:

parameter name describe
hqueue The queue structure that needs to be initialized, if the second initialization will clear the contents of the original queue.
buffer The first address pointer of the queue cache
len The queue length cannot be larger than the queue buffer length.

Reference Code:

Queue_Init(&qUartTx, BufferUartTx, Q_UART_BUFFER_SIZE);

6. Push into the queue

1. Single data push

Use the Queue_Pushfunction . The function prototype is as follows:

QUEUE_StatusTypeDef Queue_Push(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T data)

Parameter Description:

parameter name describe
hqueue The queue structure into which data needs to be pushed.
data Data to be pushed into the queue.

Return value description:

The function will return an QUEUE_StatusTypeDefenumeration data type, and the return value will return the following values ​​according to the queue status:

return value describe
QUEUE_OK The data is pushed into the queue successfully.
QUEUE_OVERLOAD No data was pushed into the queue because the queue is full.

Reference Code:

Queue_Push(&qUartTx, 'Q');
Queue_Push(&qUartTx, 0x51);
Queue_Push(&qUartTx, 81);

2. Multiple data push

If you need to push multiple data (arrays) into the queue, you can use the Queue_Push_Arrayfunction . In principle, Queue_Pushit is implemented by calling the function in a loop. The function prototype is as follows:

unsigned int Queue_Push_Array(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdatas, unsigned int len)

Parameter Description:

parameter name describe
hqueue The queue structure into which data needs to be pushed.
pdatas The first address of the array to be pushed into the queue.
len The length of the array to be pushed into the queue.

When the length of the array is greater than the remaining length of the queue, the excess data in the array will be ignored.

Return value description:

  • This function will return the length of the data actually pushed into the queue.

  • When the remaining length in the queue is surplus, the return value will be equal to lenthe .

  • When the remaining length in the queue is insufficient, the return value is the length of the data actually pushed into the queue.

Seven, pop-up queue

1. Single data popup

Use the Queue_Popfunction . It should be noted that the popped data will be deleted from the queue. The function prototype is as follows:

QUEUE_StatusTypeDef Queue_Pop(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdata)

Parameter Description:

parameter name describe
hqueue Queue structure where data needs to be popped.
pdata A pointer to a variable to hold the pop data.

Return value description:

The function will return an QUEUE_StatusTypeDefenumeration data type, and the return value will return the following values ​​according to the queue status:

return value describe
QUEUE_OK Data popped from the queue successfully.
QUEUE_VOID No data was popped into the queue because the queue was empty.

Reference Code:

QUEUE_DATA_T temp;
if(QUEUE_OK = Queue_Pop(&qUartTx, &temp))
{
    // temp 为队列弹出的数据
}
else
{
    // 弹出数据失败
}

2. Multiple data pop-up

If you need to pop multiple data from the queue, you can use the Queue_Pop_Arrayfunction . In principle, the Queue_Popfunction is called cyclically. It should be noted that the data that is successfully popped will be deleted from the queue. The function prototype is as follows:

unsigned int Queue_Pop_Array(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdatas, unsigned int len)

Parameter Description:

parameter name describe
hqueue Queue structure where data needs to be popped.
pdatas Used to save the first address of the popup data array.
len The length of the data array that needs to be popped.

When the length of the data to be popped is greater than the length of the data in the queue, the extra space of the pop-up array will not be assigned.

Return value description:

  • This function will return the length of the data actually popped from the queue.

  • When the length of data in the queue is sufficient, the return value will be equal to lenthe .

  • When the data length in the queue is insufficient, the return value is the actual data length popped from the queue.

3. Single data copy

When you need to get data from the head of the queue, but you don't want the data to be deleted from the queue, you can use the Queue_Peekfunction to achieve this. The parameters and return values ​​of this function are Queue_Popexactly the same as .

The difference between using Queue_Peekand Queue_Popfunctions is that:

  • Queue_PopAfter getting the data in the queue, the data in the queue will be deleted.

  • Queue_PeekAfter getting the data in the queue, the data in the queue will be retained.

4. Multiple data replication

When you need to get multiple data from the head of the queue, but do not want the data to be deleted from the queue, you can use the Queue_Peek_Arrayfunction to achieve this. The parameters and return values ​​of this function are Queue_Pop_Arrayexactly the same as .

The difference between using Queue_Peek_Arrayand Queue_Pop_Arrayfunctions is that:

  • Queue_Pop_ArrayAfter getting the data in the queue, the data in the queue will be deleted.

  • Queue_Peek_ArrayAfter getting the data in the queue, the data in the queue will be retained.

8. Other functions

1. Clear the queue

When you need to clear the queue data, you don't need to pop up all the data. You only need to call Queue_Clearto quickly clear the specified queue. When the queue is created, this function will be called to initialize the queue, so there is no need to call the clear queue function for the queue that has just been created.

Function prototype:

void Queue_Clear(QUEUE_HandleTypeDef * hqueue)

Parameter Description:

parameter name describe
hqueue The queue structure that needs to be emptied.

2. Get the number of queue data

When the data length in the queue needs to be obtained, the Queue_Countfunction . The function prototype is as follows:

unsigned int Queue_Count(QUEUE_HandleTypeDef * hqueue)

Parameter Description:

parameter name describe
hqueue Queue structure that needs to get the length of the data.

Return value description:

  • This function will return the length of the data in the queue.

  • The return value ranges from 0 to the length of the queue when it was created.


—— The End ——

Recommended in the past

MCU development never uses data structures?

After the person who wrote the bad code left...

I'm stumped, what is the role of C language enumeration end?

30 solutions to common problems with single-chip microcomputers! Normal people I don't tell them

Musk's brain-computer interface can be made with a Raspberry Pi?

Join the embedded technology exchange group and make progress together

Click on the card above to follow me

430ba210c6f4b0b22183354cdcfc1e1c.png

Everything you ordered looks good , I take it seriously as I like it

Guess you like

Origin blog.csdn.net/u010632165/article/details/123415290