[MCU Framework] [queue] Implement a simple message queue

Publisher:SparklingMelodyLatest update time:2022-09-19 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Here’s how to use it:


#define USB_RECV_Q_ITEM_CNT 8

#define USB_RECV_Q_ITEM_SIZE (64 + 1)

 // Total buffer area for USB message queue

static uint8_t m_usb_recv_q_buff[USB_RECV_Q_ITEM_CNT * USB_RECV_Q_ITEM_SIZE] = {0};

queue_t m_usb_recv_q =

{

    .pbuff = m_usb_recv_q_buff,

    .front = 0,

    .rear = 0,

    .item_cnt = USB_RECV_Q_ITEM_CNT,

    .item_size = USB_RECV_Q_ITEM_SIZE

};

uint8_t g_usb_recv_data[USB_RECV_Q_ITEM_SIZE] = {0}; // Temporary buffer for USB messages


//Function usage:

/**

 * @brief usb sends data to usb custom channel (message queue, non-real-time sending)

 * @note NULL

 * @param *data: content to be sent

 * @param len: content length (byte size)

 * @retval None

 */

void biz_usb_send_data(uint8_t *data, uint16_t len)

{

    queue_en(&m_usb_recv_q, data);

}


/**

 * @brief Process messages in the message queue and send them via USB

 * @note NULL

 * @retval None

 */

void biz_usb_data_dispose(void)

{

    if (!queue_de(&m_usb_recv_q, g_usb_recv_data))

        return;

    usb_send(g_usb_recv_data, MAX_USB_PACKET_SIZE);

    // LOG_D(" [usb_send] %02x %02x %02x %02x %02xrn", g_usb_recv_data[0], g_usb_recv_data[1], g_usb_recv_data[2], g_usb_recv_data[3], g_usb_recv_data[4]);

}


/********************************************************************************

* @file queue.c

* @author jianqiang.xue

* @Version V1.0.0

* @Date 2021-10-10

* @brief message queue borrows from nrf

* @example 

#define RECV_Q_ITEM_CNT (8)

#define RECV_Q_ITEM_SIZE (20)

//Total message queue buffer

static uint8_t m_recv_q_buff[RECV_Q_ITEM_CNT * RECV_Q_ITEM_SIZE] = {0};

// Define the queue message structure

queue_t m_recv_q =

{

    .pbuff = m_recv_q_buff,

    .front = 0,

    .rear = 0,

    .item_cnt = RECV_Q_ITEM_CNT,

    .item_size = RECV_Q_ITEM_SIZE

};

// Temporary buffer for message queue items

uint8_t g_recv_data[USB_RECV_Q_ITEM_SIZE] = {0};


// Data is queued (data length is RECV_Q_ITEM_SIZE)

queue_en(&m_recv_q, data);


// Data dequeue (return if the queue is empty)

if (!queue_de(&m_recv_q, g_recv_data))

    return;

************************************************************************************/

/* Includes ------------------------------------------------------------------*/

#include

#include

#include

#include


/* Private includes ----------------------------------------------------------*/

#include "queue.h"


/* Private function prototypes -----------------------------------------------*/

/**

 * @brief [message queue] initialization

 * @note NULL

 * @param q: queue information pointer

 * @param data: queue buffer

 * @param item_cnt: total number of queues

 * @param item_size: total queue size

 * @retval 1--success 0--failure

 */

bool queue_init(queue_t *q, uint8_t *data, uint32_t item_cnt, uint32_t item_size)

{

    q->pbuff = data;

    q->item_cnt = item_cnt;

    q->item_size = item_size;

    q->front = 0;

    q->rear = 0;

    return true;

}


/**

 * @brief [message queue] Join the queue

 * @note NULL

 * @param q: queue information pointer

 * @param data: data header pointer

 * @retval 1--success 0--failure

 */

bool queue_en(queue_t *q, uint8_t *data)

{

    if (((q->rear+1)%q->item_cnt) == q->front)

        return false;

    

    memcpy(q->pbuff + (q->rear*q->item_size), data, q->item_size);

    q->rear = (q->rear+1)%q->item_cnt;


    return true;

}


/**

 * @brief [message queue] out of the queue

 * @note NULL

 * @param q: queue information pointer

 * @param data: get the current queue information data head pointer

 * @retval 1--success 0--failure

 */

bool queue_de(queue_t *q, uint8_t *data)

{

    if (q->front == q->rear)

        return false;


    if (data)

    {

        memcpy(data, q->pbuff + (q->front * q->item_size), q->item_size);

    }

    q->front = (q->front + 1) % q->item_cnt;

    return true;

}


/**

 * @brief [message queue] Read the current first queue information

 * @note NULL

 * @param q: queue information pointer

 * @retval The first queue information head pointer

 */

uint8_t* queue_read(queue_t *q)

{

    if (q->front == q->rear)

        return NULL;


    return q->pbuff + (q->front * q->item_size);

}


/********************************************************************************

* @file queue.h

* @author jianqiang.xue

* @Version V1.0.0

* @Date 2021-10-10

* @brief message queue borrows from nrf

************************************************************************************/


#ifndef __QUEUE_H__

#define __QUEUE_H__


/* Includes ------------------------------------------------------------------*/

#include

#include


/* Public Struct -----------------------------------------------*/


typedef struct 

{

uint8_t *pbuff;

uint32_t front;

uint32_t rear;

    uint32_t item_cnt;

    uint32_t item_size;

} queue_t;


/* Public function prototypes -----------------------------------------------*/

bool queue_init(queue_t *q, uint8_t *data, uint32_t item_cnt, uint32_t item_size);

bool queue_en(queue_t *q, uint8_t *data);

bool queue_de(queue_t *q, uint8_t *data);

uint8_t* queue_read(queue_t *q);


#endif

Reference address:[MCU Framework] [queue] Implement a simple message queue

Previous article:[MCU framework][OS layer] RTX4 middleware public functions
Next article:[keil5] Compile HEX file, modify the starting address, the HEX file address remains unchanged

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号