ucos:ucos 应用编程大全

2024-02-12 04:18
文章标签 应用 编程 大全 ucos

本文主要是介绍ucos:ucos 应用编程大全,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

0:ucos for linux port 代码
   
 ucos for linux port代码可以在: 我的资源中下载。

1:说明

主要对ucos中的task创建,消息队列,消息邮箱,信号量,互斥量的使用进行了举例说明。
对不同的模块的测试:可以通过不同的宏定义来实现。
#define UCOS_BASIC_TASK_TEST   0  //基本任务的创建
#define UCOS_MSG_QUEUE_TASK_TEST 0  //消息队列
#define UCOS_MAIL_B0X_TASK_TEST //消息邮箱
#define UCOS_MEM_TASK_TEST          0 //内存管理
#define UCOS_SEM_TASK_TEST          0 //信号量
#define UCOS_MUTEX_TASK_TEST        1 //互斥量

对不同的模块的测试:可以通过不同的宏定义来实现。
#define UCOS_BASIC_TASK_TEST  0//基本任务的创建
#define UCOS_MSG_QUEUE_TASK_TEST0//消息队列
#define UCOS_MAIL_B0X_TASK_TEST0//消息邮箱
#define UCOS_MEM_TASK_TEST         0//内存管理
#define UCOS_SEM_TASK_TEST         0//信号量
#define UCOS_MUTEX_TASK_TEST       1//互斥量

2:示例代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ucos_ii.h"typedef unsigned char uint8 ;
typedef unsigned short uint16;
struct msgbuf {uint8  option;                 /* Message type */uint8  msgID;uint16 msgData;
};
struct msgType {long mtype;struct msgbuf mdata;
};
#define  TASK_STK_SIZE    128
#define  BUFFER_SIZE  	  3struct storage_data
{INT16U input;INT16U output;INT16U buffer[BUFFER_SIZE];
};
OS_STK        AppStk_send_one[TASK_STK_SIZE];
OS_STK        AppStk_send_two[TASK_STK_SIZE];
OS_STK        AppStk_recv[TASK_STK_SIZE];
OS_STK        qsmBox_AppStk_send[TASK_STK_SIZE];
OS_STK        qsmBox_AppStk_recv[TASK_STK_SIZE];
OS_STK        mem_AppStk_one[TASK_STK_SIZE];
OS_STK        mem_AppStk_two[TASK_STK_SIZE];
OS_STK        sem_AppStk_producer[TASK_STK_SIZE];
OS_STK        sem_AppStk_customer[TASK_STK_SIZE];
OS_STK        mutex_Stack1[TASK_STK_SIZE];
OS_STK        mutex_Stack2[TASK_STK_SIZE];
OS_MEM *mem_p;
OS_EVENT  *pmsgqueue;
OS_EVENT  *pmailbox;
OS_EVENT  *sem_custom;
OS_EVENT  *sem_produce;
OS_EVENT  *ResourceMutex;
void *ptr;
void *msgqueue[10];
struct storage_data *storage;
static int g_counter=0;unsigned long long monotonic_us(void)
{struct timeval tv;gettimeofday(&tv, NULL);return tv.tv_sec * 1000000ULL + tv.tv_usec;
}
void printf_task_info(uint8 i)
{if(OSTCBPrioTbl[i]==NULL){printf("the prio=%u task is no exit\n",i);return ;}else{OS_Printf("the prio=%u task information as follow\n",i);OS_Printf("the prio=%u task delay time is %u\n",i,OSTCBPrioTbl[i]->OSTCBDly);OS_Printf("the prio=%u task status is 0x%x\n",i,OSTCBPrioTbl[i]->OSTCBStat);OS_Printf("the prio=%u task name is %s\n",i,OSTCBPrioTbl[i]->OSTCBTaskName);OS_Printf("OSTCBCur->OSTCBStkPtr=%p\n",OSTCBCur->OSTCBStkPtr);OS_Printf("current task prio=%u\n",OSPrioCur);return ;}
}
void MyTask( void *p_arg )
{char* sTaskName = (char*)p_arg;while(1){/* printf uses mutex to get terminal access, therefore must enter critical section */OS_Printf("%s\t%s: line: %d Name: %s\n", __FILE__,__func__, __LINE__, sTaskName );/* Delay so other tasks may execute. */OSTimeDlyHMSM(0, 0,0 , 50);}/* while */}void App_send_one(void *p_arg)
{INT8U  err;struct msgType sendMsg;struct msgType *recvdMsg;memset(&sendMsg,0,sizeof(sendMsg));p_arg = p_arg;OSTaskNameSet(OSPrioCur,"App_send_one",&err);while(1)                            { srand(monotonic_us());sendMsg.mtype=0x01;sendMsg.mdata.option=0x01;sendMsg.mdata.msgID=0x01;sendMsg.mdata.msgData=rand();recvdMsg =&sendMsg;if((err=OSQPost(pmsgqueue,(void *)&sendMsg))==OS_NO_ERR){OS_Printf("App_send_one: mtype:%d option:%d msgID:%d msgData:%d\n",recvdMsg->mtype,recvdMsg->mdata.option ,recvdMsg->mdata.msgID, recvdMsg->mdata.msgData);}else if(err==OS_Q_FULL){OS_Printf("this is senderone the msg_q is full\n");}else{OS_Printf("other err\n");}OSTimeDlyHMSM(0, 0, 0, 20);//	printf_task_info(OSPrioCur);}
}void App_send_two(void *p_arg)
{INT8U err;p_arg = p_arg;struct msgType sendMsg;struct msgType *recvdMsg;memset(&sendMsg,0,sizeof(sendMsg));OSTaskNameSet(OSPrioCur,"App_send_two",&err);while(1)                           { srand(monotonic_us());sendMsg.mtype=0x02;sendMsg.mdata.option=0x02;sendMsg.mdata.msgID=0x02;sendMsg.mdata.msgData=rand();recvdMsg =&sendMsg;if((err=OSQPost(pmsgqueue,&sendMsg))==OS_NO_ERR){OS_Printf("App_send_two: mtype:%d option:%d msgID:%d msgData:%d\n",recvdMsg->mtype,recvdMsg->mdata.option ,recvdMsg->mdata.msgID, recvdMsg->mdata.msgData);}else if(err==OS_Q_FULL){OS_Printf("this is sendertwo\nthe msg_q is full\n");}else{OS_Printf("other err\n");}OSTimeDlyHMSM(0, 0, 2, 0);sendMsg.mtype=0x03;sendMsg.mdata.option=0x03;sendMsg.mdata.msgID=0x03;sendMsg.mdata.msgData=rand();recvdMsg =&sendMsg;if((err=OSQPostFront(pmsgqueue,&sendMsg))==OS_NO_ERR){OS_Printf("App_send_two: mtype:%d option:%d msgID:%d msgData:%d\n",recvdMsg->mtype,recvdMsg->mdata.option ,recvdMsg->mdata.msgID, recvdMsg->mdata.msgData);}else if(err==OS_Q_FULL){OS_Printf("this is sendertwo the msg_q is full\n");}else{OS_Printf("other err\n");}OSTimeDlyHMSM(0, 0, 0, 10);//printf_task_info(OSPrioCur);}
}void App_recv(void *p_arg)
{INT8U err;INT16U timeout=100;struct msgType *recvdMsg;p_arg = p_arg;OS_Printf("when create:\n");pmsgqueue=OSQCreate(msgqueue,10);for (;;){recvdMsg=OSQPend(pmsgqueue,timeout,&err);if(err==OS_NO_ERR){OS_Printf("recver data is: mtype:%d option:%d msgID:%d msgData:%d\n",recvdMsg->mtype,recvdMsg->mdata.option ,recvdMsg->mdata.msgID, recvdMsg->mdata.msgData);}else if(err==OS_TIMEOUT){OS_Printf("timeout\n");break;}else if(err==OS_ERR_EVENT_TYPE){	OS_Printf("this is recver err is OS_ERR_EVENT_TYP\n");break;}else{break;}OSTimeDlyHMSM(0, 0, 0, 1);}OSQDel(pmsgqueue,OS_DEL_NO_PEND, &err);if(err==OS_ERR_TASK_WAITING){OS_Printf("some task wait for this Mailbox\n");}
}
void qsmBox_App_send(void *p_arg)
{INT8U i;INT8U err;INT8U *buffer[3];buffer[0]="test01\n";buffer[1]="test02\n";buffer[2]="test03\n";p_arg = p_arg;pmailbox=OSMboxCreate(NULL);while(1){for (i=0;i<=2;){if((err=OSMboxPost(pmailbox,buffer[i]))==OS_NO_ERR){OS_Printf("qsmBox_App_send,data is: %s\n",buffer[i]);i++;}else if(err==OS_MBOX_FULL){OS_Printf("this is sender\nthe Mailbox has data,data is: %s\n",pmailbox->OSEventPtr);}else{OS_Printf("other err\n");}OSTimeDlyHMSM(0, 0, 0, 25);}}OSTimeDlyHMSM(0, 0, 5, 0);OSMboxDel (pmailbox,OS_DEL_NO_PEND, &err);if(err==OS_ERR_TASK_WAITING){OS_Printf("some task wait for this Mailbox\n");}
}void qsmBox_App_recv(void *p_arg)
{INT8U err;INT16U timeout=100;INT8U *buffer;p_arg = p_arg;for (;;){buffer=OSMboxPend(pmailbox,timeout,&err);if(err==OS_NO_ERR){OS_Printf("qsmBox_App_recv data is: %s\n",buffer);}else if(err==OS_TIMEOUT){OS_Printf("timeout\n");}else if(err==OS_ERR_EVENT_TYPE){OS_Printf("this is recver\nerr is OS_ERR_EVENT_TYP\n");break;}else{break;}OSTimeDlyHMSM(0, 0, 0, 10);}
}
void mem_App_one(void *p_arg)
{INT8U err;INT8U *string="test memory";p_arg = p_arg;ptr=malloc(10*1024);mem_p=OSMemCreate (ptr, 10, 1024, &err);OSMemNameSet (mem_p, string, &err);output("after create:",mem_p);OSTimeDlyHMSM(0, 0, 2, 0);output("\nafter get twice:",mem_p);OSTimeDlyHMSM(0, 0, 2, 0);output("\nafter release one:",mem_p);
}void mem_App_two(void *p_arg)
{INT8U err;void *ptr[10];p_arg = p_arg;printf("\nfirst get:\n");ptr[0]=OSMemGet (mem_p, &err);printf("ptr[0]=%p\n",ptr[0]);printf("second get:\n");ptr[1]=OSMemGet (mem_p, &err);printf("ptr[1]=%p\n",ptr[1]);OSTimeDlyHMSM(0, 0, 3, 0);OSMemPut (mem_p, ptr[1]);
}void output(char *info,OS_MEM *p)
{printf("%s\n",info);printf("OSMemAddr=%p\n",p->OSMemAddr);printf("OSMemFreeList=%p\n",p->OSMemFreeList);printf("OSMemBlkSize=%d\n",p->OSMemBlkSize);printf("OSMemName=%s\n",p->OSMemName);printf("OSMemNFree=%d\n",p->OSMemNFree);printf("OSMemNBlks=%d\n",p->OSMemNBlks);
}
void sem_App_producer(void *p_arg)
{INT16U i;INT16U timeout;INT8U err;timeout=30000;storage=malloc(sizeof(struct storage_data));sem_produce=OSSemCreate(BUFFER_SIZE);sem_custom=OSSemCreate(0);sem_init();for(i=1;;){OSSemPend (sem_produce, timeout, &err);if(err==OS_NO_ERR){storage->buffer[storage->input]=i;storage->input++;i++;if (storage->input >= BUFFER_SIZE){storage->input = 0;}sem_info();OSSemPost(sem_custom);}else if(err==OS_TIMEOUT){continue;}else{printf("other error %d\n",err);break;}OSTimeDlyHMSM(0, 0, 0, 10);}
}void sem_App_customer(void *p_arg)
{INT16U i;INT16U data;INT16U timeout;INT8U err;timeout=30000;for(i=1;;){OSSemPend (sem_custom, timeout, &err);if(err==OS_NO_ERR){data=storage->buffer[storage->output];storage->buffer[storage->output]=0;printf("consume data %d\n",data);storage->output++;i++;if(storage->output >= BUFFER_SIZE){storage->output = 0;}sem_info();OSSemPost(sem_produce);}else if(err==OS_TIMEOUT){continue;	}else{printf("other error abc\n");break;}OSTimeDlyHMSM(0, 0, 3, 10);}
}void sem_init()
{int j;storage->input=0;storage->output=0;for(j=0;j<BUFFER_SIZE;j++){storage->buffer[j]=0;}
}void sem_info()
{int j;for(j=0;j<BUFFER_SIZE;j++){printf("buffer[%d]=%u\t",j,storage->buffer[j]);}printf("storage->input=%u\t",storage->input);printf("storage->output=%u\n",storage->output);
}static void mutex_Task1(void *p_arg)
{uint8  err;p_arg=p_arg;
#if OS_CRITICAL_METHOD == 3OS_CPU_SR  cpu_sr;
#endifResourceMutex= OSMutexCreate(9,&err);  while(1){OSMutexPend(ResourceMutex,0,&err);OS_ENTER_CRITICAL();g_counter++;OS_EXIT_CRITICAL();  OSMutexPost(ResourceMutex);OSTimeDly(OS_TICKS_PER_SEC*2);OS_Printf("g_counter:%ld\n",g_counter);}
}static void mutex_Task2(void *p_arg)
{   uint8  err;p_arg=p_arg;
#if OS_CRITICAL_METHOD == 3OS_CPU_SR  cpu_sr;
#endifwhile(1){OSMutexPend(ResourceMutex,0,&err);OS_ENTER_CRITICAL();g_counter++;OS_EXIT_CRITICAL(); OSMutexPost(ResourceMutex);OSTimeDly(OS_TICKS_PER_SEC);OS_Printf("g_counter:%ld\n",g_counter);}
}
#define UCOS_BASIC_TASK_TEST  			0
#define UCOS_MSG_QUEUE_TASK_TEST		0
#define UCOS_MAIL_B0X_TASK_TEST			0
#define UCOS_MEM_TASK_TEST          0
#define UCOS_SEM_TASK_TEST          0
#define UCOS_MUTEX_TASK_TEST        1
int main (void)
{uint8 Stk1[TASK_STK_SIZE];uint8 Stk2[TASK_STK_SIZE];char sTask1[] = "Task 1";char sTask2[] = "Task 2";OSInit();
#if UCOS_BASIC_TASK_TESTOSTaskCreate(App_recv,NULL,(OS_STK *)&AppStk_recv[TASK_STK_SIZE-1],(INT8U)4);OSTaskCreate( MyTask, sTask2, (OS_STK *)&Stk2[TASK_STK_SIZE-1], 5 );
#endif	#if UCOS_MSG_QUEUE_TASK_TEST0		OSTaskCreate(App_send_one,NULL,(OS_STK *)&AppStk_send_one[TASK_STK_SIZE-1],(INT8U)11);OSTaskCreate(App_send_two,NULL,(OS_STK *)&AppStk_send_two[TASK_STK_SIZE-1],(INT8U)12);OSTaskCreate(MyTask, sTask1, (OS_STK *)&Stk1[TASK_STK_SIZE-1], 13);
#endif	#if UCOS_MAIL_B0X_TASK_TEST	OSTaskCreate(qsmBox_App_send,NULL,(OS_STK *)&qsmBox_AppStk_send[TASK_STK_SIZE-1],(INT8U)14);OSTaskCreate(qsmBox_App_recv,NULL,(OS_STK *)&qsmBox_AppStk_recv[TASK_STK_SIZE-1],(INT8U)35);
#endif#if UCOS_MEM_TASK_TESTOSTaskCreate(mem_App_one,NULL,(OS_STK *)&mem_AppStk_one[TASK_STK_SIZE-1],(INT8U)16);OSTaskCreate(mem_App_two,NULL,(OS_STK *)&mem_AppStk_two[TASK_STK_SIZE-1],(INT8U)17);
#endif#if UCOS_SEM_TASK_TESTOSTaskCreate(sem_App_producer,NULL,(OS_STK *)&sem_AppStk_producer[TASK_STK_SIZE-1],(INT8U)18);OSTaskCreate(sem_App_customer,NULL,(OS_STK *)&sem_AppStk_customer[TASK_STK_SIZE-1],(INT8U)19);
#endif#if UCOS_MUTEX_TASK_TESTOSTaskCreate(mutex_Task1,NULL,(OS_STK *)&mutex_Stack1[TASK_STK_SIZE-1],(INT8U)20);OSTaskCreate(mutex_Task2,NULL,(OS_STK *)&mutex_Stack2[TASK_STK_SIZE-1],(INT8U)21);
#endifOSStart();return 0;
}


这篇关于ucos:ucos 应用编程大全的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/701653

相关文章

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹

Python Flask 库及应用场景

《PythonFlask库及应用场景》Flask是Python生态中​轻量级且高度灵活的Web开发框架,基于WerkzeugWSGI工具库和Jinja2模板引擎构建,下面给大家介绍PythonFl... 目录一、Flask 库简介二、核心组件与架构三、常用函数与核心操作 ​1. 基础应用搭建​2. 路由与参

Spring Boot中的YML配置列表及应用小结

《SpringBoot中的YML配置列表及应用小结》在SpringBoot中使用YAML进行列表的配置不仅简洁明了,还能提高代码的可读性和可维护性,:本文主要介绍SpringBoot中的YML配... 目录YAML列表的基础语法在Spring Boot中的应用从YAML读取列表列表中的复杂对象其他注意事项总

电脑系统Hosts文件原理和应用分享

《电脑系统Hosts文件原理和应用分享》Hosts是一个没有扩展名的系统文件,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应... Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应

CSS 样式表的四种应用方式及css注释的应用小结

《CSS样式表的四种应用方式及css注释的应用小结》:本文主要介绍了CSS样式表的四种应用方式及css注释的应用小结,本文通过实例代码给大家介绍的非常详细,详细内容请阅读本文,希望能对你有所帮助... 一、外部 css(推荐方式)定义:将 CSS 代码保存为独立的 .css 文件,通过 <link> 标签

Python使用Reflex构建现代Web应用的完全指南

《Python使用Reflex构建现代Web应用的完全指南》这篇文章为大家深入介绍了Reflex框架的设计理念,技术特性,项目结构,核心API,实际开发流程以及与其他框架的对比和部署建议,感兴趣的小伙... 目录什么是 ReFlex?为什么选择 Reflex?安装与环境配置构建你的第一个应用核心概念解析组件

C#通过进程调用外部应用的实现示例

《C#通过进程调用外部应用的实现示例》本文主要介绍了C#通过进程调用外部应用的实现示例,以WINFORM应用程序为例,在C#应用程序中调用PYTHON程序,具有一定的参考价值,感兴趣的可以了解一下... 目录窗口程序类进程信息类 系统设置类 以WINFORM应用程序为例,在C#应用程序中调用python程序

Java应用如何防止恶意文件上传

《Java应用如何防止恶意文件上传》恶意文件上传可能导致服务器被入侵,数据泄露甚至服务瘫痪,因此我们必须采取全面且有效的防范措施来保护Java应用的安全,下面我们就来看看具体的实现方法吧... 目录恶意文件上传的潜在风险常见的恶意文件上传手段防范恶意文件上传的关键策略严格验证文件类型检查文件内容控制文件存储

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2