/**
 * \brief Main Application Routine
 *  - Initialize the system clocks
 *  - Initialize the sleep manager
 *  - Initialize the power save measures
 *  - Initialize the ACIFB module
 *  - Initialize the AST to trigger ACIFB at regular intervals
 *  - Go to STATIC sleep mode and wake up on a touch status change
 */
int main(void)
{
	/* Switch on the STATUS LED */
	gpio_clr_gpio_pin(STATUS_LED);
	/* Switch off the error LED. */
	gpio_set_gpio_pin(ERROR_LED);

	/*
	 * Initialize the system clock.
	 * Note: Clock settings are specified in conf_clock.h
	 */
	sysclk_init();

	/*
	 * Initialize the sleep manager.
	 * Note: CONFIG_SLEEPMGR_ENABLE should have been defined in conf_sleepmgr.h
	 */
	sleepmgr_init();
	/* Lock required sleep mode. */
	sleepmgr_lock_mode(SLEEPMGR_STATIC);

	/* Initialize the delay routines */
	delay_init(sysclk_get_cpu_hz());

	/* Initialize the power saving features */
	power_save_measures_init();

	/* Switch off the error LED. */
	gpio_set_gpio_pin(ERROR_LED);

#if DEBUG_MESSAGES
	/* Enable the clock to USART interface */
	sysclk_enable_peripheral_clock(DBG_USART);
	/* Initialize the USART interface to print trace messages */
	init_dbg_rs232(sysclk_get_pba_hz());

	print_dbg("\r Sleepwalking with ACIFB Module in UC3L \n");
	print_dbg("\r Initializing ACIFB Module..... \n");
#endif

	/* Initialize the Analog Comparator peripheral */
	if (ac_init() != STATUS_OK) {
#if DEBUG_MESSAGES
		/* Error initializing the ACIFB peripheral */
		print_dbg("\r Error initializing Analog Comparator module \n");
#endif
		while (1) {
			delay_ms(LED_DELAY);
			gpio_tgl_gpio_pin(ERROR_LED);
		}
	}

#if DEBUG_MESSAGES
	print_dbg("\r ACIFB Module initialized. \n");
	print_dbg("\r Initializing AST Module..... \n");
#endif

	/* Initialize the AST peripheral */
	if (ast_init() != STATUS_OK) {
#if DEBUG_MESSAGES
		print_dbg("\r Error initializing AST module \n");
#endif
		/* Error initializing the AST peripheral */
		while (1) {
			delay_ms(LED_DELAY);
			gpio_tgl_gpio_pin(ERROR_LED);
		}
	}

#if DEBUG_MESSAGES
	print_dbg("\r AST Module initialized. \n");
#endif

	/* Application routine */
	while (1) {
		/* Enable Asynchronous wake-up for ACIFB */
		pm_asyn_wake_up_enable(AVR32_PM_AWEN_ACIFBWEN_MASK);

#if DEBUG_MESSAGES
		print_dbg("\r Going to STATIC sleep mode \n");
		print_dbg(
				"\r Wake up only when input is higher than threshold \n");
#endif

		/* Switch off the status LED */
		gpio_set_gpio_pin(STATUS_LED);
		/* Disable GPIO clock before entering sleep mode */
		sysclk_disable_pba_module(SYSCLK_GPIO);
		AVR32_INTC.ipr[0];
		/* Enter STATIC sleep mode */
		sleepmgr_enter_sleep();
		/* Enable GPIO clock after waking from sleep mode */
		sysclk_enable_pba_module(SYSCLK_GPIO);
		/* Switch on the status LED */
		gpio_clr_gpio_pin(STATUS_LED);

#if DEBUG_MESSAGES
		print_dbg("\r Output higher than threshold \n");
		print_dbg("\n");
#else
		/* LED on for few ms */
		delay_ms(LED_DELAY);
#endif

		/* Clear the wake up & enable it to enter sleep mode again */
		pm_asyn_wake_up_disable(AVR32_PM_AWEN_ACIFBWEN_MASK);
		/* Clear All AST Interrupt request and clear SR */
		ast_clear_all_status_flags(&AVR32_AST);
	}

	return 0;
} /* End of main() */
/**
 * \brief Main Application Routine
 *  - Initialize the system clocks
 *  - Initialize the sleep manager
 *  - Initialize the power save measures
 *  - Initialize the touch library and sensors
 *  - Initialize the AST to trigger CAT at regular intervals
 *  - Go to STATIC sleep mode and wake up on a touch status change
 */
int main(void)
{
#if DEF_TOUCH_QDEBUG_ENABLE == 1
	uint32_t delay_counter;
#endif
	uint32_t i;
	/* Switch on the STATUS LED */
	gpio_clr_gpio_pin(STATUS_LED);
	/* Switch off the error LED. */
	gpio_set_gpio_pin(ERROR_LED);

	/*
	 * Initialize the system clock.
	 * Note: Clock settings are specified in conf_clock.h
	 */
	sysclk_init();

	/*
	 * Initialize the sleep manager.
	 * Note: CONFIG_SLEEPMGR_ENABLE should have been defined in conf_sleepmgr.h
	 */
	sleepmgr_init();
	/* Lock required sleep mode. */
	sleepmgr_lock_mode(SLEEPMGR_STATIC);

	/* Initialize the power saving features */
	power_save_measures_init();

	/* Switch off the error LED. */
	gpio_set_gpio_pin(ERROR_LED);

#if DEF_TOUCH_QDEBUG_ENABLE == 0
	/* Initialize the AST peripheral */
	if (ast_init() != STATUS_OK) {
		/* Error initializing the AST peripheral */
		while (1) {
			for (i = 0; i < 10000; i++) {
			}
			gpio_tgl_gpio_pin(ERROR_LED);
		}
	}

#endif

	/* Initialize the touch library */
	if (touch_api_init() != STATUS_OK) {
		/* Error initializing the touch sensors */
		while (1) {
			for (i = 0; i < 10000; i++) {
			}
			gpio_tgl_gpio_pin(ERROR_LED);
		}
	}

#if DEF_TOUCH_QDEBUG_ENABLE == 1
	/* Enable PBA clock for AST clock to switch its source */
	sysclk_enable_peripheral_clock(QDEBUG_USART);
	/* 
	 * Initialize the QDebug interface.
	 * QT600 USB Bridge two-way QDebug communication.
	 */
	QDebug_Init();
#endif

	/* Turn OFF the Status LED */
	gpio_set_gpio_pin(STATUS_LED);

	/* Loop forever */
	while (1) {
#if DEF_TOUCH_QDEBUG_ENABLE == 1
		/* Process any commands received from QTouch Studio. */
		QDebug_ProcessCommands();

		/*
		 * Send out the Touch debug information data each time when
		 * Touch measurement process is completed.
		 * param - 0x000A -> Enable TOUCH_STATUS_CHANGE &
		 * TOUCH_CHANNEL_REF_CHANGE
		 * qt_lib_flags always for Autonomous QTouch.
		 */
		QDebug_SendData(0x000A);

		/* Delay to avoid sending the data to QTouch Studio too frequently. */
		for (delay_counter = 0u; delay_counter < 1200u;
				delay_counter++) {
		}

#else
		/* Enable Asynchronous Wakeup for CAT module */
		pm_asyn_wake_up_enable(AVR32_PM_AWEN_CATWEN_MASK);
		/* Disable GPIO clock after waking from sleep mode */
		sysclk_disable_pba_module(SYSCLK_GPIO);

		/* Enter STATIC sleep mode */
		sleepmgr_enter_sleep();

		/* Disable Asynchronous Wakeup for CAT module. */
		pm_asyn_wake_up_disable(AVR32_PM_AWEN_CATWEN_MASK);

		/* Clear All AST Interrupt request and clear SR */
		ast_clear_all_status_flags(&AVR32_AST);

		/**
		 * When woken up by Autonomous QTouch interrupt, the
		 * touch_at_status_change_interrupt_callback() is called that
		 * updates the autonomous_qtouch_in_touch status flag.
		 */

		/* Host application code goes here */
#endif
	}
} /* End of main() */