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

📄 avm_init.c

📁 sti5518 Audio and video memory standard API functions source file
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************

File name : avm_init.c

Description : Audio and video memory standard API functions source file


Date               Modification                                     Name
----               ------------                                     ----
17 jun 1999         Created                                          HG
 6 Oct 1999         Suppressed Open/Close and introduce
                    partitions functions                             HG
13 Oct 1999         Added base addresses as init params              HG
10 Jan 2000         Added video, SDRAM base addresses and size,
                    and cached memory map in init params.
                    Moved away init related to mem access module.    HG
 9 Feb 2000         Corrected problem with too long device names     HG
*******************************************************************************/


/* Includes ----------------------------------------------------------------- */

#include <stdlib.h>
#include <string.h>

#include "stlite.h"
#include "sttbx.h"
#include "stddefs.h"

#include "stavmem.h"
#include "avm_init.h"
#include "avm_allo.h"
#include "avm_acc.h"


/* Private Types ------------------------------------------------------------ */


/* Private Constants -------------------------------------------------------- */

#define STAVMEM_MAX_DEVICE  1
#define INVALID_DEVICE_INDEX (-1)


/* Private Variables (static)------------------------------------------------ */

static stavmem_Device_t DeviceArray[STAVMEM_MAX_DEVICE];
static semaphore_t FctAccessCtrl;   /* Init/Open/Close/Term protection semaphore */
static FirstInitDone = FALSE;


/* Global Variables --------------------------------------------------------- */


/* Private Macros ----------------------------------------------------------- */


/* Private Function prototypes ---------------------------------------------- */

static void EnterCriticalSection(void);
static void LeaveCriticalSection(void);
static S32 GetIndexOfDeviceNamed(const ST_DeviceName_t WantedName);
static ST_ErrorCode_t Init(stavmem_Device_t * const Device_p, const STAVMEM_InitParams_t * const InitParams_p);
static ST_ErrorCode_t Term(stavmem_Device_t * const Device_p, const STAVMEM_TermParams_t * const TermParams_p);


/* Functions ---------------------------------------------------------------- */


/*******************************************************************************
Name        : EnterCriticalSection
Description : Used to protect critical sections of Init/Open/Close/Term
Parameters  : None
Assumptions :
Limitations :
Returns     : Nothing
*******************************************************************************/
static void EnterCriticalSection(void)
{
    static FctAccessCtrlInitialized = FALSE;

    interrupt_lock();
    if (!FctAccessCtrlInitialized)
    {
        /* Initialise the Init/Open/Close/Term protection semaphore
          Caution: this semaphore is never deleted. (Not an issue) */
        semaphore_init_fifo(&FctAccessCtrl, 1);
        FctAccessCtrlInitialized = TRUE;
    }
    interrupt_unlock();

    /* Wait for the Init/Open/Close/Term protection semaphore */
    semaphore_wait(&FctAccessCtrl);
}

/*******************************************************************************
Name        : LeaveCriticalSection
Description : Used to release protection of critical sections of Init/Open/Close/Term
Parameters  : None
Assumptions :
Limitations :
Returns     : Nothing
*******************************************************************************/
static void LeaveCriticalSection(void)
{
    /* Release the Init/Open/Close/Term protection semaphore */
    semaphore_signal(&FctAccessCtrl);
}


/*******************************************************************************
Name        : Init
Description : API specific initialisations
Parameters  : pointer on device and initialisation parameters
Assumptions : pointer on device is not NULL
Limitations :
Returns     : ST_ErrorCode_t for the API Init function
*******************************************************************************/
static ST_ErrorCode_t Init(stavmem_Device_t * const Device_p, const STAVMEM_InitParams_t * const InitParams_p)
{
    MemBlockDesc_t *FirstBlock_p;
    U32 TotalBlocks = 0;
    ST_ErrorCode_t Err = ST_NO_ERROR;
    U32 Tmp = 0;
    stavmem_MemAccessInitParams_t MemAccessParams;

    /* Save constants */
    Device_p->MaxUsedBlocks = InitParams_p->MaxBlocks;
    Device_p->CurrentlyUsedBlocks = 0;
    Device_p->MaxForbiddenRanges = InitParams_p->MaxForbiddenRanges;
    Device_p->MaxForbiddenBorders = InitParams_p->MaxForbiddenBorders;

    /* Total number of blocks can be potentially:
        + (2*Init->MaxBlocks)+1 blocks alterning used/unused blocks,
        + (2*NumberOfSubSpace)+1 blocks because of reserved/sub-space blocks,
        + 2*MaxForbiddenRanges blocks when marking forbidden blocks for allocation,
        + MaxForbiddenBorders blocks when cutting unused blocks in 2 for allocation */
    TotalBlocks = 2 * (InitParams_p->MaxBlocks + InitParams_p->NumberOfMemoryMapRanges + Device_p->MaxForbiddenRanges) + 1 + 1 + Device_p->MaxForbiddenBorders;
    Device_p->TotalAllocatedBlocks = TotalBlocks;
    /* Allocate memory for internal use: for as much block structures as required */
    Device_p->MemoryAllocated_p = memory_allocate(InitParams_p->CPUPartition_p, TotalBlocks * sizeof(MemBlockDesc_t));
    if (Device_p->MemoryAllocated_p == NULL)
    {
        /* If allocation failed, quit with failure */
        return(ST_ERROR_NO_MEMORY);
    }

    /* Save the partition where the memory allocation for internal use was done */
    Device_p->CPUPartition_p = InitParams_p->CPUPartition_p;
    /* Remember first block address to test block handle validity */
    Device_p->FirstBlock_p = Device_p->MemoryAllocated_p;

    /* First block is at the first address of allocated memory */
    FirstBlock_p = (MemBlockDesc_t *) Device_p->MemoryAllocated_p;

    /* Initialise first block */
    FirstBlock_p->BlockAbove_p = NULL;
    FirstBlock_p->BlockBelow_p = NULL;
    FirstBlock_p->Alignment = 1;
    FirstBlock_p->StartAddr_p = (void *) 0;
    FirstBlock_p->AlignedStartAddr_p = (void *) 0;
    FirstBlock_p->Size = 0;     /* 0 means full-range size: all space is considered */
    FirstBlock_p->UserSize = FirstBlock_p->Size;
    FirstBlock_p->AllocMode = STAVMEM_ALLOC_MODE_RESERVED;  /* All space is reserved before un-reservation */
    FirstBlock_p->IsUsed = TRUE;

    /* Position space top and bottom blocks */
    Device_p->SpaceVeryBottomBlock_p = FirstBlock_p;
    Device_p->SpaceVeryTopBlock_p = FirstBlock_p;

    /* Initalise space free blocks list */
    for (Tmp = 1; Tmp < TotalBlocks - 1; Tmp++)
    {
        FirstBlock_p[Tmp].BlockBelow_p = &FirstBlock_p[Tmp + 1];
        FirstBlock_p[Tmp].AllocMode = STAVMEM_ALLOC_MODE_INVALID;
    }
    /* Write end of list pointer */
    FirstBlock_p[1].BlockAbove_p = NULL;
    FirstBlock_p[TotalBlocks - 1].BlockBelow_p = NULL;
    Device_p->TopOfSpaceFreeBlocksList_p = &FirstBlock_p[1];

    /* Initialise the space as a list of not used / reserved blocks */
    /* For this, start for the reserved entire space and un-reserve
       InitParams-defined sub-spaces */
    for (Tmp = 0; Tmp < InitParams_p->NumberOfMemoryMapRanges; Tmp++)
    {
        /* Un-reserve */
        Err = stavmem_UnReserveBlockNoSemaphore(Device_p, &(InitParams_p->MemoryMapRanges_p[Tmp]));
        if (Err != ST_NO_ERROR)
        {
            /* Should not happend */
            return(ST_ERROR_NO_MEMORY);
        }
    }

    /* Initialise allocations locking semaphore */
    semaphore_init_fifo(&(Device_p->LockAllocSemaphore), 1);    /* Create allocations locking semaphore */

    /* Initialise memory accesses */
    MemAccessParams.CPUPartition_p = InitParams_p->CPUPartition_p;
    MemAccessParams.BlockMoveDmaBaseAddr_p = InitParams_p->BlockMoveDmaBaseAddr_p;
    MemAccessParams.CacheBaseAddr_p = InitParams_p->CacheBaseAddr_p;
    MemAccessParams.VideoBaseAddr_p = InitParams_p->VideoBaseAddr_p;
    MemAccessParams.SDRAMBaseAddr_p = InitParams_p->SDRAMBaseAddr_p;
    MemAccessParams.SDRAMSize       = InitParams_p->SDRAMSize;
    MemAccessParams.NumberOfDCachedAreas = InitParams_p->NumberOfDCachedRanges;
    MemAccessParams.DCachedRanges_p = InitParams_p->DCachedRanges_p;
    MemAccessParams.OptimisedMemAccessStrategy_p = InitParams_p->OptimisedMemAccessStrategy_p;
    Err = stavmem_MemAccessInit(&MemAccessParams);

    return(Err);
}



/*******************************************************************************
Name        : Term
Description : API specific terminations
Parameters  : pointer on device and termination parameters
Assumptions : pointer on device is not NULL
Limitations :
Returns     : ST_ErrorCode_t for the API Term function
*******************************************************************************/
static ST_ErrorCode_t Term(stavmem_Device_t * const Device_p, const STAVMEM_TermParams_t * const TermParams_p)
{
    ST_ErrorCode_t Err = ST_NO_ERROR;
    stavmem_MemAccessTermParams_t MemAccessParams;

    /* Terminate memory accesses */
    stavmem_MemAccessTerm(&MemAccessParams);

    /* De-allocate memory allocated for internal use at init() */
    memory_deallocate(Device_p->CPUPartition_p, Device_p->MemoryAllocated_p);

    /* Delete allocations locking semaphore */
    semaphore_delete(&(Device_p->LockAllocSemaphore));

    return(Err);
}



/*******************************************************************************
Name        : GetIndexOfDeviceNamed
Description : Get the index in DeviceArray where a device has been
              initialised with the wanted name, if it exists
Parameters  : the name to look for
Assumptions :
Limitations :
Returns     : the index if the name was found, INVALID_DEVICE_INDEX otherwise
*******************************************************************************/
static S32 GetIndexOfDeviceNamed(const ST_DeviceName_t WantedName)
{
    S32 WantedIndex = INVALID_DEVICE_INDEX, Index = 0;

    do
    {
        /* Device initialised: check if name is matching */
        if (strcmp(DeviceArray[Index].DeviceName, WantedName) == 0)
        {
            /* Name found in the initialised devices */
            WantedIndex = Index;
        }
        Index++;
    } while ((WantedIndex == INVALID_DEVICE_INDEX) && (Index < STAVMEM_MAX_DEVICE));

    return(WantedIndex);
}


⌨️ 快捷键说明

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