/*
** ###################################################################
**
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**
**     Filename  : SM1.C
**
**     Project   : Full1
** 
**     Processor : MC68HC908AZ60MFU
**
**     Beantype  : SynchroMaster
**
**     Version   : Bean 02.096, Driver 01.05, CPU db: 2.81.019
**
**     Compiler  : Metrowerks HC08 C Compiler V-5.0.13
**
**     Date/Time : 2.4.2002, 9:36
**
**     Abstract  :
**
**         This bean "SynchroMaster" implements MASTER part of synchronous
**         serial master-slave communication.
**
**     Settings  :
**
**         Synchro type                : MASTER
**
**         Serial channel              : SPI
**
**         Protocol
**             Init baud rate          : 64us
**             Clock edge              : rising
**             Width                   : 8 bits (always)
**             Empty character         : 0
**             Empty char. on input    : RECEIVED
**
**         Registers
**             Input buffer            : SPDR      [18]
**             Output buffer           : SPDR      [18]
**             Control register        : SPCR      [16]
**             Mode register           : SPCR      [16]
**             Baud setting reg.       : SPSCR     [17]
**
**         Input interrupt
**             Vector name             : INT_SPIReceive
**
**         Output interrupt
**             Vector name             : INT_SPITransmit
**
**         Used pins                   : 
**             ----------------------------------------------------
**               Function | On package |    Name
**             ----------------------------------------------------
**                Input   |     18     |  PTE5_MISO
**                Output  |     19     |  PTE6_MOSI
**                Clock   |     20     |  PTE7_SPSCK
**             ----------------------------------------------------
**
**     Contents  :
**
**         RecvChar        - byte SM1_RecvChar(byte *Chr);
**         SendChar        - byte SM1_SendChar(byte Chr);
**         GetCharsInRxBuf - word SM1_GetCharsInRxBuf(void);
**         GetCharsInTxBuf - word SM1_GetCharsInTxBuf(void);
**         GetError        - byte SM1_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 SM1. */

#include "SM1.h"
#include "PWM1.h"
#include "PPG1.h"
#include "TI1.h"
#include "AD2.h"
#include "AS1.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"
#include "Events.h"

#define OVERRUN_ERR      1             /* Overrun 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      */

static byte SerFlag;                   /* Flags for serial communication */
                                       /* Bits: 0 - OverRun error */
                                       /*       1 - Unused */
                                       /*       2 - Unused */
                                       /*       3 - Char in RX buffer */
                                       /*       4 - Full TX buffer */
                                       /*       5 - Running int from TX */
                                       /*       6 - Full RX buffer */
                                       /*       7 - Unused */
static byte ErrFlag;                   /* Error flags mirror of SerFlag */
static byte BufferRead;                /* Input char SPI commmunication */

/*
** ===================================================================
**     Method      :  SM1_RecvChar (bean SynchroMaster)
**
**     Description :
**         If any data received, this method returns one character,
**         otherwise it returns error code (it does not wait for
**         data).
**     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 was detected
**                           from last char or block receiving
**                           ERR_FAULT - Fault error was detected
**                           from last char or block receiving. This
**                           error does not need to be supported on
**                           some Cpu (see generated code).
** ===================================================================
*/
byte SM1_RecvChar(byte *Chr)
{
  byte FlagTmp;

  if (!(SerFlag & CHAR_IN_RX))         /* Is any char in RX buffer? */
    return ERR_RXEMPTY;                /* If no then error */
  SaveStatusReg();                     /* Save the PS register */
  __DI();                              /* Disable interrupt */
  *Chr = BufferRead;                   /* Read the char */
  FlagTmp = SerFlag;                   /* Safe the flags */
  SerFlag &= ~(OVERRUN_ERR | CHAR_IN_RX | FULL_RX); /* Clear flag "char in RX buffer" */
  RestoreStatusReg();                  /* Restore the PS register */
  if ((FlagTmp & OVERRUN_ERR) != 0)    /* Is the overrun occured? */
    return ERR_OVERRUN;                /* If yes then return error */
  else
    return ERR_OK;  
}

/*
** ===================================================================
**     Method      :  SM1_SendChar (bean SynchroMaster)
**
**     Description :
**         Send one character to the channel.
**     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 SM1_SendChar(byte Chr)
{
  if (SerFlag & FULL_TX)               /* Is any char in the TX buffer? */
    return ERR_TXFULL;                 /* If yes then error */
  SaveStatusReg();                     /* Save the PS register */
  __DI();                              /* Disable interrupt */
  SPDR = Chr;                          /* Store char to transmitter register */
  SPCR_SPTIE = 1;                      /* Enable transmit interrupt */
  SerFlag |= FULL_TX;                  /* Set the flag "full TX buffer" */
  RestoreStatusReg();                  /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_GetCharsInRxBuf (bean SynchroMaster)
**
**     Description :
**         Return number of characters in the input buffer.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the input
**                           buffer.
** ===================================================================
*/
word SM1_GetCharsInRxBuf(void)
{
  return (SerFlag >> 3) & 1;           /* Return number of chars in receive buffer */
}

/*
** ===================================================================
**     Method      :  SM1_GetCharsInTxBuf (bean SynchroMaster)
**
**     Description :
**         Return number of characters in the output buffer.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the output
**                           buffer.
** ===================================================================
*/
word SM1_GetCharsInTxBuf(void)
{
  return (SerFlag >> 4) & 1;           /* Return number of chars in the transmit buffer */
}

/*
** ===================================================================
**     Method      :  SM1_GetError (bean SynchroMaster)
**
**     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 SM1_GetError(SM1_TError *Err)
{
  SaveStatusReg();                     /* Save the PS register */
  __DI();                              /* Disable interrupt */
  Err->OverRun = ((ErrFlag & OVERRUN_ERR) != 0); /* Overrun error */
  ErrFlag = 0;                         /* Reset error flags */
  RestoreStatusReg();                  /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_InterruptRx (bean SynchroMaster)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
__interrupt void SM1_InterruptRx(void)
{
  byte Data;                           /* Temporary variable for data */
  byte Flags = 0;                      /* Temporary variable for flags */
  byte Status;                         /* Temporary variable for flags */

  Status = SPSCR;                      /* Read the device error register */
  Data = SPDR;                         /* Read data from receiver */
  if ((Status & 32) || (SerFlag & CHAR_IN_RX)) { /* Is the overrun error flag set? */
    SerFlag |= OVERRUN_ERR;            /* If yes then set the OnError flag */
    Flags |= 1;                        /* If yes then set the OnError flag */
  }
  if (!(Status & 32)) {
    SerFlag |= CHAR_IN_RX;             /* Set flag "char in RX buffer" */
    BufferRead = Data;                 /* Read data from receiver */
    Flags |= 2;                        /* If yes then set the OnRxChar flag */
  }
  ErrFlag |= SerFlag;                  /* Update error flag mirror */
  if(Flags & 1) {                      /* Is any error flag set? */
    SM1_OnError();                     /* If yes then invoke user event */
  }
  else {
    if(Flags & 2) {                    /* Is OnRxChar flag set? */
      SM1_OnRxChar();                  /* If yes then invoke user event */
    }
  }
}

/*
** ===================================================================
**     Method      :  SM1_InterruptTx (bean SynchroMaster)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
__interrupt void SM1_InterruptTx(void)
{
  byte Flags = 0;

  if (SerFlag & FULL_TX)               /* Is any char already present in the transmit buffer? */
    Flags |= 1;                        /* Set flag "OnTxChar" */
  SerFlag &= ~FULL_TX;                 /* Reset flag "full TX buffer" */
  SPCR_SPTIE = 0;                      /* Disable transmit interrupt */
    if (Flags & 1) {                   /* Is flag "OnTxChar" set? */
      SM1_OnTxChar();                  /* If yes then invoke user event */
    }
}

/*
** ===================================================================
**     Method      :  SM1_Init (bean SynchroMaster)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void SM1_Init(void)
{
  SPCR = 0;                            /* Reset the device register */
  #pragma MESSAGE DISABLE C4002        /* Disable warning C4002 "Result not used" */
  SPDR;                                /* Read the device register */
  SPSCR = 67;                          /* Set the baud rate register */
  SPCR = 40;                           /* Set the clock edge to rising */
  SerFlag = 0;                         /* Reset all flags */
  ErrFlag = 0;                         /* Reset all flags in mirror */
  SPCR_SPE = 1;                        /* Enable device */
  SPCR_SPRIE = 1;                      /* Enable receive interrupt */
}

/* END SM1. */

/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 02.84 for 
**     the Motorola HC08 series of microcontrollers.
**
** ###################################################################
*/
