Exemple #1
0
//-----------------------------------------------------------------------------
BOOL bq27410_init()
{
	BOOL rc = FALSE;
	USHORT volt = 0;
	
	if((hI2C = I2COpen(OMAP_DEVICE_I2C3)) == NULL) 
	{
        RETAILMSG(1,(L"ERROR: bq27410_init Failed open I2C device driver\r\n"));
        goto cleanUp;
	}
	
	if( I2CSetSlaveAddress(hI2C,  BQ27410_SLAVE_ADDRESS) == FALSE )
		RETAILMSG(1,(L"ERROR: bq27410_init - I2CSetSlaveAddress Failed\r\n"));
	
	I2CSetSubAddressMode(hI2C, I2C_SUBADDRESS_MODE_8);
	I2CSetBaudIndex(hI2C, FULLSPEED_MODE);
	
	if( BQ27410_WriteReg(bq27410CMD_CNTL_LSB,  0x0C) == FALSE ) // send battery insert
		RETAILMSG(1,(L"ERROR: BatteryPDDInitialize: Battery insert Failed\r\n"));
	
	if( BQ27410_ReadReg(bq27410CMD_VOLT_LSB, &volt) )
		RETAILMSG(1,(L"bq27410_init: volt = %d\r\n", volt));
	
	I2CClose(hI2C);
	rc = TRUE;
cleanUp:

	return rc;
}
Exemple #2
0
//------------------------------------------------------------------------------
//
//  Function:  I2C_IOControl
//
//  This function sends a command to a device.
//
BOOL
I2C_IOControl(
    DWORD context, 
    DWORD code, 
    UCHAR *pInBuffer, 
    DWORD inSize, 
    UCHAR *pOutBuffer, 
    DWORD outSize, 
    DWORD *pOutSize
    )
{
    RETAILMSG(ZONE_FUNCTION, (
        L"+I2C_IOControl(0x%08x, 0x%08x, 0x%08x, %d, 0x%08x, %d, 0x%08x)\r\n",
        context, code, pInBuffer, inSize, pOutBuffer, outSize, pOutSize
        ));

    BOOL rc = FALSE;
    Instance_t *pInstance = (Instance_t*)context;

	UNREFERENCED_PARAMETER(inSize);
	UNREFERENCED_PARAMETER(pOutBuffer);
	UNREFERENCED_PARAMETER(outSize);
	UNREFERENCED_PARAMETER(pOutSize);

    // Check if we get correct context
    if ((pInstance == NULL) || (pInstance->cookie != I2C_INSTANCE_COOKIE))
        {
        RETAILMSG(ZONE_ERROR, (L"ERROR: I2C_IOControl: "
            L"Incorrect context parameter\r\n"
            ));
        goto cleanUp;
        }

    switch (code)
        {
        case IOCTL_I2C_SET_SLAVE_ADDRESS:
            I2CSetSlaveAddress(pInstance->hI2C, *(UINT16*)pInBuffer);
            rc = TRUE;
            break;

        case IOCTL_I2C_SET_SUBADDRESS_MODE:
            I2CSetSubAddressMode(pInstance->hI2C, *(DWORD*)pInBuffer);
            rc = TRUE;
            break;

		case IOCTL_I2C_SET_BAUD_INDEX:
            I2CSetBaudIndex(pInstance->hI2C, *(DWORD*)pInBuffer);
            rc = TRUE;
            break;
        }

cleanUp:

    RETAILMSG(ZONE_FUNCTION, (L"-I2C_IOControl(rc = %d)\r\n", rc));
    return rc;
}
/**
 *  @brief  Initialize i2c module:
 *  I2C clock is BRG
 *  If mode parameter is SLAVE, uses address to set slave address for the module
 *  Enable module
 *  @param[in]  I2C_MODULE i2cnum (use I2C1 otherwise is necessary to modify the functions)
 *  @param[in]  i2cmode mode (MASTER or SLAVE)
 *  @param[in]  BYTE address for SLAVE mode
 *  @return     none
 */
void i2c_init(I2C_MODULE i2cnum, i2cmode mode, BYTE address) {
	//enabling i2c module doesnt need changing port
	//direction/value etc, and is not a pin muxed peripheral
	I2CConfigure ( i2cnum, I2C_ENABLE_SLAVE_CLOCK_STRETCHING);
	I2CSetFrequency ( i2cnum, GetPeripheralClock(), BRG);

	if(mode == SLAVE)
	{
		//address mask is set to 0
		I2CSetSlaveAddress ( i2cnum, address&0x7f, 0, I2C_USE_7BIT_ADDRESS );
	}

	I2CEnable(i2cnum, TRUE);
}
//------------------------------------------------------------------------------
//
//  Function:  ReadRegs
//
//  Internal routine to read triton registers.
//
BOOL
TWLReadRegs(
    HANDLE hContext,
    DWORD address,
    VOID *pBuffer,
    DWORD size
    )
{
    BOOL rc = FALSE;   
    
    Device_t * pDevice = (Device_t *) hContext;

    if (pDevice->csInitialized) EnterCriticalSection(&pDevice->cs);
    // set slave address if necessary
    if (pDevice->slaveAddress != HIWORD(address))
        {
        I2CSetSlaveAddress(pDevice->hI2C, BSPGetTritonSlaveAddress() | HIWORD(address));
        pDevice->slaveAddress = HIWORD(address);
        }

    if (I2CRead(pDevice->hI2C, (UCHAR)address, pBuffer, size) != size)
        {
        goto cleanUp;
        }
    
    // We succceded
    rc = TRUE;


cleanUp:    
    if (pDevice->csInitialized) LeaveCriticalSection(&pDevice->cs);

    //if (rc)
    //{
    //    RETAILMSG(1,(TEXT("Triton ReadRegs @0x%x = 0x%x\r\n"),address,*(UCHAR*)pBuffer));
    //}
    //else
    //{
    //    RETAILMSG(1,(TEXT("Triton ReadRegs @0x%x failed\r\n"),address));
    //}
    return rc;
}
BOOL  TWLSetValue(
               HANDLE hContext,
               DWORD address,
               UCHAR value,
               UCHAR mask
               )
{
    UCHAR regval;
    BOOL rc = FALSE;
    
    Device_t * pDevice = (Device_t *) hContext;

    if (pDevice->csInitialized) EnterCriticalSection(&pDevice->cs);

    // set slave address if necessary
    if (pDevice->slaveAddress != HIWORD(address))
        {
        I2CSetSlaveAddress(pDevice->hI2C, BSPGetTritonSlaveAddress() | HIWORD(address));
        pDevice->slaveAddress = HIWORD(address);
        }

    if (I2CRead(pDevice->hI2C, (UCHAR)address, &regval, sizeof(regval)) != sizeof(regval))
        {
        goto cleanUp;
        }   

        regval = (regval & ~mask) | value;

    if (I2CWrite(pDevice->hI2C, (UCHAR)address, &regval, sizeof(regval)) != sizeof(regval))
        {
        goto cleanUp;
        }   

    // We succceded
    rc = TRUE;

cleanUp:  
    if (pDevice->csInitialized) LeaveCriticalSection(&pDevice->cs);
  
    return rc;    
}
//------------------------------------------------------------------------------
//
//  Functions: TWLxxx
//
HANDLE 
TWLOpen(
    )
{
    static  Device_t Device = {NULL,0,FALSE,0};     

    // Open i2c bus
    if (Device.refCount == 0)
    {
        Device.hI2C = I2COpen(BSPGetTritonBusID());
        if (Device.hI2C == NULL)
        {
            DEBUGMSG(ZONE_ERROR, (L"ERROR: TWLOpen: "
                L"Failed open I2C bus driver\r\n"
            ));
            return NULL;
        }
        
        Device.slaveAddress = BSPGetTritonSlaveAddress();
        I2CSetSlaveAddress(Device.hI2C, Device.slaveAddress);  
    }
    Device.refCount++;
    return &Device;
}