📄 ostype.c
字号:
/*****************************************************************************
FILE NAME: Ostype.c
DESCRIPTION:
os-specific API implementation used in the FSM
Copyright (c) 2002, VIA Technologies, Inc.
*****************************************************************************/
#include "fsmdefs.h"
#include "ostype.h"
#if (OS_TYPE == OS_NUCLEUS)
/*============================ MEMORY SERVICE ========================= */
static NU_MEMORY_POOL FsmMemoryPool;
static uint8 FsmMemPoolInitOK = 0;
#ifdef DEBUG_FSM
static uint32 FsmAllocCount = 0;
#endif /* DEBUG_FSM */
/*============================ MEMORY SERVICE ========================= */
#else
#endif
/*******************************************************
* FsmMemInit()
*
* Descriptions: Create memory pool.
*
* Parameters:
*
* Returned: ERR_NONE, ERR_SYSTEM.
*
*******************************************************/
uint32 FsmMemInit(void * start, uint32 size)
{
#if (OS_TYPE == OS_NUCLEUS)
STATUS status;
#ifdef DEBUG_FSM
MonPrintf("\nEnter FsmMemInit() ---->>");
#endif
/* Pool has been INITIALIZED! */
if(FsmMemPoolInitOK != 0)
{
#ifdef DEBUG_FSM
MonPrintf("\nMem Pool has been initialized!");
#endif
return ERR_NONE;
}
#ifdef DEBUG_FSM
FsmAllocCount = 0;
#endif
FsmMemPoolInitOK = 0;
status = NU_Create_Memory_Pool(&FsmMemoryPool,
"FsmMem",
(void *)start,
size,
32,
NU_FIFO);
if(status != NU_SUCCESS)
{
#ifdef DEBUG_FSM
MonPrintf("\nFsmMem: NU_Create_Memory_Pool() error!\n");
#endif
return ERR_SYSTEM;
}
else
{
FsmMemPoolInitOK = 1;
}
#ifdef DEBUG_FSM
MonPrintf("\n---->> Exit FsmMemInit().");
#endif
return ERR_NONE;
#else
return ERR_NONE;
#endif
}
/*******************************************************
* FsmMemExit()
*
* Descriptions: Delete memory pool.
*
* Parameters:
*
* Returned: ERR_NONE, ERR_SYSTEM.
*
*******************************************************/
uint32 FsmMemExit(void)
{
#if (OS_TYPE == OS_NUCLEUS)
STATUS status;
#ifdef DEBUG_FSM
MonPrintf("\nEnter FsmMemExit() ---->>");
#endif
if(FsmMemPoolInitOK == 0)
{
#ifdef DEBUG_FSM
MonPrintf("\nMem Pool hasn't been initialized!");
#endif
return ERR_SYSTEM;
}
#ifdef DEBUG_FSM
if(FsmAllocCount > 0)
{
MonPrintf("\nMem Pool is in use!");
return ERR_SYSTEM;
}
#endif
FsmMemPoolInitOK = 0;
status = NU_Delete_Memory_Pool(&FsmMemoryPool);
if(status != NU_SUCCESS)
{
#ifdef DEBUG_FSM
MonPrintf("\nFsmMem:NU_Delete_Memory_Pool() error!\n");
#endif
/* restore the state of 'FsmMemPoolInitOK'. */
FsmMemPoolInitOK = 1;
return ERR_SYSTEM;
}
#ifdef DEBUG_FSM
MonPrintf("\n---->> Exit FsmMemExit().");
#endif
return ERR_NONE;
#else
return ERR_NONE;
#endif
}
/*******************************************************
* FsmMalloc()
*
* Descriptions: Allocate memory.
*
* Parameters:
*
* Returned: NULL, memory pointer.
*
*******************************************************/
void *FsmMalloc(uint32 size)
{
#if (OS_TYPE == OS_NUCLEUS)
void * mem_ptr;
STATUS status;
#ifdef DEBUG_FSM
MonPrintf("\nEnter FsmMalloc() ---->>");
#endif
if(FsmMemPoolInitOK == 0)
{
#ifdef DEBUG_FSM
MonPrintf("\nMem Pool hasn't been initialized!\n");
#endif
return NULL;
}
/* Maybe the following code line is unnecessary! */
size = (size + 0x03) & (~0x03);
if(size == 0)
{
#ifdef DEBUG_FSM
MonPrintf("\nsize == 0 in FsmMalloc()!");
#endif
return NULL;
}
status = NU_Allocate_Memory(&FsmMemoryPool,
&mem_ptr,
size,
NU_SUSPEND);
if (status != NU_SUCCESS)
{
#ifdef DEBUG_FSM
MonPrintf("\nFsmMem:NU_Allocate_Memory() error!\n");
#endif
return NULL;
}
#ifdef DEBUG_FSM
DISABLE_INTERRUPTS();
FsmAllocCount++;
ENABLE_INTERRUPTS();
MonPrintf("\n---->> Exit FsmMalloc().");
#endif
return mem_ptr;
#else
return malloc(size);
#endif
}
/*******************************************************
* FsmTryMalloc()
*
* Descriptions: Allocate memory -- Non-Blocked version.
*
* Parameters:
*
* Returned: NULL, memory pointer.
*
*******************************************************/
void *FsmTryMalloc(uint32 size)
{
#if (OS_TYPE == OS_NUCLEUS)
void * mem_ptr;
STATUS status;
#ifdef DEBUG_FSM
MonPrintf("\nEnter FsmTryMalloc() ---->>");
#endif
if(FsmMemPoolInitOK == 0)
{
#ifdef DEBUG_FSM
MonPrintf("\nMem Pool hasn't been initialized!\n");
#endif
return NULL;
}
/* Maybe the following code line is unnecessary! */
size = (size + 0x03) & (~0x03);
if(size == 0)
{
#ifdef DEBUG_FSM
MonPrintf("\nsize == 0 in FsmTryMalloc()!\n");
#endif
return NULL;
}
status = NU_Allocate_Memory(&FsmMemoryPool,
&mem_ptr,
size,
NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
#ifdef DEBUG_FSM
MonPrintf("\nFsmMem:NU_Allocate_Memory() error!\n");
#endif
return NULL;
}
#ifdef DEBUG_FSM
DISABLE_INTERRUPTS();
FsmAllocCount++;
ENABLE_INTERRUPTS();
MonPrintf("\n---->> Exit FsmTryMalloc().");
#endif
return mem_ptr;
#else
return malloc(size);
#endif
}
/*******************************************************
* FsmFree()
*
* Descriptions: Free memory.
*
* Parameters:
*
* Returned: ERR_NONE, ERR_SYSTEM.
*
*******************************************************/
uint32 FsmFree(void * mem_ptr)
{
#if (OS_TYPE == OS_NUCLEUS)
STATUS status;
#ifdef DEBUG_FSM
MonPrintf("\nEnter FsmFree() ---->>");
#endif
if(FsmMemPoolInitOK == 0)
{
#ifdef DEBUG_FSM
MonPrintf("\nMem Pool hasn't been initialized!\n");
#endif
return ERR_SYSTEM;
}
status = NU_Deallocate_Memory(mem_ptr);
if (status != NU_SUCCESS)
{
#ifdef DEBUG_FSM
MonPrintf("\nFsmMem:NU_Deallocate_Memory() error!\n");
#endif
return ERR_SYSTEM;
}
#ifdef DEBUG_FSM
DISABLE_INTERRUPTS();
FsmAllocCount--;
ENABLE_INTERRUPTS();
MonPrintf("\n---->> Exit FsmFree().");
#endif
return ERR_NONE;
#else
free(mem_ptr);
return ERR_NONE;
#endif
}
/*******************************************************
* FsmMemAvailable()
*
* Descriptions: Get the number of available
* bytes in the memory pool.
*
* Parameters:
*
* out: available memory size.
*
* Returned: ERR_NONE, ERR_SYSTEM.
*
*******************************************************/
uint32 FsmMemAvailable(uint32 * available)
{
#if (OS_TYPE == OS_NUCLEUS)
STATUS status;
char pool_name[8];
void * start_address;
uint32 pool_size;
uint32 min_allocation;
OPTION suspend_type;
uint32 tasks_suspended;
NU_TASK * first_task;
#ifdef DEBUG_FSM
MonPrintf("\nEnter FsmMemAvailable() ---->>");
#endif
if(FsmMemPoolInitOK == 0)
{
#ifdef DEBUG_FSM
MonPrintf("\nMem Pool hasn't been initialized!\n");
#endif
return ERR_SYSTEM;
}
/* Obtain information about the memory pool. */
status = NU_Memory_Pool_Information(&FsmMemoryPool,
pool_name,
&start_address,
&pool_size,
&min_allocation,
available,
&suspend_type,
&tasks_suspended,
&first_task);
if (status != NU_SUCCESS)
{
#ifdef DEBUG_FSM
MonPrintf("\nNU_Memory_Pool_Information() error!\n");
#endif
return ERR_SYSTEM;
}
#ifdef DEBUG_FSM
MonPrintf("\n---->> Exit FsmMemAvailable().");
#endif
return ERR_NONE;
#else
*available = 0xFFFFFFFF;
return ERR_NONE;
#endif
}
/*******************************************************
* FsmMemoryMove()
*
* Descriptions: Copy num_bytes of data
* from source to destinition.
*
* Parameters:
*
*
* Returned:
*
*******************************************************/
void FsmMemoryMove(uint8 * pDst, const uint8 * pSrc, uint32 num_bytes)
{
while (num_bytes > 0)
{
*pDst = *pSrc;
pDst++;
pSrc++;
num_bytes--;
}
}
/*******************************************************
* FsmMemorySet()
*
* Descriptions: Set the whole buffer to the specified value.
*
* Parameters:
*
*
* Returned:
*
*******************************************************/
void FsmMemorySet(uint8 * buf, uint8 byte_value, uint32 bytes)
{
while (bytes > 0)
{
*buf = byte_value;
buf++;
bytes--;
}
}
/*============================== Time service ============================ */
/**********************************************************
* FsmGetCurrentTime()
*
* Descriptions:
* Get the current system time.
*
* Parameters:
*
*
* Returned:
*
*********************************************************/
uint32 FsmGetCurrentTime()
{
return 0x1234;
}
/*============================ SEMAPHORE SERVICE ========================= */
/**********************************************************
* FsmCreateBinSem()
*
* Descriptions:
* Creates a Binary semaphore.
*
* Parameters:
*
*
* Returned:
*
*********************************************************/
HBSEM FsmCreateBinSem(uint8 state)
{
#if (OS_TYPE == OS_NUCLEUS)
STATUS status;
HBSEM hBinSem;
#ifdef DEBUG_FSM
MonPrintf("\nEnter FsmCreateBinSem() ---->>");
#endif
/* Allocate memory for semaphore */
if((hBinSem = (HBSEM)FsmTryMalloc(sizeof(BinSemT))) == NULL)
{
return NULL;
}
if(state >= 1)
{
state = 1;
}
else
{
state = 0;
}
if((status = NU_Create_Semaphore(hBinSem, "", state, NU_FIFO)) != NU_SUCCESS)
{
FsmFree(hBinSem);
#ifdef DEBUG_FSM
MonPrintf("\nNU_Create_Semaphore() failed.");
#endif
return NULL;
}
#ifdef DEBUG_FSM
MonPrintf("\n---->> Exit FsmCreateBinSem().");
#endif
return hBinSem;
#else
if(state >= 1)
{
state = 1;
}
else
{
state = 0;
}
return CreateSemaphore(NULL, state, 1, NULL);
#endif
}
/**********************************************************
* FsmDeleteBinSem()
*
* Descriptions:
*
*
* Parameters:
*
*
* Returned:
*
*********************************************************/
uint32 FsmDeleteBinSem(HBSEM hBinSem)
{
#if (OS_TYPE == OS_NUCLEUS)
STATUS status;
#ifdef DEBUG_FSM
MonPrintf("\nEnter FsmDeleteBinSem() ---->>");
#endif
status = NU_Delete_Semaphore(hBinSem);
if(status != NU_SUCCESS)
{
#ifdef DEBUG_FSM
MonPrintf("\nNU_Delete_Semaphore() failed.");
#endif
return ERR_SYSTEM;
}
FsmFree(hBinSem);
#ifdef DEBUG_FSM
MonPrintf("\n---->> Exit FsmDeleteBinSem().");
#endif
return ERR_NONE;
#else
CloseHandle(hBinSem);
return ERR_NONE;
#endif
}
/**********************************************************
* FsmGetBinSemTry()
*
* Descriptions:
* Try to get the semaphore -- non-blocked
*
* Parameters:
*
*
* Returned:
*
*********************************************************/
uint32 FsmGetBinSemTry(HBSEM hBinSem)
{
#if (OS_TYPE == OS_NUCLEUS)
STATUS status;
#ifdef DEBUG_FSM
MonPrintf("\nEnter FsmGetBinSemTry() ---->>");
#endif
if((status = NU_Obtain_Semaphore(hBinSem, NU_NO_SUSPEND)) != NU_SUCCESS)
{
return ERR_SYSTEM;
}
#ifdef DEBUG_FSM
MonPrintf("\n---->> Exit FsmGetBinSemTry().");
#endif
return ERR_NONE;
#else
if(WaitForSingleObject(hBinSem, 0) != WAIT_OBJECT_0)
return ERR_SYSTEM;
else
return ERR_NONE;
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -