📄 uart.c
字号:
/** ###################################################################
** THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
** Filename : UART.C
** Project : RTOSDemo
** Processor : MC9S08GT60CFB
** Beantype : AsynchroSerial
** Version : Bean 02.333, Driver 01.12, CPU db: 2.87.086
** Compiler : Metrowerks HCS08 C Compiler
** Date/Time : 3/10/2006, 2:35 PM
** 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 : 57600baud
** Width : 8 bits
** Stop bits : 1
** Parity : none
** Breaks : Disabled
**
** Registers
** Input buffer : SCI1D [001F]
** Output buffer : SCI1D [001F]
** Control register : SCI1C1 [001A]
** Mode register : SCI1C2 [001B]
** Baud setting reg. : SCI1BD [0018]
** Special register : SCI1S1 [001C]
**
** Input interrupt
** Vector name : Vsci1rx
**
** Output interrupt
** Vector name : Vsci1tx
**
** Used pins:
** ----------------------------------------------------------
** Function | On package | Name
** ----------------------------------------------------------
** Input | 10 | PTE1_RxD1
** Output | 9 | PTE0_TxD1
** RTS | 33 | PTA1_KBI1P1
** ----------------------------------------------------------
**
** Note: RTS pin is NOT supported by hardware.
** It is handled by software.
**
**
** 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-2005
** 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 */
#define FULL_TX 0x20 /* Full transmit 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 */
/* Bit 5 - Full TX buffer */
static bool EnMode; /* Enable/Disable SCI in speed mode */
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 : HWEnDi (bean AsynchroSerial)
**
** Description :
** Enables or disables the peripheral(s) associated with the bean.
** The method is called automatically as a part of the Enable and
** Disable methods and several internal methods.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void HWEnDi(void)
{
if(EnMode) { /* Enable device? */
SCI1BDH = 0x00; /* Set high divisor register (enable device) */
SCI1BDL = 0x16; /* Set low divisor register (enable device) */
/* SCI1C3: ORIE=1,NEIE=1,FEIE=1,PEIE=1 */
SCI1C3 |= 0x0F; /* Enable error interrupts */
SCI1C2 |= ( SCI1C2_TE_MASK | SCI1C2_RE_MASK | SCI1C2_RIE_MASK); /* Enable transmitter, Enable receiver, Enable receiver interrupt */
if(UART_InpLen < UART_RTS_BUF_SIZE) /* Is number of chars in the receive buffer lower than size of the RTS buffer? */
/* PTAD: PTAD1=0 */
PTAD &= ~0x02; /* Set RTS to the low level */
}
else {
/* PTAD: PTAD1=1 */
PTAD |= 0x02; /* Set RTS to the high level */
/* SCI1C3: ORIE=0,NEIE=0,FEIE=0,PEIE=0 */
SCI1C3 &= ~0x0F; /* Disable error interrupts */
SCI1C2 &= ( (~SCI1C2_RE_MASK) & (~SCI1C2_TE_MASK) & (~SCI1C2_TIE_MASK) & (~SCI1C2_RIE_MASK)); /* Disable receiver, Disable transmitter, Disable transmit interrupt, Disable receiver interrupt */
SCI1BDH = 0x00; /* Set high divisor register to zero (disable device) */
SCI1BDL = 0x00; /* Set low divisor register to zero (disable device) */
}
}
/*
** ===================================================================
** 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.
** DMA mode:
** If DMA controller is available on the selected CPU and
** the receiver is configured to use DMA controller then
** this method only sets the selected DMA channel. Then the
** status of the DMA transfer can be checked using
** GetCharsInRxBuf method. See an example of a typical usage
** for details about the communication using DMA.
** 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 <Interrupt service> property
** is disabled)
** ERR_COMMON - common error occurred (the
** GetError method can be used for error
** specification)
** DMA mode:
** If DMA controller is available on the
** selected CPU and the receiver is
** configured to use DMA controller then
** only ERR_OK, ERR_RXEMPTY, and ERR_SPEED
** error code can be returned from this
** method.
** ===================================================================
*/
byte UART_RecvChar(UART_TComData *Chr)
{
byte Result = ERR_OK; /* Return error code */
if(!EnMode) /* Is the device disabled in the actual speed CPU mode? */
return ERR_SPEED; /* If yes then error */
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 */
if (++InpIndxR >= UART_INP_BUF_SIZE) /* Is the index out of the buffer? */
InpIndxR = 0; /* Set the index to the start of the buffer */
if(UART_InpLen <= UART_RTS_BUF_SIZE)
PTAD &= ~0x02; /* Set RTS to the low level */
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.
** DMA mode:
** If DMA controller is available on the selected CPU and
** the transmitter is configured to use DMA controller then
** this method only sets selected DMA channel. Then the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -