sdk7a400_ts_driver.c

来自「Sharp LH7A400 BSP平台无关部分的代码,有很高的参考价值,尤其是系」· C语言 代码 · 共 622 行 · 第 1/2 页

C
622
字号
 * Parameters:
 *     ipbase: Touchscreen device address
 *     arg   : Not used
 *
 * Outputs: None
 *
 * Returns: The pointer to a Touchscreen config structure or NULL
 *
 * Notes: None
 *
 **********************************************************************/
INT_32 ts_open(void *ipbase,
               INT_32 arg)
{
    INT_32 status = 0;

    if (tscfg.init == FALSE)
    {
        /* Device is valid and not previously initialized */
        tscfg.init = TRUE;

        /* Clear the touchscreen data queue */
        tscfg.rx_head = tscfg.rx_tail = 0;

        /* Disable interrupt in CPLD */
        cpld_ts_int_enable(FALSE);

        /* Default state of callback is NULL */
        tscfg.tscb_func = (PFV) NULL;

        /* Perform a 'dummy' sample read to initialize the ADS7843
           controller */
        * (volatile UNS_16 *) INTMSK_REG_BASE =
            * (volatile UNS_16 *) INTMSK_REG_BASE & ~INTMSK_PENIRQ_IN;
        ts_command(TSC_START |
            TSC_MEASURE_X | TSC_DIFF | TSC_CONVBITS | TSC_SINGLE);
        * (volatile UNS_16 *) INTMSK_REG_BASE =
            * (volatile UNS_16 *) INTMSK_REG_BASE | INTMSK_PENIRQ_IN;

        /* Return pointer to touchscreen configuration structure */
        status = (INT_32) &tscfg;
    }

    return status;
}

/***********************************************************************
 *
 * Function: ts_close
 *
 * Purpose: Close the touchscreen interface
 *
 * Processing:
 *     If init is not TRUE, then return _ERROR to the caller as the
 *     device was not previously opened. Otherwise, set init to FALSE,
 *     disable the touchscreen interrupt, and return _NO_ERROR to the
 *     caller.
 *
 * Parameters:
 *     devid: Pointer to touchscreen config structure
 *
 * Outputs: None
 *
 * Returns: The status of the close operation
 *
 * Notes: None
 *
 **********************************************************************/
STATUS ts_close(INT_32 devid)
{
    TS_CFG_T *tscfgptr = (TS_CFG_T *) devid;
    STATUS status = _ERROR;

    if (tscfgptr->init == TRUE)
    {
        /* Disable touchscreen interrupt in CPLD */
        cpld_ts_int_enable(FALSE);
        
        status = _NO_ERROR;
        tscfgptr->init = FALSE;
    }

    return status;
}

/***********************************************************************
 *
 * Function: ts_ioctl
 *
 * Purpose: Touchscreen configuration block
 *
 * Processing:
 *     This function is a large case block. Based on the passed function
 *     and option values, set or get the appropriate touschreen
 *     parameter.
 *
 * Parameters:
 *     devid: Pointer to touchscreen config structure
 *     cmd:   ioctl command
 *     arg:   ioctl argument
 *
 * Outputs: None
 *
 * Returns: The status of the ioctl operation
 *
 * Notes: None
 *
 **********************************************************************/
STATUS ts_ioctl(INT_32 devid,
                INT_32 cmd,
                INT_32 arg)
{
    TS_CFG_T *tscfgptr = (TS_CFG_T *) devid;
    STATUS status = _ERROR;

    if (tscfgptr->init == TRUE)
    {
        status = _NO_ERROR;
        switch (cmd)
        {
            case TS_ENABLE:
                /* Enable or disable the touchscreen interface */
                if (arg == 0)
                {
                    /* Disable interface */
                    cpld_ts_int_enable(FALSE);
                }
                else
                {
                    /* Enable interface */
                    cpld_ts_int_enable(TRUE);
                }
                break;
 
            case TS_FLUSH_QUEUE:
                /* Flush the touchscreen data queue */
                tscfg.rx_head = tscfg.rx_tail = 0;
                break;

            case TS_REQUEST_DATA:
                /* Gets the current touchscreen data and places it into
                   the data queue */
                ts_sample_data(tscfgptr);
                break;

            case TS_INSTALL_TSCB:
                /* Install a touchscreen callback function */
                tscfgptr->tscb_func = (PFV) arg;
                break;

            case TS_ENABLE_INT:
                /* Enable or disable the touchscreen interrupt */
                if (arg == 1)
                {
                    cpld_ts_int_enable(TRUE);
                }
                else
                {
                    cpld_ts_int_enable(FALSE);
                }
                break;

            case TS_GET_STATUS:
                /* Return touchscreen status */
                switch (arg)
                {
                    case TS_PEND_STATE:
                        /* Returns 1 if a touchscreen interrupt
                           pending */
                        status = 0;
                        if (cpld_ts_int_pending() == TRUE)
                        {
                            status = 1;
                        }
                        break;
                        
                    default:
                        /* Unsupported parameter */
                        status = SMA_BAD_PARAMS;
                        break;
                }
                break;

            default:
                /* Unsupported parameter */
                status = SMA_BAD_PARAMS;
        }
    }

    return status;
}

/***********************************************************************
 *
 * Function: ts_read
 *
 * Purpose: Touchscreen read function
 *
 * Processing:
 *     While the use buffer is greater than a sample size and there are
 *     samples to read in the queue, read the sample out of the top of
 *     the queue into the passed sample buffer. Increment the queue tail
 *     pointer. If the tail pointer exceeds the queue size, reset the
 *     tail pointer to 0.
 *
 * Parameters:
 *     devid:     Pointer to touchscreen config structure
 *     buffer:    Pointer to a TS_DATA_T structure (or array of them)
 *     max_bytes: Size of the TS_DATA_T structure(s)
 *
 * Outputs: None
 *
 * Returns: The pendown state and raw X/Y touchscreen values
 *
 * Notes: None
 *
 **********************************************************************/
INT_32 ts_read(INT_32 devid,
               void *buffer,
               INT_32 max_bytes)
{
    TS_DATA_T *tsdata = (TS_DATA_T *) buffer;
    TS_CFG_T *tscfgptr = (TS_CFG_T *) devid;
    INT_32 bytes = 0;

    /* Make sure size of buffer is large enough and data exists to
       copy */
    while ((max_bytes >= sizeof (TS_DATA_T)) &&
        (tscfgptr->rx_head != tscfgptr->rx_tail))
    {
        /* Move data to user buffer */
        tsdata->pendown = tscfgptr->queue[tscfgptr->rx_tail].pendown;
        tsdata->xraw = tscfgptr->queue[tscfgptr->rx_tail].xraw;
        tsdata->yraw = tscfgptr->queue[tscfgptr->rx_tail].yraw;

        /* Increment tail pointer for the queue and bound it */
        tscfgptr->rx_tail++;
        if (tscfgptr->rx_tail >= TSQ_SIZE)
        {
            tscfgptr->rx_tail = 0;
        }

        /* Update next user buffer area if it exists */
        tsdata++;
        max_bytes = max_bytes - sizeof (TS_DATA_T);
        bytes = bytes + sizeof (TS_DATA_T);
    }

    return bytes;
}

/***********************************************************************
 *
 * Function: ts_write
 *
 * Purpose: Touchscreen write function
 *
 * Processing:
 *     This function is not used and is a stub function only.
 *
 * Parameters:
 *     devid:   Pointer to touchscreen config structure
 *     buffer:  Pointer to data buffer to copy from
 *     n_bytes: Number of bytes to write
 *
 * Outputs: None
 *
 * Returns: Always returns 0
 *
 * Notes: None
 *
 **********************************************************************/
INT_32 ts_write(INT_32 devid,
                void *buffer,
                INT_32 n_bytes)
{    
    return 0;
}

/***********************************************************************
 *
 * Function: ts_isr
 *
 * Purpose: Touchscreen Interrupt
 *
 * Processing:
 *     Get the touchscreen data and place it into the data queue by
 *     calling the ts_sample_data() function. If the user defined
 *     callback function exists, call it before exiting.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
void ts_isr(void)
{
    /* Sample the data and place it into the data queue */
    ts_sample_data(&tscfg);

    /* Call user defined callback function if it exists */
    if (tscfg.tscb_func != (PFV) NULL)
    {
        tscfg.tscb_func();
    }
}

⌨️ 快捷键说明

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