Exemple #1
0
int main(void)
{
	// 
	// Device configuration: PLL is used, crystal of 16 MHz, main clock is the source clock
	// System clock divider = 4
	//
	ROM_SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	//
	// PF2 as output (blue led)
	// 
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, LED_RED|LED_BLUE|LED_GREEN);
	ButtonsInit(); // Init buttons

	// Loop
	while(1)
	{
		In = ROM_GPIOPinRead(BUTTONS_GPIO_BASE, RIGHT_BUTTON); // Read PORTF 0
		if (In==0){
			ROM_GPIOPinWrite(GPIO_PORTF_BASE, LED_BLUE, LED_BLUE); // Turn on LED BLUE
			ROM_GPIOPinWrite(GPIO_PORTF_BASE, LED_RED, 0);
		}
		else {
			ROM_GPIOPinWrite(GPIO_PORTF_BASE, LED_RED, LED_RED); // Turn on LED RED
			ROM_GPIOPinWrite(GPIO_PORTF_BASE, LED_BLUE, 0);
		}
	}
}
int main(void) {
	
	uint8_t ucState;
	uint8_t ucDelta;
	uint8_t ui8LedData=2;

	SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

	ButtonsInit();

	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);

	while(1){
		ucState = ButtonsPoll(&ucDelta,0);

		if(BUTTON_PRESSED(LEFT_BUTTON,ucState,ucDelta)){
			GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1,ui8LedData);
			ui8LedData^=1<<1;
		}

	}


	return 0;
}
Exemple #3
0
void confGPIO(){
	//Inicializa el puerto F (LEDs) --> No hace falta si usamos la libreria RGB
	 //   ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	 // ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
	//ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);	//LEDS APAGADOS

	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
	GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
	SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOE);



	//Inicializa los LEDs usando libreria RGB
	RGBInit(1);
	SysCtlPeripheralSleepEnable(GREEN_TIMER_PERIPH);
	SysCtlPeripheralSleepEnable(BLUE_TIMER_PERIPH);
	RGBEnable();

	//Inicializamos los botones y su interrupción
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	ButtonsInit();
	GPIOIntClear(GPIO_PORTF_BASE, GPIO_INT_PIN_0|GPIO_INT_PIN_4);
	GPIOIntRegister(GPIO_PORTF_BASE,ButtonHandler);
	GPIOIntTypeSet(GPIO_PORTF_BASE,GPIO_INT_PIN_0|GPIO_INT_PIN_4, GPIO_BOTH_EDGES);
	GPIOIntEnable(GPIO_PORTF_BASE, GPIO_INT_PIN_0|GPIO_INT_PIN_4);
	SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOF);


}
//*****************************************************************************
//
// Initializes the switch task.
//
//*****************************************************************************
uint32_t
SwitchTaskInit(void)
{
    //
    // Unlock the GPIO LOCK register for Right button to work.
    //
    HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
    HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0xFF;

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

    //
    // Create the switch task.
    //
    if(xTaskCreate(SwitchTask, (signed portCHAR *)"Switch",
                   SWITCHTASKSTACKSIZE, NULL, tskIDLE_PRIORITY +
                   PRIORITY_SWITCH_TASK, NULL) != pdTRUE)
    {
        return(1);
    }

    //
    // Success.
    //
    return(0);
}
int main(void)
{
  // TivaC application specific code
  MAP_FPUEnable();
  MAP_FPULazyStackingEnable();
  // TivaC system clock configuration. Set to 80MHz.
  MAP_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);

  uint8_t button_debounced_delta;
  uint8_t button_raw_state;
  ButtonsInit();

  // ROS nodehandle initialization and topic registration
  nh.initNode();
  nh.advertise(button_publisher);

  while (1)
  {
    uint8_t button_debounced_state = ButtonsPoll(&button_debounced_delta, &button_raw_state);
    // Publish message to be transmitted.
    button_msg.sw1.data = button_debounced_state & LEFT_BUTTON;
    button_msg.sw2.data = button_debounced_state & RIGHT_BUTTON;
    button_publisher.publish(&button_msg);

    // Handle all communications and callbacks.
    nh.spinOnce();

    // Delay for a bit.
    nh.getHardware()->delay(100);
  }
}
Exemple #6
0
void main(void) {
		Init_Device();
		#if WITH_TASKER
		InitTasker();
		led_task = AddTask(&LedToggle,1000,INFINITE_TASK);
		#endif
		#if WITH_BINDER
		InitBinder();
		#endif
		P0 &= (~0x80);
		#if WITH_COMM_LINK
		StartCommLinkCommunication(0x26);
		#endif

		#if WITH_Z_UNO_CHANNEL_DEBUG
		{
			BYTE result = setupUserChannels();
		}
		#endif
				
		#if WITH_LCD
		LCD_Init_VIM878();
		#endif

		#if WITH_BUTTONS
		ButtonsInit();
		AddTask(ButtonsPoll, 10, INFINITE_TASK);
		#endif

		while(TRUE) {
			#if WITH_Z_UNO_CODE
			ApplicationPoll();
			#endif
			#if WITH_UART0
			CheckUART();
			#endif
			#if WITH_SODA_UART
			checkSodaUart();
			#endif
			#if WITH_BUTTONS
			ParseButtonEvents();
			ClearButtonEvents();
			#endif
			#if WITH_TASKER
			if (tasker_flag == TRUE) {
				tasker_flag = FALSE;
				Tasker();
			}
			#endif
			#if WITH_BINDER
			EventBinder();
			#endif
			#if WITH_GENERAL_TESTING_FILE
			generalTestingRoutine();
			#endif
		}
}
Exemple #7
0
void Keyboard_Handler(void)
{
   // task setup
   INT8U  key      = NO_KEY;
   INT32U read = 0;

   ButtonsInit();

   if (OSSemCreate(0,&sKeyboard) != ALLOC_EVENT_OK)
   {
     // Oh Oh
     // Não deveria entrar aqui !!!
     while(1){};
   };

   if (OSQueueCreate(64, &qKeyboard) != ALLOC_EVENT_OK)
   {
     // Oh Oh
     // Não deveria entrar aqui !!!
     while(1){};
   };

   // task main loop
   for (;;)
   {
      // Wait for a keyboard interrupt
      OSSemPend (sKeyboard,0);
      DelayTask(50);

      read = GPIOPinRead(BUTTONS_GPIO_BASE, ALL_BUTTONS);

      // Find out which key was pressed
      key = (INT8U)read;

      // Copy the key to the keyboard buffer
      if(key != NO_KEY)
      {
    	if (OSQueuePost(qKeyboard, key) == BUFFER_UNDERRUN)
        {
          // Buffer overflow
          OSQueueClean(qKeyboard);
        }
      }

      key = NO_KEY;
      DelayTask(200);
      // Enable interrupt to the next key detection
      GPIOIntEnable(BUTTONS_GPIO_BASE, ALL_BUTTONS);
   }
}
/****************************************************************************
NAME 
 	buttonManagerInit
*/  
void buttonManagerInit ( void ) 
{   	
			/*put the buttons task and the button patterens in a single memory block*/
	int lSize = sizeof(ButtonsTaskData) ; 
		
	/*allocate the memory*/
	theSink.theButtonsTask = mallocPanic( lSize );

    /* initialise structure */    
    memset(theSink.theButtonsTask, 0, lSize);  
	
    theSink.theButtonsTask->client = &theSink.task;
 
	/*create the array of Button Events that we are going to populate*/   
    BM_DEBUG(("BM: ButtonEvents block size [%u]\n" ,  sizeof( ButtonEvents_t ) * BM_EVENTS_PER_CONF_BLOCK ));
    theSink.theButtonsTask->gButtonEvents[0] = (ButtonEvents_t * ) ( mallocPanic( sizeof( ButtonEvents_t ) * BM_EVENTS_PER_CONF_BLOCK ) ) ;
    theSink.theButtonsTask->gButtonEvents[1]= (ButtonEvents_t * ) ( mallocPanic( sizeof( ButtonEvents_t ) * BM_EVENTS_PER_CONF_BLOCK ) ) ;
    
      /*init the PIO button routines with the Button manager Task data */ 
    ButtonsInit( theSink.theButtonsTask ) ; 
}
Exemple #9
0
int main(void)
{
    DDRC = 0x00;//PORTC на вход
    PORTC = 0x3;


    ButtonsInit();
    ButtonsSet(&PINC, PIN0, 1, &OnClick0);
    ButtonsSet(&PINC, PIN1, 1, &OnClick1);
    ButtonsActivate();

    serialInit(SPEED9600);
    sei();


    serialWrite("Hello! AVR Mega16 Boot OK\n");
    while(1)
    {
    }

    return 0;
}
Exemple #10
0
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
    uint8_t ui8ButtonsChanged, ui8Buttons;
    bool bUpdate;

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

    //
    // Enable the GPIO port that is used for the on-board LED.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    //
    // Enable the GPIO pin for the Blue LED (PF2).
    //
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);

    //
    // Open UART0 and show the application name on the UART.
    //
    ConfigureUART();

    UARTprintf("\033[2JTiva C Series USB gamepad device example\n");
    UARTprintf("---------------------------------\n\n");

    //
    // Not configured initially.
    //
    g_iGamepadState = eStateNotConfigured;

    //
    // Enable the GPIO peripheral used for USB, and configure the USB
    // pins.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    SysCtlGPIOAHBEnable(SYSCTL_PERIPH_GPIOD);
    ROM_GPIOPinTypeUSBAnalog(GPIO_PORTD_AHB_BASE, GPIO_PIN_4 | GPIO_PIN_5);

    //
    // Configure the GPIOS for the buttons.
    //
    ButtonsInit();

    //
    // Initialize the ADC channels.
    //
    ADCInit();

    //
    // Tell the user what we are up to.
    //
    UARTprintf("Configuring USB\n");

    //
    // Set the USB stack mode to Device mode.
    //
    USBStackModeSet(0, eUSBModeForceDevice, 0);

    //
    // Pass the device information to the USB library and place the device
    // on the bus.
    //
    USBDHIDGamepadInit(0, &g_sGamepadDevice);

    //
    // Zero out the initial report.
    //
    sReport.ui8Buttons = 0;
    sReport.i8XPos = 0;
    sReport.i8YPos = 0;
    sReport.i8ZPos = 0;

    //
    // Tell the user what we are doing and provide some basic instructions.
    //
    UARTprintf("\nWaiting For Host...\n");

    //
    // Trigger an initial ADC sequence.
    //
    ADCProcessorTrigger(ADC0_BASE, 0);

    //
    // The main loop starts here.  We begin by waiting for a host connection
    // then drop into the main gamepad handling section.  If the host
    // disconnects, we return to the top and wait for a new connection.
    //
    while(1)
    {
        //
        // Wait here until USB device is connected to a host.
        //
        if(g_iGamepadState == eStateIdle)
        {
            //
            // No update by default.
            //
            bUpdate = false;

            //
            // See if the buttons updated.
            //
            ButtonsPoll(&ui8ButtonsChanged, &ui8Buttons);

            sReport.ui8Buttons = 0;

            //
            // Set button 1 if left pressed.
            //
            if(ui8Buttons & LEFT_BUTTON)
            {
                sReport.ui8Buttons |= 0x01;
            }

            //
            // Set button 2 if right pressed.
            //
            if(ui8Buttons & RIGHT_BUTTON)
            {
                sReport.ui8Buttons |= 0x02;
            }

            if(ui8ButtonsChanged)
            {
                bUpdate = true;
            }

            //
            // See if the ADC updated.
            //
            if(ADCIntStatus(ADC0_BASE, 0, false) != 0)
            {
                //
                // Clear the ADC interrupt.
                //
                ADCIntClear(ADC0_BASE, 0);

                //
                // Read the data and trigger a new sample request.
                //
                ADCSequenceDataGet(ADC0_BASE, 0, &g_pui32ADCData[0]);
                ADCProcessorTrigger(ADC0_BASE, 0);

                //
                // Update the report.
                //
                sReport.i8XPos = Convert8Bit(g_pui32ADCData[0]);
                sReport.i8YPos = Convert8Bit(g_pui32ADCData[1]);
                sReport.i8ZPos = Convert8Bit(g_pui32ADCData[2]);
                bUpdate = true;
            }

            //
            // Send the report if there was an update.
            //
            if(bUpdate)
            {
                USBDHIDGamepadSendReport(&g_sGamepadDevice, &sReport,
                                         sizeof(sReport));

                //
                // Now sending data but protect this from an interrupt since
                // it can change in interrupt context as well.
                //
                IntMasterDisable();
                g_iGamepadState = eStateSending;
                IntMasterEnable();

                //
                // Limit the blink rate of the LED.
                //
                if(g_ui32Updates++ == 40)
                {
                    //
                    // Turn on the blue LED.
                    //
                    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);

                    //
                    // Reset the update count.
                    //
                    g_ui32Updates = 0;
                }
            }
        }
    }
}
int main(void) {
    // Enable FPU for interrupt routines
    // ROM_FPULazyStackingEnable();
    // ROM_FPUEnable();

    // Set clock to 80MHz
    SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

    // Turn off LEDs
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2|GPIO_PIN_3, 0);

    // Setup buttons
    ButtonsInit();

    // Initialize the UART.
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
    ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTStdioInit(0);

    // Setup servos and start the timer for them
    setupServos();

    uint16_t servoPosition = SERVO_MIN_PULSE;

    // Continually check which button is pressed and move the servo position that direction
    while(1) {
        switch(ButtonsPoll(0, 0) & ALL_BUTTONS) {
            case LEFT_BUTTON:
                servoPosition -= 10;

                if(servoPosition < SERVO_MIN_PULSE) {
                    servoPosition = SERVO_MIN_PULSE;

                    // Too far, blink red
                    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_1);
                } else {
                    // Valid position, blink green
                    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_2);
                }
                break;

            case RIGHT_BUTTON:
                servoPosition += 10;

                if(servoPosition > SERVO_MAX_PULSE) {
                    servoPosition = SERVO_MAX_PULSE;
                    
                    // Too far, blink red
                    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_1);
                } else {
                    // Valid position, blink blue
                    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_3);
                }
                break;
        }

        // Update servo positions
        servoSet(servo, servoPosition);
        servoSet(servo2, servoPosition);

        // Wait .002s
        SysCtlDelay((SysCtlClockGet() / 3) / 500);

        // Turn off all LEDs
        ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
    }
}
Exemple #12
0
//*****************************************************************************
//
// Toggle the JTAG pins between JTAG and GPIO mode with a push button selecting
// between the two.
//
//*****************************************************************************
int
main(void)
{
    uint32_t ui32Mode;

    //
    // Set the clocking to run directly from the crystal at 120MHz.
    //
    g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                            SYSCTL_OSC_MAIN |
                                            SYSCTL_USE_PLL |
                                            SYSCTL_CFG_VCO_480), 120000000);
    //
    // Enable the peripherals used by this application.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);


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

    //
    // Set up a SysTick Interrupt to handle polling and debouncing for our
    // buttons.
    //
    SysTickPeriodSet(g_ui32SysClock / 100);
    SysTickIntEnable();
    SysTickEnable();

    IntMasterEnable();

    //
    // Configure the LEDs as outputs and turn them on in the JTAG state.
    //
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_PIN_0);

    //
    // Set the global and local indicator of pin mode to zero, meaning JTAG.
    //
    g_ui32Mode = 0;
    ui32Mode = 0;

    //
    // Initialize the UART, clear the terminal, print banner.
    //
    ConfigureUART();
    UARTprintf("\033[2J\033[H");
    UARTprintf("GPIO <-> JTAG\n");

    //
    // Indicate that the pins start out as JTAG.
    //
    UARTprintf("Pins are JTAG\n");

    //
    // Loop forever.  This loop simply exists to display on the UART the
    // current state of PC0-3; the handling of changing the JTAG pins to and
    // from GPIO mode is done in GPIO Interrupt Handler.
    //
    while(1)
    {
        //
        // Wait until the pin mode changes.
        //
        while(g_ui32Mode == ui32Mode)
        {
        }

        //
        // Save the new mode locally so that a subsequent pin mode change can
        // be detected.
        //
        ui32Mode = g_ui32Mode;

        //
        // See what the new pin mode was changed to.
        //
        if(ui32Mode == 0)
        {
            //
            // Indicate that PC0-3 are currently JTAG pins.
            //
            UARTprintf("Pins are JTAG\n");
        }
        else
        {
            //
            // Indicate that PC0-3 are currently GPIO pins.
            //
            UARTprintf("Pins are GPIO\n");
        }
    }
}
//*****************************************************************************
//
// The program main function.  It performs initialization, then runs a loop to
// process USB activities and operate the user interface.
//
//*****************************************************************************
int
main(void)
{
    uint32_t ui32DriveTimeout;

    //
    // 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);

    //
    // 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);

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

    //
    // Enable the uDMA controller and set up the control table base.
    // The uDMA controller is used by the USB library.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);
    ROM_uDMAEnable();
    ROM_uDMAControlBaseSet(g_psDMAControlTable);

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

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

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

    //
    // Initialize two offscreen displays and assign the palette.  These
    // buffers are used by the slide menu widget to allow animation effects.
    //
    GrOffScreen4BPPInit(&g_sOffscreenDisplayA, g_pui8OffscreenBufA, 96, 64);
    GrOffScreen4BPPPaletteSet(&g_sOffscreenDisplayA, g_pui32Palette, 0,
                              NUM_PALETTE_ENTRIES);
    GrOffScreen4BPPInit(&g_sOffscreenDisplayB, g_pui8OffscreenBufB, 96, 64);
    GrOffScreen4BPPPaletteSet(&g_sOffscreenDisplayB, g_pui32Palette, 0,
                              NUM_PALETTE_ENTRIES);

    //
    // Show an initial status screen
    //
    g_pcStatusLines[0] = "Waiting";
    g_pcStatusLines[1] = "for device";
    ShowStatusScreen(g_pcStatusLines, 2);

    //
    // Add the compile-time defined widgets to the widget tree.
    //
    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sFileMenuWidget);

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

    //
    // Initialize the USB stack for host mode.
    //
    USBStackModeSet(0, eUSBModeHost, 0);

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

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

    //
    // Initialize the drive timeout.
    //
    ui32DriveTimeout = USBMSC_DRIVE_RETRY;

    //
    // 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 host operation.
    //
    USBHCDInit(0, g_pui8HCDPool, HCD_MEMORY_SIZE);

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

    //
    // Enter an infinite loop to run the user interface and process USB
    // events.
    //
    while(1)
    {
        uint32_t ui32LastTickCount = 0;

        //
        // Call the USB stack to keep it running.
        //
        USBHCDMain();

        //
        // Process any messages in the widget message queue.  This keeps the
        // display UI running.
        //
        WidgetMessageQueueProcess();

        //
        // Take action based on the application state.
        //
        switch(g_eState)
        {
            //
            // A device has enumerated.
            //
            case STATE_DEVICE_ENUM:
            {
                //
                // Check to see if the device is ready.  If not then stay
                // in this state and we will check it again on the next pass.
                //
                if(USBHMSCDriveReady(g_psMSCInstance) != 0)
                {
                    //
                    // Wait about 500ms before attempting to check if the
                    // device is ready again.
                    //
                    ROM_SysCtlDelay(ROM_SysCtlClockGet()/(3));

                    //
                    // Decrement the retry count.
                    //
                    ui32DriveTimeout--;

                    //
                    // If the timeout is hit then go to the
                    // STATE_TIMEOUT_DEVICE state.
                    //
                    if(ui32DriveTimeout == 0)
                    {
                        g_eState = STATE_TIMEOUT_DEVICE;
                    }

                    break;
                }

                //
                // Getting here means the device is ready.
                // Reset the CWD to the root directory.
                //
                g_pcCwdBuf[0] = '/';
                g_pcCwdBuf[1] = 0;

                //
                // Set the initial directory level to the root
                //
                g_ui32Level = 0;

                //
                // We need to reset the indexes of the root menu to 0, so that
                // it will start at the top of the file list, and reset the
                // slide menu widget to start with the root menu.
                //
                g_psFileMenus[g_ui32Level].ui32CenterIndex = 0;
                g_psFileMenus[g_ui32Level].ui32FocusIndex = 0;
                SlideMenuMenuSet(&g_sFileMenuWidget, &g_psFileMenus[g_ui32Level]);

                //
                // Initiate a directory change to the root.  This will
                // populate a menu structure representing the root directory.
                //
                if(ProcessDirChange("/", g_ui32Level))
                {
                    //
                    // If there were no errors reported, we are ready for
                    // MSC operation.
                    //
                    g_eState = STATE_DEVICE_READY;

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

                    //
                    // Request a repaint so the file menu will be shown
                    //
                    WidgetPaint(WIDGET_ROOT);
                }

                break;
            }

            //
            // If there is no device then just wait for one.
            //
            case STATE_NO_DEVICE:
            {
                if(g_ui32Flags == FLAGS_DEVICE_PRESENT)
                {
                    //
                    // Show waiting message on screen
                    //
                    g_pcStatusLines[0] = "Waiting";
                    g_pcStatusLines[1] = "for device";
                    ShowStatusScreen(g_pcStatusLines, 2);

                    //
                    // Clear the Device Present flag.
                    //
                    g_ui32Flags &= ~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_ui32Flags & FLAGS_DEVICE_PRESENT) == 0)
                {
                    //
                    // Clear the screen and indicate that an unknown device
                    // is present.
                    //
                    g_pcStatusLines[0] = "Unknown";
                    g_pcStatusLines[1] = "device";
                    ShowStatusScreen(g_pcStatusLines, 2);
                }

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

                break;
            }

            //
            // The connected mass storage device is not reporting ready.
            //
            case STATE_TIMEOUT_DEVICE:
            {
                //
                // If this is the first time in this state then print a
                // message.
                //
                if((g_ui32Flags & FLAGS_DEVICE_PRESENT) == 0)
                {
                    //
                    //
                    // Clear the screen and indicate that an unknown device
                    // is present.
                    //
                    g_pcStatusLines[0] = "Device";
                    g_pcStatusLines[1] = "Timeout";
                    ShowStatusScreen(g_pcStatusLines, 2);
                }

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

                break;
            }

            //
            // The device is ready and in use.
            //
            case STATE_DEVICE_READY:
            {
                //
                // Process occurrence of timer tick.  Check for user input
                // once each tick.
                //
                if(g_ui32SysTickCount != ui32LastTickCount)
                {
                    uint8_t ui8ButtonState;
                    uint8_t ui8ButtonChanged;

                    ui32LastTickCount = g_ui32SysTickCount;

                    //
                    // Get the current debounced state of the buttons.
                    //
                    ui8ButtonState = ButtonsPoll(&ui8ButtonChanged, 0);

                    //
                    // If select button or right button is pressed, then we
                    // are trying to descend into another directory
                    //
                    if(BUTTON_PRESSED(SELECT_BUTTON,
                                      ui8ButtonState, ui8ButtonChanged) ||
                       BUTTON_PRESSED(RIGHT_BUTTON,
                                      ui8ButtonState, ui8ButtonChanged))
                    {
                        uint32_t ui32NewLevel;
                        uint32_t ui32ItemIdx;
                        char *pcItemName;

                        //
                        // Get a pointer to the current menu for this CWD.
                        //
                        tSlideMenu *psMenu = &g_psFileMenus[g_ui32Level];

                        //
                        // Get the highlighted index in the current file list.
                        // This is the currently highlighted file or dir
                        // on the display.  Then get the name of the file at
                        // this index.
                        //
                        ui32ItemIdx = SlideMenuFocusItemGet(psMenu);
                        pcItemName = psMenu->psSlideMenuItems[ui32ItemIdx].pcText;

                        //
                        // Make sure we are not yet past the maximum tree
                        // depth.
                        //
                        if(g_ui32Level < MAX_SUBDIR_DEPTH)
                        {
                            //
                            // Potential new level is one greater than the
                            // current level.
                            //
                            ui32NewLevel = g_ui32Level + 1;

                            //
                            // Process the directory change to the new
                            // directory.  This function will populate a menu
                            // structure with the files and subdirs in the new
                            // directory.
                            //
                            if(ProcessDirChange(pcItemName, ui32NewLevel))
                            {
                                //
                                // If the change was successful, then update
                                // the level.
                                //
                                g_ui32Level = ui32NewLevel;

                                //
                                // Now that all the prep is done, send the
                                // KEY_RIGHT message to the widget and it will
                                // "slide" from the previous file list to the
                                // new file list of the CWD.
                                //
                                SendWidgetKeyMessage(WIDGET_MSG_KEY_RIGHT);

                            }
                        }
                    }

                    //
                    // If the UP button is pressed, just pass it to the widget
                    // which will handle scrolling the list of files.
                    //
                    if(BUTTON_PRESSED(UP_BUTTON, ui8ButtonState, ui8ButtonChanged))
                    {
                        SendWidgetKeyMessage(WIDGET_MSG_KEY_UP);
                    }

                    //
                    // If the DOWN button is pressed, just pass it to the widget
                    // which will handle scrolling the list of files.
                    //
                    if(BUTTON_PRESSED(DOWN_BUTTON, ui8ButtonState, ui8ButtonChanged))
                    {
                        SendWidgetKeyMessage(WIDGET_MSG_KEY_DOWN);
                    }

                    //
                    // If the LEFT button is pressed, then we are attempting
                    // to go up a level in the file system.
                    //
                    if(BUTTON_PRESSED(LEFT_BUTTON, ui8ButtonState, ui8ButtonChanged))
                    {
                        uint32_t ui32NewLevel;

                        //
                        // Make sure we are not already at the top of the
                        // directory tree (at root).
                        //
                        if(g_ui32Level)
                        {
                            //
                            // Potential new level is one less than the
                            // current level.
                            //
                            ui32NewLevel = g_ui32Level - 1;

                            //
                            // Process the directory change to the new
                            // directory.  This function will populate a menu
                            // structure with the files and subdirs in the new
                            // directory.
                            //
                            if(ProcessDirChange("..", ui32NewLevel))
                            {
                                //
                                // If the change was successful, then update
                                // the level.
                                //
                                g_ui32Level = ui32NewLevel;

                                //
                                // Now that all the prep is done, send the
                                // KEY_LEFT message to the widget and it will
                                // "slide" from the previous file list to the
                                // new file list of the CWD.
                                //
                                SendWidgetKeyMessage(WIDGET_MSG_KEY_LEFT);
                            }
                        }
                    }
                }
                break;
            }
            //
            // Something has caused a power fault.
            //
            case STATE_POWER_FAULT:
            {
                //
                // Clear the screen and show a power fault indication.
                //
                g_pcStatusLines[0] = "Power";
                g_pcStatusLines[1] = "fault";
                ShowStatusScreen(g_pcStatusLines, 2);
                break;
            }

            default:
            {
                break;
            }
        }
    }
}
Exemple #14
0
int
main(void)
{
	char cThisChar;
			/* Result code */

	unsigned long ulResetCause;

	//SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
	SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
				SYSCTL_XTAL_16MHZ);
	unsigned long g=SysCtlClockGet();

	FPUEnable();
	FPUStackingEnable();
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
	ulResetCause = SysCtlResetCauseGet();
	SysCtlResetCauseClear(ulResetCause);
	HibernateEnableExpClk(SysCtlClockGet());
	ButtonsInit();
	SysTickPeriodSet(SysCtlClockGet() / APP_SYSTICKS_PER_SEC);
	SysTickEnable();
	SysTickIntEnable();
	IntMasterEnable();

	SysCtlPeripheralEnable(SYSCTL_PERIPH_UART4);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
	GPIOPinConfigure(GPIO_PC4_U4RX);
	GPIOPinConfigure(GPIO_PC5_U4TX);
	GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);
	UARTConfigSetExpClk(UART4_BASE, SysCtlClockGet(), 115200,
			(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
					UART_CONFIG_PAR_NONE));

	SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
	GPIOPinConfigure(GPIO_PB0_U1RX);
	GPIOPinConfigure(GPIO_PB1_U1TX);

	GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);

	UARTConfigSetExpClk(UART1_BASE, SysCtlClockGet(), 9600, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
						UART_CONFIG_PAR_NONE));

	//Orden al PGS de que me devuelva solo un mensaje..
	for(i=0; i<sizeof(buferA); i++){
			UARTCharPut(UART1_BASE, buferA[i]);}

	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);

	rc = f_mount(0, &Fatfs);					//registra un area de trabajo
	rc = f_open(&Fil, "BuffGPS.TXT", FA_WRITE | FA_CREATE_ALWAYS);	//abre o crea un archivo


	do {


    int contador1=0;
    cThisChar='0';

    	do{

    	cThisChar=UARTCharGet(UART1_BASE);
		BuffGPS[contador1]=cThisChar;
		contador1=contador1+1;

    	} while((cThisChar != '\n'));

    	cThisChar='0';

		for(i=0; i<sizeof(cuaternion); i++){
			UARTCharPut(UART4_BASE, cuaternion[i]);}

				do{

		    	cThisChar=UARTCharGet(UART4_BASE);
		    	BuffGPS[contador1]=cThisChar;
				contador1=contador1+1;

					} while((cThisChar != '\n'));

		rc = f_write(&Fil, &BuffGPS, contador1, &bwGPS);
		rc = f_sync(&Fil);
		contador1=0;

		//while(UARTCharsAvail(UART1_BASE)==false){;}
	}

	while(1);
}
Exemple #15
0
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
    uint_fast32_t ui32LastTickCount;
    bool bLastSuspend;
    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 for this board.
    //
    PinoutSet(false, true);

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

    //
    // Enable UART0
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

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

    //
    // Not configured initially.
    //
    g_bConnected = false;
    g_bSuspended = false;
    bLastSuspend = false;

    //
    // Initialize the USB stack for device mode.
    //
    USBStackModeSet(0, eUSBModeDevice, 0);

    //
    // Pass our device information to the USB HID device class driver,
    // initialize the USB controller and connect the device to the bus.
    //
    USBDHIDKeyboardInit(0, &g_sKeyboardDevice);

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

    //
    // Initial Message
    //
    UARTprintf("\033[2J\033[H\n");
    UARTprintf("******************************\n");
    UARTprintf("*      usb-dev-keyboard      *\n");
    UARTprintf("******************************\n");

    //
    // The main loop starts here.  We begin by waiting for a host connection
    // then drop into the main keyboard handling section.  If the host
    // disconnects, we return to the top and wait for a new connection.
    //
    while(1) {
        uint8_t ui8Buttons;
        uint8_t ui8ButtonsChanged;

        //
        // Tell the user what we are doing and provide some basic instructions.
        //
        UARTprintf("\nWaiting For Host...\n");

        //
        // Wait here until USB device is connected to a host.
        //
        while(!g_bConnected) {
        }

        //
        // Update the status.
        //
        UARTprintf("\nHost Connected...\n");

        //
        // Enter the idle state.
        //
        g_eKeyboardState = STATE_IDLE;

        //
        // Assume that the bus is not currently suspended if we have just been
        // configured.
        //
        bLastSuspend = false;

        //
        // Keep transferring characters from the UART to the USB host for as
        // long as we are connected to the host.
        //
        while(g_bConnected) {
            //
            // Remember the current time.
            //
            ui32LastTickCount = g_ui32SysTickCount;

            //
            // Has the suspend state changed since last time we checked?
            //
            if(bLastSuspend != g_bSuspended) {
                //
                // Yes - the state changed. Print state to terminal.
                //
                bLastSuspend = g_bSuspended;
                if(bLastSuspend) {
                    UARTprintf("\nBus Suspended ... \n");
                } else {
                    UARTprintf("\nHost Connected ... \n");
                }
            }

            //
            // See if the button was just pressed.
            //
            ui8Buttons = ButtonsPoll(&ui8ButtonsChanged, 0);
            if(BUTTON_PRESSED(LEFT_BUTTON | RIGHT_BUTTON, ui8Buttons,
                              ui8ButtonsChanged)) {
                //
                // If the bus is suspended then resume it.  Otherwise, type
                // some "random" characters.
                //
                if(g_bSuspended) {
                    USBDHIDKeyboardRemoteWakeupRequest(
                        (void *)&g_sKeyboardDevice);
                } else {
                    SendString("Make the Switch to TI Microcontrollers!");
                }
            }

            //
            // Wait for at least 1 system tick to have gone by before we poll
            // the buttons again.
            //
            while(g_ui32SysTickCount == ui32LastTickCount) {
            }
        }
    }
}
//*****************************************************************************
//
// 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;
            }
        }
    }
}
Exemple #17
0
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
    //
    // Turn on stacking of FPU registers if FPU is used in the ISR.
    //
    FPULazyStackingEnable();

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

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

    //
    // Enable the Debug UART.
    //
    ConfigureUART();

    //
    // Print the welcome message to the terminal.
    //
    UARTprintf("\033[2JAir Mouse Application\n");

    //
    // Configure desired interrupt priorities. This makes certain that the DCM
    // is fed data at a consistent rate. Lower numbers equal higher priority.
    //
    ROM_IntPrioritySet(INT_I2C3, 0x00);
    ROM_IntPrioritySet(INT_GPIOB, 0x10);
    ROM_IntPrioritySet(FAULT_SYSTICK, 0x20);
    ROM_IntPrioritySet(INT_UART1, 0x60);
    ROM_IntPrioritySet(INT_UART0, 0x70);
    ROM_IntPrioritySet(INT_WTIMER5B, 0x80);

    //
    // Configure the USB D+ and D- pins.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    ROM_GPIOPinTypeUSBAnalog(GPIO_PORTD_BASE, GPIO_PIN_5 | GPIO_PIN_4);

    //
    // Pass the USB library our device information, initialize the USB
    // controller and connect the device to the bus.
    //
    USBDHIDMouseCompositeInit(0, &g_sMouseDevice, &g_psCompDevices[0]);
    USBDHIDKeyboardCompositeInit(0, &g_sKeyboardDevice, &g_psCompDevices[1]);

    //
    // Set the USB stack mode to Force Device mode.
    //
    USBStackModeSet(0, eUSBModeForceDevice, 0);

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

    //
    // User Interface Init
    //
    ButtonsInit();
    RGBInit(0);
    RGBEnable();

    //
    // Initialize the motion sub system.
    //
    MotionInit();

    //
    // Initialize the Radio Systems.
    //
    LPRFInit();

    //
    // Drop into the main loop.
    //
    while(1)
    {

        //
        // Check for and handle timer tick events.
        //
        if(HWREGBITW(&g_ui32Events, USB_TICK_EVENT) == 1)
        {
            //
            // Clear the Tick event flag. Set in SysTick interrupt handler.
            //
            HWREGBITW(&g_ui32Events, USB_TICK_EVENT) = 0;

            //
            // Each tick period handle wired mouse and keyboard.
            //
            if(HWREGBITW(&g_ui32USBFlags, FLAG_CONNECTED) == 1)
            {
                MouseMoveHandler();
                KeyboardMain();
            }
        }

        //
        // Check for LPRF tick events.  LPRF Ticks are slower since UART to
        // RNP is much slower data connection than the USB.
        //
        if(HWREGBITW(&g_ui32Events, LPRF_TICK_EVENT) == 1)
        {
            //
            // Clear the event flag.
            //
            HWREGBITW(&g_ui32Events, LPRF_TICK_EVENT) = 0;

            //
            // Perform the LPRF Main task handling
            //
            LPRFMain();

        }

        //
        // Check for and handle motion events.
        //
        if((HWREGBITW(&g_ui32Events, MOTION_EVENT) == 1) ||
           (HWREGBITW(&g_ui32Events, MOTION_ERROR_EVENT) == 1))
        {
            //
            // Clear the motion event flag. Set in the Motion I2C interrupt
            // handler when an I2C transaction to get sensor data is complete.
            //
            HWREGBITW(&g_ui32Events, MOTION_EVENT) = 0;

            //
            // Process the motion data that has been captured
            //
            MotionMain();
        }
    }
}
Exemple #18
0
int main(void) {
	unsigned char button_val;
	unsigned char Delta;
	unsigned char Rawstate;

	//
	// Setup the system clock to run at 80 Mhz from PLL with crystal reference
	//
	SysCtlClockSet(
			SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN
					| SYSCTL_XTAL_16MHZ);

	//
	// Enable and configure the GPIO port for the LED operation.
	//
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	GPIOPinTypeGPIOOutput(LED, RED_LED | BLUE_LED | GREEN_LED);
	GPIOPinWrite(LED, RED_LED | BLUE_LED | GREEN_LED, 0x00);

	Blink(GREEN_LED | BLUE_LED, 5, 50);

	ButtonsInit();
	UARTInit();

	while (1) {
		SysCtlDelay(100000);

		button_val = ButtonsPoll(&Delta, &Rawstate);

		if (BUTTON_PRESSED(LEFT_BUTTON,button_val,Delta)) {
			if (SendAndCheck("AT", 1)) {
				Blink(BLUE_LED, 1, 300);
			} else {
				Blink(RED_LED, 1, 300);
			}
		}

		if (BUTTON_PRESSED(RIGHT_BUTTON,button_val,Delta)) {
			break;
		}
	}

	Blink(GREEN_LED, 1, 1000);

	ADCInit();
	InitSonar();
	SimInit();

	Blink(BLUE_LED, 1, 1000);

	//
	// Loop Forever
	//
	while (1) {
		SysCtlDelay(100000);

		button_val = ButtonsPoll(&Delta, &Rawstate);

		if (BUTTON_PRESSED(LEFT_BUTTON,button_val,Delta)) {
			GPIOPinWrite(LED, RED_LED | BLUE_LED | GREEN_LED, GREEN_LED);
			max_distance = GetDistanceSonar();
			GPIOPinWrite(LED, RED_LED | BLUE_LED | GREEN_LED, 0x00);

			if (SendAndCheck("AT", 1)) {
				Blink(BLUE_LED, 1, 300);
			} else {
				Blink(RED_LED, 1, 300);
			}
		}

		if (BUTTON_PRESSED(RIGHT_BUTTON,button_val,Delta)) {
			isRunning = !isRunning;
		}

		if (isRunning) {
			tempC = GetTemperature();
			if (tempC > 70) {
				SendData(DATA_TEMP, tempC);
				Blink(RED_LED, 3, 100);
			}

			distance = GetDistanceSonar();
			percentage = (int) floor(
					((max_distance - distance) * 100.0) / max_distance);
			if ((percentage >= 0) && (percentage <= 100)
					&& abs(percentage - oldPercentage) > 10) {
				SendData(DATA_TRASH, percentage);
				oldPercentage = percentage;
			}

			Blink(GREEN_LED, 1, 10);
		}
	}
}
//*****************************************************************************
//
// This example application demonstrates the use of the different sleep modes
// and different power configuration options.
//
//*****************************************************************************
int
main(void)
{
    //
    // Set the clocking to run from the MOSC with the PLL at 16MHz.
    //
    g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                              SYSCTL_OSC_MAIN |
                                              SYSCTL_USE_PLL |
                                              SYSCTL_CFG_VCO_320), 16000000);


    //
    // Set the clocking for Deep-Sleep.
    // Power down the PIOSC & MOSC to save power and run from the
    // internal 30kHz osc.
    //
    ROM_SysCtlDeepSleepClockConfigSet(1, (SYSCTL_DSLP_OSC_INT30 |
                SYSCTL_DSLP_PIOSC_PD | SYSCTL_DSLP_MOSC_PD));

    //
    // Initialize the UART and write the banner.
    // Indicate we are currently in Run Mode.
    //
    ConfigureUART();
    UARTprintf("\033[2J\033[H");
    UARTprintf("Sleep Modes example\n\n");
    UARTprintf("Mode:\t\tClock Source:\tLED Toggle Source:");
    UARTprintf("\nRun\t\tMOSC with PLL\tTimer");

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

    //
    // Enable the interrupt for the button.
    //
    ROM_GPIOIntEnable(GPIO_PORTJ_AHB_BASE, GPIO_INT_PIN_0);

    //
    // Enable the GPIO ports that are used for the on-board LEDs.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    //
    // Set pad config.
    //
    MAP_GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_0,
                          GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

    //
    // Set direction.
    //
    ROM_GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_0, GPIO_DIR_MODE_IN);

    //
    // Enable the interrupt for the button.
    //
    ROM_GPIOIntEnable(GPIO_PORTJ_BASE, GPIO_INT_PIN_0);

    //
    // Enable interrupt to NVIC.
    //
    ROM_IntEnable(INT_GPIOJ);

    //
    // Enable the GPIO ports that are used for the on-board LEDs.
    //
    ROM_SysCtlPeripheralEnable(RUN_GPIO_SYSCTL);
    ROM_SysCtlPeripheralEnable(SLEEP_GPIO_SYSCTL);
    ROM_SysCtlPeripheralEnable(DSLEEP_GPIO_SYSCTL);
    ROM_SysCtlPeripheralEnable(TOGGLE_GPIO_SYSCTL);

    //
    // Enable the GPIO pins for the LED.
    //
    ROM_GPIOPinTypeGPIOOutput(RUN_GPIO_BASE, RUN_GPIO_PIN);
    ROM_GPIOPinTypeGPIOOutput(SLEEP_GPIO_BASE, SLEEP_GPIO_PIN);
    ROM_GPIOPinTypeGPIOOutput(DSLEEP_GPIO_BASE, DSLEEP_GPIO_PIN);
    ROM_GPIOPinTypeGPIOOutput(TOGGLE_GPIO_BASE, TOGGLE_GPIO_PIN);

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

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

    //
    // Configure the 32-bit periodic timer.
    //
    ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, g_ui32SysClock);

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

    //
    // Configure the PWM0 to count down without synchronization.
    // This will be used in Deep-Sleep.
    //
    ROM_PWMGenConfigure(PWM0_BASE, PWM_GEN_0,
                        PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);

    //
    // Enable the PWM0 output signal.
    //
    ROM_PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, true);

    //
    // Set up the period to match the timer.
    //
    ROM_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 65000);

    //
    // Configure the PWM for a 50% duty cycle.
    //
    ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 65000 >> 1);

    //
    // Enable the timer.
    //
    ROM_TimerEnable(TIMER0_BASE, TIMER_A);

    //
    // Enable the Timer in Sleep Mode.
    //
    ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER0);

    //
    // Enable the PWM in Deep-Sleep Mode.
    //
    ROM_SysCtlPeripheralDeepSleepEnable(SYSCTL_PERIPH_PWM0);

    //
    // Enable the Button Port in Sleep & Deep-Sleep Mode.
    //
    ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOJ);
    ROM_SysCtlPeripheralDeepSleepEnable(SYSCTL_PERIPH_GPIOJ);

    //
    // Enable the LED Ports in Sleep & Deep-Sleep Mode.
    //
    ROM_SysCtlPeripheralSleepEnable(SLEEP_GPIO_SYSCTL);
    ROM_SysCtlPeripheralDeepSleepEnable(DSLEEP_GPIO_SYSCTL);
    ROM_SysCtlPeripheralSleepEnable(TOGGLE_GPIO_SYSCTL);
    ROM_SysCtlPeripheralDeepSleepEnable(TOGGLE_GPIO_SYSCTL);

    //
    // Enable Auto Clock Gating Control.
    //
    ROM_SysCtlPeripheralClockGating(true);

    //
    // Set LDO to 1.15V in Sleep.
    // Set LDO to 1.10V in Deep-Sleep.
    //
    SysCtlLDOSleepSet(SYSCTL_LDO_1_15V);
    SysCtlLDODeepSleepSet(SYSCTL_LDO_1_10V);

    //
    // Set SRAM to Standby when in Sleep Mode.
    // Set Flash & SRAM to Low Power in Deep-Sleep Mode.
    //
    SysCtlSleepPowerSet(SYSCTL_SRAM_STANDBY);
    SysCtlDeepSleepPowerSet(SYSCTL_FLASH_LOW_POWER | SYSCTL_SRAM_LOW_POWER);

    //
    // Call to set initial LED power state.
    //
    PowerLEDsSet();

    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Handle going into the different sleep modes outside of
        // interrupt context.
        //
        if (g_ui32SleepMode == 1)
        {
            //
            // Go into Sleep Mode.
            //
            ROM_SysCtlSleep();
        }
        else if (g_ui32SleepMode == 2)
        {
            //
            // Go into Deep-Sleep Mode.
            //
            ROM_SysCtlDeepSleep();
        }
    }
}
//*****************************************************************************
//
// The main loop for the user interface.
//
//*****************************************************************************
int
main(void)
{
    unsigned long ulPanel;

    //
    // If running on Rev A2 silicon, turn the LDO voltage up to 2.75V.  This is
    // a workaround to allow the PLL to operate reliably.
    //
    if(REVISION_IS_A2)
    {
        SysCtlLDOSet(SYSCTL_LDO_2_75V);
    }

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

    //
    // Set the priority of the interrupts.
    //
    IntPrioritySet(INT_CAN0, 0x00);
    IntPrioritySet(FAULT_SYSTICK, 0x20);

    //
    // Configure SysTick to generate an interrupt every millisecond.
    //
    SysTickPeriodSet(SysCtlClockGet() / 1000);
    SysTickIntEnable();
    SysTickEnable();

    //
    // Initialize the push button driver.
    //
    ButtonsInit();

    //
    // Initialize the CAN communication channel.
    //
    CANCommInit();

    //
    // Initialize the UART used to perform a "firmware update".
    //
    UpdateUARTInit();

    //
    // Initialize the display.
    //
    RIT128x96x4Init(3500000);

    //
    // Add the screen-clearing widget to the widget tree.  As the first widget
    // in the tree, this will always be drawn first, resulting in a blank
    // screen before anything else is drawn.
    //
    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sBackground);

    //
    // Display the splash screen.
    //
    DisplaySplash();

    //
    // Set the CAN device ID to one.
    //
    CANSetID(1);

    //
    // The "Voltage Control Mode" panel should be displayed first.
    //
    ulPanel = PANEL_VOLTAGE;

    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Determine which panel to display.
        //
        switch(ulPanel)
        {
            //
            // The "Voltage Control Mode" panel should be displayed.
            //
            case PANEL_VOLTAGE:
            {
                ulPanel = DisplayVoltage();
                break;
            }

            //
            // The "VComp Control Mode" panel should be displayed.
            //
            case PANEL_VCOMP:
            {
                ulPanel = DisplayVComp();
                break;
            }

            //
            // The "Current Control Mode" panel should be displayed.
            //
            case PANEL_CURRENT:
            {
                ulPanel = DisplayCurrent();
                break;
            }

            //
            // The "Speed Control Mode" panel should be displayed.
            //
            case PANEL_SPEED:
            {
                ulPanel = DisplaySpeed();
                break;
            }

            //
            // The "Position Control Mode" panel should be displayed.
            //
            case PANEL_POSITION:
            {
                ulPanel = DisplayPosition();
                break;
            }

            //
            // The "Configuration" panel should be displayed.
            //
            case PANEL_CONFIGURATION:
            {
                ulPanel = DisplayConfig();
                break;
            }

            //
            // The "Device List" panel should be displayed.
            //
            case PANEL_DEV_LIST:
            {
                ulPanel = DisplayDevList();
                break;
            }

            //
            // The "Firmware Update" panel should be displayed.
            //
            case PANEL_UPDATE:
            {
                ulPanel = DisplayUpdate();
                break;
            }

            //
            // The "Help" panel should be displayed.
            //
            case PANEL_HELP:
            {
                ulPanel = DisplayHelp();
                break;
            }

            //
            // The "About" panel should be displayed.
            //
            case PANEL_ABOUT:
            {
                ulPanel = DisplayAbout();
                break;
            }
        }
    }
}
Exemple #21
0
//*****************************************************************************
//
// Run the hibernate example.  Use a loop to put the microcontroller into
// hibernate mode, and to wake up based on time. Also allow the user to cause
// it to hibernate and/or wake up based on button presses.
//
//*****************************************************************************
int
main(void)
{
    uint32_t ui32Idx;
    uint32_t ui32Status = 0;
    uint32_t ui32HibernateCount = 0;
    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 clocking to run directly from the crystal.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Initialize the UART.
    //
    ConfigureUART();

    //
    // Initialize the OLED display
    //
    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);

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

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

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

    //
    // Set up systick to generate interrupts at 100 Hz.
    //
    ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100);
    ROM_SysTickIntEnable();
    ROM_SysTickEnable();

    //
    // Enable the Hibernation module.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE);

    //
    // Print wake cause message on display.
    //
    GrStringDrawCentered(&sContext, "Wake due to:", -1,
                         GrContextDpyWidthGet(&sContext) / 2, Row(2) + 4,
                         true);

    //
    // Check to see if Hibernation module is already active, which could mean
    // that the processor is waking from a hibernation.
    //
    if(HibernateIsActive())
    {
        //
        // Read the status bits to see what caused the wake.
        //
        ui32Status = HibernateIntStatus(0);
        HibernateIntClear(ui32Status);

        //
        // Wake was due to the push button.
        //
        if(ui32Status & HIBERNATE_INT_PIN_WAKE)
        {
            GrStringDrawCentered(&sContext, "BUTTON", -1,
                                 GrContextDpyWidthGet(&sContext) / 2,
                                 Row(3) + 4, true);
        }

        //
        // Wake was due to RTC match
        //
        else if(ui32Status & HIBERNATE_INT_RTC_MATCH_0)
        {
            GrStringDrawCentered(&sContext, "TIMEOUT", -1,
                                 GrContextDpyWidthGet(&sContext) / 2,
                                 Row(3) + 4, true);
        }

        //
        // Wake is due to neither button nor RTC, so it must have been a hard
        // reset.
        //
        else
        {
            GrStringDrawCentered(&sContext, "RESET", -1,
                                 GrContextDpyWidthGet(&sContext) / 2,
                                 Row(3) + 4, true);
        }

        //
        // If the wake is due to button or RTC, then read the first location
        // from the battery backed memory, as the hibernation count.
        //
        if(ui32Status & (HIBERNATE_INT_PIN_WAKE | HIBERNATE_INT_RTC_MATCH_0))
        {
            HibernateDataGet(&ui32HibernateCount, 1);
        }
    }

    //
    // Enable the Hibernation module.  This should always be called, even if
    // the module was already enabled, because this function also initializes
    // some timing parameters.
    //
    HibernateEnableExpClk(ROM_SysCtlClockGet());

    //
    // If the wake was not due to button or RTC match, then it was a reset.
    //
    if(!(ui32Status & (HIBERNATE_INT_PIN_WAKE | HIBERNATE_INT_RTC_MATCH_0)))
    {
        //
        // Configure the module clock source.
        //
        HibernateClockConfig(HIBERNATE_OSC_LOWDRIVE);

        //
        // Finish the wake cause message.
        //
        GrStringDrawCentered(&sContext, "RESET", -1,
                             GrContextDpyWidthGet(&sContext) / 2,
                             Row(3) + 4, true);

        //
        // Wait a couple of seconds in case we need to break in with the
        // debugger.
        //
        SysTickWait(3 * 100);

        //
        // Allow time for the crystal to power up.  This line is separated from
        // the above to make it clear this is still needed, even if the above
        // delay is removed.
        //
        SysTickWait(15);
    }

    //
    // Print the count of times that hibernate has occurred.
    //
    usnprintf(g_pcBuf, sizeof(g_pcBuf), "Hib count=%4u", ui32HibernateCount);
    GrStringDrawCentered(&sContext, g_pcBuf, -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(1) + 4, true);

    //
    // Print messages on the screen about hibernation.
    //
    GrStringDrawCentered(&sContext, "Select to Hib", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(4) + 4, true);
    GrStringDrawCentered(&sContext, "Wake in 5 s,", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(5) + 4, true);
    GrStringDrawCentered(&sContext, "or press Select", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(6) + 4, true);
    GrStringDrawCentered(&sContext, "for immed. wake.", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(7) + 4, true);

    //
    // Clear the button pressed flag, in case it was held down at the
    // beginning.
    //
    bSelectPressed = 0;

    //
    // Wait for user to press the button.
    //
    while(!bSelectPressed)
    {
        //
        // Wait a bit before looping again.
        //
        SysTickWait(10);
    }

    //
    // Tell user to release the button.
    //
    GrStringDrawCentered(&sContext, "                ", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(4) + 4, true);
    GrStringDrawCentered(&sContext, "                ", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(5) + 4, true);
    GrStringDrawCentered(&sContext, "                ", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(6) + 4, true);
    GrStringDrawCentered(&sContext, "                ", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(7) + 4, true);
    GrStringDrawCentered(&sContext, "Release the", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(5) + 4, true);
    GrStringDrawCentered(&sContext, "button.", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(6) + 4, true);
    GrStringDrawCentered(&sContext, "                ", -1,
                         GrContextDpyWidthGet(&sContext) / 2,
                         Row(7) + 4, true);

    //
    // Wait for user to release the button.
    //
    while(bSelectPressed)
    {
    }

    //
    // If hibernation count is very large, it may be that there was already
    // a value in the hibernate memory, so reset the count.
    //
    ui32HibernateCount = (ui32HibernateCount > 10000) ? 0 : ui32HibernateCount;

    //
    // Increment the hibernation count, and store it in the battery backed
    // memory.
    //
    ui32HibernateCount++;
    HibernateDataSet(&ui32HibernateCount, 1);

    //
    // Clear and enable the RTC and set the match registers to 5 seconds in the
    // future. Set both to same, though they could be set differently, the
    // first to match will cause a wake.
    //
    HibernateRTCSet(0);
    HibernateRTCEnable();
    HibernateRTCMatchSet(0, 5);

    //
    // Set wake condition on pin or RTC match.  Board will wake when 5 seconds
    // elapses, or when the button is pressed.
    //
    HibernateWakeSet(HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC);

    //
    // Request hibernation.
    //
    HibernateRequest();

    //
    // Give it time to activate, it should never get past this wait.
    //
    SysTickWait(100);

    //
    // Should not have got here, something is wrong.  Print an error message to
    // the user.
    //
    sRect.i16XMin = 0;
    sRect.i16XMax = 95;
    sRect.i16YMin = 0;
    sRect.i16YMax = 63;
    GrContextForegroundSet(&sContext, ClrBlack);
    GrRectFill(&sContext, &sRect);
    GrContextForegroundSet(&sContext, ClrWhite);
    ui32Idx = 0;
    while(g_pcErrorText[ui32Idx])
    {
        GrStringDraw(&sContext, g_pcErrorText[ui32Idx], -1, Col(0),
                     Row(ui32Idx), true);
        ui32Idx++;
    }

    //
    // Wait for the user to press the button, then restart the app.
    //
    bSelectPressed = 0;
    while(!bSelectPressed)
    {
    }

    //
    // Reset the processor.
    //
    ROM_SysCtlReset();

    //
    // Finished.
    //
    while(1)
    {
    }
}
Exemple #22
0
//*****************************************************************************
//
// Main function performs init and manages system.
//
// Called automatically after the system and compiler pre-init sequences.
// Performs system init calls, restores state from hibernate if needed and
// then manages the application context duties of the system.
//
//*****************************************************************************
int
main(void)
{
    uint32_t ui32Status;
    uint32_t ui32ResetCause;
    int32_t i32CommandStatus;

    //
    // 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_FPUEnable();
    ROM_FPUStackingEnable();

    //
    // Set the system clock to run at 40Mhz off PLL with external crystal as
    // reference.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
                       SYSCTL_OSC_MAIN);

    //
    // Enable the hibernate module
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE);

    //
    // Enable and Initialize the UART.
    //
    ConfigureUART();

    UARTprintf("Welcome to the Tiva C Series TM4C123G LaunchPad!\n");
    UARTprintf("Type 'help' for a list of commands\n");
    UARTprintf("> ");

    //
    // Determine why system reset occurred and respond accordingly.
    //
    ui32ResetCause = SysCtlResetCauseGet();
    SysCtlResetCauseClear(ui32ResetCause);
    if(ui32ResetCause == SYSCTL_CAUSE_POR)
    {
        if(HibernateIsActive())
        {
            //
            // Read the status bits to see what caused the wake.
            //
            ui32Status = HibernateIntStatus(0);
            HibernateIntClear(ui32Status);

            //
            // Wake was due to the push button.
            //
            if(ui32Status & HIBERNATE_INT_PIN_WAKE)
            {
                UARTprintf("Hibernate Wake Pin Wake Event\n");
                UARTprintf("> ");

                //
                // Recover the application state variables from battery backed
                // hibernate memory.  Set ui32Mode to normal.
                //
                HibernateDataGet((uint32_t*) &g_sAppState,
                                 sizeof(tAppState) / 4 + 1);
                g_sAppState.ui32Mode = APP_MODE_NORMAL;
            }

            //
            // Wake was due to RTC match
            //
            else if(ui32Status & HIBERNATE_INT_RTC_MATCH_0)
            {
                UARTprintf("Hibernate RTC Wake Event\n");
                UARTprintf("> ");
                //
                // Recover the application state variables from battery backed
                // hibernate memory. Set ui32Mode to briefly flash the RGB.
                //
                HibernateDataGet((uint32_t*) &g_sAppState,
                                sizeof(tAppState) / 4 + 1);
                g_sAppState.ui32Mode = APP_MODE_HIB_FLASH;
            }
        }

        else
        {
            //
            // Reset was do to a cold first time power up.
            //
            UARTprintf("Power on reset. Hibernate not active.\n");
            UARTprintf("> ");

            g_sAppState.ui32Mode = APP_MODE_NORMAL;
            g_sAppState.fColorWheelPos = 0;
            g_sAppState.fIntensity = APP_INTENSITY_DEFAULT;
            g_sAppState.ui32Buttons = 0;
        }
    }
    else
    {
        //
        // External Pin reset or other reset event occured.
        //
        UARTprintf("External or other reset\n");
        UARTprintf("> ");

        //
        // Treat this as a cold power up reset without restore from hibernate.
        //
        g_sAppState.ui32Mode = APP_MODE_NORMAL;
        g_sAppState.fColorWheelPos = APP_PI;
        g_sAppState.fIntensity = APP_INTENSITY_DEFAULT;
        g_sAppState.ui32Buttons = 0;

    //
        // colors get a default initialization later when we call AppRainbow.
        //
    }

    //
    // Initialize clocking for the Hibernate module
    //
    HibernateEnableExpClk(SysCtlClockGet());

    //
    // Initialize the RGB LED. AppRainbow typically only called from interrupt
    // context. Safe to call here to force initial color update because
    // interrupts are not yet enabled.
    //
    RGBInit(0);
    RGBIntensitySet(g_sAppState.fIntensity);
    AppRainbow(1);
    RGBEnable();

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

    //
    // Initialize the SysTick interrupt to process colors and buttons.
    //
    SysTickPeriodSet(SysCtlClockGet() / APP_SYSTICKS_PER_SEC);
    SysTickEnable();
    SysTickIntEnable();
    IntMasterEnable();

    //
    // spin forever and wait for carriage returns or state changes.
    //
    while(1)
    {

        UARTprintf("\n>");


        //
        // Peek to see if a full command is ready for processing
        //
        while(UARTPeek('\r') == -1)
        {
            //
            // millisecond delay.  A SysCtlSleep() here would also be OK.
            //
            SysCtlDelay(SysCtlClockGet() / (1000 / 3));

            //
            // Check for change of mode and enter hibernate if requested.
            // all other mode changes handled in interrupt context.
            //
            if(g_sAppState.ui32Mode == APP_MODE_HIB)
            {
                AppHibernateEnter();
            }
        }

        //
        // a '\r' was detected get the line of text from the user.
        //
        UARTgets(g_cInput,sizeof(g_cInput));

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

        //
        // Handle the case of bad command.
        //
        if(i32CommandStatus == CMDLINE_BAD_CMD)
        {
            UARTprintf("Bad command!\n");
        }

        //
        // Handle the case of too many arguments.
        //
        else if(i32CommandStatus == CMDLINE_TOO_MANY_ARGS)
        {
            UARTprintf("Too many arguments for command processor!\n");
        }
    }
}
Exemple #23
0
//*****************************************************************************
//
// Toggle the JTAG pins between JTAG and GPIO mode with a push button selecting
// between the two.
//
//*****************************************************************************
int
main(void)
{
    uint32_t ui32Mode;

    //
    // 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_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
                       SYSCTL_OSC_MAIN);

    //
    // Enable the peripherals used by this application.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);

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

    //
    // Set up a SysTick Interrupt to handle polling and debouncing for our
    // buttons.
    //
    SysTickPeriodSet(SysCtlClockGet() / 100);
    SysTickIntEnable();
    SysTickEnable();

    IntMasterEnable();

    //
    // Configure the LED as an output and turn it on.
    //
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3 | GPIO_PIN_1);
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3 | GPIO_PIN_1, GPIO_PIN_3);

    //
    // Set the global and local indicator of pin mode to zero, meaning JTAG.
    //
    g_ui32Mode = 0;
    ui32Mode = 0;

    //
    // Initialize the UART.
    //
    ConfigureUART();

    UARTprintf("\033[2JGPIO <-> JTAG\n");

    //
    // Indicate that the pins start out as JTAG.
    //
    UARTprintf("Pins are JTAG\n");

    //
    // Loop forever.  This loop simply exists to display on the UART the
    // current state of PC0-3; the handling of changing the JTAG pins to and
    // from GPIO mode is done in GPIO Interrupt Handler.
    //
    while(1)
    {
        //
        // Wait until the pin mode changes.
        //
        while(g_ui32Mode == ui32Mode)
        {
        }

        //
        // Save the new mode locally so that a subsequent pin mode change can
        // be detected.
        //
        ui32Mode = g_ui32Mode;

        //
        // See what the new pin mode was changed to.
        //
        if(ui32Mode == 0)
        {
            //
            // Indicate that PC0-3 are currently JTAG pins.
            //
            UARTprintf("Pins are JTAG\n");
        }
        else
        {
            //
            // Indicate that PC0-3 are currently GPIO pins.
            //
            UARTprintf("Pins are GPIO\n");
        }
    }
}
//*****************************************************************************
//
// This example demonstrates the use of the watchdog timer.
//
//*****************************************************************************
int
main(void)
{
    //
    // Set the clocking to run directly from the crystal at 120MHz.
    //
    g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                             SYSCTL_OSC_MAIN |
                                             SYSCTL_USE_PLL |
                                             SYSCTL_CFG_VCO_480), 120000000);
    //
    // Initialize the buttons driver.
    //
    ButtonsInit();

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

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

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

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

    //
    // Set the period of the watchdog timer to 1 second.
    //
    ROM_WatchdogReloadSet(WATCHDOG0_BASE, g_ui32SysClock);

    //
    // 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)
    {
        //
        // Poll for the select button pressed
        //
        uint8_t ui8Buttons = ButtonsPoll(0, 0);
        if(ui8Buttons & USR_SW1)
        {
            SW1ButtonPressed();
            while(1)
            {
            }
        }
    }
}
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
    uint8_t ui8ButtonsChanged, ui8Buttons;
    uint32_t ui32SysClock;

    //
    // Set the clocking to run from the PLL at 120MHz
    //
    ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                                           SYSCTL_XTAL_25MHZ |
                                           SYSCTL_CFG_VCO_480), 120000000);

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

    //
    // Configure the buttons driver.
    //
    ButtonsInit(ALL_BUTTONS);

    //
    // 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-dev-gamepad");

    //
    // Default status is disconnected.
    //
    DisplayStatus(&g_sContext, "Disconnected");

    //
    // Not configured initially.
    //
    g_iGamepadState = eStateNotConfigured;

    //
    // Initialize the USB stack for device mode.
    //
    USBStackModeSet(0, eUSBModeDevice, 0);

    //
    // Pass the device information to the USB library and place the device
    // on the bus.
    //
    USBDHIDGamepadInit(0, &g_sGamepadDevice);

    //
    // Zero out the initial report.
    //
    g_sReport.ui8Buttons = 0;
    g_sReport.i8XPos = 0;
    g_sReport.i8YPos = 0;

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

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

    //
    // The main loop starts here.  We begin by waiting for a host connection
    // then drop into the main gamepad handling section.  If the host
    // disconnects, we return to the top and wait for a new connection.
    //
    while(1)
    {
        //
        // Wait here until USB device is connected to a host.
        //
        if(g_iGamepadState == eStateIdle)
        {
            //
            // See if the buttons updated.
            //
            ButtonsPoll(&ui8ButtonsChanged, &ui8Buttons);

            g_sReport.ui8Buttons = 0;

            //
            // Set button 1 if up button pressed.
            //
            if(ui8Buttons & UP_BUTTON)
            {
                g_sReport.ui8Buttons |= 0x01;
            }

            //
            // Set button 2 if down button pressed.
            //
            if(ui8Buttons & DOWN_BUTTON)
            {
                g_sReport.ui8Buttons |= 0x02;
            }

            //
            // Set button 3 if select button pressed.
            //
            if(ui8Buttons & SELECT_BUTTON)
            {
                g_sReport.ui8Buttons |= 0x04;
            }

            if(ui8ButtonsChanged)
            {
                g_bUpdate = true;
            }

            //
            // Send the report if there was an update.
            //
            if(g_bUpdate)
            {
                g_bUpdate = false;

                USBDHIDGamepadSendReport(&g_sGamepadDevice, &g_sReport,
                                         sizeof(g_sReport));

                //
                // Now sending data but protect this from an interrupt since
                // it can change in interrupt context as well.
                //
                IntMasterDisable();
                g_iGamepadState = eStateSending;
                IntMasterEnable();
            }
        }
    }
}
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
    tRectangle sRect;

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

    //
    // Enable the USB mux GPIO.
    //
    SysCtlPeripheralEnable(USB_MUX_GPIO_PERIPH);

    //
    // The LM3S3748 board uses a USB mux that must be switched to use the
    // host connector 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_DEVICE);

#ifdef DEBUG
    //
    // Configure the relevant pins such that UART0 owns them.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

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

    //
    // Initialize the pushbuttons.
    //
    ButtonsInit();
    ButtonsSetAutoRepeat((LEFT_BUTTON | RIGHT_BUTTON | UP_BUTTON |
                          DOWN_BUTTON), 0, 2);

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

    //
    // 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_pFontFixed6x8);
    GrStringDrawCentered(&g_sContext, "boot_demo_usb", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 7, 0);

    //
    // 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);

    //
    // Drop into the main loop.
    //
    while(!g_bUpdateSignalled)
    {
        //
        // Fill all but the top 15 rows of the screen with black to erase the
        // previous status.
        //
        sRect.sXMin = 0;
        sRect.sYMin = 15;
        sRect.sXMax = GrContextDpyWidthGet(&g_sContext) - 1;
        sRect.sYMax = GrContextDpyHeightGet(&g_sContext) - 1;
        GrContextForegroundSet(&g_sContext, ClrBlack);
        GrRectFill(&g_sContext, &sRect);

        //
        // Tell the user what we are doing.
        //
        GrContextForegroundSet(&g_sContext, ClrWhite);
        GrStringDrawCentered(&g_sContext, "Waiting for host...", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 24, true);
        //
        // Wait for USB configuration to complete.
        //
        while(!g_bConnected)
        {
        }

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

        //
        // Now keep processing the mouse as long as the host is connected.
        //
        while(g_bConnected && !g_bUpdateSignalled)
        {
            //
            // If it is time to check the button state then do so.
            //
            if(g_ulCommands & BUTTON_TICK_EVENT)
            {
                g_ulCommands &= ~BUTTON_TICK_EVENT;
                ButtonHandler();
            }
        }

        //
        // If we drop out of the previous loop, the host has disconnected so
        // go back and wait for a new connection.
        //
    }

    //
    // If we drop out of the main loop, the host has signalled that it wants us
    // to switch into DFU mode in preparation for a firmware upgrade.  First,
    // let the user know what's going on.
    //
    GrStringDrawCentered(&g_sContext, "Entering DFU mode...", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 24, true);

    //
    // Call the USB DFU device class to tidy things up and transfer control to
    // the boot loader for us.  Note that this function never returns.
    //
    USBDDFUUpdateBegin();
}
Exemple #27
0
//*****************************************************************************
//
// Initialize and operate the data logger.
//
//*****************************************************************************
int
main(void)
{
    tContext sDisplayContext, sBufferContext;
    uint32_t ui32HibIntStatus, ui32SysClock, ui32LastTickCount;
    bool bSkipSplash;
    uint8_t ui8ButtonState, ui8ButtonChanged;
    uint_fast8_t ui8X, ui8Y;


    //
    // 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.
    //
    MAP_FPULazyStackingEnable();

    //
    // Set the clocking to run at 50 MHz.
    //
    MAP_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
                       SYSCTL_OSC_MAIN);
    ui32SysClock = MAP_SysCtlClockGet();

    //
    // Initialize locals.
    //
    bSkipSplash = false;
    ui32LastTickCount = 0;

    //
    // Initialize the data acquisition module.  This initializes the ADC
    // hardware.
    //
    AcquireInit();

    //
    // Enable access to  the hibernate peripheral.  If the hibernate peripheral
    // was already running then this will have no effect.
    //
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE);

    //
    // Check to see if the hiberate module is already active and if so then
    // read the saved configuration state.  If both are okay, then proceed
    // to check and see if we are logging data using sleep mode.
    //
    if(HibernateIsActive() && !GetSavedState(&g_sConfigState))
    {
        //
        // Read the status of the hibernate module.
        //
        ui32HibIntStatus = HibernateIntStatus(1);

        //
        // If this is a pin wake, that means the user pressed the select
        // button and we should terminate the sleep logging.  In this case
        // we will fall out of this conditional section, and go through the
        // normal startup below, but skipping the splash screen so the user
        // gets immediate response.
        //
        if(ui32HibIntStatus & HIBERNATE_INT_PIN_WAKE)
        {
            //
            // Clear the interrupt flag so it is not seen again until another
            // wake.
            //
            HibernateIntClear(HIBERNATE_INT_PIN_WAKE);
            bSkipSplash = true;
        }

        //
        // Otherwise if we are waking from hibernate and it was not a pin
        // wake, then it must be from RTC match.  Check to see if we are
        // sleep logging and if so then go through an abbreviated startup
        // in order to collect the data and go back to sleep.
        //
        else if(g_sConfigState.ui32SleepLogging &&
                (ui32HibIntStatus & HIBERNATE_INT_RTC_MATCH_0))
        {
            //
            // Start logger and pass the configuration.  The logger should
            // configure itself to take one sample.
            //
            AcquireStart(&g_sConfigState);
            g_iLoggerState = eSTATE_LOGGING;

            //
            // Enter a forever loop to run the acquisition.  This will run
            // until a new sample has been taken and stored.
            //
            while(!AcquireRun())
            {
            }

            //
            // Getting here means that a data acquisition was performed and we
            // can now go back to sleep.  Save the configuration and then
            // activate the hibernate.
            //
            SetSavedState(&g_sConfigState);

            //
            // Set wake condition on pin-wake or RTC match.  Then put the
            // processor in hibernation.
            //
            HibernateWakeSet(HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC);
            HibernateRequest();

            //
            // Hibernating takes a finite amount of time to occur, so wait
            // here forever until hibernate activates and the processor
            // power is removed.
            //
            for(;;)
            {
            }
        }

        //
        // Otherwise, this was not a pin wake, and we were not sleep logging,
        // so just fall out of this conditional and go through the normal
        // startup below.
        //
    }
    else
    {
        //
        // In this case, either the hibernate module was not already active, or
        // the saved configuration was not valid.  Initialize the configuration
        // to the default state and then go through the normal startup below.
        //
        GetDefaultState(&g_sConfigState);
    }

    //
    // Enable the Hibernate module to run.
    //
    HibernateEnableExpClk(SysCtlClockGet());

    //
    // The hibernate peripheral trim register must be set per silicon
    // erratum 2.1
    //
    HibernateRTCTrimSet(0x7FFF);

    //
    // Start the RTC running.  If it was already running then this will have
    // no effect.
    //
    HibernateRTCEnable();

    //
    // In case we were sleep logging and are now finished (due to user
    // pressing select button), then disable sleep logging so it doesnt
    // try to start up again.
    //
    g_sConfigState.ui32SleepLogging = 0;
    SetSavedState(&g_sConfigState);

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

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

    //
    // Pass the restored state to the menu system.
    //
    MenuSetState(&g_sConfigState);

    //
    // Enable the USB peripheral
    //
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);

    //
    // Configure the required pins for USB operation.
    //
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
    MAP_GPIOPinConfigure(GPIO_PG4_USB0EPEN);
    MAP_GPIOPinTypeUSBDigital(GPIO_PORTG_BASE, GPIO_PIN_4);
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL);
    MAP_GPIOPinTypeUSBAnalog(GPIO_PORTL_BASE, GPIO_PIN_6 | GPIO_PIN_7);
    MAP_GPIOPinTypeUSBAnalog(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Erratum workaround for silicon revision A1.  VBUS must have pull-down.
    //
    if(CLASS_IS_BLIZZARD && REVISION_IS_A1)
    {
        HWREG(GPIO_PORTB_BASE + GPIO_O_PDR) |= GPIO_PIN_1;
    }

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

    //
    // Initialize the stack to be used with USB stick.
    //
    USBStickInit();

    //
    // Initialize the stack to be used as a serial device.
    //
    USBSerialInit();

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

    //
    // Initialize the menus module.  This module will control the user
    // interface menuing system.
    //
    MenuInit(WidgetActivated);

    //
    // Configure SysTick to periodically interrupt.
    //
    g_ui32TickCount = 0;
    MAP_SysTickPeriodSet(ui32SysClock / CLOCK_RATE);
    MAP_SysTickIntEnable();
    MAP_SysTickEnable();

    //
    // Initialize the display context and another context that is used
    // as an offscreen drawing buffer for display animation effect
    //
    GrContextInit(&sDisplayContext, &g_sCFAL96x64x16);
    GrContextInit(&sBufferContext, &g_sOffscreenDisplayA);

    //
    // Show the splash screen if we are not skipping it.  The only reason to
    // skip it is if the application was in sleep-logging mode and the user
    // just waked it up with the select button.
    //
    if(!bSkipSplash)
    {
        const uint8_t *pui8SplashLogo = g_pui8Image_TI_Black;

        //
        // Draw the TI logo on the display.  Use an animation effect where the
        // logo will "slide" onto the screen.  Allow select button to break
        // out of animation.
        //
        for(ui8X = 0; ui8X < 96; ui8X++)
        {
            if(ButtonsPoll(0, 0) & SELECT_BUTTON)
            {
                break;
            }
            GrImageDraw(&sDisplayContext, pui8SplashLogo, 95 - ui8X, 0);
        }

        //
        // Leave the logo on the screen for a long duration.  Monitor the
        // buttons so that if the user presses the select button, the logo
        // display is terminated and the application starts immediately.
        //
        while(g_ui32TickCount < 400)
        {
            if(ButtonsPoll(0, 0) & SELECT_BUTTON)
            {
                break;
            }
        }

        //
        // Extended splash sequence
        //
        if(ButtonsPoll(0, 0) & UP_BUTTON)
        {
            for(ui8X = 0; ui8X < 96; ui8X += 4)
            {
                GrImageDraw(&sDisplayContext,
                            g_ppui8Image_Splash[(ui8X / 4) & 3],
                            (int32_t)ui8X - 96L, 0);
                GrImageDraw(&sDisplayContext, pui8SplashLogo, ui8X, 0);
                MAP_SysCtlDelay(ui32SysClock / 12);
            }
            MAP_SysCtlDelay(ui32SysClock / 3);
            pui8SplashLogo = g_ppui8Image_Splash[4];
            GrImageDraw(&sDisplayContext, pui8SplashLogo, 0, 0);
            MAP_SysCtlDelay(ui32SysClock / 12);
        }

        //
        // Draw the initial menu into the offscreen buffer.
        //
        SlideMenuDraw(&g_sMenuWidget, &sBufferContext, 0);

        //
        // Now, draw both the TI logo splash screen (from above) and the initial
        // menu on the screen at the same time, moving the coordinates so that
        // the logo "slides" off the display and the menu "slides" onto the
        // display.
        //
        for(ui8Y = 0; ui8Y < 64; ui8Y++)
        {
            GrImageDraw(&sDisplayContext, pui8SplashLogo, 0, -ui8Y);
            GrImageDraw(&sDisplayContext, g_pui8OffscreenBufA, 0, 63 - ui8Y);
        }
    }

    //
    // Add the menu widget to the widget tree and send an initial paint
    // request.
    //
    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sMenuWidget);
    WidgetPaint(WIDGET_ROOT);

    //
    // Set the focus handle to the menu widget.  Any button events will be
    // sent to this widget
    //
    g_ui32KeyFocusWidgetHandle = (uint32_t)&g_sMenuWidget;

    //
    // Forever loop to run the application
    //
    while(1)
    {

        //
        // Each time the timer tick occurs, process any button events.
        //
        if(g_ui32TickCount != ui32LastTickCount)
        {
            //
            // Remember last tick count
            //
            ui32LastTickCount = g_ui32TickCount;

            //
            // Read the debounced state of the buttons.
            //
            ui8ButtonState = ButtonsPoll(&ui8ButtonChanged, 0);

            //
            // Pass any button presses through to the widget message
            // processing mechanism.  The widget that has the button event
            // focus (probably the menu widget) will catch these button events.
            //
            if(BUTTON_PRESSED(SELECT_BUTTON, ui8ButtonState, ui8ButtonChanged))
            {
                SendWidgetKeyMessage(WIDGET_MSG_KEY_SELECT);
            }
            if(BUTTON_PRESSED(UP_BUTTON, ui8ButtonState, ui8ButtonChanged))
            {
                SendWidgetKeyMessage(WIDGET_MSG_KEY_UP);
            }
            if(BUTTON_PRESSED(DOWN_BUTTON, ui8ButtonState, ui8ButtonChanged))
            {
                SendWidgetKeyMessage(WIDGET_MSG_KEY_DOWN);
            }
            if(BUTTON_PRESSED(LEFT_BUTTON, ui8ButtonState, ui8ButtonChanged))
            {
                SendWidgetKeyMessage(WIDGET_MSG_KEY_LEFT);
            }
            if(BUTTON_PRESSED(RIGHT_BUTTON, ui8ButtonState, ui8ButtonChanged))
            {
                SendWidgetKeyMessage(WIDGET_MSG_KEY_RIGHT);
            }
        }

        //
        // Tell the OTG library code how much time has passed in milliseconds
        // since the last call.
        //
        USBOTGMain(GetTickms());

        //
        // Call functions as needed to keep the host or device mode running.
        //
        if(g_iCurrentUSBMode == eUSBModeDevice)
        {
            USBSerialRun();
        }
        else if(g_iCurrentUSBMode == eUSBModeHost)
        {
            USBStickRun();
        }

        //
        // If in the logging state, then call the logger run function.  This
        // keeps the data acquisition running.
        //
        if((g_iLoggerState == eSTATE_LOGGING) ||
           (g_iLoggerState == eSTATE_VIEWING))
        {
            if(AcquireRun() && g_sConfigState.ui32SleepLogging)
            {
                //
                // If sleep logging is enabled, then at this point we have
                // stored the first data item, now save the state and start
                // hibernation.  Wait for the power to be cut.
                //
                SetSavedState(&g_sConfigState);
                HibernateWakeSet(HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC);
                HibernateRequest();
                for(;;)
                {
                }
            }

            //
            // If viewing instead of logging then request a repaint to keep
            // the viewing window updated.
            //
            if(g_iLoggerState == eSTATE_VIEWING)
            {
                WidgetPaint(WIDGET_ROOT);
            }
        }

        //
        // If in the saving state, then save data from flash storage to
        // USB stick.
        //
        if(g_iLoggerState == eSTATE_SAVING)
        {
            //
            // Save data from flash to USB
            //
            FlashStoreSave();

            //
            // Return to idle state
            //
            g_iLoggerState = eSTATE_IDLE;
        }

        //
        // If in the erasing state, then erase the data stored in flash.
        //
        if(g_iLoggerState == eSTATE_ERASING)
        {
            //
            // Save data from flash to USB
            //
            FlashStoreErase();

            //
            // Return to idle state
            //
            g_iLoggerState = eSTATE_IDLE;
        }

        //
        // If in the flash reporting state, then show the report of the amount
        // of used and free flash memory.
        //
        if(g_iLoggerState == eSTATE_FREEFLASH)
        {
            //
            // Report free flash space
            //
            FlashStoreReport();

            //
            // Return to idle state
            //
            g_iLoggerState = eSTATE_IDLE;
        }

        //
        // If we are exiting the clock setting widget, that means that control
        // needs to be given back to the menu system.
        //
        if(g_iLoggerState == eSTATE_CLOCKEXIT)
        {
            //
            // Give the button event focus back to the menu system
            //
            g_ui32KeyFocusWidgetHandle = (uint32_t)&g_sMenuWidget;

            //
            // Send a button event to the menu widget that means the left
            // key was pressed.  This signals the menu widget to deactivate
            // the current child widget (which was the clock setting wigdet).
            // This will cause the menu widget to slide the clock set widget
            // off the screen and resume control of the display.
            //
            SendWidgetKeyMessage(WIDGET_MSG_KEY_LEFT);
            g_iLoggerState = eSTATE_IDLE;
        }

        //
        // Process any new messages that are in the widget queue.  This keeps
        // the user interface running.
        //
        WidgetMessageQueueProcess();
    }
}
//*****************************************************************************
//
// 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);
}
Exemple #29
0
//*****************************************************************************
//
// This example application demonstrates the use of the timers to generate
// periodic interrupts.
//
//*****************************************************************************
int
main(void)
{
    //
    // 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.
    //
		FPUEnable();
		FPULazyStackingEnable();

    //
    // Set the clocking to run directly from the crystal.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);
	
		//
    // Enable processor interrupts.
    //
    IntMasterEnable();

    //
    // Initialize the UART and write status.
    //
    ConfigureUART();
		ConfigureXBeeUART();
		ConfigureUARTSensores();
		ButtonsInit();
		inicializa_motores();

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

    //
    // Configure the two 32-bit periodic timers.
    //
    TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
    TimerLoadSet(TIMER1_BASE, TIMER_A, SysCtlClockGet()/200000);
		
		
    //
    // Setup the interrupts for the timer timeouts.
    //
    IntEnable(INT_TIMER1A);
   
    TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);

    //
    // Enable the timers.
    //
		
    TimerEnable(TIMER1_BASE, TIMER_A);


		// Configure a wide timer for timing purposes
		
		SysCtlPeripheralEnable(SYSCTL_PERIPH_WTIMER0);
		TimerConfigure(WTIMER0_BASE, TIMER_CFG_PERIODIC_UP);
		TimerLoadSet64(WTIMER0_BASE, (((long long)1) << 60));
		TimerEnable(WTIMER0_BASE, TIMER_A);
	
	int counter_verify_no_ar = 0;
	
	while(1) {
		SysCtlDelay(SysCtlClockGet() / 1000);
		checkButtons();
		
		readPackage();
		
		counter_verify_no_ar++;
		
		if (counter_verify_no_ar == 1000) {
			counter_verify_no_ar = 0;
			if (no_ar) {
				enviaNoAr();
				no_ar = false;
			}
			if (no_chao) {
				enviaNoChao();
				no_chao = false;
			}
		}
	}
}
Exemple #30
0
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
    uint_fast32_t ui32LastTickCount;
    bool bLastSuspend;
    tRectangle sRect;
    tContext sContext;
    int_fast32_t i32CenterX;

    //
    // 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 from the PLL at 50MHz.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // 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);

    //
    // Erratum workaround for silicon revision A1.  VBUS must have pull-down.
    //
    if(CLASS_IS_BLIZZARD && REVISION_IS_A1)
    {
        HWREG(GPIO_PORTB_BASE + GPIO_O_PDR) |= GPIO_PIN_1;
    }

    //
    // Enable the GPIO that is used for the on-board LED.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_2);
    ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, 0);

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

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

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

    //
    // 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, "usb-dev-keyboard", -1, i32CenterX, 4, 0);

    //
    // Not configured initially.
    //
    g_bConnected = false;
    g_bSuspended = false;
    bLastSuspend = false;

    //
    // Initialize the USB stack for device mode.
    //
    USBStackModeSet(0, eUSBModeDevice, 0);

    //
    // Pass our device information to the USB HID device class driver,
    // initialize the USB
    // controller and connect the device to the bus.
    //
    USBDHIDKeyboardInit(0, &g_sKeyboardDevice);

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

    //
    // The main loop starts here.  We begin by waiting for a host connection
    // then drop into the main keyboard handling section.  If the host
    // disconnects, we return to the top and wait for a new connection.
    //
    while(1)
    {
        uint8_t ui8Buttons;
        uint8_t ui8ButtonsChanged;

        //
        // Tell the user what we are doing and provide some basic instructions.
        //
        GrStringDrawCentered(&sContext, "    Waiting    ",
                             -1, i32CenterX, 22, 1);
        GrStringDrawCentered(&sContext, " for host ... ", -1,
                             i32CenterX, 32, 1);

        //
        // Wait here until USB device is connected to a host.
        //
        while(!g_bConnected)
        {
        }

        //
        // Update the status.
        //
        GrStringDrawCentered(&sContext, "     Host     ",
                             -1, i32CenterX, 22, 1);
        GrStringDrawCentered(&sContext, " connected ... ",
                             -1, i32CenterX, 32, 1);

        //
        // Enter the idle state.
        //
        g_eKeyboardState = STATE_IDLE;

        //
        // Assume that the bus is not currently suspended if we have just been
        // configured.
        //
        bLastSuspend = false;

        //
        // Keep transferring characters from the UART to the USB host for as
        // long as we are connected to the host.
        //
        while(g_bConnected)
        {
            //
            // Remember the current time.
            //
            ui32LastTickCount = g_ui32SysTickCount;

            //
            // Has the suspend state changed since last time we checked?
            //
            if(bLastSuspend != g_bSuspended)
            {
                //
                // Yes - the state changed so update the display.
                //
                bLastSuspend = g_bSuspended;
                if(bLastSuspend)
                {
                    GrStringDrawCentered(&sContext, "      Bus      ", -1,
                                         i32CenterX, 22, 1);
                    GrStringDrawCentered(&sContext, " suspended ... ", -1,
                                         i32CenterX, 32, 1);
                }
                else
                {
                    GrStringDrawCentered(&sContext, "     Host     ", -1,
                                         i32CenterX, 22, 1);
                    GrStringDrawCentered(&sContext, " connected ... ",
                                         -1, i32CenterX, 32, 1);
                }
            }

            //
            // See if the button was just pressed.
            //
            ui8Buttons = ButtonsPoll(&ui8ButtonsChanged, 0);
            if(BUTTON_PRESSED(SELECT_BUTTON, ui8Buttons, ui8ButtonsChanged))
            {
                //
                // If the bus is suspended then resume it.  Otherwise, type
                // some "random" characters.
                //
                if(g_bSuspended)
                {
                    USBDHIDKeyboardRemoteWakeupRequest(
                                                   (void *)&g_sKeyboardDevice);
                }
                else
                {
                    SendString("Make the Switch to TI Microcontrollers!");
                }
            }

            //
            // Wait for at least 1 system tick to have gone by before we poll
            // the buttons again.
            //
            while(g_ui32SysTickCount == ui32LastTickCount)
            {
            }
        }
    }
}