/***********************************************************************
 * $Id:: usbmem_example.c 1750 2009-03-17 22:34:40Z pdurgesh           $
 *
 * Project: Timer driver example
 *
 * Description:
 *     A simple timer driver example.
 *
 ***********************************************************************
 * Software that is described herein is for illustrative purposes only
 * which provides customers with programming information regarding the
 * products. This software is supplied "AS IS" without any warranties.
 * NXP Semiconductors assumes no responsibility or liability for the
 * use of the software, conveys no license or title under any patent,
 * copyright, or mask work right to the product. NXP Semiconductors
 * reserves the right to make changes in the software without
 * notification. NXP Semiconductors also make no representation or
 * warranty that such application will be suitable for the specified
 * use without further testing or modification.
 **********************************************************************/
#include <stdio.h>
#include <string.h>

#include "lpc_types.h"
#include "lpc_irq_fiq.h"
#include "lpc_arm922t_cp15_driver.h"
#include "ea3141_board.h"
#include "lpc313x_timer_driver.h"
#include "lpc313x_intc_driver.h"
#include "lpc313x_ioconf_driver.h"
#include "lpc313x_uart_driver.h"
#include "mscuser.h"
#include "usbhw.h"

UNS_8*  Memory = (UNS_8*)(EXT_SDRAM_BASE + 0x4000);  /* MSC RAM */
//UNS_8  Memory[MSC_MemorySize];

/***********************************************************************
 *
 * Function: uart_string_write
 *
 * Purpose: UART string output helper function
 *
 * Processing:
 *     See function.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 ***********************************************************************/
static void uart_string_write(INT_32 devid, char *pbuff)
{
  UNS_32 len = strlen(pbuff);
  UNS_32 written;

  if (devid != 0)
  {
    while (len > 0)
    {
      written = uart_write(devid, pbuff, len);
      len -= written;
      pbuff += written;
    }
  }
}

/***********************************************************************
 *
 * Function: c_entry
 *
 * Purpose: Application entry point from the startup code
 *
 * Processing:
 *     See function.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Always returns 1
 *
 * Notes: None
 *
 **********************************************************************/
int c_entry(void)
{
  INT_32 uartdev, n;
  char buff[512];

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

  /* Setup miscellaneous board functions */
  ea3141_board_init();

  /* Set virtual address of MMU table */
  cp15_set_vmmu_addr((void *)ISROM_MMU_TTB_BASE);

  /* Initialize interrupt system */
  int_initialize(0xFFFFFFFF);

  /******************************************************************/
  /* Setup UART for 115.2K, 8 data bits, no parity, 1 stop bit */
  uartdev = uart_open((void *) UART_BASE, 0);
  sprintf(buff, "LPC313x USB memory example\r\n");
  uart_string_write(uartdev, buff);

  for (n = 0; n < MSC_ImageSize; n++)       /* Copy Initial Disk Image */
  {
    Memory[n] = DiskImage[n];               /*   from Flash to RAM     */
  }

  /* Install timer interrupts handlers as a IRQ interrupts */
  int_install_irq_handler(IRQ_USB, (PFV) USB_ISR);

  USB_Init();                               /* USB Initialization */

  /* Enable USB interrupts in the interrupt controller */
  int_enable(IRQ_USB);
  /* Enable IRQ interrupts in the ARM core */
  enable_irq();

 USB_Connect(1);                        /* USB Connect */

  while (1)
  {
    //USB_ISR();
  }


  /* Disable timer interrupts in the interrupt controller */
  int_disable(IRQ_USB);

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

  return 1;
}
