/*
** ###################################################################
**
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**
**     Filename  : CAN1.C
**
**     Project   : Full1
** 
**     Processor : MC68HC908AZ60MFU
**
**     Beantype  : MotorolaCAN
**
**     Version   : Bean 02.193, 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 "MotorolaCAN" implements an CAN serial channel.
**
**     Settings  :
**
**         CAN channel                 : CAN
**
**         Protocol
**             Time segment 1          : 7
**             Time segment 2          : 3
**             RSJ                     : 1
**
**             Recieve accept. code    : 0
**             Recieve accept. mask    : 0
**
**
**
**     Contents  :
**
**         SetAcceptanceCode - byte CAN1_SetAcceptanceCode(dword Mask);
**         SetAcceptanceMask - byte CAN1_SetAcceptanceMask(dword Mask);
**         SetAcceptanceMode - byte CAN1_SetAcceptanceMode(byte Mode);
**         SendFrame         - byte CAN1_SendFrame(byte BufferNum,dword MessageID,byte FrameType,byte Length,by
**         ReadFrame         - byte CAN1_ReadFrame(dword *MessageID,byte *FrameType,byte *FrameFormat,byte *Len
**         GetStateTX        - byte CAN1_GetStateTX(byte BufferNum,bool *State);
**         GetStateRX        - byte CAN1_GetStateRX(void);
**         GetError          - byte CAN1_GetError(void);
**
**
**     (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 CAN1. */

#pragma MESSAGE DISABLE C1420 /* WARNING C1420: Result of function-call is ignored */

#include "CAN1.h"
#include "PWM1.h"
#include "PPG1.h"
#include "TI1.h"
#include "AD2.h"
#include "AS1.h"
#include "SM1.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"

static byte* IDR1regs[3] = {&IDR10,&IDR11,&IDR12}; /* adresses of IDR1 registers */
static dword* IDRregs[3] = {&IDR0,&IDR1,&IDR2}; /* adresses of IDR registers */
static byte* IDR3regs[3] = {&IDR30,&IDR31,&IDR32}; /* adresses of IDR3 registers */
static byte* DSRregs[3] = {&DSR00,&DSR01,&DSR02}; /* adresses of DSR registers  */
static byte* DLRregs[3] = {&DLR0,&DLR1,&DLR2}; /* adresses of DLR registers  */
static byte* TBPRregs[3] = {&TBPR0,&TBPR1,&TBPR2}; /* adresses of TBPR registers */

/*
** ===================================================================
**     Method      :  CAN1_SetAcceptanceMode (bean MotorolaCAN)
**
**     Description :
**         Set a content of the acceptance mode register.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Mode            - Acceptance mode.
**                           Supported modes: 
**                           SINGLE_32_FILTER - Single 32-bit
**                           acceptance filter
**                           TWO_16_FILTER - Two 16-bit acceptance
**                           filter
**                           FOUR_8_FILTER - Four 8-bit acceptance
**                           filter
**                           FILTER_CLOSED - Filter closed
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - This bean is disabled by
**                           user
** ===================================================================
*/
byte CAN1_SetAcceptanceMode(byte Mode)
{
  if (Mode > 3)                        /* Is mode parameter greater then 3 */
    return ERR_VALUE;                  /* If yes then error */
  CIDAC_IDAM = Mode;                   /* Set acceptance mode of the receiver */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  CAN1_GetStateRX (bean MotorolaCAN)
**
**     Description :
**         Return a value of the reception complete flag.
**     Parameters  : None
**     Returns     :
**         ---             - Gets state of the receiver.
**                           0 - Receiver is empty
**                           1 - Receiver is full 
** ===================================================================
*/
/*
byte CAN1_GetStateRX(void)

**      This method is implemented as a macro. See header module. **
*/

/*
** ===================================================================
**     Method      :  CAN1_SetAcceptanceCode (bean MotorolaCAN)
**
**     Description :
**         Set acceptance code register.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Mask            - Mask could be 11-bit for standard
**                           frame or 29-bit for extended frame
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - This bean is disabled by
**                           user
** ===================================================================
*/
byte CAN1_SetAcceptanceCode(dword Mask)
{
  CMCR0 |= 1;                          /* Software reset */
  CIDMR = Mask;                        /* Set acceptance code of the receiver */
  CMCR0 = 0;                           /* Start the device */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  CAN1_GetError (bean MotorolaCAN)
**
**     Description :
**         Return a content of the receiver flag register.
**     Parameters  : None
**     Returns     :
**         ---             - Meaning of returned 8 bits:
**                           Bit 0 - Receive buffer full
**                           Bit 1 - Overrun
**                           Bit 2 - Bus-Off
**                           Bit 3 - Transmitter error passive
**                           Bit 4 - Receiver error passive
**                           Bit 5 - Transmitter warning
**                           Bit 6 - Receiver warning
**                           Bit 7 - Wakeup
** ===================================================================
*/
byte CAN1_GetError(void)
{
  byte Err = CRFLG;                    /* Temporary variable */

  CRFLG |= 254;
  return Err;                          /* Result error state */
}

/*
** ===================================================================
**     Method      :  CAN1_HWEnDi (bean MotorolaCAN)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void CAN1_HWEnDi(void)
{
    CMCR0 &= ~1;                       /* Start the device */
    while(!CMCR0_SYNCH);               /* Wait for synchronization */
}

/*
** ===================================================================
**     Method      :  CAN1_SetHigh (bean MotorolaCAN)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void CAN1_SetHigh(void)
{
  CBTR0=73;                            /* Set the device timing register */
  CBTR1=55;                            /* Set the device timing register */
  CMCR1_CLKSRC=0;                      /* Select the clock source */
  CAN1_HWEnDi();                       /* Enable/disable device according to status flags */
}

/*
** ===================================================================
**     Method      :  CAN1_SendFrame (bean MotorolaCAN)
**
**     Description :
**         Send a frame.
**     Parameters  :
**         NAME            - DESCRIPTION
**         BufferNum       - Number of the buffer (0..2)
**         MessageID       - Identification of the
**                           message
**         FrameType       - Type of frame
**                           DATA_FRAME - data frame
**                           REMOTE_FRAME - remote frame
**         Length          - Length of the frame in bytes (0..
**                           8)
**       * Data            - Pointer to data 
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - This bean is disabled by
**                           user
**                           ERR_VALUE - Some parameter is out of
**                           possible range
** ===================================================================
*/
byte CAN1_SendFrame(byte BufferNum,dword MessageID,byte FrameType,byte Length,byte *Data)
{
  byte i;                              /* Temorary variables */
  byte bufmask=((word)1<<BufferNum);   /* Buffer mask */

  if ( (BufferNum>2) || (Length>8) )   /* Is BufferNum greater than 2 or Length greater than 8?*/
    return ERR_VALUE;                  /* If yes then error */
  if (FrameType > REMOTE_FRAME)        /* Is FrameType other than REMOTE_FRAME or DATA_FRAME */
    return ERR_VALUE;                  /* If yes then error */
  if (!(CTFLG & bufmask))              /* Is the transmit buffer full? */
    return ERR_TXFULL;                 /* If yes then error */
  if (MessageID < 2048)                /* Is it the standard frame? */
    MessageID = (MessageID << 21);     /* Sets the message as "standard" */
  else
    MessageID = ( ((MessageID<<1) & 0x0007FFFF) | ((MessageID << 3) & 0xFFE00000) ) | 0x00080000 ; /* Sets the message as "extended" */
  *IDRregs[BufferNum] = MessageID;
  if (FrameType == DATA_FRAME) {       /* Is it a data frame? */
    for(i=0; i<Length; i++)
      *((DSRregs[BufferNum])+i) = Data[i]; /* Store data to the transmit register */
    if (MessageID < 2048)              /* Is it the standard frame? */
      *IDR1regs[BufferNum] &= ~16;     /* If yes then set message type as "data frame" */
    else
      *IDR3regs[BufferNum] &= ~1;      /* If no then set message type as "data frame" */
  }
  else {                               /* Remote frame */
    if (MessageID < 2048)              /* Is it the standard frame? */
      *IDR1regs[BufferNum] |= 16;      /* If yes then set message type as "remote frame" */
    else
      *IDR3regs[BufferNum] |= 1;       /* If yes then set message type as "remote frame" */
  }
  *DLRregs[BufferNum] = Length;        /* Set the length of the message */
  *TBPRregs[BufferNum] = 0;            /* Set the priority (high) */
  CTFLG = 1 << BufferNum;              /* Start transmission */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  CAN1_ReadFrame (bean MotorolaCAN)
**
**     Description :
**         Read a frame.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * MessageID       - Identification of message
**       * FrameType       - Type of frame
**                           DATA_FRAME - data frame
**                           REMOTE_FRAME - remote frame
**       * FrameFormat     - Format of frame
**                           STANDARD_FRAME - standard frame 11-bits
**                           EXTENDED_FRAME - extended frame 29-bits
**       * Length          - Length of the frame
**       * Data            - Pointer to the buffer for received
**                           data
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - This bean is disabled by
**                           user
** ===================================================================
*/
byte CAN1_ReadFrame(dword *MessageID,byte *FrameType,byte *FrameFormat,byte *Length,byte *Data)
{
  byte i;                              /* Temporary variable */
  dword ID;                            /* Temporary variable */

  ID = REC_IDR;                        /* Read the identification of the received message */
  if ((REC_IDR1 & 8)) {                /* Is the received message "extended frame" */
    ID = ( ((ID >> 1) & 0x0003FFFF) | ((ID >> 3) & 0xFFFC0000) ); /* Result the identification */
    *FrameType = (REC_IDR3 & 1)? REMOTE_FRAME : DATA_FRAME; /* Result the frame type */
  } else {                             /* Message is "standard frame" */
    ID >>= 21;                         /* Result the identification */
    *FrameType = (REC_IDR1 & 16)? REMOTE_FRAME : DATA_FRAME; /* Result the frame type */
  }
  *MessageID = ID;                     /* Result the identification */
  *FrameFormat = (REC_IDR1 & 8)? EXTENDED_FORMAT : STANDARD_FORMAT; /* Result the frame type */
  if (*FrameType == DATA_FRAME) {      /* Is frame "data frame" */
    *Length = REC_DLR & 0x0F;          /* Result length of the message */
    if (!(REC_IDR1 & 8)) {             /* Was the data frame received? */
      for(i=0; i<*Length; i++)
        Data[i] = REC_DSR[i];          /* If yes then result message data */
    }
  } else
    *Length = 0;                       /* Result zero length */
  CRFLG_RXF = 1;                       /* Reset the interrupt request flag */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  CAN1_Init (bean MotorolaCAN)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void CAN1_Init(void)
{
  CMCR0 = 1;                           /* CAN reset */
  CMCR1 = 0;
  CIDAC_IDAM = 0;                      /* Set the acceptance mode */
  CIDAR = 0;                           /* Set the acceptance code */
  CIDMR = 0;                           /* Set the acceptance mask */
  CAN1_SetHigh();                      /* Initial speed CPU mode is high */
}

/*
** ===================================================================
**     Method      :  CAN1_GetStateTX (bean MotorolaCAN)
**
**     Description :
**         Return a value of the transmission complete flag of given
**         buffer.
**     Parameters  :
**         NAME            - DESCRIPTION
**         BufferNum       - Number of the buffer
**       * State           - Value of transmission complete flag
**                           of the given buffer. FALSE - message
**                           buffer is empty; TRUE - message buffer
**                           isn't empty.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - This bean is disabled by
**                           user
**                           ERR_VALUE - Some parameter is out of
**                           possible range
** ===================================================================
*/
byte CAN1_GetStateTX(byte BufferNum,bool *State)
{
  if (BufferNum > 2)                   /* Is number of buffer greater than 2? */
    return ERR_VALUE;                  /* If yes then error */
  *State = (CTFLG & (1 << BufferNum)); /* Result state of the transmitter interrupt flag */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  CAN1_SetAcceptanceMask (bean MotorolaCAN)
**
**     Description :
**         Set the acceptance mask registers.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Mask            - Mask could be 11-bit for standard
**                           frame or 29-bit for extended frame
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - This bean is disabled by
**                           user
** ===================================================================
*/
byte CAN1_SetAcceptanceMask(dword Mask)
{
  CIDMR = Mask;
  return ERR_OK;                       /* OK */
}



/* END CAN1. */
/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 02.84 for 
**     the Motorola HC08 series of microcontrollers.
**
** ###################################################################
*/
