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(" } /******************************************************************************** * @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
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
- Popular Resources
- Popular amplifiers
- Algorithm Competition (Volume 2) (Scanned version) (Luo Yongjun and Guo Weibin)
- A Simple Introduction to GAN Generative Adversarial Networks: Principle Analysis and TensorFlow Practice
- Algorithm Notebooks Practical Guide for Computer Training (Edited by Hu Fan and Zeng Lei)
- Introduction to Algorithm Training Camp (Chen Xiaoyu)
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- C2000 Piccolo MCU F280049C LaunchPad Development Kit
- [Top Micro Smart Display Screen Review] 2.1 SGTools Graphics Editing Software - Touch, Variable, Time Control Display
- [National Technology Low Power Series N32L43x Review] 06. Implementing Agile Modbus Master-Slave Communication Based on USART
- PWM control of BLDC six-step commutation based on ATC7801
- What is the difference between LIN bus and UART
- [RVB2601 Creative Application Development] RVB2601 Startup Process
- A new Agilent oscilloscope
- How do you pronounce “5G” in your native dialect?
- Multi-axis motor control design [Comparison of DSP solution and FPGA motor control solution]
- [WPI Live Review on August 30] Application of TI ultra-low power MCU in touch and intelligent industrial gateways (including PPT, QA)