📄 uart.c
字号:
** status of the DMA transfer can be checked using
** GetCharsInTxBuf method. See an example of a typical usage
** for details about communication using DMA.
** 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(!EnMode) /* Is the device disabled in the actual speed CPU mode? */
return ERR_SPEED; /* If yes then error */
if(UART_OutLen == UART_OUT_BUF_SIZE) /* 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 */
UART_OutLen++; /* Increase number of bytes in the transmit buffer */
OutBuffer[OutIndxW] = Chr; /* Store char to buffer */
if (++OutIndxW >= UART_OUT_BUF_SIZE) /* Is the index out of the buffer? */
OutIndxW = 0; /* Set the index to the start of the buffer */
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.
** 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 communication using DMA.
** 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_VALUE - Parameter is out of range.
** 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 codes can be returned from this
** method.
** ===================================================================
*/
byte UART_RecvBlock(UART_TComData *Ptr, word Size, word *Rcv)
{
word count; /* Number of received chars */
byte result = ERR_OK; /* Last error */
if(!EnMode) /* Is the device disabled in the actual speed CPU mode? */
return ERR_SPEED; /* If yes then error */
for(count = 0; count < Size; count++) {
result = UART_RecvChar(Ptr++);
if(result != ERR_OK) { /* Receiving given number of chars */
break; /* Break data block receiving */
}
}
*Rcv = count; /* Return number of received chars */
return result; /* Return 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.
** 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 the selected DMA channel. Then the
** status of the DMA transfer can be checked using
** GetCharsInTxBuf method. See typical usage for details
** about communication using DMA.
** 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 UART_SendBlock(UART_TComData * Ptr, word Size, word *Snd)
{
word count; /* Number of sent chars */
byte result = ERR_OK; /* Last error */
if(!EnMode) /* Is the device disabled in the actual speed CPU mode? */
return ERR_SPEED; /* If yes then error */
for(count = 0; count < Size; count++) {
result = UART_SendChar(*Ptr++);
if(result != ERR_OK) { /* Sending given number of chars */
break; /* Break data block sending */
}
}
*Snd = count; /* Return number of sent chars */
return result; /* Return last error code*/
}
/*
** ===================================================================
** Method : UART_ClearRxBuf (bean AsynchroSerial)
**
** Description :
** Clears the receive buffer.
** This method is available only if non-zero length of the
** input buffer is defined and 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 stops selected DMA channel.
** Parameters : None
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ===================================================================
*/
byte UART_ClearRxBuf(void)
{
if(!EnMode) /* Is the device disabled in the actual speed CPU mode? */
return ERR_SPEED; /* If yes then error */
EnterCritical(); /* Save the PS register */
UART_InpLen = 0; /* Set number of chars in the transmit buffer to 0 */
InpIndxR = InpIndxW = 0; /* Reset indices */
ExitCritical(); /* Restore the PS register */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : UART_ClearTxBuf (bean AsynchroSerial)
**
** Description :
** Clears the transmit buffer.
** This method is available only if non-zero length of the
** output buffer is defined and the receiver 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 stops selected DMA channel.
** Parameters : None
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ===================================================================
*/
byte UART_ClearTxBuf(void)
{
if(!EnMode) /* Is the device disabled in the actual speed CPU mode? */
return ERR_SPEED; /* If yes then error */
EnterCritical(); /* Save the PS register */
UART_OutLen = 0; /* Set number of chars in the receive buffer to 0 */
OutIndxR = OutIndxW = 0; /* Reset indices */
ExitCritical(); /* Restore the PS register */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : UART_GetCharsInRxBuf (bean AsynchroSerial)
**
** Description :
** Returns the number of characters in the input buffer.
** This method is available 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 returns the number of characters in the
** receive buffer.
** Parameters : None
** Returns :
** --- - The number of characters in the input
** buffer.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -