Freescale 9S12 系列单片机应用笔记(SCI)2
时间:11-20
来源:互联网
点击:
- aseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- *err=SCI_BAD_CH;
- return(0);
- }
- OS_ENTER_CRITICAL();
- if(pbuf->RingBufRxCtr>0)/*Seeifbufferisempty*/
- {
- pbuf->RingBufRxCtr--;/*No,decrementcharactercount*/
- c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
- if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE])
- {
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];/*WrapOUTpointer*/
- }
- OS_EXIT_CRITICAL();
- *err=SCI_NO_ERR;
- return(c);
- }
- else
- {
- OS_EXIT_CRITICAL();
- *err=SCI_RX_EMPTY;
- c=0;/*Bufferisempty,return0*/
- return(c);
- }
- }
- /**
- *Tosendacharacteronthecommunicationschannel.
- *ThecharactertosendisfirstinsertedintotheTxbufferandwillbesentby
- *theTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISRwillbe
- *enabled.IftheTxbufferisfull,thecharacterwillnotbesent(i.e.itwillbelost)
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnCOMM_NO_ERRifthefunctionwassuccessful(thebufferwasnotfull)
- *COMM_TX_FULLifthebufferwasfull
- *COMM_BAD_CHifyouhavespecifiedanincorrectchannel
- */
- unsignedcharSCIPutCharB(unsignedcharport,unsignedcharc)
- {
- unsignedcharcpu_sr;
- SCI_RING_BUF*pbuf;
- /*Obtainpointertocommunicationschannel*/
- switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- return(SCI_BAD_CH);
- }
OS_ENTER_CRITICAL();
- if(pbuf->RingBufTxCtr
- {
- pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
- *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
- if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[SCI_TX_BUF_SIZE])
- {
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];/*WrapINpointer*/
- }
- if(pbuf->RingBufTxCtr==1)
- {/*Seeifthisisthefirstcharacter*/
- SCIEnableTxInt(port);/*Yes,EnableTxinterrupts*/
- OS_EXIT_CRITICAL();
- }
- else
- {
- OS_EXIT_CRITICAL();
- }
- return(SCI_NO_ERR);
- }
- else
- {
- OS_EXIT_CRITICAL();
- return(SCI_TX_FULL);
- }
- }
/**
- *Toinitializethecommunicationsmodule.
- *Youmustcallthisfunctionbeforecallinganyotherfunctions.
- */
- voidSCIBufferInit(void)
- {
- SCI_RING_BUF*pbuf;
- /*InitializetheringbufferforSCI0*/
- pbuf=&SCI0Buf;
- pbuf->RingBufRxCtr=0;
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufTxCtr=0;
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- /*InitializetheringbufferforSCI1*/
- pbuf=&SCI1Buf;
- pbuf->RingBufRxCtr=0;
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufTxCtr=0;
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- }
- /**
- *Toseeifanycharacterisavailablefromthecommunicationschannel.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnIfatleastonecharacterisavailable,thefunctionreturns
- *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
- */
- unsignedcharSCIBufferIsEmpty(unsignedcharport)
- {
- unsignedcharcpu_sr;
- unsignedcharempty;
- SCI_RING_BUF*pbuf;
- /*Obtainpointertocommunicationschannel*/
- switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- return(1);
- }
- OS_ENTER_CRITICAL();
- if(pbuf->RingBufRxCtr>0)/*Seeifbufferisempty*/
- {
- empty=0;/*BufferisNOTempty*/
- }
- else
- {
- empty=1;/*Bufferisempty*/
- }
- OS_EXIT_CRITICAL();
- return(empty);
- }
- /**
- *ToseeifanymorecharacterscanbeplacedintheTxbuffer.
- *Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnIfthebufferisfull,thefunctionreturnsTRUE
- *otherwise,thefunctionreturnsFALSE.
- */
- unsignedcharSCIBufferIsFull(unsignedcharport)
- {
- unsignedcharcpu_sr;
- charfull;
- SCI_RING_BUF*pbuf;
- /*Obtainpointertocommunicationschannel*/
- switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- return(1);
- }
- OS_ENTER_CRITICAL();
- if(pbuf->RingBufTxCtr
- {/*Seeifbufferisfull*/
- full=0;/*BufferisNOTfull*/
- }
- else
- {
- full=1;/*Bufferisfull*/
- }
- OS_EXIT_CRITICAL();
- return(full);
- }
- //ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
- staticvoidSCIPutRxChar(u