当前位置:网站首页>RT thread kernel application development message queue experiment
RT thread kernel application development message queue experiment
2022-06-30 07:18:00 【liwuxing】
Message queuing experiment
The message queuing experiment is in RT-Thread Created in 3 Threads ,2 One is the sending message thread ,1 The first is to get the message thread .3 Threads run independently ,key1 When pressed key1 Threads send messages 1;key2 When pressed key2 Thread connection sends messages 2 common 20 Time ,key2 Threads send more frequently than they receive and print output .Key1、key2 If the thread fails to send a message , Print the returned error information code on the serial port . The receiving thread is used to get the message thread , Wait for messages until the message queue has no messages , Once the message is obtained, it will be printed in the serial port debugging assistant .
In the experiment , Press first key2, Because the sending frequency is greater than the processing rate of the receiving thread , Cause message queue overflow ; Press down key2 after , Press the button key1, May succeed , Or when the message queue is full , An overflow error code also appears .
This code is in Wildfire “ Development board RT-Thread 3.0 + STM32 Message queue ” Routine . The original code is 2 Threads , Here instead 3 Threads , And press key2 Send messages continuously when .
*************************************************************************
* Included header file
*************************************************************************
*/
#include "board.h"
#include "rtthread.h"
/*
*************************************************************************
* Variable
*************************************************************************
*/
/* Define thread control blocks */
static rt_thread_t receive_thread = RT_NULL;
static rt_thread_t send_thread = RT_NULL;
/* Define the message queue control block */
static rt_mq_t test_mq = RT_NULL;
/*
*************************************************************************
* Function declaration
*************************************************************************
*/
static void receive_thread_entry(void* parameter);
static void key1_send_thread_entry(void* parameter);
static void key2_send_thread_entry(void* parameter);
/*
*************************************************************************
* main function
*************************************************************************
*/
int main(void)
{
/*
* Hardware initialization of development board ,RTT System initialization is already in main Complete before function ,
* That is to say component.c In the document rtthread_startup() Function .
* So in main Function , Just create and start threads .
*/
rt_kprintf(" This is a [ Wildfire ]-STM32F103- Guider -RTT Message queuing experiment !\n");
rt_kprintf(" Press down K1 perhaps K2 Send queue message \n");
rt_kprintf("receive The message received by the task is echoed on the serial port \n");
/* Create a message queue */
test_mq = rt_mq_create("test_mq",/* Message queue name */
4, /* Maximum message length */
10, /* Maximum capacity of message queue */
RT_IPC_FLAG_FIFO);/* Queue mode FIFO(0x00)*/
if (test_mq != RT_NULL)
rt_kprintf(" Message queue created successfully !\n\n");
receive_thread = /* Thread control block pointer */
rt_thread_create( "receive", /* Thread name */
receive_thread_entry, /* Thread entry function */
RT_NULL, /* Thread entry function parameters */
512, /* Thread stack size */
3, /* Thread priority */
20); /* Thread timeslice */
/* Start thread , Turn on scheduling */
if (receive_thread != RT_NULL)
rt_thread_startup(receive_thread);
else
return -1;
send_thread = /* Thread control block pointer */
rt_thread_create( "key1send", /* Thread name */
key1_send_thread_entry, /* Thread entry function */
RT_NULL, /* Thread entry function parameters */
512, /* Thread stack size */
2, /* Thread priority */
20); /* Thread timeslice */
/* Start thread , Turn on scheduling */
if (send_thread != RT_NULL)
rt_thread_startup(send_thread);
else
return -1;
send_thread = /* Thread control block pointer */
rt_thread_create( "key2send", /* Thread name */
key2_send_thread_entry, /* Thread entry function */
RT_NULL, /* Thread entry function parameters */
512, /* Thread stack size */
2, /* Thread priority */
20); /* Thread timeslice */
/* Start thread , Turn on scheduling */
if (send_thread != RT_NULL)
rt_thread_startup(send_thread);
else
return -1;
}
/*
*************************************************************************
* Thread definition
*************************************************************************
*/
static void receive_thread_entry(void* parameter)
{
rt_err_t uwRet = RT_EOK;
uint32_t r_queue;
/* The task is an infinite loop , Can't return */
while (1)
{
/* Queue read ( receive ), The waiting time is always waiting */
uwRet = rt_mq_recv(test_mq, /* Read ( receive ) Queued ID( Handle ) */
&r_queue, /* Read ( receive ) Where to save your data */
sizeof(r_queue), /* Read ( receive ) The length of the data */
RT_WAITING_FOREVER); /* Waiting time : Keep waiting */
if(RT_EOK == uwRet)
{
rt_kprintf(" The data received this time is :%x\n",r_queue);
}
else
{
rt_kprintf(" Data receiving error , Error code : 0x%lx\n",uwRet);
}
rt_thread_delay(100);
}
}
static void key1_send_thread_entry(void* parameter)
{
rt_err_t uwRet = RT_EOK;
uint32_t send_data1 = 0x1111;
while (1)
{
if( Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON )/* K1 Pressed */
{
/* Write data to ( send out ) Go to the queue , The waiting time is 0 */
uwRet = rt_mq_send( test_mq, /* write in ( send out ) Queued ID( Handle ) */
&send_data1, /* write in ( send out ) The data of */
sizeof(send_data1)); /* Length of data */
if(RT_EOK != uwRet)
{
rt_kprintf("KEY1 Data cannot be sent to message queue ! Error code : %lx\n",uwRet);
}
}
rt_thread_delay(20);
}
}
static void key2_send_thread_entry(void* parameter)
{
rt_err_t uwRet = RT_EOK;
uint32_t send_data2 = 0x2222;
while (1)
{
if( Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON )/* K1 Pressed */
{
for(int i=0;i<22;i++)
{
/* Write data to ( send out ) Go to the queue , The waiting time is 0 */
uwRet = rt_mq_send(test_mq, /* write in ( send out ) Queued ID( Handle ) */
&send_data2, /* write in ( send out ) The data of */
sizeof(send_data2)); /* Length of data */
if(RT_EOK != uwRet)
{
rt_kprintf("KEY2 Data cannot be sent to message queue ! Error code : %lx\n",uwRet);
}
rt_thread_delay(50);
}
}
rt_thread_delay(20);
}
}
/********************************END OF FILE****************************/
Press first key2, Then press key1, The printout results are shown in the figure below .

边栏推荐
- app闪退
- Linux服务器安装Redis
- Linux服務器安裝Redis
- LabVIEW程序代码更新缓慢
- [datawhale team learning] task02: mathematical operation, string and text, list
- Write and run the first go language program
- Idea running run and services
- QT generate random number qrandomgenerator
- js创建pdf文件
- [semidrive source code analysis] [x9 chip startup process] 34 - RTOS side display module SDM_ display_ Init display initialization source code analysis
猜你喜欢

Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)
![[most complete] install MySQL on a Linux server](/img/5d/8d95033fe577c161dfaedd2accc533.png)
[most complete] install MySQL on a Linux server

The first up Master of station B paid to watch the video still came! Price "Persuading" netizens

QT generate random number qrandomgenerator

Go语言指针介绍

QT common macro definitions
![[docsify basic use]](/img/9d/db689f5f13708f3e241474afeca1d0.png)
[docsify basic use]

IDEA import导入的类明明存在,却飘红?

Raspberry pie trivial configuration

Double click the idea to solve the problem of downloading again
随机推荐
Linux服务器安装Redis
【最全】linux服务器上安装Mysql
Calculation and parameter quantity of neural network
Four great happenings on earth
[semidrive source code analysis] [x9 chip startup process] 34 - RTOS side display module SDM_ display_ Init display initialization source code analysis
2021-07-02
【已解决】MySQL异常:ERROR 1045 (28000): Unknown error 1045,忘记初始密码
Network security - layer 3 switching technology and internal network planning
[resolved] MySQL exception: error 1045 (28000): unknown error 1045, forgetting the initial password
Golan common shortcut key settings
app闪退
Connection flood attack principle
failed to create symbolic link ‘/usr/bin/mysql’: File exists
Can introduction
网络安全-三层交换技术和内部网络规划
Go语言指针介绍
Keil serial port redirection
Cypress nor flash driver - s29glxxxs
Mysql5.7 compressed version installation tutorial
[resolved] error 1290 (HY000): unknown error 1290