RTOS2-keil RTX5使用笔记1

头文件包含

#include “RTE_Components.h”
#include CMSIS_device_header
#include "cmsis_os2.h"

RTE_Components.h中,
#define CMSIS_device_header “stm32f10x.h”

用户定义任务数

修改RTX配置,默认1个任务不够用,
否则第二个任务
start_thread_id = osThreadNew(start_thread, NULL,NULL);
返回值为0.

在这里插入图片描述

消息队列osMessageQueue

osMessageQueueId_t mid_MsgQueue; // message queue id
mid_MsgQueue = osMessageQueueNew(2, sizeof(uint32_t), NULL);
MessageQueue为先入先出。FIFO
为节省传送时间,传送保存数组地址,需要往osMessageQueue发送保存数组地址的地址,
接收任务中再把保存数组地址取出。

	u8 AD_Buffer[2][16]={0};
	static u8 AD_ArrayNo=0;
	u32 msg;
	注意&AD_Buffer[AD_ArrayNo][0]不是地址的地址。
	正确的方式:
				msg=&AD_Buffer[AD_ArrayNo][0];
			osMessageQueuePut(mid_MsgQueue, &msg, 0U, 0U);

接收任务:

    u8  *cpyptr;
    		if(osOK == osMessageQueueGet(mid_MsgQueue, &msgptr1, NULL, 0U))	// wait for message   does not wait
			{

			cpyptr=(u8 *)msgptr1; 
			}
			for (bufi = 0; bufi < 16; bufi++)
			{
				canBack[bufi] 	= *(cpyptr+bufi);
			}

创建任务osThreadNew

实测。
创建任务既可以在main函数中,osKernelStart之前。
也可以在startThread中,osKernelStart之后。
也可以在普通任务中,以控制上电任务执行顺序。

		if(CreatCanTxThreadFlag==0)
		{
				CreatCanTxThreadFlag = 1;
				CanTx_thread_id = osThreadNew(CanTx_thread, NULL, NULL); // create the thread	
				osThreadSetPriority(CanTx_thread_id, osPriorityRealtime);		

		}

使用osThreadFlags

非阻塞查询式使用

start_thread

			Flags=osThreadFlagsWait(0x00000002U, osFlagsWaitAny, 0);	
			if(Flags==0x00000002U)//
			{
					......;			
			}
			//也可判断不等于osFlagsErrorResource: awaited flags have not been set when no timeout was specified.
			//但osFlagsErrorUnknown不能排除
阻塞式使用

CanTx_thread

osThreadFlagsWait(0x00000001U, osFlagsWaitAny, osWaitForever);
中断中发送信号
if((CanRxBuf[0] == CANID)&&(CanRxBuf[1] == 0xA1)&&(RxMessage.StdId ==0x100) )//LKID
{
	osThreadFlagsSet (CanTx_thread_id, 0x00000001U);	  // set the signal 0x0001 for thread tid_thread1
	osThreadFlagsSet (start_thread_id, 0x00000002U);	  // set the signal 0x0001 for thread tid_thread1
			 
}	
注意,中断中发送osThreadFlagsSet (CanTx_thread_id, 0x00000003U);	
start_thread任务中并不能收到。

终结任务

terminates the calling thread. 不再执行。
//osThreadExit();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值