当前位置:网站首页>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 .

边栏推荐
- Linu foundation - zoning planning and use
- Linux服务器安装Redis
- Install go language development tools
- [resolved] error 1290 (HY000): unknown error 1290
- TC397 QSPI(CPU)
- next InitializeSecurityContext failed: Unknown error (0x80092012) - 吊销功能无法检查证书是否吊销。
- 嵌入式测试流程
- Linux server installation redis
- QT wmic command obtains some hardware information
- 我今年毕业,但我不知道我要做什么
猜你喜欢

QT elementary notes

Go项目目录结构介绍

Keil plug-in Usage Summary

Daemon and user threads

系统软件开发基础知识

Linux server installation redis

Resolution: div failed to get keyboard event

How crazy are young people in sideline industry: 3000 monthly salary and 3W sideline income

踩坑记录:supervisor 日志返回信息:redis扩展未安装

Realization of dissolve effect in unity and its principle analysis
随机推荐
Linu foundation - zoning planning and use
What does the real name free domain name mean?
Xshell transfer file
Vs2019 and SQL
Merge: extension click the El table table data to expand
QT elementary notes
【已解决】MySQL异常:ERROR 1045 (28000): Unknown error 1045,忘记初始密码
How crazy are young people in sideline industry: 3000 monthly salary and 3W sideline income
Stm32g0 Tim interrupt use
TC397 QSPI(CPU)
Linux server installation redis
Connection Flood攻击原理
[solved] failed! Error: Unknown error 1130
Install go language development tools
03 - programming framework: Division of application layer, middle layer and driver layer in bare metal programming
安装Go语言开发工具
Egret P2 pit encountered by physical engine (1)
动态内存管理
Class template case - encapsulation of array classes
If I am in Zhuhai, where can I open an account? In addition, is it safe to open a mobile account?