Example #1
0
/******************************************************************************
* ID : 60.0
* Outline : Glyph_uEZ_Open
* Include : Glyph_uEZ_0.h
* Function Name: Glyph_uEZ_Open
* Description : Open and setup the communications channel 0.
* Argument : aHandle - the Glyph handle to setup for the LCD and Communications.
* Return Value : 0=success, not 0= error
* Calling Functions : GlyphCommOpen
******************************************************************************/
T_glyphError Glyph_uEZ_Open(T_glyphHandle aHandle)
{
    int i = 0 ;
    T_uezDevice dev;
    T_uezDeviceWorkspace *G_gpioC;
    
    if (G_spi == 0)  {
        UEZDeviceTableFind("SPI0", &dev);
        UEZDeviceTableGetWorkspace(dev, (T_uezDeviceWorkspace **)&G_spi);
        HALInterfaceFind("GPIOC", (T_halWorkspace **)&G_gpioC);

        G_request.iBitsPerTransfer = 8;
        // Chip select on Port C2 (high true)
        G_request.iCSGPIOPort = (HAL_GPIOPort **)G_gpioC;
        G_request.iCSGPIOBit = (1<<2);
        G_request.iCSPolarity = EFalse;
        G_request.iClockOutPhase = EFalse;
        G_request.iClockOutPolarity = EFalse;
        G_request.iDataMISO = 0;
        G_request.iDataMOSI = 0;
        G_request.iNumTransfers = 0;
        G_request.iRate = 12000; // 12 MHz for now

        /* Set data direction for this bit to output */
        PORTC.DDR.BIT.B3 = 1 ;   // LCD GPIO for Reset LCD
        PORT5.DDR.BIT.B1 = 1 ;   // LCD RS A0

        StartResetLCD() ;
        for (i=0;i<1000000;i++){}
        EndResetLCD() ;
        for (i=0;i<1000000;i++){}
    }

    return GLYPH_ERROR_NONE ;
}
Example #2
0
/*---------------------------------------------------------------------------*
 * Routine:  PWM_Generic_Configure
 *---------------------------------------------------------------------------*
 * Description:
 *      Setup a PWM device to ensure single accesses at a time.
 * Inputs:
 *      void *aW                    -- Workspace
 * Outputs:
 *      T_uezError                   -- Error code
 *---------------------------------------------------------------------------*/
T_uezError PWM_Generic_Configure(void *aW, const char *aPWMHALName)
{
    T_PWM_Generic_Workspace *p = (T_PWM_Generic_Workspace *)aW;

    // Link to the PWM device or report an error when tried
    return HALInterfaceFind(aPWMHALName, (T_halWorkspace **)&p->iPWM);
}
Example #3
0
/*---------------------------------------------------------------------------*
 * Routine:  LED_Generic_GPIO_Configure_AddLED
 *---------------------------------------------------------------------------*
 * Description:
 *      Add an LED to the list of LEDs in this LED Generic GPIO driver.
 *      The bit number assigned to each entry starts at 0 and goes to
 *      the limit of the driver (usually 32 total).
 *      NOTE: This routine is NOT semaphore and expected to be called
 *          before the RTOS is up and running.
 * Inputs:
 *      void *aW                    -- Workspace
 *      T_GneericLEDGPIODefinition *iDefinition -- LED definition structure
 * Outputs:
 *      T_uezError                   -- Error code
 *---------------------------------------------------------------------------*/
T_uezError LED_Generic_GPIO_Configure_AddLED(
    void *aWorkspace,
    const T_GenericLEDGPIODefinition *aDefinition)
{
    T_LED_Generic_GPIO_Workspace *p =
        (T_LED_Generic_GPIO_Workspace *)aWorkspace;
    T_LEDGPIOEntry *p_entry;
    HAL_GPIOPort **p_gpio;
    T_uezError error;

    error = HALInterfaceFind(aDefinition->iGPIOPortName,
        (T_halWorkspace **)&p_gpio);
    if (error)
        return error;

    if (p->iNumEntries < LED_GENERIC_GPIO_MAX_NUM_GPIOS) {
        p_entry = p->iEntries + p->iNumEntries++;
        p_entry->iGPIOPort = (HAL_GPIOPort **)p_gpio;
        p_entry->iIsHighTrue = aDefinition->iIsHighTrue;
        p_entry->iGPIOPinIndex = aDefinition->iGPIOPinIndex;
        p_entry->iLEDState = STATE_UNKNOWN;
        // Start with LED turned off
        if (p_entry->iIsHighTrue) {
            (*p_gpio)->Set(*p_gpio, (1 << p_entry->iGPIOPinIndex));
        } else {
            (*p_gpio)->Clear(*p_gpio, (1 << p_entry->iGPIOPinIndex));
        }
        (*p_gpio)->SetOutputMode(*p_gpio, (1 << p_entry->iGPIOPinIndex));
        return UEZ_ERROR_NONE;
    } else {
        return UEZ_ERROR_OVERFLOW;
    }
}
Example #4
0
/*---------------------------------------------------------------------------*
 * Routine:  CRC_Generic_Create
 *---------------------------------------------------------------------------*
 * Description:
 *      Create the CRC Device driver and link it to an existing CRC HAL Driver.
 * Inputs:
 *      const char *aName           -- Name of new CRC generic device driver
 *      const char *aHALDriverName  -- HAL Device driver name to link to
 * Outputs:
 *      T_uezError                   -- Error code
 *---------------------------------------------------------------------------*/
T_uezError CRC_Generic_Create(const char *aName, const char *aHALDriverName)
{
    T_CRC_Generic_Workspace *p;

    UEZDeviceTableRegister(aName,
            (T_uezDeviceInterface *)&CRC_Generic_Interface, 0,
            (T_uezDeviceWorkspace **)&p);
    return HALInterfaceFind(aHALDriverName, (T_halWorkspace **)&p->iCRC);
}
Example #5
0
/*---------------------------------------------------------------------------*
 * Routine:  DAC_Generic_Configure
 *---------------------------------------------------------------------------*
 * Description:
 *      Link the DAC Device Driver to a DAC HAL driver.
 * Inputs:
 *      void *aW                    -- Workspace
 *      const char *aDACHALName     -- HAL Device driver name
 * Outputs:
 *      T_uezError                   -- Error code
 *---------------------------------------------------------------------------*/
T_uezError DAC_Generic_Configure(void *aW, const char *aDACHALName)
{
    T_DAC_Generic_Workspace *p = (T_DAC_Generic_Workspace *)aW;
    T_uezError error;

    // Access the DAC device
    error = HALInterfaceFind(aDACHALName, (T_halWorkspace **)&p->iDAC);

    return error;
}
Example #6
0
T_uezError RTC_Generic_Create(const char *aName, const char *aHALName)
{
    HAL_RTC **p_rtchal;
    T_uezDeviceWorkspace *p_rtcdev;

    UEZDeviceTableRegister(aName,
            (T_uezDeviceInterface *)&RTC_Generic_Interface, 0, &p_rtcdev);
    HALInterfaceFind(aHALName, (T_halWorkspace **)&p_rtchal);
    RTC_Generic_Configure(p_rtcdev, (HAL_RTC **)p_rtchal);
    
    return UEZ_ERROR_NONE;
}
T_uezError RS485_Generic_Timer_Create(
    const char *aName,
    const T_RS485_Generic_Timer_Settings *aSettings)
{
    T_uezDeviceWorkspace *p;
    HAL_Serial **p_serial;
    HAL_Timer **p_timer;

    UEZDeviceTableRegister(aName,
        (T_uezDeviceInterface *)&RS485_Generic_Timer_Stream_Interface, 0, &p);
    HALInterfaceFind(aSettings->iSerialName, (T_halWorkspace **)&p_serial);
    HALInterfaceFind(aSettings->iTimerName, (T_halWorkspace **)&p_timer);
    return RS485_Generic_Timer_Configure(p, p_serial,
        aSettings->iQueueSendSize, aSettings->iQueueReceiveSize,
        GPIO_TO_HAL_PORT(aSettings->iDriveEnable),
        GPIO_TO_PIN_BIT(aSettings->iDriveEnable),
        aSettings->iDriveEnablePolarity,
        GPIO_TO_HAL_PORT(aSettings->iReceiveEnable),
        GPIO_TO_PIN_BIT(aSettings->iReceiveEnable),
        aSettings->iReceiveEnablePolarity, p_timer, aSettings->iReleaseTime,
        aSettings->iDriveTime);
}
/*---------------------------------------------------------------------------*
 * Routine:  LCD_UMSH_8596MD_20T_InitializeWorkspace_8Bit
 *---------------------------------------------------------------------------*
 * Description:
 *      Setup workspace for UMSH_8596MD_20T LCD.
 * Inputs:
 *      void *aW                    -- Particular workspace
 * Outputs:
 *      T_uezError                   -- Error code
 *---------------------------------------------------------------------------*/
T_uezError LCD_UMSH_8596MD_20T_InitializeWorkspace_8Bit(void *aW)
{
    T_UMSH_8596MD_20TWorkspace *p = (T_UMSH_8596MD_20TWorkspace *)aW;
    p->iBaseAddress = 0xA0000000;
    p->aNumOpen = 0;
    p->iBacklightLevel = 256; // 100%
    p->iConfiguration = &LCD_UMSH_8596MD_20T_configuration_8Bit;
#if UEZ_LCD_POWER_GPIO_PIN
    HALInterfaceFind(UEZ_LCD_POWER_GPIO_PORT, (T_halWorkspace **)&p->iPowerGPIOPort);
#else
    p->iPowerGPIOPort = 0;
#endif

    return UEZSemaphoreCreateBinary(&p->iVSyncSem);
}
void USBHost_Generic_Create(const char *aName, const char *aHALUSBHostName)
{
    T_halWorkspace *p_halUSBHost;
    T_uezDeviceWorkspace *p_usbhost;

    // Register the USB Host device driver
    UEZDeviceTableRegister(
            aName,
            (T_uezDeviceInterface *)&G_Generic_USBHost_Interface,
            0,
            (T_uezDeviceWorkspace **)&p_usbhost);
    // Link the Generic USB host to the HAL USB host
    HALInterfaceFind(aHALUSBHostName, (T_halWorkspace **)&p_halUSBHost);
    Generic_USBHost_Configure(p_usbhost, (HAL_USBHost **)p_halUSBHost);
}
Example #10
0
/*---------------------------------------------------------------------------*
 * Routine:  PlayAudio
 *---------------------------------------------------------------------------*
 * Description:
 *      Play a tone for the given length, staying here until done.
 * Inptus:
 *      TUInt32 aHz                 -- Tone in Hz
 *      TUInt32 aMS                 -- Duration of tone
 *---------------------------------------------------------------------------*/
void PlayAudio(TUInt32 aHz, TUInt32 aMS)
{

#if OPTION_USE_GPIO_LINES_FOR_AUDIO
    static HAL_GPIOPort **p_gpio0;
    TUInt32 n;
    TUInt32 start = UEZTickCounterGet();
    TUInt32 count;

    HALInterfaceFind("GPIO0", (T_halWorkspace **)&p_gpio0);
    (*p_gpio0)->SetOutputMode(p_gpio0, (1<<26));

    // Wait for the parameters to be ready
    n = (G_subTickCount*1000/2)/aHz;
    taskDISABLE_INTERRUPTS();
    G_dummyCounter = 0;
    // Wait for first tick count
    while ((T1IR&2)==0)
        {}
    while (aMS--) {
        T1IR = 2;
        while (!(T1IR & 2)) {
            count = n;
            while (count--)
                { G_dummyCounter++; }
            (*p_gpio0)->Clear(p_gpio0, (1<<26));
            count = n;
            while (count--)
                { G_dummyCounter++; }
            (*p_gpio0)->Set(p_gpio0, (1<<26));
        }
    }

    taskENABLE_INTERRUPTS();
#else
    if (!G_tg)
        CalibrateAudioTiming();
    //PWMAudio(aHz, (aMS+10)/10, 0);
    if (G_tg)
        UEZToneGeneratorPlayTone(
                G_tg,
                TONE_GENERATOR_HZ(aHz),
                aMS);
    else
        UEZTaskDelay(aMS);
#endif
}
Example #11
0
/*---------------------------------------------------------------------------*
 * Routine:  Timer_Generic_Create
 *---------------------------------------------------------------------------*
 * Description:
 *      Setup a Timer and link to the HAL equivalent
 * Inputs:
 *      const char *aHALDriver -- Name of HAL driver to link to
 * Outputs:
 *      T_uezError                   -- Error code
 *---------------------------------------------------------------------------*/
T_uezError Timer_Generic_Create(const char *aDeviceName, const char *aHALDriver)
{
    T_uezError error;
    T_Timer_Generic_Workspace *p;

    // Create the timer device
    error = UEZDeviceTableRegister(aDeviceName,
                                   (T_uezDeviceInterface *)&Timer_Generic_Interface, 0,
                                   (T_uezDeviceWorkspace **)&p);
    if (error)
        return error;

    // Find the HAL and link to this device
    error = HALInterfaceFind(aHALDriver, (T_halWorkspace **)&p->iTimer);
    if (error)
        return error;

    return error;
}
Example #12
0
 /*---------------------------------------------------------------------------*/
static TUInt32 Heartbeat(T_uezTask aMyTask, void *aParams)
{
    HAL_GPIOPort **p_gpio;
    const TUInt32 pin = 13;
    TBool run = ETrue;

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

    (*p_gpio)->SetOutputMode(p_gpio, 1<<pin);
    (*p_gpio)->SetMux(p_gpio, pin, 0); // set to GPIO
    // Blink
    while (run) {
        (*p_gpio)->Set(p_gpio, 1<<pin);
        UEZTaskDelay(250);
        (*p_gpio)->Clear(p_gpio, 1<<pin);
        UEZTaskDelay(250);
    }
    return 0;
}
Example #13
0
portBASE_TYPE Init_EMAC(unsigned short PHYType)
{
    T_uezError error;
    extern T_uezError SetupEMAC(void);

    // Find the EMAC HAL and keep it around
    error = HALInterfaceFind("EMAC", (T_halWorkspace **)&G_emacWS);
    if (error != UEZ_ERROR_NONE) {
        G_emac = 0;
        G_emacWS = 0;
        return pdFAIL;
    }
    G_emac = (HAL_EMAC *)(G_emacWS->iInterface);

    // USe the NV settings to configure the EMAC
    if (SetupEMAC() != UEZ_ERROR_NONE)
        return pdFAIL;

    return pdPASS;
}
Example #14
0
int UEZGUICmd3VERR(void *aWorkspace, int argc, char *argv[])
{
    // Read the GPIO for the 3VERR on P2_25
    HAL_GPIOPort **p_gpio;
    const TUInt32 pin = 2;
    TUInt32 reading;

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

    // All we do is determine if this pin is high or low.  High level is good,
    // low means we are tripping an error.
    (*p_gpio)->Read(p_gpio, 1 << pin, &reading);
    if (reading) {
        // High: good
        FDICmdSendString(aWorkspace, "PASS: OK\n");
    } else {
        // Low: bad
        FDICmdSendString(aWorkspace, "FAIL: Triggered\n");
    }
    return 0;
}
Example #15
0
/*---------------------------------------------------------------------------*
 * Routine:  SetupEMAC
 *---------------------------------------------------------------------------*
 * Description:
 *      When the network goes active, SetupEMAC is called.  This routine
 *      initializes the EMAC.
 *---------------------------------------------------------------------------*/
T_uezError SetupEMAC(void)
{
    T_uezError error;
    HAL_EMAC **emac;
    extern void *UEZEMACGetMACAddr(TUInt32 aUnitNumber);

//    printf("Setting up EMAC ... ");

    // Copy over the emac settings
    memcpy(UEZ_EMAC_Settings.iMACAddress, UEZEMACGetMACAddr(0), 6);

    // Setup and configure the EMAC
    error = HALInterfaceFind("EMAC", (T_halWorkspace **)&emac);
    if (!error)
        error = (*emac)->Configure(emac, &UEZ_EMAC_Settings);
//    if (error)
//        printf("Error! (code %d)\n", error);
//    else
//        printf("OK\n");

    return error;
}
/*---------------------------------------------------------------------------*
 * Routine:  Serial_Generic_HalfDuplex_Stream_Create
 *---------------------------------------------------------------------------*
 * Description:
 *      Create a full duplex serial stream to a HAL serial device and create
 *      the read and write buffers.
 * Inputs:
 *      const char *aName -- Name of this created driver
 *      const char *aSerialHALName -- Name of low level Serial HAL driver
 *      TUInt32 aWriteBufferSize -- Size of the write buffer size in bytes
 *      TUInt32 aReadBufferSize -- Size of the read buffer size in bytes
 * Outputs:
 *      T_uezError -- Error code.
 *---------------------------------------------------------------------------*/
T_uezError Serial_Generic_HalfDuplex_Stream_Create(
        const char *aName,
        const char *aSerialHALName,
        TUInt32 aWriteBufferSize,
        TUInt32 aReadBufferSize,
        T_uezGPIOPortPin aDriveEnablePortPin,
        TBool aDriveEnablePolarity,
        TUInt32 aDriveEnableReleaseTime)
{
    T_halWorkspace *p_serialUART;
    T_uezDeviceWorkspace *p_serial;

    // Create serial stream device and link to HAL_Serial driver
    UEZDeviceTableRegister(aName,
            (T_uezDeviceInterface *)&Serial_GenericHalfDuplex_Stream_Interface,
            0, &p_serial);
    HALInterfaceFind(aSerialHALName, (T_halWorkspace **)&p_serialUART);
    return Serial_GenericHalfDuplex_Configure(p_serial,
            (HAL_Serial **)p_serialUART, aWriteBufferSize, aReadBufferSize,
            UEZGPIOGetPort(aDriveEnablePortPin), 1
                    <<UEZ_GPIO_PIN_FROM_PORT_PIN(aDriveEnablePortPin),
            aDriveEnablePolarity, aDriveEnableReleaseTime);
}
Example #17
0
int UEZGUICmdSpeaker(void *aWorkspace, int argc, char *argv[])
{
    TUInt32 freq;
    T_uezDevice speaker;
    HAL_GPIOPort **p_gpio2 = 0;

    if (argc == 2) {
        HALInterfaceFind("GPIO2", (T_halWorkspace **)&p_gpio2);
        UEZToneGeneratorOpen("Speaker", &speaker);
        freq = FDICmdUValue(argv[1]);
        if (freq) {
            (*p_gpio2)->SetMux(p_gpio2, 1, 1);
            UEZToneGeneratorPlayToneContinuous(speaker, TONE_GENERATOR_HZ(freq));
        } else {
            UEZToneGeneratorPlayToneContinuous(speaker, TONE_GENERATOR_OFF);
            (*p_gpio2)->SetMux(p_gpio2, 1, 0);
        }
        FDICmdPrintf(aWorkspace, "PASS: %d Hz\n", freq);
    } else {
        FDICmdSendString(aWorkspace, "FAIL: Incorrect parameters\n");
    }
    return 0;
}
/*---------------------------------------------------------------------------*
 * Routine:  ExternalInterrupt_Renesas_RX62N_Set
 *---------------------------------------------------------------------------*
 * Description:
 *      Set the rate of a given blink register (as close as possible).
 * Inputs:
 *      void *aWorkspace            -- Workspace
 *      TUInt32 aChannel            -- EINT channel (0 to 15)
 *      TUInt8 aTrigger             -- Trigger type (if used)
 *      EINTCallback aCallbackFunc  -- Function to call when trigger.
 *      void *aCallbackWorkspace    -- Parameter to pass to callback function.
 * Outputs:
 *      T_uezError                   -- Error code
 *---------------------------------------------------------------------------*/
T_uezError ExternalInterrupt_Renesas_RX62N_Set(
    void *aWorkspace,
    TUInt32 aChannel,
    T_EINTTrigger aTrigger,
    EINT_Callback aCallbackFunc,
    void *aCallbackWorkspace,
    T_irqPriority aPriority,
    const char *aName)
{
    TUInt32 irqNum;
    //    T_ExternalInterrupt_Renesas_RX62N_Workspace *p =
    //       (T_ExternalInterrupt_Renesas_RX62N_Workspace *)aWorkspace;
    T_eintEntry *p_entry;
    T_uezError error = UEZ_ERROR_NONE;
    TUInt8 edge;
    const T_ExternalInterrupt_Renesas_RX62N_Info *p_info;
    HAL_GPIOPort **p_gpio;
    TUInt32 gpioPinIndex;

    // Fake loop
    while (1) {
        // Is this a valid channel?
        if (aChannel >= NUM_EXTERNAL_INTERRUPTS) {
            error = UEZ_ERROR_OUT_OF_RANGE;
            break;
        }

        // Is this external interrupt already in use?
        p_entry = G_eintEntries + aChannel;
        if (p_entry->iCallbackFunc) {
            error = UEZ_ERROR_ALREADY_EXISTS;
            break;
        }

        // Is this interrupt already registered?
        irqNum = VECT_ICU_IRQ0 + aChannel;
        if (InterruptIsRegistered(irqNum)) {
            error = UEZ_ERROR_NOT_AVAILABLE;
            break;
        }

        // Interrupt is available.  Register it
        p_entry->iPriority = aPriority;
        p_entry->iTrigger = aTrigger;
        p_entry->iCallbackWorkspace = aCallbackWorkspace;
        p_entry->iCallbackFunc = aCallbackFunc;

        // Start disabled
        InterruptDisable(irqNum);

        // Find the info about this pin
        p_info = G_eintInfo + aChannel;
        error = HALInterfaceFind((p_entry->iIsPinSetB) ? p_info->iGPIONameB
            : p_info->iGPIONameA, (T_halWorkspace **)&p_gpio);
        if (error)
            break;
        gpioPinIndex = (p_entry->iIsPinSetB) ? p_info->iGPIOPinIndexB
            : p_info->iGPIOPinIndexA;
        (*p_gpio)->SetInputMode(p_gpio, (1<<gpioPinIndex));
        (*p_gpio)->Control(p_gpio, gpioPinIndex, GPIO_CONTROL_ENABLE_INPUT_BUFFER, 0);

        // Map the trigger type to a constant
        switch (aTrigger) {
            case EINT_TRIGGER_EDGE_FALLING:
                edge = (1 << 3);
                break;
            case EINT_TRIGGER_EDGE_RISING:
                edge = (2 << 3);
                break;
            case EINT_TRIGGER_LEVEL_LOW:
                edge = (0 << 3);
                break;
            case EINT_TRIGGER_EDGE_BOTH:
                edge = (3 << 3);
                break;
            case EINT_TRIGGER_LEVEL_HIGH:
            default:
                edge = 0xFF;
                break;
        }
        if (edge == 0xFF) {
            error = UEZ_ERROR_INVALID_PARAMETER;
            break;
        }
        ICU.IRQCR[aChannel].BYTE = edge;

        // Clear the interrupt
        ICU.IR[irqNum].BIT.IR = 0;

        InterruptRegister(irqNum, G_eintFuncs[aChannel], aPriority, aName);

        // Do not loop
        break;
    }

    return error;
}
/*---------------------------------------------------------------------------*
 * 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_MC_AR1020_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_MC_AR1020_Poll(void *aWorkspace, T_uezTSReading *aReading)
{
    T_MC_AR1020Workspace *p =
        (T_MC_AR1020Workspace *) aWorkspace;
    T_uezError error;
    I2C_Request r;
    T_uezDevice i2c2;
    TUInt8 data[5] = {0,0,0,0,0};
    HAL_GPIOPort **p_gpio;
    const TUInt32 pin = 15;
    TUInt32 Read = 0;
    TUInt32 x;
    TUInt32 y;

    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 ( Read == 0)
    {
        (aReading->iFlags) = (p->iLastTouch) ;
        return UEZ_ERROR_NONE;
    }
    else
    {
        r.iAddr = AR1020_I2C_ADDRESS;
        r.iSpeed = AR1020_I2C_SPEED;
        r.iWriteData = 0;
        r.iWriteLength = 0;
        r.iWriteTimeout = UEZ_TIMEOUT_INFINITE;
        r.iReadData = data;
        r.iReadLength = 5;
        r.iReadTimeout = UEZ_TIMEOUT_INFINITE;

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

        UEZI2CTransaction(i2c2, &r);

        UEZI2CClose(i2c2);

        x = data[4];
        x = (x <<7);
        x = x | data[3];

        y = data[2];
        y = (y <<7);
        y = y | data[1];

        //if(p->iIsCalibrating)
        //for testing printf("%04x _ %04X\r\n", x, y);

        if((data[0] & 0x01)== 0x01)
        {
            (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_MC_AR1020_ApplyCalibration(
                    p, x, y,
                    (TUInt32 *) &aReading->iX,
                    (TUInt32 *) &aReading->iY);
            }
        }
        else
        {
            (aReading->iFlags) = (p->iLastTouch)= 0;
            (aReading->iX) = (p->iLastX);
            (aReading->iY) = (p->iLastY);

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

        return error;
    }
}