//*****************************************************************************
//
// Main 'C' Language entry point.
//
//*****************************************************************************
int
main(void)
{
    float fTemperature, fPressure, fAltitude;
    int32_t i32IntegerPart;
    int32_t i32FractionPart;
    tContext sContext;
    uint32_t ui32SysClock;
    char pcBuf[15];

    //
    // Setup the system clock to run at 40 Mhz from PLL with crystal reference
    //
    ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                           SYSCTL_OSC_MAIN |
                                           SYSCTL_USE_PLL |
                                           SYSCTL_CFG_VCO_480), 40000000);

    //
    // Configure the device pins.
    //
    PinoutSet();

    //
    // Initialize the display driver.
    //
    Kentec320x240x16_SSD2119Init(ui32SysClock);

    //
    // Initialize the graphics context.
    //
    GrContextInit(&sContext, &g_sKentec320x240x16_SSD2119);

    //
    // Draw the application frame.
    //
    FrameDraw(&sContext, "bmp180");

    //
    // Flush any cached drawing operations.
    //
    GrFlush(&sContext);

    //
    // Enable UART0
    //
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Initialize the UART for console I/O.
    //
    UARTStdioConfig(0, 115200, ui32SysClock);

    //
    // Print the welcome message to the terminal.
    //
    UARTprintf("\033[2JBMP180 Example\n");

    //
    // The I2C3 peripheral must be enabled before use.
    //
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);

    //
    // Configure the pin muxing for I2C3 functions on port G4 and G5.
    // This step is not necessary if your part does not support pin muxing.
    //
    MAP_GPIOPinConfigure(GPIO_PG4_I2C3SCL);
    MAP_GPIOPinConfigure(GPIO_PG5_I2C3SDA);

    //
    // Select the I2C function for these pins.  This function will also
    // configure the GPIO pins pins for I2C operation, setting them to
    // open-drain operation with weak pull-ups.  Consult the data sheet
    // to see which functions are allocated per pin.
    //
    MAP_GPIOPinTypeI2CSCL(GPIO_PORTG_BASE, GPIO_PIN_4);
    MAP_GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_5);

    //
    // Enable interrupts to the processor.
    //
    MAP_IntMasterEnable();

    //
    // Initialize I2C3 peripheral.
    //
    I2CMInit(&g_sI2CInst, I2C3_BASE, INT_I2C3, 0xff, 0xff,
             ui32SysClock);

    //
    // Initialize the BMP180
    //
    BMP180Init(&g_sBMP180Inst, &g_sI2CInst, BMP180_I2C_ADDRESS,
               BMP180AppCallback, &g_sBMP180Inst);

    //
    // Wait for initialization callback to indicate reset request is complete.
    //
    while(g_vui8DataFlag == 0)
    {
        //
        // Wait for I2C Transactions to complete.
        //
    }

    //
    // Reset the data ready flag
    //
    g_vui8DataFlag = 0;

    //
    // Enable the system ticks at 10 hz.
    //
    MAP_SysTickPeriodSet(ui32SysClock / (10 * 3));
    MAP_SysTickIntEnable();
    MAP_SysTickEnable();

    //
    // Configure PQ4 to control the blue LED.
    //
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTQ_BASE, GPIO_PIN_4);

    //
    // Print temperature, pressure and altitude labels once on the LCD.
    //
    GrStringDraw(&sContext, "Temperature", 11,
                 ((GrContextDpyWidthGet(&sContext) / 2) - 96),
                 ((GrContextDpyHeightGet(&sContext) - 32) / 2) - 24, 1);
    GrStringDraw(&sContext, "Pressure", 8,
                 ((GrContextDpyWidthGet(&sContext) / 2) - 63),
                 (GrContextDpyHeightGet(&sContext) - 32) / 2, 1);
    GrStringDraw(&sContext, "Altitude", 8,
                 ((GrContextDpyWidthGet(&sContext) / 2) - 59),
                 ((GrContextDpyHeightGet(&sContext) - 32) / 2) + 24, 1);

    //
    // Begin the data collection and printing.  Loop Forever.
    //
    while(1)
    {
        //
        // Read the data from the BMP180 over I2C.  This command starts a
        // temperature measurement.  Then polls until temperature is ready.
        // Then automatically starts a pressure measurement and polls for that
        // to complete.  When both measurement are complete and in the local
        // buffer then the application callback is called from the I2C
        // interrupt context.  Polling is done on I2C interrupts allowing
        // processor to continue doing other tasks as needed.
        //
        BMP180DataRead(&g_sBMP180Inst, BMP180AppCallback, &g_sBMP180Inst);
        while(g_vui8DataFlag == 0)
        {
            //
            // Wait for the new data set to be available.
            //
        }

        //
        // Reset the data ready flag.
        //
        g_vui8DataFlag = 0;

        //
        // Get a local copy of the latest temperature data in float format.
        //
        BMP180DataTemperatureGetFloat(&g_sBMP180Inst, &fTemperature);

        //
        // Convert the floats to an integer part and fraction part for easy
        // print.
        //
        i32IntegerPart = (int32_t) fTemperature;
        i32FractionPart =(int32_t) (fTemperature * 1000.0f);
        i32FractionPart = i32FractionPart - (i32IntegerPart * 1000);
        if(i32FractionPart < 0)
        {
            i32FractionPart *= -1;
        }

        //
        // Print temperature with three digits of decimal precision to LCD and
        // terminal.
        //
        usnprintf(pcBuf, sizeof(pcBuf), "%03d.%03d ", i32IntegerPart,
                                                     i32FractionPart);
        GrStringDraw(&sContext, pcBuf, 8,
                     ((GrContextDpyWidthGet(&sContext) / 2) + 16),
                     ((GrContextDpyHeightGet(&sContext) - 32) / 2) - 24, 1);
        UARTprintf("Temperature %3d.%03d\t\t", i32IntegerPart,
                                               i32FractionPart);

        //
        // Get a local copy of the latest air pressure data in float format.
        //
        BMP180DataPressureGetFloat(&g_sBMP180Inst, &fPressure);

        //
        // Convert the floats to an integer part and fraction part for easy
        // print.
        //
        i32IntegerPart = (int32_t) fPressure;
        i32FractionPart =(int32_t) (fPressure * 1000.0f);
        i32FractionPart = i32FractionPart - (i32IntegerPart * 1000);
        if(i32FractionPart < 0)
        {
            i32FractionPart *= -1;
        }

        //
        // Print Pressure with three digits of decimal precision to LCD and
        // terminal.
        //
        usnprintf(pcBuf, sizeof(pcBuf), "%3d.%03d ", i32IntegerPart,
                                                     i32FractionPart);
        GrStringDraw(&sContext, pcBuf, -1,
                     ((GrContextDpyWidthGet(&sContext) / 2) + 16),
                     (GrContextDpyHeightGet(&sContext) - 32) / 2, 1);
        UARTprintf("Pressure %3d.%03d\t\t", i32IntegerPart, i32FractionPart);

        //
        // Calculate the altitude.
        //
        fAltitude = 44330.0f * (1.0f - powf(fPressure / 101325.0f,
                                            1.0f / 5.255f));

        //
        // Convert the floats to an integer part and fraction part for easy
        // print.
        //
        i32IntegerPart = (int32_t) fAltitude;
        i32FractionPart =(int32_t) (fAltitude * 1000.0f);
        i32FractionPart = i32FractionPart - (i32IntegerPart * 1000);
        if(i32FractionPart < 0)
        {
            i32FractionPart *= -1;
        }

        //
        // Print altitude with three digits of decimal precision to LCD and
        // terminal.
        //
        usnprintf(pcBuf, sizeof(pcBuf), "%3d.%03d ", i32IntegerPart,
                                                     i32FractionPart);
        GrStringDraw(&sContext, pcBuf, 8,
                     ((GrContextDpyWidthGet(&sContext) / 2) + 16),
                     ((GrContextDpyHeightGet(&sContext) - 32) / 2) + 24, 1);
        UARTprintf("Altitude %3d.%03d", i32IntegerPart, i32FractionPart);

        //
        // Print new line.
        //
        UARTprintf("\n");

        //
        // Delay to keep printing speed reasonable. About 100 milliseconds.
        //
        MAP_SysCtlDelay(ui32SysClock / (10 * 3));
    }
}
int
main(void)
{
    tContext sContext;
    tRectangle sRect;

    //
    // The FPU should be enabled because some compilers will use floating-
    // point registers, even for non-floating-point code.  If the FPU is not
    // enabled this will cause a fault.  This also ensures that floating-
    // point operations could be added to this application and would work
    // correctly and use the hardware floating-point unit.  Finally, lazy
    // stacking is enabled for interrupt handlers.  This allows floating-
    // point instructions to be used within interrupt handlers, but at the
    // expense of extra stack usage.
    //
    FPUEnable();
    FPULazyStackingEnable();

    //
    // Set the clock to 40Mhz derived from the PLL and the external oscillator
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
                       SYSCTL_OSC_MAIN);

    //
    // Initialize the display driver.
    //
    Adafruit320x240x16_ILI9325Init();

    //
    // Initialize the graphics context.
    //
    GrContextInit(&sContext, &g_sAdafruit320x240x16_ILI9325);

    //
    // Configure and enable uDMA
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);
    SysCtlDelay(10);
    uDMAControlBaseSet(&sDMAControlTable[0]);
    uDMAEnable();

    //
    // Initialize the touch screen driver and have it route its messages to the
    // widget tree.
    //
    TouchScreenInit();

    //
    // Paint touch calibration targets and collect calibration data
    //
    GrContextForegroundSet(&sContext, ClrWhite);
    GrContextBackgroundSet(&sContext, ClrBlack);
    GrContextFontSet(&sContext, &g_sFontCm20);
    GrStringDraw(&sContext, "Touch center of circles to calibrate", -1, 0, 0, 1);
    GrCircleDraw(&sContext, 32, 24, 10);
    GrFlush(&sContext);
    TouchScreenCalibrationPoint(32, 24, 0);

    GrCircleDraw(&sContext, 280, 200, 10);
    GrFlush(&sContext);
    TouchScreenCalibrationPoint(280, 200, 1);

    GrCircleDraw(&sContext, 200, 40, 10);
    GrFlush(&sContext);
    TouchScreenCalibrationPoint(200, 40, 2);

    //
    // Calculate and set calibration matrix
    //
    long* plCalibrationMatrix = TouchScreenCalibrate();
    
    //
    // Write out calibration data if successful
    //
    if(plCalibrationMatrix)
    {
    	char pcStringBuf[20];
    	usprintf(pcStringBuf, "A %d", plCalibrationMatrix[0]);
    	GrStringDraw(&sContext, pcStringBuf, -1, 0, 20, 1);
    	usprintf(pcStringBuf, "B %d", plCalibrationMatrix[1]);
    	GrStringDraw(&sContext, pcStringBuf, -1, 0, 40, 1);
    	usprintf(pcStringBuf, "C %d", plCalibrationMatrix[2]);
    	GrStringDraw(&sContext, pcStringBuf, -1, 0, 60, 1);
    	usprintf(pcStringBuf, "D %d", plCalibrationMatrix[3]);
    	GrStringDraw(&sContext, pcStringBuf, -1, 0, 80, 1);
    	usprintf(pcStringBuf, "E %d", plCalibrationMatrix[4]);
    	GrStringDraw(&sContext, pcStringBuf, -1, 0, 100, 1);
    	usprintf(pcStringBuf, "F %d", plCalibrationMatrix[5]);
    	GrStringDraw(&sContext, pcStringBuf, -1, 0, 120, 1);
    	usprintf(pcStringBuf, "Div %d", plCalibrationMatrix[6]);
    	GrStringDraw(&sContext, pcStringBuf, -1, 0, 140, 1);
    	TouchScreenCalibrationPoint(0,0,0);	// wait for dummy touch
    }

    //
    // Enable touch screen event handler for grlib widgets
    //
    TouchScreenCallbackSet(WidgetPointerMessage);

    //
    // Fill the top 24 rows of the screen with blue to create the banner.
    //
    sRect.sXMin = 0;
    sRect.sYMin = 0;
    sRect.sXMax = GrContextDpyWidthGet(&sContext) - 1;
    sRect.sYMax = 23;
    GrContextForegroundSet(&sContext, ClrDarkBlue);
    GrRectFill(&sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&sContext, ClrWhite);
    GrRectDraw(&sContext, &sRect);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&sContext, &g_sFontCm20);
    GrStringDrawCentered(&sContext, "grlib demo", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 8, 0);

    //
    // Add the title block and the previous and next buttons to the widget
    // tree.
    //
    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sPrevious);
    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sTitle);
    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sNext);

    //
    // Add the first panel to the widget tree.
    //
    g_ulPanel = 0;
    WidgetAdd(WIDGET_ROOT, (tWidget *)g_psPanels);
    CanvasTextSet(&g_sTitle, g_pcPanelNames[0]);

    //
    // Issue the initial paint request to the widgets.
    //
    WidgetPaint(WIDGET_ROOT);

    //
    // Loop forever handling widget messages.
    //
    while(1)
    {
        //
        // Process any messages in the widget message queue.
        //
        WidgetMessageQueueProcess();
    }
}
Esempio n. 3
0
//*****************************************************************************
//
// A simple demonstration of the features of the TivaWare Graphics Library.
//
//*****************************************************************************
int
main(void)
{
    tContext sContext;
    tRectangle sRect;

    //
    // The FPU should be enabled because some compilers will use floating-
    // point registers, even for non-floating-point code.  If the FPU is not
    // enabled this will cause a fault.  This also ensures that floating-
    // point operations could be added to this application and would work
    // correctly and use the hardware floating-point unit.  Finally, lazy
    // stacking is enabled for interrupt handlers.  This allows floating-
    // point instructions to be used within interrupt handlers, but at the
    // expense of extra stack usage.
    //
    FPUEnable();
    FPULazyStackingEnable();

    //
    // Run from the PLL at 120 MHz.
    //
    g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                            SYSCTL_OSC_MAIN |
                                            SYSCTL_USE_PLL |
                                            SYSCTL_CFG_VCO_480), 120000000);

    //
    // Initialize the display driver.
    //
    Kentec320x240x16_SSD2119Init(g_ui32SysClock);

    //
    // Initialize the graphics context.
    //
    GrContextInit(&sContext, &g_sKentec320x240x16_SSD2119);

    //
    // Fill the top 24 rows of the screen with blue to create the banner.
    //
    sRect.i16XMin = 0;
    sRect.i16YMin = 0;
    sRect.i16XMax = GrContextDpyWidthGet(&sContext) - 1;
    sRect.i16YMax = 23;
    GrContextForegroundSet(&sContext, ClrDarkBlue);
    GrRectFill(&sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&sContext, ClrWhite);
    GrRectDraw(&sContext, &sRect);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&sContext, &g_sFontCm20);
    GrStringDrawCentered(&sContext, "grlib demo", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 8, 0);

    //
    // Configure and enable uDMA
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);
    SysCtlDelay(10);
    uDMAControlBaseSet(&psDMAControlTable[0]);
    uDMAEnable();

    //
    // Initialize the touch screen driver and have it route its messages to the
    // widget tree.
    //
    TouchScreenInit(g_ui32SysClock);
    TouchScreenCallbackSet(WidgetPointerMessage);

    //
    // Add the title block and the previous and next buttons to the widget
    // tree.
    //
    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sPrevious);
    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sTitle);
    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sNext);

    //
    // Add the first panel to the widget tree.
    //
    g_ui32Panel = 0;
    WidgetAdd(WIDGET_ROOT, (tWidget *)g_psPanels);
    CanvasTextSet(&g_sTitle, g_pcPanei32Names[0]);

    //
    // Issue the initial paint request to the widgets.
    //
    WidgetPaint(WIDGET_ROOT);

    //
    // Loop forever handling widget messages.
    //
    while(1) {
        //
        // Process any messages in the widget message queue.
        //
        WidgetMessageQueueProcess();
    }
}
Esempio n. 4
0
//*****************************************************************************
//
// This is the main example program.  It checks to see that the interrupts are
// processed in the correct order when they have identical priorities,
// increasing priorities, and decreasing priorities.  This exercises interrupt
// preemption and tail chaining.
//
//*****************************************************************************
int
main(void)
{
    uint_fast8_t ui8Error;
    uint32_t ui32SysClock;

    //
    // Run from the PLL at 120 MHz.
    //
    ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                           SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
                                           SYSCTL_CFG_VCO_480), 120000000);

    //
    // Configure the device pins.
    //
    PinoutSet();

    //
    // Initialize the display driver.
    //
    Kentec320x240x16_SSD2119Init(ui32SysClock);

    //
    // Initialize the graphics context.
    //
    GrContextInit(&g_sContext, &g_sKentec320x240x16_SSD2119);

    //
    // Draw the application frame.
    //
    FrameDraw(&g_sContext, "interrupts");

    //
    // Put the status header text on the display.
    //
    GrContextFontSet(&g_sContext, g_psFontCm20);
    GrStringDrawCentered(&g_sContext, "Active:      Pending:     ", -1,
                 GrContextDpyWidthGet(&g_sContext) / 2, 150, 0);

    //
    // Configure the B3, L1 and L0 to be outputs to indicate entry/exit of one
    // of the interrupt handlers.
    //
    GPIOPinTypeGPIOOutput(GPIO_A_BASE, GPIO_A_PIN);
    GPIOPinTypeGPIOOutput(GPIO_B_BASE, GPIO_B_PIN);
    GPIOPinTypeGPIOOutput(GPIO_C_BASE, GPIO_C_PIN);
    GPIOPinWrite(GPIO_A_BASE, GPIO_A_PIN, 0);
    GPIOPinWrite(GPIO_B_BASE, GPIO_B_PIN, 0);
    GPIOPinWrite(GPIO_C_BASE, GPIO_C_PIN, 0);

    //
    // Set up and enable the SysTick timer.  It will be used as a reference
    // for delay loops in the interrupt handlers.  The SysTick timer period
    // will be set up for 100 times per second.
    //
    ROM_SysTickPeriodSet(ui32SysClock / 100);
    ROM_SysTickEnable();

    //
    // Reset the error indicator.
    //
    ui8Error = 0;

    //
    // Enable interrupts to the processor.
    //
    ROM_IntMasterEnable();

    //
    // Enable the interrupts.
    //
    ROM_IntEnable(INT_GPIOA);
    ROM_IntEnable(INT_GPIOB);
    ROM_IntEnable(INT_GPIOC);

    //
    // Indicate that the equal interrupt priority test is beginning.
    //
    GrStringDrawCentered(&g_sContext, "Equal Priority", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 60, 1);

    //
    // Set the interrupt priorities so they are all equal.
    //
    ROM_IntPrioritySet(INT_GPIOA, 0x00);
    ROM_IntPrioritySet(INT_GPIOB, 0x00);
    ROM_IntPrioritySet(INT_GPIOC, 0x00);

    //
    // Reset the interrupt flags.
    //
    g_ui32GPIOa = 0;
    g_ui32GPIOb = 0;
    g_ui32GPIOc = 0;
    g_ui32Index = 1;

    //
    // Trigger the interrupt for GPIO C.
    //
    HWREG(NVIC_SW_TRIG) = INT_GPIOC - 16;

    //
    // Put the current interrupt state on the LCD.
    //
    DisplayIntStatus();

    //
    // Verify that the interrupts were processed in the correct order.
    //
    if((g_ui32GPIOa != 3) || (g_ui32GPIOb != 2) || (g_ui32GPIOc != 1))
    {
        ui8Error |= 1;
    }

    //
    // Wait two seconds.
    //
    Delay(2);

    //
    // Indicate that the decreasing interrupt priority test is beginning.
    //
    GrStringDrawCentered(&g_sContext, " Decreasing Priority ", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 60, 1);

    //
    // Set the interrupt priorities so that they are decreasing (i.e. C > B >
    // A).
    //
    ROM_IntPrioritySet(INT_GPIOA, 0x80);
    ROM_IntPrioritySet(INT_GPIOB, 0x40);
    ROM_IntPrioritySet(INT_GPIOC, 0x00);

    //
    // Reset the interrupt flags.
    //
    g_ui32GPIOa = 0;
    g_ui32GPIOb = 0;
    g_ui32GPIOc = 0;
    g_ui32Index = 1;

    //
    // Trigger the interrupt for GPIO C.
    //
    HWREG(NVIC_SW_TRIG) = INT_GPIOC - 16;

    //
    // Put the current interrupt state on the display.
    //
    DisplayIntStatus();

    //
    // Verify that the interrupts were processed in the correct order.
    //
    if((g_ui32GPIOa != 3) || (g_ui32GPIOb != 2) || (g_ui32GPIOc != 1))
    {
        ui8Error |= 2;
    }

    //
    // Wait two seconds.
    //
    Delay(2);

    //
    // Indicate that the increasing interrupt priority test is beginning.
    //
    GrStringDrawCentered(&g_sContext, " Increasing Priority ", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 60, 1);

    //
    // Set the interrupt priorities so that they are increasing (i.e. C < B <
    // A).
    //
    ROM_IntPrioritySet(INT_GPIOA, 0x00);
    ROM_IntPrioritySet(INT_GPIOB, 0x40);
    ROM_IntPrioritySet(INT_GPIOC, 0x80);

    //
    // Reset the interrupt flags.
    //
    g_ui32GPIOa = 0;
    g_ui32GPIOb = 0;
    g_ui32GPIOc = 0;
    g_ui32Index = 1;

    //
    // Trigger the interrupt for GPIO C.
    //
    HWREG(NVIC_SW_TRIG) = INT_GPIOC - 16;

    //
    // Put the current interrupt state on the display.
    //
    DisplayIntStatus();

    //
    // Verify that the interrupts were processed in the correct order.
    //
    if((g_ui32GPIOa != 1) || (g_ui32GPIOb != 2) || (g_ui32GPIOc != 3))
    {
        ui8Error |= 4;
    }

    //
    // Wait two seconds.
    //
    Delay(2);

    //
    // Disable the interrupts.
    //
    ROM_IntDisable(INT_GPIOA);
    ROM_IntDisable(INT_GPIOB);
    ROM_IntDisable(INT_GPIOC);

    //
    // Disable interrupts to the processor.
    //
    ROM_IntMasterDisable();

    //
    // Print out the test results.
    //
    GrStringDrawCentered(&g_sContext, " Interrupt Priority ", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 60, 1);
    if(ui8Error)
    {
        GrStringDrawCentered(&g_sContext, "     Equal: P  Inc: P  Dec: P     ",
                             -1, GrContextDpyWidthGet(&g_sContext) / 2, 150, 1);
        if(ui8Error & 1)
        {
            GrStringDrawCentered(&g_sContext, " F ", -1, 113, 150, 1);
        }
        if(ui8Error & 2)
        {
            GrStringDrawCentered(&g_sContext, " F ", -1, 187, 150, 1);
        }
        if(ui8Error & 4)
        {
            GrStringDrawCentered(&g_sContext, " F ", -1, 272, 150, 1);
        }
    }
    else
    {
        GrStringDrawCentered(&g_sContext, "           Success!           ", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 150, 1);
    }

    //
    // Flush the display.
    //
    GrFlush(&g_sContext);

    //
    // Loop forever.
    //
    while(1)
    {
    }
}
Esempio n. 5
0
//*****************************************************************************
//
// This function initializes the ADC hardware in preparation for data
// acquisition.
//
//*****************************************************************************
void
AcquireInit(void)
{
    unsigned long ulChan;

    //
    // Enable the ADC peripherals and the associated GPIO port
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOP);

    ROM_SysCtlADCSpeedSet(SYSCTL_ADCSPEED_125KSPS);

    ROM_GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 |GPIO_PIN_7 | GPIO_PIN_3);
    ROM_GPIOPinTypeADC(GPIO_PORTP_BASE, GPIO_PIN_0);

//    ROM_ADCReferenceSet(ADC0_BASE, ADC_REF_EXT_3V);
    //ROM_ADCR

    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    HWREG(GPIO_PORTB_BASE + GPIO_O_AMSEL) |= GPIO_PIN_6;
    ADCSequenceDisable(ADC0_BASE,SEQUENCER);

    ROM_ADCSequenceConfigure(ADC0_BASE, SEQUENCER, ADC_TRIGGER_TIMER, 0);

    for(ulChan = 0; ulChan < 2; ulChan++)
    {
        unsigned long ulChCtl;


        if (ulChan ==1)
        {

            ROM_ADCSequenceStepConfigure(ADC0_BASE, SEQUENCER, ulChan, ADC_CTL_CH1|ADC_CTL_IE | ADC_CTL_END);
        }
        else if(ulChan==0)
        {
        	ulChCtl = ADC_CTL_CH0;
        	ROM_ADCSequenceStepConfigure(ADC0_BASE, SEQUENCER, ulChan, ADC_CTL_CH0);
        }


    }

    ADCHardwareOversampleConfigure(ADC0_BASE, 1);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    //SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);

    ROM_TimerConfigure(TIMER0_BASE,TIMER_CFG_32_BIT_PER );

    //100 micro
    unsigned long freq=SAMPLING_FREQUENCY;
    unsigned long period=(2*ROM_SysCtlClockGet()/(freq));

    ROM_TimerLoadSet(TIMER0_BASE, TIMER_A,period);
    ROM_TimerControlTrigger(TIMER0_BASE, TIMER_A, true);

    //ROM_TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
    //ROM_TimerLoadSet(TIMER1_BASE, TIMER_A, ROM_SysCtlClockGet() /2);




    CFAL96x64x16Init();
    GrContextInit(&sDisplayContext, &g_sCFAL96x64x16);
    sRect1.sXMin = 0;
    sRect1.sYMin = 0;
    sRect1.sXMax = GrContextDpyWidthGet(&sDisplayContext) - 1;
    sRect1.sYMax = 23;

    sRect2.sXMin = 0;
    sRect2.sYMin = 23;
    sRect2.sXMax = GrContextDpyWidthGet(&sDisplayContext) - 1;
    sRect2.sYMax = GrContextDpyHeightGet(&sDisplayContext) - 1;


	snprintf(text,sizeof(text),"STart");


    GrContextForegroundSet(&sDisplayContext, ClrDarkBlue);
    GrRectFill(&sDisplayContext, &sRect1);
    GrContextForegroundSet(&sDisplayContext, ClrWhite);
    //GrRectDraw(&sDisplayContext, &sRect1);
    GrContextFontSet(&sDisplayContext, g_pFontCm12);
    GrStringDrawCentered(&sDisplayContext,text, -1,
                         GrContextDpyWidthGet(&sDisplayContext) / 2, 10, 0);

    //GrContextForegroundSet(&sDisplayContext, ClrDarkBlue);
    //GrRectFill(&sDisplayContext, &sRect1);
    //GrContextForegroundSet(&sDisplayContext, ClrWhite);
    //GrRectDraw(&sDisplayContext, &sRect1);

    /*GrContextForegroundSet(&sDisplayContext, ClrDarkBlue);
    GrRectFill(&sDisplayContext, &sRect2);
    GrContextForegroundSet(&sDisplayContext, ClrWhite);
    GrRectDraw(&sDisplayContext, &sRect2);
*/
    compute_filter();
    maxi1=0;
    maxi2=0;
    max1=0;
    max2=0;
    res=0;
    res1=0;
    g_ulADCCount=0;
    buffer_index=0;
    ulLastADCCount=0;

}
//*****************************************************************************
//
// A simple application demonstrating use of the boot loader,
//
//*****************************************************************************
int
main(void)
{
    tRectangle sRect;
    tContext sContext;
    unsigned long ulSysClock;

    //
    // Enable lazy stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
    ROM_FPULazyStackingEnable();

    //
    // Set the system clock to run at 50MHz from the PLL
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
                       SYSCTL_OSC_MAIN);
    ulSysClock = ROM_SysCtlClockGet();

    //
    // Initialize the peripherals that each of the boot loader flavors
    // supports.  Since this example is intended for use with any of the
    // boot loaders and we don't know which is actually in use, we cover all
    // bases and initialize for serial, Ethernet and USB use here.
    //
    SetupForUART();
    SetupForUSB();

    //
    // Initialize the buttons driver.
    //
    ButtonsInit();

    //
    // Initialize the display driver.
    //
    CFAL96x64x16Init();

    //
    // Initialize the graphics context.
    //
    GrContextInit(&sContext, &g_sCFAL96x64x16);

    //
    // Fill the top part of the screen with blue to create the banner.
    //
    sRect.sXMin = 0;
    sRect.sYMin = 0;
    sRect.sXMax = GrContextDpyWidthGet(&sContext) - 1;
    sRect.sYMax = 9;
    GrContextForegroundSet(&sContext, ClrDarkBlue);
    GrRectFill(&sContext, &sRect);

    //
    // Change foreground for white text.
    //
    GrContextForegroundSet(&sContext, ClrWhite);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&sContext, g_pFontFixed6x8);
    GrStringDrawCentered(&sContext, "boot-demo2", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 4, 0);

    GrStringDrawCentered(&sContext, "Press select", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 20, false);
    GrStringDrawCentered(&sContext, "button to", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 30, false);
    GrStringDrawCentered(&sContext, "update.", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 40, false);
   
    //
    // Wait for select button to be pressed.
    // 
    while ((ButtonsPoll(0, 0) & SELECT_BUTTON) == 0) 
    {
        ROM_SysCtlDelay(ulSysClock / 1000);
    }

    GrStringDrawCentered(&sContext, "             ", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 20, true);
    GrStringDrawCentered(&sContext, " Updating... ", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 30, true);
    GrStringDrawCentered(&sContext, "             ", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 40, true);

    //
    // Transfer control to the boot loader.
    //
    JumpToBootLoader();

    //
    // The previous function never returns but we need to stick in a return
    // code here to keep the compiler from generating a warning.
    //
    return(0);
}
Esempio n. 7
0
//*****************************************************************************
//
// This function updates the status area of the screen.  It uses the current
// state of the application to print the status bar.
//
//*****************************************************************************
void
UpdateStatus(char *pcString, uint32_t ui32Buttons, bool bClrBackground)
{
    tRectangle sRect;

    //
    // Fill the bottom rows of the screen with blue to create the status area.
    //
    sRect.i16XMin = 0;
    sRect.i16YMin = GrContextDpyHeightGet(&g_sContext) -
                  DISPLAY_BANNER_HEIGHT - 1;
    sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.i16YMax = sRect.i16YMin + DISPLAY_BANNER_HEIGHT;

    //
    // Were we asked to clear the background of the status area?
    //
    GrContextBackgroundSet(&g_sContext, DISPLAY_BANNER_BG);

    if(bClrBackground)
    {
        //
        // Draw the background of the banner.
        //
        GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_BG);
        GrRectFill(&g_sContext, &sRect);

        //
        // Put a white box around the banner.
        //
        GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_FG);
        GrRectDraw(&g_sContext, &sRect);
    }

    //
    // Write the current state to the left of the status area.
    //
    GrContextFontSet(&g_sContext, g_psFontFixed6x8);

    //
    // Update the status on the screen.
    //
    if(pcString != 0)
    {
        UARTprintf(pcString);
        UARTprintf("\n");
        GrStringDraw(&g_sContext, pcString, -1, 10, sRect.i16YMin + 4, 1);

        g_ui32Buttons = ui32Buttons;
    }
    else if(iUSBState == eStateNoDevice)
    {
        //
        // Mouse is currently disconnected.
        //
        UARTprintf("no device\n");
        GrStringDraw(&g_sContext, "no device     ", -1, 10, sRect.i16YMin + 4,
                     1);
    }
    else if(iUSBState == eStateMouseConnected)
    {
        //
        // Mouse is connected.
        //
        UARTprintf("connected\n");
        GrStringDraw(&g_sContext, "connected     ", -1, 10, sRect.i16YMin + 4,
                     1);
    }
    else if(iUSBState == eStateUnknownDevice)
    {
        //
        // Some other (unknown) device is connected.
        //
        UARTprintf("unknown device\n");
        GrStringDraw(&g_sContext, "unknown device", -1, 10, sRect.i16YMin + 4,
                     1);
    }
    else if(iUSBState == eStatePowerFault)
    {
        //
        // Power fault.
        //
        UARTprintf("power fault\n");
        GrStringDraw(&g_sContext, "power fault   ", -1, 10, sRect.i16YMin + 4,
                     1);
    }

    UpdateButtons();
}
Esempio n. 8
0
//*****************************************************************************
//
// Demonstrate the use of the USB stick update example.
//
//*****************************************************************************
int
main(void)
{
    uint_fast32_t ui32Count;
    tContext sContext;
    tRectangle sRect;

    //
    // Enable lazy stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
    ROM_FPULazyStackingEnable();

    //
    // Set the system clock to run at 50MHz from the PLL.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Initialize the display driver.
    //
    CFAL96x64x16Init();

    //
    // Initialize the graphics context.
    //
    GrContextInit(&sContext, &g_sCFAL96x64x16);

    //
    // Fill the top 24 rows of the screen with blue to create the banner.
    //
    sRect.i16XMin = 0;
    sRect.i16YMin = 0;
    sRect.i16XMax = GrContextDpyWidthGet(&sContext) - 1;
    sRect.i16YMax = 9;
    GrContextForegroundSet(&sContext, ClrDarkBlue);
    GrRectFill(&sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&sContext, ClrWhite);
    GrRectDraw(&sContext, &sRect);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&sContext, g_psFontFixed6x8);
    GrStringDrawCentered(&sContext, "usb-stick-demo", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 4, 0);

    //
    // Indicate what is happening.
    //
    GrStringDrawCentered(&sContext, "Press the", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 20, 0);
    GrStringDrawCentered(&sContext, "select button to", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 30, 0);
    GrStringDrawCentered(&sContext, "start the USB", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 40, 0);
    GrStringDrawCentered(&sContext, "stick updater.", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 50, 0);

    //
    // Flush any cached drawing operations.
    //
    GrFlush(&sContext);

    //
    // Enable the GPIO module which the select button is attached to.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOM);

    //
    // Enable the GPIO pin to read the user button.
    //
    ROM_GPIODirModeSet(GPIO_PORTM_BASE, GPIO_PIN_4, GPIO_DIR_MODE_IN);
    MAP_GPIOPadConfigSet(GPIO_PORTM_BASE, GPIO_PIN_4, GPIO_STRENGTH_2MA,
                         GPIO_PIN_TYPE_STD_WPU);

    //
    // Wait for the pullup to take effect or the next loop will exist too soon.
    //
    SysCtlDelay(1000);

    //
    // Wait until the select button has been pressed for ~40ms (in order to
    // debounce the press).
    //
    ui32Count = 0;
    while(1) {
        //
        // See if the button is pressed.
        //
        if(ROM_GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_4) == 0) {
            //
            // Increment the count since the button is pressed.
            //
            ui32Count++;

            //
            // If the count has reached 4, then the button has been debounced
            // as being pressed.
            //
            if(ui32Count == 4) {
                break;
            }
        } else {
            //
            // Reset the count since the button is not pressed.
            //
            ui32Count = 0;
        }

        //
        // Delay for approximately 10ms.
        //
        SysCtlDelay(16000000 / (3 * 100));
    }

    //
    // Wait until the select button has been released for ~40ms (in order to
    // debounce the release).
    //
    ui32Count = 0;
    while(1) {
        //
        // See if the button is pressed.
        //
        if(ROM_GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_4) != 0) {
            //
            // Increment the count since the button is released.
            //
            ui32Count++;

            //
            // If the count has reached 4, then the button has been debounced
            // as being released.
            //
            if(ui32Count == 4) {
                break;
            }
        } else {
            //
            // Reset the count since the button is pressed.
            //
            ui32Count = 0;
        }

        //
        // Delay for approximately 10ms.
        //
        SysCtlDelay(16000000 / (3 * 100));
    }

    //
    // Indicate that the updater is being called.
    //
    GrStringDrawCentered(&sContext, "The USB stick", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 20, true);
    GrStringDrawCentered(&sContext, "updater is now", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 30, true);
    GrStringDrawCentered(&sContext, "waiting for a", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 40, true);
    GrStringDrawCentered(&sContext, "USB stick.", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 50, true);

    //
    // Flush any cached drawing operations.
    //
    GrFlush(&sContext);

    //
    // Call the updater so that it will search for an update on a memory stick.
    //
    (*((void (*)(void))(*(uint32_t *)0x2c)))();

    //
    // The updater should take control, so this should never be reached.
    // Just in case, loop forever.
    //
    while(1) {
    }
}
Esempio n. 9
0
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
    FRESULT FileResult;
    tRectangle sRect;

    //
    // Initially wait for device connection.
    //
    g_eState = STATE_NO_DEVICE;

    //
    // Set the clocking to run directly from the crystal.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_8MHZ);

    //
    // Enable Clocking to the USB controller.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);

    //
    // Enable the peripherals used by this example.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    //
    // Set the USB pins to be controlled by the USB controller.
    //
    GPIOPinTypeUSBDigital(GPIO_PORTH_BASE, GPIO_PIN_3 | GPIO_PIN_4);

    //
    // The LM3S3748 board uses a USB mux that must be switched to use the
    // host connecter and not the device connecter.
    //
    GPIOPinTypeGPIOOutput(USB_MUX_GPIO_BASE, USB_MUX_GPIO_PIN);
    GPIOPinWrite(USB_MUX_GPIO_BASE, USB_MUX_GPIO_PIN, USB_MUX_SEL_HOST);

    //
    // Turn on USB Phy clock.
    //
    SysCtlUSBPLLEnable();

    //
    // Set the system tick to fire 100 times per second.
    //
    SysTickPeriodSet(SysCtlClockGet()/100);
    SysTickIntEnable();
    SysTickEnable();

    //
    // Enable the uDMA controller and set up the control table base.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);
    uDMAEnable();
    uDMAControlBaseSet(g_sDMAControlTable);

    //
    // Initialize the display driver.
    //
    Formike128x128x16Init();

    //
    // Turn on the backlight.
    //
    Formike128x128x16BacklightOn();

    //
    // Initialize the graphics context.
    //
    GrContextInit(&g_sContext, &g_sFormike128x128x16);

    //
    // Fill the top 15 rows of the screen with blue to create the banner.
    //
    sRect.sXMin = 0;
    sRect.sYMin = 0;
    sRect.sXMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.sYMax = SPLASH_HEIGHT - 1;
    GrContextForegroundSet(&g_sContext, ClrDarkBlue);
    GrRectFill(&g_sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&g_sContext, ClrWhite);
    GrRectDraw(&g_sContext, &sRect);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&g_sContext, &g_sFontFixed6x8);

    GrStringDrawCentered(&g_sContext, "usb_host_msc", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 7, 0);

    //
    // Set the color to white and select the 20 point font.
    //
    GrContextForegroundSet(&g_sContext, FILE_COLOR);

    GrStringDraw(&g_sContext, "No Device",
        100, 0, TOP_HEIGHT, 1);

    //
    // Register the host class drivers.
    //
    USBHCDRegisterDrivers(0, g_ppHostClassDrivers, g_ulNumHostClassDrivers);

    //
    // Open an instance of the mass storage class driver.
    //
    g_ulMSCInstance = USBHMSCDriveOpen(0, MSCCallback);

    //
    // Initialize the power configuration. This sets the power enable signal
    // to be active high and does not enable the power fault.
    //
    USBHCDPowerConfigInit(0, USB_HOST_PWREN_HIGH);

    //
    // Initialize the host controller.
    //
    USBHCDInit(0, g_pHCDPool, HCD_MEMORY_SIZE);

    //
    // Initialize the pushbuttons.
    //
    ButtonsInit();

    //
    // Set the auto repeat rates for the up and down buttons.
    //
    ButtonsSetAutoRepeat(UP_BUTTON, 50, 15);
    ButtonsSetAutoRepeat(DOWN_BUTTON, 50, 15);

    //
    // Current directory is "/"
    //
    g_DirData.szPWD[0] = '/';
    g_DirData.szPWD[1] = 0;

    //
    // Initialize the file system.
    //
    FileInit();

    while(1)
    {
        USBHCDMain();

        switch(g_eState)
        {
            case STATE_DEVICE_ENUM:
            {
                //
                // Reset the root directory.
                //
                g_DirData.szPWD[0] = '/';
                g_DirData.szPWD[1] = 0;

                //
                // Open the root directory.
                //
                FileResult = f_opendir(&g_DirData.DirState, g_DirData.szPWD);

                //
                // Wait for the root directory to open successfully.  The MSC
                // device can enumerate before being read to be accessed, so
                // there may be some delay before it is ready.
                //
                if(FileResult == FR_OK)
                {
                    //
                    // Reset the directory state.
                    //
                    g_DirData.ulIndex = 0;
                    g_DirData.ulSelectIndex = 0;
                    g_DirData.ulValidValues = 0;
                    g_eState = STATE_DEVICE_READY;

                    //
                    // Ignore buttons pressed before being ready.
                    //
                    g_ulButtons = 0;

                    //
                    // Update the screen if the root dir opened successfully.
                    //
                    DirUpdate();
                    UpdateWindow();
                }
                else if(FileResult != FR_NOT_READY)
                {
                    // some kind of error
                }

                //
                // Set the Device Present flag.
                //
                g_ulFlags = FLAGS_DEVICE_PRESENT;

                break;
            }

            //
            // This is the running state where buttons are checked and the
            // screen is updated.
            //
            case STATE_DEVICE_READY:
            {
                //
                // Down button pressed and released.
                //
                if(g_ulButtons & BUTTON_DOWN_CLICK)
                {
                    //
                    // Update the screen and directory state.
                    //
                    MoveDown();

                    //
                    // Clear the button pressed event.
                    //
                    g_ulButtons &= ~BUTTON_DOWN_CLICK;
                }

                //
                // Up button pressed and released.
                //
                if(g_ulButtons & BUTTON_UP_CLICK)
                {
                    //
                    // Update the screen and directory state.
                    //
                    MoveUp();

                    //
                    // Clear the button pressed event.
                    //
                    g_ulButtons &= ~BUTTON_UP_CLICK;
                }

                //
                // Select button pressed and released.
                //
                if(g_ulButtons & BUTTON_SELECT_CLICK)
                {
                    //
                    // If this was a directory go into it.
                    //
                    SelectDir();

                    //
                    // Clear the button pressed event.
                    //
                    g_ulButtons &= ~BUTTON_SELECT_CLICK;
                }
                break;
            }

            //
            // If there is no device then just wait for one.
            //
            case STATE_NO_DEVICE:
            {
                if(g_ulFlags == FLAGS_DEVICE_PRESENT)
                {
                    //
                    // Clear the screen and indicate that there is no longer
                    // a device present.
                    //
                    ClearTextBox();
                    GrStringDraw(&g_sContext, "No Device",
                         100, 0, TOP_HEIGHT, 1);

                    //
                    // Clear the Device Present flag.
                    //
                    g_ulFlags &= ~FLAGS_DEVICE_PRESENT;
                }
                break;
            }

            //
            // An unknown device was connected.
            //
            case STATE_UNKNOWN_DEVICE:
            {
                //
                // If this is a new device then change the status.
                //
                if((g_ulFlags & FLAGS_DEVICE_PRESENT) == 0)
                {
                    //
                    // Clear the screen and indicate that an unknown device is
                    // present.
                    //
                    ClearTextBox();
                    GrStringDraw(&g_sContext, "Unknown Device",
                         100, 0, TOP_HEIGHT, 1);
                }

                //
                // Set the Device Present flag.
                //
                g_ulFlags = FLAGS_DEVICE_PRESENT;

                break;
            }

            //
            // Something has caused a power fault.
            //
            case STATE_POWER_FAULT:
            {
                break;
            }

            default:
            {
                break;
            }
        }
    }
}
Esempio n. 10
0
//*****************************************************************************
//
// This example demonstrates how to send a string of data to the UART.
//
//*****************************************************************************
int
main(void)
{
    tRectangle sRect;
    tContext sContext;

    //
    // Enable lazy stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
    ROM_FPULazyStackingEnable();

    //
    // Set the clocking to run directly from the crystal.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Initialize the display driver.
    //
    CFAL96x64x16Init();

    //
    // Initialize the graphics context.
    //
    GrContextInit(&sContext, &g_sCFAL96x64x16);

    //
    // Fill the top part of the screen with blue to create the banner.
    //
    sRect.i16XMin = 0;
    sRect.i16YMin = 0;
    sRect.i16XMax = GrContextDpyWidthGet(&sContext) - 1;
    sRect.i16YMax = 9;
    GrContextForegroundSet(&sContext, ClrDarkBlue);
    GrRectFill(&sContext, &sRect);

    //
    // Change foreground for white text.
    //
    GrContextForegroundSet(&sContext, ClrWhite);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&sContext, g_psFontFixed6x8);
    GrStringDrawCentered(&sContext, "uart-echo", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 4, 0);

    //
    // Initialize the display and write some instructions.
    //
    GrStringDrawCentered(&sContext, "Connect a", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 20, false);
    GrStringDrawCentered(&sContext, "terminal", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 30, false);
    GrStringDrawCentered(&sContext, "to UART0.", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 40, false);
    GrStringDrawCentered(&sContext, "115000,N,8,1", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 50, false);

    //
    // Enable the peripherals used by this example.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Enable processor interrupts.
    //
    ROM_IntMasterEnable();

    //
    // Set GPIO A0 and A1 as UART pins.
    //
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Configure the UART for 115,200, 8-N-1 operation.
    //
    ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200,
                            (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                             UART_CONFIG_PAR_NONE));

    //
    // Enable the UART interrupt.
    //
    ROM_IntEnable(INT_UART0);
    ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);

    //
    // Prompt for text to be entered.
    //
    UARTSend((uint8_t *)"Enter text: ", 12);

    //
    // Loop forever echoing data through the UART.
    //
    while(1)
    {
    }
}
Esempio n. 11
0
//*****************************************************************************
//
// This example demonstrates the use of the watchdog timer.
//
//*****************************************************************************
int
main(void)
{
    tRectangle sRect;

    //
    // Set the clocking to run directly from the crystal.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Set the device pinout appropriately for this board.
    //
    PinoutSet();

    //
    // Initialize the touch screen driver.
    //
    TouchScreenInit();
    TouchScreenCallbackSet(WatchdogTouchCallback);

    //
    // Initialize the display driver.
    //
    Kitronix320x240x16_SSD2119Init();

    //
    // Initialize the graphics context and find the middle X coordinate.
    //
    GrContextInit(&g_sContext, &g_sKitronix320x240x16_SSD2119);

    //
    // Fill the top 15 rows of the screen with blue to create the banner.
    //
    sRect.sXMin = 0;
    sRect.sYMin = 0;
    sRect.sXMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.sYMax = 23;
    GrContextForegroundSet(&g_sContext, ClrDarkBlue);
    GrRectFill(&g_sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&g_sContext, ClrWhite);
    GrRectDraw(&g_sContext, &sRect);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&g_sContext, g_pFontCm20);
    GrStringDrawCentered(&g_sContext, "watchdog", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 8, 0);

    //
    // Show the state and offer some instructions to the user.
    //
    GrContextFontSet(&g_sContext, g_pFontCmss20);
    GrStringDrawCentered(&g_sContext, "Watchdog is being fed.", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2 ,
                         (GrContextDpyHeightGet(&g_sContext) / 2), 1);
    GrContextFontSet(&g_sContext, g_pFontCmss14);
    GrStringDrawCentered(&g_sContext, "Tap the screen to starve the watchdog",
                         -1, GrContextDpyWidthGet(&g_sContext) / 2 ,
                         (GrContextDpyHeightGet(&g_sContext) / 2) + 20, 1);

    //
    // Enable the peripherals used by this example.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_WDOG0);

    //
    // Enable processor interrupts.
    //
    ROM_IntMasterEnable();

    //
    // Set GPIO PF3 as an output.  This drives an LED on the board that will
    // toggle when a watchdog interrupt is processed.
    //
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3);
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);

    //
    // Enable the watchdog interrupt.
    //
    ROM_IntEnable(INT_WATCHDOG);

    //
    // Set the period of the watchdog timer.
    //
    ROM_WatchdogReloadSet(WATCHDOG0_BASE, ROM_SysCtlClockGet());

    //
    // Enable reset generation from the watchdog timer.
    //
    ROM_WatchdogResetEnable(WATCHDOG0_BASE);

    //
    // Enable the watchdog timer.
    //
    ROM_WatchdogEnable(WATCHDOG0_BASE);

    //
    // Loop forever while the LED winks as watchdog interrupts are handled.
    //
    while(1)
    {
    }
}
//*****************************************************************************
//
// This example decrypts blocks ciphertext using AES128 and AES256 in GCM
// mode.  It does the decryption first without uDMA and then with uDMA.
// The results are checked after each operation.
//
//*****************************************************************************
int
main(void)
{
    uint32_t pui32PlainText[64], pui32Tag[4], pui32Y0[4], ui32Errors, ui32Idx;
    uint32_t *pui32Key, ui32IVLength, *pui32IV, ui32DataLength;
    uint32_t *pui32ExpPlainText, ui32AuthDataLength, *pui32AuthData;
    uint32_t *pui32CipherText, *pui32ExpTag;
    uint32_t ui32KeySize;
    uint32_t ui32SysClock;
    uint8_t ui8Vector;
    tContext sContext;
    
    //
    // Run from the PLL at 120 MHz.
    //
    ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                           SYSCTL_OSC_MAIN |
                                           SYSCTL_USE_PLL |
                                           SYSCTL_CFG_VCO_480), 120000000);

    //
    // Configure the device pins.
    //
    PinoutSet();

    //
    // Initialize the display driver.
    //
    Kentec320x240x16_SSD2119Init(ui32SysClock);

    //
    // Initialize the graphics context.
    //
    GrContextInit(&sContext, &g_sKentec320x240x16_SSD2119);

    //
    // Draw the application frame.
    //
    FrameDraw(&sContext, "aes-gcm-decrypt");
    
    //
    // Show some instructions on the display
    //
    GrContextFontSet(&sContext, g_psFontCm20);
    GrContextForegroundSet(&sContext, ClrWhite);
    GrStringDrawCentered(&sContext, "Connect a terminal to", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 60, false);
    GrStringDrawCentered(&sContext, "UART0 (115200,N,8,1)", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 80, false);
    GrStringDrawCentered(&sContext, "for more information.", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 100, false);

    //
    // Initialize local variables.
    //
    ui32Errors = 0;
    for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
    {
        pui32PlainText[ui32Idx] = 0;
    }
    for(ui32Idx = 0; ui32Idx < 4; ui32Idx++)
    {
        pui32Tag[ui32Idx] = 0;
    }

    //
    // Enable stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
    ROM_FPUStackingEnable();

    //
    // Enable AES interrupts.
    //
    ROM_IntEnable(INT_AES0);

    //
    // Enable debug output on UART0 and print a welcome message.
    //
    ConfigureUART();
    UARTprintf("Starting AES GCM decryption demo.\n");
    GrStringDrawCentered(&sContext, "Starting demo...", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 140, false);

    //
    // Enable the uDMA module.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);

    //
    // Setup the control table.
    //
    ROM_uDMAEnable();
    ROM_uDMAControlBaseSet(g_psDMAControlTable);

    //
    // Initialize the CCM and AES modules.
    //
    if(!AESInit())
    {
        UARTprintf("Initialization of the AES module failed.\n");
        ui32Errors |= 0x00000001;
    }

    //
    // Loop through all the given vectors.
    //
    for(ui8Vector = 0;
        (ui8Vector <
         (sizeof(g_psAESGCMTestVectors) / sizeof(g_psAESGCMTestVectors[0]))) &&
        (ui32Errors == 0);
        ui8Vector++)
    {
        UARTprintf("Starting vector #%d\n", ui8Vector);

        //
        // Get the current vector's data members.
        //
        ui32KeySize = g_psAESGCMTestVectors[ui8Vector].ui32KeySize;
        pui32Key = g_psAESGCMTestVectors[ui8Vector].pui32Key;
        ui32IVLength = g_psAESGCMTestVectors[ui8Vector].ui32IVLength;
        pui32IV = g_psAESGCMTestVectors[ui8Vector].pui32IV;
        ui32DataLength = g_psAESGCMTestVectors[ui8Vector].ui32DataLength;
        pui32ExpPlainText = g_psAESGCMTestVectors[ui8Vector].pui32PlainText;
        ui32AuthDataLength =
            g_psAESGCMTestVectors[ui8Vector].ui32AuthDataLength;
        pui32AuthData = g_psAESGCMTestVectors[ui8Vector].pui32AuthData;
        pui32CipherText = g_psAESGCMTestVectors[ui8Vector].pui32CipherText;
        pui32ExpTag = g_psAESGCMTestVectors[ui8Vector].pui32Tag;

        //
        // If both the data lengths are zero, then it's a special case.
        //
        if((ui32DataLength == 0) && (ui32AuthDataLength == 0))
        {
            UARTprintf("Performing decryption without uDMA.\n");

            //
            // Figure out the value of Y0 depending on the IV length.
            //
            AESGCMY0Get(ui32KeySize, pui32IV, ui32IVLength, pui32Key, pui32Y0);

            //
            // Perform the basic encryption.
            //
            AESECBEncrypt(ui32KeySize, pui32Y0, pui32Tag, pui32Key, 16);
        }
        else
        {
            //
            // Figure out the value of Y0 depending on the IV length.
            //
            AESGCMY0Get(ui32KeySize, pui32IV, ui32IVLength, pui32Key, pui32Y0);

            //
            // Perform the decryption without uDMA.
            //
            UARTprintf("Performing decryption without uDMA.\n");
            AESGCMDecrypt(ui32KeySize, pui32CipherText, pui32PlainText,
                          ui32DataLength, pui32Key, pui32Y0, pui32AuthData,
                          ui32AuthDataLength, pui32Tag, false);
        }

        //
        // Check the results.
        //
        for(ui32Idx = 0; ui32Idx < (ui32DataLength / 4); ui32Idx++)
        {
            if(pui32ExpPlainText[ui32Idx] != pui32PlainText[ui32Idx])
            {
                UARTprintf("Plaintext mismatch on word %d. Exp: 0x%x, Act: "
                           "0x%x\n", ui32Idx, pui32ExpPlainText[ui32Idx],
                           pui32PlainText[ui32Idx]);
                ui32Errors |= (ui32Idx << 16) | 0x00000002;
            }
        }
        for(ui32Idx = 0; ui32Idx < 4; ui32Idx++)
        {
            if(pui32ExpTag[ui32Idx] != pui32Tag[ui32Idx])
            {
                UARTprintf("Tag mismatch on word %d. Exp: 0x%x, Act: 0x%x\n",
                           ui32Idx, pui32ExpTag[ui32Idx], pui32Tag[ui32Idx]);
                ui32Errors |= (ui32Idx << 16) | 0x00000003;
            }
        }

        //
        // Clear the arrays containing the ciphertext and tag to ensure things
        // are working correctly.
        //
        for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
        {
            pui32PlainText[ui32Idx] = 0;
        }
        for(ui32Idx = 0; ui32Idx < 4; ui32Idx++)
        {
            pui32Tag[ui32Idx] = 0;
        }

        //
        // Only use DMA with the vectors that have data.
        //
        if((ui32DataLength != 0) || (ui32AuthDataLength != 0))
        {
            //
            // Perform the decryption with uDMA.
            //
            UARTprintf("Performing decryption with uDMA.\n");
            AESGCMDecrypt(ui32KeySize, pui32CipherText, pui32PlainText,
                          ui32DataLength, pui32Key, pui32Y0, pui32AuthData,
                          ui32AuthDataLength, pui32Tag, true);

            //
            // Check the result.
            //
            for(ui32Idx = 0; ui32Idx < (ui32DataLength / 4); ui32Idx++)
            {
                if(pui32ExpPlainText[ui32Idx] != pui32PlainText[ui32Idx])
                {
                    UARTprintf("Plaintext mismatch on word %d. Exp: 0x%x, "
                               "Act: 0x%x\n", ui32Idx,
                               pui32ExpPlainText[ui32Idx],
                               pui32PlainText[ui32Idx]);
                    ui32Errors |= (ui32Idx << 16) | 0x00000002;
                }
            }
            for(ui32Idx = 0; ui32Idx < 4; ui32Idx++)
            {
                if(pui32ExpTag[ui32Idx] != pui32Tag[ui32Idx])
                {
                    UARTprintf("Tag mismatch on word %d. Exp: 0x%x, Act: "
                               "0x%x\n", ui32Idx, pui32ExpTag[ui32Idx],
                               pui32Tag[ui32Idx]);
                    ui32Errors |= (ui32Idx << 16) | 0x00000003;
                }
            }
        }
    }

    //
    // Finished.
    //
    if(ui32Errors)
    {
        UARTprintf("Demo failed with error code 0x%x.\n", ui32Errors);
        GrStringDrawCentered(&sContext, "Demo failed.", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 180, false);
    }
    else
    {
        UARTprintf("Demo completed successfully.\n");
        GrStringDrawCentered(&sContext, "Demo passed.", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 180, false);
    }

    //
    // Wait forever.
    //
    while(1)
    {
    }
}
Esempio n. 13
0
/*****************************************************************************
* 
*  This is the main loop that runs the application.
* 
*****************************************************************************/
int main(void)
{
    tRectangle sRect;

    MMUConfigAndEnable();
    
	/* Enable USB module clock */
	
	USB0ModuleClkConfig();

	/* configures arm interrupt controller to generate raster interrupt  */
	
	USBInterruptEnable();

	/* LCD Back light setup */
	
	LCDBackLightEnable();

	/* UPD Pin setup */
	
	UPDNPinControl();

	/* Delay timer setup */
	
	DelayTimerSetup();

	/* Configures raster to display image  */
	
	SetUpLCD();		
	/* Configures raster to display image  and Copy palette info into buffer */	
	LCDInit();

	GrOffScreen24BPPInit(&g_s35_800x480x24Display, g_pucBuffer, LCD_WIDTH, LCD_HEIGHT);
	
	/* Initialize a drawing context. */
	GrContextInit(&g_sContext, &g_s35_800x480x24Display);

	/* enable End of frame interrupt */
	RasterEndOfFrameIntEnable(SOC_LCDC_0_REGS);

	/* enable raster */
	RasterEnable(SOC_LCDC_0_REGS);	
		
	/* Fill the top 24 rows of the screen with blue to create the banner. */

	sRect.sXMin = 0;
	sRect.sYMin = 0;
	sRect.sXMax = GrContextDpyWidthGet(&g_sContext) - 1;
	sRect.sYMax = (MAX_ROW_NUM - 1);
	GrContextForegroundSet(&g_sContext, ClrDarkBlue);
	GrRectFill(&g_sContext, &sRect);


	/* Put a white box around the banner. */
	GrContextForegroundSet(&g_sContext, ClrWhite);
	GrRectDraw(&g_sContext, &sRect);

	/* Put the application name in the middle of the banner. */
	GrContextFontSet(&g_sContext, &g_sFontCm20);
	GrStringDrawCentered(&g_sContext, "usb-dev-composite", -1,
						 GrContextDpyWidthGet(&g_sContext) / 2, 10, 0);

		
     /* Show the various static text elements on the color STN display. */
     
     GrContextFontSet(&g_sContext, TEXT_FONT);
 	 GrContextForegroundSet(&g_sContext, ClrViolet);
     GrStringDraw(&g_sContext, "CDC Serial 1 :-", -1, CDC1_STR_X_POSITION,
                   CDC1_STR_Y_POSITION, false);
     GrStringDraw(&g_sContext, "CDC Serial 2 :-", -1, CDC2_STR_X_POSITION,
                   CDC1_STR_Y_POSITION, false);
 	 GrContextForegroundSet(&g_sContext, ClrWhite);

 	 GrStringDraw(&g_sContext, "Tx bytes:", -1, CDC1_STR_X_POSITION,
 	              (CDC1_STR_Y_POSITION + CDC_STR_Y_DIFF), false);
     GrStringDraw(&g_sContext, "Tx buffer:", -1, CDC1_STR_X_POSITION,
                  (CDC1_STR_Y_POSITION + (CDC_STR_Y_DIFF * 2)), false);
     GrStringDraw(&g_sContext, "Rx bytes:", -1, CDC1_STR_X_POSITION, 
                  (CDC1_STR_Y_POSITION + (CDC_STR_Y_DIFF * 4)), false);
     GrStringDraw(&g_sContext, "Rx buffer:", -1, CDC1_STR_X_POSITION, 
                  (CDC1_STR_Y_POSITION + (CDC_STR_Y_DIFF * 5)), false);
     DrawBufferMeter(&g_sContext, CDC1_BUF_METER_X_POS, CDC1_BUF_METER_Y_POS);
     DrawBufferMeter(&g_sContext, CDC1_BUF_METER_X_POS,
                     (CDC1_BUF_METER_Y_POS + CDC_BUF_METER_Y_DIFF));

 	 GrStringDraw(&g_sContext, "Tx bytes:", -1, CDC2_STR_X_POSITION, 
 	              (CDC2_STR_Y_POSITION + CDC_STR_Y_DIFF), false);
     GrStringDraw(&g_sContext, "Tx buffer:", -1, CDC2_STR_X_POSITION,
                  (CDC2_STR_Y_POSITION + (CDC_STR_Y_DIFF * 2)), false);
     GrStringDraw(&g_sContext, "Rx bytes:", -1, CDC2_STR_X_POSITION, 
                  (CDC2_STR_Y_POSITION + (CDC_STR_Y_DIFF * 4)), false);
     GrStringDraw(&g_sContext, "Rx buffer:", -1, CDC2_STR_X_POSITION, 
                  (CDC2_STR_Y_POSITION + (CDC_STR_Y_DIFF * 5)), false);
     DrawBufferMeter(&g_sContext, CDC2_BUF_METER_X_POS, CDC2_BUF_METER_Y_POS);
     DrawBufferMeter(&g_sContext, CDC2_BUF_METER_X_POS, 
                     (CDC2_BUF_METER_Y_POS + CDC_BUF_METER_Y_DIFF));

     DisplayStatus(&g_sContext, " Waiting for host... ");

    /* Pass the USB library our device information, initialize the USB
       controller and connect the device to the bus.
    */ 

    g_psCompDevices[0].pvInstance =
        USBDCDCCompositeInit(0, (tUSBDCDCDevice *)&g_sCDCDevice1);
    g_psCompDevices[1].pvInstance =
        USBDCDCCompositeInit(0, (tUSBDCDCDevice *)&g_sCDCDevice2);        

    /*
      Pass the device information to the USB library and place the device
      on the bus.
    */
    USBDCompositeInit(0, &g_sCompDevice, DESCRIPTOR_DATA_SIZE,
                      g_pucDescriptorData);

    
    /* Initialize the serial devices. */
    SerialInit();

    
    /* Drop into the main loop. */
    while(1)
    {
        
        /* Allow the main serial routine to run. */
        SerialMain();
    }
}
Esempio n. 14
0
//*****************************************************************************
//
// This example encrypts a block of payload using AES128 in CCM mode.  It
// does the encryption first without uDMA and then with uDMA.  The results
// are checked after each operation.
//
//*****************************************************************************
int
main(void)
{
    uint32_t pui32CipherText[16], pui32Tag[4], ui32Errors, ui32Idx;
    uint32_t ui32PayloadLength, ui32TagLength;
    uint32_t ui32NonceLength, ui32AuthDataLength;
    uint32_t *pui32Nonce, *pui32AuthData, ui32SysClock;
    uint32_t *pui32Key, *pui32Payload, *pui32ExpCipherText;
    uint32_t ui32Keysize;
    uint8_t ui8Vector;
    uint8_t *pui8ExpTag, *pui8Tag;
    tContext sContext;
    
    //
    // Run from the PLL at 120 MHz.
    //
    ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                           SYSCTL_OSC_MAIN |
                                           SYSCTL_USE_PLL |
                                           SYSCTL_CFG_VCO_480), 120000000);

    //
    // Configure the device pins.
    //
    PinoutSet();

    //
    // Initialize the display driver.
    //
    Kentec320x240x16_SSD2119Init(ui32SysClock);

    //
    // Initialize the graphics context.
    //
    GrContextInit(&sContext, &g_sKentec320x240x16_SSD2119);

    //
    // Draw the application frame.
    //
    FrameDraw(&sContext, "aes-ccm-encrypt");
    
    //
    // Show some instructions on the display
    //
    GrContextFontSet(&sContext, g_psFontCm20);
    GrStringDrawCentered(&sContext, "Connect a terminal to", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 60, false);
    GrStringDrawCentered(&sContext, "UART0 (115200,N,8,1)", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 80, false);
    GrStringDrawCentered(&sContext, "for more information.", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 100, false);

    //
    // Initialize local variables.
    //
    ui32Errors = 0;
    for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
    {
        pui32CipherText[ui32Idx] = 0;
    }
    for(ui32Idx = 0; ui32Idx < 4; ui32Idx++)
    {
        pui32Tag[ui32Idx] = 0;
    }
    pui8Tag = (uint8_t *)pui32Tag;

    //
    // Enable stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
    ROM_FPUStackingEnable();

    //
    // Configure the system clock to run off the internal 16MHz oscillator.
    //
    MAP_SysCtlClockFreqSet(SYSCTL_OSC_INT | SYSCTL_USE_OSC, 16000000);

    //
    // Enable AES interrupts.
    //
    ROM_IntEnable(INT_AES0);

    //
    // Enable debug output on UART0 and print a welcome message.
    //
    ConfigureUART();
    UARTprintf("Starting AES CCM encryption demo.\n");
    GrStringDrawCentered(&sContext, "Starting demo...", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 140, false);

    //
    // Enable the uDMA module.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);

    //
    // Setup the control table.
    //
    ROM_uDMAEnable();
    ROM_uDMAControlBaseSet(g_psDMAControlTable);

    //
    // Initialize the CCM and AES modules.
    //
    if(!AESInit())
    {
        UARTprintf("Initialization of the AES module failed.\n");
        ui32Errors |= 0x00000001;
    }

    //
    // Loop through all the given vectors.
    //
    for(ui8Vector = 0;
        (ui8Vector <
         (sizeof(g_psAESCCMTestVectors) / sizeof(g_psAESCCMTestVectors[0]))) &&
        (ui32Errors == 0);
        ui8Vector++)
    {
        UARTprintf("Starting vector #%d\n", ui8Vector);

        //
        // Get the current vector's data members.
        //
        ui32Keysize = g_psAESCCMTestVectors[ui8Vector].ui32KeySize;
        pui32Key = g_psAESCCMTestVectors[ui8Vector].pui32Key;
        pui32Payload = g_psAESCCMTestVectors[ui8Vector].pui32Payload;
        ui32PayloadLength = 
            g_psAESCCMTestVectors[ui8Vector].ui32PayloadLength;
        pui32AuthData = g_psAESCCMTestVectors[ui8Vector].pui32AuthData;
        ui32AuthDataLength = 
            g_psAESCCMTestVectors[ui8Vector].ui32AuthDataLength;
        pui32ExpCipherText = 
            g_psAESCCMTestVectors[ui8Vector].pui32CipherText;
        pui8ExpTag = (uint8_t *)g_psAESCCMTestVectors[ui8Vector].pui32Tag;
        ui32TagLength = g_psAESCCMTestVectors[ui8Vector].ui32TagLength;
        pui32Nonce = g_psAESCCMTestVectors[ui8Vector].pui32Nonce;
        ui32NonceLength =
            g_psAESCCMTestVectors[ui8Vector].ui32NonceLength;

        //
        // Perform the encryption without uDMA.
        //
        UARTprintf("Performing encryption without uDMA.\n");
        AESCCMEncrypt(ui32Keysize, pui32Key, pui32Payload, pui32CipherText,
                      ui32PayloadLength, pui32Nonce, ui32NonceLength,
                      pui32AuthData, ui32AuthDataLength, pui32Tag,
                      ui32TagLength, false);
            
        //
        // Check the result.
        //
        for(ui32Idx = 0; ui32Idx < (ui32PayloadLength / 4); ui32Idx++)
        {
            if(pui32CipherText[ui32Idx] != pui32ExpCipherText[ui32Idx])
            {
                UARTprintf("Ciphertext mismatch on word %d. Exp: 0x%x, Act: "
                           "0x%x\n", ui32Idx, pui32ExpCipherText[ui32Idx],
                           pui32CipherText[ui32Idx]);
                ui32Errors |= (ui32Idx << 16) | 0x00000002;
            }
        }
        for(ui32Idx = 0; ui32Idx < ui32TagLength; ui32Idx++)
        {
            if(pui8Tag[ui32Idx] != pui8ExpTag[ui32Idx])
            {
                UARTprintf("Tag mismatch on byte %d. Exp: 0x%x, Act: "
                           "0x%x\n", ui32Idx, pui8ExpTag[ui32Idx],
                           pui8Tag[ui32Idx]);
                ui32Errors |= (ui32Idx << 16) | 0x00000004;
            }
        }

        //
        // Clear the array containing the ciphertext.
        //
        for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
        {
            pui32CipherText[ui32Idx] = 0;
        }
        for(ui32Idx = 0; ui32Idx < 4; ui32Idx++)
        {
            pui32Tag[ui32Idx] = 0;
        }

        //
        // Perform the encryption with uDMA.
        //
        UARTprintf("Performing encryption with uDMA.\n");
        AESCCMEncrypt(ui32Keysize, pui32Key, pui32Payload, pui32CipherText,
                      ui32PayloadLength, pui32Nonce, ui32NonceLength,
                      pui32AuthData, ui32AuthDataLength, pui32Tag,
                      ui32TagLength, true);
        
        //
        // Check the result.
        //
        for(ui32Idx = 0; ui32Idx < (ui32PayloadLength / 4); ui32Idx++)
        {
            if(pui32CipherText[ui32Idx] != pui32ExpCipherText[ui32Idx])
            {
                UARTprintf("Ciphertext mismatch on word %d. Exp: 0x%x, Act: "
                           "0x%x\n", ui32Idx, pui32ExpCipherText[ui32Idx],
                           pui32CipherText[ui32Idx]);
                ui32Errors |= (ui32Idx << 16) | 0x00000002;
            }
        }
        for(ui32Idx = 0; ui32Idx < ui32TagLength; ui32Idx++)
        {
            if(pui8Tag[ui32Idx] != pui8ExpTag[ui32Idx])
            {
                UARTprintf("Tag mismatch on byte %d. Exp: 0x%x, Act: "
                           "0x%x\n", ui32Idx, pui8ExpTag[ui32Idx],
                           pui8Tag[ui32Idx]);
                ui32Errors |= (ui32Idx << 16) | 0x00000004;
            }
        }
        
        //
        // Clear the array containing the ciphertext.
        //
        for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
        {
            pui32CipherText[ui32Idx] = 0;
        }
        for(ui32Idx = 0; ui32Idx < 4; ui32Idx++)
        {
            pui32Tag[ui32Idx] = 0;
        }
    }

    //
    // Finished.
    //
    if(ui32Errors)
    {
        UARTprintf("Demo failed with error code 0x%x.\n", ui32Errors);
        GrStringDrawCentered(&sContext, "Demo failed.", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 180, false);
    }
    else
    {
        UARTprintf("Demo completed successfully.\n");
        GrStringDrawCentered(&sContext, "Demo passed.", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 180, false);
    }

    while(1)
    {
    }
}
//*****************************************************************************
//
// This function updates the status area of the screen.  It uses the current
// state of the application to print the status bar.
//
//*****************************************************************************
void
UpdateStatus(char *pcString, bool bClrBackground)
{
    tRectangle sRect;

    //
    //
    //
    GrContextBackgroundSet(&g_sContext, DISPLAY_BANNER_BG);

    if(bClrBackground)
    {
        //
        // Fill the bottom rows of the screen with blue to create the status area.
        //
        sRect.i16XMin = 0;
        sRect.i16YMin = GrContextDpyHeightGet(&g_sContext) -
                      DISPLAY_BANNER_HEIGHT;
        sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
        sRect.i16YMax = sRect.i16YMin + DISPLAY_BANNER_HEIGHT - 1;

        //
        // Draw the background of the banner.
        //
        GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_BG);
        GrRectFill(&g_sContext, &sRect);

        //
        // Put a white box around the banner.
        //
        GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_FG);
        GrRectDraw(&g_sContext, &sRect);
    }
    else
    {
        //
        // Fill the bottom rows of the screen with blue to create the status area.
        //
        sRect.i16XMin = 1;
        sRect.i16YMin = GrContextDpyHeightGet(&g_sContext) -
                      DISPLAY_BANNER_HEIGHT + 1;
        sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 2;
        sRect.i16YMax = sRect.i16YMin + DISPLAY_BANNER_HEIGHT - 3;

        //
        // Draw the background of the banner.
        //
        GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_BG);
        GrRectFill(&g_sContext, &sRect);

        //
        // White text in the banner.
        //
        GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_FG);
    }

    //
    // Write the current state to the left of the status area.
    //
    GrContextFontSet(&g_sContext, g_psFontFixed6x8);

    //
    // Update the status on the screen.
    //
    if(pcString != 0)
    {
        GrStringDrawCentered(&g_sContext, pcString,
                             -1, GrContextDpyWidthGet(&g_sContext) / 2,
                             58, 1);
    }
}
Esempio n. 16
0
//*****************************************************************************
//
// This example encrypts blocks of plaintext using TDES in CBC mode.  It
// does the encryption first without uDMA and then with uDMA.  The results
// are checked after each operation.
//
//*****************************************************************************
int
main(void)
{
    uint32_t pui32CipherText[16], ui32Errors, ui32Idx, ui32SysClock;
    tContext sContext;
    
    //
    // Run from the PLL at 120 MHz.
    //
    ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                       SYSCTL_OSC_MAIN |
                                       SYSCTL_USE_PLL |
                                       SYSCTL_CFG_VCO_480), 120000000);

    //
    // Configure the device pins.
    //
    PinoutSet();

    //
    // Initialize the display driver.
    //
    Kentec320x240x16_SSD2119Init(ui32SysClock);

    //
    // Initialize the graphics context.
    //
    GrContextInit(&sContext, &g_sKentec320x240x16_SSD2119);

    //
    // Draw the application frame.
    //
    FrameDraw(&sContext, "tdes-cbc-encrypt");
    
    //
    // Show some instructions on the display
    //
    GrContextFontSet(&sContext, g_psFontCm20);
    GrContextForegroundSet(&sContext, ClrWhite);
    GrStringDrawCentered(&sContext, "Connect a terminal to", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 60, false);
    GrStringDrawCentered(&sContext, "UART0 (115200,N,8,1)", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 80, false);
    GrStringDrawCentered(&sContext, "for more information.", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 100, false);

    //
    // Initialize local variables.
    //
    ui32Errors = 0;
    for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
    {
        pui32CipherText[ui32Idx] = 0;
    }

    //
    // Enable stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
    ROM_FPUStackingEnable();

    //
    // Enable DES interrupts.
    //
    ROM_IntEnable(INT_DES0);

    //
    // Enable debug output on UART0 and print a welcome message.
    //
    ConfigureUART();
    UARTprintf("Starting TDES CBC encryption demo.\n");
    GrStringDrawCentered(&sContext, "Starting demo...", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 140, false);

    //
    // Enable the uDMA module.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);

    //
    // Setup the control table.
    //
    ROM_uDMAEnable();
    ROM_uDMAControlBaseSet(g_psDMAControlTable);

    //
    // Initialize the CCM and DES modules.
    //
    if(!DESInit())
    {
        UARTprintf("Initialization of the DES module failed.\n");
        ui32Errors |= 0x00000001;
    }

    //
    // Perform the encryption without uDMA.
    //
    UARTprintf("Performing encryption without uDMA.\n");
    TDESCBCEncrypt(g_pui32TDESPlainText, pui32CipherText, g_pui32TDESKey,
                   64, g_pui32TDESIV, false);

    //
    // Check the result.
    //
    for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
    {
        if(pui32CipherText[ui32Idx] != g_pui32TDESCipherText[ui32Idx])
        {
            UARTprintf("Ciphertext mismatch on word %d. Exp: 0x%x, Act: "
                       "0x%x\n", ui32Idx, g_pui32TDESCipherText[ui32Idx],
                       pui32CipherText[ui32Idx]);
            ui32Errors |= (ui32Idx << 16) | 0x00000002;
        }
    }

    //
    // Clear the array containing the ciphertext.
    //
    for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
    {
        pui32CipherText[ui32Idx] = 0;
    }

    //
    // Perform the encryption with uDMA.
    //
    UARTprintf("Performing encryption with uDMA.\n");
    TDESCBCEncrypt(g_pui32TDESPlainText, pui32CipherText, g_pui32TDESKey,
                   64, g_pui32TDESIV, true);

    //
    // Check the result.
    //
    for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
    {
        if(pui32CipherText[ui32Idx] != g_pui32TDESCipherText[ui32Idx])
        {
            UARTprintf("Ciphertext mismatch on word %d. Exp: 0x%x, Act: "
                       "0x%x\n", ui32Idx, g_pui32TDESCipherText[ui32Idx],
                       pui32CipherText[ui32Idx]);
            ui32Errors |= (ui32Idx << 16) | 0x00000004;
        }
    }

    //
    // Finished.
    //
    if(ui32Errors)
    {
        UARTprintf("Demo failed with error code 0x%x.\n", ui32Errors);
        GrStringDrawCentered(&sContext, "Demo failed.", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 180, false);
    }
    else
    {
        UARTprintf("Demo completed successfully.\n");
        GrStringDrawCentered(&sContext, "Demo passed.", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 180, false);
    }

    while(1)
    {
    }
}
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
    tRectangle sRect;
    uint_fast32_t ui32Retcode;

    //
    // Set the system clock to run at 50MHz from the PLL.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Configure SysTick for a 100Hz interrupt.  The FatFs driver wants a 10 ms
    // tick.
    //
    ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100);
    ROM_SysTickEnable();
    ROM_SysTickIntEnable();

    //
    // Configure and enable uDMA
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);
    SysCtlDelay(10);
    ROM_uDMAControlBaseSet(&sDMAControlTable[0]);
    ROM_uDMAEnable();

    //
    // Initialize the display driver.
    //
    CFAL96x64x16Init();

    //
    // Initialize the graphics context.
    //
    GrContextInit(&g_sContext, &g_sCFAL96x64x16);

    //
    // Fill the top 15 rows of the screen with blue to create the banner.
    //
    sRect.i16XMin = 0;
    sRect.i16YMin = 0;
    sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.i16YMax = DISPLAY_BANNER_HEIGHT - 1;
    GrContextForegroundSet(&g_sContext, ClrDarkBlue);
    GrRectFill(&g_sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&g_sContext, ClrWhite);
    GrRectDraw(&g_sContext, &sRect);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&g_sContext, g_psFontFixed6x8);
    GrStringDrawCentered(&g_sContext, "usb-dev-msc", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 5, 0);

    //
    // Initialize the idle timeout and reset all flags.
    //
    g_ui32IdleTimeout = 0;
    g_ui32Flags = 0;

    //
    // Initialize the state to idle.
    //
    g_eMSCState = MSC_DEV_DISCONNECTED;

    //
    // Draw the status bar and set it to idle.
    //
    UpdateStatus("Disconnected", 1);

    //
    // Enable the USB controller.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);

    //
    // Set the USB pins to be controlled by the USB controller.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
    ROM_GPIOPinConfigure(GPIO_PG4_USB0EPEN);
    ROM_GPIOPinTypeUSBDigital(GPIO_PORTG_BASE, GPIO_PIN_4);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL);
    ROM_GPIOPinTypeUSBAnalog(GPIO_PORTL_BASE, GPIO_PIN_6 | GPIO_PIN_7);
    ROM_GPIOPinTypeUSBAnalog(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Set the USB stack mode to Device mode with VBUS monitoring.
    //
    USBStackModeSet(0, eUSBModeDevice, 0);

    //
    // Pass our device information to the USB library and place the device
    // on the bus.
    //
    USBDMSCInit(0, &g_sMSCDevice);

    //
    // Determine whether or not an SDCard is installed.  If not, print a
    // warning and have the user install one and restart.
    //
    ui32Retcode = disk_initialize(0);

    GrContextFontSet(&g_sContext, g_psFontFixed6x8);
    if(ui32Retcode != RES_OK)
    {
        GrStringDrawCentered(&g_sContext, "No SDCard Found", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 16, 0);
        GrStringDrawCentered(&g_sContext, "Please insert",
                             -1, GrContextDpyWidthGet(&g_sContext) / 2, 26, 0);
        GrStringDrawCentered(&g_sContext, "a card and",
                             -1, GrContextDpyWidthGet(&g_sContext) / 2, 36, 0);
        GrStringDrawCentered(&g_sContext, "reset the board.",
                             -1, GrContextDpyWidthGet(&g_sContext) / 2, 46, 0);
    }
    else
    {
        GrStringDrawCentered(&g_sContext, "SDCard Found",  -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 30, 0);
    }

    //
    // Drop into the main loop.
    //
    while(1)
    {
        switch(g_eMSCState)
        {
            case MSC_DEV_READ:
            {
                //
                // Update the screen if necessary.
                //
                if(g_ui32Flags & FLAG_UPDATE_STATUS)
                {
                    UpdateStatus("Reading", 0);
                    g_ui32Flags &= ~FLAG_UPDATE_STATUS;
                }

                //
                // If there is no activity then return to the idle state.
                //
                if(g_ui32IdleTimeout == 0)
                {
                    UpdateStatus("Idle", 0);
                    g_eMSCState = MSC_DEV_IDLE;
                }

                break;
            }
            case MSC_DEV_WRITE:
            {
                //
                // Update the screen if necessary.
                //
                if(g_ui32Flags & FLAG_UPDATE_STATUS)
                {
                    UpdateStatus("Writing", 0);
                    g_ui32Flags &= ~FLAG_UPDATE_STATUS;
                }

                //
                // If there is no activity then return to the idle state.
                //
                if(g_ui32IdleTimeout == 0)
                {
                    UpdateStatus("Idle", 0);
                    g_eMSCState = MSC_DEV_IDLE;
                }
                break;
            }
            case MSC_DEV_DISCONNECTED:
            {
                //
                // Update the screen if necessary.
                //
                if(g_ui32Flags & FLAG_UPDATE_STATUS)
                {
                    UpdateStatus("Disconnected", 0);
                    g_ui32Flags &= ~FLAG_UPDATE_STATUS;
                }
                break;
            }
            case MSC_DEV_IDLE:
            {
                break;
            }
            default:
            {
                break;
            }
        }
    }
}
//*****************************************************************************
//
// Run the AES encryption/decryption example
//
//*****************************************************************************
int
main(void)
{
    unsigned char ucBlockBuf[17];
    const unsigned *puKey;
    unsigned char ucTempIV[16];
    tContext sContext;
    tRectangle sRect;
    long lCenterX;

    //
    // Set the clocking to run directly from the crystal.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Set the pinout appropriately for this board.
    //
    PinoutSet();

    //
    // Initialize the display driver.
    //
    Kitronix320x240x16_SSD2119Init();

    //
    // Initialize the graphics context and find the middle X coordinate.
    //
    GrContextInit(&sContext, &g_sKitronix320x240x16_SSD2119);
    lCenterX = GrContextDpyWidthGet(&sContext) / 2;

    //
    // Fill the top 15 rows of the screen with blue to create the banner.
    //
    sRect.sXMin = 0;
    sRect.sYMin = 0;
    sRect.sXMax = GrContextDpyWidthGet(&sContext) - 1;
    sRect.sYMax = 23;
    GrContextForegroundSet(&sContext, ClrDarkBlue);
    GrRectFill(&sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&sContext, ClrWhite);
    GrRectDraw(&sContext, &sRect);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&sContext, g_pFontCm20);
    GrStringDrawCentered(&sContext, "aes-expanded-key", -1, lCenterX, 8, 0);

    //
    // Print the plain text title
    //
    GrContextFontSet(&sContext, g_pFontCmss22b);
    GrStringDrawCentered(&sContext, "Plain Text:", -1, lCenterX, 60, 0);
    GrStringDrawCentered(&sContext, g_cPlainText, -1, lCenterX, 85, 0);

    //
    // Get the expanded key to use for encryption
    //
    puKey = AESExpandedEncryptKeyData();

    //
    // Generate the initialization vector needed for CBC mode.
    // A temporary copy is made that will be used with the crypt
    // function because the crypt function will modify the IV that is passed.
    //
    AESGenerateIV(g_ucIV, 1);
    memcpy(ucTempIV, g_ucIV, 16);

    //
    // Encrypt the plaintext message using CBC mode
    //
    aes_crypt_cbc(puKey, AES_ENCRYPT, 16, ucTempIV,
                  (unsigned char *)g_cPlainText, ucBlockBuf);

    //
    // Print the encrypted block to the display.  Note that it will
    // appear as nonsense data.  The block needs to be null terminated
    // so that the StringDraw function will work correctly.
    //
    ucBlockBuf[16] = 0;
    GrStringDrawCentered(&sContext, "Encrypted:", -1, lCenterX, 120, 0);
    GrStringDrawCentered(&sContext, (char *)ucBlockBuf, -1, lCenterX, 145, 0);

    //
    // Get the expanded key to use for decryption
    //
    puKey = AESExpandedDecryptKeyData();

    //
    // Decrypt the message using CBC mode
    //
    memcpy(ucTempIV, g_ucIV, 16);
    aes_crypt_cbc(puKey, AES_DECRYPT, 16, ucTempIV, ucBlockBuf, ucBlockBuf);

    //
    // Print the decrypted block to the display.  It should be the same text
    // as the original message.
    //
    GrStringDrawCentered(&sContext, "Decrypted:", -1, lCenterX, 180, 0);
    GrStringDrawCentered(&sContext, (char *)ucBlockBuf, -1, lCenterX, 205, 0);

    //
    // Finished.
    //
    while(1)
    {
    }
}
Esempio n. 19
0
//*****************************************************************************
//
// This function updates the cursor position based on deltas received from
// the mouse device.
//
// \param i32XDelta is the signed movement in the X direction.
// \param i32YDelta is the signed movement in the Y direction.
//
// This function is called by the mouse handler code when it detects a change
// in the position of the mouse.  It will take the inputs and force them
// to be constrained to the display area of the screen.  If the left mouse
// button is pressed then the mouse will draw on the screen and if it is not
// it will move around normally.  A side effect of not being able to read the
// current state of the screen is that the cursor will erase anything it moves
// over while the left mouse button is not pressed.
//
// \return None.
//
//*****************************************************************************
void
UpdateCursor(int32_t i32XDelta, int32_t i32YDelta)
{
    int32_t i32Temp;

    //
    // If the left button is not pressed then erase the previous cursor
    // position.
    //
    if((g_ui32Buttons & 1) == 0)
    {
        //
        // Erase the previous cursor.
        //
        GrContextForegroundSet(&g_sContext, DISPLAY_MOUSE_BG);
        GrRectFill(&g_sContext, &g_sCursor);
    }

    //
    // Need to do signed math so use the temporary signed value.
    //
    i32Temp = g_sCursor.i16XMin;

    //
    // Update the X position without going off the screen.
    //
    if(((int)g_sCursor.i16XMin + i32XDelta + DISPLAY_MOUSE_SIZE) <
       GrContextDpyWidthGet(&g_sContext))
    {
        //
        // Update the X cursor position.
        //
        i32Temp += i32XDelta;

        //
        // Don't let the cursor go off the left of the screen either.
        //
        if(i32Temp < 0)
        {
            i32Temp = 0;
        }
    }

    //
    // Update the X position.
    //
    g_sCursor.i16XMin = i32Temp;
    g_sCursor.i16XMax = i32Temp + DISPLAY_MOUSE_SIZE;

    //
    // Need to do signed math so use the temporary signed value.
    //
    i32Temp = g_sCursor.i16YMin;

    //
    // Update the Y position without going off the screen.
    //
    if(((int)g_sCursor.i16YMin + i32YDelta) <
       (GrContextDpyHeightGet(&g_sContext) -
        DISPLAY_BANNER_HEIGHT - DISPLAY_MOUSE_SIZE - 1))
    {
        //
        // Update the Y cursor position.
        //
        i32Temp += i32YDelta;

        //
        // Don't let the cursor overwrite the status area of the screen.
        //
        if(i32Temp < DISPLAY_BANNER_HEIGHT + 1)
        {
            i32Temp = DISPLAY_BANNER_HEIGHT + 1;
        }
    }

    //
    // Update the Y position.
    //
    g_sCursor.i16YMin = i32Temp;
    g_sCursor.i16YMax = i32Temp + DISPLAY_MOUSE_SIZE;

    //
    // Draw the new cursor.
    //
    GrContextForegroundSet(&g_sContext, DISPLAY_MOUSE_FG);
    GrRectFill(&g_sContext, &g_sCursor);
}
Esempio n. 20
0
//*****************************************************************************
//
// This example application demonstrates the use of the timers to generate
// periodic interrupts.
//
//*****************************************************************************
int
main(void)
{
    tRectangle sRect;

    //
    // Enable lazy stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
    ROM_FPULazyStackingEnable();

    //
    // Set the clocking to run directly from the crystal.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Initialize the display driver.
    //
    CFAL96x64x16Init();

    //
    // Initialize the graphics context and find the middle X coordinate.
    //
    GrContextInit(&g_sContext, &g_sCFAL96x64x16);

    //
    // Fill the top part of the screen with blue to create the banner.
    //
    sRect.sXMin = 0;
    sRect.sYMin = 0;
    sRect.sXMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.sYMax = 9;
    GrContextForegroundSet(&g_sContext, ClrDarkBlue);
    GrRectFill(&g_sContext, &sRect);

    //
    // Change foreground for white text.
    //
    GrContextForegroundSet(&g_sContext, ClrWhite);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&g_sContext, g_pFontFixed6x8);
    GrStringDrawCentered(&g_sContext, "timers", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 4, 0);

    //
    // Initialize timer status display.
    //
    GrContextFontSet(&g_sContext, g_pFontFixed6x8);
    GrStringDraw(&g_sContext, "Timer 0:", -1, 16, 26, 0);
    GrStringDraw(&g_sContext, "Timer 1:", -1, 16, 36, 0);

    //
    // Enable the peripherals used by this example.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);

    //
    // Enable processor interrupts.
    //
    ROM_IntMasterEnable();

    //
    // Configure the two 32-bit periodic timers.
    //
    ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    ROM_TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
    ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet());
    ROM_TimerLoadSet(TIMER1_BASE, TIMER_A, ROM_SysCtlClockGet() / 2);

    //
    // Setup the interrupts for the timer timeouts.
    //
    ROM_IntEnable(INT_TIMER0A);
    ROM_IntEnable(INT_TIMER1A);
    ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    ROM_TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);

    //
    // Enable the timers.
    //
    ROM_TimerEnable(TIMER0_BASE, TIMER_A);
    ROM_TimerEnable(TIMER1_BASE, TIMER_A);

    //
    // Loop forever while the timers run.
    //
    while(1)
    {
    }
}
Esempio n. 21
0
//*****************************************************************************
//
// This is the main example program.  It checks to see that the interrupts are
// processed in the correct order when they have identical priorities,
// increasing priorities, and decreasing priorities.  This exercises interrupt
// preemption and tail chaining.
//
//*****************************************************************************
int
main(void)
{
    tRectangle sRect;
    uint_fast8_t ui8Error;

    //
    // Enable lazy stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
    ROM_FPULazyStackingEnable();

    //
    // Set the clocking to run directly from the crystal.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Enable the peripherals used by this example.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);

    //
    // Initialize the display driver.
    //
    CFAL96x64x16Init();

    //
    // Initialize the graphics context and find the middle X coordinate.
    //
    GrContextInit(&g_sContext, &g_sCFAL96x64x16);

    //
    // Fill the top part of the screen with blue to create the banner.
    //
    sRect.i16XMin = 0;
    sRect.i16YMin = 0;
    sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.i16YMax = 9;
    GrContextForegroundSet(&g_sContext, ClrDarkBlue);
    GrRectFill(&g_sContext, &sRect);

    //
    // Change foreground for white text.
    //
    GrContextForegroundSet(&g_sContext, ClrWhite);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&g_sContext, g_psFontFixed6x8);
    GrStringDrawCentered(&g_sContext, "interrupts", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 4, 0);
    GrContextFontSet(&g_sContext, g_psFontFixed6x8);

    //
    // Put the status header text on the display.
    //
    GrStringDraw(&g_sContext, "Active:", -1, 6, 32, 0);
    GrStringDraw(&g_sContext, "Pending:", -1, 0, 44, 0);

    //
    // Configure the PB0-PB2 to be outputs to indicate entry/exit of one
    // of the interrupt handlers.
    //
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1 |
                                               GPIO_PIN_3);
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2, 0);

    //
    // Set up and enable the SysTick timer.  It will be used as a reference
    // for delay loops in the interrupt handlers.  The SysTick timer period
    // will be set up for one second.
    //
    ROM_SysTickPeriodSet(ROM_SysCtlClockGet());
    ROM_SysTickEnable();

    //
    // Reset the error indicator.
    //
    ui8Error = 0;

    //
    // Enable interrupts to the processor.
    //
    ROM_IntMasterEnable();

    //
    // Enable the interrupts.
    //
    ROM_IntEnable(INT_GPIOA);
    ROM_IntEnable(INT_GPIOB);
    ROM_IntEnable(INT_GPIOC);

    //
    // Indicate that the equal interrupt priority test is beginning.
    //
    GrStringDrawCentered(&g_sContext, "Equal Pri", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 20, 1);

    //
    // Set the interrupt priorities so they are all equal.
    //
    ROM_IntPrioritySet(INT_GPIOA, 0x00);
    ROM_IntPrioritySet(INT_GPIOB, 0x00);
    ROM_IntPrioritySet(INT_GPIOC, 0x00);

    //
    // Reset the interrupt flags.
    //
    g_ui32GPIOa = 0;
    g_ui32GPIOb = 0;
    g_ui32GPIOc = 0;
    g_ui32Index = 1;

    //
    // Trigger the interrupt for GPIO C.
    //
    HWREG(NVIC_SW_TRIG) = INT_GPIOC - 16;

    //
    // Put the current interrupt state on the LCD.
    //
    DisplayIntStatus();

    //
    // Verify that the interrupts were processed in the correct order.
    //
    if((g_ui32GPIOa != 3) || (g_ui32GPIOb != 2) || (g_ui32GPIOc != 1))
    {
        ui8Error |= 1;
    }

    //
    // Wait two seconds.
    //
    Delay(2);

    //
    // Indicate that the decreasing interrupt priority test is beginning.
    //
    GrStringDrawCentered(&g_sContext, " Decreasing Pri ", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 20, 1);

    //
    // Set the interrupt priorities so that they are decreasing (i.e. C > B >
    // A).
    //
    ROM_IntPrioritySet(INT_GPIOA, 0x80);
    ROM_IntPrioritySet(INT_GPIOB, 0x40);
    ROM_IntPrioritySet(INT_GPIOC, 0x00);

    //
    // Reset the interrupt flags.
    //
    g_ui32GPIOa = 0;
    g_ui32GPIOb = 0;
    g_ui32GPIOc = 0;
    g_ui32Index = 1;

    //
    // Trigger the interrupt for GPIO C.
    //
    HWREG(NVIC_SW_TRIG) = INT_GPIOC - 16;

    //
    // Put the current interrupt state on the CSTN.
    //
    DisplayIntStatus();

    //
    // Verify that the interrupts were processed in the correct order.
    //
    if((g_ui32GPIOa != 3) || (g_ui32GPIOb != 2) || (g_ui32GPIOc != 1))
    {
        ui8Error |= 2;
    }

    //
    // Wait two seconds.
    //
    Delay(2);

    //
    // Indicate that the increasing interrupt priority test is beginning.
    //
    GrStringDrawCentered(&g_sContext, " Increasing Pri ", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 20, 1);

    //
    // Set the interrupt priorities so that they are increasing (i.e. C < B <
    // A).
    //
    ROM_IntPrioritySet(INT_GPIOA, 0x00);
    ROM_IntPrioritySet(INT_GPIOB, 0x40);
    ROM_IntPrioritySet(INT_GPIOC, 0x80);

    //
    // Reset the interrupt flags.
    //
    g_ui32GPIOa = 0;
    g_ui32GPIOb = 0;
    g_ui32GPIOc = 0;
    g_ui32Index = 1;

    //
    // Trigger the interrupt for GPIO C.
    //
    HWREG(NVIC_SW_TRIG) = INT_GPIOC - 16;

    //
    // Put the current interrupt state on the CSTN.
    //
    DisplayIntStatus();

    //
    // Verify that the interrupts were processed in the correct order.
    //
    if((g_ui32GPIOa != 1) || (g_ui32GPIOb != 2) || (g_ui32GPIOc != 3))
    {
        ui8Error |= 4;
    }

    //
    // Wait two seconds.
    //
    Delay(2);

    //
    // Disable the interrupts.
    //
    ROM_IntDisable(INT_GPIOA);
    ROM_IntDisable(INT_GPIOB);
    ROM_IntDisable(INT_GPIOC);

    //
    // Disable interrupts to the processor.
    //
    ROM_IntMasterDisable();

    //
    // Print out results if error occurred.
    //
    if(ui8Error)
    {
        GrStringDraw(&g_sContext, "Equal: P        ", -1, 0, 32, 1);
        GrStringDraw(&g_sContext, "  Inc: P        ", -1, 0, 44, 1);
        GrStringDraw(&g_sContext, "  Dec: P        ", -1, 0, 56, 1);
        if(ui8Error & 1)
        {
            GrStringDraw(&g_sContext, "F ", -1, 42, 32, 1);
        }
        if(ui8Error & 2)
        {
            GrStringDraw(&g_sContext, "F ", -1, 42, 44, 1);
        }
        if(ui8Error & 4)
        {
            GrStringDraw(&g_sContext, "F ", -1, 42, 56, 1);
        }
    }
    else
    {
        GrStringDrawCentered(&g_sContext, "    Success!    ", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 20, 1);
    }

    //
    // Flush the display.
    //
    GrFlush(&g_sContext);

    //
    // Loop forever.
    //
    while(1)
    {
    }
}
Esempio n. 22
0
//*****************************************************************************
//
// A simple demonstration of the features of the Stellaris Graphics Library.
//
//*****************************************************************************
int
main(void)
{
    unsigned long ulIdx;
    tRectangle sRect;

    //
    // Set the clocking to run from the PLL.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_8MHZ);

    //
    // Initialize the display driver.
    //
    Formike128x128x16Init();

    //
    // Turn on the backlight.
    //
    Formike128x128x16BacklightOn();

    //
    // Initialize the graphics context.
    //
    GrContextInit(&g_sContext, &g_sFormike128x128x16);

    //
    // Fill the top 15 rows of the screen with blue to create the banner.
    //
    sRect.sXMin = 0;
    sRect.sYMin = 0;
    sRect.sXMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.sYMax = 14;
    GrContextForegroundSet(&g_sContext, ClrDarkBlue);
    GrRectFill(&g_sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&g_sContext, ClrWhite);
    GrRectDraw(&g_sContext, &sRect);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&g_sContext, &g_sFontFixed6x8);
    GrStringDrawCentered(&g_sContext, "grlib_demo", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 7, 0);

    //
    // Draw a vertical sweep of lines from red to green.
    //
    for(ulIdx = 0; ulIdx <= 10; ulIdx++)
    {
        GrContextForegroundSet(&g_sContext,
                               (((((10 - ulIdx) * 255) / 10) << ClrRedShift) |
                                (((ulIdx * 255) / 10) << ClrGreenShift)));
        GrLineDraw(&g_sContext, 62, 70, 2, 70 - (5 * ulIdx));
    }

    //
    // Draw a horizontal sweep of lines from green to blue.
    //
    for(ulIdx = 1; ulIdx <= 10; ulIdx++)
    {
        GrContextForegroundSet(&g_sContext,
                               (((((10 - ulIdx) * 255) / 10) <<
                                 ClrGreenShift) |
                                (((ulIdx * 255) / 10) << ClrBlueShift)));
        GrLineDraw(&g_sContext, 62, 70, 2 + (ulIdx * 6), 20);
    }

    //
    // Draw a filled circle with an overlapping circle.
    //
    GrContextForegroundSet(&g_sContext, ClrBrown);
    GrCircleFill(&g_sContext, 88, 37, 17);
    GrContextForegroundSet(&g_sContext, ClrSkyBlue);
    GrCircleDraw(&g_sContext, 104, 45, 17);

    //
    // Draw a filled rectangle with an overlapping rectangle.
    //
    GrContextForegroundSet(&g_sContext, ClrSlateGray);
    sRect.sXMin = 4;
    sRect.sYMin = 84;
    sRect.sXMax = 42;
    sRect.sYMax = 104;
    GrRectFill(&g_sContext, &sRect);
    GrContextForegroundSet(&g_sContext, ClrSlateBlue);
    sRect.sXMin += 12;
    sRect.sYMin += 15;
    sRect.sXMax += 12;
    sRect.sYMax += 15;
    GrRectDraw(&g_sContext, &sRect);

    //
    // Draw a piece of text in fonts of increasing size.
    //
    GrContextForegroundSet(&g_sContext, ClrSilver);
    GrStringDraw(&g_sContext, "Strings", -1, 75, 114, 0);

    //
    // Draw an image.
    //
    GrImageDraw(&g_sContext, g_pucLogo, 80, 77);

    //
    // Flush any cached drawing operations.
    //
    GrFlush(&g_sContext);

    //
    // Loop forever.
    //
    while(1)
    {
    }
}
Esempio n. 23
0
int AcquireRun(void)
{


    if(g_ulADCCount != ulLastADCCount)
    {
    	ulLastADCCount=g_ulADCCount;



        if(ind2 <0)
        ind2=ind2+buffer_size;
        else
        ind2=ind2%buffer_size;

        buffer[0][ind2]=(unsigned long)(g_ulADCData[0]&0xfff);
        //max1=max1+buffer[0][ind2];
        buffer[1][ind2]=(unsigned long)g_ulADCData[1];
        //prev_index=ind2;
        ind2++;





        rcount++;
        compute_sum1(0);
        if(rcount%buffer_increment==0)
        {
        	compute_sum1(1);
        }

        rem=rcount%((int)PULSE_SAMPLE);

    	if(rem==0)
    	{

    	    mode1=(mode1+1)%2;
    	    if(mode1==0)
    	    	led_on();
    	    else
    	    	led_off();


    	    int angle=0;
    	    if(max2>0 && max1>0 && abs(maxi1-maxi2)<PULSE_SAMPLE)
    	    {
    	    angle=abs(maxi1-maxi2);
    	    }

    	    acount++;


    	    max1=max1*20.0/4095;
    	    max2=max2*20.0/4095;
    	    //max1=max1/(2*buffer_increment);
    		snprintf(text,sizeof(text),"%ld,%ld",max1,max2);


    	    GrContextForegroundSet(&sDisplayContext, ClrDarkBlue);
    	    GrRectFill(&sDisplayContext, &sRect1);
    	    GrContextForegroundSet(&sDisplayContext, ClrWhite);
    	    //GrRectDraw(&sDisplayContext, &sRect1);
    	    GrContextFontSet(&sDisplayContext, g_pFontCm12);
    	    GrStringDrawCentered(&sDisplayContext,text, -1,
    	                         GrContextDpyWidthGet(&sDisplayContext) / 2, 10, 0);

    	    GrContextForegroundSet(&sDisplayContext, ClrDarkBlue);
    	    GrRectFill(&sDisplayContext, &sRect2);
    	    GrContextForegroundSet(&sDisplayContext, ClrWhite);
    	    sprintf(text,"%4d,%4d",buffer_increment,FILTER_SAMPLE);
    	    GrContextFontSet(&sDisplayContext, g_pFontCm12/*g_pFontFixed6x8*/);
    	    GrStringDrawCentered(&sDisplayContext, text, -1,
    	                         GrContextDpyWidthGet(&sDisplayContext) / 2,
    	                         ((GrContextDpyHeightGet(&sDisplayContext) - 24) / 2) + 24,
    	                         0);
    	    GrFlush(&sDisplayContext);

    		max1=0;
    	    max2=0;
    	    res=0;
    	    res1=0;
    	    maxi1=0;
    	    maxi2=0;
    	    buffer_index=0;

    	    for(i=0;i<buffer_size;i++)
    	    {
        		buffer[0][i]=0;
    	    	buffer[1][i]=0;
    	    }
    	    i=0;
    	    j=0;
    	    ind2=buffer_index-buffer_increment+1;
    	    buffer_index=0;
    	}

    }


}
//*****************************************************************************
//
// This is the generic callback from host stack.
//
// pvData is actually a pointer to a tEventInfo structure.
//
// This function will be called to inform the application when a USB event has
// occurred that is outside those related to the mouse device.  At this
// point this is used to detect unsupported devices being inserted and removed.
// It is also used to inform the application when a power fault has occurred.
// This function is required when the g_USBGenericEventDriver is included in
// the host controller driver array that is passed in to the
// USBHCDRegisterDrivers() function.
//
//*****************************************************************************
void
USBHCDEvents(void *pvData)
{
    tEventInfo *pEventInfo;
    tRectangle sRect;

    //
    // Fill the bottom rows of the screen with blue to create the status area.
    //
    sRect.i16XMin = 0;
    sRect.i16YMin = GrContextDpyHeightGet(&g_sContext) -
                  DISPLAY_BANNER_HEIGHT - 1;
    sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.i16YMax = sRect.i16YMin + DISPLAY_BANNER_HEIGHT;

    GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_BG);
    GrRectFill(&g_sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&g_sContext, ClrWhite);
    GrRectDraw(&g_sContext, &sRect);

    //
    // Set the font for the status message
    //
    GrContextFontSet(&g_sContext, g_psFontFixed6x8);

    //
    // Cast this pointer to its actual type.
    //
    pEventInfo = (tEventInfo *)pvData;

    switch(pEventInfo->ui32Event)
    {
        //
        // New mouse detected.
        //
        case USB_EVENT_CONNECTED:
        {
            //
            // See if this is a HID Mouse.
            //
            if((USBHCDDevClass(pEventInfo->ui32Instance, 0) == USB_CLASS_HID) &&
               (USBHCDDevProtocol(pEventInfo->ui32Instance, 0) ==
                USB_HID_PROTOCOL_MOUSE))
            {
                //
                // Indicate that the mouse has been detected.
                //
                GrStringDrawCentered(&g_sContext, "Mouse Connected", -1,
                                     GrContextDpyWidthGet(&g_sContext) / 2,
                                     sRect.i16YMin + 5, 0);

                //
                // Set initial mouse information.
                //
                GrStringDrawCentered(&g_sContext, "0,0", -1,
                                     GrContextDpyWidthGet(&g_sContext) / 2, 26, 1);
                GrStringDrawCentered(&g_sContext, "000", -1,
                                     GrContextDpyWidthGet(&g_sContext) / 2, 46, 1);

                //
                // Proceed to the STATE_MOUSE_INIT state so that the main loop
                // can finish initialized the mouse since USBHMouseInit()
                // cannot be called from within a callback.
                //
                g_eUSBState = STATE_MOUSE_INIT;
            }

            break;
        }
        //
        // Unsupported device detected.
        //
        case USB_EVENT_UNKNOWN_CONNECTED:
        {
            GrStringDrawCentered(&g_sContext, "Unknown Device", -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2,
                                 sRect.i16YMin + 5, 0);
            //
            // An unknown device was detected.
            //
            g_eUSBState = STATE_UNKNOWN_DEVICE;

            break;
        }
        //
        // Device has been unplugged.
        //
        case USB_EVENT_DISCONNECTED:
        {
            //
            // Indicate that the device has been disconnected.
            //
            GrStringDrawCentered(&g_sContext, "No Device", -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2,
                                 sRect.i16YMin + 5, 0);

            //
            // Reset the mouse information.
            //
            GrStringDrawCentered(&g_sContext, "   -,-   ", -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2, 26, 1);
            GrStringDrawCentered(&g_sContext, "---", -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2, 46, 1);

            //
            // Change the state so that the main loop knows that the device is
            // no longer present.
            //
            g_eUSBState = STATE_NO_DEVICE;

            //
            // Reset the button state.
            //
            g_ui32Buttons = 0;

            break;
        }
        //
        // Power Fault has occurred.
        //
        case USB_EVENT_POWER_FAULT:
        {
            //
            // Indicate that there has been a power fault.
            //
            GrStringDrawCentered(&g_sContext, "Power Fault", -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2,
                                 sRect.i16YMin + 5, 0);

            //
            // Reset the mouse information.
            //
            GrStringDrawCentered(&g_sContext, "   -,-   ", -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2, 26, 1);
            GrStringDrawCentered(&g_sContext, "---", -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2, 46, 1);

            //
            // No power means no device is present.
            //
            g_eUSBState = STATE_POWER_FAULT;

            break;
        }

        default:
        {
            break;
        }
    }
}
//*****************************************************************************
//
// This function shows a status screen.  It draws a banner at the top of the
// screen with the name of the application, and then up to 5 lines of text
// in the remaining screen area.  The caller specifies a pointer to a set of
// strings, and the count of number of lines of text (up to 5).  This
// function attempts to vertically center the strings on the display.
//
//*****************************************************************************
static void
ShowStatusScreen(char *pcStatus[], uint32_t ui32Count)
{
    tContext sContext;
    tRectangle sRect;
    uint32_t ui32Idx;
    int32_t i32Y;

    //
    // Initialize the graphics context.
    //
    GrContextInit(&sContext, &g_sCFAL96x64x16);

    //
    // Fill the top part of the screen with blue to create the banner.
    //
    sRect.i16XMin = 0;
    sRect.i16YMin = 0;
    sRect.i16XMax = GrContextDpyWidthGet(&sContext) - 1;
    sRect.i16YMax = 9;
    GrContextForegroundSet(&sContext, ClrDarkBlue);
    GrRectFill(&sContext, &sRect);

    //
    // Fill the rest of the display with black, to clear whatever was there
    // before.
    //
    sRect.i16YMin = 10;
    sRect.i16YMax = 63;
    GrContextForegroundSet(&sContext, ClrBlack);
    GrRectFill(&sContext, &sRect);

    //
    // Change foreground for white text.
    //
    GrContextForegroundSet(&sContext, ClrWhite);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&sContext, g_psFontFixed6x8);
    GrStringDrawCentered(&sContext, "usb-host-msc", -1,
                         GrContextDpyWidthGet(&sContext) / 2, 4, 0);

    //
    // Cap the max number of status lines to 5
    //
    ui32Count = (ui32Count > 5) ? 5 : ui32Count;

    //
    // Compute the starting Y coordinate based on the number of lines.
    //
    i32Y = 40 - (ui32Count * 5);

    //
    // Display the status lines
    //
    for(ui32Idx = 0; ui32Idx < ui32Count; ui32Idx++)
    {
        GrStringDrawCentered(&sContext, pcStatus[ui32Idx], -1,
                             GrContextDpyWidthGet(&sContext) / 2, i32Y, 0);
        i32Y += 10;
    }
}
//*****************************************************************************
//
// This is the callback from the USB HID mouse handler.
//
// \param psMsInstance is ignored by this function.
// \param ui32Event is one of the valid events for a mouse device.
// \param ui32MsgParam is defined by the event that occurs.
// \param pvMsgData is a pointer to data that is defined by the event that
// occurs.
//
// This function will be called to inform the application when a mouse has
// been plugged in or removed and any time mouse movement or button pressed
// is detected.
//
// \return None.
//
//*****************************************************************************
void
MouseCallback(tUSBHMouse *psMsInstance, uint32_t ui32Event,
              uint32_t ui32MsgParam, void *pvMsgData)
{
    int32_t i32DoUpdate;
    char pcBuffer[20];

    //
    // Do an update unless there is no reason to.
    //
    i32DoUpdate = 1;

    switch(ui32Event)
    {
        //
        // Mouse button press detected.
        //
        case USBH_EVENT_HID_MS_PRESS:
        {
            //
            // Save the new button that was pressed.
            //
            g_ui32Buttons |= ui32MsgParam;

            break;
        }

        //
        // Mouse button release detected.
        //
        case USBH_EVENT_HID_MS_REL:
        {
            //
            // Remove the button from the pressed state.
            //
            g_ui32Buttons &= ~ui32MsgParam;

            break;
        }

        //
        // Mouse X movement detected.
        //
        case USBH_EVENT_HID_MS_X:
        {
            //
            // Update the cursor X position.
            //
            g_i32CursorX += (int8_t)ui32MsgParam;

            //
            // Cap the value to not cause an overflow.
            //
            if(g_i32CursorX > 9999)
            {
                g_i32CursorX = 9999;
            }

            if(g_i32CursorX < -9999)
            {
                g_i32CursorX = -9999;
            }

            break;
        }

        //
        // Mouse Y movement detected.
        //
        case USBH_EVENT_HID_MS_Y:
        {
            //
            // Update the cursor Y position.
            //
            g_i32CursorY += (int8_t)ui32MsgParam;

            //
            // Cap the value to not cause an overflow.
            //
            if(g_i32CursorY > 9999)
            {
                g_i32CursorY = 9999;
            }

            if(g_i32CursorY < -9999)
            {
                g_i32CursorY = -9999;
            }

            break;
        }
        default:
        {
            //
            // No reason to update.
            //
            i32DoUpdate = 0;

            break;
        }
    }

    //
    // Display the current mouse position and button state if there was an
    // update.
    //
    if(i32DoUpdate)
    {
        GrStringDrawCentered(&g_sContext, "Position:", -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2, 16, 0);
        usprintf(pcBuffer, "   %d,%d   ", g_i32CursorX, g_i32CursorY);
        GrStringDrawCentered(&g_sContext, pcBuffer, -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2, 26, 1);
        GrStringDrawCentered(&g_sContext, "Buttons:", -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2, 36, 0);
        usprintf(pcBuffer, "%d%d%d", g_ui32Buttons & 1, (g_ui32Buttons & 2) >> 1,
                 (g_ui32Buttons & 4) >> 2);
        GrStringDrawCentered(&g_sContext, pcBuffer, -1,
                                 GrContextDpyWidthGet(&g_sContext) / 2, 46, 1);
    }
}
//*****************************************************************************
//
// Handles wrapping a string within an area.
//
// \param psContext is the context of the area to update.
// \param pcString is the string to print out.
// \param i32LineHeight is the height of a character in the currrent font.
// \param i32X is the x position to start printing this string.
// \param i32Y is the y position to start printing this string.
// \param bSplitOnSpace is true if strings in the current language must be
// split only on space characters or false if they may be split between any
// two characters.
//
// \return Returns the number of lines that were printed due to this string.
//
//*****************************************************************************
uint32_t
DrawStringWrapped(tContext *psContext, char *pcString,
                  int32_t i32LineHeight, int32_t i32X, int32_t i32Y,
                  bool bSplitOnSpace)
{
    uint32_t ui32Width, ui32CharWidth, ui32StrWidth, ui32Char;
    uint32_t ui32Lines, ui32Skip;
    char *pcStart, *pcEnd;
    char *pcLastSpace;

    ui32Lines = 0;

    //
    // Get the number of pixels we have to fit the string into across the
    // screen.
    //
    ui32Width = GrContextDpyWidthGet(psContext) - 16 - i32X;

    //
    // Get a pointer to the terminating zero.
    //
    pcEnd = pcString;
    while(*pcEnd)
    {
        pcEnd++;
    }

    //
    // The first substring we draw will start at the beginning of the string.
    //
    pcStart = pcString;
    pcLastSpace = pcString;
    ui32StrWidth = 0;

    //
    // Keep processing until we have no more characters to display.
    //
    do
    {
        //
        // Get the next character in the string.
        //
        ui32Char = GrStringNextCharGet(psContext, pcString,
                                       (pcEnd - pcString), &ui32Skip);

        //
        // Did we reach the end of the string?
        //
        if(ui32Char)
        {
            //
            // No - how wide is this character?
            //
            ui32CharWidth = GrStringWidthGet(psContext, pcString, ui32Skip);

            //
            // Have we run off the edge of the display?
            //
            if((ui32StrWidth + ui32CharWidth) > ui32Width)
            {
                //
                // If we are splitting on spaces, rewind the string pointer to
                // the byte after the last space.
                //
                if(bSplitOnSpace)
                {
                    pcString = pcLastSpace;
                }

                //
                // Yes - draw the substring.
                //
                GrStringDraw(psContext, pcStart, (pcString - pcStart),
                             i32X, i32Y, 0);

                //
                // Increment the line count and move the y position down by the
                // current font's line height.
                //
                ui32Lines++;
                i32Y += i32LineHeight;
                ui32StrWidth = 0;

                //
                // The next string we draw will start at the current position.
                //
                pcStart = pcString;
            }
            else
            {
                //
                // No - update the width and move on to the next character.
                //
                ui32StrWidth += ui32CharWidth;
                pcString += ui32Skip;

                //
                // If this is a space, remember where we are.
                //
                if(ui32Char == SPACE_CHAR)
                {
                    pcLastSpace = pcString;
                }
            }
        }
        else
        {
            //
            // Do we have any remaining chunk of string to draw?
            //
            if(pcStart != pcString)
            {
                //
                // Yes - draw the last section of string.
                //
                GrStringDraw(psContext, pcStart, -1, i32X, i32Y, 0);
                ui32Lines++;
            }
        }
    } while(ui32Char);

    return(ui32Lines);
}
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
    tRectangle sRect;

    //
    // Set the clocking to run from the PLL at 50MHz.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Initialize the display driver.
    //
    CFAL96x64x16Init();

    //
    // Initialize the graphics context.
    //
    GrContextInit(&g_sContext, &g_sCFAL96x64x16);

    //
    // Fill the top part of the screen with blue to create the banner.
    //
    sRect.i16XMin = 0;
    sRect.i16YMin = 0;
    sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.i16YMax = (DISPLAY_BANNER_HEIGHT) - 1;
    GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_BG);
    GrRectFill(&g_sContext, &sRect);

    //
    // Change foreground for white text.
    //
    GrContextForegroundSet(&g_sContext, DISPLAY_TEXT_FG);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&g_sContext, g_psFontFixed6x8);
    GrStringDrawCentered(&g_sContext, "usb-host-mouse", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 4, 0);

    //
    // Display default information about the mouse
    //
    GrStringDrawCentered(&g_sContext, "Position:", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 16, 0);
    GrStringDrawCentered(&g_sContext, "-,-", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 26, 1);
    GrStringDrawCentered(&g_sContext, "Buttons:", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 36, 0);
    GrStringDrawCentered(&g_sContext, "---", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 46, 1);

    //
    // Fill the bottom rows of the screen with blue to create the status area.
    //
    sRect.i16XMin = 0;
    sRect.i16YMin = GrContextDpyHeightGet(&g_sContext) -
                  DISPLAY_BANNER_HEIGHT - 1;
    sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.i16YMax = sRect.i16YMin + DISPLAY_BANNER_HEIGHT;

    GrContextForegroundSet(&g_sContext, DISPLAY_BANNER_BG);
    GrRectFill(&g_sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&g_sContext, ClrWhite);
    GrRectDraw(&g_sContext, &sRect);

    //
    // Print a no device message a placeholder for the message printed
    // during an event.
    //
    GrStringDrawCentered(&g_sContext, "No Device", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2,
                         sRect.i16YMin + 5, 0);

    //
    // Configure SysTick for a 100Hz interrupt.
    //
    ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / TICKS_PER_SECOND);
    ROM_SysTickEnable();
    ROM_SysTickIntEnable();

    //
    // Enable Clocking to the USB controller.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);

    //
    // Configure the required pins for USB operation.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
    ROM_GPIOPinConfigure(GPIO_PG4_USB0EPEN);
    ROM_GPIOPinTypeUSBDigital(GPIO_PORTG_BASE, GPIO_PIN_4);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL);
    ROM_GPIOPinTypeUSBAnalog(GPIO_PORTL_BASE, GPIO_PIN_6 | GPIO_PIN_7);
    ROM_GPIOPinTypeUSBAnalog(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Initially wait for device connection.
    //
    g_eUSBState = STATE_NO_DEVICE;

    //
    // Initialize the USB stack mode and pass in a mode callback.
    //
    USBStackModeSet(0, eUSBModeOTG, ModeCallback);

    //
    // Register the host class drivers.
    //
    USBHCDRegisterDrivers(0, g_ppHostClassDrivers, g_ui32NumHostClassDrivers);

    //
    // Initialized the cursor.
    //
    g_ui32Buttons = 0;
    g_i32CursorX = 0;
    g_i32CursorY = 0;

    //
    // Open an instance of the mouse driver.  The mouse does not need
    // to be present at this time, this just saves a place for it and allows
    // the applications to be notified when a mouse is present.
    //
    g_psMouseInstance =
        USBHMouseOpen(MouseCallback, g_pui8Buffer, MOUSE_MEMORY_SIZE);

    //
    // Initialize the power configuration. This sets the power enable signal
    // to be active high and does not enable the power fault.
    //
    USBHCDPowerConfigInit(0, USBHCD_VBUS_AUTO_HIGH | USBHCD_VBUS_FILTER);

    //
    // Initialize the USB controller for OTG operation with a 2ms polling
    // rate.
    //
    USBOTGModeInit(0, 2000, g_pui8HCDPool, HCD_MEMORY_SIZE);

    //
    // The main loop for the application.
    //
    while(1)
    {
        //
        // Tell the OTG state machine how much time has passed in
        // milliseconds since the last call.
        //
        USBOTGMain(GetTickms());

        switch(g_eUSBState)
        {
            //
            // This state is entered when the mouse is first detected.
            //
            case STATE_MOUSE_INIT:
            {
                //
                // Initialize the newly connected mouse.
                //
                USBHMouseInit(g_psMouseInstance);

                //
                // Proceed to the mouse connected state.
                //
                g_eUSBState = STATE_MOUSE_CONNECTED;

                break;
            }

            case STATE_MOUSE_CONNECTED:
            {
                //
                // Nothing is currently done in the main loop when the mouse
                // is connected.
                //
                break;
            }

            case STATE_NO_DEVICE:
            {
                //
                // The mouse is not connected so nothing needs to be done here.
                //
                break;
            }

            default:
            {
                break;
            }
        }
    }
}
Esempio n. 29
0
//*****************************************************************************
//
// The main application loop.
//
//*****************************************************************************
int
main(void)
{
    int32_t i32Status, i32Idx;
    uint32_t ui32SysClock, ui32PLLRate;
#ifdef USE_ULPI
    uint32_t ui32Setting;
#endif

    ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                           SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
                                           SYSCTL_CFG_VCO_480), 120000000);

    //
    // Set the part pin out appropriately for this device.
    //
    PinoutSet();

#ifdef USE_ULPI
    //
    // Switch the USB ULPI Pins over.
    //
    USBULPIPinoutSet();

    //
    // Enable USB ULPI with high speed support.
    //
    ui32Setting = USBLIB_FEATURE_ULPI_HS;
    USBOTGFeatureSet(0, USBLIB_FEATURE_USBULPI, &ui32Setting);

    //
    // Setting the PLL frequency to zero tells the USB library to use the
    // external USB clock.
    //
    ui32PLLRate = 0;
#else
    //
    // Save the PLL rate used by this application.
    //
    ui32PLLRate = 480000000;
#endif

    //
    // Initialize the hub port status.
    //
    for(i32Idx = 0; i32Idx < NUM_HUB_STATUS; i32Idx++)
    {
        g_psHubStatus[i32Idx].bConnected = false;
    }

    //
    // Enable Clocking to the USB controller.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);

    //
    // Enable Interrupts
    //
    ROM_IntMasterEnable();

    //
    // Initialize the USB stack mode and pass in a mode callback.
    //
    USBStackModeSet(0, eUSBModeHost, 0);

    //
    // Register the host class drivers.
    //
    USBHCDRegisterDrivers(0, g_ppHostClassDrivers, g_ui32NumHostClassDrivers);

    //
    // Open the Keyboard interface.
    //
    KeyboardOpen();
    MSCOpen(ui32SysClock);

    //
    // Open a hub instance and provide it with the memory required to hold
    // configuration descriptors for each attached device.
    //
    USBHHubOpen(HubCallback);

    //
    // Initialize the power configuration. This sets the power enable signal
    // to be active high and does not enable the power fault.
    //
    USBHCDPowerConfigInit(0, USBHCD_VBUS_AUTO_HIGH | USBHCD_VBUS_FILTER);

    //
    // Tell the USB library the CPU clock and the PLL frequency.
    //
    USBOTGFeatureSet(0, USBLIB_FEATURE_CPUCLK, &ui32SysClock);
    USBOTGFeatureSet(0, USBLIB_FEATURE_USBPLL, &ui32PLLRate);

    //
    // Initialize the USB controller for Host mode.
    //
    USBHCDInit(0, g_pui8HCDPool, sizeof(g_pui8HCDPool));

    //
    // Initialize the display driver.
    //
    Kentec320x240x16_SSD2119Init(ui32SysClock);

    //
    // Initialize the graphics context.
    //
    GrContextInit(&g_sContext, &g_sKentec320x240x16_SSD2119);

    //
    // Draw the application frame.
    //
    FrameDraw(&g_sContext, "usb-host-hub");

    //
    // Calculate the number of characters that will fit on a line.
    // Make sure to leave a small border for the text box.
    //
    g_ui32CharsPerLine = (GrContextDpyWidthGet(&g_sContext) - 16) /
                         GrFontMaxWidthGet(g_psFontFixed6x8);

    //
    // Calculate the number of lines per usable text screen.  This requires
    // taking off space for the top and bottom banners and adding a small bit
    // for a border.
    //
    g_ui32LinesPerScreen = (GrContextDpyHeightGet(&g_sContext) -
                           (2*(DISPLAY_BANNER_HEIGHT + 1)))/
                           GrFontHeightGet(g_psFontFixed6x8);

    //
    // Initial update of the screen.
    //
    UpdateStatus(0);
    UpdateStatus(1);
    UpdateStatus(2);
    UpdateStatus(3);

    g_ui32CmdIdx = 0;
    g_ui32CurrentLine = 0;

    //
    // Initialize the file system.
    //
    FileInit();

    //
    // The main loop for the application.
    //
    while(1)
    {
        //
        // Print a prompt to the console.  Show the CWD.
        //
        WriteString("> ");

        //
        // Is there a command waiting to be processed?
        //
        while((g_ui32Flags & FLAG_CMD_READY) == 0)
        {
            //
            // Call the YSB library to let non-interrupt code run.
            //
            USBHCDMain();

            //
            // Call the keyboard and mass storage main routines.
            //
            KeyboardMain();
            MSCMain();
        }

        //
        // Pass the line from the user to the command processor.
        // It will be parsed and valid commands executed.
        //
        i32Status = CmdLineProcess(g_pcCmdBuf);

        //
        // Handle the case of bad command.
        //
        if(i32Status == CMDLINE_BAD_CMD)
        {
            WriteString("Bad command!\n");
        }
        //
        // Handle the case of too many arguments.
        //
        else if(i32Status == CMDLINE_TOO_MANY_ARGS)
        {
            WriteString("Too many arguments for command processor!\n");
        }
        //
        // Otherwise the command was executed.  Print the error
        // code if one was returned.
        //
        else if(i32Status != 0)
        {
            WriteString("Command returned error code\n");
            WriteString((char *)StringFromFresult((FRESULT)i32Status));
            WriteString("\n");
        }

        //
        // Reset the command flag and the command index.
        //
        g_ui32Flags &= ~FLAG_CMD_READY;
        g_ui32CmdIdx = 0;
    }
}
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
    tRectangle sRect;

    //
    // Set the clocking to run directly from the crystal.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Set the device pinout appropriately for this board.
    //
    PinoutSet();

#ifdef DEBUG
    //
    // Open UART0 for debug output.
    //
    UARTStdioInit(0);
#endif

    //
    // Set the system tick to fire 100 times per second.
    //
    ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / SYSTICKS_PER_SECOND);
    ROM_SysTickIntEnable();
    ROM_SysTickEnable();

    //
    // Initialize the display driver.
    //
    Kitronix320x240x16_SSD2119Init();

    //
    // Initialize the graphics context.
    //
    GrContextInit(&g_sContext, &g_sKitronix320x240x16_SSD2119);

    //
    // Fill the top 24 rows of the screen with blue to create the banner.
    //
    sRect.sXMin = 0;
    sRect.sYMin = 0;
    sRect.sXMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.sYMax = 23;
    GrContextForegroundSet(&g_sContext, ClrDarkBlue);
    GrRectFill(&g_sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&g_sContext, ClrWhite);
    GrRectDraw(&g_sContext, &sRect);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&g_sContext, g_pFontCm20);
    GrStringDrawCentered(&g_sContext, "boot-demo-usb", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 10, 0);

    //
    // Draw the buttons in their initial (unpressed)state.
    //
    UpdateDisplay(g_ucButtons, true);

    //
    // Initialize each of the device instances that will form our composite
    // USB device.
    //
    g_sCompDevice.psDevices[0].pvInstance =
        USBDHIDMouseCompositeInit(0, (tUSBDHIDMouseDevice *)&g_sMouseDevice);
    g_sCompDevice.psDevices[1].pvInstance =
        USBDDFUCompositeInit(0, (tUSBDDFUDevice *)&g_sDFUDevice);

    //
    // Pass the USB library our device information, initialize the USB
    // controller and connect the device to the bus.
    //
    USBDCompositeInit(0, &g_sCompDevice, DESCRIPTOR_BUFFER_SIZE,
                      g_pcDescriptorBuffer);

    //
    // Initialize the touch screen driver.
    //
    TouchScreenInit();

    //
    // Set the touch screen event handler.
    //
    TouchScreenCallbackSet(MouseTouchHandler);

    //
    // Drop into the main loop.
    //
    while(!g_bUpdateSignalled)
    {
        //
        // Tell the user what we are doing.
        //
        GrContextFontSet(&g_sContext, g_pFontCmss22b);
        GrContextForegroundSet(&g_sContext, ClrWhite);
        GrStringDrawCentered(&g_sContext, "   Waiting for host...   ", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 40, true);

        //
        // Wait for USB configuration to complete.
        //
        while(!g_bConnected)
        {
        }

        //
        // Update the status.
        //
        GrStringDrawCentered(&g_sContext, "   Host connected...   ", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 40, true);

        //
        // Now keep processing the mouse as long as the host is connected and
        // we've not been told to prepare for a firmware upgrade.
        //
        while(g_bConnected && !g_bUpdateSignalled)
        {
            //
            // If it is time to check the touchscreen state then do so.
            //
            if(g_ulCommands & TOUCH_TICK_EVENT)
            {
                g_ulCommands &= ~TOUCH_TICK_EVENT;
                TouchHandler();
            }
        }

        //
        // If we drop out of the previous loop, either the host has
        // disconnected or a firmware upgrade has been signalled.
        //
    }

    //
    // Tell the user what's going on.
    //
    GrContextFontSet(&g_sContext, g_pFontCmss22b);
    GrStringDrawCentered(&g_sContext, " Switching to DFU mode ", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 40, true);

    //
    // If we get here, a firmware upgrade has been signalled so we need to get
    // back into the boot loader to allow this to happen.  Call the USB DFU
    // device class to do this for us.  Note that this function never returns.
    //
    USBDDFUUpdateBegin();
}