⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 as1.c

📁 FREESCALE 16位单片机片MC9S12DG128的串口驱动程序,环行缓冲区大小可设置,支持半双工RS485通信,可调用用户事件
💻 C
📖 第 1 页 / 共 2 页
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : AS1.C
**     Project   : displayboard
**     Processor : MC9S12DG128BCFU
**     Beantype  : AsynchroSerial
**     Version   : Bean 02.231, Driver 01.08, CPU db: 2.87.238
**     Compiler  : Metrowerks HC12 C Compiler
**     Date/Time : 2005-11-2, 10:18
**     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              : SCI0
**
**         Protocol
**             Init baud rate          : 9600baud
**             Width                   : 8 bits
**             Stop bits               : 1
**             Parity                  : none
**             Breaks                  : Disabled
**
**         Registers
**             Input buffer            : SCI0DRL   [207]
**             Output buffer           : SCI0DRL   [207]
**             Control register        : SCI0CR1   [202]
**             Mode register           : SCI0CR2   [203]
**             Baud setting reg.       : SCI0BD    [200]
**             Special register        : SCI0SR1   [204]
**
**         Input interrupt
**             Vector name             : INT_SCI0
**             Priority                : 1
**
**         Output interrupt
**             Vector name             : INT_SCI0
**             Priority                : 1
**
**         Used pins                   : 
**             ----------------------------------------------------
**               Function | On package |    Name
**             ----------------------------------------------------
**                Input   |     63     |  PS0_RxD0
**                Output  |     64     |  PS1_TxD0
**             ----------------------------------------------------
**
**
**     Contents  :
**         RecvChar        - byte AS1_RecvChar(AS1_TComData *Chr);
**         SendChar        - byte AS1_SendChar(AS1_TComData Chr);
**         RecvBlock       - byte AS1_RecvBlock(AS1_TComData *Ptr,word Size,word *Rcv);
**         SendBlock       - byte AS1_SendBlock(AS1_TComData *Ptr,word Size,word *Snd);
**         ClearRxBuf      - byte AS1_ClearRxBuf(void);
**         ClearTxBuf      - byte AS1_ClearTxBuf(void);
**         GetCharsInRxBuf - word AS1_GetCharsInRxBuf(void);
**         GetCharsInTxBuf - word AS1_GetCharsInTxBuf(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 AS1. */

#pragma MESSAGE DISABLE C4002 /* WARNING C4002: Result not used is ignored */
#pragma MESSAGE DISABLE C4301 /* INFORMATION C4301: Inline expansion done for function call */

#include "AS1.h"
#include "PWM6.h"
#include "IEE1.h"
#include "TI1.h"
#include "Events.h"

/* Definition of DATA and CODE segments for this bean. User can specify where
   these segments will be located on "Build options" tab of the selected CPU bean. */
#pragma DATA_SEG AS1_DATA              /* Data section for this module. */
#pragma CODE_SEG AS1_CODE              /* Code section for this module. */


#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 - Running int from TX */
                                       /*       6 - Full RX buffer */
                                       /*       7 - Noise error */
                                       /*       8 - Idle character  */
                                       /*       9 - Break detected  */
                                       /*      10 - Unused */
static word InpLen;                    /* Length of input buffer's content */
static AS1_TComData *InpPtrR;          /* Pointer for reading from input buffer */
static AS1_TComData *InpPtrW;          /* Pointer for writing to input buffer */
static AS1_TComData InpBuffer[80];    /* Input buffer for SCI commmunication */
static word OutLen;                    /* Length of output bufer's content */
static AS1_TComData *OutPtrR;          /* Pointer for reading from output buffer */
static AS1_TComData *OutPtrW;          /* Pointer for writing to output buffer */
static AS1_TComData OutBuffer[80];    /* Output buffer for SCI commmunication */
extern byte DataFrameFlag; //接收完一帧数据标志位
extern word DataFramLen;  //接收到一帧数据的数据长度
/*
** ===================================================================
**     Method      :  HWEnDi (bean AsynchroSerial)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
static void HWEnDi(void)
{
    SCI0CR2_TE = 1;                    /* Enable transmitter */
    SCI0CR2_RE = 1;                    /* Enable receiver */
    SCI0CR2_RIE = 1;                   /* Enable recieve interrupt */
}

/*
** ===================================================================
**     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
**                           ERR_BREAK - Break character is detected
**                           ERR_IDLE - Idle character is detected
**                           (this error is hardware specific).
** ===================================================================
*/
byte AS1_RecvChar(AS1_TComData *Chr)
{
  byte Result = ERR_OK;                /* Return error code */
  word SerFlagCopy;                    /* copy of the SerFlag */

  if(InpLen > 0) {                     /* Is number of received chars greater than 0? */
    EnterCritical();                   /* Save the PS register */
    InpLen--;                          /* Decrease number of received chars */
    *Chr = *(InpPtrR++);               /* Received char */
    if(InpPtrR >= InpBuffer + 80)     /* Is the pointer out of the receive buffer? */
      InpPtrR = InpBuffer;             /* Set pointer to the first item into the receive buffer */
    SerFlagCopy = SerFlag;             /* create copy of the SerFlag */
    SerFlag &= ~(OVERRUN_ERR|FRAMING_ERR|PARITY_ERR|NOISE_ERR|FULL_RX|CHAR_IN_RX); /* Clear all errors in the status variable */
    ExitCritical();                    /* Restore the PS register */
  }
  else {
    return ERR_RXEMPTY;                /* Receiver is empty */
  }
  if((SerFlagCopy & OVERRUN_ERR) != 0) /* Is the overrun error detected? */
    Result = ERR_OVERRUN;              /* If yes then set the overrun error flag */
  else if((SerFlagCopy & PARITY_ERR) != 0) /* Is the parity error detected? */
    Result = ERR_PARITY;               /* If yes then set the parity error flag */
  else if((SerFlagCopy & FRAMING_ERR) != 0) /* Is the framing error detected? */
    Result = ERR_FRAMING;              /* If yes then set the framing error flag */
  else if((SerFlagCopy & FULL_RX) != 0) /* Is the software overrun error detected? */
    Result = ERR_OVERRUN;              /* If yes then set the overrun error flag */
  else if((SerFlagCopy & NOISE_ERR) != 0) /* Is the noise error detected? */
    Result = ERR_NOISE;                /* If yes then set the noise error flag */
  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(AS1_TComData Chr)
{
  if(OutLen == 80)                    /* Is number of chars in buffer is the same as a size of the transmit buffer */
    return ERR_TXFULL;                 /* If yes then error */
  EnterCritical();                     /* Save the PS register */
  OutLen++;                            /* Increase number of bytes in the transmit buffer */
  *(OutPtrW++) = Chr;                  /* Store char to buffer */
  if(OutPtrW >= OutBuffer + 80)       /* Is the pointer out of the transmit buffer */
    OutPtrW = OutBuffer;               /* Set pointer to first item in the transmit buffer */
  RS485_DE_RE = 1; 
  SCI0CR2_SCTIE = 1;                   /* Enable transmit interrupt */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AS1_RecvBlock (bean AsynchroSerial)
**
**     Description :
**         If any data received, this method returns the block ofthe
**         data and its lenght (and incidental error), otherwise it
**         returns error code (it does not wait for data).
**         This method is available only if non-zero lenght of the
**         input buffer is defined and the receiver property is
**         enabled.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Ptr             - Pointer to the block of received data
**         Size            - Size of the block
**       * Rcv             - Pointer to real number of the received
**                           data
**     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
**                           ERR_BREAK - Break character is detected
**                           ERR_IDLE - Idle character is detected
**                           (this error is hardware specific).
** ===================================================================
*/
byte AS1_RecvBlock(AS1_TComData *Ptr, word Size, word *Rcv)
{
  word count;                          /* Number of received chars */
  byte result = ERR_OK;                /* Last error */

  if(!Size)                            /* Is size not greater than 0 */
    return ERR_OK;                     /* If yes then OK */
  for(count = 0; count < Size; count++) {
    result = AS1_RecvChar(Ptr++);
    if(result != ERR_OK) {             /* Receiving given number of chars */
      *Rcv = count;                    /* Return number of received chars */
      return result;                   /* Return last error */
    }
  }
  *Rcv = count;                        /* Return number of received chars */
  return result;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AS1_SendBlock (bean AsynchroSerial)
**
**     Description :
**         Send a block of characters to the channel.
**         This method is available only if non-zero lenght of the
**         output buffer is defined and the transmitter property is
**         enabled.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Ptr             - Pointer to the block of data to send
**         Size            - Size of the block
**       * Snd             - Pointer to number of data that are sent
**                           (moved to buffer)
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_TXFULL - It was not possible to send
**                           requested number of bytes
** ===================================================================
*/
byte AS1_SendBlock(AS1_TComData *Ptr, word Size, word *Snd)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -