📄 osapi.c
字号:
/*** File : osapi.c** Author : Joe-Paul Swinski*//* Revisions: 2/7/2005 by Nicholas J Yanchik * added global variables for name functionality: * * char* OS_task_name_table[OS_MAX_TASKS] * char* OS_mesage_q_name_table[OS_MAX_QUEUES] * char* OS_binary_sem_name_table[OS_MAX_SEMAPHORES] * char* OS_mutex_sem_name_table[OS_MAX_MUTEXES] * Also added initializations for the above variables is OS_API_Init() * * The Create() functions for the above were modified to return an Id to * the caller, not have the Id passed into it from the caller. * * Added functions: * uint32 OS_TaskGetIdByName (uint32 *task_id, char *task_name) * uint32 OS_QueueGetIdByName (uint32 *queue_id, char *queue_name) * uint32 OS_BinSemGetIdByName (uint32 *sem_id, char *sem_name) * uint32 OS_MutSemGetIdByName (uint32 *sem_id, char *sem_name) * * InfoGetTaskId was changed to OS_TaskGetId and moved to the Tasks section * * 2/8/05 - 2/9/05: * Added functions: * OS_TaskDelete (uint32 task_id) * OS_QueueDelete (uint32 queue_id) * OS_BinSemDelete (uint32 sem_id) * OS_MutSemDelete (uint32 sem_id) * *//**************************************************************************************** INCLUDE FILES****************************************************************************************/#include "vxWorks.h"#include "stdio.h"#include "stdlib.h"#include "string.h"#include "semLib.h"#include "taskLib.h"#include "sysLib.h"#include "msgQLib.h"#include "time.h"#include "intLib.h"#include "taskVarLib.h"#include "arch/ppc/ivPpc.h"#include "common_types.h"#include "osapi.h"/**************************************************************************************** DEFINES****************************************************************************************/#define MAX_PRIORITY 255#define UNINITIALIZED 0/**************************************************************************************** GLOBAL DATA****************************************************************************************/uint32 OS_FindCreator(void);/* tables for the properties of objects *//*tasks */typedef struct{ int free; int id; char name [OS_MAX_API_NAME]; int creator; uint32 stack_size; uint32 priority;}OS_task_record_t; /* queues */typedef struct{ int free; MSG_Q_ID id; /* a pointer to the id */ char name [OS_MAX_API_NAME]; int creator;}OS_queue_record_t;/* Binary Semaphores */typedef struct{ int free; SEM_ID id; /* a pointer to the id */ char name [OS_MAX_API_NAME]; int creator;}OS_bin_sem_record_t;/* Mutexes */typedef struct{ int free; SEM_ID id; char name [OS_MAX_API_NAME]; int creator;}OS_mut_sem_record_t;uint32 OS_task_key;uint32 OS_interrupt_lock_key;/* Tables where the OS object information is stored */OS_task_record_t OS_task_table [OS_MAX_TASKS];OS_queue_record_t OS_queue_table [OS_MAX_QUEUES];OS_bin_sem_record_t OS_bin_sem_table [OS_MAX_SEMAPHORES];OS_mut_sem_record_t OS_mut_sem_table [OS_MAX_MUTEXES];SEM_ID OS_task_table_sem;SEM_ID OS_queue_table_sem;SEM_ID OS_bin_sem_table_sem;SEM_ID OS_mut_sem_table_sem;/**************************************************************************************** INITIALIZATION FUNCTION****************************************************************************************//*--------------------------------------------------------------------------------------- Name: OS_API_Init Purpose: Initialize the tables that the OS API uses to keep track of information about objects returns: nothing---------------------------------------------------------------------------------------*/void OS_API_Init(void){ int i; /* Initialize Task Table */ for(i = 0; i < OS_MAX_TASKS; i++) { OS_task_table[i].free = TRUE; OS_task_table[i].id = UNINITIALIZED; OS_task_table[i].creator = UNINITIALIZED ; strcpy(OS_task_table[i].name,""); } /* Initialize Message Queue Table */ for(i = 0; i < OS_MAX_QUEUES; i++) { OS_queue_table[i].free = TRUE; OS_queue_table[i].id = NULL; OS_queue_table[i].creator = UNINITIALIZED ; strcpy(OS_queue_table[i].name,""); } /* Initialize Binary Semaphore Table */ for(i = 0; i < OS_MAX_SEMAPHORES; i++) { OS_bin_sem_table[i].free = TRUE; OS_bin_sem_table[i].id = NULL; OS_bin_sem_table[i].creator = UNINITIALIZED ; strcpy(OS_bin_sem_table[i].name,""); } /* Initialize Mutex Semaphore Table */ for(i = 0; i < OS_MAX_MUTEXES; i++) { OS_mut_sem_table[i].free = TRUE; OS_mut_sem_table[i].id = NULL; OS_mut_sem_table[i].creator = UNINITIALIZED ; strcpy(OS_mut_sem_table[i].name,""); } OS_task_table_sem = semBCreate(SEM_Q_PRIORITY, OS_SEM_FULL); OS_queue_table_sem = semBCreate(SEM_Q_PRIORITY, OS_SEM_FULL); OS_bin_sem_table_sem = semBCreate(SEM_Q_PRIORITY, OS_SEM_FULL); OS_mut_sem_table_sem = semBCreate(SEM_Q_PRIORITY, OS_SEM_FULL); return; } /* end OS_API_Init *//**************************************************************************************** TASK API****************************************************************************************//*--------------------------------------------------------------------------------------- Name: OS_TaskCreate Purpose: Creates a task and starts running it. returns: OS_INVALID_POINTER if any of the necessary pointers are NULL OS_ERR_NAME_TOO_LONG if the name of the task is too long to be copied OS_ERR_INVALID_PRIORITY if the priority is bad OS_ERR_NO_FREE_IDS if there can be no more tasks created OS_ERR_NAME_TAKEN if the name specified is already used by a task OS_ERROR if the operating system calls fail OS_SUCCESS if success NOTES: task_id is passed back to the user as the ID. stack_pointer is usually null. Flags are unused at this point.---------------------------------------------------------------------------------------*/int32 OS_TaskCreate (uint32 *task_id, const char *task_name, const void *function_pointer, const uint32 *stack_pointer, uint32 stack_size, uint32 priority, uint32 flags){ uint32 possible_taskid; uint32 i; /* we don't want to allow names too long*/ /* if truncated, two names might be the same */ /* Check for NULL pointers */ if( (task_name == NULL) || (function_pointer == NULL) || (task_id == NULL) ) return OS_INVALID_POINTER; if (strlen(task_name) > OS_MAX_API_NAME) return OS_ERR_NAME_TOO_LONG; /* Check for bad priority */ if (priority > MAX_PRIORITY) return OS_ERR_INVALID_PRIORITY; /* Check Parameters */ semTake(OS_task_table_sem,WAIT_FOREVER); for(possible_taskid = 0; possible_taskid < OS_MAX_TASKS; possible_taskid++) { if (OS_task_table[possible_taskid].free == 1) { break; } } semGive(OS_task_table_sem); /* Check to see if the id is out of bounds */ if( possible_taskid >= OS_MAX_TASKS || OS_task_table[possible_taskid].free != TRUE) return OS_ERR_NO_FREE_IDS; /* Check to see if the name is already taken */ semTake(OS_task_table_sem,WAIT_FOREVER); for (i = 0; i < OS_MAX_TASKS; i++) { if ((OS_task_table[i].free != TRUE) && ( strcmp(task_name, OS_task_table[i].name) == 0)) { semGive(OS_task_table_sem); return OS_ERR_NAME_TAKEN; } } semGive(OS_task_table_sem); /* Create VxWorks Task */ OS_task_table[possible_taskid].id = taskSpawn((char*)task_name, priority, flags, stack_size, (FUNCPTR)function_pointer, 0,0,0,0,0,0,0,0,0,0); /* check if taskSpawn failed */ if(OS_task_table[possible_taskid].id == ERROR) return OS_ERROR; /* Set the task_id to the id that was found available Set the name of the task, the stack size, and priority */ *task_id = possible_taskid; strcpy(OS_task_table[*task_id].name, task_name); /* this Id no longer free */ semTake(OS_task_table_sem,WAIT_FOREVER); OS_task_table[*task_id].free = FALSE; OS_task_table[*task_id].creator = OS_FindCreator(); OS_task_table[*task_id].stack_size = stack_size; OS_task_table[*task_id].priority = priority; semGive(OS_task_table_sem); return OS_SUCCESS; } /* end OS_TaskCreate *//*-------------------------------------------------------------------------------------- Name: OS_TaskDelete Purpose: Deletes the specified Task and removes it from the OS_task_table. returns: OS_ERR_INVALID_ID if the ID given to it is invalid OS_ERROR if the OS delete call fails OS_SUCCESS if success---------------------------------------------------------------------------------------*/int32 OS_TaskDelete (uint32 task_id){ int status; /* Check to see if the task_id given is valid */ if (task_id >= OS_MAX_TASKS || OS_task_table[task_id].free == TRUE) return OS_ERR_INVALID_ID; /* Try to delete the task */ status = taskDelete(OS_task_table[task_id].id); if (status == ERROR) { /* These statements are here for debugging purposes only */ if (errno == S_objLib_OBJ_ID_ERROR) printf("Error # %d S_objLib_OBJ_ID_ERROR\n\n", errno); else printf("I dont know this error number yet\n"); return OS_ERROR; } /* * Now that the task is deleted, remove its * "presence" in OS_task_table */ semTake(OS_task_table_sem,WAIT_FOREVER); OS_task_table[task_id].free = TRUE; OS_task_table[task_id].id = UNINITIALIZED; strcpy(OS_task_table[task_id].name, ""); OS_task_table[task_id].creator = UNINITIALIZED; OS_task_table[task_id].stack_size = UNINITIALIZED; OS_task_table[task_id].priority = UNINITIALIZED; semGive(OS_task_table_sem); return OS_SUCCESS; }/* end OS_TaskDelete *//*--------------------------------------------------------------------------------------- Name: OS_TaskDelay Purpose: Delay a task for specified amount of milliseconds returns: OS_ERROR if sleep fails OS_SUCCESS if success Notes: VxWorks uses the system clock to handle task delays. The system clock usually runs at 60Hz. This means that the resolution of the delay will be course. It rounds up.---------------------------------------------------------------------------------------*/int32 OS_TaskDelay (uint32 milli_seconds){ /* msecs rounded to the closest system tick count */ uint32 sys_ticks; sys_ticks = OS_Milli2Ticks(milli_seconds); /* if successful, the execution of task will pend here until delay finishes */ if(taskDelay(sys_ticks) != OK) return OS_ERROR; return OS_SUCCESS;}/* end OS_TaskDelay *//*--------------------------------------------------------------------------------------- Name: OS_TaskSetPriority Purpose: Sets the given task to a new priority returns: OS_ERR_INVALID_ID if the ID passed to it is invalid OS__ERR_INVALID_PRIORITY if the priority is greater than the max allowed OS_ERROR if the OS call to change the priority fails OS_SUCCESS if success---------------------------------------------------------------------------------------*/int32 OS_TaskSetPriority (uint32 task_id, uint32 new_priority){ /* Check Parameters */ if(task_id >= OS_MAX_TASKS || OS_task_table[task_id].free == TRUE) return OS_ERR_INVALID_ID; if (new_priority > MAX_PRIORITY) return OS_ERR_INVALID_PRIORITY; /* Set VxWorks Task Priority */ if(taskPrioritySet(OS_task_table[task_id].id, new_priority) != OK) return OS_ERROR; return OS_SUCCESS;}/* end OS_TaskSetPriority *//*--------------------------------------------------------------------------------------- Name: OS_TaskRegister Purpose: Registers the calling task id with the task by adding the var to the tcb It searches the OS_task_table to find the task_id corresponding to the t cb_id
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -