/************************************************************************************
* Main file for PTC (Test). Includes Main routine.
*
* Author(s): Jakob Koed
*
* (c) Copyright 2004, Freescale Semiconductor, Inc. All rights reserved.
*
* Freescale Confidential Proprietary
* Digianswer Confidential
*
* No part of this document must be reproduced in any form - including copied,
* transcribed, printed or by any electronic means - without specific written
* permission from Freescale.
*
* Last Inspected:
* Last Tested:
*
* Source Safe revision history (Do not edit manually)
*   $Date: 11-03-04 15:37 $
*   $Author: Bpped1 $
*   $Revision: 39 $
*   $Workfile: Main.c $
************************************************************************************/

#include "App_Target.h"
#include "DigiType.h"
#include "MacPhy.h"
#if !defined( INCLUDE_802_15_4 )
#include "PhyMacMsg.h"
#include "../Ptc/TestParserNwkMac.h"
#include "../Ptc/TestUart.h"
#endif // !defined( INCLUDE_802_15_4 )
#include "NV_Data.h"


#ifdef BOOTLOADER_ENABLED
	#include "Embedded_Bootloader.h"
#endif BOOTLOADER_ENABLED

#ifdef FLASH_LIB_ENABLED
    #include "Update_NVM.h"
#endif FLASH_LIB_ENABLED


/************************************************************************************
*************************************************************************************
* Version for embedded debugging via UART or BDM interface
*************************************************************************************
************************************************************************************/

#include "Gb60_io.h" /* include peripheral declarations */
#include "hwdrv_hcs08.h"

// NOTE!!! This include line is only added to get the symbols in the build. The linker will remove these symbols if not referenced.
// Hope to find a better way 21.10.03 MVC 
#include "..\Sys\Crt0.h"

/**********************************************************************/
// NOTE!!! This code is only added to get the symbols in the build. The linker will remove these symbols if not referenced.
// Hope to find a better way 21.10.03 MVC 

typedef void(*ISR_func_t)(void);
extern const ISR_func_t ISR_vectors[]; 
extern const ISR_func_t Reset_vector[];	 // The system reset vector

#ifdef BOOTLOADER_ENABLED
  extern const uint8_t interface_status;
  extern const uint8_t boot_loader_flag;
#endif BOOTLOADER_ENABLED

/**********************************************************************/

extern void FLib_MemCpy(void *pDst, void *pSrc, uint8_t n);
extern uint8_t aExtendedAddress[];
/**********************************************************************/

#ifdef FLASH_LIB_ENABLED
    extern const uint8_t HCS08_Flash_Lib_Version_String[];
#endif FLASH_LIB_ENABLED

/**********************************************************************/

/**********************************************************************/
/**********************************************************************/
// If the code is build as a stand-alone application, this function is
// the main entry point.

#if defined( INCLUDE_802_15_4 )

void Init_802_15_4(void) {

#else

void main(void) {

#endif INCLUDE_802_15_4

#pragma MESSAGE DISABLE C1420 /* WARNING C1420: Result of function-call is ignored */

/**********************************************************************/

// NOTE!!! This code is only added to get the symbols in the build. The linker will remove these symbols if not referenced.
// Hope to find a better way 21.10.03 MVC 

#ifdef FLASH_LIB_ENABLED
  if(HCS08_Flash_Lib_Version_String[0] == 0)
    DisableInterrupts;
#endif FLASH_LIB_ENABLED

#ifndef BOOTLOADER_ENABLED
  if(none_volatile_struct.nv_backkey[0] == 0)
    DisableInterrupts;
  if(Reset_vector[0] == (ISR_func_t)0x0000)
    DisableInterrupts;
#endif BOOTLOADER_ENABLED
  if(ISR_vectors[0] == (ISR_func_t)0x0000)
    DisableInterrupts;
  if((Freescale_Copyright[0] == 0x00) || (Firmware_Database_Label[0] == 0x00) ||
     (MAC_Version[0] == 0x00) || (PHY_Version[0] == 0x00) ||	(NV_RAM_ptr->Freescale_Copyright[0] == 0x00) ||
     (NV_RAM0.MAC_Address[0] == 0x00))
  {
    DisableInterrupts;
    NV_Data_Init();
  }

#ifdef BOOTLOADER_ENABLED
  if(boot_loader_control == ((uint8_t)0x00))
    DisableInterrupts;
  if(boot_loader_flag == ((uint8_t)0x00))
    DisableInterrupts;

  BootLoader_Interface_Init();
#endif BOOTLOADER_ENABLED

/**********************************************************************/
#ifdef FLASH_LIB_ENABLED
  NV_Flash_Setup();
#endif FLASH_LIB_ENABLED

  // Insert correct IEEE address from NV RAM
  FLib_MemCpy(aExtendedAddress, (NV_RAM_Struct_t *)NV_RAM_ptr->MAC_Address, 8);

/**********************************************************************/

  HwSetup();

  InitializePhy();
  InitializeMac(TRUE);

#if defined( INCLUDE_802_15_4 )
  EnableInterrupts; /* enable interrupts */
} // End of Init_802_15_4()
#else 

  InitTestInterface(); // Initialises the UART
  PTC_NWKMAC_NwkInit(); // Make sure queue is initialized
  //PTC_APPASP_AppInit(); // Make sure queue is initialized


#ifdef BOOTLOADER_ENABLED
	
  // Push all 4 buttons from reset to active BootLoader
  // Next time the board is reset it will start up in BootLoader mode
  if(mSWITCH_PORT_GET == mSWITCH_MASK)
  {
     Enable_Download_Firmware(ALL_BIT_ENABLED, DO_UPDATE_FIRMWARE);
            
     // Do a soft reset (execute illegal instruction
     Hard_Reset();
            
     // Wait for manual reset
//   for(;;);
  }

#endif BOOTLOADER_ENABLED

  EnableInterrupts; /* enable interrupts */
 
  for(;;) {
       
    PTC_NWKMAC_NwkMainLoop();	// Do NWK-similar polling
    //PTC_APPASP_AppMainLoop(); // Do APP-similar polling

  } /* loop forever */
#pragma MESSAGE DEFAULT C1420 /* WARNING C1420: Result of function-call is ignored */
}

#endif INCLUDE_802_15_4 // End of main()

/***********************************************************************************/
