/*
 * FILENAME: sysinit.c
 *
 * Copyright 2011 by InterNiche Technologies Inc. All rights reserved
 *
 * NXP1768 hardware dependent files
 *
 * MODULE: MCB1700
 *
 * ROUTINES: system_init(),
 * ROUTINES: enter_crit_section(), exit_crit_section(),
 * ROUTINES: npalloc(), npfree(), nprealloc(), ahb_alloc(), ahb_free(),
 *
 * PORTABLE: NO
 */

#include "lpc17xx.h"
#include <stdio.h>
#include <rt_misc.h>
#include "iniche_types.h"

#pragma import(__use_no_semihosting_swi)


void uart_init(int);
void write_leds(int n);


/* FUNCTION: system_init()
 *
 * Perform any system initialization tasks
 *
 * PARAMS: none
 *
 * RETURNS: int               0 = success, non-zero = error code
 *
 * Put any system-level initialization here. This might include
 * stack/heap, interrupts, timers, etc.
 */
int
system_init(void)
{
   /* Initialize the system clock */
   SystemInit();
   SystemCoreClockUpdate();

   /* Reset all GPIO pins to default: primary function */
   LPC_PINCON->PINSEL0 = 0x40000050;     /* TXD0, RXD0, TXD1 */
   LPC_PINCON->PINSEL1 = 0x00001555;     /* RXD1, CTS1, DCD1, DSR1, DTR1,
                                            RI1, RTS1 */
   LPC_PINCON->PINSEL2 = 0x00000000;
   LPC_PINCON->PINSEL3 = 0x00000000;
   LPC_PINCON->PINSEL4 = 0x00000000;
   LPC_PINCON->PINSEL5 = 0x00000000;
   LPC_PINCON->PINSEL6 = 0x00000000;
   LPC_PINCON->PINSEL7 = 0x00000000;
   LPC_PINCON->PINSEL8 = 0x00000000;
   LPC_PINCON->PINSEL9 = 0x00000000;
   LPC_PINCON->PINSEL10 = 0x00000000;
 
   LPC_GPIO0->FIODIR = 0x00000000;
   LPC_GPIO1->FIODIR = 0x00000000;
   LPC_GPIO2->FIODIR = 0x00000000;
   LPC_GPIO3->FIODIR = 0x00000000;
   LPC_GPIO4->FIODIR = 0x00000000;
    
   LPC_GPIO0->FIOCLR = 0xffffffff;
   LPC_GPIO1->FIOCLR = 0xffffffff;
   LPC_GPIO2->FIOCLR = 0xffffffff;
   LPC_GPIO3->FIOCLR = 0xffffffff;
   LPC_GPIO4->FIOCLR = 0xffffffff;

   /* configure LED pins (P1.31,29,28, P2.6-P2.2) */
   LPC_GPIO1->FIODIR |= 0xb0000000;     /* LEDs on PORT1 defined as Output */
   LPC_GPIO2->FIODIR |= 0x0000007c;     /* LEDs on PORT2 defined as Output */
   write_leds(0);   

   uart_init(0);              /* Console UART */

   return (0);
}



/* FUNCTION: write_leds
 *
 * Display an integer value on the 8 LEDs
 * Port1 drives LED 0-2, and Port2 drives LED 3-7.
 *
 * PARAM1: int            value to display
 *
 * RETURNS: none
 */
void
write_leds(int n)
{
   unsigned long port1, port2;

   port1 = (((n & 0x4) << 1) | (n & 0x3)) << 28;
   port2 = (n & 0xf8) >> 1;

   LPC_GPIO1->FIOCLR = (~port1) & 0xb0000000;
   LPC_GPIO1->FIOSET = port1;
   LPC_GPIO2->FIOCLR = (~port2) & 0x0000007c;
   LPC_GPIO2->FIOSET = port2;
}



/*** Retargeting printf() ************************************************/
/* Refer to the Libraries and Floating Point Support Guide for additional
 * information.
 */

void dputchar(int ch);
int  uart_getc(int unit);


struct __FILE
{
   int handle;

   /*
    * Add whatever you require here.
    * If the only file you are using is standard output using
    * printf() for debugging, no file handling is required.
    */
};

/* FILE is typedef'd in stdio.h. */

FILE  __stdin;
FILE  __stdout;

int fputc(int ch, FILE *f)
{
   /* Output the character to the UART console */
   /* <LF> is expanded into <CR><LF> */
   dputchar(ch);
   return (ch);
}


int fgetc(FILE *f)
{
  return (uart_getc(0));
}


int ferror(FILE *f)
{
  /* Your implementation of ferror(). */
  return (0);
}


void _ttywrch(int c) {
  dputchar(c);
}


void _sys_exit(int return_code)
{
label:
   goto label;  /* endless loop */
}


#ifdef  DEBUG

/*******************************************************************************
* @brief		Reports the name of the source file and the source line number
* 				where the CHECK_PARAM error has occurred.
* @param[in]	file Pointer to the source file name
* @param[in]    line assert_param error line source number
* @return		None
*******************************************************************************/
void
check_failed(uint8_t *file, uint32_t line)
{
   dprintf("Wrong parameters value: file %s on line %d\r\n", file, line);
   dtrap();
}

#endif

