打印

LPC1788库函数,串口通信发送代码,blocking原理不是很懂求指教

[复制链接]
3845|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
hyh19890917|  楼主 | 2013-12-25 14:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/*********************************************************************//**
* @brief                Send a block of data via UART peripheral
* @param[in]        UARTx        Selected UART peripheral used to send data, should be:
*                                - UART_0: UART0 peripheral
*                                - UART_1: UART1 peripheral
*                                - UART_2: UART2 peripheral
*                                - UART_3: UART3 peripheral
*                                - UART_4: UART4 peripheral
* @param[in]        txbuf         Pointer to Transmit buffer
* @param[in]        buflen         Length of Transmit buffer
* @param[in]         flag         Flag used in  UART transfer, should be
*                                                 NONE_BLOCKING or BLOCKING
* @return                 Number of bytes sent.
*
* Note: when using UART in BLOCKING mode, a time-out condition is used
* via defined symbol UART_BLOCKING_TIMEOUT.
**********************************************************************/
uint32_t UART_Send(UART_ID_Type UartID, uint8_t *txbuf,
                                                        uint32_t buflen, TRANSFER_BLOCK_Type flag)
{
        uint32_t bToSend, bSent, timeOut, fifo_cnt;
        uint8_t *pChar = txbuf;
        __IO uint32_t *LSR = NULL;

        switch (UartID)
        {
                case UART_0:
                        LSR = (__IO uint32_t *)&LPC_UART0->LSR;
                        break;
                case UART_1:
                        LSR = (__IO uint32_t *)&LPC_UART1->LSR;
                        break;
                case UART_2:
                        LSR = (__IO uint32_t *)&LPC_UART2->LSR;
                        break;
                case UART_3:
                        LSR = (__IO uint32_t *)&LPC_UART3->LSR;
                        break;
                case UART_4:
                        LSR = (__IO uint32_t *)&LPC_UART4->LSR;
                        break;
        }

        bToSend = buflen;

        // blocking mode
        if (flag == BLOCKING)
        {
                bSent = 0;
                while (bToSend)
                {
                        timeOut = UART_BLOCKING_TIMEOUT;

                        // Wait for THR empty with timeout
                        while (!(*LSR & UART_LSR_THRE))
                        {
                                if (timeOut == 0)
                                        break;

                                timeOut--;
                        }

                        // Time out!
                        if(timeOut == 0)
                                break;

                        fifo_cnt = UART_TX_FIFO_SIZE;

                        while (fifo_cnt && bToSend)
                        {
                                UART_SendByte(UartID, (*pChar++));

                                fifo_cnt--;

                                bToSend--;

                                bSent++;
                        }
                }
        }

        // None blocking mode
        else
        {
                bSent = 0;
                while (bToSend)
                {
                        if (bToSend == 0)
                                break;

                        if (!(*LSR & UART_LSR_THRE))
                        {
                                break;
                        }

                        fifo_cnt = UART_TX_FIFO_SIZE;

                        while (fifo_cnt && bToSend)
                        {
                                UART_SendByte(UartID, (*pChar++));

                                bToSend--;

                                fifo_cnt--;

                                bSent++;
                        }
                }
        }

        return bSent;
}

相关帖子

沙发
huangxz| | 2013-12-25 21:00 | 只看该作者
block在这里是阻塞的意思

使用特权

评论回复
板凳
hyh19890917|  楼主 | 2013-12-26 14:23 | 只看该作者
huangxz 发表于 2013-12-25 21:00
block在这里是阻塞的意思

单词意思我知道啊,阻塞是为了避免什么么?

使用特权

评论回复
地板
huangxz| | 2013-12-26 14:55 | 只看该作者
hyh19890917 发表于 2013-12-26 14:23
单词意思我知道啊,阻塞是为了避免什么么?

避免丢失数据,

使用特权

评论回复
5
huangxz| | 2013-12-26 15:00 | 只看该作者
如果你知道英文单词是阻塞的意思,我觉的你到网上一搜就明白了,
阻塞和非阻塞的概念了。

使用特权

评论回复
6
hyh19890917|  楼主 | 2013-12-26 18:57 | 只看该作者
huangxz 发表于 2013-12-26 15:00
如果你知道英文单词是阻塞的意思,我觉的你到网上一搜就明白了,
阻塞和非阻塞的概念了。 ...

额好吧

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

20

主题

162

帖子

0

粉丝