freescale

来自「Freescale 系列单片机常用模块与综合系统设计」· 代码 · 共 542 行 · 第 1/2 页

TXT
542
字号
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : Uart.C
**     Project   : Pc_Communication
**     Processor : MC9S08JM60CLHE
**     Beantype  : AsynchroSerial
**     Version   : Bean 02.453, Driver 01.25, CPU db: 3.00.033
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 2009-10-25, 15:01
**     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              : SCI1
**
**         Protocol
**             Init baud rate          : 19200baud
**             Width                   : 8 bits
**             Stop bits               : 1
**             Parity                  : none
**             Breaks                  : Disabled
**
**         Registers
**             Input buffer            : SCI1D     [$003F]
**             Output buffer           : SCI1D     [$003F]
**             Control register        : SCI1C1    [$003A]
**             Mode register           : SCI1C2    [$003B]
**             Baud setting reg.       : SCI1BD    [$0038]
**             Special register        : SCI1S1    [$003C]
**
**         Input interrupt
**             Vector name             : Vsci1rx
**             Priority                : 
**
**         Output interrupt
**             Vector name             : Vsci1tx
**             Priority                : 
**
**         Used pins:
**         ----------------------------------------------------------
**           Function | On package           |    Name
**         ----------------------------------------------------------
**            Input   |     14               |  PTE1_RxD1
**            Output  |     13               |  PTE0_TxD1
**         ----------------------------------------------------------
**
**
**
**     Contents  :
**         RecvChar        - byte Uart_RecvChar(Uart_TComData *Chr);
**         SendChar        - byte Uart_SendChar(Uart_TComData Chr);
**         RecvBlock       - byte Uart_RecvBlock(Uart_TComData *Ptr, word Size, word *Rcv);
**         SendBlock       - byte Uart_SendBlock(Uart_TComData *Ptr, word Size, word *Snd);
**         ClearRxBuf      - byte Uart_ClearRxBuf(void);
**         ClearTxBuf      - byte Uart_ClearTxBuf(void);
**         GetCharsInRxBuf - word Uart_GetCharsInRxBuf(void);
**         GetCharsInTxBuf - word Uart_GetCharsInTxBuf(void);
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2008
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/


/* MODULE Uart. */

#pragma MESSAGE DISABLE C4002 /* WARNING C4002: Result not used is ignored */

#include "Uart.h"
#include "Events.h"




#define OVERRUN_ERR      0x01          /* Overrun error flag bit   */
#define COMMON_ERR       0x02          /* Common error of RX       */
#define CHAR_IN_RX       0x04          /* Char is in RX buffer     */
#define RUNINT_FROM_TX   0x08          /* Interrupt is in progress */
#define FULL_RX          0x10          /* Full receive buffer      */


static byte SerFlag;                   /* Flags for serial communication */
                                       /* Bit 0 - Overrun error */
                                       /* Bit 1 - Common error of RX */
                                       /* Bit 2 - Char in RX buffer */
                                       /* Bit 3 - Interrupt is in progress */
                                       /* Bit 4 - Full RX buffer */
byte Uart_InpLen;                      /* Length of the input buffer content */
static byte InpIndxR;                  /* Index for reading from input buffer */
static byte InpIndxW;                  /* Index for writing to input buffer */
static Uart_TComData InpBuffer[Uart_INP_BUF_SIZE]; /* Input buffer for SCI commmunication */
byte Uart_OutLen;                      /* Length of the output buffer content */
static byte OutIndxR;                  /* Index for reading from output buffer */
static byte OutIndxW;                  /* Index for writing to output buffer */
static Uart_TComData OutBuffer[Uart_OUT_BUF_SIZE]; /* Output buffer for SCI commmunication */

/*
** ===================================================================
**     Method      :  Uart_RecvChar (bean AsynchroSerial)
**
**     Description :
**         If any data is received, this method returns one
**         character, otherwise it returns an error code (it does
**         not wait for data). This method is enabled only if the
**         receiver property is enabled.
**         [Note:] Because the preferred method to handle error and
**         break exception in the interrupt mode is to use events
**         <OnError> and <OnBreak> the return value ERR_RXEMPTY has
**         higher priority than other error codes. As a consequence
**         the information about an exception in interrupt mode is
**         returned only if there is a valid character ready to be
**         read.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Chr             - Pointer to a 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_BREAK - Break character is detected
**                           (only when the <Interrupt service>
**                           property is disabled and the <Break
**                           signal> property is enabled)
**                           ERR_COMMON - common error occurred (the
**                           <GetError> method can be used for error
**                           specification)
** ===================================================================
*/
byte Uart_RecvChar(Uart_TComData *Chr)
{
  byte Result = ERR_OK;                /* Return error code */

  if(Uart_InpLen > 0) {                /* Is number of received chars greater than 0? */
    EnterCritical();                   /* Save the PS register */
    Uart_InpLen--;                     /* Decrease number of received chars */
    *Chr = InpBuffer[InpIndxR];        /* Received char */
    InpIndxR = (byte)((InpIndxR+1) & (Uart_INP_BUF_SIZE-1)); /* Update index */
    Result = (byte)((SerFlag & (OVERRUN_ERR|COMMON_ERR|FULL_RX))?ERR_COMMON:ERR_OK);
    SerFlag &= ~(OVERRUN_ERR|COMMON_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 */
  }
  return Result;                       /* Return error code */
}

/*
** ===================================================================
**     Method      :  Uart_SendChar (bean AsynchroSerial)
**
**     Description :
**         Sends one character to the channel. If the bean is
**         temporarily disabled (Disable method) SendChar method
**         only stores data into an output buffer. In case of a zero
**         output buffer size, only one character can be stored.
**         Enabling the bean (Enable method) starts the transmission
**         of the stored data. 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 Uart_SendChar(Uart_TComData Chr)
{
  if(Uart_OutLen == Uart_OUT_BUF_SIZE) { /* Is number of chars in buffer same as a size of the transmit buffer */
    return ERR_TXFULL;                 /* If yes then error */
  }
  EnterCritical();                     /* Save the PS register */
  Uart_OutLen++;                       /* Increase number of bytes in the transmit buffer */
  OutBuffer[OutIndxW] = Chr;           /* Store char to buffer */
  OutIndxW = (byte)((OutIndxW+1) & (Uart_OUT_BUF_SIZE-1)); /* Update index */
  SCI1C2_TIE = 1;                      /* Enable transmit interrupt */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  Uart_RecvBlock (bean AsynchroSerial)
**
**     Description :
**         If any data is received, this method returns the block of
**         the data and its length (and incidental error), otherwise
**         it returns an error code (it does not wait for data).
**         This method is available only if non-zero length of the
**         input buffer is defined and the receiver property is
**         enabled.
**         If less than requested number of characters is received
**         only the available data is copied from the receive buffer
**         to the user specified destination. The value ERR_EXEMPTY
**         is returned and the value of variable pointed by the Rcv
**         parameter is set to the number of received characters.
**     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 - The receive buffer didn't
**                           contain the requested number of data.
**                           Only available data has been returned.
**                           ERR_COMMON - common error occurred (the
**                           GetError method can be used for error
**                           specification)
** ===================================================================
*/
byte Uart_RecvBlock(Uart_TComData *Ptr, word Size, word *Rcv)
{
  word count;                          /* Number of received chars */
  byte result = ERR_OK;                /* Last error */

  for(count = 0; count < Size; count++) {
    switch ( Uart_RecvChar(Ptr++)) {   /* Receive data and test the return value*/
    case ERR_RXEMPTY:                  /* No data in the buffer */
      if (result == ERR_OK) {          /* If no receiver error reported */
        result = ERR_RXEMPTY;          /* Return info that requested number of data is not available */
      }
     *Rcv = count;                     /* Return number of received chars */
      return result;
      break;
    case ERR_COMMON:                   /* Receiver error reported */
      result = ERR_COMMON;             /* Return info that an error was detected */
      break;
    default:
      break;
    }
  }
  *Rcv = count;                        /* Return the number of received chars */
  return result;                       /* Return the last error code*/ /* Return the last error code*/
}

/*
** ===================================================================
**     Method      :  Uart_SendBlock (bean AsynchroSerial)
**
**     Description :
**         Sends a block of characters to the channel.
**         This method is available only if non-zero length 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

⌨️ 快捷键说明

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