/********************(C)COPYRIGHT2008STMicroelectronics*********************FileName:uart.c*Author:MCDApplicationTeam*Version:V2.0*Date:05/23/2008*Description:Mainprogrambody*********************************************************************************THEPRESENTFIRMWAREWHICHISFORGUIDANCEONLYAIMSATPROVIDINGCUSTOMERS*WITHCODINGINFORMATIONREGARDINGTHEIRPRODUCTSINORDERFORTHEMTOSAVETIME.*ASARESULT,STMICROELECTRONICSSHALLNOTBEHELDLIABLEFORANYDIRECT,*INDIRECTORCONSEQUENTIALDAMAGESWITHRESPECTTOANYCLAIMSARISINGFROMTHE*CONTENTOFSUCHSOFTWAREAND/ORTHEUSEMADEBYCUSTOMERSOFTHECODING*INFORMATIONCONTAINEDHEREININCONNECTIONWITHTHEIRPRODUCTS.*FORMOREINFORMATIONPLEASECAREFULLYREADTHELICENSEAGREEMENTFILELOCATED*INTHEROOTDIRECTORYOFTHISFIRMWAREPACKAGE.*******************************************************************************//*Includes------------------------------------------------------------------*/#includeUart.h/*Privatetypedef-----------------------------------------------------------*///#defineInc_Addr(Addr,BufSize){Addr++;if(Addr=BufSize){Addr=0;}}//#defineAdd_Addr(Addr,Num,BufSize){Addr+=Num;if(Addr=BufSize){Addr-=BufSize;}}/*Privatedefine------------------------------------------------------------*//*Privatemacro-------------------------------------------------------------*//*Privatevariables---------------------------------------------------------*//*Privatefunctionprototypes-----------------------------------------------*//*Privatefunctions---------------------------------------------------------*///=============================================================================//=============================================================================/********************************************************************************FunctionName:UserUart_Configuration*Description:UserUartprogram*Input:None*Output:None*Return:None*******************************************************************************/unsignedintUartFromPosRxBufDataLen(UART_CTRL_TYPE*pUart,unsignedintPos){if(Pos==pUart-pRxLoop-Tail){if(pUart-pRxLoop-Empty)return(0);if(pUart-pRxLoop-Full)return(pUart-pPara-RxBufSize);}if(PospUart-pRxLoop-Tail)return(pUart-pPara-RxBufSize+pUart-pRxLoop-Tail-Pos);elsereturn(pUart-pRxLoop-Tail-Pos);}unsignedintUartRxBufDataLen(UART_CTRL_TYPE*pUart){if(pUart-pRxLoop-Empty)return(0);if(pUart-pRxLoop-Full)return(pUart-pPara-RxBufSize);if(pUart-pRxLoop-HeadpUart-pRxLoop-Tail)return(pUart-pPara-RxBufSize+pUart-pRxLoop-Tail-pUart-pRxLoop-Head);elsereturn(pUart-pRxLoop-Tail-pUart-pRxLoop-Head);}unsignedintUartTxBufNopLen(UART_CTRL_TYPE*pUart){if(pUart-pTxLoop-Empty)return(pUart-pPara-TxBufSize);if(pUart-pTxLoop-Full)return(0);if(pUart-pTxLoop-HeadpUart-pTxLoop-Tail)return(pUart-pPara-TxBufSize+pUart-pTxLoop-Head-pUart-pTxLoop-Tail);elsereturn(pUart-pTxLoop-Head-pUart-pTxLoop-Tail);}//-------voidUART_IRQHandler(UART_CTRL_TYPE*pUart){if(USART_GetITStatus(pUart-pBase,USART_IT_RXNE)!=RESET){//Readonebytefromthereceivedataregisterif(!pUart-pRxLoop-Full){pUart-pRxLoop-pBuf[pUart-pRxLoop-Tail]=USART_ReceiveData(pUart-pBase);Inc_Addr(pUart-pRxLoop-Tail,pUart-pPara-RxBufSize)if(pUart-pRxLoop-Head==pUart-pRxLoop-Tail)pUart-pRxLoop-Full=true;pUart-pRxLoop-Empty=false;}else{USART_ReceiveData(pUart-pBase);//¿Õ¶ÁÇåÖжÏ}pUart-UartRxNopSysTick=0;//Æô¶¯Uart½ÓÊÕ¶¨Ê±//PutLedDispStatus(RxOn);}elseif(USART_GetITStatus(pUart-pBase,USART_IT_TXE)!=RESET){if(pUart-pTxLoop-Empty){//DisabletheUSARTTransmitinterruptUSART_ITConfig(pUart-pBase,USART_IT_TXE,DISABLE);if(!pUart-DuplexStatus){//´®¿Ú²»ÊÇÈ«Ë«¹¤USART_ITConfig(pUart-pBase,USART_IT_RXNE,ENABLE);//EnableUSARTReceiveinterrupts}//PutLedDispStatus(TxOff);}else{USART_SendData(pUart-pBase,pUart-pTxLoop-pBuf[pUart-pTxLoop-Head]);//WriteonebytetothetransmitdataregisterInc_Addr(pUart-pTxLoop-Head,pUart-pPara-TxBufSize)if(pUart-pTxLoop-Head==pUart-pTxLoop-Tail){pUart-pTxLoop-Empty=true;}pUart-pTxLoop-Full=false;//PutLedDispStatus(TxOn);}}elseif(USART_GetITStatus(pUart-pBase,USART_IT_ORE)!=RESET){//ORE:OverrunerrorinterruptItisclearedbyasoftwaresequence(anreadtotheUSART_SRregisterfollowedbyareadtotheUSART_DRregister).//USART_ClearFlag(pUart-pBase,USART_IT_ORE);//USART_ClearITPendingBit(pUart-pBase,USART_IT_ORE);//¶ÁUSART-SRUSART_GetFlagStatus(pUart-pBase,USART_IT_ORE);//¶ÁUSART-SRUSART_ReceiveData(pUart-pBase);//¿Õ¶ÁUSART_DR}}//-------boolUartPutByte(UART_CTRL_TYPE*pUart,unsignedcharUartData){//Ïò´®¿Ú·¢ËÍÒ»×Ö½ÚÊý¾Ýif(pUart-pTxLoop-Full)return(false);pUart-pTxLoop-pBuf[pUart-pTxLoop-Tail]=UartData;Inc_Addr(pUart-pTxLoop-Tail,pUart-pPara-TxBufSize)if(pUart-pTxLoop-Head==pUart-pTxLoop-Tail){pUart-pTxLoop-Full=true;}pUart-pTxLoop-Empty=false;USART_ITConfig(pUart-pBase,USART_IT_TXE,ENABLE);//EnableUSARTTransmitinterruptsif(!pUart-DuplexStatus){//´®¿Ú²»ÊÇÈ«Ë«¹¤USART_ITConfig(pUart-pBase,USART_IT_RXNE,DISABLE);//DisableUSARTReceiveinterrupts}return(true);}//-------boolUar