⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adc_example.c

📁 含t h r e a d x,u c o s 的b s p
💻 C
字号:
/***********************************************************************
 * $Workfile:   adc_example.c  $
 * $Revision:   1.1  $
 * $Author:   WellsK  $
 * $Date:   Oct 28 2003 10:51:54  $
 *
 * Project: ADC driver example
 *
 * Description:
 *     A simple ADC driver example that demonstrates use of interrupts
 *     and automatic toucschreen detection.
 *
 * Revision History:
 * $Log:   //smaicnt2/pvcs/VM/sharpmcu/archives/sharpmcu/software/csps/lh7a404/bsps/sdk7a404/examples/adc_ts_int/adc_example.c-arc  $
 * 
 *    Rev 1.1   Oct 28 2003 10:51:54   WellsK
 * Updated to use common LCD parameters file.
 * 
 *    Rev 1.0   Oct 15 2003 16:38:56   WellsK
 * Initial revision.
 * 
 *
 ***********************************************************************
 * SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION
 * OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE,
 * AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES, 
 * SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE.
 *
 * SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY 
 * FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A 
 * SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE
 * FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS.
 *
 * COPYRIGHT (C) 2001 SHARP MICROELECTRONICS OF THE AMERICAS, INC.
 *     CAMAS, WA
 **********************************************************************/

#include "abl_types.h"
#include "abl_irq_fiq.h"
#include "abl_swim.h"
#include "abl_swim_font.h"
#include "abl_arm922t_cp15_driver.h"
#include "lh7a404_adc_driver.h"
#include "lh7a404_clcdc_driver.h"
#include "lh7a404_gpio_driver.h"
#include "sdk7a404_cpld_driver.h"
#include "lh7a404_vic_driver.h"

/* Pick only one display! */
#define LCD_DISPLAY sharp_lq035
//#define LCD_DISPLAY sharp_lq039
//#define LCD_DISPLAY sharp_lq057
//#define LCD_DISPLAY sharp_lq064
//#define LCD_DISPLAY sharp_lq104
//#define LCD_DISPLAY sharp_lq121

/* ADC and LCD device handles */
STATIC INT_32 adcdev, lcddev;

/* Physical address of LCD frame buffer */
#define FBPHY 0xC1C00000

/* Sample enumeration type */
typedef enum {XCOORD, YCOORD} SAMPLE_T;

/* Sample array */
static UNS_16 samples[YCOORD + 1];

/* Flag used to indicate that sample is complete */
static volatile BOOL_32 sampleflag;

/* Pendown event count flag */
static volatile UNS_16 pdevent;

/***********************************************************************
 *
 * Function: make_hex_str
 *
 * Purpose: Convert a number to a hex string
 *
 * Processing:
 *     Using successive nibble computation, compute the base16 value of
 *     a number into a string and return the string value to the caller.
 *
 * Parameters:
 *     str      : Where to place the generated string
 *     iteration: Number to generate a string from
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
void make_hex_str(CHAR *str,
                  UNS_16 iteration)
{
    INT_32 cnt = 0;
    UNS_8 li;

    while (cnt < 4)
    {
        li = (UNS_8) ((iteration >> ((3 - cnt) * 4)) & 0xF);
        if (li > 9)
        {
            str[cnt] = 'A' + li - 10;
        }
        else
        {
            str[cnt] = '0' + li;
        }

        cnt++;
    }

    str[cnt] = '\0';
}

/***********************************************************************
 *
 * Function: pd_callback
 *
 * Purpose: ADC pendown event callback function
 *
 * Processing:
 *     This function is called when the ADC has detected a pendown
 *     event. It is not really needed, but provided as part of the
 *     example to show how it is used.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
void pd_callback(void)
{
    /* Increment pendown event flag */
    pdevent++;
}

/***********************************************************************
 *
 * Function: eos_callback
 *
 * Purpose: ADC end of sequence event callback function
 *
 * Processing:
 *     This function is called when the ADC has completed the sample
 *     sequence that was started when a pendown event opccured and will
 *     read the X/Y coordinate data.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
void eos_callback(void)
{
    INT_32 idx;
    
    /* Read samples from ADC */
    adc_read_ring(adcdev, samples, sizeof(samples));

    /* Strip off sample number and justify data */
    for (idx = (INT_32) XCOORD; idx <= (INT_32) YCOORD; idx++)
    {
        samples[idx] = ADC_READ_OUTPUT(samples[idx]);
    }
    
    /* Set sample flag so background knows it has data */
    sampleflag = TRUE;
}

/***********************************************************************
 *
 * Function: c_entry
 *
 * Purpose: ADC example
 *
 * Processing:
 *     This example will configure the Touchscreen controller (ADC) and
 *     ADC driver to sample the pendown status, touchscreen X
 *     coordinate, and touchscreen Y coordinate in interrupt triggered
 *     mode. In this mode, a user defined function will be called when
 *     a pendown event occurs and coordinate data has been fetched.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Always returns 1
 *
 * Notes:
 *     This example assumes that the touchscreen controller is
 *     configured as follows:
 *         AN0/AN1 drive X grid, X value is read on AN2
 *         AN2/AN3 drive Y grid, Y value is read on AN0
 *
 **********************************************************************/
int c_entry(void)
{
    INT_32 idx;
    ADC_CH_CONV_T chdata;
    COLOR_T *fblog;
    SWIM_WINDOW_T win1;
    CHAR str[32];

    /* Initial count and flasg values */
    sampleflag = FALSE;
    pdevent = 0;

    /* Disable interrupts in ARM core */
    disable_irq_fiq();

    /* Set virtual address of MMU table (needed for VIC driver
       functions) */
    cp15_set_vmmu_addr((UNS_32 *) cp15_get_ttb());

    /* Initialize interrupt system */
    vic_initialize(0xC0000000);

    /* Install standard IRQ dispatcher at ARM IRQ vector */
    vic_install_arm_handler(IRQ_VEC, (PFV) vic_arm_irq_dispatcher);

    /* Install VIC1 and VIC2 handlers */
    vic_install_arm_handler(VIC1_IRQ_VEC, (PFV) vic1_irq_dispatcher);
    vic_install_arm_handler(VIC2_IRQ_VEC, (PFV) vic2_irq_dispatcher);

    /* Install ADC handler in the VIC dispatcher */
    vic_install_handler(VIC_ADCINTR, VIC_IRQ, (PFV) adc_isr);



    
    /* Setup LCD muxing for all 16 data bits */
    gpio_lcd_signal_select(GPIO_LCDV_0_15);

    /* Setup LCD paramaters in the LCD controller */
    lcddev = lcd_open(CLCDC, (INT_32) &LCD_DISPLAY);

    /* Set frame buffer address */
    lcd_ioctl(lcddev, LCD_SET_UP_FB, FBPHY);

    /* Also make sure that the CPLD_JTAG_OE signal is in the
       correct state */
    gpio_set_data_dir(GPIO_PORT_A, 0x04, GPIO_OUTPUT);
    gpio_data_write(GPIO_PORT_A, 0x04);

    /* Set color depth to 16 bits per pixel */
    lcd_ioctl(lcddev, LCD_SET_BPP, 16);
 
    /* For displays that require more bandwidth, set DMA to request
       a transfer on 4 words empty instead of the default 8. This may
       help prevent 'display tearing' due to a starved LCD controller */
    idx = lcd_ioctl(lcddev, LCD_GET_STATUS, LCD_XSIZE) *
        lcd_ioctl(lcddev, LCD_GET_STATUS, LCD_YSIZE) *
        sizeof (COLOR_T);
    if (idx >= (800 * 600 * 2))
    {
        /* Displays of 800x600 pixels and 16-bits of color (or larger)
           will use faster DMA requests */
        lcd_ioctl(lcddev, LCD_DMA_ON_4MT, 1);
    }

    /* HRTFT/TFT panel board initialization only */
    if ((lcd_ioctl(lcddev, LCD_GET_STATUS, LCD_PANEL_TYPE) == HRTFT) ||
        (lcd_ioctl(lcddev, LCD_GET_STATUS, LCD_PANEL_TYPE) == ADTFT) ||
        (lcd_ioctl(lcddev, LCD_GET_STATUS, LCD_PANEL_TYPE) == TFT))
    {
        /* Enable power to the LCD panel (sets VDDEN on PC3) */
        gpio_set_data_dir(GPIO_PORT_C, 0x08, GPIO_OUTPUT);
        gpio_data_write(GPIO_PORT_C, 0x08);
    }
    else
    {
        /* Other displays - do nothing (yet) */
        ;
    }

    /* Enable LCD controller and power signals */
    lcd_ioctl(lcddev, LCD_PWENABLE, 1);
    
    /* Get virtual address of frame buffer */
    fblog = cp15_map_physical_to_virtual(FBPHY);

    /* Create SWIM window for output data */
    swim_window_open(&win1, LCD_DISPLAY.pixels_per_line,
        LCD_DISPLAY.lines_per_panel, fblog, 0,
        0, (LCD_DISPLAY.pixels_per_line - 1),
        (LCD_DISPLAY.lines_per_panel - 1), 1, WHITE, BLACK, DARKGRAY);
    swim_set_title(&win1, "Touchscreen coordinates", GREEN);
    
    /* Open ADC */
    if ((adcdev = adc_open(ADC, 0)) == 0x00000000)
    {
        return 0;
    }

    /* Reset the ADC and driver to a known 'clean' state. This isn't
       needed directly after open, but is good practice */
    adc_ioctl(adcdev, ADC_SET_STATE, ADC_MODE_RESET);

    /* Set ADC clock speed to 400KHz */
    adc_ioctl(adcdev, ADC_SET_CLOCK, 400000);

    /* Setup idle channel */
    chdata.settle_clks = 100;
    chdata.inpc = ADC_ANALOG_1;
    chdata.innc = ADC_REFN_VREFN;
    chdata.vrefp = ADC_REFP_VREF;
    chdata.vrefn = ADC_REFN_VREF;
    chdata.bias = (ADC_FET_AN0_VDDA1K | ADC_FET_AN3_GND);
    adc_ioctl(adcdev, ADC_SET_IDLE_CH, (INT_32) &chdata);
    
    /* X coordinate channel */
    chdata.settle_clks = 100;
    chdata.inpc = ADC_ANALOG_0;
    chdata.innc = ADC_REFN_VREFN;
    chdata.vrefp = ADC_REFP_AN2;
    chdata.vrefn = ADC_REFN_AN3;
    chdata.bias = (ADC_FET_AN2_VDDA | ADC_FET_AN3_GND);
    adc_ioctl(adcdev, ADC_ADD_CHANNEL, (INT_32) &chdata);

    /* Y coordinate channel */
    chdata.settle_clks = 100;
    chdata.inpc = ADC_ANALOG_2;
    chdata.innc = ADC_REFN_VREFN;
    chdata.vrefp = ADC_REFP_AN0;
    chdata.vrefn = ADC_REFN_AN1;
    chdata.bias = (ADC_FET_AN0_VDDA | ADC_FET_AN1_GND);
    adc_ioctl(adcdev, ADC_ADD_CHANNEL, (INT_32) &chdata);

    /* Disable internal reference voltage */
    adc_ioctl(adcdev, ADC_ENABLE_IREF, 1);
    
    /* Set FIFO trip level to 2 samples (X and Y) */
    adc_ioctl(adcdev, ADC_SET_WMLEVEL, 2);

    /* Enable the ADC interrupt to interrupt on end of sequence and
       a pendown event */
    adc_ioctl(adcdev, ADC_INT_ENABLE, (ADC_EOS_INT | ADC_PEN_INT));

    /* Clear the FIFO of any garbage */
    adc_ioctl(adcdev, ADC_CLEAR_FIFO, 0);

    /* Install the pendown event callback function (this is not
       really needed) */
    adc_ioctl(adcdev, ADC_INSTALL_PDC, (INT_32) pd_callback);

    /* Install the end of sequence event callback function */
    adc_ioctl(adcdev, ADC_INSTALL_EOS, (INT_32) eos_callback);

    /* Set sample mode to sample on pendown event */
    adc_ioctl(adcdev, ADC_SET_STATE, ADC_PENDOWN_TRIGGERED);

    /* Enable ADC interrupt in the VIC */
    vic_int_enable(VIC_ADCINTR, TRUE);

    /* Enable IRQ interrupts in ARM core */
    enable_irq();
     
    /* Do samples */
    while (pdevent < 5000)
    {
        /* Wait for samples */
        if (sampleflag == TRUE)
        {
            sampleflag = FALSE;
            swim_put_text_xy(&win1, "Events: ", 5, 5);
            make_hex_str(str, pdevent);
            swim_put_text(&win1, str);
            swim_put_text(&win1, "    ");
            swim_put_text_xy(&win1, "X: ", 5, 30);
            make_hex_str(str, samples[XCOORD]);
            swim_put_text(&win1, str);
            swim_put_text(&win1, "    ");
            swim_put_text_xy(&win1, "Y: ", 5, 55);
            make_hex_str(str, samples[YCOORD]);
            swim_put_text(&win1, str);
            swim_put_text(&win1, "    ");

            idx++;
        }
    }
    
    /* Disable ADC interrupt in the VIC */
    vic_int_enable(VIC_ADCINTR, FALSE);

    /* Disable IRQ interrupts in ARM core */
    disable_irq();

    /* Close ADC and LCD */
    adc_close(adcdev);
    adc_close(lcddev);

    return 1;
}

#ifndef __GNUC__
/* With ARM and GHS toolsets, the entry point is main() - this will
   allow the linker to generate wrapper code to setup stacks, allocate
   heap area, and initialize and copy code and data segments. For GNU
   toolsets, the entry point is through __start() in the crt0_gnu.asm
   file, and that startup code will setup stacks and data */
int main(void)
{
    return c_entry();
}
#endif

⌨️ 快捷键说明

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