?? osslib.c
字號(hào):
/* ossLib.c - O/S-independent services for vxWorks *//* Copyright 2000-2002 Wind River Systems, Inc. *//*Modification history--------------------01f,22jan02,wef All USB memory now comes from a cacheDmaMalloc'd partition. Instrument routines to get partition information.01e,25oct01,wef changed ossMalloc to call memalign instead of malloc - it now returns buffers that are a multiple of a cache line size aligned on a cache line boundary01d,18sep01,wef merge from wrs.tor2_0.usb1_1-f for veloce01c,01aug01,rcb fixed manpage generation in comments01b,07mar00,rcb Cast handles as necessary to reflect change in handle definition from UINT32 to pVOID.01a,07jun99,rcb First.*//*DESCRIPTIONImplements functions defined by ossLib.h. See ossLib.h fora complete description of these functions.*//* includes */#include "vxWorks.h"#include "stdio.h"#include "stdlib.h"#include "string.h"#include "errnoLib.h" /* errno */#include "sysLib.h" /* sysClkRateGet(), etc. */#include "taskLib.h" /* taskSpawn(), etc. */#include "semLib.h" /* semCreate(), etc. */#include "objLib.h" /* S_objLib_OBJ_xxxx errors */#include "tickLib.h" /* tickGet(), etc. */#include "cacheLib.h" /* cacheDmaMalloc, Free */#include "usb/usbPlatform.h"#include "usb/ossLib.h" /* Our API *//* Defines *//* #define FILL_MEMORY */ /* for debugging *//* Definitions related to the creation of new threads. */#define DEFAULT_OPTIONS 0 /* task options */#define DEFAULT_STACK 0x4000 /* Default stack size *//* * TIMEOUT() calculates the number of ticks for a timeout from a blockFlag * parameter to one of the ossLib functions. */#define TIMEOUT(bf) (((bf) == OSS_BLOCK) ? \ WAIT_FOREVER : ((bf) + msecsPerTick - 1) / msecsPerTick)#define USB_MEM_PART_DEF_SIZE 0x10000 /* 0x10000 Default * partition size - 64k *//* typedefs *//* globals *//* Default to using the partition method of malloc / free. */FUNCPTR ossMallocFuncPtr;FUNCPTR ossFreeFuncPtr;/* locals */LOCAL int msecsPerTick = 55; /* milliseconds per system tick */ /* value updated by ossInitialize() */LOCAL BOOL ossPartInitFlag = TRUE;LOCAL char * pUsbMemSpace; LOCAL PART_ID usbMemPartId;LOCAL UINT32 usbMemPartSize = USB_MEM_PART_DEF_SIZE;LOCAL UINT32 usbMemCount = 0;LOCAL UINT32 ossOldInstallFlag = FALSE;/* Functions *//***************************************************************************** translateError - translates vxWorks error to S_ossLib_xxxx** Translates certain vxWorks errno values to an appropriate S_ossLib_xxxx.* This function should only be invoked when the caller has already* ascertained that a vxWorks function has returned an error.** RETURNS: S_ossLib_xxxx** ERRNO:* S_ossLib_BAD_HANDLE* S_ossLib_TIMEOUT* S_ossLib_GENERAL_FAULT*/LOCAL int translateError (void) { switch (errno) { case S_objLib_OBJ_ID_ERROR: return S_ossLib_BAD_HANDLE; case S_objLib_OBJ_TIMEOUT: return S_ossLib_TIMEOUT; default: return S_ossLib_GENERAL_FAULT; } }/***************************************************************************** translatePriority - translates OSS_PRIORITY_xxx to vxWorks priority** RETURNS: vxWorks task priority*/LOCAL int translatePriority ( UINT16 priority ) { int curPriority; switch (priority) { case OSS_PRIORITY_LOW: case OSS_PRIORITY_TYPICAL: case OSS_PRIORITY_HIGH: case OSS_PRIORITY_INTERRUPT: return priority; case OSS_PRIORITY_INHERIT: default: taskPriorityGet (0, &curPriority); return curPriority; } }/***************************************************************************** threadHead - vxWorks taskSpawn() compatible thread entry point** ossThreadCreate() uses this intermediary function to spawn threads. The* vxWorks taskSpawn() function passes 10 int parameters to the task entry* point. By convention, ossThreadCreate passes the function's real entry* point in <arg1> and the thread's <param> in <arg2>..*/LOCAL int threadHead ( int arg1, /* <func> in this parameter */ int arg2, /* <param> in this parameter */ int arg3, int arg4, int arg5, int arg6, int arg7, int arg8, int arg9, int arg10 ) { THREAD_PROTOTYPE func = (THREAD_PROTOTYPE) arg1; pVOID param = (pVOID) arg2; (*func) (param); return 0; }/***************************************************************************** ossStatus - Returns OK or ERROR and sets errno based on status** If <status> & 0xffff is not equal to zero, then sets errno to the* indicated <status> and returns ERROR. Otherwise, does not set errno* and returns OK.** RETURNS: OK or ERROR*/STATUS ossStatus ( int status ) { if ((status & 0xffff) != 0) { errnoSet (status); return ERROR; } return OK; }/***************************************************************************** ossShutdown - Shuts down ossLib** This function should be called once at system shutdown if an only if* the corresponding call to ossInitialize() was successful.** RETURNS: OK or ERROR*/STATUS ossShutdown (void) { return OK; }/***************************************************************************** ossThreadCreate - Spawns a new thread** The ossThreadCreate() function creates a new thread which begins execution * with the specified <func>. The <param> argument will be passed to <func>. * The ossThreadCreate() function creates the new thread with a stack of a * default size and with no security restrictions - that is, there are no * restrictions on the use of the returned <pThreadHandle> by other threads. * The newly created thread will execute in the same address space as the * calling thread. <priority> specifies the thread's desired priority - in* systems which implement thread priorities, as OSS_PRIORITY_xxxx. ** RETURNS: OK or ERROR** ERRNO:* S_ossLib_BAD_PARAMETER* S_ossLib_GENERAL_FAULT*/STATUS ossThreadCreate ( THREAD_PROTOTYPE func, /* function to spawn as new thread */ pVOID param, /* Parameter to be passed to new thread */ UINT16 priority, /* OSS_PRIORITY_xxxx */ pCHAR name, /* thread name or NULL */ pTHREAD_HANDLE pThreadHandle /* Handle of newly spawned thread */ ) { /* validate params */ if (pThreadHandle == NULL) return ossStatus (S_ossLib_BAD_PARAMETER); /* Use vxWorks to spawn a new thread (task in vxWorks parlance). */ if ((*pThreadHandle = (THREAD_HANDLE) taskSpawn (name, translatePriority (priority), DEFAULT_OPTIONS, DEFAULT_STACK, threadHead, (int) func, (int) param, 0, 0, 0, 0, 0, 0, 0, 0)) == (THREAD_HANDLE) ERROR) return ossStatus (S_ossLib_GENERAL_FAULT); return OK; }/***************************************************************************** ossThreadDestroy - Attempts to destroy a thread** This function attempts to destroy the thread specified by <threadHandle>.** NOTE: Generally, this function should be called only after the given* thread has terminated normally. Destroying a running thread may result* in a failure to release resources allocated by the thread.** RETURNS: OK or ERROR** ERRNO:* S_ossLib_GENERAL_FAULT*/STATUS ossThreadDestroy ( THREAD_HANDLE threadHandle /* handle of thread to be destroyed */ ) { if (taskDelete ((int) threadHandle) == ERROR) return ossStatus (S_ossLib_GENERAL_FAULT); return OK; } /***************************************************************************** ossThreadSleep - Voluntarily relinquishes the CPU** Threads may call ossThreadSleeph() to voluntarily release the CPU to* another thread/process. If the <msec> argument is 0, then the thread will* be reschuled for execution as soon as possible. If the <msec> argument is* greater than 0, then the current thread will sleep for at least the number* of milliseconds specified.** RETURNS: OK or ERROR*/STATUS ossThreadSleep ( UINT32 msec /* Number of msec to sleep */ ) { /* Delay for a number of ticks at least as long as the number of milliseconds requested by the caller. */ taskDelay ((msec == 0) ? 0 : ((msec + msecsPerTick - 1) / msecsPerTick) + 1); return OK; }/***************************************************************************** ossSemCreate - Creates a new semaphore** This function creates a new semaphore and returns the handle of that* semaphore in <pSemHandle>. The semaphore's initial count is set to* <curCount> and has a maximum count as specified by <maxCount>.** RETURNS: OK or ERROR* * ERRNO:* S_ossLib_BAD_PARAMETER* S_ossLib_GENERAL_FAULT*/STATUS ossSemCreate ( UINT32 maxCount, /* Max count allowed for semaphore */ UINT32 curCount, /* initial count for semaphore */ pSEM_HANDLE pSemHandle /* newly created semaphore handle */ ) { /* Validate parameters */ if (pSemHandle == NULL) return ossStatus (S_ossLib_BAD_PARAMETER); if ((*pSemHandle = (SEM_HANDLE) semCCreate (SEM_Q_FIFO, curCount)) == NULL) return ossStatus (S_ossLib_GENERAL_FAULT); return OK; }/***************************************************************************** ossSemDestroy - Destroys a semaphore** Destroys the semaphore <semHandle> created by ossSemCreate().** RETURNS: OK or ERROR** ERRNO:* S_ossLib_GENERAL_FAULT*/STATUS ossSemDestroy ( SEM_HANDLE semHandle /* Handle of semaphore to destroy */ ) { if (semDelete ((SEM_ID) semHandle) != OK) return ossStatus (S_ossLib_GENERAL_FAULT); return OK; }/***************************************************************************** ossSemGive - Signals a semaphore** This function signals the sepcified semaphore. A semaphore may have more* than one outstanding signal, as specified by the maxCount parameter when* the semaphore was created by ossSemCreate(). While the semaphore is at its* maximum count, additional calls to ossSemSignal for that semaphore have no* effect.** RETURNS: OK or ERROR** ERRNO:* S_ossLib_BAD_HANDLE*/STATUS ossSemGive ( SEM_HANDLE semHandle /* semaphore to signal */ ) { if (semGive ((SEM_ID) semHandle) != OK) return ossStatus (S_ossLib_BAD_HANDLE); return OK; }/***************************************************************************** ossSemTake - Attempts to take a semaphore** ossSemTake() attempts to "take" the semaphore specified by <semHandle>.* <blockFlag> specifies the blocking behavior. OSS_BLOCK blocks indefinitely* waiting for the semaphore to be signalled. OSS_DONT_BLOCK does not block* and returns an error if the semaphore is not in the signalled state. Other* values of <blockFlag> are interpreted as a count of milliseconds to wait* for the semaphore to enter the signalled state before declaring an error.** RETURNS: OK or ERROR*/STATUS ossSemTake ( SEM_HANDLE semHandle, /* semaphore to take */ UINT32 blockFlag /* specifies blocking action */ ) { if (semTake ((SEM_ID) semHandle, TIMEOUT (blockFlag)) != OK) return ossStatus (translateError ()); return OK; }/***************************************************************************** ossMutexCreate - Creates a new mutex** This function creates a new mutex and returns the handle of that* mutex in <pMutexHandle>. The mutex is created in the "untaken" state.** RETURNS: OK or STATUS** ERRNO:* S_ossLib_BAD_PARAMETER* S_ossLib_GENERAL_FAULT*/STATUS ossMutexCreate ( pMUTEX_HANDLE pMutexHandle /* Handle of newly created mutex */ ) { /* Validate parameters */ if (pMutexHandle == NULL) return ossStatus (S_ossLib_BAD_PARAMETER); if ((*pMutexHandle = (MUTEX_HANDLE) semMCreate (SEM_Q_FIFO)) == NULL) return ossStatus (S_ossLib_GENERAL_FAULT); return OK; }/***************************************************************************** ossMutexDestroy - Destroys a mutex** Destroys the mutex <mutexHandle> created by ossMutexCreate().** RETURNS: OK or ERROR** ERRNO:
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -