/*******************************************************************************
 * (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 "DirectC/dpuser.h"
#include "DirectC/dpalg.h"
#include "mss_watchdog.h"
#include "mss_iap.h"
#ifdef ENABLE_OLED
   #include "./Drivers/i2c_driver/i2c.h"
   #include "./Drivers/oled_driver/oled.h"
   /* Configuration for OLED */
   #define OLED_I2C_INSTANCE    &g_mss_i2c0
   struct oled_data                write_data;
   #define FIRST_CHARACTER 0
   /**************************************************************************/
   /* Functions to show the menu on OLED */
   /**************************************************************************/
   void menu_show(char *line1, char *line2)
   {
      write_data.string1        = line1;
      write_data.string2        = line2;
      OLED_write_data(&write_data,BOTH_LINES);
      return;
   }
#endif

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];
   /*--------------------------------------------------------------------------
   * 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;
   #ifdef ENABLE_OLED
      OLED_init();
      write_data.line1          = FIRST_LINE;
      write_data.char_offset1   = FIRST_CHARACTER;
      write_data.line2          = SECOND_LINE;
      write_data.char_offset2   = FIRST_CHARACTER;
      write_data.contrast_val                 = OLED_CONTRAST_VAL;
      write_data.on_off                       = OLED_HORIZ_SCROLL_OFF;
      write_data.column_scrool_per_step       = OLED_HORIZ_SCROLL_STEP;
      write_data.start_page                   = OLED_START_PAGE;
      write_data.time_intrval_btw_scroll_step = OLED_HORIZ_SCROLL_TINVL;
      write_data.end_page                     = OLED_END_PAGE;
   #endif
   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 */
      MSS_UART_polled_tx(&g_mss_uart0,&error_code,1);
      /* poll for acknowledgement from the host */
      while(!(UART_Polled_Rx ( &g_mss_uart0, rx_buff, 1 )))
      disable_IAP();
   }
   return 0;
}

