⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stos.c

📁 stos Linux 源码示范程序。 可以移植到其他平台
💻 C
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************

File name   :  stos.c

Description :  Operating system independence file.

               COPYRIGHT (C) STMicroelectronics 2005.

Date               Modification                                          Name
----               ------------                                          ----
04/05/2005         Added support for semaphore calls                     DG
05/01/2005         Created                                               MH

*****************************************************************************/

/*#######################################################################*/
/*########################## INCLUDES FILE ##############################*/
/*#######################################################################*/

/* --- Includes -------------------------------------------------------- */
#if !defined ST_OSLINUX || (defined ST_OSLINUX && !defined MODULE)
/* MODULE is a Linux specific define, if not defined current working space is USER space */
#include <stdio.h>
#include <assert.h>
#endif

#if defined ST_OSLINUX && defined MODULE
#include <linux/kthread.h>
#include <linux/kernel.h>
#endif

#include "stos.h"

#if !defined ST_OSLINUX
#include "stsys.h"
/* Define to add debug info */
/* define STOS_DEBUG */
#if defined(STOS_DEBUG)
#include "sttbx.h"
#else
#define STTBX_Print(x) {}
#endif
#endif  /* ST_OSLINUX */

/*#######################################################################*/
/*########################### DEFINITION ################################*/
/*#######################################################################*/

/* --- Constants (default values) -------------------------------------- */

/* --- Global variables ------------------------------------------------ */

/* --- Prototype ------------------------------------------------------- */

/* --- Externals ------------------------------------------------------- */


/*******************************************************************************
Name        : STOS_GetRevision
Description : Get revision of the STOS driver
Parameters  :
Assumptions :
Limitations :
Returns     :
*******************************************************************************/
ST_Revision_t STOS_GetRevision(void)
{
  static const char Revision[] = "STOS-REL_1.4.2";

  return((ST_Revision_t) Revision);
}

/*******************************************************************************
Name        : STOS_TaskCreate
Description : Common call to OS20 and OS21 for task creation
Parameters  :
Assumptions :
Limitations :
Returns     : ST_NO_ERROR, ST_ERROR_NO_MEMORY
*******************************************************************************/
ST_ErrorCode_t  STOS_TaskCreate   (void (*Function)(void* Param),
                                        void* Param,
                                        partition_t* StackPartition,
                                        size_t StackSize,
                                        void** Stack,
                                        partition_t* TaskPartition,
                                        task_t** Task,
                                        tdesc_t* Tdesc,
                                        int Priority,
                                        const char* Name,
                                        task_flags_t Flags )
{
#if defined (ST_OS20)

    ST_ErrorCode_t Error = ST_NO_ERROR;

    *Stack = (void *) memory_allocate(StackPartition, StackSize);
    if(*Stack == NULL)
    {
        STTBX_Print(("ERROR: Couldn't Allocate memory for task %s's stack\n", Name));
        Error = ST_ERROR_NO_MEMORY;
    }
    else
    {
        *Task = (void *) memory_allocate(TaskPartition, sizeof(task_t));
        if(*Task == NULL)
        {
            STTBX_Print(("ERROR: Couldn't Allocate memory for task %s\n", Name));

            /* deallocate memory for the stack*/
            memory_deallocate((void *) StackPartition, *Stack);
            Error = ST_ERROR_NO_MEMORY;
        }
    }

    if(Error == ST_NO_ERROR)
    {
        Error = task_init( Function, Param, *Stack, StackSize, *Task, Tdesc, Priority, Name, 0);

        if (Error != STOS_SUCCESS)
        {
            STTBX_Print(("ERROR: Couldn't create task %s\n", Name));
            Error = ST_ERROR_NO_MEMORY;
        }
    }
    return Error;

#elif defined (ST_OS21)

    *Task = task_create_p(TaskPartition, Function, Param, StackPartition, StackSize,
                          Priority, Name, Flags | task_flags_no_min_stack_size);

    if(*Task == NULL)
    {
        STTBX_Print(("ERROR: Couldn't create task %s\n", Name ));
        return ST_ERROR_NO_MEMORY;
    }
    return(ST_NO_ERROR);
#elif defined (ST_OSLINUX)

#ifdef MODULE
/* MODULE is a Linux specific define, if not defined current working space is USER space */
    struct sched_param  SchedParam;

    *Task = kthread_run((int(*)(void *))Function, (void *) Param, "%s", Name);
    if( IS_ERR((*Task)) )
    {
        STTBX_Print(("%s: Error task creation.\n", Name));
        return(ST_ERROR_BAD_PARAMETER);
    }
    else
    {
        SchedParam.sched_priority = Priority;
        if (STLINUX_sched_setscheduler((*Task)->pid, SCHED_RR, &SchedParam) < 0)
        {
            STTBX_Print(("%s: could not switch thread %d to RT\n", Name, (*Task)->pid));
        }
        else
        {
            STTBX_Print(("Thread %s created: RT priority=%d, PID=%d\n", Name, Priority, (*Task)->pid));
        }
    }

    return(ST_NO_ERROR);
#else
    if (pthread_create(*Task, NULL, (void*)Function, (void *)Param))
	{
        return(ST_ERROR_BAD_PARAMETER);
	}
    return(ST_NO_ERROR);
#endif  /* MODULE */

#else
    #error STOS_TaskCreate() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_TaskDelete
Description : Common call to OS20 and OS21 for task deletion
Parameters  :
Assumptions :
Limitations :
Returns     : ST_NO_ERROR or ST_ERROR_TIMEOUT
*******************************************************************************/
ST_ErrorCode_t  STOS_TaskDelete ( task_t* Task,
                                  partition_t* TaskPartition,
                                  void* Stack,
                                  partition_t* StackPartition )
{
#if defined (ST_OS20)

    ST_ErrorCode_t Error = ST_NO_ERROR;

    if(task_delete(Task) != 0)
    {
        STTBX_Print(("ERROR: Couldn't delete Task - it may not have terminated...\n"));
        Error = ST_ERROR_TIMEOUT;
    }
    else
    {
        /* Deallocate the task memory */
        memory_deallocate((void *)TaskPartition, Task);

        /* Deallocate the stack memory */
        memory_deallocate((void *)StackPartition, Stack);
    }
    return Error;
#elif defined (ST_OS21)

    if( task_delete(Task) == STOS_SUCCESS)
    {
        STTBX_Print(("Task Deleted Successfully\n"));
        return ST_NO_ERROR;
    }
    else
    {
        STTBX_Print(("ERROR: Couldn't delete Task - it may not have terminated...\n"));
        return ST_ERROR_TIMEOUT;
    }

#elif defined (ST_OSLINUX)

#ifdef MODULE
/* MODULE is a Linux specific define, if not defined current working space is USER space */
    /* Nothing to do */
    return ST_NO_ERROR;
#else
    if (Task == NULL)    
    {
        return ST_ERROR_BAD_PARAMETER;
    }

    if (pthread_cancel(*Task))
    {
        STTBX_Print(("ERROR: Couldn't delete Task - it may not have terminated...\n"));
        return ST_ERROR_TIMEOUT;
    }

    return ST_NO_ERROR;
#endif  /* MODULE */

#else
    #error STOS_TaskDelete() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_TaskWait
Description : Common call to OS20 and OS21 for task deletion wait
Parameters  :
Assumptions :
Limitations :
Returns     : ST_NO_ERROR or ST_ERROR_TIMEOUT
*******************************************************************************/
ST_ErrorCode_t  STOS_TaskWait ( task_t** Task, const STOS_Clock_t * TimeOutValue_p )
{
#if defined (ST_OS20) || defined (ST_OS21)
    ST_ErrorCode_t   ret;
    ret = task_wait(Task, 1, (STOS_Clock_t *)TimeOutValue_p);

    return ret;

#elif defined (ST_OSLINUX)

#ifdef MODULE
/* MODULE is a Linux specific define, if not defined current working space is USER space */
    kill_proc((*Task)->pid, SIGKILL, 1);
    kthread_stop(*Task);

    return ST_NO_ERROR;
#else
    Task = Task;                        /* To avoid warnings */
    TimeOutValue_p = TimeOutValue_p;    /* To avoid warnings */
    
    return ST_NO_ERROR;
#endif  /* MODULE */

#else
    #error STOS_TaskWait() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_TaskEnter
Description : Common call to OS20, OS21 and LINUX for task entering
Parameters  :
Assumptions :
Limitations :
Returns     : ST_NO_ERROR
*******************************************************************************/
ST_ErrorCode_t  STOS_TaskEnter ( void * Param )
{
    Param = Param;   /* To avoid warnings */

#if defined (ST_OS20) || defined (ST_OS21)
    /* Nothing to do */

    return ST_NO_ERROR;

#elif defined (ST_OSLINUX)

#ifdef MODULE
    set_current_state(TASK_INTERRUPTIBLE);
    allow_signal(SIGKILL);

    return ST_NO_ERROR;
#else
    return ST_NO_ERROR;
#endif  /* MODULE */

#else
    #error STOS_TaskEnter() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_TaskExit
Description : Common call to OS20, OS21 and LINUX for task exiting
Parameters  :
Assumptions :

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -