/* $Id$ */
/**
 * @file main.c
 * @date 2007-Dez-11
 * @author S. Rohdemann, S. Radtke
 * @c (C) 2007 Nanotron Technologies
 * @brief System main loop for AVR portation.
 *
 * @todo making this file doxygen compatible.
 *
 * @note BuildNumber = "BuildNumber : 2267";
 *
 * @note This file contains the source code for the system main loop
 *       that controls the nanoLOC devkit board.
 *
 */

/*
 * $Log$
 */

#include 	"avrport.h"
#include	"led.h"
#ifdef CONFIG_NTRX_POWER_MNGT
#include 	"pwrmngt.h"
#endif
#include	"ntrxutil.h"
#include 	<avr/sleep.h>
#include	<avr/wdt.h>
#include    "config.h"
#include    "ntrxtypes.h"
#include    "hwclock.h"
#include	"phy.h"
#include    <stdio.h>


#ifdef	CONFIG_CONSOLE
#include    "usart.h"
#endif	/* CONFIG_CONSOLE */

extern  void    APLInit(void);
extern	void	APLPoll(void);

#ifdef CONFIG_WATCHDOG
#include 	<avr/wdt.h>
#endif


/**
 * error_handler:
 *
 * error_handler() print out the error number and stop the system.
 *
 * Returns: None
 */
void	error_handler(MyInt16T err)
{
#ifdef	CONFIG_CONSOLE
#ifdef	CONFIG_PRINTF
	printf("ErrorHandler: error %d\n", err);
	puts("System halted!\n");
#endif	/* CONFIG_PRINTF */
#endif	/* CONFIG_CONSOLE */
	LED1 (LED_ON);
	LED2 (LED_ON);
	LED3 (LED_ON);
	LED4 (LED_ON);
	LED5 (LED_ON);
	LED6 (LED_ON);
	LED7 (LED_ON);

	while(1);
}



/**
 * main:
 *
 * The main() function of the PPS
 *
 * Returns: None.
 */
int	main(int ac, char *av[])
{
	/*
     * LEDs are on Port C off avr board
     */
	LED0_DIR |= LED0_PIN;						/* Set LED0 port as output */
	LED1_DIR |= LED1_PIN;						/* Set LED1 port as output */
	LED2_DIR |= LED2_PIN;						/* Set LED2 port as output */
	LED3_DIR |= LED3_PIN;						/* Set LED3 port as output */
	LED4_DIR |= LED4_PIN;						/* Set LED4 port as output */
	LED5_DIR |= LED5_PIN;						/* Set LED5 port as output */
	LED6_DIR |= LED6_PIN;						/* Set LED6 port as output */
	LED7_DIR |= LED7_PIN;						/* Set LED7 port as output */

	LED0(LED_OFF);
	LED1(LED_OFF);
	LED2(LED_OFF);
	LED3(LED_OFF);
	LED4(LED_OFF);
	LED5(LED_OFF);
	LED6(LED_OFF);
	LED7(LED_OFF);
	/*
     * Initialize the timer for periodic tasks
     * or time supervized actions
     */
	hwclock_init();

#	ifdef	CONFIG_CONSOLE
	console_init();
#	endif	/* CONFIG_CONSOLE */

	/*
	 * Initialize the ntrx driver
	 */
 	NTRXInit();
	PHYInit ();

	/*
	 * Start the ntrx driver
	 */
#	if CONFIG_REG_MAP_NR != 501
	NTRXStart();
#	endif
	/*
     * initialize user application
     */
	APLInit();

	/*
	 * SYSTEM MAIN LOOP
	 */
	while(1)
	{
		/*
		 * call application periodically
         */
		APLPoll ();
		/*
         * Check for incomming packets
         */
		PHYPoll ();
	}
	return	0;
}
