/*
** ###################################################################
**
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**
**     Filename  : AS1.C
**
**     Project   : Full1
** 
**     Processor : MC68HC908AZ60MFU
**
**     Beantype  : AsynchroSerial
**
**     Version   : Bean 02.133, Driver 01.02, CPU db: 2.81.019
**
**     Compiler  : Metrowerks HC08 C Compiler V-5.0.13
**
**     Date/Time : 2.4.2002, 9:36
**
**     Abstract  :
**
**         This bean "AsynchroSerial" implements an asynchronous serial
**         communication. The bean supports different settings of 
**         parity, word width, stop-bit and communication speed,
**         user can select interrupt or polling handler.
**         Communication speed can be changed also in runtime.
**         The bean requires one on-chip asynchronous serial channel.
**
**     Settings  :
**
**         Serial channel              : SCI
**
**         Protocol
**             Init baud rate          : 2400baud
**             Width                   : 8 bits
**             Stop bits               : 1
**             Parity                  : none
**             Breaks                  : Disabled
**
**         Registers
**             Input buffer            : SCDR      [24]
**             Output buffer           : SCDR      [24]
**             Control register        : SCC1      [19]
**             Mode register           : SCC2      [20]
**             Baud setting reg.       : SCBR      [25]
**             Special register        : SCS1      [22]
**
**
**
**         Used pins                   : 
**             ----------------------------------------------------
**               Function | On package |    Name
**             ----------------------------------------------------
**                Input   |     14     |  PTE1_RxD
**                Output  |     13     |  PTE0_TxD
**             ----------------------------------------------------
**
**
**
**     Contents  :
**
**         RecvChar        - byte AS1_RecvChar(word *Chr);
**         SendChar        - byte AS1_SendChar(word Chr);
**         GetCharsInRxBuf - word AS1_GetCharsInRxBuf(void);
**         GetCharsInTxBuf - word AS1_GetCharsInTxBuf(void);
**         GetError        - byte AS1_GetError(byte *Err);
**
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2002
**
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
**
** ###################################################################
*/

/* MODULE AS1. */
#pragma MESSAGE DISABLE C4002 /* WARNING C4002: Result not used is ignored */

#include "AS1.h"
#include "PWM1.h"
#include "PPG1.h"
#include "TI1.h"
#include "AD2.h"
#include "SM1.h"
#include "CAN1.h"
#include "Byte1.h"
#include "Bits1.h"
#include "Bits2.h"
#include "Bits3.h"
#include "Bit1.h"
#include "Bits4.h"
#include "Bits5.h"
#include "Bits6.h"
#include "EInt1.h"


#define OVERRUN_ERR      1             /* Overrun error flag bit   */
#define FRAMING_ERR      2             /* Framing error flag bit   */
#define PARITY_ERR       4             /* Parity error flag bit    */
#define CHAR_IN_RX       8             /* Char is in RX buffer     */
#define FULL_TX          16            /* Full transmit buffer     */
#define RUNINT_FROM_TX   32            /* Interrupt is in progress */
#define FULL_RX          64            /* Full receive buffer      */
#define NOISE_ERR        128           /* Noise erorr flag bit     */
#define IDLE_ERR         256           /* Idle character flag bit  */
#define BREAK_ERR        512           /* Break detect             */

static word SerFlag;                   /* Flags for serial communication */
                                       /* Bits: 0 - OverRun error */
                                       /*       1 - Framing error */
                                       /*       2 - Parity error */
                                       /*       3 - Char in RX buffer */
                                       /*       4 - Full TX buffer */
                                       /*       5 - Unused */
                                       /*       6 - Full RX buffer */
                                       /*       7 - Noise error */
                                       /*       8 - Idle character  */
                                       /*       9 - Break detected  */
                                       /*      10 - Unused */
static word ErrFlag;                   /* Error flags mirror of SerFlag */


/*
** ===================================================================
**     Method      :  AS1_HWEnDi (bean AsynchroSerial)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void AS1_HWEnDi(void)
{
    SCC1_ENSCI = 1;                    /* Enable device */
    SCC2_TE = 1;                       /* Enable transmitter */
    SCC2_RE = 1;                       /* Enable receiver */
}


/*
** ===================================================================
**     Method      :  AS1_RecvChar (bean AsynchroSerial)
**
**     Description :
**         If any data received, this method returns one character,
**         otherwise it returns error code (it does not wait for
**         data). This method is enabled only if the receiver
**         property is enabled.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Chr             - Pointer to received character
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_RXEMPTY - No data in receiver
**                           ERR_OVERRUN - Overrun error is detected
**                           ERR_FRAMING - Framing error is detected
**                           ERR_PARITY - Parity error is detected
** ===================================================================
*/
byte AS1_RecvChar(byte *Chr)
{
  byte Result = ERR_OK;                /* Return error code */

  if (!(SCS1_SCRF) && !(SCS1 & 15))    /* Is the reciver empty and no error is set? */
    return ERR_RXEMPTY;                /* If yes then error */
  if (SCS1_IDLE)
    SerFlag |= IDLE_ERR;               /* Idle character flag */
  if (SCS1_PE)
    SerFlag |= PARITY_ERR;             /* Parity error flag */
  if (SCS1_NF)
    SerFlag |= NOISE_ERR;              /* Noise error flag */
  if (SCS1_OR)
    SerFlag |= OVERRUN_ERR;            /* Overrun error flag */
  if (SCS2_BKF)
    SerFlag |= BREAK_ERR;              /* Break flag */
  if (SCS1_FE)
    SerFlag |= FRAMING_ERR;            /* Framming error flag */
  *Chr = SCDR;                         /* Read data from the receiver */
  ErrFlag = SerFlag;                   /* Copy SerFlag status to ErrorFlag status variable */
  if((SerFlag & OVERRUN_ERR) != 0)     /* Is the overrun error detected? */
    Result = ERR_OVERRUN;              /* If yes then set the overrun error flag */
  else if((SerFlag & BREAK_ERR) != 0)  /* Is the break error detected? */
    Result = ERR_BREAK;                /* If yes then set the break error flag */
  else if((SerFlag & PARITY_ERR) != 0) /* Is the parity error detected? */
    Result = ERR_PARITY;               /* If yes then set the parity error flag */
  else if((SerFlag & FRAMING_ERR) != 0) /* Is the framing error detected? */
    Result = ERR_FRAMING;              /* If yes then set the framing error flag */
  else if((SerFlag & NOISE_ERR) != 0)  /* Is the noise error detected? */
    Result = ERR_NOISE;                /* If yes then set the noise error flag */
  SerFlag &= ~(OVERRUN_ERR|FRAMING_ERR|PARITY_ERR|NOISE_ERR|BREAK_ERR); /* Clear all errors in the status variable */
  return Result;                       /* Return error code */
}

/*
** ===================================================================
**     Method      :  AS1_SendChar (bean AsynchroSerial)
**
**     Description :
**         Send one character to the channel. This method is
**         available only if the transmitter property is enabled.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Chr             - Character to send
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_TXFULL - Transmitter is full
** ===================================================================
*/
byte AS1_SendChar(byte Chr)
{
  if(!SCS1_SCTE)                       /* Is the transmitter empty? */
    return ERR_TXFULL;                 /* If no then error */
  SCDR = (byte)Chr;
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AS1_GetCharsInRxBuf (bean AsynchroSerial)
**
**     Description :
**         Return number of characters in the input buffer. This
**         method is available only if the receiver property is
**         enabled.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the input
**                           buffer.
** ===================================================================
*/
word AS1_GetCharsInRxBuf(void)
{
  return SCS1_SCRF;                    /* Return number of chars in receive buffer */
}

/*
** ===================================================================
**     Method      :  AS1_GetCharsInTxBuf (bean AsynchroSerial)
**
**     Description :
**         Return number of characters in the output buffer. This
**         method is available only if the transmitter property is
**         enabled.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the output
**                           buffer.
** ===================================================================
*/
word AS1_GetCharsInTxBuf(void)
{
  return (!SCS1_SCTE);                 /* Return number of chars in the transmitter buffer */
}

/*
** ===================================================================
**     Method      :  AS1_GetError (bean AsynchroSerial)
**
**     Description :
**         Return a set of errors on the channel (errors that cannot
**         be returned in given methods). The errors accumulate in a
**         set; after calling GetError this set is returned and
**         cleared.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Err             - Pointer to returned set of errors
**     Returns     :
**         ---             - Error code (if GetError did not succeed),
**                           possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte AS1_GetError(AS1_TError *Err)
{
  Err->err = 0;
  Err->errName.OverRun = ((ErrFlag & OVERRUN_ERR) != 0); /* Overrun error */
  Err->errName.Framing = ((ErrFlag & FRAMING_ERR ) != 0); /* Framing error */
  Err->errName.Parity = ((ErrFlag & PARITY_ERR) != 0); /* Parity error */
  Err->errName.Noise = ((ErrFlag & NOISE_ERR) != 0); /* Noise error */
  Err->errName.Break = ((ErrFlag & BREAK_ERR) != 0); /* Break error */
  Err->errName.Idle = ((ErrFlag & IDLE_ERR) != 0); /* Idle character */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AS1_Init (bean AsynchroSerial)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void AS1_Init(void)
{
  SerFlag = 0;                         /* Reset flags */
  SCC1 = 0;
  SCC3 = 0;                            /* Disable error interrupts */
  SCBR_SCP = 3;                        /* Set prescaler bits */
  SCBR_SCR = 2;                        /* Set divisor bits */
  AS1_HWEnDi();                        /* Enable/disable device according to status flags */
}


/* END AS1. */


/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 02.84 for 
**     the Motorola HC08 series of microcontrollers.
**
** ###################################################################
*/
