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

📄 stos.c

📁 stos Linux 源码示范程序。 可以移植到其他平台
💻 C
📖 第 1 页 / 共 3 页
字号:
/*******************************************************************************
Name        : STOS_SemaphoreWaitTimeOut
Description : OS-independent implementation of semaphore_wait_timeout
Parameters  : semaphore to wait for, time out value
Assumptions :
Limitations :
Returns     : STOS_SUCCESS if success, STOS_FAILURE if timeout occurred
*******************************************************************************/
int STOS_SemaphoreWaitTimeOut(semaphore_t * Semaphore_p, const STOS_Clock_t * TimeOutValue_p)
{
    assert(Semaphore_p != NULL);

#if (defined (ST_OS20)) || (defined (ST_OS21)) || (defined (ST_OSLINUX))
    return(semaphore_wait_timeout(Semaphore_p, (STOS_Clock_t *) TimeOutValue_p));
#else
    #error STOS_SemaphoreWaitTimeOut() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_memsetUncached
Description : Does an uncached CPU access to the given address and fills the
              given area with the given pattern
Parameters  : the address of the first byte, the pattern, the area size
Assumptions : None
Limitations :
Returns     : None
*******************************************************************************/
void STOS_memsetUncached(const void* const PhysicalAddress, U8 Pattern, U32 Size)
{
#if defined ST_OSLINUX && !defined MODULE
    /* MODULE is a Linux specific define, if not defined current working space is USER space */
    STTBX_Print(("%s(): Feature not supported under Linux in User space!\n", __FUNCTION__));
    return;

#else

    U32 i;

    for (i = 0; i < Size; i++)
    {
        STSYS_WriteRegMemUncached8((U8*)((U32)PhysicalAddress + i), Pattern);
    }
#endif
} /* End of STOS_memsetUncached() function */

/*******************************************************************************
Name        : STOS_memcpyUncachedToUncached
Description : Does only uncached read/write CPU accesses for copying data from
              src to dest
Parameters  : the address of the src, the address of the dest, the copy size
Assumptions : None
Limitations :
Returns     : None
*******************************************************************************/
void STOS_memcpyUncachedToUncached(const void* const uncached_dest, const void* const uncached_src, U32 Size)
{
#if defined ST_OSLINUX && !defined MODULE
    /* MODULE is a Linux specific define, if not defined current working space is USER space */
    STTBX_Print(("%s(): Feature not supported under Linux in User space!\n", __FUNCTION__));
    return;

#else
    U32 i;

#ifdef ST_OSLINUX
    if (uncached_dest == uncached_src)
    {
        return;
    }
#endif

    for (i = 0; i < Size; i++)
    {
        STSYS_WriteRegMemUncached8((U8*)((U32)uncached_dest + i), STSYS_ReadRegMemUncached8((U8*)((U32)uncached_src + i)));
    }
#endif
} /* End of STOS_memcpyUncachedToUncached() function */

/*******************************************************************************
Name        : STOS_memcpyCachedToUncached
Description : Does only uncached write CPU accesses and cached read CPU accesses
              for copying data from src to dest
Parameters  : the address of the src, the address of the dest, the copy size
Assumptions : None
Limitations :
Returns     : None
*******************************************************************************/
void STOS_memcpyCachedToUncached(const void* const uncached_dest, const void* const cached_src, U32 Size)
{
#if defined ST_OSLINUX && !defined MODULE
    /* MODULE is a Linux specific define, if not defined current working space is USER space */
    STTBX_Print(("%s(): Feature not supported under Linux in User space!\n", __FUNCTION__));
    return;

#else
    U32 i;

#ifdef ST_OSLINUX
    if ( (((U32)uncached_dest & MASK_STBUS_FROM_ST40) | REGION2) == (((U32)cached_src & MASK_STBUS_FROM_ST40) | REGION2) )
    {
        return;
    }
#endif

    for (i = 0; i < Size; i++)
    {
        STSYS_WriteRegMemUncached8((U8*)((U32)uncached_dest + i), ((U8*)cached_src)[i]);
    }
#endif
} /* End of STOS_memcpyCachedToUncached() function */

/*******************************************************************************
Name        : STOS_memcpyUncachedToCached
Description : Does only cached write CPU accesses and uncached read CPU accesses
              for copying data from src to dest
Parameters  : the address of the src, the address of the dest, the copy size
Assumptions : None
Limitations :
Returns     : None
*******************************************************************************/
void STOS_memcpyUncachedToCached(void* const cached_dest, const void* const uncached_src, U32 Size)
{
#if defined ST_OSLINUX && !defined MODULE
    /* MODULE is a Linux specific define, if not defined current working space is USER space */
    STTBX_Print(("%s(): Feature not supported under Linux in User space!\n", __FUNCTION__));
    return;

#else
    U32 i;

#ifdef ST_OSLINUX
    if ( (((U32)cached_dest & MASK_STBUS_FROM_ST40) | REGION2) == (((U32)uncached_src & MASK_STBUS_FROM_ST40) | REGION2) )
    {
        return;
    }
#endif

    for (i = 0; i < Size; i++)
    {
        ((U8*)cached_dest)[i] = STSYS_ReadRegMemUncached8((U8*)((U32)uncached_src + i));
    }
#endif
} /* End of STOS_memcpyUncachedToCached() function */

/*******************************************************************************
Name        : STOS_TaskDelay
Description : Asleep the task for a given amount of ticks
Parameters  : number of ticks to wait (none if ticks <= 0)
Assumptions : None
Limitations :
Returns     : None
*******************************************************************************/
#if defined ST_OSLINUX && !defined MODULE
void STOS_TaskDelay(int ticks)
{
    struct timespec x;

    if (ticks <= 0)
    {
        STTBX_Print(("%s(): WARNING !!! Inadequate number of ticks (0x%.8x), this may hide problem !\n", __FUNCTION__, (U32)ticks));
        if (ticks < 0)
        {
            return;
        }
    }

    /* if < 2ms */    
    if (ticks <= (2 * STLINUX_GetClocksPerSecond() / 1000 ))
    {
        /* Waits 2ms */
        x.tv_sec  = 0;
        x.tv_nsec = 2000001;
    }
    else
    {
        x.tv_sec  = ticks / STLINUX_GetClocksPerSecond();
        x.tv_nsec = (((ticks % STLINUX_GetClocksPerSecond()) * 1000000) / STLINUX_GetClocksPerSecond()) * 1000;
    }

    nanosleep (&x, 0);
}
#endif  /* LINUX & !MODULE */

/*******************************************************************************
Name        : STOS_TaskDelayUs
Description : Asleep the task for a given time in microseconds
Parameters  : number of microseconds to wait (none if microsec <= 0)
Assumptions : None
Limitations :
Returns     : None
*******************************************************************************/
#if defined ST_OSLINUX && !defined MODULE
void STOS_TaskDelayUs(int microsec)
{
    struct timespec x;

    /* microsec compared to duration of a tick in us */
    if (microsec <= 1000000/STLINUX_GetClocksPerSecond())
    {
        /* Required duration is to low compared to duration of tick */
        STTBX_Print(("%s(): WARNING !!! Inadequate number of us (0x%.8x), this may hide problem !\n", __FUNCTION__, (U32)microsec));
        if (microsec < 0)
        {
            return;
        }
    }

    /* if < 2ms */    
    if (microsec <= 2000)
    {
        /* Waits 2ms */
        x.tv_sec  = 0;
        x.tv_nsec = 2000001;
    }
    else
    {
        x.tv_sec  = microsec / 1000000;
        x.tv_nsec = microsec % 1000000;
    }

    nanosleep (&x, 0);
}
#endif  /* LINUX & !MODULE */

/*******************************************************************************
Name        : STOS_TaskDelayUntil
Description : Asleep the task until given absolute time is reached
Parameters  : wake up absolute time
Assumptions : None
Limitations :
Returns     : None
*******************************************************************************/
#if defined ST_OSLINUX && !defined MODULE
void STOS_TaskDelayUntil(STOS_Clock_t timeout)
{
    int Delay;

    if ((Delay = STOS_time_minus(timeout, STOS_time_now())) > 0)
    {
        STOS_TaskDelay(Delay);
    }
}
#endif  /* LINUX & !MODULE */


/*******************************************************************************
Name        : STOS_InterruptUninstall
Description : free interrupt
Parameters  : interrupt number, level and parameters
Assumptions : None
Limitations :
Returns     : STOS_SUCCESS/STOS_FAILURE
*******************************************************************************/
STOS_Error_t STOS_InterruptInstall( U32 InterruptNumber,
                                    U32 InterruptLevel,
                                    STOS_INTERRUPT_PROTOTYPE(Fct),
                                    char * IrqName,
                                    void * Params)
{
#if defined ST_OSLINUX
#ifdef MODULE
    return (request_irq(InterruptNumber,
                        Fct,
                        SA_INTERRUPT /* fast interrupt */,
                        IrqName,
                        Params) == 0 ? STOS_SUCCESS : STOS_FAILURE);

#else
    STTBX_Print(("%s() cannot be used in USER mode!\n", __FUNCTION__));

    return STOS_FAILURE;
#endif    

#elif defined ST_OS21
    return (interrupt_install(InterruptNumber,
                              InterruptLevel,
                              Fct,
                              Params) == OS21_SUCCESS ? STOS_SUCCESS : STOS_FAILURE);

#elif defined ST_OS20
    return (interrupt_install(InterruptNumber,
                              InterruptLevel,
                              Fct,
                              Params) == 0 ? STOS_SUCCESS : STOS_FAILURE);
#endif
}

/*******************************************************************************
Name        : STOS_InterruptUninstall
Description : free interrupt
Parameters  : interrupt number, level and parameters
Assumptions : None
Limitations :
Returns     : STOS_SUCCESS/STOS_FAILURE
*******************************************************************************/
STOS_Error_t STOS_InterruptUninstall(U32 InterruptNumber, U32 InterruptLevel, void * Params)
{
#if defined ST_OSLINUX
#ifdef MODULE
    free_irq(InterruptNumber, Params);
    return STOS_SUCCESS;
#else
    STTBX_Print(("%s() cannot be used in USER mode!\n", __FUNCTION__));
    return STOS_FAILURE;
#endif    

#elif defined ST_OS21
    Params = Params;    /* To avoid warnings */
    return (interrupt_uninstall(InterruptNumber, InterruptLevel) == OS21_SUCCESS ? STOS_SUCCESS : STOS_FAILURE);

#elif defined ST_OS20
    Params = Params;    /* To avoid warnings */
    return (interrupt_uninstall(InterruptNumber, InterruptLevel) == 0 ? STOS_SUCCESS : STOS_FAILURE);
#endif
}

/* End of stos.c */

⌨️ 快捷键说明

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