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

📄 wmplatform.c

📁 WM9713 audio codec driver for WinCE 5.0
💻 C
字号:
/*-----------------------------------------------------------------------------
 * Copyright (c) Wolfson Microelectronics plc.  All rights reserved.
 *
 * This software as well as any related documentation is furnished under 
 * license and may only be used or copied in accordance with the terms of the 
 * license. The information in this file is furnished for informational use 
 * only, is subject to change without notice, and should not be construed as 
 * a commitment by Wolfson Microelectronics plc. Wolfson Microelectronics plc
 * assumes no responsibility or liability for any errors or inaccuracies that
 * may appear in this document or any software that may be provided in
 * association with this document. 
 *
 * Except as permitted by such license, no part of this document may be 
 * reproduced, stored in a retrieval system, or transmitted in any form or by 
 * any means without the express written consent of Wolfson Microelectronics plc. 
 *
 * $Id: WMPlatform.c 2326 2005-10-20 08:40:38Z ib $
 *
 * This file contains the Windows CE platform layer for the drivers for
 * the Wolfson chips.
 *
 * Warning:
 *  This driver is specifically written for Wolfson Codecs. It is not a 
 *  general CODEC device driver.
 *
 *  This platform file is specifically designed to work Windows CE.  There
 *  is no guarantee of correct operation with other platforms/controllers.
 *
 * -----------------------------------------------------------------------------*/

/*
 * Include files
 */
#include "WMCommon.h"
#include "WMPlatform_OS.h"
#include <wavedbg.h>
#include <CEDDK.h>

/*
 * Global definitions
 */
#define BUFSIZE     255

/*
 * Public data.
 */

/*
 * Private data
 */

/*
 * Function prototypes
 */
void private_char2tchar( TCHAR *dest, const char *src );
void private_char2wchar( wchar_t *dest, const char *src );

/*-----------------------------------------------------------------------------
 * Function:    WMMapMemory
 *
 * Allocate an area of memory and copy from the specified physical address.
 *
 * Parameters:
 *      hDevice			handle to the device (from WMOpenDevice)
 *      size			The size, in bytes, of the region.
 *      str				Error message string indicating what was being mapped
 *		pAddress		Pointer to the physical memory.
 *
 * Returns:     void *
 *              The base address of the allocated region of pages 
 *              indicates success.
 *				NULL indicates failure.
 *---------------------------------------------------------------------------*/
void * WMMapMemory( WM_DEVICE_HANDLE hDevice,
                    unsigned size,
                    char *str,
                    void *pAddress
                  )
{
    PHYSICAL_ADDRESS RegPA;
    void *ptr;

    RegPA.QuadPart = (LONGLONG) pAddress;
	ptr = MmMapIoSpace( RegPA, size, FALSE );      /* FALSE = don't cache */
	if ( !ptr )
	{
        WM_TRACE( hDevice, (
                  "MmMapIoSpace of 0x%X failed! %s : size=0x%X, (0x%X)",
                  pAddress,
                  str,
                  size,
                  GetLastError()
                ));
	}
    
    return ptr;
}

/*-----------------------------------------------------------------------------
 * Function:    WMAllocateSharedMemory
 *
 * Allocate memory which is shared across all processes (i.e. so all processes
 * can access the same area of memory and have a shared common state).
 *
 * Parameters:
 *      hDevice        handle to the device (from WMOpenDevice)
 *      size           The size, in bytes, to allocate
 *      name           The "name", or unique identifier, of the shared memory.
 *
 * Returns:     void *
 *      The virtual address, or NULL on failure.
 *---------------------------------------------------------------------------*/
void *WMAllocateSharedMemory( WM_DEVICE_HANDLE hDevice,
                              unsigned size,
                              const TCHAR *name
                            )
{
    void    *pWMData = NULL;
    HANDLE  hGlobalMemory = NULL;

    /*
     * Create a file mapping to back our data.
     */
    hGlobalMemory = CreateFileMapping( INVALID_HANDLE_VALUE,
                                       NULL,
                                       PAGE_READWRITE,
                                       0,
                                       size,
                                       name
                                     );
    if ( !hGlobalMemory ) 
    {
        goto error0;
    }
    
    /*
     * Now map it into our address space.
     */
    pWMData = MapViewOfFile( hGlobalMemory,
                             FILE_MAP_WRITE,
                             0, 
                             0, 
                             0 
                           );
    if ( !pWMData )
    {
        goto error1;
    }
    
    /*
     * Close the handle - we'll not be using it again.
     */
    CloseHandle( hGlobalMemory );
    
    /*
     * We're done.
     */
    return pWMData;
    
    /*
     * Error cleanup.
     */
error1:
    CloseHandle( hGlobalMemory );

error0:
    return NULL;
}

/*-----------------------------------------------------------------------------
 * Function:    WMFreeSharedMemory
 *
 * Free memory allocated using ALLOC_SHARED_MEMORY.
 *
 * Parameters:
 *      pAddr       pointer to the virtual memory to be released.
 *
 * Returns:     void
 *---------------------------------------------------------------------------*/
void WMFreeSharedMemory( void *pAddr )
{
    UnmapViewOfFile( pAddr );
}

/*-----------------------------------------------------------------------------
 * Function:    WMAllocateBufferMemory
 *
 * Allocate an area of physical memory and return both physical and virtual
 * addresses.
 *
 * Parameters:
 *      hDevice         handle to the device (from WMOpenDevice)
 *      size            The size, in bytes, to allocate.
 *      str             Error message string indicating what was being allocated
 *      ppPhysicalAddr  Variable to receive physical address
 *
 * Returns:     void *
 *      The virtual address, or NULL on failure.
 *---------------------------------------------------------------------------*/
void *WMAllocateBufferMemory( WM_DEVICE_HANDLE hDevice,
                              unsigned size,
                              char *str,
                              void **ppPhysicalAddr
                            )
{
#if ALLOC_BUFFER_MEMORY_AVAILABLE
    DMA_ADAPTER_OBJECT  Adapter;
    PHYSICAL_ADDRESS    PA;
    void                *pVirtualAddr;
    
    Adapter.ObjectSize    = sizeof(DMA_ADAPTER_OBJECT);
    Adapter.InterfaceType = Internal;
    Adapter.BusNumber     = 0;

    pVirtualAddr = (void *)
              HalAllocateCommonBuffer( &Adapter, 
                                       size, 
                                       &PA, 
                                       FALSE
                                     );

    if ( pVirtualAddr )
    {
        *ppPhysicalAddr = (void *)PA.LowPart;
    }
    else
    {
        WM_TRACE( hDevice, (
                  "HalAllocateCommonBuffer failed! %s : size=0x%X, (0x%X)",
                  str,
                  size,
                  GetLastError()
                ));
    }
    
    return pVirtualAddr;
    
#else
    /*
     * No common buffer allocation available - fail.
     */
    return NULL;
#endif
}

/*-----------------------------------------------------------------------------
 * Function:    WMFreeBufferMemory
 *
 * Releases the buffer memory allocated using ALLOC_OR_MAP_BUFFER.
 *
 * Parameters:
 *      pVirtualAddr    pointer to the virtual memory to be released.
 *      pPhysicalAddr   pointer to the physical memory to be released.      
 *      size            size of the memory to be released.
 *
 * Returns:     void
 *---------------------------------------------------------------------------*/
void WMFreeBufferMemory( void *pVirtualAddr, 
                         void *pPhysicalAddr, 
                         unsigned long size 
                       )
{
#if ALLOC_BUFFER_MEMORY_AVAILABLE

    DMA_ADAPTER_OBJECT Adapter;
    PHYSICAL_ADDRESS   PA;

    Adapter.ObjectSize    = sizeof(DMA_ADAPTER_OBJECT);
    Adapter.InterfaceType = Internal;
    Adapter.BusNumber     = 0;

    PA.LowPart = (unsigned long) pPhysicalAddr;

    HalFreeCommonBuffer( &Adapter,
                         size,
                         PA,
                         pVirtualAddr,
                         FALSE
                        );
                        
#else /* ALLOC_BUFFER_MEMORY_AVAILABLE */

    VirtualFree( pVirtualAddr, 0, MEM_RELEASE );
    
#endif /*ALLOC_BUFFER_MEMORY_AVAILABLE */
}

/*-----------------------------------------------------------------------------
 * Function:    WMGetNamedEvent
 *
 * returns an event based on the name that is passed it.
 *
 * Parameters:
 *      hDevice handle to the device (from WMOpenDevice)
 *      name    The name of the event.
 *
 * Returns:     handle to the event.
 *---------------------------------------------------------------------------*/
WMEvent_t WMGetNamedEvent( WM_DEVICE_HANDLE hDevice, char *name )
{
    WMSTATUS status;
	WMEvent_t event;

	TCHAR tName[WM_EVENT_STRING] = TEXT("WMGetNamedEvent");

	private_char2tchar(tName,name);

    status = CREATE_EVENT( &event, tName );

    WM_ASSERT( hDevice, WM_SUCCESS(status) );
    
	return event;
}

/*-----------------------------------------------------------------------------
 * Function:    WMFormatString
 *
 * returns a formatted string.
 *
 * Parameters:
 *      output  The formatted string.
 *		format  The format string.
 *      ...     Format parameters
 *
 * Returns:     void
 *---------------------------------------------------------------------------*/
void WMFormatString( char * output, const char *format, ... )
{
    va_list args;

    va_start( args, format );
    vsprintf( output, format, args );
    va_end( args );
}

/*-----------------------------------------------------------------------------
 * Debugging functions (also used during testing).
 */
#if defined(DEBUG) || WM_TESTING

/*-----------------------------------------------------------------------------
 * Function:    WMTrace
 *
 * Outputs a debug message on its own line.  This should append a carriage
 * return if the underlying trace mechanism does not.
 *
 * NB This function should only be called if system calls are allowed.  It does
 * no checking of its own.  The WM_TRACE macro makes the check and should
 * always be used instead if there is any doubt.
 *
 * Parameters:
 *		format  The format string.
 *      ...     Format parameters
 *
 * Returns:     void
 *---------------------------------------------------------------------------*/
void WMTrace( const char *format, ... )
{
    char    buffer[BUFSIZE];
    wchar_t wBuffer[BUFSIZE];
    va_list args;

    va_start( args, format );
    vsprintf( buffer, format, args );
    buffer[BUFSIZE-1] = '\0';
    va_end( args );

    private_char2wchar( wBuffer, buffer );
    
    NKDbgPrintfW( wBuffer );
}

#endif  /* DEBUG */

#if defined(DEBUG) || WM_TESTING
/*-----------------------------------------------------------------------------
 * Function:    WMDebugAssert
 *
 * Outputs a message if it is safe to do so (WMSystemCallsAllowed(hDevice) is
 * TRUE or hDevice is NULL).  Throws a debug exception, or loops infinitely
 * if that's not possible.
 *
 * Parameters:
 *      hDevice handle to the device (from WMOpenDevice), or NULL if
 *              not available.
 *      file    Source file of assertion (from __FILE__)
 *      line    Source line of assertion (from __LINE__)
 *      text    The textual form of the assertion.
 *
 * Returns:     void
 *      Will only happen when debugging.
 *---------------------------------------------------------------------------*/
void WMDebugAssert( WM_DEVICE_HANDLE hDevice,
                    const char *file,
                    int line,
                    const char *text
                  )
{
    /* Trace to the output */
    if ( !hDevice || WMSystemCallsAllowed( hDevice ) )
        WMTrace( "%s(%d): assert failed: %s", file, line, text );
    
    /* And stop */
    WMDebugBreakpoint();
    
    return;
}

/*-----------------------------------------------------------------------------
 * Function:    WMDebugBreakpoint
 *
 * Puts a breakpoint into the code.
 *
 * Parameters:
 *      none
 *
 * Returns:     void
 *---------------------------------------------------------------------------*/
void WMDebugBreakpoint()
{
    DebugBreak();
}

#endif /* DEBUG || WM_TESTING */

/*-----------------------------------------------------------------------------
 * Function:    private_char2tchar
 *
 * Converts from char to TCHAR.
 *
 * Parameters:
 *      dest    TCHAR buffer to receive converted data.
 *      src     char buffer with text.
 *
 * Returns:     void
 *---------------------------------------------------------------------------*/
void private_char2tchar( TCHAR *dest, const char *src )
{
    while ( ( *dest++ = *src++ ) )
    {
        /* Do nothing */
    }
}

/*-----------------------------------------------------------------------------
 * Function:    private_char2wchar
 *
 * Converts from char to wchar_t.
 *
 * Parameters:
 *      dest    wchar_t buffer to receive converted data.
 *      src     char buffer with text.
 *
 * Returns:     void
 *---------------------------------------------------------------------------*/
void private_char2wchar( wchar_t *dest, const char *src )
{
    while ( ( *dest++ = *src++ ) )
    {
        /* Do nothing */
    }
}


/*------------------------------ END OF FILE ---------------------------------*/

⌨️ 快捷键说明

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