📄 ucos2_memmanager.c
字号:
/***************************************************************
* 声明:
* 本程序只具备演示功能,不能保证适用于您的真实应用。如需使用,请根据
* 您的实际需要修改本程序。
*******************************************************************
* 电子科技大学嵌入式软件工程中心 版权所有
*
* Copyright (C) 2006 UESTC ESEC
**************************************************************/
/**************************************************************
* 模块: init.c
*
* 目的:
* 这个程序演示要体现内核的内存分配思想的策略。为此,设计了
内存的分配和回收,完成一系列动作。
**************************************************************/
/*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
*
*
*********************************************************************************************************
*/
#include <stdio.h>
#include "includes.h"
/*
*********************************************************************************************************
* CONSTANTS
*********************************************************************************************************
*/
#define TASK_STK_SIZE 512 /* Size of each task's stacks (# of WORDs) */
#define N_TASKS 3 /* Number of tasks */
/*
*********************************************************************************************************
* VARIABLES
*********************************************************************************************************
*/
OS_STK TaskStk[N_TASKS][TASK_STK_SIZE]; /* Tasks stacks */
OS_STK TaskStartStk[TASK_STK_SIZE];
INT8U TaskData[N_TASKS]; /* Parameters to pass to each task */
void *CommMsg1; //为OSMemGet函数设置返回值存储。
void *CommMsg2; //为OSMemGet函数设置返回值存储。
void *CommMsg3;
OS_MEM *CommMem; //为OSMemCreate函数设置返回值存储。
INT8U CommBuf[2][128]; //初始化内存分配的空间。
OS_STK TaskStk[N_TASKS][TASK_STK_SIZE]; /* Tasks stacks */
OS_STK TaskStartStk[TASK_STK_SIZE];
INT8U TaskData[N_TASKS]; /* Parameters to pass to each task */
OS_EVENT *mutex; /* 1 mutual exclusive semaphore*/
/*
*********************************************************************************************************
* FUNCTION PROTOTYPES
*********************************************************************************************************
*/
void Task(void *pdata); /* Function prototypes of tasks */
void TaskStart(void *pdata); /* Function prototypes of Startup task */
static void TaskStartCreateTasks(void);
extern void main(void); // Function prototypes of main
extern void ucBsp_Init(); // Function prototypes of Bsp_Init
extern void printk(); // Function prototypes of printk
extern void Timer_Start( ); // Function prototypes of Timer_Start
extern void CPSR_Init(); // Function prototypes of CPSR_Init
void _SWI_Handler() // Function of _SWI_Handler avoid compile error
{
}
extern void OS_Sched(void); /*Function prototypes of OS_Sched */
/**************************** 定义部分 *****************************************/
/*
*******************************************************************************************
创建内存分区
*******************************************************************************************
*/
static void MemoryCreate()
{
INT8U err; //为OSMemCreate函数设置包含错误码的变量的指针。
CommMem = OSMemCreate(&CommBuf[0][0], 2, 128, &err); //OSMemCreate()函数建立并初始化一块内存区。2块,每块128B。
}
/*
*******************************************************************************************
给用户显示内存空间的状态信息。
*******************************************************************************************
*/
static void DispShow()
{
OS_MEM_DATA mem_data;
OSMemQuery(CommMem, &mem_data);
printk( "the pointer to the begining of memory address is: %d\n\r",(int)(mem_data.OSAddr));
printk( "the size of blocks in this memory area is: %d\n\r",(int)(mem_data.OSBlkSize));
printk( "the number of free blocks in this memory area is: %d\n\r", (int)(mem_data.OSNFree));
printk( "the number of using blocks in this memory area is: %d\n\r", (int)mem_data.OSNUsed);
printk("\n\r\n\r\n\r");
OSTimeDlyHMSM(0,0,4,0);
}
/*
*********************************************************************************************************
* 释放内存
*********************************************************************************************************
*/
void ReleaseMem(int i)
{
INT8U err;
switch(i)
{
case 3:err=OSMemPut(CommMem,CommMsg3);
if (err == OS_NO_ERR)
{
printk("Third memory has been released.\n\r"); /* 释放内存块 */
}
else
{
printk("Third memory didn't exist.\n\r");
}
break;
case 2:err=OSMemPut(CommMem,CommMsg2);
if (err == OS_NO_ERR)
{
printk("Second memory has been released.\n\r"); /* 释放内存块 */
}
else
{
printk("Second memory didn't been releasd.\n\r");
}
break;
case 1:err=OSMemPut(CommMem,CommMsg1);
if (err == OS_NO_ERR)
{
printk("First memory has been released.\n\r"); /* 释放内存块 */
}
else
{
printk("First memory didn't been releasd.\n\r");
}
break;
}
}
/*
*************************************************************************************************************
* MAIN
*************************************************************************************************************
*/
void boot_card()
{
ucBsp_Init(); /* Initializa ucBsp */
main();
}
void main (void)
{
CPSR_Init(); /* open the arm interrupt bit */
OSInit(); /* Initialize uC/OS-II */
OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1],4);
OSStart(); /* Start multitasking */
}
/*
*********************************************************************************************************
* STARTUP TASK
*********************************************************************************************************
*/
void TaskStart (void *pdata)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
pdata = pdata; /* Prevent compiler warning */
MemoryCreate();
Timer_Start();
TaskStartCreateTasks(); /* Create all the application tasks */
OSTaskSuspend(OS_PRIO_SELF); /* Suspend the TaskStart */
}
/*
*********************************************************************************************************
* CREATE TASKS
*********************************************************************************************************
*/
static void TaskStartCreateTasks (void)
{
OSTaskCreate(Task, (void *)&TaskData[0], &TaskStk[0][TASK_STK_SIZE - 1], 5);
}
/*
*********************************************************************************************************
* TASKS
*********************************************************************************************************
*/
void Task (void *pdata)
{
INT8U err;
INT8U i=0;
DispShow();
for (;;) {
if(i==0)
{
CommMsg1=OSMemGet(CommMem,&err); //申请内存区中的一块。
if (err == OS_NO_ERR)
{
printk("First memory application HAS accept.\n\r"); /* 内存块已经分配 */
}
else
{
printk("First memory alpplication NOT accept.\n\r");
}
DispShow(); //将内存区信息显示到屏幕上。
i++;
}
else if(i==1)
{
CommMsg2=OSMemGet(CommMem,&err); //申请内存区中的一块。
if (err == OS_NO_ERR)
{
printk("Second memory application HAS accept.\n\r"); /* 内存块已经分配 */
}
else
{
printk("Second memory alpplication NOT accept.\n\r");
}
DispShow(); //将内存区信息显示到屏幕上。
i++;
}
else if(i==3)
{
for(i=3;i>0;i--)
{
ReleaseMem(i); //释放一个内存块。
DispShow(); //将内存区信息显示到屏幕上。
}
}
else
{
CommMsg3=OSMemGet(CommMem,&err);
if (err == OS_NO_ERR)
{
printk("Third memory application HAS accept.\n\r"); /* 内存块已经分配 */
}
else
{
printk("Third memory alpplication NOT accept.\n\r");
}
DispShow();
i++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -