📄 uart.c
字号:
** ===================================================================
*/
/*
word UART_GetCharsInRxBuf(void)
** This method is implemented as a macro. See header module. **
*/
/*
** ===================================================================
** Method : UART_GetCharsInTxBuf (bean AsynchroSerial)
**
** Description :
** Returns the number of characters in the output buffer.
** 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 returns the number of characters in the
** transmit buffer.
** Parameters : None
** Returns :
** --- - The number of characters in the output
** buffer.
** ===================================================================
*/
/*
word UART_GetCharsInTxBuf(void)
** This method is implemented as a macro. See header module. **
*/
/*
** ===================================================================
** Method : UART_InterruptRx (bean AsynchroSerial)
**
** Description :
** The method services the receive interrupt of the selected
** peripheral(s) and eventually invokes the bean's event(s).
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define ON_ERROR 1
#define ON_FULL_RX 2
#define ON_RX_CHAR 4
#define ON_IDLE_CHAR 8
ISR(UART_InterruptRx)
{
UART_TComData Data; /* Temporary variable for data */
byte StatReg = getReg(SCI1S1);
byte OnFlags = 0; /* Temporary variable for flags */
PTAD |= 0x02;
Data = SCI1D; /* Read data from the receiver */
if(UART_InpLen < UART_INP_BUF_SIZE) { /* Is number of bytes in the receive buffer lower than size of buffer? */
UART_InpLen++; /* Increse number of chars in the receive buffer */
InpBuffer[InpIndxW] = Data; /* Save received char to the receive buffer */
if (++InpIndxW >= UART_INP_BUF_SIZE) /* Is the index out of the buffer? */
InpIndxW = 0; /* Set the index to the start of the buffer */
OnFlags |= ON_RX_CHAR; /* Set flag "OnRXChar" */
} else {
SerFlag |= FULL_RX; /* If yes then set flag buffer overflow */
OnFlags |= ON_ERROR; /* Set flag "OnError" */
}
if(OnFlags & ON_ERROR) { /* Was error flag detect? */
UART_OnError(); /* If yes then invoke user event */
}
else {
if(OnFlags & ON_RX_CHAR) { /* Is OnRxChar flag set? */
UART_OnRxChar(); /* If yes then invoke user event */
}
if(OnFlags & ON_FULL_RX) { /* Is OnFullRxBuf flag set? */
UART_OnFullRxBuf(); /* If yes then invoke user event */
}
}
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 low level */
}
/*
** ===================================================================
** Method : UART_InterruptTx (bean AsynchroSerial)
**
** Description :
** The method services the transmit interrupt of the selected
** peripheral(s) and eventually invokes the bean's event(s).
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define ON_FREE_TX 1
#define ON_TX_CHAR 2
ISR(UART_InterruptTx)
{
byte OnFlags = 0; /* Temporary variable for flags */
if(SerFlag & RUNINT_FROM_TX) /* Is flag "running int from TX" set? */
OnFlags |= ON_TX_CHAR; /* Set flag "OnTxChar" */
SerFlag &= ~RUNINT_FROM_TX; /* Reset flag "running int from TX" */
if(UART_OutLen) { /* Is number of bytes in the transmit buffer greater then 0? */
UART_OutLen--; /* Decrease number of chars in the transmit buffer */
SerFlag |= RUNINT_FROM_TX; /* Set flag "running int from TX"? */
SCI1S1; /* Reset interrupt request flag */
SCI1D = OutBuffer[OutIndxR]; /* Store char to transmitter register */
if (++OutIndxR >= UART_OUT_BUF_SIZE) /* Is the index out of the buffer? */
OutIndxR = 0; /* Set the index to the start of the buffer */
}
else {
OnFlags |= ON_FREE_TX; /* Set flag "OnFreeTxBuf" */
SCI1C2_TIE = 0; /* Disable transmit interrupt */
}
if(OnFlags & ON_TX_CHAR) { /* Is flag "OnTxChar" set? */
UART_OnTxChar(); /* If yes then invoke user event */
}
if(OnFlags & ON_FREE_TX) { /* Is flag "OnFreeTxBuf" set? */
UART_OnFreeTxBuf(); /* If yes then invoke user event */
}
}
/*
** ===================================================================
** Method : UART_InterruptError (bean AsynchroSerial)
**
** Description :
** The method services the error interrupt of the selected
** peripheral(s) and eventually invokes the bean's event(s).
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
ISR(UART_InterruptError)
{
byte StatReg = getReg(SCI1S1);
if(StatReg & (SCI1S1_OR_MASK|SCI1S1_NF_MASK|SCI1S1_FE_MASK|SCI1S1_PF_MASK)) { /* Is an error detected? */
SerFlag |= COMMON_ERR; /* If yes then set an internal flag */
}
SCI1D; /* Dummy read of data register - clear error bits */
if(SerFlag & (COMMON_ERR)) { /* Was any error set? */
UART_OnError(); /* If yes then invoke user event */
}
}
/*
** ===================================================================
** Method : UART_Init (bean AsynchroSerial)
**
** Description :
** Initializes the associated peripheral(s) and the bean's
** internal variables. The method is called automatically as a
** part of the application initialization code.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void UART_Init(void)
{
SerFlag = 0; /* Reset flags */
UART_InpLen = 0; /* No char in the receive buffer */
InpIndxR = InpIndxW = 0; /* Reset indices */
UART_OutLen = 0; /* No char in the transmit buffer */
OutIndxR = OutIndxW = 0; /* Reset indices */
/* SCI1C1: LOOPS=0,SCISWAI=0,RSRC=0,M=0,WAKE=0,ILT=0,PE=0,PT=0 */
setReg8(SCI1C1, 0x00); /* Configure the SCI */
/* SCI1C3: R8=0,T8=0,TXDIR=0,??=0,ORIE=0,NEIE=0,FEIE=0,PEIE=0 */
setReg8(SCI1C3, 0x00); /* Disable error interrupts */
/* SCI1C2: TIE=0,TCIE=0,RIE=0,ILIE=0,TE=0,RE=0,RWU=0,SBK=0 */
setReg8(SCI1C2, 0x00); /* Disable all interrupts */
UART_SetHigh(); /* Initial speed CPU mode is high */
}
/*
** ===================================================================
** Method : UART_SetHigh (bean AsynchroSerial)
**
** Description :
** The method reconfigures the bean and its selected peripheral(s)
** when the CPU is switched to the High speed mode. The method is
** called automatically as s part of the CPU SetHighSpeed method.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void UART_SetHigh(void)
{
EnMode = TRUE; /* Set the flag "device enabled" in the actual speed CPU mode */
HWEnDi(); /* Enable/disable device according to status flags */
}
/*
** ===================================================================
** Method : UART_SetLow (bean AsynchroSerial)
**
** Description :
** The method reconfigures the bean and its selected peripheral(s)
** when the CPU is switched to the Low speed mode. The method is
** called automatically as a part of the CPU SetLowSpeed method.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void UART_SetLow(void)
{
EnMode = FALSE; /* Set the flag "device disabled" in the actual speed CPU mode */
HWEnDi(); /* Enable/disable device according to status flags */
}
/* END UART. */
/*
** ###################################################################
**
** This file was created by UNIS Processor Expert 2.97 [03.74]
** for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -