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

边栏推荐
- 编写并运行第一个Go语言程序
- 网络安全-路由原理
- QT wmic command obtains some hardware information
- Introduction to go project directory structure
- Ad usage notes
- IDEA import导入的类明明存在,却飘红?
- How to use string branches for switch case
- [docsify basic use]
- grep命令用法
- Resolved: initialize specified but the data directory has files in it Aborting
猜你喜欢

Out of class implementation of member function of class template

网络安全-ARP协议和防御

Skillfully use 5 keys to improve office efficiency

All errors reported by NPM

Calculation and parameter quantity of neural network

Connection flood attack principle

How to use string branches for switch case

What if I don't know what to do after graduating from university?

Realization of dissolve effect in unity and its principle analysis

Binary tree traversal
随机推荐
踩坑记录:supervisor 日志返回信息:redis扩展未安装
【已解决】ERROR 1290 (HY000): Unknown error 1290
网络安全-三层交换技术和内部网络规划
Essence of signal slot macros signal and slot
QT msvc2015 compiler reports an error: error: lnk1158: unable to run "rc.exe"
Skillfully use 5 keys to improve office efficiency
What if I don't know what to do after graduating from university?
[docsify basic use]
SQL Server2005中SUM函数内嵌套IF语句
Base64 encoding method implemented by native JS
Cypress nor flash driver - s29glxxxs
FreeRTOS timer group
神经网络计算量及参数量
Realization of dissolve effect in unity and its principle analysis
手机开户股票开户安全吗?开户需要准备什么?
Basic knowledge of system software development
15 minutes learn to use JWT
[resolved] error 1290 (HY000): unknown error 1290
MAX6675 usage notes
网络安全-单臂路由、DHCP中继和ICMP协议