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

📄 buffer.c

📁 基于STR710和嵌入式操作系统的串口驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
* Arguments   : 'ch'    is the COMM port channel number and can either be:
*                           COMM1
*                           COMM2
* Returns     : TRUE    if the buffer IS empty.
*               FALSE   if the buffer IS NOT empty or you have specified an incorrect channel.
*********************************************************************************************************
*/

BOOLEAN  CommRxBufIsEmpty (INT8U ch)
{
    BOOLEAN        empty;
    OS_CPU_SR  	   cpu_sr;
    COMM_RING_BUF *pbuf;


    switch (ch)											/* Obtain pointer to communications channel */ 
    {                                          
        /*case COMM0:
             pbuf = &Comm0Buf;
             break;
        case COMM1:
             pbuf = &Comm1Buf;
             break;
        case COMM2:
             pbuf = &Comm2Buf;
             break;*/
        case COMM3:
             pbuf = &Comm3Buf;
             break;
        default:
             return (TRUE);
    }
    OS_ENTER_CRITICAL();
    if (pbuf->RingBufRxCtr > 0) {                          /* See if buffer is empty                   */
        empty = FALSE;                                     /* Buffer is NOT empty                      */
    } else {
        empty = TRUE;                                      /* Buffer is empty                          */
    }
    OS_EXIT_CRITICAL();
    return (empty);
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                   SEE IF TX CHARACTER BUFFER IS FULL
*
*
* Description : This function is called by your application to see if any more characters can be placed
*               in the Tx buffer.  In other words, this function check to see if the Tx buffer is full.
*               If the buffer is full, the function returns TRUE otherwise, the function returns FALSE.
* Arguments   : 'ch'    is the COMM port channel number and can either be:
*                           COMM1
*                           COMM2
* Returns     : TRUE    if the buffer IS full.
*               FALSE   if the buffer IS NOT full or you have specified an incorrect channel.
*********************************************************************************************************
*/

BOOLEAN  CommTxBufIsFull (INT8U ch)
{
    BOOLEAN        full;
    OS_CPU_SR  	   cpu_sr;
    COMM_RING_BUF *pbuf;

    switch (ch)											/* Obtain pointer to communications channel */ 
    {                                          
        /*case COMM0:
             pbuf = &Comm0Buf;
             break;
        case COMM1:
             pbuf = &Comm1Buf;
             break;
        case COMM2:
             pbuf = &Comm2Buf;
             break;*/
        case COMM3:
             pbuf = &Comm3Buf;
             break;
        default:
             return (TRUE);
    }
    OS_ENTER_CRITICAL();
    if (pbuf->RingBufTxCtr < COMM_TX_BUF_SIZE) 			   /* See if buffer is full                    */
    {           
        full = FALSE;                                      /* Buffer is NOT full                       */
    } 
    else 
    {
        full = TRUE;                                       /* Buffer is full                           */
    }
    OS_EXIT_CRITICAL();
    return (full);
}
/*$PAGE*/
/*
*********************************************************************************************************
*                                    INSERT CHARACTER INTO RING BUFFER
*
*
* Description : This function is called by the Rx ISR to insert a character into the receive ring buffer.
* Arguments   : 'ch'    is the COMM port channel number and can either be:
*                           COMM1
*                           COMM2
*               'c'     is the character to insert into the ring buffer.  If the buffer is full, the
*                       character will not be inserted, it will be lost.
*********************************************************************************************************
*/

void  CommPutRxChar (INT8U ch, INT8U c)
{
    COMM_RING_BUF *pbuf;


    switch (ch)											/* Obtain pointer to communications channel */ 
    {                                          
        /*case COMM0:
             pbuf = &Comm0Buf;
             break;
        case COMM1:
             pbuf = &Comm1Buf;
             break;
        case COMM2:
             pbuf = &Comm2Buf;
             break;*/
        case COMM3:
             pbuf = &Comm3Buf;
             break;
        default:
             return;
    }
    if (pbuf->RingBufRxCtr < COMM_RX_BUF_SIZE) {           /* See if buffer is full                    */
        pbuf->RingBufRxCtr++;                              /* No, increment character count            */
        *pbuf->RingBufRxInPtr++ = c;                       /* Put character into buffer                */
        if (pbuf->RingBufRxInPtr == &pbuf->RingBufRx[COMM_RX_BUF_SIZE]) { /* Wrap IN pointer           */
            pbuf->RingBufRxInPtr = &pbuf->RingBufRx[0];
        }
        OSSemPost(pbuf->RingBufRxSem);                     /* Indicate that character was received     */
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                            OUTPUT CHARACTER
*
*
* Description : This function is called by your application to send a character on the communications
*               channel.  The function will wait for the buffer to empty out if the buffer is full.
*               The function returns to your application if the buffer doesn't empty within the specified
*               timeout.  A timeout value of 0 means that the calling function will wait forever for the
*               buffer to empty out.  The character to send is first inserted into the Tx buffer and will
*               be sent by the Tx ISR.  If this is the first character placed into the buffer, the Tx ISR
*               will be enabled.
* Arguments   : 'ch'    is the COMM port channel number and can either be:
*                           COMM1
*                           COMM2
*               'c'     is the character to send.
*               'to'    is the timeout (in clock ticks) to wait in case the buffer is full.  If you
*                       specify a timeout of 0, the function will wait forever for the buffer to empty.
* Returns     : COMM_NO_ERR      if the character was placed in the Tx buffer
*               COMM_TX_TIMEOUT  if the buffer didn't empty within the specified timeout period
*               COMM_BAD_CH      if you specify an invalid channel number
*********************************************************************************************************
*/

INT8U  CommPutChar (INT8U ch, INT8U c, INT16U to)
{
    INT8U          oserr;
    OS_CPU_SR  	   cpu_sr;
    COMM_RING_BUF *pbuf;


    switch (ch)											/* Obtain pointer to communications channel */ 
    {                                          
        /*case COMM0:
             pbuf = &Comm0Buf;
             break;
        case COMM1:
             pbuf = &Comm1Buf;
             break;
        case COMM2:
             pbuf = &Comm2Buf;
             break;*/
        case COMM3:
             pbuf = &Comm3Buf;
             break;
        default:
             return (COMM_BAD_CH);
    }
    
    OSSemPend(pbuf->RingBufTxSem, to, &oserr);             /* Wait for space in Tx buffer              */
    if (oserr == OS_TIMEOUT) 
    {
        return (COMM_TX_TIMEOUT);                          /* Timed out, return error code             */
    }
    
    OS_ENTER_CRITICAL();
    
    pbuf->RingBufTxCtr++;                                  /* No, increment character count            */
    *pbuf->RingBufTxInPtr++ = c;                           /* Put character into buffer                */
    
    if (pbuf->RingBufTxInPtr == &pbuf->RingBufTx[COMM_TX_BUF_SIZE]) /* Wrap IN pointer           */
    {     
        pbuf->RingBufTxInPtr = &pbuf->RingBufTx[0];
    }
    
    if (pbuf->RingBufTxCtr == 1) 						/* See if this is the first character       */
    {                         
       CommTxIntEn(ch);                                /* 如果是第一个字符则允许发送中断           */
    }
    
    OS_EXIT_CRITICAL();
    return (COMM_NO_ERR);
}
/********************************************************************************************************
** 函数名称: CommSendData
** 功能描述: 发送多个字节数据
**
** 参    数: Data:发送数据存储位置
**           NByte:发送数据个数
**
** 返 回 值: None
**         
** 作   者: 罗辉联
** 日   期: 2006年10月10日
**-------------------------------------------------------------------------------------------------------
** 修 改 人: 
** 日   期: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void CommSendData(INT8U ch,INT8U *data, INT16U count)
{
	OS_CPU_SR	cpu_sr;
	
    OS_ENTER_CRITICAL();
    while (count-- > 0)
    {
        CommPutChar (ch, *data++, 0);
    }
    OS_EXIT_CRITICAL();
} 

/********************************************************************************************************
** 函数名称: CommTxIntEn
** 功能描述: 允许制定串口发送中断
**
** 参    数: ch指串行通信信道,可以是COMM1、COMM2、COMM3、COMM4
**
** 返 回 值: None
**         
** 作   者: 罗辉联
** 日   期: 2006年11月10日
**-------------------------------------------------------------------------------------------------------
** 修 改 人: 
** 日   期: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void CommTxIntEn(INT8U ch)
{
	UART_ITConfig(ch,UART_TXEMPTY,ENABLE);
}

/********************************************************************************************************
** 函数名称: CommTxIntDis
** 功能描述: 禁止允许制定串口发送中断
**
** 参    数: ch指串行通信信道,可以是COMM1、COMM2、COMM3、COMM4

**
** 返 回 值: None
**         
** 作   者: 罗辉联
** 日   期: 2006年11月10日
**-------------------------------------------------------------------------------------------------------
** 修 改 人: 
** 日   期: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void CommTxIntDis(INT8U ch)
{
	UART_ITConfig(ch,UART_TXEMPTY,DISABLE);
}

⌨️ 快捷键说明

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