/************************************************************************************
* This is the public header file for the Debug module. 
*
* Author(s): BPPED1
*
* (c) Copyright 2004, Freescale, 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: 10-03-04 12:34 $
*   $Author: Bpped1 $
*   $Revision: 9 $
*   $Workfile: Debug.h $
************************************************************************************/

/************************************************************************************
DEBUG FEATURE DESCRIPTION

TOC:
1. Intoducation
2. Printing Text.
 2.1 Windows Console
 2.2 Serial Port
 2.3 LCD panel
3. Asserts
 2.1 Simulator asserts
 2.2 Asserts with BDM debugger
 2.3 Asserts with LCD panel
4. LED's
5. Buzzer

1. INTRODUCTION
The Debug functionality is used for various tasks such as outputting debug text,
and asserting on unexpected behaviour. The following sections will describe each
of the available debug features, and their possible configurations. The features
are configured by conditional compiler directives defined in Target.h, and this
header file.

2. PRINTING TEXT --------------------------------------------------------------
The main function for printing text is Dbg_Print. It is a downscaled version of
printf. I.e. it does only offer very basic formatting. The %s, and %d format
specifiers are available. Only the %s specifier takes a length parameter such
as %08s. The length must consist of two digits if it is included in the string.
A minimum number of chars specified by the length parameter is printed, and
spaces are appended if the sub-string is shorter than the minimum length.

In order to use Dbg_Print(), ENABLE_DPRINTF must be defined in Debug.h.

Other functions are also available for outputting text (please see the comments,
and description at the function declarations):
Dbg_PutStr - Print a string
Dbg_PutHexNum - Print a hexadecimal number.

The following sub-sections describe the possible output methods currently
supported for printing text. The methods are mutually exclusive, and it is not
defined what will happen if selecting two or more methods at the same time.

2.1 WINDOWS CONSOLE
If ENABLE_DPRINTF is defined, and the code is compiled for WinSim, the Dbg_Print
function will as default print to the Console using putchar() from <stdio.h>.
Nothing needs to be defined in order to redirect the output to the console, but
DBG_PRINT_TO_CONSOLE may be defined to clarify the intentions of the developer. 

2.2 SERIAL PORT
In order to redirect the Debug output to the serial port, a special cable, and
VHDL code for the SPI logger is required. The Debug output is written one byte
at a time to 8 GPIO's (8bit parallel), and converted to a serial stream @115200bps
by the modified SPI logger. The serial stream is looped back to the demo boards
COM2 port.

To redirect the printer output to the serial port DBG_PRINT_TO_COMPORT must be
defined. Please note that the output over the serial port may be slow, and thus
impair performance if called from timing sensitive code.

2.3 LCD PANEL
The LCD panel is a little limited for printing text because it only supports two
lines of text with 16 chars in each line. However, this may be sufficient for
simple debugging purposes. The Dbg_Print function will toggle the line in the
LCD display if it comes across a '\n' in the string. Otherwise, it will stay in
the current line, and everything after 16 characters is not visible. Though, a
'\n' that occurs after 16 chars of text has been printed will still toggle the
lines.

To redirect the printer output to the LCD panel DBG_PRINT_TO_LCDDISP must be
defined. Please note that the output to the LCD panel is quite slow, and thus
impair performance if called from timing sensitive code.

3. ASSERTS --------------------------------------------------------------------
Asserts can be used in two ways. Either the assert will halt the code, or the
assert will just notify the software developer that some (unexpected) event has
occured. In order to use asserts at all, ENABLE_ASSERTS must be defined in the
global header file Target.h. This header file also contains the switches for
enabling/disabling asserts for various modules. Currently the following switches
are available: PHY_ASSERT, MAC_ASSERT, SEQ_ASSERT, MEM_ASSERT, and SMAPI_ASSERT
(See Target.h for the list of switches. The list may be out of sync with this text).
These switches will halt the code on an assert condition. If the swithes are
appended with _INFO (e.g. MAC_ASSERT_INFO), then the assert will not halt the
code but only generate some sort of notification of the assert condition.

There are two options for asserts that halt the code. The first option will only
work if the demo board has a BDM debugger connected. This will set a bit in the
HCS08 CPU which enables the use of SW breakpoints. If ENABLE_ASSERT_BREAKPOINT is
defined in Target.h then the asserts will utilize the SW breakpoints.

The second option is relevant if no BDM debugger is connected. In this case the
ENABLE_ASSERT_BREAKPOINT switch must not be defined. Instead of the SW breakpoint,
an infinite loop is used for trapping the code in the assert function.

2.1 SIMULATOR ASSERTS
No options are available for the WinSim asserts. The assert function of the Visual
Studio development environment is used. The WinSim asserts are always enabled.

2.2 ASSERTS WITH SERIAL PORT
If ENABLE_ASSERT_TEXTUAL is defined in Target.h, and DBG_PRINT_TO_COMPORT in this
Debug.h, the line number and filename of the assert is printed to the serial port.

2.3 ASSERTS WITH LCD PANEL
If ENABLE_ASSERT_TEXTUAL, and DBG_PRINT_TO_LCDDISP is defined in Target.h, the line
number and filename of the assert is printed to the LCD panel. It's possible to
save a bit of code space by defining ENABLE_LCD instead of DBG_PRINT_TO_LCDDISP if
the LCD panel is only used for asserts, and not general Dbg_Print output.

4. LED'S ----------------------------------------------------------------------
Use SetLeds() with gLedOn_c/gLedOff_c, and gLed1_c, gLed2_c, gLed3_c, gLed4_c,
or gLedAll_c to turn on or off the various LEDs. ORed Combinations are possible.
ENABLE_LEDS must be defined to control the LEDs.

5. BUZZER ---------------------------------------------------------------------
The buzzer can be enabled by defining ENABLE_BUZZER. Also define ENABLE_ASSERT_BUZZER
to generate a short blip on asserts. The buzzer will generate a permanent tone by
calling the function BuzzerControl() with the parameter gBuzzerOn_c. To turn the
buzzer off, the function must be called with the parameter gBuzzerOff_c. In order
to generate a tone with a short duration, BuzzerControl() can be called with the
parameter gBuzzerBlip_c. This parameter will block the code execution for the
duration of the tone.

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


#ifndef _DEBUG_H_
#define _DEBUG_H_

#include "NwkMacInterface.h"


/************************************************************************************
*************************************************************************************
* Public macros
*************************************************************************************
************************************************************************************/

/**** Debug configuation options BEGIN ****/

#ifdef PLATFORM_WINDOWS
  // Define output method for Dbg functions. Only define one at a time.

  // Console: Default if compiling for simulator. Is overridden by build environment.
  // Has no real effect but clarifying the intentions of the developer. Will auto-
  // define ENABLE_DPRINTF.
//#define DBG_PRINT_TO_CONSOLE 1

#else  // PLATFORM_GB60
  // Comport: Can be used if special cabling is used to connect the demo board to a
  // sniffer. The sniffer will then redirect the debug output to the COM2 port on the
  // demo board. Port settings are 115200, 8N1, RTS/CTS-FC. Auto-defines ENABLE_DPRINTF.
//#define DBG_PRINT_TO_COMPORT

  // Print to LCD dispaly on demo board. Only two lines of text with each 16 characters.
  // A '\n' will change which line the text is displayed in. If the length of a line of
  // text exceeds 16 characters, only the 16 first are printed. However, a terminating
  // '\n' will still cause a line change. Will auto-define ENABLE_LCD, and ENABLE_DPRINTF.
//#define DBG_PRINT_TO_LCDDISP

#define DBG_NUM_LINES_LCD 2

//#define ENABLE_LCD           // Enable LCD panel
//#define ENABLE_BUZZER        // Enable buzzer function BuzzerControl()
//#define ENABLE_ASSERT_BUZZER // Buzz the buzzer if bugz.
//#define ENABLE_LEDS          // Enable LEDs function SetLeds()

  // Enable this to make the Dbg_SetPortA0() macro set/clr Port A bit 0.
  // can be used for e.g. measuring the time between events using a scope.
//#define DBG_TOGGLE_PORTA0 1

#endif // PLATFORM_WINDOWS

//#define ENABLE_DPRINTF   // Main switch for Debug Print functionality.
//#define DBG_MLME_NWK_LOG // Print out message types between NWK and MLME
//#define DBG_MLME_MEM_LOG // Print out message types between MEM and MLME
//#define DBG_LOG_MAX      // Print all parameters of messages between NWK and MLME

/***** Debug configuation options END *****/

#if defined( DBG_PRINT_TO_COMPORT ) && !defined(ENABLE_DPRINTF)
#define ENABLE_DPRINTF  // Enable Dbg_Print functionality.
#endif
#if defined( DBG_PRINT_TO_LCDDISP ) && !defined(ENABLE_LCD)
#define ENABLE_LCD      // Enable LCD panel for debug output 
#define ENABLE_DPRINTF  // Enable Dbg_Print functionality.
#endif

#ifdef ENABLE_DPRINTF
#ifdef PLATFORM_WINDOWS

int putchar( int c );
#define txchar putchar 
#define txchar_init()
#define Dbg_PutChar(x) putchar((uint8_t)(x))

#else

void txchar(char);
void txchar_init(void);

#ifdef DBG_PRINT_TO_COMPORT
 #define Dbg_PutChar(ch) { txchar((uint8_t)(ch)); if((ch)=='\n') txchar('\r'); }
#else
 #ifdef DBG_PRINT_TO_LCDDISP
  extern void Dbg_PutChar(uint8_t ch);
 #endif DBG_PRINT_TO_LCDDISP
#endif DBG_PRINT_TO_COMPORT

#endif PLATFORM_WINDOWS
#endif ENABLE_DPRINTF


/************************************************************************************
*************************************************************************************
* Public prototypes
*************************************************************************************
************************************************************************************/

#if (defined(ENABLE_DPRINTF) || defined(ENABLE_LCD) || defined(ENABLE_BUZZER) || defined(ENABLE_LEDS) || defined(DBG_TOGGLE_PORTA0)) && defined(PLATFORM_GB60)
void Dbg_Init(void);
#else
#define Dbg_Init()
#endif //(ENABLE_DPRINTF || ASSERTS_USE_LCD || ENABLE_BUZZER || ENABLE_LEDS || DBG_TOGGLE_PORTA0) && PLATFORM_GB60

#if defined(ENABLE_BUZZER) && defined(PLATFORM_GB60)
void BuzzerControl( uint8_t on );
#else
#define BuzzerControl( on )
#endif //ENABLE_BUZZER && PLATFORM_GB60

#if defined(ENABLE_LEDS) && defined(PLATFORM_GB60)
void SetLed(uint8_t led, uint8_t state);
#else
#define SetLed(led, state)
#endif //ENABLE_LEDS && PLATFORM_GB60


#ifdef ENABLE_DPRINTF

uint8_t Dbg_Print(void *dummy, uint8_t *format, ...);

void Dbg_PutStr(uint8_t *pStr);
void Dbg_PutHexNum(uint8_t *pStr, uint8_t len);

#ifdef DBG_MLME_NWK_LOG 
void Dbg_NwkToMlmeLog(mlmeMessage_t *pMsg);
void Dbg_MlmeToNwkLog(nwkMessage_t *pMsg);
#else
#define Dbg_NwkToMlmeLog(pMsg)
#define Dbg_MlmeToNwkLog(pMsg)
#endif //DBG_MLME_NWK_LOG 

#ifdef DBG_MLME_MEM_LOG 
void Dbg_MemToMlmeLog(void *pMsg);
void Dbg_MlmeToMemLog(void *pMsg);
#else
#define Dbg_MemToMlmeLog(pMsg)
#define Dbg_MlmeToMemLog(pMsg)
#endif //DBG_MLME_MEM_LOG 

#else

#define Dbg_NwkToMlmeLog(pMsg)
#define Dbg_MlmeToNwkLog(pMsg)
#define Dbg_MemToMlmeLog(pMsg)
#define Dbg_MlmeToMemLog(pMsg)

#endif ENABLE_DPRINTF

/************************************************************************************
*************************************************************************************
* Public type definitions
*************************************************************************************
************************************************************************************/

#if defined(ENABLE_BUZZER) && defined(PLATFORM_GB60)

enum {
  gBuzzerOn_c,
  gBuzzerOff_c,
  gBuzzerBlip_c,
};

#endif //ENABLE_BUZZER && PLATFORM_GB60

#if defined(ENABLE_LEDS) && defined(PLATFORM_GB60)

enum {
  gLedOff_c,
  gLedOn_c,
};

enum {
  gLed1_c   = 1,
  gLed2_c   = 2,
  gLed3_c   = 4,
  gLed4_c   = 8,
  gLedAll_c = 0x0F,
};
#endif //ENABLE_LEDS && PLATFORM_GB60


#if defined(PLATFORM_GB60) && defined(DBG_TOGGLE_PORTA0)

#pragma DISABLE_WARNING(ConditionAlwaysTrue_c)
#pragma DISABLE_WARNING(ConditionAlwaysFalse_c)

#define Dbg_SetPortA0(state) if((state) == 1) PTAD |=  0x01; /* Raise GPIO Port A bit 0*/ \
                             else             PTAD &= ~0x01  /* Lower GPIO Port A bit 0*/
#define Dbg_SetPortA1(state) if((state) == 1) PTAD |=  0x04; /* Raise GPIO Port A bit 0*/ \
                             else             PTAD &= ~0x04  /* Lower GPIO Port A bit 0*/

#else
#define Dbg_SetPortA0(state)
#define Dbg_SetPortA1(state)
#endif // PLATFORM_GB60 && DBG_TOGGLE_PORTA0

/************************************************************************************
*************************************************************************************
* Public memory declarations
*************************************************************************************
************************************************************************************/

#ifdef DBG_PRINT_TO_LCDDISP
extern uint8_t lcdCurrentLine;
#endif

/************************************************************************************
*************************************************************************************
* Public functions
*************************************************************************************
************************************************************************************/



/************************************************************************************
* This is a template function header.
*   
* Interface assumptions:
*   None
*   
* Return value:
*   None
* 
* Revision history:
*   date      Author    Comments
*   ------    ------    --------
*   DDMMYY     XXX      Created
* 
************************************************************************************/



/************************************************************************************
*************************************************************************************
* Level 1 block comment
*************************************************************************************
************************************************************************************/

//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
// Level 2 block comment
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------

/* Level 3 block comment */




// Delimiters

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

//-----------------------------------------------------------------------------------

#endif /* _TEMPLATE_H_ */
