text: //*****************************************************************************
  //
  // boot_demo2.c - Second boot loader example.
  //
  // Copyright (c) 2008-2013 Texas Instruments Incorporated.  All rights reserved.
  // Software License Agreement
  // 
  // Texas Instruments (TI) is supplying this software for use solely and
  // exclusively on TI's microcontroller products. The software is owned by
  // TI and/or its suppliers, and is protected under applicable copyright
  // laws. You may not combine this software with "viral" open-source
  // software in order to form a larger program.
  // 
  // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  // DAMAGES, FOR ANY REASON WHATSOEVER.
  // 
  // This is part of revision 10636 of the DK-LM3S9D96 Firmware Package.
  //
  //*****************************************************************************
  
  #include "inc/hw_types.h"
  #include "inc/hw_memmap.h"
  #include "inc/hw_nvic.h"
  #include "driverlib/interrupt.h"
  #include "driverlib/sysctl.h"
  #include "driverlib/systick.h"
  #include "driverlib/flash.h"
  #include "driverlib/uart.h"
  #include "driverlib/uart.h"
  #include "driverlib/gpio.h"
  #include "driverlib/rom.h"
  #include "utils/ustdlib.h"
  #include "utils/lwiplib.h"
  #include "grlib/grlib.h"
  #include "grlib/widget.h"
  #include "grlib/canvas.h"
  #include "grlib/pushbutton.h"
  #include "drivers/kitronix320x240x16_ssd2119_8bit.h"
  #include "drivers/touch.h"
  #include "drivers/set_pinout.h"
  
  //*****************************************************************************
  //
  //! \addtogroup example_list
  //! <h1>Boot Loader Demo 2 (boot_demo2)</h1>
  //!
  //! An example to demonstrate the use of a flash-based boot loader.  At startup,
  //! the application will configure the UART, USB and Ethernet peripherals, wait
  //! for a widget on the screen to be pressed, and then branch to the boot
  //! loader to await the start of an update.  If using the serial boot loader
  //! (boot_serial), the UART will always be configured at 115,200 baud and does
  //! not require the use of auto-bauding.
  //!
  //! This application is intended for use with any of the three flash-based boot
  //! loader flavors (boot_eth, boot_serial or boot_usb) included in the software
  //! release.  To accommodate the largest of these (boot_usb), the link address
  //! is set to 0x1800.  If you are using serial or Ethernet boot loader, you
  //! may change this address to a 1KB boundary higher than the last address
  //! occupied by the boot loader binary as long as you also rebuild the boot
  //! loader itself after modifying its bl_config.h file to set APP_START_ADDRESS
  //! to the same value.
  //!
  //! The boot_demo1 application can be used along with this application to
  //! easily demonstrate that the boot loader is actually updating the on-chip
  //! flash.
  //!
  //! Note that the LM3S9D96 and other Firestorm-class Stellaris devices also
  //! support serial and ethernet boot loaders in ROM.  To make use of this
  //! function, link your application to run at address 0x0000 in flash and enter
  //! the bootloader using either the ROM_UpdateEthernet or ROM_UpdateSerial
  //! functions (defined in rom.h).  This mechanism is used in the
  //! utils/swupdate.c module when built specifically targeting a suitable
  //! Firestorm-class device.
  //
  //*****************************************************************************
  
  //*****************************************************************************
  //
  // The number of SysTick ticks per second.
  //
  //*****************************************************************************
  #define TICKS_PER_SECOND 100
  
  //*****************************************************************************
  //
  // A global we use to keep track of when the user presses the "Update now"
  // button.
  //
  //*****************************************************************************
  volatile tBoolean g_bFirmwareUpdate = false;
  
  //*****************************************************************************
  //
  // Buffers used to hold the Ethernet MAC and IP addresses for the board.
  //
  //*****************************************************************************
  #define SIZE_MAC_ADDR_BUFFER 32
  #define SIZE_IP_ADDR_BUFFER 32
  char g_pcMACAddr[SIZE_MAC_ADDR_BUFFER];
  char g_pcIPAddr[SIZE_IP_ADDR_BUFFER];
  
  //*****************************************************************************
  //
  // Forward reference to various widget structures.
  //
  //*****************************************************************************
  extern tCanvasWidget g_sBackground;
  
  //*****************************************************************************
  //
  // Forward reference to the button press handler.
  //
  //*****************************************************************************
  void OnButtonPress(tWidget *pWidget);
  
  //*****************************************************************************
  //
  // The canvas widget used to display the board's Ethernet IP address.
  //
  //*****************************************************************************
  Canvas(g_sIPAddr, &g_sBackground, 0, 0,
         &g_sKitronix320x240x16_SSD2119, 0, 220, 320, 20,
         (CANVAS_STYLE_FILL | CANVAS_STYLE_TEXT),
         ClrBlack, 0, ClrWhite, g_pFontCm20, g_pcIPAddr, 0, 0);
  
  //*****************************************************************************
  //
  // The canvas widget used to display the board's Ethernet MAC address.  This
  // is required if using the Ethernet boot loader.
  //
  //*****************************************************************************
  Canvas(g_sMACAddr, &g_sBackground, &g_sIPAddr, 0,
         &g_sKitronix320x240x16_SSD2119, 0, 200, 320, 20,
         (CANVAS_STYLE_FILL | CANVAS_STYLE_TEXT),
         ClrBlack, 0, ClrWhite, g_pFontCm20, g_pcMACAddr, 0, 0);
  
  //*****************************************************************************
  //
  // The button used to initiate a boot loader software update.
  //
  //*****************************************************************************
  RectangularButton(g_sPushBtn, &g_sBackground, &g_sMACAddr, 0,
                    &g_sKitronix320x240x16_SSD2119, 60, 110, 200, 40,
                    (PB_STYLE_OUTLINE | PB_STYLE_TEXT_OPAQUE | PB_STYLE_TEXT |
                     PB_STYLE_FILL | PB_STYLE_RELEASE_NOTIFY),
                     ClrDarkBlue, ClrBlue, ClrWhite, ClrWhite,
                     g_pFontCmss22b, "Update Now", 0, 0, 0, 0, OnButtonPress);
  
  //*****************************************************************************
  //
  // The canvas widget acting as the background to the display.
  //
  //*****************************************************************************
  Canvas(g_sBackground, WIDGET_ROOT, 0, &g_sPushBtn,
         &g_sKitronix320x240x16_SSD2119, 0, 23, 320, (240 - 23),
         CANVAS_STYLE_FILL, ClrBlack, 0, 0, 0, 0, 0, 0);
  
  //*****************************************************************************
  //
  // The heading containing the application title.
  //
  //*****************************************************************************
  Canvas(g_sHeading, WIDGET_ROOT, &g_sBackground, 0,
         &g_sKitronix320x240x16_SSD2119, 0, 0, 320, 23,
         (CANVAS_STYLE_FILL | CANVAS_STYLE_OUTLINE | CANVAS_STYLE_TEXT),
         ClrDarkBlue, ClrWhite, ClrWhite, g_pFontCm20, "boot-demo2", 0, 0);
  
  //*****************************************************************************
  //
  // The error routine that is called if the driver library encounters an error.
  //
  //*****************************************************************************
  #ifdef DEBUG
  void
  __error__(char *pcFilename, unsigned long ulLine)
  {
  }
  #endif
  
  //*****************************************************************************
  //
  // This is the handler for this SysTick interrupt.  We use this to provide the
  // required timer call to the lwIP stack.
  //
  //*****************************************************************************
  void
  SysTickHandler(void)
  {
      //
      // Call the lwIP timer.
      //
      lwIPTimer(1000 / TICKS_PER_SECOND);
  }
  
  //*****************************************************************************
  //
  // Passes control to the bootloader and initiates a remote software update.
  //
  // This function passes control to the bootloader and initiates an update of
  // the main application firmware image via UART0, Ethernet or USB depending
  // upon the specific boot loader binary in use.
  //
  // \return Never returns.
  //
  //*****************************************************************************
  void
  JumpToBootLoader(void)
  {
      //
      // We must make sure we turn off SysTick and its interrupt before entering
      // the boot loader!
      //
      ROM_SysTickIntDisable();
      ROM_SysTickDisable();
  
      //
      // Disable all processor interrupts.  Instead of disabling them
      // one at a time, a direct write to NVIC is done to disable all
      // peripheral interrupts.
      //
      HWREG(NVIC_DIS0) = 0xffffffff;
      HWREG(NVIC_DIS1) = 0xffffffff;
  
      //
      // Return control to the boot loader.  This is a call to the SVC
      // handler in the boot loader.
      //
      (*((void (*)(void))(*(unsigned long *)0x2c)))();
  }
  
  //*****************************************************************************
  //
  // Perform the initialization steps required to start up the Ethernet controller
  // and lwIP stack.
  //
  //*****************************************************************************
  void
  SetupForEthernet(void)
  {
      unsigned long ulUser0, ulUser1;
      unsigned char pucMACAddr[6];
  
      //
      // Configure SysTick for a 100Hz interrupt.
      //
      ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / TICKS_PER_SECOND);
      ROM_SysTickEnable();
      ROM_SysTickIntEnable();
  
      //
      // Configure the pins used to control the Ethernet LEDs.
      //  LED0        PF3   Output
      //  LED1        PF2   Output
      //
      GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
  
      //
      // Get the MAC address from the UART0 and UART1 registers in NV ram.
      //
      ROM_FlashUserGet(&ulUser0, &ulUser1);
  
      //
      // Convert the 24/24 split MAC address from NV ram into a MAC address
      // array.
      //
      pucMACAddr[0] = ulUser0 & 0xff;
      pucMACAddr[1] = (ulUser0 >> 8) & 0xff;
      pucMACAddr[2] = (ulUser0 >> 16) & 0xff;
      pucMACAddr[3] = ulUser1 & 0xff;
      pucMACAddr[4] = (ulUser1 >> 8) & 0xff;
      pucMACAddr[5] = (ulUser1 >> 16) & 0xff;
  
      //
      // Format this address into the string used by the relevant widget.
      //
      usnprintf(g_pcMACAddr, SIZE_MAC_ADDR_BUFFER,
                "MAC: %02X-%02X-%02X-%02X-%02X-%02X",
                pucMACAddr[0], pucMACAddr[1], pucMACAddr[2], pucMACAddr[3],
                pucMACAddr[4], pucMACAddr[5]);
  
      //
      // Remember that we don't have an IP address yet.
      //
      usnprintf(g_pcIPAddr, SIZE_IP_ADDR_BUFFER, "IP: Not assigned");
  
      //
      // Initialize the lwIP TCP/IP stack.
      //
      lwIPInit(pucMACAddr, 0, 0, 0, IPADDR_USE_DHCP);
  }
  
  //*****************************************************************************
  //
  // Initialize UART0 and set the appropriate communication parameters.
  //
  //*****************************************************************************
  void
  SetupForUART(void)
  {
      //
      // We need to make sure that UART0 and its associated GPIO port are
      // enabled before we pass control to the boot loader.  The serial boot
      // loader does not enable or configure these peripherals for us if we
      // enter it via its SVC vector.
      //
      ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
      ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
  
      //
      // Set GPIO A0 and A1 as UART.
      //
      ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
  
      //
      // Configure the UART for 115200, n, 8, 1
      //
      ROM_UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
                              (UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE |
                               UART_CONFIG_WLEN_8));
  
      //
      // Enable the UART operation.
      //
      ROM_UARTEnable(UART0_BASE);
  }
  
  //*****************************************************************************
  //
  // Enable the USB controller
  //
  //*****************************************************************************
  void
  SetupForUSB(void)
  {
      //
      // The USB boot loader takes care of all required USB initialization so,
      // if the application itself doesn't need to use the USB controller, we
      // don't actually need to enable it here.  The only requirement imposed by
      // the USB boot loader is that the system clock is running from the PLL
      // when the boot loader is entered.
      //
  }
  
  //*****************************************************************************
  //
  // This function is called by the graphics library widget manager whenever the
  // "Update Now" button is pressed.  It sets a flag that the main loop checks
  // and, when set, causes control to transfer to the boot loader.
  //
  //*****************************************************************************
  void
  OnButtonPress(tWidget *pWidget)
  {
      g_bFirmwareUpdate = true;
  }
  
  //*****************************************************************************
  //
  // A simple application demonstrating use of the boot loader,
  //
  //*****************************************************************************
  int
  main(void)
  {
      unsigned long ulIPAddr;
  
      //
      // 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);
  
      //
      // Set the device pinout appropriately for this board.
      //
      PinoutSet();
  
      //
      // 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();
      SetupForEthernet();
      SetupForUSB();
  
      //
      // Enable Interrupts
      //
      ROM_IntMasterEnable();
  
      //
      // Initialize the display driver.
      //
      Kitronix320x240x16_SSD2119Init();
  
      //
      // Initialize the touch screen driver.
      //
      TouchScreenInit();
  
      //
      // Set the touch screen event handler.
      //
      TouchScreenCallbackSet(WidgetPointerMessage);
  
      //
      // Add the compile-time defined widgets to the widget tree.
      //
      WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sHeading);
  
      //
      // Paint the widget tree to make sure they all appear on the display.
      //
      WidgetPaint(WIDGET_ROOT);
  
      //
      // We don't have an IP address yet so clear the variable to tell us to
      // check until we are assigned one.
      //
      ulIPAddr = 0;
  
      //
      // Loop forever, processing widget messages.
      //
      while(!g_bFirmwareUpdate)
      {
          //
          // Do we have an IP address yet? If not, check to see if we've been
          // assigned one since the last time we checked.
          //
          if(ulIPAddr == 0)
          {
              //
              // What is our current IP address?
              //
              ulIPAddr = lwIPLocalIPAddrGet();
  
              //
              // If it's non zero, update the display.
              //
              if(ulIPAddr)
              {
                  usprintf(g_pcIPAddr, "IP: %d.%d.%d.%d",
                           ulIPAddr & 0xff, (ulIPAddr >> 8) & 0xff,
                           (ulIPAddr >> 16) & 0xff,  ulIPAddr >> 24);
                  WidgetPaint((tWidget *)&g_sIPAddr);
              }
          }
  
          //
          // Process any messages from or for the widgets.
          //
          WidgetMessageQueueProcess();
      }
  
      //
      // If we drop out, the user has pressed the "Update Now" button so we
      // tidy up and transfer control to the boot loader.
      //
  
      //
      // Tell the user that we got their instruction.
      //
      PushButtonTextSet(&g_sPushBtn, "Updating...");
      WidgetPaint((tWidget *)&g_sPushBtn);
  
      //
      // Process all remaining messages on the queue (including the paint message
      // we just posted).
      //
      WidgetMessageQueueProcess();
  
      //
      // 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);
  }
  