📄 ostype.c
字号:
/* ###########################################################################
### Modified from Intel fdi_type.h
### -------------------------------------------------------------------------
###
### Module: OSTYPE.C - This file implement os specific services
### such as memory,semaphore,task services.
###
### $Date: 09/30/02
### $Author: gzhu $
### $NoKeywords $
########################################################################### */
#include "fdi_type.h"
/* FDI memory pool */
NU_MEMORY_POOL FDI_Memory_Pool;
UNSIGNED FDI_Memory_Pool_Array[FDI_POOL_SIZE];
int MemPoolInitOK = 0;
/*#############################################################################
### FDI_Setup()
###
### DESCRIPTION: Create memory pool
###
### PARAMETERS:
###
### RETURNS:
###*/
ERR_CODE FDI_Setup(void)
{
STATUS status;
MemPoolInitOK = 0;
status = NU_Create_Memory_Pool(&FDI_Memory_Pool,
"FDIMem",
(void *)FDI_Memory_Pool_Array,
sizeof(FDI_Memory_Pool_Array),
16,
NU_FIFO);
if(status != NU_SUCCESS)
{
MonPrintf("\nNU_Create_Memory_Pool() error!\n");
return ERR_SYSTEM;
}
else
{
MemPoolInitOK = 1;
}
#if(SEM_CREATE_DESTROY == FALSE)
/* Create all semahpores for FDI */
/*return SEM_Init();*/ /* commented by jjs */
/*The following lines were added by jjs to take place of the previous line. */
/* No such function call,we set the SEM_CREATE_DESTROY == TRUE */
if(SEM_Init() != ERR_NONE)
return ERR_SYSTEM;
#endif /* !SEM_CREATE_DESTROY */
/*if(FDI_Format() != ERR_NONE)
{
NU_Delete_Memory_Pool(&FDI_Memory_Pool);
MemPoolInitOK = 0;
MonPrintf("\nError in FDI_Format()!!!\n");
return ERR_INIT;
}*/
return ERR_NONE;
}
void FDI_Exit(void)
{
NU_Delete_Memory_Pool(&FDI_Memory_Pool);
MemPoolInitOK = 0;
}
/*#############################################################################
### FDI_Malloc()
###
### DESCRIPTION: Allocate memory
###
### PARAMETERS:
###
### RETURNS:
###*/
void *FDI_Malloc(UNSIGNED size)
{
void * mem_ptr;
STATUS status;
if(MemPoolInitOK == 0)
{
MonPrintf("\nMem Pool hasn't been initialized!\n");
return NULL;
}
size = (size + 0x03) & (~0x03);
if(size == 0)
{
MonPrintf("\nsize == 0 in FDI_Malloc error!\n");
return NULL;
}
status = NU_Allocate_Memory(&FDI_Memory_Pool,
&mem_ptr,
size,
NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
MonPrintf("\nNU_Allocate_Memory() error!\n");
return NULL;
}
return mem_ptr;
}
/*#############################################################################
### FDI_Free()
###
### DESCRIPTION: Free memory
###
### PARAMETERS:
###
### RETURNS:
###*/
ERR_CODE FDI_Free(void *mem_ptr)
{
STATUS status;
if(MemPoolInitOK == 0)
{
MonPrintf("\nMem Pool hasn't been initialized!\n");
return ERR_SYSTEM;
}
status = NU_Deallocate_Memory(mem_ptr);
if (status != NU_SUCCESS)
{
MonPrintf("\nNU_Deallocate_Memory() error!\n");
return ERR_SYSTEM;
}
return ERR_NONE;
}
/*#############################################################################
### SEM_TRY_WAIT()
###
### DESCRIPTION: Test the semaphore
###
### PARAMETERS:
###
### RETURNS:
###*/
ERR_CODE SEM_TRY_WAIT(SEM_ID pSem)
{
STATUS status;
if((status = NU_Obtain_Semaphore(pSem, NU_NO_SUSPEND)) == NU_SUCCESS)
{
return ERR_NONE;
}
else
{
return ERR_SYSTEM;
}
}
ERR_CODE SEM_WAIT(SEM_ID pSem)
{
STATUS status;
if((status = NU_Obtain_Semaphore(pSem, NU_SUSPEND)) == NU_SUCCESS)
{
return ERR_NONE;
}
else
{
return ERR_SYSTEM;
}
}
ERR_CODE SEM_WAIT_TIME(SEM_ID pSem, UNSIGNED time_out)
{
STATUS status;
if((status = NU_Obtain_Semaphore(pSem, time_out)) == NU_SUCCESS)
{
return ERR_NONE;
}
else
{
return ERR_SYSTEM;
}
}
ERR_CODE SEM_POST(SEM_ID pSem)
{
STATUS status;
char name[NU_MAX_NAME];
UNSIGNED current_count;
UNSIGNED tasks_waiting;
OPTION suspend_type;
NU_TASK *first_task;
status = NU_Semaphore_Information(pSem, name,
¤t_count,
&suspend_type,
&tasks_waiting,
&first_task);
if(status != NU_SUCCESS)
return ERR_SYSTEM;
if(current_count >= 1)
return ERR_NONE;
if((status = NU_Release_Semaphore(pSem)) == NU_SUCCESS)
{
return ERR_NONE;
}
else
{
return ERR_SYSTEM;
}
}
/*#############################################################################
### SEM_BIN_CREATE()
###
### DESCRIPTION: Creates a Binary semaphore.
###
### PARAMETERS:
###
### RETURNS:
###*/
SEM_ID SEM_BIN_CREATE()
{
STATUS status;
SEM_ID pSem;
DWORD key0, key1;
DISABLE_INTERRUPTS(key0, key1);
/* Allocate memory for semaphore */
if((pSem = (SEM_ID)FDI_Malloc(sizeof(NU_SEMAPHORE))) == NULL)
{
ENABLE_INTERRUPTS(key0, key1);
return NULL;
}
if((status = NU_Create_Semaphore(pSem, "", 0, NU_FIFO)) != NU_SUCCESS)
{
FDI_Free(pSem);
ENABLE_INTERRUPTS(key0, key1);
return NULL;
}
ENABLE_INTERRUPTS(key0, key1);
return pSem;
}
/*#############################################################################
### SemDestroy()
###
### DESCRIPTION:
###
### PARAMETERS:
###
### RETURNS:
###*/
ERR_CODE SemDestroy(SEM_ID pSem)
{
DWORD key0, key1;
DISABLE_INTERRUPTS(key0, key1);
NU_Delete_Semaphore(pSem);
FDI_Free(pSem);
ENABLE_INTERRUPTS(key0, key1);
return ERR_NONE;
}
/*#############################################################################
### SPAWN()
###
### DESCRIPTION:
###
### PARAMETERS:
###
### RETURNS:
###*/
PTASK_INFO
SPAWN(CHAR *Name,
OPTION Priority,
UNSIGNED unused,
UNSIGNED stack_size,
DWORD task_entry)
{
PTASK_INFO pTaskInfo;
STATUS status;
pTaskInfo = FDI_Malloc(sizeof(TASK_INFO));
if(pTaskInfo == NULL)
return NULL;
pTaskInfo->pStack = FDI_Malloc(stack_size);
if(pTaskInfo->pStack == NULL)
{
FDI_Free(pTaskInfo);
return NULL;
}
status = NU_Create_Task(&pTaskInfo->TaskCB,
Name,
(TASK_ENTRY)task_entry,
0,
NULL,
pTaskInfo->pStack,
stack_size,
Priority,
0,
NU_PREEMPT,
NU_START);
if(status != NU_SUCCESS)
{
FDI_Free(pTaskInfo->pStack);
FDI_Free(pTaskInfo);
return NULL;
}
return pTaskInfo;
}
/*#############################################################################
### TaskDestroy()
###
### DESCRIPTION:
###
### PARAMETERS:
###
### RETURNS:
###*/
ERR_CODE
TaskDestroy(PTASK_INFO pTaskInfo)
{
STATUS status;
status = NU_Terminate_Task(&pTaskInfo->TaskCB);
if(status != NU_SUCCESS)
{
MonPrintf("\n can't terminate task! \n");
}
status = NU_Delete_Task(&pTaskInfo->TaskCB);
if(status != NU_SUCCESS)
{
MonPrintf("\n can't delete task! \n");
}
FDI_Free(pTaskInfo->pStack);
FDI_Free(pTaskInfo);
return ERR_NONE;
}
/*****************************************************************************
* $Log: ostype.c $
* Revision 1.4 2003/01/17 03:35:15 gzhu
* Check in for VTC
* Revision 1.4 2003/01/07 17:04:46 jjs
* Revision 1.3 2003/01/07 11:30:42 jjs
* Fix bug in TaskDestroy
* Revision 1.2 2002/12/16 17:48:30 jjs
* In function SPAWN, changes task creation option from NU_NO_PREEMPT to NU_PREEMPT.
*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -