/*******************************************************************************
 * (c) Copyright 2008 Actel Corporation.  All rights reserved.
 *
 * Burn-in Design 1.0.101
 * 
 *
 * SVN $Revision:$
 * SVN $Date:$
 */
#include "main.h"
#include "mss_uart.h"
#include "dpuser.h"
#include "dpalg.h"
#include "drivers/mss_watchdog/mss_watchdog.h"
#include "drivers/mss_iap/mss_iap.h"
extern size_t
UART_Polled_Rx
(
   mss_uart_instance_t * this_uart,
   uint8_t * rx_buff,
   size_t buff_size
);
int main(void)
{
    uint8_t rx_buff[1];
    uint32_t i;
    /*--------------------------------------------------------------------------
    * Disable watchdog.
    */
    MSS_WD_disable();
    MSS_UART_init(&g_mss_uart0, MSS_UART_57600_BAUD, MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT );

    hardware_interface = IAP_SEL;
    
    while( 1 )
    {


       /* start the handshake with the host */
       while(!(UART_Polled_Rx ( &g_mss_uart0, rx_buff, 1 )))
          ;
       if(rx_buff[0] == 'h')
          MSS_UART_polled_tx( &g_mss_uart0, (const uint8_t * )"a", 1 );

       while(!(UART_Polled_Rx ( &g_mss_uart0, rx_buff, 1 )))
          ;

       if(rx_buff[0] == 'n')
          MSS_UART_polled_tx( &g_mss_uart0, (const uint8_t * )"d", 1 );

       while(!(UART_Polled_Rx ( &g_mss_uart0, rx_buff, 1 )))
          ;

       if(rx_buff[0] == 's')
          MSS_UART_polled_tx( &g_mss_uart0, (const uint8_t * )"h", 1 );

       while(!(UART_Polled_Rx ( &g_mss_uart0, rx_buff, 1 )))
          ;

       if(rx_buff[0] == 'a')
          MSS_UART_polled_tx( &g_mss_uart0, (const uint8_t * )"k", 1 );

       while(!(UART_Polled_Rx ( &g_mss_uart0, rx_buff, 1 )))
          ;

       if(rx_buff[0] == 'e')
       {
           #ifdef ENABLE_OLED
              menu_show("Beginning Direct IAP","Program....");
           #endif
       }

       /* poll for starting Ack message from the host as an acknowledgment
       *  that the host is ready to send Action code */
       while(!(UART_Polled_Rx ( &g_mss_uart0, rx_buff, 1 )))
          ;
       MSS_UART_polled_tx(&g_mss_uart0,(const uint8_t * )"a",1);
          //poll for Action code from the host */
       while(!(UART_Polled_Rx ( &g_mss_uart0, &Action_code, 1 )))
          ;

       #ifdef ENABLE_OLED
          menu_show("Action code","Received");
       #endif
       MSS_UART_polled_tx(&g_mss_uart0,(const uint8_t * )"a",1);

       #ifdef ENABLE_DEBUG
          dp_display_text("\r\nPerforming IAP actions...");
       #endif
       enable_IAP();

       dp_top();


       #ifdef ENABLE_DEBUG
          dp_display_text("\r\nExit code = ");
          dp_display_value(error_code,DEC);
          dp_display_text("\r\n-------------------------------------------------------------------\r\n\r\n ");
       #endif
       /* Transmit the error code */
       if(error_code == 0u)
       {
           MSS_UART_polled_tx(&g_mss_uart0,"z",1);
       }
       else
       {
            rx_buff[0] = error_code;
            //MSS_UART_polled_tx(&g_mss_uart0,(const uint8_t * )rx_buff,1);
            MSS_UART_polled_tx(&g_mss_uart0,"$",1);
       }

       /* poll for acknowledgment from the host */
       while(!(UART_Polled_Rx ( &g_mss_uart0, rx_buff, 1 )))
    	   ;

       disable_IAP();
       bx_3rd_eNVM_Image();

      }
    return 0;
}
    
