#ifndef _aplmt_hardware_atmel_avr_atmega128_uart_initapi_h_
#define _aplmt_hardware_atmel_avr_atmega128_uart_initapi_h_
/*----------------------------------------------------------------------------
** SUMMARY: Atmel-ATMega128 Hardware UARTs Initialization Interface (HWIUART)
**
** DESCRIPTION:
**  This file provides the initizaltion inteface(s) for the onboard UART(s)
**  SPECIFIC to Atmel's ATMega128 micro.
**  
** CONFIGURATION 
** -------------
**  COMPILE:     none.
**
**  APPLICATION: The application is REQUIRED to define the following symbol(s)
**                  OPTION_APLMT_DEVICE_UART_BAUDRATE_CLOCK
** 
**  PLATFORM:    none.
----------------------------------------------------------------------------*/

#include "aplcfg.h"                     /* For: Application configuration */
#include "apl/types/types.h"            /* For: basic types */

/** The application is required to define the chip's operating speed */
#if !defined(OPTION_APLMT_DEVICE_UART_BAUDRATE_CLOCK) 
#error The application must define, OPTION_APLMT_DEVICE_UART_BAUDRATE_CLOCK, it is the Targets operating frequency for calculating BAUD Rate
#endif


#ifdef __cplusplus
extern "C" {
#endif


/*-------------- MACROS/CONSTANTS ------------------------------------------*/
/** Use the following macros to determine the 'baud' parameter values for 
    UART initialization routines below.
    NOTES:
        o Both 'baud' and OPTION_APLMT_DEVICE_UART_BAUDRATE_CLOCK SHOULD 
          ALWAYS BE CONSTANTs or lots of code will be generated.
        o OPTION_APLMT_DEVICE_UART_BAUDRATE_CLOCK should be a float point 
          value that is the value of OSC in Hz, i.e: 9.8306e6 Hz -->9.8306Mhz
 */
#define APLMT_HALUART_CALC_BAUD(baud)           ((Apl16u)(((OPTION_APLMT_DEVICE_UART_BAUDRATE_CLOCK) / (16.0 * (baud))) + 0.5) - 1)

/** Baud Rates */
#define APLMT_HALUART_BAUD_115200               (APLMT_HALUART_CALC_BAUD(115200UL))
#define APLMT_HALUART_BAUD_76800                (APLMT_HALUART_CALC_BAUD(76800UL))
#define APLMT_HALUART_BAUD_57600                (APLMT_HALUART_CALC_BAUD(57600UL))
#define APLMT_HALUART_BAUD_38400                (APLMT_HALUART_CALC_BAUD(38400UL))
#define APLMT_HALUART_BAUD_28800                (APLMT_HALUART_CALC_BAUD(28800U))
#define APLMT_HALUART_BAUD_19200                (APLMT_HALUART_CALC_BAUD(19200U))
#define APLMT_HALUART_BAUD_14400                (APLMT_HALUART_CALC_BAUD(14400))
#define APLMT_HALUART_BAUD_9600                 (APLMT_HALUART_CALC_BAUD(9600))
#define APLMT_HALUART_BAUD_4800                 (APLMT_HALUART_CALC_BAUD(4800))
#define APLMT_HALUART_BAUD_2400                 (APLMT_HALUART_CALC_BAUD(2400))
#define APLMT_HALUART_BAUD_1200                 (APLMT_HALUART_CALC_BAUD(1200))

/** Double Speed operation (assuming the target mcu support its) */
#define APLMT_HALUART_2x_SPEED_BIT              0x8000
#define APLMT_HALUART_CALC_2x_BAUD(baud)        (((Apl16u)((OPTION_APLMT_DEVICE_UART_BAUDRATE_CLOCK / (8.0 * (baud))) + 0.5) - 1) | APLMT_HALUART_2x_SPEED_BIT)

/** Baud Rates using the "double speed" operation */
#define APLMT_HALUART_2x_BAUD_115200            (APLMT_HALUART_CALC_2x_BAUD(115200L))
#define APLMT_HALUART_2x_BAUD_76800             (APLMT_HALUART_CALC_2x_BAUD(76800UL))
#define APLMT_HALUART_2x_BAUD_57600             (APLMT_HALUART_CALC_2x_BAUD(57600UL))
#define APLMT_HALUART_2x_BAUD_38400             (APLMT_HALUART_CALC_2x_BAUD(38400UL))
#define APLMT_HALUART_2x_BAUD_28800             (APLMT_HALUART_CALC_2x_BAUD(28800U))
#define APLMT_HALUART_2x_BAUD_19200             (APLMT_HALUART_CALC_2x_BAUD(19200U))
#define APLMT_HALUART_2x_BAUD_14400             (APLMT_HALUART_CALC_2x_BAUD(14400))
#define APLMT_HALUART_2x_BAUD_9600              (APLMT_HALUART_CALC_2x_BAUD(9600))
#define APLMT_HALUART_2x_BAUD_4800              (APLMT_HALUART_CALC_2x_BAUD(4800))
#define APLMT_HALUART_2x_BAUD_2400              (APLMT_HALUART_CALC_2x_BAUD(2400))
#define APLMT_HALUART_2x_BAUD_1200              (APLMT_HALUART_CALC_2x_BAUD(1200))

/** Frame format.  The following constants can be bit-wised ORed
    to generate desired frame structure.
 */
#define APLMT_HALUART_PARITY_NONE               0x00
#define APLMT_HALUART_PARITY_EVEN               0x20
#define APLMT_HALUART_PARITY_ODD                0x30

#define APLMT_HALUART_DATABITS_8                0x06
#define APLMT_HALUART_DATABITS_7                0x04
#define APLMT_HALUART_DATABITS_6                0x02
#define APLMT_HALUART_DATABITS_5                0x00

#define APLMT_HALUART_STOPBITS_1                0x00
#define APLMT_HALUART_STOPBITS_2                0x08


/*-------------- PUBLIC/PUBLISHED API --------------------------------------*/
/** This method initializes the primary onboard uart with the specified 
    framing and baud rate.  This method disables, and leaves disabled, the
    transmitter, receiver, as well as all UART interrupts.
    NOTES:
        o This method MAY be called before and/or after the platform's
          kernel has been started.
        o This method can be called multiple times to reset/re-initialize
          the UART.
 */
void AplmtAtmega128_uartAInitBaudAndFrame( Apl16u baudrate, AplByte frameConfig );

/** This method initializes the secondary onboard uart with the specified 
    framing and baud rate.  This method disables, and leaves disabled, the
    transmitter, receiver, as well as all UART interrupts.
    NOTES:
        o This method MAY be called before and/or after the platform's
          kernel has been started.
        o This method can be called multiple times to reset/re-initialize
          the UART.
 */
void AplmtAtmega128_uartBInitBaudAndFrame( Apl16u baudrate, AplByte frameConfig );


#ifdef __cplusplus
}
#endif
/*--------------------------------------------------------------------------*/
#endif  /* end _aplmt_hardware_atmel_avr_atmega128_uart_initapi_h_ */

