#define UART_GLOBAL
#include "uart.h"
pUartFifo_t pUart1RxFifo; // Pointer to a FIFO Buffer of the UART1 Receive
pUartFifo_t pUart1TxFifo; // Pointer to a FIFO Buffer of the UART1 Transmit
pUartFifo_t pUart2RxFifo; // Pointer to a FIFO Buffer of the UART2 Receive
pUartFifo_t pUart2TxFifo; // Pointer to a FIFO Buffer of the UART2 Transmit
// Hold UART1 Evens (PE, BI, FE, OE)
UartLineEvents_t Uart1LineEvents;
// Hold UART2 Evens (PE, BI, FE, OE)
UartLineEvents_t Uart2LineEvents;
/*************************************************************************
* Function Name: FifoPush
* Parameters: pUartFifo_t Fifo, Int8U Data
*
* Return: Boolean
*
* Description: Push a char in a FIFO. Return TRUE when push is successful
* or FALSE when the FIFO is full.
*
*************************************************************************/
static Boolean FifoPush(pUartFifo_t Fifo, Int8U Data)
{
Int32U IndxTmp;
// calculate next push index
IndxTmp = Fifo->PushIndx + 1;
IndxTmp = IndxTmp % UART_FIFO_SIZE;
// Check FIFO state
if (IndxTmp == Fifo->PopIndx)
{
// The FIFO is full
return(FALSE);
}
// Push the data
Fifo->Buffer[Fifo->PushIndx] = Data;
// Updating the push's index
Fifo->PushIndx = IndxTmp;
return(TRUE);
}
========================================================
看完是怎么压FIFO和弹FIFO的,然后再看看中断,我把发送中断提出来了。
=========================================================
void Uart1Isr(void)
{
Int8U Data;
if( USART_GetFlagStatus(USART1,USART_FLAG_TXE)
&& (USART_GetITStatus (USART1,USART_IT_TXE) == SET))
{
if(FifoPop(pUart1TxFifo,&Data))
{
USART_SendData(USART1,Data);
}
else
{
USART_ITConfig(USART1,USART_IT_TXE ,DISABLE);
}
}
}
=======================================================
再看发送函数
=========================================================
Int32U UartWrite(UartNum_t Uart,pInt8U pBuffer, Int32U BufferSize)
{
Int32U Count = 0;
pUartFifo_t pUartFifo;
USART_TypeDef * pUart;
switch(Uart)
{
case UART_1:
pUart = USART1;
pUartFifo = pUart1TxFifo;
break;
case UART_2:
pUart = USART2;
pUartFifo = pUart2TxFifo;
break;
default:
return(0);
}
if(BufferSize != 0)
{
ENTR_CRT_SECTION();
if(pUartFifo->PopIndx == pUartFifo->PushIndx)
{
USART_SendData(pUart,*pBuffer);
USART_ITConfig(pUart,USART_IT_TXE ,ENABLE);
++Count;
}
for ( ; Count < BufferSize; ++Count)
{
if(!FifoPush(pUartFifo,*(pBuffer+Count)))
{
break;
}
}
EXT_CRT_SECTION();
}
return(Count);
}
============================================================
装入新数据前,看看FIFO是不是被挤干了,如果挤干了,就往发送器里放入新数,然后再填装FIFO
注意了,假如我们要一个字节一个字节的发,万一出现以下情况:
当FIFO里还有最后一个数据时,串口中断发生,最后一个字节送入到发送器,这个时候可不可以算FIFO已经空了?
然后,调用发送函数,发送函数判断到FIFO已经空了。会不会往发送器中再放数据?可惜这个时候发送器正在发送上一包的最后一个字节啊。
============================================================ |