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

📄 plato_nled.cpp

📁 Windows CE 6.0 BSP for VOIP sample phone. Intel PXA270 platform.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name:  

Abstract:  

Notes: 
--*/
#include <windows.h>
#include <nled.h>
#include <led_drvr.h>
#include <types.h>
#include  <ceddk.h>

#include "bulverde_base_regs.h"
#include "xllp_gpio.h"
#include "pcf50606.h"

#define KEYBOARD_BACKLIGHT_GPIO_PIN  87
#define VIBRATE_GPIO_PIN        53

HANDLE g_hPCF = INVALID_HANDLE_VALUE;
XLLP_GPIO_T *g_pGPIORegisters = NULL;

HANDLE g_hKbdBacklightThread = NULL;
BOOL   g_fExitKbdBacklightThread = FALSE;
HANDLE g_hKbdBacklightUpdateEvent = NULL;

const struct NLED_SUPPORTS_INFO g_LEDSupportsInfo[4] = {
    // 0 - Green LED
    { 
        0,    // LedNum
        50000,   // lCycleAdjust (50mSec)
        FALSE,   // fAdjustTotalCycleTime
        TRUE,    // fAdjustOnTime
        TRUE,    // fAdjustOffTime
        FALSE,   // fMetaCycleOn
        FALSE    // fMetaCycleOff
    },
    // 1 - Red LED
    { 
        1,    // LedNum
        50000,   // lCycleAdjust (50mSec)
        FALSE,   // fAdjustTotalCycleTime
        TRUE,    // fAdjustOnTime
        TRUE,    // fAdjustOffTime
        FALSE,   // fMetaCycleOn
        FALSE    // fMetaCycleOff
    },
    // 2 - Keyboard Backlight LED
    { 
        2,    // LedNum
        50000,   // lCycleAdjust (50mSec)
        FALSE,   // fAdjustTotalCycleTime
        TRUE,    // fAdjustOnTime
        TRUE,    // fAdjustOffTime
        FALSE,   // fMetaCycleOn
        FALSE    // fMetaCycleOff
    },
    // 3 - Vibrate
    { 
        3,    // LedNum
        -1,      // lCycleAdjust (-1 indicates it is a Vibrate device, All blink support options should be FALSE.)
        FALSE,   // fAdjustTotalCycleTime
        FALSE,   // fAdjustOnTime
        FALSE,   // fAdjustOffTime
        FALSE,   // fMetaCycleOn
        FALSE    // fMetaCycleOff
    }
};

#define NUM_LEDS (sizeof(g_LEDSupportsInfo)/sizeof(g_LEDSupportsInfo[0]))
#define MAX_INDEX_LEDS (NUM_LEDS - 1)

struct NLED_SETTINGS_INFO g_CurrentLEDSettings[NUM_LEDS];
const struct NLED_SETTINGS_INFO g_DefaultLEDSettings[NUM_LEDS] = 
{
    // 0 - Green LED
    {
        0,      //LedNum
        0,      //OffOnBlink
        1000000, //TotalCycleTime 
        100000, //OnTime    (1/10th Sec)   
        900000, //OffTime   (9/10th Sec)
        0,      //MetaCycleOn
        0       //MetaCycleOff
    },
    // 1 - Red LED
    {
        1,      //LedNum
        0,      //OffOnBlink
        1000000, //TotalCycleTime 
        100000, //OnTime    (1/10th Sec)   
        900000, //OffTime   (9/10th Sec)
        0,      //MetaCycleOn
        0       //MetaCycleOff
    },
    // 2 - Keyboard Backlight LED
    {
        2,      //LedNum
        0,      //OffOnBlink
        1000000, //TotalCycleTime 
        500000, //OnTime    (5/10th Sec)   
        500000, //OffTime   (5/10th Sec)
        0,      //MetaCycleOn
        0       //MetaCycleOff
    },
    // 3 - Vibrate
    {
        3,      //LedNum
        0,      //OffOnBlink
        0,      //TotalCycleTime 
        0,      //OnTime  
        0,      //OffTime
        0,      //MetaCycleOn
        0       //MetaCycleOff
    }
};

#define GREEN_LED_INDEX             0
#define RED_LED_INDEX               1
#define KEYBOARD_BACKLIGHT_INDEX    2
#define VIBRATOR_INDEX              3



INT WINAPI fnKbdBacklightThread(void)
{

    DWORD dwWaitTime = 1000;
    DWORD dwWaitReturn = 0;
    BOOL fOnCycle = TRUE;
    XLLP_UINT32_T pins[2] = {1, KEYBOARD_BACKLIGHT_GPIO_PIN};

    while(!g_fExitKbdBacklightThread)
    {
        dwWaitReturn = WaitForSingleObject(g_hKbdBacklightUpdateEvent, dwWaitTime);

        if(g_fExitKbdBacklightThread)
        {
            break;
        }

        switch (dwWaitReturn)
        {
        // apply settings 
        case WAIT_OBJECT_0:
            switch(g_CurrentLEDSettings[KEYBOARD_BACKLIGHT_INDEX].OffOnBlink)
            {
            // OFF
            case 0:
                XllpGpioSetOutput0(g_pGPIORegisters, pins);
                dwWaitTime = INFINITE;
                break;

            // ON
            case 1:
                XllpGpioSetOutputState1(g_pGPIORegisters, pins);
                dwWaitTime = INFINITE;
                break;

            //Blink
            case 2:
                XllpGpioSetOutputState1(g_pGPIORegisters, pins);
                dwWaitTime = g_CurrentLEDSettings[KEYBOARD_BACKLIGHT_INDEX].OnTime/1000;
                fOnCycle = TRUE;
                break;

            default:
                RETAILMSG(1,
                (_T("fnKbdBacklightThread: Invalid LED State %d\r\n"),
                g_CurrentLEDSettings[KEYBOARD_BACKLIGHT_INDEX].OffOnBlink));

            }
            break;

        //continue blink cycle
        case WAIT_TIMEOUT:
            if(fOnCycle)
            {
                XllpGpioSetOutput0(g_pGPIORegisters, pins);
                dwWaitTime = g_CurrentLEDSettings[KEYBOARD_BACKLIGHT_INDEX].OffTime/1000;
                fOnCycle = FALSE;
            }
            else
            {
                XllpGpioSetOutputState1(g_pGPIORegisters, pins);
                dwWaitTime = g_CurrentLEDSettings[KEYBOARD_BACKLIGHT_INDEX].OnTime/1000;
                fOnCycle = TRUE;
            }

            break; 

        //error
        case WAIT_FAILED:
            RETAILMSG(1,
                (_T("fnKbdBacklightThread: WaitForSingleObject failed %d\r\n"),
                GetLastError()));
            break;
        }
    }

    return 0;
}


BOOL ApplyCurrentLEDSettings(UCHAR cLedIndex)
{
    BOOL fRetVal = FALSE;
    XLLP_UINT32_T pins[2] = {1, VIBRATE_GPIO_PIN};

    switch(cLedIndex)
    {
    case GREEN_LED_INDEX:
        //Apply settings to LED1 (Green LED) 
        // The PCF driver applies a closest fit the the requested settings 
        // so the resulting true settings are returned
        if (!DeviceIoControl(g_hPCF, 
                             IOCTL_SET_PCF_LED1, 
                             (PBYTE)&g_CurrentLEDSettings[GREEN_LED_INDEX], 
                             sizeof(g_CurrentLEDSettings[GREEN_LED_INDEX]),
                             (PBYTE)&g_CurrentLEDSettings[GREEN_LED_INDEX], 
                             sizeof(g_CurrentLEDSettings[GREEN_LED_INDEX]),
                             NULL,
                             NULL)
           )
        {
            RETAILMSG(1,
                (_T("ApplyCurrentLEDSettings: IOCTL_SET_PCF_LED1 failed %d\r\n"),
                GetLastError()));
        }
        else
        {
            fRetVal = TRUE;
        }
        break;

    case RED_LED_INDEX:
        //Apply settings to LED2 (Red LED) 
        // The PCF driver applies a closest fit the the requested settings 
        // so the resulting true settings are returned
        if (!DeviceIoControl(g_hPCF, 
                             IOCTL_SET_PCF_LED2, 
                             (PBYTE)&g_CurrentLEDSettings[RED_LED_INDEX], 
                             sizeof(g_CurrentLEDSettings[RED_LED_INDEX]),
                             (PBYTE)&g_CurrentLEDSettings[RED_LED_INDEX], 
                             sizeof(g_CurrentLEDSettings[RED_LED_INDEX]),
                             NULL,
                             NULL)
           )
        {
            RETAILMSG(1,
                (_T("ApplyCurrentLEDSettings: IOCTL_SET_PCF_LED2 failed %d\r\n"),
                GetLastError()));
        }
        else
        {
            fRetVal = TRUE;
        }
        break;

    case KEYBOARD_BACKLIGHT_INDEX:
        //Apply settings to Keyboard Backlight 
        SetEvent(g_hKbdBacklightUpdateEvent);
        fRetVal = TRUE;
        break;

    case VIBRATOR_INDEX:
        switch(g_CurrentLEDSettings[VIBRATOR_INDEX].OffOnBlink)
        {
        // OFF
        case 0:
            XllpGpioSetOutput0(g_pGPIORegisters, pins);
            fRetVal = TRUE;
            break;

        // ON
        case 1:
            XllpGpioSetOutputState1(g_pGPIORegisters, pins);
            fRetVal = TRUE;
            break;

        default:
            RETAILMSG(1,
                (_T("ApplyCurrentLEDSettings: Vibrator: Invalid LED State %d\r\n"),
                g_CurrentLEDSettings[VIBRATOR_INDEX].OffOnBlink));

        }
        break;

    default:
        RETAILMSG(1,
                (_T("ApplyCurrentLEDSettings: Invalid LED index %d\r\n"),
                cLedIndex));
        break;
    }

    return fRetVal;
}



// The NLED MDD calls this routine to initialize the underlying NLED hardware.
// This routine should return TRUE if successful.  If there's a problem
// it should return FALSE and call SetLastError() to pass back the reason
// for the failure.
BOOL WINAPI
NLedDriverInitialize(
                    VOID
                    )
{
    BOOL fRetVal = FALSE;
    PHYSICAL_ADDRESS PA;
    XLLP_UINT32_T pins[3] = {2, KEYBOARD_BACKLIGHT_GPIO_PIN, VIBRATE_GPIO_PIN};

    DEBUGMSG(ZONE_PDD, (_T("NLedDriverInitialize: invoked\r\n")));
   
    PA.QuadPart  = BULVERDE_BASE_REG_PA_GPIO;
    g_pGPIORegisters  = (XLLP_GPIO_T *) MmMapIoSpace(PA, sizeof(XLLP_GPIO_T), FALSE);

    if(NULL == g_pGPIORegisters)
    {
        RETAILMSG(1,
            (_T("MmMapIoSpace(0x%x) failed %d\r\n"),PA.QuadPart,GetLastError()));

        goto ExitInit;
    }

    XllpGpioClearAlternateFn(g_pGPIORegisters, pins);
    XllpGpioSetOutput0(g_pGPIORegisters, pins);
    XllpGpioSetDirectionOut(g_pGPIORegisters, pins);


    //Create an event to interact with the KbdBacklight thread
    g_hKbdBacklightUpdateEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (!g_hKbdBacklightUpdateEvent)
    {
        RETAILMSG(1,
            (_T("NLedDriverInitialize: CreateEvent failed %d\r\n"),GetLastError()));

        goto ExitInit;
    }

    //make a thread to control the Keyboard Backlight pin
    g_fExitKbdBacklightThread = FALSE;
    g_hKbdBacklightThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) fnKbdBacklightThread, NULL, 0, NULL);
    if (!g_hKbdBacklightUpdateEvent)
    {
        RETAILMSG(1,
            (_T("NLedDriverInitialize: CreateThread failed %d\r\n"),GetLastError()));

        goto ExitInit;
    }

    // Open PMIC driver.
    if(INVALID_HANDLE_VALUE == g_hPCF)
    {
        g_hPCF = CreateFile(TEXT("PCF1:"), 0, 0, NULL, 
                          OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL,
                          NULL);
    }

    if (INVALID_HANDLE_VALUE == g_hPCF)
    {
        RETAILMSG(1,
            (_T("CreateFile(%s) failed %d\r\n"),TEXT("PCF1:"),GetLastError()));
        goto ExitInit;
    }

    //copy default settings
    memcpy(g_CurrentLEDSettings, g_DefaultLEDSettings, sizeof(g_DefaultLEDSettings));

    ApplyCurrentLEDSettings(GREEN_LED_INDEX);

⌨️ 快捷键说明

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