TUInt32 TestVCNL4010Task(T_uezTask aMyTask, void *aParameters)
{
    TUInt8 cmd[2];
    TUInt8 data[4];
    TUInt32 ambient;
    TUInt32 proximity;
    int count = 0;

// Set Current to 20
    cmd[0] = REGISTER_PROX_CURRENT;
    cmd[1] = 20; // 20 x 10 = 200 mA, max
    UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 2, 1000);

    for(count = 0; count < 100; count++) {
        printf("Reading ... ");
        // Request to trigger both ALS and Proximity
        cmd[0] = REGISTER_COMMAND;
        cmd[1] = 0x18;
        UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 2, 1000);

        do {
            printf("?");
            // Now read back the status (after writing command index)
            cmd[0] = REGISTER_COMMAND;
            UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 1, 1000);
            UEZI2CRead(G_i2cBus, G_i2cAddr, I2C_SPEED, data, 1, 1000);

            // Is it ready?
            if (data[0] & (COMMAND_MASK_PROX_DATA_READY | COMMAND_MASK_AMBI_DATA_READY))
                break;

            // Not ready yet, pause and try again in a moment
            UEZTaskDelay(10);
        } while (1);

        // Now read the data
        cmd[0] = REGISTER_AMBI_VALUE;
        UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 1, 1000);
        UEZI2CRead(G_i2cBus, G_i2cAddr, I2C_SPEED, data, 4, 1000);

        ambient = (data[0] << 8) | data[1];
        proximity = (data[2] << 8) | data[3];
        printf("Ambient: %04X, Proximity: %04X\n", ambient, proximity);

        UEZTaskDelay(100);
    }
    return UEZ_ERROR_NONE;
}
/*-------------------------------------------------------------------------*
 * Prototypes:
 *-------------------------------------------------------------------------*/
TUInt32 TestVCNL4010FCT(const char *aI2CName, TUInt8 aI2CAddr,
                        TUInt32 * const ambient, TUInt32 * const proximity)
{
    TUInt8 cmd[2];
    TUInt8 data[4];
	UEZI2COpen(aI2CName, &G_i2cBus);
    G_i2cAddr = aI2CAddr;
 
// Set Current to 20
    cmd[0] = REGISTER_PROX_CURRENT;
    cmd[1] = 20; // 20 x 10 = 200 mA, max
    UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 2, 1000);

    printf("Reading ... ");
    // Request to trigger both ALS and Proximity
        cmd[0] = REGISTER_COMMAND;
        cmd[1] = 0x18;
        UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 2, 1000);

        do {
            printf("?");
            // Now read back the status (after writing command index)
            cmd[0] = REGISTER_COMMAND;
            UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 1, 1000);
            UEZI2CRead(G_i2cBus, G_i2cAddr, I2C_SPEED, data, 1, 1000);

            // Is it ready?
            if (data[0] & (COMMAND_MASK_PROX_DATA_READY | COMMAND_MASK_AMBI_DATA_READY))
                break;

            // Not ready yet, pause and try again in a moment
            UEZTaskDelay(10);
        } while (1);

        // Now read the data
        cmd[0] = REGISTER_AMBI_VALUE;
        UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 1, 1000);
        UEZI2CRead(G_i2cBus, G_i2cAddr, I2C_SPEED, data, 4, 1000);

       *ambient = (data[0] << 8) | data[1];
       *proximity = (data[2] << 8) | data[3];
        printf("Ambient: %04X, Proximity: %04X\n", *ambient, *proximity);

        UEZTaskDelay(100);
    return UEZ_ERROR_NONE;
}
Exemple #3
0
/*---------------------------------------------------------------------------*
 * Routine:  UEZGUI_EXP_DK_Detect
 *---------------------------------------------------------------------------*
 * Description:
 *      Determine if the EXP_DK is plugged in.  In this case, we'll use
 *      the I2C Mux and see if we can read it with a raw I2C read.
 * Outputs:
 *      TBool                     -- ETrue if found, else EFalse
 *---------------------------------------------------------------------------*/
TBool UEZGUI_EXP_DK_Detect(void)
{
    T_uezError error = UEZ_ERROR_NOT_FOUND;
    T_uezDevice i2c;
    TUInt8 data;

    // Determine if the EXP_DK is plugged in.
    UEZPlatform_ExpansionPrimary_I2C_A_Require();
    error = UEZI2COpen(PRIMARY_EXPANSION_I2C_A, &i2c);
    if (error)
        return EFalse;
    error = UEZI2CRead(i2c, UEZGUI_EXP_DK_I2CMUX_I2C_ADDRESS, 400, &data, 1, 200);
    UEZI2CClose(i2c);

    return (error == UEZ_ERROR_NONE)?ETrue:EFalse;
}
Exemple #4
0
/*---------------------------------------------------------------------------*/
int UEZCmdI2CProbe(void *aWorkspace, int argc, char *argv[])
{
    T_uezDevice i2c;
    T_uezError error;
    TUInt8 addr;
    TUInt8 data[10];
    char busName[] = "I2C0";

    if (argc == 2) {
        busName[3] = argv[1][0];
        error = UEZI2COpen(busName, &i2c);
        if (error) {
            FDICmdPrintf(aWorkspace, "I2C Bus %s not found!\n", busName);
            return UEZ_ERROR_NOT_FOUND;
        }

        // Probe all 127 addresses
        FDICmdPrintf(aWorkspace, "Probing all 127 I2C addresses on %s:\n",
            busName);
        for (addr = 1; addr <= 127; addr++) {
            // Give it 1 second each (way too much time)
            error = UEZI2CRead(i2c, addr, 100, data, 1, 100);
            if (error == UEZ_ERROR_NONE) {
                FDICmdPrintf(aWorkspace, "  Device @ 0x%02X\n", addr);
            } else if (error == UEZ_ERROR_TIMEOUT) {
                FDICmdPrintf(aWorkspace, "  Timeout at device 0x%02X!", addr);
                break;
            }
        }

        UEZI2CClose(i2c);
        if (addr >= 127) {
            FDICmdSendString(aWorkspace, "PASS: OK\n");
        }
    } else {
        FDICmdSendString(aWorkspace,
            "FAIL: Incorrect parameters.\nI2CPROBE <bus number>\n");
    }

    return UEZ_ERROR_NONE;
}
Exemple #5
0
/*---------------------------------------------------------------------------*/
int UEZCmdI2CRead(void *aWorkspace, int argc, char *argv[])
{
    T_uezDevice i2c;
    T_uezError error;
    TUInt8 addr;
    TUInt32 num;
    TUInt8 data[128];
    char busName[] = "I2C0";
    int i;

    if (argc == 4) {
        busName[3] = argv[1][0];

        // Open up the bus
        error = UEZI2COpen(busName, &i2c);
        if (error) {
            FDICmdPrintf(aWorkspace, "I2C Bus %s not found!\n", busName);
            return UEZ_ERROR_NOT_FOUND;
        }

        // Get the address
        addr = FDICmdUValue(argv[2]);
        if (addr >= 128) {
            FDICmdPrintf(aWorkspace, "Address must be 7-bit (0-127)\n");
            UEZI2CClose(i2c);
            return UEZ_ERROR_INVALID_PARAMETER;
        }

        // Get the number of bytes to read
        num = FDICmdUValue(argv[3]);
        if (num > sizeof(data)) {
            FDICmdPrintf(aWorkspace, "Out of space. Limited read buffer of %d bytes\n", sizeof(data));
            UEZI2CClose(i2c);
            return UEZ_ERROR_INVALID_PARAMETER;
        }

        // Write the data
        FDICmdPrintf(aWorkspace, "Reading bus %s, addr 0x%02X, %d bytes:\n",
            busName, addr, num);

        // For now, assume 100 kHz is valid
        error = UEZI2CRead(i2c, addr, 100, data, num, 1000);
        if (error == UEZ_ERROR_NONE) {
            for (i=0; i<num; i++) {
                FDICmdPrintf(aWorkspace, "%02X ", data[i]);
                if ((i%15)==7)
                    FDICmdPrintf(aWorkspace, "- ");
                if ((i%15)==15)
                    FDICmdPrintf(aWorkspace, "\n");
            }
            if ((num%15)!=15)
                FDICmdPrintf(aWorkspace, "\n");
            FDICmdSendString(aWorkspace, "PASS: OK\n");
        } else if (error == UEZ_ERROR_NAK) {
            FDICmdSendString(aWorkspace, "FAIL: NAK\n");
        } else if (error == UEZ_ERROR_TIMEOUT) {
            FDICmdSendString(aWorkspace, "FAIL: Timeout!\n");
        } else {
            FDICmdPrintf(aWorkspace, "FAIL: Error #%d\n", error);
        }

        UEZI2CClose(i2c);
    } else {
        FDICmdSendString(aWorkspace,
            "FAIL: Incorrect parameters.\nI2CREAD <bus number> <addr> <num>\n");
    }

    return UEZ_ERROR_NONE;
}
/*---------------------------------------------------------------------------*
 * Routine:  TS_MC_AR1020_Open
 *---------------------------------------------------------------------------*
 * Description:
 *      The TI TSC2046 is being opened.
 * Inputs:
 *      void *aW                    -- Particular SPI workspace
 * Outputs:
 *      T_uezError                   -- Error code
 *---------------------------------------------------------------------------*/
T_uezError TS_MC_AR1020_Open(void *aW)
{
    T_MC_AR1020Workspace *p = (T_MC_AR1020Workspace *)aW;
    T_uezError error = UEZ_ERROR_NONE;
    I2C_Request r;
    T_uezDevice i2c2;
    TUInt8 dataout[4] = {0x00, 0x55,0x01,EnableTouch};
    TUInt8 datain[4] = {0xff, 0xff, 0xff, 0xff};
    TUInt8 commandToSend[8] = {0x00, 0x55,0x05,RegisterWrite,0x00, 0x00, 0x01,0x60 };
    TUInt8 regstart[6];

    HAL_GPIOPort **p_gpio;
    const TUInt32 pin = 15;
    TUInt32 Read = 0;

    HALInterfaceFind("GPIO2", (T_halWorkspace **)&p_gpio);

    (*p_gpio)->SetInputMode(p_gpio, 1<<pin);
    (*p_gpio)->SetMux(p_gpio, pin, 0); // set to GPIO
    (*p_gpio)->Read(p_gpio, 1<<pin, &Read);

    if(p->aNumOpen == 0)
    {

        error = UEZI2COpen("I2C2", &i2c2);


        error = UEZI2CWrite(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, dataout, 4, UEZ_TIMEOUT_INFINITE);

        while(Read == 0)
        {
            (*p_gpio)->Read(p_gpio, 1<<pin, &Read);
        }
        error = UEZI2CRead(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, datain, 5, UEZ_TIMEOUT_INFINITE);

        //send command to change TouchThreshold to 0x60
//      to write a register first send RegisterStartAddressRequest     0x22
//      calculate the address by adding the offset of the register to the start address TouchThreshold 0x02
//      issue the register write command RegisterWrite 0x21

        dataout[0] = 0;
        dataout[3] = RegisterStartAddressRequest;
//get start address
        error = UEZI2CWrite(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, dataout, 4, UEZ_TIMEOUT_INFINITE);

        while(Read == 0)
        {
            (*p_gpio)->Read(p_gpio, 1<<pin, &Read);
        }
        error = UEZI2CRead(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, regstart, 6, UEZ_TIMEOUT_INFINITE);
//

        commandToSend[5] = regstart[4]+ TouchThreshold;
        error = UEZI2CWrite(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, commandToSend, 8, UEZ_TIMEOUT_INFINITE);

        while(Read == 0)
        {
            (*p_gpio)->Read(p_gpio, 1<<pin, &Read);
        }
        error = UEZI2CRead(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, datain, 4, UEZ_TIMEOUT_INFINITE);

        commandToSend[5] = SensitivityFilter;
        commandToSend[6] = 1;
        commandToSend[7] = 10;
        Read = 0;

        error = UEZI2CWrite(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, commandToSend, 8, UEZ_TIMEOUT_INFINITE);

//      while(Read == 0)
//      {
//        (*p_gpio)->Read(p_gpio, 1<<pin, &Read);
//      }
//      error = UEZI2CRead(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, datain, 4, UEZ_TIMEOUT_INFINITE);

        UEZI2CClose(i2c2);
    }
    p->aNumOpen++;

    return error;
}
/*---------------------------------------------------------------------------*
 * Routine:  TS_EXC7200_Poll
 *---------------------------------------------------------------------------*
 * Description:
 *      Take a reading from the TSC2406 and put in reading structure.
 * Inputs:
 *      void *aW                    -- Workspace
 *      T_uezTSReading *aReading     -- Pointer to final reading
 * Outputs:
 *      T_uezError                   -- Error code
 *---------------------------------------------------------------------------*/
T_uezError TS_EXC7200_Poll(void *aWorkspace, T_uezTSReading *aReading)
{
    T_EXC7200Workspace *p = (T_EXC7200Workspace *)aWorkspace;
    T_uezError error;
    I2C_Request r;
    T_uezDevice i2c0;
    TUInt8 dataIn[11];
    TUInt8 dataOut[5];
    TUInt32 Read = 0;
    TUInt32 x;
    TUInt32 y;
    static TBool loop = EFalse;
    TUInt32 start = UEZTickCounterGet();
    TUInt8 i;

    error = UEZI2COpen(p->iI2CBus, &i2c0);

    //while (UEZTickCounterGetDelta(start) < 2) {
    for(i = 0; i < 10; i++){
        // Try to grab the semaphore -- do we have new data?
        if (UEZSemaphoreGrab(p->iSemWaitForTouch, 0) == UEZ_ERROR_NONE) {
            // Got new data!
            Read = 1;
        } else {
            Read = 0;
        }

        if (Read == 0 && !loop) {
            aReading->iFlags = p->iLastTouch;
            aReading->iX = p->iLastX;
            aReading->iY = p->iLastY;

            TS_EXC7200_ApplyCalibration(p, (p->iLastX), (p->iLastY),
                    (TUInt32 *)&aReading->iX, (TUInt32 *)&aReading->iY);
            return UEZ_ERROR_NONE;
        } else {
//            dataOut[0] = 0x00;
//            error = UEZI2CWrite(i2c0,
//                    EXC7200_I2C_ADDRESS,
//                    EXC7200_I2C_SPEED,
//                    dataOut,
//                    1,
//                    50);

            memset((void*)dataIn, 0, sizeof(dataIn));
            error = UEZI2CRead(i2c0, EXC7200_I2C_ADDRESS, EXC7200_I2C_SPEED,
                    dataIn, 0x0A, 50);

            x = (((dataIn[3] & 0xFF) << 8) | dataIn[2]) / 51;

            y = (((dataIn[5] & 0xFF) << 8) | dataIn[4]) / 70;

            if ((dataIn[1] & 0x81) == 0x81 && (dataIn[0] == 0x04) && ((dataIn[1] & 0x7C) == 0)) {
                loop = ETrue;
                (aReading->iFlags) = (p->iLastTouch) = TSFLAG_PEN_DOWN;
                (aReading->iX) = (p->iLastX) = x;
                (aReading->iY) = (p->iLastY) = y;
                if ((!p->iIsCalibrating) && (p->iHaveCalibration)) {
                    // Convert X & Y coordinates
                    TS_EXC7200_ApplyCalibration(p, x, y,
                            (TUInt32 *)&aReading->iX, (TUInt32 *)&aReading->iY);
                }
            } else if (((dataIn[1] & 0x81) == 0x80) && (dataIn[0] == 0x04) && ((dataIn[1] & 0x7C) == 0)) {
                (aReading->iFlags) = (p->iLastTouch) = 0;
                (aReading->iX) = (p->iLastX);
                (aReading->iY) = (p->iLastY);
                UEZGPIOClearIRQ(p->iInteruptPin);
            } else { //if ((dataIn[3] & 0xC0) == 0x40){
                (aReading->iFlags) = (p->iLastTouch);
                (aReading->iX) = (p->iLastX);
                (aReading->iY) = (p->iLastY);

                TS_EXC7200_ApplyCalibration(p, (p->iLastX), (p->iLastY),
                        (TUInt32 *)&aReading->iX, (TUInt32 *)&aReading->iY);
            }

            if (!UEZGPIORead(p->iInteruptPin)) {
                loop = ETrue;
            } else {
                loop = EFalse;
                break;
            }

            if (((dataIn[1] & 0x81) == 0x80) && (dataIn[0] == 0x04)) {
              i = i + 1;
              i = i -1;
                break;
            }
        }
#if 0
        TUInt32 count = 0;
        while (!UEZGPIORead(p->iInteruptPin)) {
            error = UEZI2CRead(i2c0,
                    EXC7200_I2C_ADDRESS,
                    EXC7200_I2C_SPEED,
                    dataIn,
                    0x0A,
                    50);
            count++;
        }
#endif

    }
    return error;
}