예제 #1
0
//------------------------------------------------------------------------------
//
//  Function:  InitializeDevice
//
//  Initializes the MADC registers for USB-VBat reference value and
//  channel averaging.
//
BOOL InitializeDevice(
    Device_t *pDevice
    )
{
    DEBUGMSG(ZONE_FUNCTION, (
        L"ADC: +InitializeDevice(0x%08x)\r\n", pDevice
        ));

    BOOL rc = FALSE;

    if (TWLWriteRegs(pDevice->hTWL, TWL_SW1AVERAGE_LSB, 
            &(pDevice->GPChAvgEnable), 2) == FALSE)
        {
        DEBUGMSG(ZONE_ERROR, (L"ADC: !ERROR - InitializeDevice "
            L"Failed to initialize GP average registers\r\n"
            ));
        goto cleanUp;
        }

    if (TWLWriteRegs(pDevice->hTWL, TWL_BCI_USBAVERAGE, 
            &(pDevice->UsbBciAvgEnable), 1) == FALSE)
        {
        DEBUGMSG(ZONE_ERROR, (L"ADC: !ERROR - InitializeDevice "
            L"Failed to initialize BCI-USB average registers\r\n"
            ));
        goto cleanUp;
        }

    // format to appropriate value
    pDevice->VBatRef <<= 6;
    if (TWLWriteRegs(pDevice->hTWL, TWL_USBREF_LSB, 
            &(pDevice->VBatRef), 2) == FALSE)
        {
        DEBUGMSG(ZONE_ERROR, (L"ADC: !ERROR - InitializeDevice "
            L"Failed to initialize VBAT reference registers\r\n"
            ));
        goto cleanUp;
        }

    rc = TRUE;

cleanUp:

    DEBUGMSG(ZONE_FUNCTION, (
        L"ADC: -InitializeDevice(0x%08x)\r\n", pDevice
        ));

    return rc;
}
예제 #2
0
//------------------------------------------------------------------------------
//
//  Function:  
//
BOOL TWLSetVoltage(UINT voltage,UINT32 mv)
{
	BYTE Buffer;
	ValidateHandle();
	
	switch(voltage)
	{
	case VLDO1:
		if( mv < VLDO1_MIN_VOLTAGE || mv > VLDO1_MAX_VOLTAGE)
			return FALSE;
		Buffer = FindVoltageIndex(LDO1_table,mv);
		TWLSetValue(g_hTwl, LDO_CTRL, Buffer, 0x7);
		break;

	case VLDO2:
		if( mv < VLDO2_MIN_VOLTAGE || mv > VLDO2_MAX_VOLTAGE)
			return FALSE;
		Buffer = FindVoltageIndex(LDO2_table,mv);		
		TWLSetValue(g_hTwl, LDO_CTRL, Buffer << 4, 0x7 << 4);
		break;

	case VDCDC1:
		if( mv < VDCDC1_MIN_VOLTAGE || mv > VDCDC1_MAX_VOLTAGE)
			return FALSE;
        
        if (mv > 1575)  //special case (0x1F that should be 1575mv is actually 1600mv)
            mv = 1575;

		Buffer = (BYTE) ((mv - VDCDC1_MIN_VOLTAGE)/VDCDC1_VOLTAGE_STEP);        
		TWLWriteRegs(g_hTwl, DEFCORE, &Buffer, 1);
        TWLSetValue(g_hTwl, CON_CTRL2, CON_CTRL2_GO, CON_CTRL2_GO);
        
		break;

	case VDCDC2:
		return FALSE;

	case VDCDC3:
		return FALSE;

	default :
		return FALSE;
	}
	return TRUE;
}
예제 #3
0
//------------------------------------------------------------------------------
//
//  Function:  KPD_Init
//
//  Called by device manager to initialize device.
//
DWORD KPD_Init(LPCTSTR szContext, LPCVOID pBusContext)
{
    DWORD rc = (DWORD)NULL;
    KeypadDevice_t *pDevice = NULL;
    UINT8 regval;

    UNREFERENCED_PARAMETER(pBusContext);

    DEBUGMSG(ZONE_FUNCTION, (
        L"+KPD_Init(%s, 0x%08x)\r\n", szContext, pBusContext
        ));

    // Create device structure
    pDevice = (KeypadDevice_t *)LocalAlloc(LPTR, sizeof(KeypadDevice_t));
    if (pDevice == NULL)
		{
        DEBUGMSG(ZONE_ERROR, (L"ERROR: KPD_Init: "
            L"Failed allocate KDP driver structure\r\n"
            ));
        goto cleanUp;
		}

    memset(pDevice, 0, sizeof(KeypadDevice_t));

    // Set cookie & initialize critical section
    pDevice->cookie = KPD_DEVICE_COOKIE;
    
    // Read device parameters
    if (GetDeviceRegistryParams(
            szContext, pDevice, dimof(s_deviceRegParams), s_deviceRegParams)
            != ERROR_SUCCESS)
        {
        DEBUGMSG(ZONE_ERROR, (L"ERROR: KPD_Init: "
            L"Failed read KPD driver registry parameters\r\n"
            ));
        goto cleanUp;
        }

    // Open parent bus
    pDevice->hTWL = TWLOpen();
    if (pDevice->hTWL == NULL)
        {
        DEBUGMSG(ZONE_ERROR, (L"ERROR: KPD_Init: "
            L"Failed open TWL bus driver\r\n"
            ));
        goto cleanUp;
        }

    // Set debounce delay and enable hardware mode
    regval = TWL_KBD_CTRL_KBD_ON | TWL_KBD_CTRL_NRESET | TWL_KBD_CTRL_NSOFT_MODE;
    TWLWriteRegs(pDevice->hTWL, TWL_KEYP_CTRL_REG, &regval, sizeof(regval));
    regval = 0x07 << 5;
    TWLWriteRegs(pDevice->hTWL, TWL_LK_PTV_REG, &regval, sizeof(regval));
    regval = (UINT8)pDevice->debounceCount & 0x3F;
    TWLWriteRegs(pDevice->hTWL, TWL_KEY_DEB_REG, &regval, sizeof(regval));
  
    // Create interrupt event
    pDevice->hIntrEventKeypad = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (pDevice->hIntrEventKeypad == NULL)
        {
        DEBUGMSG(ZONE_ERROR, (L"ERROR: KPD_Init: "
            L"Failed create keypad interrupt event\r\n"
            ));
        goto cleanUp;
        }

    // Associate event with TWL KP interrupt
    if (!TWLInterruptInitialize(pDevice->hTWL, TWL_INTR_ITKPI, pDevice->hIntrEventKeypad))
        {
        DEBUGMSG (ZONE_ERROR, (L"ERROR: KPD_Init: "
            L"Failed associate event with TWL KBD interrupt\r\n"
            ));
        goto cleanUp;
        }

    // Enable KP event
    if (!TWLInterruptMask(pDevice->hTWL, TWL_INTR_ITKPI, FALSE))
        {
        DEBUGMSG (ZONE_ERROR, (L"ERROR: KPD_Init: "
            L"Failed associate event with TWL KBD interrupt\r\n"
            ));
        }
        
    // register to be wake-up enabled
    if (pDevice->enableWake != 0)
        {
        TWLWakeEnable(pDevice->hTWL, TWL_INTR_ITKPI, TRUE);
        }

    // Start keypad interrupt service thread
    pDevice->hIntrThreadKeypad = CreateThread(
        NULL, 0, KPD_IntrThread, pDevice, 0,NULL
        );
    if (!pDevice->hIntrThreadKeypad)
        {
        DEBUGMSG (ZONE_ERROR, (L"ERROR: KPD_Init: "
            L"Failed create keypad interrupt thread\r\n"
            ));
        goto cleanUp;
        }

    if( pDevice->KpLedNum != -1)
        {
        // Create keypress notification event
        pDevice->hKeypressEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
        if ( pDevice->hKeypressEvent == NULL )
            {
            DEBUGMSG (ZONE_ERROR, (L"ERROR: KPD_Init: "
                L"Failed to create keypress event\r\n"
                ));
            goto cleanUp;
            }
    
        // Start interrupt service thread
        pDevice->hLightThread = CreateThread(
            NULL, 0, KPD_LightThread, pDevice, 0,NULL
            );
        if (!pDevice->hLightThread)
        {
            DEBUGMSG (ZONE_ERROR, (L"ERROR: KPD_Init: "
                L"Failed to create keypad light thread\r\n"
                ));
            goto cleanUp;
            }
        }

    // Return non-null value
    rc = (DWORD)pDevice;

cleanUp:
    if (rc == 0)
        {
        KPD_Deinit((DWORD)pDevice);
        }
    DEBUGMSG(ZONE_FUNCTION, (L"-KPD_Init(rc = %d\r\n", rc));
    return rc;
}
예제 #4
0
//------------------------------------------------------------------------------
//
//  Function:  ReadValue
//
//  Entry point to start process of reading the ADC value for any given 
// channel
//
DWORD ReadValue(
    DWORD context, 
    DWORD mask, 
    DWORD *prgResults, 
    DWORD count
    )
{
    DEBUGMSG(ZONE_FUNCTION, (
        L"ADC: +ReadValue(0x%08x, 0x%08x, 0x%08x, 0x%08x)\r\n", 
        context, mask, prgResults, count
        ));

    UINT8 pwr;
    UINT8 val;
    int readCount;
    UINT16 channels;
    DWORD rc = 0;
    BOOL bLocked = FALSE;
    Device_t *pDevice = (Device_t*)context;

    // do some sanity checks
    if ((pDevice == NULL) || (pDevice->cookie != MADC_DEVICE_COOKIE) || 
        prgResults == NULL || count == 0)
        {
        DEBUGMSG(ZONE_ERROR, (L"ADC: !ERROR - ReadValue: "
            L"Incorrect context parameter\r\n"
            ));
        goto cleanUp;
        }

    // serialize access
    bLocked = TRUE;
    EnterCriticalSection(&pDevice->cs);

    // power on MADC
    pwr = MADCON;
    if (TWLWriteRegs(pDevice->hTWL, TWL_CTRL1, &pwr, 1) == FALSE)
        {
        DEBUGMSG(ZONE_ERROR, (L"ADC: !ERROR - ReadValue: "
            L"Unable to enable MADCON\r\n"
            ));
        goto cleanUp;
        }

    // select the channels for conversion 
    channels = (UINT16)(mask & 0xFFFF);
    if (TWLWriteRegs(pDevice->hTWL, TWL_SW1SELECT_LSB, &channels, 2) == FALSE)
        {
        DEBUGMSG(ZONE_ERROR, (L"ADC: !ERROR - ReadGeneralPurposeLines: "
            L"Failed writing sw1 selection\r\n"
            ));
        goto cleanUp;
        }

    // start conversion
    val = TWL_MADC_CTRL_SW_TOGGLE;
    if (TWLWriteRegs(pDevice->hTWL, TWL_CTRL_SW1, &val, 1) == FALSE)
        {
        DEBUGMSG(ZONE_ERROR, (L"ADC: !ERROR - ReadGeneralPurposeLines: "
            L"Failed writing start conversion\r\n"
            ));
        goto cleanUp;
        }


    // first read general purpose lines
    channels = (UINT16)(mask & 0xFFFF);
    if (channels)
        {
        readCount = ReadGeneralPurposeLines(pDevice, channels, 
                            prgResults, count);
        if (readCount == 0)
            {
            DEBUGMSG(ZONE_ERROR, (L"ADC: !ERROR - ReadValue: "
                L"Unable to read general purpose lines\r\n"
                ));
            goto cleanUp;
            }

        // move pointer to next set of 
        rc += readCount;
        prgResults += readCount;
        count -= readCount;
        }

    // next read bci lines
    channels = (UINT16)(mask >> 16);
    if (channels != 0 && count > 0)
        {
        readCount = ReadBCILines(pDevice, channels, 
                            prgResults, count);
        if (readCount == 0)
            {
            DEBUGMSG(ZONE_ERROR, (L"ADC: !ERROR - ReadValue: "
                L"Unable to read BCI lines\r\n"
                ));
            goto cleanUp;
            }
        rc += readCount;
        }

cleanUp:    
    if (bLocked == TRUE) 
        {
        pwr = 0;
        TWLWriteRegs(pDevice->hTWL, TWL_CTRL1, &pwr, 1);
        LeaveCriticalSection(&pDevice->cs);
        }

    DEBUGMSG(ZONE_FUNCTION, (
        L"ADC: -ReadValue(0x%08x, 0x%08x, 0x%08x, 0x%08x)\r\n", 
        pDevice, mask, prgResults, count
        ));

    return rc;
}