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

📄 pwmmdd.c

📁 Microsoft WinCE 6.0 BSP FINAL release source code for use with the i.MX27ADS TO2 WCE600_FINAL_MX27_S
💻 C
字号:
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//------------------------------------------------------------------------------
//
//  Copyright (C) 2004, Motorola Inc. All Rights Reserved
//
//-----------------------------------------------------------------------------
//
//  Copyright (C) 2004-2006, Freescale Semiconductor, Inc. All Rights Reserved.
//  THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
//  AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT 
//
//-----------------------------------------------------------------------------
//
//  File:  pwmmdd.c
//
//  This module provides a stream interface for the PWM
//  driver.  Client drivers can use the stream interface to
//  configure the PWM driver and run test programs.
//
//------------------------------------------------------------------------------
#include <windows.h>
#include <Devload.h>
#include "csp.h"
#include "pwmdefs.h"

//------------------------------------------------------------------------------
// External Functions
//------------------------------------------------------------------------------
extern BOOL BSPPwmSetClockGatingMode(BOOL startClocks);

//------------------------------------------------------------------------------
// External Variables
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Defines
//------------------------------------------------------------------------------

#define PWM_DEVICE_CONTEXT_HANDLE 0xCAFE

//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
// Global Variables
//------------------------------------------------------------------------------
#ifdef DEBUG
extern DBGPARAM dpCurSettings =
{
    _T("PWM"),
    {
        _T("Init"), _T("DeInit"), _T("Ioctl"), _T(""),
        _T(""), _T(""), _T(""), _T(""),
        _T(""),_T(""),_T(""),_T(""),
        _T("Info"),_T("Function"),_T("Warnings"),_T("Errors")
    },
    ZONEMASK_ERROR | ZONEMASK_WARN
};
#endif

//------------------------------------------------------------------------------
// Local Variables
//------------------------------------------------------------------------------
static CEDEVICE_POWER_STATE CurrentDx;

//------------------------------------------------------------------------------
// Local Functions
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
//
// Function: PWM_Init
//
// The Device Manager calls this function as a result of a call to the
// ActivateDevice() function.
//
// Parameters:
//      pContext
//          [in] Pointer to a string containing the registry path to the
//          active key for the stream interface driver.
//
// Returns:
//      Returns a handle to the device context created if successful. Returns
//      zero if not successful.
//
//------------------------------------------------------------------------------
DWORD PWM_Init(LPCTSTR pContext)
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("++PWM_Init\r\n")));

    if (!PwmInitialize())
        return 0;
    CurrentDx = D4;

    DEBUGMSG(ZONE_FUNCTION, (TEXT("--PWM_Init\r\n")));
    return PWM_DEVICE_CONTEXT_HANDLE;
}


//------------------------------------------------------------------------------
//
// Function: PWM_Deinit
//
// This function uninitializes a device.
//
// Parameters:
//      hDeviceContext
//          [in] Handle to the device context.
//
// Returns:
//      TRUE indicates success.
//
//------------------------------------------------------------------------------
BOOL PWM_Deinit(DWORD hDeviceContext)
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("++PWM_Deinit\r\n")));

    PwmDeinit();

    DEBUGMSG(ZONE_FUNCTION, (TEXT("--PWM_Deinit\r\n")));

    return TRUE;
}


//------------------------------------------------------------------------------
//
// Function: PWM_Open
//
// This function opens a device for reading, writing, or both.
//
// Parameters:
//      hDeviceContext
//          [in] Handle to the device context. The XXX_Init function creates
//          and returns this handle.
//
//      AccessCode
//          [in] Access code for the device. The access is a combination of
//          read and write access from CreateFile.
//
//      ShareMode
//          [in] File share mode of the device. The share mode is a
//          combination of read and write access sharing from CreateFile.
//
// Returns:
//      This function returns a handle that identifies the open context of
//      the device to the calling application.
//
//------------------------------------------------------------------------------
DWORD PWM_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
{
    DWORD dwStatus;
    DEBUGMSG(ZONE_FUNCTION, (TEXT("++PWM_Open\r\n")));
    dwStatus = DevicePowerNotify(_T("PWM1:"), D0, POWER_NAME);
    if(dwStatus != ERROR_SUCCESS)
    {
        DEBUGMSG(ZONE_WARN, (_T("PWM1: DevicePowerNotify(D0) failed %d.\r\n"), dwStatus));
    }
    DEBUGMSG(ZONE_FUNCTION, (TEXT("--PWM_Open\r\n")));
    return PWM_DEVICE_CONTEXT_HANDLE;
}


//------------------------------------------------------------------------------
//
// Function: PWM_Close
//
// This function closes the PWM for reading and writing.
//
// Parameters:
//      hOpenContext
//          [in] Handle returned by the XXX_Open function, used to identify
//          the open context of the device.
//
// Returns:
//      TRUE indicates success. FALSE indicates failure.
//
//------------------------------------------------------------------------------
BOOL PWM_Close(DWORD hOpenContext)
{
   DWORD dwStatus;
   DEBUGMSG(ZONE_FUNCTION, (TEXT("++PWM_Close\r\n")));
   dwStatus = DevicePowerNotify(_T("PWM1:"), D4, POWER_NAME);
   if(dwStatus != ERROR_SUCCESS)
   {
       DEBUGMSG(ZONE_WARN, (_T("PWM1: DevicePowerNotify(D4) failed %d.\r\n"), dwStatus));
   }
   DEBUGMSG(ZONE_FUNCTION, (TEXT("--PWM_Close\r\n")));
   return TRUE;
}


//------------------------------------------------------------------------------
//
// Function: PWM_PowerDown
//
// This function suspends power to the device. It is useful only with
// devices that can power down under software control.
//
// Parameters:
//      hDeviceContext
//          [in] Handle to the device context.
//
// Returns:
//      None.
//
//------------------------------------------------------------------------------
void PWM_PowerDown(DWORD hDeviceContext)
{
   DEBUGMSG(ZONE_FUNCTION, (TEXT("++PWM_PowerDown\r\n")));
   
   DEBUGMSG(ZONE_FUNCTION, (TEXT("--PWM_PowerDown\r\n")));
}


//------------------------------------------------------------------------------
//
// Function: PWM_PowerUp
//
// This function restores power to a device.
//
// Parameters:
//      hDeviceContext
//          [in] Handle to the device context.
//
// Returns:
//      None.
//
//------------------------------------------------------------------------------
void PWM_PowerUp(void)
{
   DEBUGMSG(ZONE_FUNCTION, (TEXT("++PWM_PowerUp\r\n")));
   PwmReset();
   DEBUGMSG(ZONE_FUNCTION, (TEXT("--PWM_PowerUp\r\n")));
}


//------------------------------------------------------------------------------
//
// Function: PWM_Read
//
// This function reads contents of 6 PWM registers.
//
// Parameters:
//      hOpenContext
//          [in] Handle to the open context of the device. The XXX_Open
//          function creates and returns this identifier.
//
//      pBuffer
//          [out] Pointer to the buffer that stores the data read from the
//          device. This buffer should be at least Count bytes long.
//
//      Count
//          [in] Number of bytes to read from the device into pBuffer.
//
// Returns:
//      Returns zero to indicate end-of-file. Returns -1 to indicate an
//      error. Returns the number of bytes read to indicate success.
//
//------------------------------------------------------------------------------
DWORD PWM_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
{
   DEBUGMSG(ZONE_FUNCTION, (TEXT("++PWM_Read\r\n")));

   if(!PwmReadRegister((UINT32 *)pBuffer, Count))
    return (-1);

   DEBUGMSG(ZONE_FUNCTION, (TEXT("--PWM_Read\r\n")));
   return Count;
}


//------------------------------------------------------------------------------
//
// Function: PWM_Write
//
// This function writes data to the device.
//
// Parameters:
//      hOpenContext
//          [in] Handle to the open context of the device. The XXX_Open
//          function creates and returns this identifier.
//
//      pBuffer
//          [out] Pointer to the buffer that contains the data to write.
//
//      dwNumBytes
//          [in] Number of bytes to write from the pBuffer buffer into the
//          device.
//
// Returns:
//      The number of bytes written indicates success. A value of -1 indicates
//      failure.
//
//------------------------------------------------------------------------------
DWORD PWM_Write(DWORD Handle, LPCVOID pBuffer, DWORD dwNumBytes)
{
   DWORD ret = dwNumBytes;
   DEBUGMSG(ZONE_FUNCTION, (TEXT("++PWM_Write\r\n")));

   // dwNumBytes is the number of pwm samples in the buffer
   if(!PwmPlaySample((LPCVOID)pBuffer, dwNumBytes))
   {
         dwNumBytes = 0;
         return (-1);
   }
   DEBUGMSG(ZONE_FUNCTION, (TEXT("--PWM_Write\r\n")));
   return ret;
}

//------------------------------------------------------------------------------
//
// Function: PWM_IOControl
//
// This function sends a command to a device.
//
// Parameters:
//      hOpenContext
//          [in] Handle to the open context of the device. The XXX_Open
//          function creates and returns this identifier.
//
//      dwCode
//          [in] I/O control operation to perform. These codes are
//          device-specific and are usually exposed to developers through
//          a header file.
//
//      pBufIn
//          [in] Pointer to the buffer containing data to transfer to the
//          device.
//
//      dwLenIn
//          [in] Number of bytes of data in the buffer specified for pBufIn.
//
//      pBufOut
//          [out] Pointer to the buffer used to transfer the output data
//          from the device.
//
//      dwLenOut
//          [in] Maximum number of bytes in the buffer specified by pBufOut.
//
//      pdwActualOut
//          [out] Pointer to the DWORD buffer that this function uses to
//          return the actual number of bytes received from the device.
//
// Returns:
//      The new data pointer for the device indicates success. A value of -1
//      indicates failure.
//
//------------------------------------------------------------------------------
BOOL PWM_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn,
                                      DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut,
                                      PDWORD pdwActualOut)
{
    BOOL bRet = FALSE;

    switch(dwCode)
    {
       case PWM_IOCTL_RESET:
                PwmReset();
                bRet = TRUE;
                DEBUGMSG(ZONE_IOCTL, (TEXT("PWM_IOControl: PWM_IOCTL_RESET occurred\r\n")));
                break;
       case IOCTL_POWER_CAPABILITIES:
                if (!pBufOut || dwLenOut < sizeof(POWER_CAPABILITIES) || !pdwActualOut)
               {
                       SetLastError(ERROR_INVALID_PARAMETER);
                       bRet = FALSE;
                }
                else 
                {
                       PPOWER_CAPABILITIES power = (PPOWER_CAPABILITIES)pBufOut;
                       memset(power, 0, sizeof(POWER_CAPABILITIES));
                       SetPwmPower(power);          // get configurations from PDD layer
                       *pdwActualOut = sizeof(POWER_CAPABILITIES);
                       bRet = TRUE;
                 }
                 break;

    case IOCTL_POWER_GET:
            if(pBufOut != NULL && dwLenOut == sizeof(CEDEVICE_POWER_STATE))
           {
                    *(PCEDEVICE_POWER_STATE)pBufOut = CurrentDx;
                    bRet = TRUE;
            }
            break;
    case IOCTL_POWER_SET:
             if(pBufOut != NULL && dwLenOut == sizeof(CEDEVICE_POWER_STATE))
            {
                     CEDEVICE_POWER_STATE NewDx = *(PCEDEVICE_POWER_STATE)pBufOut;
                     if(VALID_DX(NewDx))
                     {
                            if(NewDx == D1) 
                           {
                                NewDx = D0;
                            }
                            if(NewDx == D3) 
                           {
                                NewDx = D4;    // turn off
                            }
                            if(NewDx == D0) 
                           {
                               BSPPwmSetClockGatingMode(TRUE);
                           }
                            if(NewDx == D4) 
                           {
                               BSPPwmSetClockGatingMode(FALSE);
							                                                                    
							}
                            CurrentDx = NewDx;
                            bRet = TRUE;
                     }
                     else
                     {
                            DEBUGMSG(ZONE_WARN, (TEXT("DUM_IOControl: IOCTL_POWER_SET: invalid state request %u\r\n"), NewDx));
                     }
              }
              break; 
        default:
              DEBUGMSG(ZONE_WARN, (TEXT("PWM_IOControl: No matching IOCTL.\r\n")));
              break;
    }
    return bRet;
}

BOOL WINAPI PWM_DllEntry(HANDLE hInstDll, DWORD dwReason, LPVOID lpvReserved)
{
    switch (dwReason)
    {
         case DLL_PROCESS_ATTACH:
                 //Register Debug Zones
                 DEBUGREGISTER((HINSTANCE) hInstDll);
                 DEBUGMSG(ZONE_INFO, (TEXT("PWM_DllEntry: DLL_PROCESS_ATTACH lpvReserved(0x%x)\r\n"),lpvReserved));
                 DisableThreadLibraryCalls((HMODULE) hInstDll);
                 break;

         case DLL_PROCESS_DETACH:
                  DEBUGMSG(ZONE_INFO, (TEXT("PWM_DllEntry: DLL_PROCESS_DETACH lpvReserved(0x%x)\r\n"),lpvReserved));
                  break;
   }
   // Return TRUE for success
   return TRUE;
}



⌨️ 快捷键说明

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